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

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 }.

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 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>
This commit is contained in:
chrnx committed 2026-10-23 00:07:48 -05:00
1 parent 3b124fb05b
commit 15f8acc346
13 files changed
+1086 -177

No files matched your search

+88
View File
@@ -99,3 +99,91 @@ test('mesh on Bun: a teacup reconnects to a teapot that came back', async () =>
await teapot.stop();
}
});
// CARD-19 (#124, D15): remote routes follow teapot membership on Bun too.
@Route('/remote')
class PingCtl {
@Get('/ping', { export: true })
ping() {
return { from: 'teapot' };
}
}
@Module({ mountpoint: '/api', controllers: [PingCtl] })
class PingTeapotModule {}
@Module({ mountpoint: '/api', controllers: [] })
class BareTeacupModule {}
async function startPingTeapot(port: number) {
const app = createApp({ modules: [PingTeapotModule], experimental: true, mesh: { secret: SECRET } });
const server = await serveBun(app, { port });
return {
stop: async () => {
server.stop(true);
await app.close();
},
};
}
/** Polls `GET path` until it answers `status`, or fails after `ms`. */
async function untilStatus(app: ReturnType<typeof createApp>, path: string, status: number, ms = 3000) {
const deadline = Date.now() + ms;
let last = 0;
while (Date.now() < deadline) {
const res = await app.fetch(new Request(`http://x${path}`));
await res.text();
last = res.status;
if (last === status) return;
await settle(25);
}
throw new Error(`${path} answered ${last}, expected ${status}`);
}
const pingTeacup = (port: number) =>
createApp({
modules: [BareTeacupModule],
experimental: true,
mesh: {
teapots: [{ url: `ws://127.0.0.1:${port}/__mesh__/control`, secret: SECRET }],
reconnect: { initialDelayMs: 20, maxDelayMs: 60 },
bootTimeoutMs: 100,
heartbeatMs: 50,
timeoutMs: 2000,
},
});
test('mesh on Bun: a teapot absent at boot has its routes served once it connects (CARD-19 T1)', async () => {
const port = freePort();
const teacup = pingTeacup(port);
let teapot: Awaited<ReturnType<typeof startPingTeapot>> | undefined;
try {
expect((await teacup.fetch(new Request('http://x/api/remote/ping'))).status).toBe(404);
teapot = await startPingTeapot(port);
await untilStatus(teacup, '/api/remote/ping', 200);
} finally {
await teacup.close();
await teapot?.stop();
}
});
test('mesh on Bun: a reconnecting teapot keeps its routes with 503 + retry-after (CARD-19 T2)', async () => {
const port = freePort();
const teapot = await startPingTeapot(port);
const teacup = pingTeacup(port);
try {
await untilStatus(teacup, '/api/remote/ping', 200);
await teapot.stop();
await untilStatus(teacup, '/api/remote/ping', 503);
const down = await teacup.fetch(new Request('http://x/api/remote/ping'));
expect(down.status).toBe(503);
expect(Number(down.headers.get('retry-after'))).toBeGreaterThanOrEqual(1);
const options = await teacup.fetch(new Request('http://x/api/remote/ping', { method: 'OPTIONS' }));
expect(options.headers.get('allow')).toBe('GET, HEAD, OPTIONS');
} finally {
await teacup.close();
}
});
+93
View File
@@ -99,3 +99,96 @@ Deno.test('mesh on Deno: a teacup reconnects to a teapot that came back', async
await teapot.stop();
}
});
// CARD-19 (#124, D15): remote routes follow teapot membership on Deno too.
@Route('/remote')
class PingCtl {
@Get('/ping', { export: true })
ping() {
return { from: 'teapot' };
}
}
@Module({ mountpoint: '/api', controllers: [PingCtl] })
class PingTeapotModule {}
@Module({ mountpoint: '/api', controllers: [] })
class BareTeacupModule {}
async function startPingTeapot(port: number) {
const app = createApp({ modules: [PingTeapotModule], experimental: true, mesh: { secret: SECRET } });
const server = await serveDeno(app, { port });
await new Promise((r) => setTimeout(r, 50));
return {
stop: async () => {
await server.shutdown();
await app.close();
},
};
}
/** Polls `GET path` until it answers `status`, or fails after `ms`. */
async function untilStatus(app: ReturnType<typeof createApp>, path: string, status: number, ms = 3000) {
const deadline = Date.now() + ms;
let last = 0;
while (Date.now() < deadline) {
const res = await app.fetch(new Request(`http://x${path}`));
await res.body?.cancel();
last = res.status;
if (last === status) return;
await settle(25);
}
throw new Error(`${path} answered ${last}, expected ${status}`);
}
const pingTeacup = (port: number) =>
createApp({
modules: [BareTeacupModule],
experimental: true,
mesh: {
teapots: [{ url: `ws://127.0.0.1:${port}/__mesh__/control`, secret: SECRET }],
reconnect: { initialDelayMs: 20, maxDelayMs: 60 },
bootTimeoutMs: 100,
heartbeatMs: 50,
timeoutMs: 2000,
},
});
Deno.test('mesh on Deno: a teapot absent at boot has its routes served once it connects (CARD-19 T1)', async () => {
const port = freePort();
const teacup = pingTeacup(port);
let teapot: Awaited<ReturnType<typeof startPingTeapot>> | undefined;
try {
const before = await teacup.fetch(new Request('http://x/api/remote/ping'));
await before.body?.cancel();
assertEquals(before.status, 404);
teapot = await startPingTeapot(port);
await untilStatus(teacup, '/api/remote/ping', 200);
} finally {
await teacup.close();
await teapot?.stop();
}
});
Deno.test('mesh on Deno: a reconnecting teapot keeps its routes with 503 + retry-after (CARD-19 T2)', async () => {
const port = freePort();
const teapot = await startPingTeapot(port);
const teacup = pingTeacup(port);
try {
await untilStatus(teacup, '/api/remote/ping', 200);
await teapot.stop();
await untilStatus(teacup, '/api/remote/ping', 503);
const down = await teacup.fetch(new Request('http://x/api/remote/ping'));
await down.body?.cancel();
assertEquals(down.status, 503);
assertEquals(Number(down.headers.get('retry-after')) >= 1, true);
const options = await teacup.fetch(new Request('http://x/api/remote/ping', { method: 'OPTIONS' }));
assertEquals(options.headers.get('allow'), 'GET, HEAD, OPTIONS');
} finally {
await teacup.close();
}
});
+341
View File
@@ -0,0 +1,341 @@
/**
* CARD-19 (#124, D15): a teacup's remote routes follow which teapots are connected.
*
* Late teapot → its routes are added. Reconnecting → they stay and answer 503 with `retry-after`.
* Gone for good → they are removed (404, `Allow` without them). A returning manifest adds and drops
* routes. A late route that conflicts is refused and logged. Every change emits `mesh:routes`, and
* none rebuilds the local route tree.
*/
import 'reflect-metadata';
import http from 'http';
import net from 'net';
import type { AddressInfo } from 'net';
import { describe, it, expect, afterEach } from 'vitest';
import { createApp, Route, Get, Module } from '../../src/index';
import { routerSeamCounts } from '../../src/http/router';
import type { App, MeshConfig } from '../../src/app/types';
import type { EventPayload } from '../../src/bus';
const SECRET = 'membership-secret';
@Route('/remote')
class PingCtl {
@Get('/ping', { export: true }) ping() {
return { from: 'teapot' };
}
}
@Module({ mountpoint: '/api', controllers: [PingCtl] })
class PingTeapot {}
@Route('/remote')
class PingExtraCtl {
@Get('/ping', { export: true }) ping() {
return { from: 'teapot v2' };
}
@Get('/extra', { export: true }) extra() {
return { extra: true };
}
}
@Module({ mountpoint: '/api', controllers: [PingExtraCtl] })
class PingExtraTeapot {}
@Route('/remote')
class ExtraOnlyCtl {
@Get('/extra', { export: true }) extra() {
return { extra: true };
}
}
@Module({ mountpoint: '/api', controllers: [ExtraOnlyCtl] })
class ExtraOnlyTeapot {}
@Route('/shape')
class ShapeIdCtl {
@Get('/:id', { export: true }) get() {
return { from: 'id' };
}
}
@Module({ mountpoint: '/api', controllers: [ShapeIdCtl] })
class ShapeIdTeapot {}
@Route('/')
class ShapeNameCtl {
@Get('/shape/:name', { export: true }) get() {
return { from: 'name' };
}
@Get('/other', { export: true }) other() {
return { from: 'other' };
}
}
@Module({ mountpoint: '/api', controllers: [ShapeNameCtl] })
class ShapeNameTeapot {}
@Route('/local')
class LocalCtl {
@Get('/ok') ok() {
return { from: 'teacup' };
}
}
@Module({ mountpoint: '/api', controllers: [LocalCtl] })
class TeacupModule {}
const FAST = { initialDelayMs: 20, maxDelayMs: 60 };
/** A port nothing listens on, that a teapot can take later. */
async function freePort(): Promise<number> {
const server = net.createServer();
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
const { port } = server.address() as AddressInfo;
await new Promise<void>((resolve) => server.close(() => resolve()));
return port;
}
const controlUrl = (port: number) => `ws://127.0.0.1:${port}/__mesh__/control`;
const open: App[] = [];
afterEach(async () => {
while (open.length) await open.pop()!.close();
});
async function teapotOn(port: number, module: new () => unknown): Promise<App> {
const teapot = createApp({ modules: [module], experimental: true, mesh: { secret: SECRET } });
await teapot.listen(port);
open.push(teapot);
return teapot;
}
async function stop(teapot: App): Promise<void> {
await teapot.close();
open.splice(open.indexOf(teapot), 1);
}
interface Teacup {
app: App;
warnings: string[];
events: EventPayload[];
/** Resolves on the next `name` event. */
next: (name: 'mesh:routes' | 'mesh:disconnect') => Promise<EventPayload>;
}
function teacup(ports: number[], mesh: Partial<MeshConfig> = {}): Teacup {
const warnings: string[] = [];
const events: EventPayload[] = [];
const waiting = new Map<string, Array<(e: EventPayload) => void>>();
const app = createApp({
modules: [TeacupModule],
experimental: true,
logger: { debug() {}, info() {}, warn: (m: string) => warnings.push(m), error: (m: string) => warnings.push(m) },
mesh: {
teapots: ports.map((port) => ({ url: controlUrl(port), secret: SECRET })),
bootTimeoutMs: 100,
timeoutMs: 1000,
heartbeatMs: 1000,
reconnect: FAST,
...mesh,
},
});
for (const name of ['mesh:routes', 'mesh:disconnect'] as const) {
app.bus.on(name, (payload) => {
if (name === 'mesh:routes') events.push(payload);
const list = waiting.get(name) ?? [];
waiting.delete(name);
for (const resolve of list) resolve(payload);
});
}
open.unshift(app); // closed before the teapots
const next = (name: string) =>
new Promise<EventPayload>((resolve, reject) => {
const timer = setTimeout(() => reject(new Error(`no ${name} within 5s`)), 5000);
waiting.set(name, [
...(waiting.get(name) ?? []),
(e) => {
clearTimeout(timer);
resolve(e);
},
]);
});
return { app, warnings, events, next };
}
const get = (app: App, path: string, method = 'GET') => app.fetch(new Request(`http://mesh.test${path}`, { method }));
function nodeGet(port: number, path: string) {
return new Promise<{ status: number; body: string }>((resolve, reject) => {
const req = http.request({ host: '127.0.0.1', port, method: 'GET', path, agent: false }, (res) => {
let body = '';
res.setEncoding('utf8');
res.on('data', (chunk) => (body += chunk));
res.on('end', () => resolve({ status: res.statusCode ?? 0, body }));
});
req.on('error', reject);
req.end();
});
}
describe('mesh: remote routes follow teapot membership (CARD-19, D15)', () => {
it('T1: a teapot absent at boot is served once it connects, on app.fetch and listen(), without rebuilding the local tree', async () => {
const port = await freePort();
const cup = teacup([port]);
const listenPort = ((await cup.app.listen(0)).address() as AddressInfo).port;
expect((await get(cup.app, '/api/remote/ping')).status).toBe(404);
expect((await get(cup.app, '/api/local/ok')).status).toBe(200);
const before = routerSeamCounts();
const routed = cup.next('mesh:routes');
await teapotOn(port, PingTeapot);
expect(await routed).toEqual({
name: controlUrl(port),
url: controlUrl(port),
added: ['GET /api/remote/ping'],
removed: [],
});
const res = await get(cup.app, '/api/remote/ping');
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ from: 'teapot' });
expect(await nodeGet(listenPort, '/api/remote/ping')).toEqual({ status: 200, body: '{"from":"teapot"}' });
expect((await get(cup.app, '/api/local/ok')).status).toBe(200);
const after = routerSeamCounts();
expect(after.indexBuilds).toBeGreaterThan(before.indexBuilds); // the remote trees were rebuilt
expect(after.localTreeBuilds).toBe(before.localTreeBuilds); // the local ones were not
});
it('T2/T3: a reconnecting teapot keeps its routes with 503 + retry-after, then serves again', async () => {
const port = await freePort();
let teapot = await teapotOn(port, PingTeapot);
const cup = teacup([port]);
expect((await get(cup.app, '/api/remote/ping')).status).toBe(200);
const dropped = cup.next('mesh:disconnect');
await stop(teapot);
await dropped;
const down = await get(cup.app, '/api/remote/ping');
expect(down.status).toBe(503);
expect(Number(down.headers.get('retry-after'))).toBeGreaterThanOrEqual(1);
expect((await get(cup.app, '/api/remote/ping', 'OPTIONS')).headers.get('allow')).toBe('GET, HEAD, OPTIONS');
expect((await get(cup.app, '/api/remote/ping', 'POST')).headers.get('allow')).toBe('GET, HEAD, OPTIONS');
teapot = await teapotOn(port, PingTeapot);
await expect.poll(async () => (await get(cup.app, '/api/remote/ping')).status, { timeout: 5000 }).toBe(200);
// Same routes back: nothing changed in the table, so no event.
expect(cup.events).toEqual([]);
});
it('T4: with reconnect: false a dropped teapot is gone — 404, Allow without its methods, local routes unaffected', async () => {
const port = await freePort();
const teapot = await teapotOn(port, PingTeapot);
const cup = teacup([port], { reconnect: false });
expect((await get(cup.app, '/api/remote/ping')).status).toBe(200);
const routed = cup.next('mesh:routes');
await stop(teapot);
expect(await routed).toMatchObject({ added: [], removed: ['GET /api/remote/ping'] });
const gone = await get(cup.app, '/api/remote/ping');
expect(gone.status).toBe(404);
expect(gone.headers.get('retry-after')).toBeNull();
const options = await get(cup.app, '/api/remote/ping', 'OPTIONS');
expect(options.status).toBe(404);
expect(options.headers.get('allow')).toBeNull();
expect((await get(cup.app, '/api/local/ok')).status).toBe(200);
expect(cup.warnings.some((w) => w.includes('gone for good'))).toBe(true);
});
it('T5: a teapot that returns with an extra route has it served', async () => {
const port = await freePort();
const teapot = await teapotOn(port, PingTeapot);
const cup = teacup([port]);
expect((await get(cup.app, '/api/remote/extra')).status).toBe(404);
const dropped = cup.next('mesh:disconnect');
await stop(teapot);
await dropped;
const routed = cup.next('mesh:routes');
await teapotOn(port, PingExtraTeapot);
expect(await routed).toMatchObject({ added: ['GET /api/remote/extra'], removed: [] });
expect((await get(cup.app, '/api/remote/extra')).status).toBe(200);
expect(await (await get(cup.app, '/api/remote/ping')).json()).toEqual({ from: 'teapot v2' });
});
it('T6: a teapot that returns without a route has it removed', async () => {
const port = await freePort();
const teapot = await teapotOn(port, PingTeapot);
const cup = teacup([port]);
expect((await get(cup.app, '/api/remote/ping')).status).toBe(200);
const dropped = cup.next('mesh:disconnect');
await stop(teapot);
await dropped;
const routed = cup.next('mesh:routes');
await teapotOn(port, ExtraOnlyTeapot);
expect(await routed).toMatchObject({ added: ['GET /api/remote/extra'], removed: ['GET /api/remote/ping'] });
expect((await get(cup.app, '/api/remote/ping')).status).toBe(404);
expect((await get(cup.app, '/api/remote/extra')).status).toBe(200);
});
it('T7: a late route ambiguous with another teapot’s is refused and logged; everything else serves', async () => {
const [portA, portB] = [await freePort(), await freePort()];
await teapotOn(portA, ShapeIdTeapot);
const cup = teacup([portA, portB]);
expect(await (await get(cup.app, '/api/shape/1')).json()).toEqual({ from: 'id' });
const routed = cup.next('mesh:routes');
await teapotOn(portB, ShapeNameTeapot);
expect(await routed).toMatchObject({ url: controlUrl(portB), added: ['GET /api/other'], removed: [] });
expect(await (await get(cup.app, '/api/shape/1')).json()).toEqual({ from: 'id' });
expect((await get(cup.app, '/api/other')).status).toBe(200);
expect(
cup.warnings.some((w) => w.includes('not serving a route from') && w.includes("'GET /api/shape/:name'")),
).toBe(true);
});
it('T8: 1 000 requests across a late connect each see the old table or the new one', async () => {
const port = await freePort();
const cup = teacup([port]);
expect((await get(cup.app, '/api/remote/ping')).status).toBe(404);
const routed = cup.next('mesh:routes');
const starting = teapotOn(port, PingTeapot);
const statuses: number[] = [];
let changed = false;
void routed.then(() => (changed = true));
// 100 waves of 10 concurrent requests, spread across the moment the table changes. Ten at a time,
// not more: past 16 RPCs in flight on one link the teapot's control channel hits the WebSocket
// inbound-queue bound (#107) and drops the link. That is pre-existing and not about membership.
for (let wave = 0; wave < 100; wave++) {
const batch = await Promise.all(
Array.from({ length: 10 }, () => get(cup.app, '/api/remote/ping').then((res) => res.status)),
);
statuses.push(...batch);
if (!changed) await new Promise((resolve) => setTimeout(resolve, 2));
}
await starting;
await routed;
expect(statuses).toHaveLength(1000);
expect(statuses.every((status) => status === 404 || status === 200)).toBe(true);
// Both tables were actually seen, so the change happened while requests were flowing.
expect(statuses).toContain(404);
expect(statuses).toContain(200);
expect((await get(cup.app, '/api/remote/ping')).status).toBe(200);
});
it('with reconnect: false a teapot absent at boot is not retried (no backoff to retry with)', async () => {
const port = await freePort();
const cup = teacup([port], { reconnect: false });
expect((await get(cup.app, '/api/remote/ping')).status).toBe(404);
await teapotOn(port, PingTeapot);
await new Promise((resolve) => setTimeout(resolve, 300));
expect((await get(cup.app, '/api/remote/ping')).status).toBe(404);
expect(cup.events).toEqual([]);
});
});
+5 -3
View File
@@ -143,7 +143,7 @@ describe('missingFromManifest', () => {
expect(missingFromManifest(booted, booted)).toEqual([]);
});
it('accepts extra exports — the graph is fixed at boot and nothing new is spliced', () => {
it('accepts extra exports — extra steps are ignored, since the graph is fixed at boot', () => {
const wider: Manifest = {
steps: [...booted.steps, 'billing'],
routes: [...booted.routes, { method: 'POST', pattern: '/svc/new' }],
@@ -161,8 +161,10 @@ describe('missingFromManifest', () => {
expect(missingFromManifest(booted, shrunk)).toEqual(["step 'auth'"]);
});
it('reports a missing route by method and pattern', () => {
// D15 (CARD-19): routes are not part of the graph, so a returning teapot's routes replace the old
// ones (a dropped route is removed from the remote table) instead of refusing the reconnect.
it('does not report a missing route — routes follow the returning manifest', () => {
const routeless: Manifest = { steps: booted.steps, routes: [] };
expect(missingFromManifest(booted, routeless)).toEqual(["route 'GET /svc/:id'"]);
expect(missingFromManifest(booted, routeless)).toEqual([]);
});
});