fix(graph)!: fail boot when a node needs the token it provides (GitHub #43) #163

Merged
chrnx merged 2 commits from fix/graph-self-dependency into develop 2026-10-24 23:58:29 +00:00
Owner

Fixes GitHub #43.

topoSort skipped a dependency whose producer was the node itself, so a step with needs: ['user'] and provides: ['user'] booted and read, at request time, a value nobody had written. That need now counts as missing unless the token is seeded (req, params).

How it interacts with CARD-18 (GitHub #35). The issue says "unless the token is seeded or provided by another node". Since the one-producer rule, the second half cannot happen: another node providing the token fails boot as provided twice first. So the only exemption left is a seed. A seeded self-need (e.g. a step that needs and provides params) still boots, because the step reads the seed from the context before its output is merged over it.

Overrides. They do not exempt the node. applyOverrides runs after finalizeGraph, and only replaces a runner, so the graph check sees the same node. A test overriding a self-dependent node now fails boot too, which I think is right (production would fail the same way), but it is a judgement call worth a look.

Breaking. Apps with such a node now fail createApp. CHANGELOG entry at the end of ### Breaking, with the migration step: drop the token from the node's needs, or move the producing work into a separate provider or step.

Tests: test/graph.test.ts (self-dependency throws unless seeded) and test/one-producer.test.ts (app-level, with and without an override). Both failing tests failed on develop and pass with the fix.

Ran: lint (0 errors), format:check, typecheck, npm test (1163 passed), complexity:check, test:deno (63 passed), test:bun (52 passed), test:edge (PASS).

Fixes GitHub #43. `topoSort` skipped a dependency whose producer was the node itself, so a step with `needs: ['user']` and `provides: ['user']` booted and read, at request time, a value nobody had written. That need now counts as missing unless the token is seeded (`req`, `params`). **How it interacts with CARD-18 (GitHub #35).** The issue says "unless the token is seeded or provided by another node". Since the one-producer rule, the second half cannot happen: another node providing the token fails boot as `provided twice` first. So the only exemption left is a seed. A seeded self-need (e.g. a step that needs and provides `params`) still boots, because the step reads the seed from the context before its output is merged over it. **Overrides.** They do not exempt the node. `applyOverrides` runs after `finalizeGraph`, and only replaces a runner, so the graph check sees the same node. A test overriding a self-dependent node now fails boot too, which I think is right (production would fail the same way), but it is a judgement call worth a look. **Breaking.** Apps with such a node now fail `createApp`. CHANGELOG entry at the end of `### Breaking`, with the migration step: drop the token from the node's `needs`, or move the producing work into a separate provider or step. Tests: `test/graph.test.ts` (self-dependency throws unless seeded) and `test/one-producer.test.ts` (app-level, with and without an override). Both failing tests failed on develop and pass with the fix. Ran: lint (0 errors), format:check, typecheck, `npm test` (1163 passed), complexity:check, test:deno (63 passed), test:bun (52 passed), test:edge (PASS).
chrnx added 2 commits 2026-10-24 23:42:27 +00:00
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: <token> needed
  by <node> — it provides '<token>' 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 <diego.resendez@zero-oneit.com>
fix(graph): report a self-dependency as the cycle it is (GitHub #43)
CI / dco (pull_request) Successful in 4s
CI / build (pull_request) Successful in 29s
CI / runtimes (bun) (pull_request) Successful in 29s
CI / runtimes (edge) (pull_request) Successful in 19s
CI / runtimes (deno) (pull_request) Successful in 34s
0ba15b7862
A node that needs the token it provides is a cycle of one, so say so in
the same words as every other cycle (`cycle detected: a -> a`), and name
the token and the fix:

  cycle detected: auth -> auth (it needs 'user', which only it provides).
  Remove 'user' from its needs, or move the work that produces 'user'
  into a step it needs.

"missing dependency … it provides it itself" pointed the reader at a
missing producer, when the producer is there and the problem is the
loop.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx force-pushed fix/graph-self-dependency from 9420f3feeb to 0ba15b7862 2026-10-24 23:42:27 +00:00 Compare
chrnx merged commit e7f08052f4 into develop 2026-10-24 23:58:29 +00:00
Sign in to join this conversation.