feat(graph)!: fail boot when a token has a second producer (CARD-18, GitHub #35) #143

Merged
chrnx merged 1 commits from feature/fprd-001c-one-producer-per-token into develop 2026-10-23 05:51:53 +00:00
Owner

What and why

CARD-18, decision D9 (graph-wide, including mesh), GitHub Expressive-Tea/green-tea#35. The Gitea #35 is an unrelated, closed issue; there's no Gitea issue for this one.

topoSort/topoLevels/subgraphFor mapped tokens with producedBy.set(key, node), so the last node won. Plugin nodes are registered after module nodes, so a plugin's scope.add({ name: 'shadow', provides: ['user'] }) silently replaced the app's @Provider({ provides: 'user' }).

  • src/graph.ts: topoSort builds its map through a new producers() that throws on a second producer of a token, naming both nodes and origins:
    token 'user' is provided twice: by 'user' (module:AppModule) and by 'shadow' (plugin) — a token has exactly one producer; …
    topoLevels/subgraphFor only see topoSort-validated nodes, so they're unchanged (Option 1 in the card).
  • The check runs at finalize(), after the mesh splice, so remote steps (mesh:<url>) and built-ins (builtin) are checked by rule, not by registration order.
  • overrides add no node, so they're unaffected. Steps that provide nothing (plugin observers) aren't producers.
  • CHANGELOG.md: ### Breaking.

Tests

  • test/one-producer.test.ts: T1 (provider and step variants, exact message with both origins), T2 (logger, rooms, events), T3, T4, T5, two plugins sharing a token, observers, and T6 on Node (teacup local step auth + teapot exporting auth → duplicate provider/step name 'auth', unchanged).
  • test/graph.test.ts: order-independence (built-in first / plugin first both throw; covers "swap provideBuiltins and mountPlugins") plus a mesh: origin.
  • test/deno/mesh.test.ts, test/bun/mesh.test.ts: T6 on Deno and Bun.
  • Against the old src/graph.ts, all 10 T1–T3/order/mesh-origin cases fail.
  • T7: the green-tea-plugins develop checkout (jwt, metrics, rate-limit), with this branch's npm pack swapped in for @green-tea/core: npm test 60/60, test:deno 31/31, test:bun 31/31 (no workerd suite there). Matcha's scaffold templates (src/plugin_files.rs, src/cmd_create.rs) keep name = token and give each instance its own token. I checked them by inspection only and didn't run them.
Check Result
npm run lint 0 errors (50 existing no-explicit-any warnings)
npm run format:check pass
npm run typecheck pass
npm test 930 passed
npm run build pass
npm run complexity:check every function ≤ 25
npm run test:deno 37 passed, 0 failed
npm run test:bun 27 pass, 0 fail
npm run test:edge PASS (router parity 1158 + 55)

No frozen router-parity capture changed. Based on develop after #134/#135; npm audit --audit-level=moderate passes locally.

Unsure about / for the owner

  • Card rule 4 is not true today. The card says seeds (req, params) "cannot be provided by a node (already true; keep the test)". There's no such test, and a probe shows a @Step({ provides: 'req' }) and a plugin node with provides: ['params'] both boot. That isn't a one-producer conflict (seeds have no graph node), so this PR does not add that boot failure. It would be a new breaking rule beyond D9's text. Should it be added (here or as a follow-up)?
  • Error order: plugins mount before built-ins are registered, so T2 reports 'fake-logger' (plugin) first and 'logger' (builtin) second. Both are always named.
  • The error hint mentions overrides, which is an app option. It points app authors to the supported way to replace a value.

Docs follow-up (CARD-12)

  • Plugins guide, "What a plugin can and cannot touch": a plugin node can't provide a token anything else provides (including logger, rooms, events); boot fails naming both producers. Keep name = node name = token.
  • Migration note for ### Breaking, and mention overrides as the way to replace a token's value in tests.
## What and why CARD-18, decision D9 (graph-wide, including mesh), GitHub `Expressive-Tea/green-tea#35`. The Gitea `#35` is an unrelated, closed issue; there's no Gitea issue for this one. `topoSort`/`topoLevels`/`subgraphFor` mapped tokens with `producedBy.set(key, node)`, so the last node won. Plugin nodes are registered after module nodes, so a plugin's `scope.add({ name: 'shadow', provides: ['user'] })` silently replaced the app's `@Provider({ provides: 'user' })`. - `src/graph.ts`: `topoSort` builds its map through a new `producers()` that throws on a second producer of a token, naming both nodes and origins: `token 'user' is provided twice: by 'user' (module:AppModule) and by 'shadow' (plugin) — a token has exactly one producer; …` `topoLevels`/`subgraphFor` only see `topoSort`-validated nodes, so they're unchanged (Option 1 in the card). - The check runs at `finalize()`, after the mesh splice, so remote steps (`mesh:<url>`) and built-ins (`builtin`) are checked by rule, not by registration order. - `overrides` add no node, so they're unaffected. Steps that provide nothing (plugin observers) aren't producers. - `CHANGELOG.md`: `### Breaking`. ## Tests - `test/one-producer.test.ts`: T1 (provider and step variants, exact message with both origins), T2 (`logger`, `rooms`, `events`), T3, T4, T5, two plugins sharing a token, observers, and T6 on Node (teacup local step `auth` + teapot exporting `auth` → `duplicate provider/step name 'auth'`, unchanged). - `test/graph.test.ts`: order-independence (built-in first / plugin first both throw; covers "swap `provideBuiltins` and `mountPlugins`") plus a `mesh:` origin. - `test/deno/mesh.test.ts`, `test/bun/mesh.test.ts`: T6 on Deno and Bun. - Against the old `src/graph.ts`, all 10 T1–T3/order/mesh-origin cases fail. - **T7:** the `green-tea-plugins` develop checkout (jwt, metrics, rate-limit), with this branch's `npm pack` swapped in for `@green-tea/core`: `npm test` 60/60, `test:deno` 31/31, `test:bun` 31/31 (no workerd suite there). Matcha's scaffold templates (`src/plugin_files.rs`, `src/cmd_create.rs`) keep name = token and give each instance its own token. I checked them **by inspection only** and didn't run them. | Check | Result | |---|---| | `npm run lint` | 0 errors (50 existing `no-explicit-any` warnings) | | `npm run format:check` | pass | | `npm run typecheck` | pass | | `npm test` | 930 passed | | `npm run build` | pass | | `npm run complexity:check` | every function ≤ 25 | | `npm run test:deno` | 37 passed, 0 failed | | `npm run test:bun` | 27 pass, 0 fail | | `npm run test:edge` | PASS (router parity 1158 + 55) | No frozen router-parity capture changed. Based on develop after #134/#135; `npm audit --audit-level=moderate` passes locally. ## Unsure about / for the owner - **Card rule 4 is not true today.** The card says seeds (`req`, `params`) "cannot be provided by a node (already true; keep the test)". There's no such test, and a probe shows a `@Step({ provides: 'req' })` and a plugin node with `provides: ['params']` both boot. That isn't a one-producer conflict (seeds have no graph node), so this PR **does not** add that boot failure. It would be a new breaking rule beyond D9's text. Should it be added (here or as a follow-up)? - Error order: plugins mount before built-ins are registered, so T2 reports `'fake-logger' (plugin)` first and `'logger' (builtin)` second. Both are always named. - The error hint mentions `overrides`, which is an app option. It points app authors to the supported way to replace a value. ## Docs follow-up (CARD-12) - Plugins guide, "What a plugin can and cannot touch": a plugin node can't provide a token anything else provides (including `logger`, `rooms`, `events`); boot fails naming both producers. Keep name = node name = token. - Migration note for `### Breaking`, and mention `overrides` as the way to replace a token's value in tests.
chrnx force-pushed feature/fprd-001c-one-producer-per-token from 3757c9bc14 to 0b80df9e3a 2026-10-23 05:02:50 +00:00 Compare
Author
Owner

Owner decision (2026-10-23): card rule 4 (seeds cannot be provided) is dropped from CARD-18. It isn't true today, and enforcing it would be a new breaking rule beyond D9. Not merging yet: implementation review first.

Owner decision (2026-10-23): card rule 4 (seeds cannot be provided) is **dropped** from CARD-18. It isn't true today, and enforcing it would be a new breaking rule beyond D9. Not merging yet: implementation review first.
chrnx added 1 commit 2026-10-23 05:50:30 +00:00
feat(graph)!: fail boot when a token has a second producer (CARD-18, GitHub #35)
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 15s
CI / build (pull_request) Successful in 26s
CI / runtimes (deno) (pull_request) Successful in 19s
CI / runtimes (edge) (pull_request) Successful in 14s
56f953f0bb
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

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx force-pushed feature/fprd-001c-one-producer-per-token from 0b80df9e3a to 56f953f0bb 2026-10-23 05:50:30 +00:00 Compare
chrnx merged commit 444910b6f0 into develop 2026-10-23 05:51:53 +00:00
chrnx deleted branch feature/fprd-001c-one-producer-per-token 2026-10-23 05:51:55 +00:00
Sign in to join this conversation.