feat(mesh): remote routes follow teapot membership (CARD-19, #124) #146

Merged
chrnx merged 2 commits from feature/fprd-001c-mesh-membership into develop 2026-10-23 06:08:11 +00:00
Owner

Depends on #137 (CARD-20). Review and merge it first. This branch is stacked on it, so the diff shows #137's two commits until it merges.

What and why

CARD-19 (FPRD-001c), issue #124, decision D15 (accepted 2026-10-18). The teacup built its HTTP route table once at boot. A teapot that started late was never served, and a teapot that was gone for good stayed advertised. Remote routes now follow membership:

State Before Now
Teapot absent at boot, connects later 404 forever retried in the background with the reconnect backoff. Routes added on connect (no restart). Steps not added: the graph is fixed at boot
Link reconnecting 503, Allow keeps it same, plus retry-after (seconds to the next attempt, ≥ 1)
Gone for good (reconnect: false and dropped, or a permanent refusal while reconnecting) 503 forever routes removed: 404, Allow drops their methods
Returning manifest adds a route ignored served
Returning manifest drops a route whole reconnect refused route removed. Steps still refused (onManifestChange: 'refuse')
Late route conflicting with another teapot's n/a refused for that teapot and logged. The table keeps serving
Late/returning route on a reserved path (/__mesh__/control; /__live__, /__ready__ with health on) n/a refused and logged; the teapot's other routes are served (at boot: still fails boot, as in #141/#145)

Every change emits mesh:routes with { url, added, removed } (name is the url too, since EventPayload.name is required). No new MeshConfig key.

How

  • src/mesh/link.ts: new onReconnect(manifest) / onGone() callbacks. On a returning session the link keeps the boot steps and takes the new routes. missingFromManifest checks steps only. A drop with reconnect: false after a successful connect, or a permanent refusal during reconnect, stops the link and calls onGone. 503s from a down link carry retry-after while a retry is scheduled.
  • src/app/index.ts: createRemoteTable keeps routes per teapot (in config order) and publishes a new array on every change (copy-on-write). At boot a conflict still throws with the same messages (the boot-mesh.json capture is unchanged). After boot it is logged and skipped. The reserved-path check from #141/#145 (assertNotReserved with the same reserved set) now runs inside set(), so it covers late and returning teapots as well as boot (review finding). retryLateTeapot does the background retry for absent teapots and is closed with the other links on shutdown. app.fetch and listen() now share one table through createRouteTable.
  • src/http/web.ts / server.ts: buildFetch / createHttpServer accept RouteDef[] | () => RouteDef[] and read it once per request, so a request routes and builds Allow against one table.
  • src/http/router.ts: local trees are reused when the local (def, position) entries are identical (localTreeCache), so a membership change rebuilds only the remote trees. routerSeamCounts().localTreeBuilds is the test seam.

Commits

  1. feat(mesh): remote routes follow teapot membership (CARD-19, #124): code, tests, CHANGELOG ### Changed (mesh, alpha, so not Breaking per #111), MeshConfig JSDoc.
  2. test(router-parity): a late teapot is served after it connects for CARD-19 (owner approval required): needs owner approval (CARD-08 rule).
    • owner-mesh-lifetime.json: only B2 changes. GET /api/remote/ping goes 404 → 200 {"from":"teapot"}, and POST goes 404 → 405 with Allow: GET, HEAD, OPTIONS. A1, A2 and B1 are byte-identical.
    • The fixture's teacup changes from reconnect: false to reconnect: { initialDelayMs: 20, maxDelayMs: 60 }. Under D15, reconnect: false would make A2 "gone" (404) and would not retry B, and the card describes A2 as 503 and B2 as 200, which is the reconnecting teacup. B2 waits for mesh:routes before probing.
    • The fixture comment and one README line are updated.

Tests (card cases)

test/mesh/membership.test.ts (Node):

  • T1 absent at boot → 404 → teapot starts → mesh:routes {added:['GET /api/remote/ping']} → 200 on app.fetch and the Node listener. Local tree not rebuilt (localTreeBuilds unchanged, indexBuilds grew).
  • T2/T3 killed with reconnect on → 503 + retry-after ≥ 1. OPTIONS and a 405 still list GET, HEAD, OPTIONS. Back → 200, no event (same routes).
  • T4 reconnect: false, killed → mesh:routes {removed} → 404 with no retry-after, OPTIONS 404 with no Allow, local route 200.
  • T5 returns with an extra route → served. T6 returns without a route → 404, the other still 200.
  • T7 late GET /api/shape/:name vs another teapot's /api/shape/:id → refused and logged. The other teapot serves, and the late teapot's non-conflicting route is served.
  • T8 1 000 requests across a late connect: every answer is 404 (old table) or 200 (new), and both are seen.
  • reconnect: false + absent at boot → not retried.
  • A late raw-protocol teapot advertising GET /__mesh__/control + GET /api/fake/ok: the reserved route is refused and logged, and /api/fake/ok is served. Same with health: true and GET /__ready__. Both tests fail without the ported check.
  • T9: Deno and Bun cases for T1 and T2 in test/{deno,bun}/mesh-reconnect.test.ts.
  • test/mesh/reconnect.test.ts: the missingFromManifest route case now expects [] (D15).

Evidence (local, Node 22, rebased on develop at 1da6bf2: #136, #139, #141, #143, #145 merged)

  • npm run lint: 0 errors, 52 warnings (all pre-existing no-explicit-any)
  • npm run format:check: clean
  • npm run typecheck: clean
  • npm test: 1001 passed (3 consecutive full runs)
  • npm run build: ok
  • npm run complexity:check: every function ≤ 25
  • npm run test:deno: 42 passed, 0 failed
  • npm run test:bun: 32 pass, 0 fail
  • npm run test:edge: PASS (incl. reserved path + health; 1158 matcher + 55 app.fetch cases). Mesh on workerd is not claimed, per the card.
  • npm audit --audit-level=moderate: 0 vulnerabilities

Unsure / decisions I made (please check)

  1. Late retry with reconnect: false. Rule 1 says "retried with the reconnect backoff". With reconnect: false there is no backoff, so an absent teapot is not retried (today's behaviour). This is the conservative reading.
  2. A permanent refusal while reconnecting now stops the link (rule 3). Before, it retried forever. It uses the existing isPermanentRefusal classification: the socket opened, then hung up before the manifest. A teapot that accepts the upgrade and hangs up mid-shutdown would look the same and be given up on.
  3. Rule 5 granularity. Only the conflicting route is refused. The late teapot's other routes are served.
  4. Dev routes moved before remote routes in the shared table, so local positions stay stable. The only observable effect: if a teapot exports a route that ties at the same specificity with devGraph/devOpenapi's path, the dev route now wins instead of the remote one.
  5. retry-after is not part of the frozen capture (the probe records status, Allow and body only). It is asserted in unit tests instead of widening the corpus.
  6. Pre-existing bug found, not fixed (out of scope): with more than ~16 RPCs in flight on one link, the teapot's control channel hits the WebSocket inbound-queue bound (#107, maxWsQueue 16) and the link drops. On develop + #137, 100 concurrent requests to one remote route all answer 503. T8 keeps its waves at 10 concurrent for that reason. This probably deserves its own issue. #142 (CARD-24) bounds the control channel's message size, not its queue.
  7. On #138 (flaky EADDRINUSE in owner.test.ts): I didn't change the port handling. The new B2 wait is event-driven, not timed.

Docs follow-up (CARD-12)

guides/mesh.md:

  • late teapots are retried in the background (not with reconnect: false). Routes are served on connect, steps are not.
  • reconnecting → 503 + retry-after. Gone for good → 404 and out of Allow.
  • a returning manifest's routes replace the old ones. Steps keep 'refuse'.
  • mesh:routes event { url, added, removed }.
  • a late conflicting route, or one on a reserved path, is refused and logged.
**Depends on #137 (CARD-20). Review and merge it first.** This branch is stacked on it, so the diff shows #137's two commits until it merges. ## What and why CARD-19 (FPRD-001c), issue #124, decision **D15** (accepted 2026-10-18). The teacup built its HTTP route table once at boot. A teapot that started late was never served, and a teapot that was gone for good stayed advertised. Remote routes now follow membership: | State | Before | Now | |---|---|---| | Teapot absent at boot, connects later | 404 forever | retried in the background with the `reconnect` backoff. Routes added on connect (no restart). Steps not added: the graph is fixed at boot | | Link reconnecting | 503, `Allow` keeps it | same, plus `retry-after` (seconds to the next attempt, ≥ 1) | | Gone for good (`reconnect: false` and dropped, or a permanent refusal while reconnecting) | 503 forever | routes removed: 404, `Allow` drops their methods | | Returning manifest adds a route | ignored | served | | Returning manifest drops a route | whole reconnect refused | route removed. Steps still refused (`onManifestChange: 'refuse'`) | | Late route conflicting with another teapot's | n/a | refused for that teapot and logged. The table keeps serving | | Late/returning route on a reserved path (`/__mesh__/control`; `/__live__`, `/__ready__` with `health` on) | n/a | refused and logged; the teapot's other routes are served (at boot: still fails boot, as in #141/#145) | Every change emits `mesh:routes` with `{ url, added, removed }` (`name` is the url too, since `EventPayload.name` is required). No new `MeshConfig` key. ## How - **`src/mesh/link.ts`**: new `onReconnect(manifest)` / `onGone()` callbacks. On a returning session the link keeps the boot steps and takes the new routes. `missingFromManifest` checks steps only. A drop with `reconnect: false` after a successful connect, or a permanent refusal during reconnect, stops the link and calls `onGone`. 503s from a down link carry `retry-after` while a retry is scheduled. - **`src/app/index.ts`**: `createRemoteTable` keeps routes per teapot (in config order) and publishes a **new array** on every change (copy-on-write). At boot a conflict still throws with the same messages (the `boot-mesh.json` capture is unchanged). After boot it is logged and skipped. **The reserved-path check from #141/#145 (`assertNotReserved` with the same `reserved` set) now runs inside `set()`**, so it covers late and returning teapots as well as boot (review finding). `retryLateTeapot` does the background retry for absent teapots and is closed with the other links on shutdown. `app.fetch` and `listen()` now share one table through `createRouteTable`. - **`src/http/web.ts` / `server.ts`**: `buildFetch` / `createHttpServer` accept `RouteDef[] | () => RouteDef[]` and read it **once per request**, so a request routes and builds `Allow` against one table. - **`src/http/router.ts`**: local trees are reused when the local `(def, position)` entries are identical (`localTreeCache`), so a membership change rebuilds only the remote trees. `routerSeamCounts().localTreeBuilds` is the test seam. ## Commits 1. `feat(mesh): remote routes follow teapot membership (CARD-19, #124)`: code, tests, CHANGELOG `### Changed` (mesh, alpha, so not Breaking per #111), `MeshConfig` JSDoc. 2. `test(router-parity): a late teapot is served after it connects for CARD-19 (owner approval required)`: **needs owner approval (CARD-08 rule).** - `owner-mesh-lifetime.json`: only B2 changes. GET `/api/remote/ping` goes 404 → 200 `{"from":"teapot"}`, and POST goes 404 → 405 with `Allow: GET, HEAD, OPTIONS`. A1, A2 and B1 are byte-identical. - The fixture's teacup changes from `reconnect: false` to `reconnect: { initialDelayMs: 20, maxDelayMs: 60 }`. Under D15, `reconnect: false` would make A2 "gone" (404) and would not retry B, and the card describes A2 as 503 and B2 as 200, which is the reconnecting teacup. B2 waits for `mesh:routes` before probing. - The fixture comment and one README line are updated. ## Tests (card cases) `test/mesh/membership.test.ts` (Node): - T1 absent at boot → 404 → teapot starts → `mesh:routes {added:['GET /api/remote/ping']}` → 200 on `app.fetch` and the Node listener. **Local tree not rebuilt** (`localTreeBuilds` unchanged, `indexBuilds` grew). - T2/T3 killed with reconnect on → 503 + `retry-after ≥ 1`. `OPTIONS` and a 405 still list `GET, HEAD, OPTIONS`. Back → 200, no event (same routes). - T4 `reconnect: false`, killed → `mesh:routes {removed}` → 404 with no `retry-after`, `OPTIONS` 404 with no `Allow`, local route 200. - T5 returns with an extra route → served. T6 returns without a route → 404, the other still 200. - T7 late `GET /api/shape/:name` vs another teapot's `/api/shape/:id` → refused and logged. The other teapot serves, and the late teapot's non-conflicting route is served. - T8 1 000 requests across a late connect: every answer is 404 (old table) or 200 (new), and both are seen. - `reconnect: false` + absent at boot → not retried. - A late raw-protocol teapot advertising `GET /__mesh__/control` + `GET /api/fake/ok`: the reserved route is refused and logged, and `/api/fake/ok` is served. Same with `health: true` and `GET /__ready__`. Both tests fail without the ported check. - T9: Deno and Bun cases for T1 and T2 in `test/{deno,bun}/mesh-reconnect.test.ts`. - `test/mesh/reconnect.test.ts`: the `missingFromManifest` route case now expects `[]` (D15). ## Evidence (local, Node 22, rebased on develop at 1da6bf2: #136, #139, #141, #143, #145 merged) - `npm run lint`: 0 errors, 52 warnings (all pre-existing `no-explicit-any`) - `npm run format:check`: clean - `npm run typecheck`: clean - `npm test`: 1001 passed (3 consecutive full runs) - `npm run build`: ok - `npm run complexity:check`: every function ≤ 25 - `npm run test:deno`: 42 passed, 0 failed - `npm run test:bun`: 32 pass, 0 fail - `npm run test:edge`: PASS (incl. reserved path + health; 1158 matcher + 55 app.fetch cases). Mesh on workerd is not claimed, per the card. - `npm audit --audit-level=moderate`: 0 vulnerabilities ## Unsure / decisions I made (please check) 1. **Late retry with `reconnect: false`.** Rule 1 says "retried with the reconnect backoff". With `reconnect: false` there is no backoff, so an absent teapot is **not** retried (today's behaviour). This is the conservative reading. 2. **A permanent refusal while reconnecting now stops the link** (rule 3). Before, it retried forever. It uses the existing `isPermanentRefusal` classification: the socket opened, then hung up before the manifest. A teapot that accepts the upgrade and hangs up mid-shutdown would look the same and be given up on. 3. **Rule 5 granularity.** Only the conflicting route is refused. The late teapot's other routes are served. 4. **Dev routes moved before remote routes** in the shared table, so local positions stay stable. The only observable effect: if a teapot exports a route that ties at the same specificity with `devGraph`/`devOpenapi`'s path, the dev route now wins instead of the remote one. 5. `retry-after` is not part of the frozen capture (the probe records status, `Allow` and body only). It is asserted in unit tests instead of widening the corpus. 6. **Pre-existing bug found, not fixed (out of scope):** with more than ~16 RPCs in flight on one link, the teapot's control channel hits the WebSocket inbound-queue bound (#107, `maxWsQueue` 16) and the link drops. On develop + #137, 100 concurrent requests to one remote route all answer 503. T8 keeps its waves at 10 concurrent for that reason. This probably deserves its own issue. #142 (CARD-24) bounds the control channel's message size, not its queue. 7. On #138 (flaky EADDRINUSE in `owner.test.ts`): I didn't change the port handling. The new B2 wait is event-driven, not timed. ## Docs follow-up (CARD-12) `guides/mesh.md`: - late teapots are retried in the background (not with `reconnect: false`). Routes are served on connect, steps are not. - reconnecting → 503 + `retry-after`. Gone for good → 404 and out of `Allow`. - a returning manifest's routes replace the old ones. Steps keep `'refuse'`. - `mesh:routes` event `{ url, added, removed }`. - a late conflicting route, or one on a reserved path, is refused and logged.
Author
Owner

Owner decisions (2026-10-23): frozen capture change in 044ef34 (owner-mesh-lifetime.json) approved. Questions 1–4 accepted as implemented. Mesh in-flight bug filed as #148, being fixed in #142. Not merging yet: implementation review first.

Owner decisions (2026-10-23): frozen capture change in 044ef34 (`owner-mesh-lifetime.json`) **approved**. Questions 1–4 accepted as implemented. Mesh in-flight bug filed as #148, being fixed in #142. Not merging yet: implementation review first.
Author
Owner

Implementation review done on 2026-10-23: a Codex review of all 12 sprint PRs, with each finding verified. The P1s in #142 and the P3 in #149 are fixed. The owner (Chrnx) authorized merging in order, each one rebased on develop with CI green. This supersedes the earlier "not merging yet" note.

Implementation review done on 2026-10-23: a Codex review of all 12 sprint PRs, with each finding verified. The P1s in #142 and the P3 in #149 are fixed. The owner (Chrnx) authorized merging in order, each one rebased on develop with CI green. This supersedes the earlier "not merging yet" note.
chrnx force-pushed feature/fprd-001c-mesh-membership from 044ef34d17 to 3054b465fd 2026-10-23 06:03:05 +00:00 Compare
chrnx added 2 commits 2026-10-23 06:04:42 +00:00
The teacup built its route table once at boot. A teapot that started
late was never served, and one that was gone for good stayed advertised.
Implement D15 (accepted 2026-10-18):

- A teapot that missed bootTimeoutMs is retried in the background with
  the reconnect backoff (not with reconnect: false). On connect its
  routes join the remote table. Its steps do not, because the graph
  was validated at boot without them.
- While a link is reconnecting, its routes stay and answer 503, now
  with retry-after (seconds until the next attempt).
- A link that gives up for good (dropped with reconnect: false, or a
  permanent refusal while reconnecting) has its routes removed: 404,
  and Allow drops its methods. A refused reconnect no longer retries
  forever.
- A returning manifest replaces the teapot's routes. missingFromManifest
  now checks steps only, so a dropped route is removed instead of
  refusing the reconnect.
- A late route that conflicts with another teapot's is refused and
  logged. The table keeps serving. At boot the same conflict still
  fails boot.
- Each change emits mesh:routes with { url, added, removed }.
- The reserved-path check from CARD-21/CARD-17 (#141, #145) runs in
  the table for every remote route: at boot a route on /__mesh__/control
  (or /__live__ and /__ready__ with health on) fails boot as before. A
  late or returning one is refused and logged, and the teapot's other
  routes are served.

Mechanics: the remote table publishes a new array per change
(copy-on-write). app.fetch and listen() share one table through a
getter, and buildFetch/createHttpServer read it once per request, so a
request sees the old table or the new one, never a mix. The router
reuses local trees when the local entries are identical, so a
membership change rebuilds only the remote trees. Dev routes now sit
before remote ones in the table to keep the local positions stable.

Tests cover T1-T8 on Node (test/mesh/membership.test.ts), plus the
local-tree build counter and late reserved-path routes, and T1/T2 on
Deno and Bun.

The frozen owner-mesh-lifetime.json capture changes in the next commit.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
test(router-parity): a late teapot is served after it connects for CARD-19 (owner approval required)
CI / runtimes (bun) (pull_request) Successful in 16s
CI / dco (pull_request) Successful in 4s
CI / build (pull_request) Successful in 26s
CI / runtimes (deno) (pull_request) Successful in 20s
CI / runtimes (edge) (pull_request) Successful in 14s
9ea1365b66
owner-mesh-lifetime.json froze phase B2 (teapot absent at boot, then up)
at 404 for GET and POST /api/remote/ping. The table was built once at
boot, so a late teapot was never spliced in. CARD-19 (#124, D15) changes
that. B2 now answers GET 200 {"from":"teapot"} and POST 405 with
Allow: GET, HEAD, OPTIONS, the same as A1.

Only those two entries change. A1, A2 and B1 are identical.

The fixture's teacup goes from reconnect: false to reconnect with a fast
backoff (20-60ms). Under D15, reconnect: false makes a dropped teapot
"gone" (404), and an absent one is not retried. The card's phases
describe the reconnecting teacup: A2 stays 503 and B2 becomes 200. B2
now waits for the mesh:routes event before probing. The fixture comment
and the corpus README line describing "built once at boot" are updated.

Owner approval required (CARD-08 rule): this edits a frozen file and the
fixture under test/router-parity/.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx force-pushed feature/fprd-001c-mesh-membership from 3054b465fd to 9ea1365b66 2026-10-23 06:04:42 +00:00 Compare
chrnx merged commit 037e695a31 into develop 2026-10-23 06:08:11 +00:00
chrnx deleted branch feature/fprd-001c-mesh-membership 2026-10-23 06:08:13 +00:00
Sign in to join this conversation.