From 033af2d1fc6ab30d3356fdebb0962f3970fd95aa Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Wed, 15 Jul 2026 16:04:57 -0500 Subject: [PATCH] fix(mesh): fail the boot when two teapots export the same route MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/app/index.ts | 17 +++++++++ test/mesh/integration.test.ts | 67 +++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/src/app/index.ts b/src/app/index.ts index f89abe5..f6c34e5 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -466,6 +466,7 @@ async function spliceRemoteScopes( ): Promise<{ remoteRoutes: RouteDef[]; meshLinks: Link[] }> { const remoteRoutes: RouteDef[] = []; const meshLinks: Link[] = []; + const routeOwners = new Map(); // "METHOD /pattern" -> the teapot that exported it try { for (const teapot of mesh.teapots ?? []) { @@ -486,6 +487,22 @@ async function spliceRemoteScopes( } for (const route of routes) { + const key = `${route.method} ${route.pattern}`; + const owner = routeOwners.get(key); + + // Fail the boot rather than pick one. Route matching keeps the first of two identical + // patterns, so the loser would be silently dead — and callers could come to depend on + // "first teapot wins", which would make adding balancing later a breaking change. + // Scope tokens already fail this way in setRunner; routes now match. + if (owner) { + throw new Error( + `mesh: route '${key}' is exported by more than one teapot (${owner} and ${teapot.url}) — ` + + 'load balancing across teapots is not implemented yet, so green-tea will not choose one for you. ' + + 'Export it from a single teapot.', + ); + } + + routeOwners.set(key, teapot.url); remoteRoutes.push({ method: route.method, pattern: route.pattern, diff --git a/test/mesh/integration.test.ts b/test/mesh/integration.test.ts index 98bbf37..3108417 100644 --- a/test/mesh/integration.test.ts +++ b/test/mesh/integration.test.ts @@ -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;