fix(mesh): fail the boot when two teapots export the same route

Duplicate scope tokens already threw in setRunner, but duplicate routes did not:
both were pushed into remoteRoutes, and matchRoute keeps the first of two
identical patterns (moreSpecific returns 0, not > 0). The second teapot's route
was silently dead. Same collision, two behaviours — one loud, one mute.

Fail the boot instead, naming the route and both teapots, since which pair
clashed is the thing an operator needs. The message says load balancing is not
implemented *yet*: it is a not-yet, not an impossibility.

Throwing now also keeps the door open. "First teapot wins" is a semantic callers
could come to rely on, so shipping it would make balancing a breaking change
later. An error cannot be relied on — turning it into a strategy stays additive.

Scopes keep failing rather than balancing, and should: an app-scope provider is
memoised, so "balancing" a cached value means nothing.

Refs .specs/2026-07-15-green-tea-mesh-cross-runtime-plan.md

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-07-15 16:04:57 -05:00
1 parent a74417494f
commit 033af2d1fc
2 files changed
+84

No files matched your search

+67
View File
@@ -23,6 +23,28 @@ class LocalCtl {
@Module({ mountpoint: '/api', controllers: [LocalCtl] })
class TeacupModule {}
// Two teapots exporting the SAME route and no scopes — a scope clash would throw first
// (setRunner enforces unique tokens), which would hide the route clash under test.
@Route('/dup')
class DupCtl {
@Get('/ping', { export: true })
ping() {
return { pong: true };
}
}
@Module({ mountpoint: '/api', controllers: [DupCtl] })
class DupTeapotModule {}
@Route('/c')
class PlainCtl {
@Get('/ok')
ok() {
return { ok: true };
}
}
@Module({ mountpoint: '/', controllers: [PlainCtl] })
class PlainTeacupModule {}
describe('mesh skeleton integration', () => {
it('teacup resolves remote app-scope + request-scope into a local handler', async () => {
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
@@ -52,6 +74,51 @@ describe('mesh skeleton integration', () => {
s.close();
});
it('refuses to boot when two teapots export the same route, naming both', async () => {
// No load balancing exists yet, so picking one silently would be a wrong answer that
// callers could come to depend on — and it would make adding LB later a breaking change.
const a = createApp({ modules: [DupTeapotModule], experimental: true, mesh: { secret: SECRET } });
const b = createApp({ modules: [DupTeapotModule], experimental: true, mesh: { secret: SECRET } });
const aServer = await a.listen(0);
const bServer = await b.listen(0);
const url = (s: any) => `ws://127.0.0.1:${(s.address() as any).port}/__mesh__/control`;
const teacup = createApp({
modules: [PlainTeacupModule],
experimental: true,
mesh: {
teapots: [
{ url: url(aServer), secret: SECRET },
{ url: url(bServer), secret: SECRET },
],
},
});
await expect(teacup.fetch(new Request('http://x/c/ok'))).rejects.toThrow(/GET \/api\/dup\/ping/);
await expect(teacup.fetch(new Request('http://x/c/ok'))).rejects.toThrow(/load balancing/i);
aServer.close();
bServer.close();
});
it('accepts the same route from a single teapot (regression: no false positive)', async () => {
const a = createApp({ modules: [DupTeapotModule], experimental: true, mesh: { secret: SECRET } });
const aServer = await a.listen(0);
const url = `ws://127.0.0.1:${(aServer.address() as any).port}/__mesh__/control`;
const teacup = createApp({
modules: [PlainTeacupModule],
experimental: true,
mesh: { teapots: [{ url, secret: SECRET }] },
});
const res = await teacup.fetch(new Request('http://x/api/dup/ping'));
expect(await res.json()).toEqual({ pong: true });
await teacup.close();
aServer.close();
});
it('rejects a bad secret (teacup boot fails)', async () => {
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
const tServer = await teapot.listen(0); const tPort = (tServer.address() as any).port;