feat(graph)!: fail boot when a token has a second producer (CARD-18, GitHub #35)
topoSort, topoLevels and subgraphFor built their token -> node map with
producedBy.set(key, node), so the last node registered for a token won.
Plugin nodes are registered after module nodes, which let a plugin's
scope.add({ name: 'shadow', provides: ['user'] }) silently replace the
app's own @Provider({ provides: 'user' }) for every route that needed
it. setRunner only rejects duplicate names, and scope.add is the one
path where a name and its tokens differ.
D9 settles it graph-wide: topoSort now throws when a token already has
a producer, naming both nodes and their origins (module:..., plugin,
mesh:<url>, builtin). One check on the whole node set covers every
current and future registration path. It also protects the built-ins
(logger, rooms, events) by rule rather than by registration order.
topoSort runs at finalize, after the mesh splice, so remote steps are
checked too. topoLevels and subgraphFor only ever see nodes topoSort
has already validated, so they keep their plain maps.
overrides replace the runner of an existing token and add no node, so
they are unaffected. Steps that provide nothing (plugin observers) are
not producers. The official plugins (jwt, metrics, rate-limit) pass
their suites against this build.
BREAKING CHANGE: an app whose graph has two nodes providing one token,
e.g. a plugin scope node providing a token a module or built-in
already provides, now fails createApp instead of letting the later
node win.
Refs: GitHub #35
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
6 files changed
+275
-3
No files matched your search
@@ -24,6 +24,34 @@ test('throws on cycle', () => {
|
||||
expect(() => topoSort(nodes, [])).toThrow(/cycle detected/);
|
||||
});
|
||||
|
||||
// CARD-18 (GitHub #35, D9): the check is a rule over the whole node set, not an artefact of the
|
||||
// order nodes were registered in — so swapping `provideBuiltins` and `mountPlugins` cannot reopen it.
|
||||
describe('one producer per token', () => {
|
||||
const builtin: GraphNode = { name: 'logger', needs: [], provides: ['logger'], origin: 'builtin' };
|
||||
const plugin: GraphNode = { name: 'fake', needs: [], provides: ['logger'], origin: 'plugin' };
|
||||
|
||||
it.each([
|
||||
['built-in first', [builtin, plugin]],
|
||||
['plugin first', [plugin, builtin]],
|
||||
])('throws naming both producers and origins (%s)', (_, nodes) => {
|
||||
expect(() => topoSort(nodes, [])).toThrow(/token 'logger' is provided twice/);
|
||||
expect(() => topoSort(nodes, [])).toThrow(/'logger' \(builtin\)/);
|
||||
expect(() => topoSort(nodes, [])).toThrow(/'fake' \(plugin\)/);
|
||||
});
|
||||
|
||||
it('names a mesh origin like any other', () => {
|
||||
const local: GraphNode = { name: 'auth', needs: [], provides: ['auth'], origin: 'module:App' };
|
||||
const remote: GraphNode = { name: 'auth-remote', needs: [], provides: ['auth'], origin: 'mesh:ws://t' };
|
||||
expect(() => topoSort([local, remote], [])).toThrow(
|
||||
"token 'auth' is provided twice: by 'auth' (module:App) and by 'auth-remote' (mesh:ws://t)",
|
||||
);
|
||||
});
|
||||
|
||||
it('allows a node to list its own token twice', () => {
|
||||
expect(topoSort([node('a', [], ['a', 'a'])], []).map((n) => n.name)).toEqual(['a']);
|
||||
});
|
||||
});
|
||||
|
||||
const N = (name: string, needs: string[] = []) => ({ name, needs, provides: [name], origin: 'm' });
|
||||
|
||||
describe('subgraphFor', () => {
|
||||
|
||||
Reference in new issue
Block a user