From b005d6a44a8e2f0934b2bf3a8346e7ac99929ae4 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Sat, 24 Oct 2026 15:53:26 -0500 Subject: [PATCH] fix(graph)!: fail boot when a node needs the token it provides (GitHub #43) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit topoSort validated each need with producedBy.has(key), and the visit skipped a dependency whose producer was the node itself. So a step with needs: ['user'] and provides: ['user'], and nothing else providing user, passed the graph check and booted. At request time it read a value nobody had written, where every other missing dependency fails at boot. The check now treats a node's own token as missing unless it is seeded. The issue also allowed "provided by another node", but since CARD-18 (GitHub #35) a token has exactly one producer, so a second node that provides it fails boot on its own; the self-producer is the only producer there can be. A seeded token (req, params) still satisfies the need, because the step reads the seed from the context before its output is merged over it. The visit keeps skipping the node itself for that case, so it is not reported as a cycle. overrides do not exempt the node. They replace a runner after finalizeGraph has run topoSort, and change no needs, so the graph check sees the same node either way. BREAKING CHANGE: an app with a node that needs a token only it provides now fails createApp with "missing dependency: needed by — it provides '' itself". Remove the token from the node's needs, or move the work that produces it into a separate provider or step. Refs: GitHub #43 Signed-off-by: Diego Resendez --- CHANGELOG.md | 10 ++++++++++ src/graph.ts | 12 ++++++++++-- test/graph.test.ts | 16 ++++++++++++++++ test/one-producer.test.ts | 11 +++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 708240b..a353098 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -115,6 +115,16 @@ npm treats versions as semver and semver forbids leading zeros. browsers from reusing a file for a lifetime they guessed from `Last-Modified`. To keep the old behaviour, set `static: { cacheControl }` to whatever your proxy expects. +- **A node that needs the token it provides fails `createApp`** (GitHub #43). The graph check + skipped a dependency whose producer was the node itself, so a step with `needs: ['user']` and + `provides: ['user']` booted, and at request time read a value nobody had written. It now fails + like any other missing dependency: `missing dependency: user needed by auth — it provides 'user' + itself`. Since a token has one producer (GitHub #35), no other node can supply it; only a seeded + token (`req`, `params`) still satisfies a node's own need, because the node reads the seed. + `overrides` do not exempt the node: they replace its runner after the graph is checked. To + migrate, remove the token from the node's `needs`, or move the work that produces it into a + separate provider or step the node needs. + ### Added - **Health probes: `createApp({ health: true })`** (#115). Off by default. `GET`/`HEAD /__live__` diff --git a/src/graph.ts b/src/graph.ts index 1d78407..ee6c836 100644 --- a/src/graph.ts +++ b/src/graph.ts @@ -83,8 +83,16 @@ export function topoSort(nodes: GraphNode[], seedKeys: string[], note = ''): Gra // validate dependencies exist for (const node of nodes) { for (const key of node.needs) { - if (!seedKeys.includes(key) && !producedBy.has(key)) { - throw new Error(`missing dependency: ${key} needed by ${node.name}${note}`); + if (seedKeys.includes(key)) continue; + const producer = producedBy.get(key); + + if (!producer) throw new Error(`missing dependency: ${key} needed by ${node.name}${note}`); + + // A node is not its own dependency (GitHub #43): it would read a value nobody wrote yet. + // `producers` already allows only one producer per token, so no other node can supply it; + // only a seed makes the need real, and the seed case returned above. + if (producer === node) { + throw new Error(`missing dependency: ${key} needed by ${node.name} — it provides '${key}' itself${note}`); } } } diff --git a/test/graph.test.ts b/test/graph.test.ts index 68ce059..73c5014 100644 --- a/test/graph.test.ts +++ b/test/graph.test.ts @@ -52,6 +52,22 @@ describe('one producer per token', () => { }); }); +// GitHub #43: a node that needs its own token reads a value nobody wrote. With one producer per +// token, nothing else can provide it either, so only a seed makes the need real. +describe('self-dependency', () => { + it('counts as missing when the token is not seeded', () => { + expect(() => topoSort([node('auth', ['user'], ['user'])], [])).toThrow( + /missing dependency: user needed by auth — it provides 'user' itself/, + ); + }); + + it('passes when the token is seeded, since the node reads the seed', () => { + expect(topoSort([node('normalize', ['params'], ['params'])], ['req', 'params']).map((n) => n.name)).toEqual([ + 'normalize', + ]); + }); +}); + const N = (name: string, needs: string[] = []) => ({ name, needs, provides: [name], origin: 'm' }); describe('subgraphFor', () => { diff --git a/test/one-producer.test.ts b/test/one-producer.test.ts index 6faf007..d07b445 100644 --- a/test/one-producer.test.ts +++ b/test/one-producer.test.ts @@ -83,6 +83,17 @@ describe('one producer per token', () => { await expect(createApp({ modules: [AppModule], plugins: [one, two] }).boot()).resolves.toBeUndefined(); }); + // GitHub #43. "Provided by another node" cannot rescue a self-dependency: a second producer of + // `user` is the CARD-18 failure above. And an override changes a runner, not the graph, so the + // check that rejects the node runs before any override could. + it('a node that needs the token it provides fails boot, with or without an override', () => { + const loop = adds('loop', { kind: 'step', name: 'loop', needs: ['loop'], provides: ['loop'] }); + expect(() => createApp({ modules: [AppModule], plugins: [loop] })).toThrow(/missing dependency: loop needed by loop/); + expect(() => createApp({ modules: [AppModule], plugins: [loop], overrides: { loop: 1 } })).toThrow( + /missing dependency: loop needed by loop/, + ); + }); + it('T5: overrides replace the runner of an existing token, not add a producer', async () => { const app = createApp({ modules: [AppModule], overrides: { user: { id: 1 } } }); const res = await app.fetch(new Request('http://x/u/me'));