From 6e66b05a4123e0bde967c5e70bdcbdbdefac90b8 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Sun, 6 Sep 2026 16:25:21 -0500 Subject: [PATCH] refactor(mesh): drop the remote app-scope binding and its rebind seam A remote provider was registered app-scope, resolved once, and cached for the process lifetime; invalidateRemoteBindings existed only to un-cache it on reconnect. With providers gone from the manifest, both go, and a remote step needs nothing on reconnect because it holds nothing between requests. Signed-off-by: Diego Resendez --- src/app/index.ts | 44 ++++--------------------------- test/mesh/integration.test.ts | 49 +++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 39 deletions(-) diff --git a/src/app/index.ts b/src/app/index.ts index 9e64599..4913df2 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -33,7 +33,7 @@ import { buildOpenApi, type OpenApiInfo } from '../openapi'; import { connectLink, isPermanentRefusal, type Link } from '../mesh/link'; import { Rooms } from '../rooms'; import type { TlsOptions, SecurityOptions, CorsOptions } from '../security'; -import { buildRemote, type RemoteScopeNode } from '../mesh/teacup'; +import { buildRemote } from '../mesh/teacup'; import { buildManifest, createMeshControl, MESH_CONTROL_PATH } from '../mesh/teapot'; import type { MeshControl } from '../http'; import type { RequestEnvelope, RouteEntry } from '../mesh/protocol'; @@ -59,7 +59,6 @@ interface Registry { */ providerInstances: Map void | Promise }>; routePlans: RoutePlan[]; - exportedProviders: string[]; exportedSteps: string[]; exportedRoutes: RouteEntry[]; setRunner(name: string, runner: Runner, framework?: boolean): void; @@ -239,7 +238,7 @@ export function createApp(opts: { // can only be built here, after finalize(), and never at construction time. const prepareGraph = async (): Promise => { if (opts.mesh && !booted) { - const spliced = await spliceRemoteScopes(opts.mesh, bus, registry, logger, container); + const spliced = await spliceRemoteScopes(opts.mesh, bus, registry, logger); remoteRoutes = spliced.remoteRoutes; meshLinks.push(...spliced.meshLinks); finalize(); @@ -424,7 +423,6 @@ function emptyRegistry(): Registry { providerMeta: new Map(), providerInstances: new Map(), routePlans: [], - exportedProviders: [], exportedSteps: [], exportedRoutes: [], setRunner, @@ -805,26 +803,12 @@ function warnIfCleartext(url: string, logger: Logger): void { ); } -/** - * Re-register a link's app-scope bindings after it reconnects, so their next resolve re-runs the RPC. - * - * Named and separate rather than inlined in the reconnect handler: this is the seam a future - * `onManifestChange: 'reconcile'` reuses, and burying it would mean writing it twice. - * - * Lazy on purpose — the RPC runs on the next resolve, not here. Re-resolving eagerly would put a - * network call on the reconnect path, where a failure has nowhere to go but a swallowed rejection. - */ -function invalidateRemoteBindings(rebind: Array<() => void>): void { - for (const bind of rebind) bind(); -} - -/** Connects the configured teapots, splices their remote providers/steps into the registry, and returns their routes. */ +/** Connects the configured teapots, splices their remote steps into the registry, and returns their routes. */ async function spliceRemoteScopes( mesh: MeshConfig, bus: Bus, registry: Registry, logger: Logger, - container: Container, ): Promise<{ remoteRoutes: RouteDef[]; meshLinks: Link[] }> { const remoteRoutes: RouteDef[] = []; const meshLinks: Link[] = []; @@ -833,9 +817,6 @@ async function spliceRemoteScopes( try { for (const teapot of mesh.teapots ?? []) { warnIfCleartext(teapot.url, logger); - // Deferred so the reconnect callback can name this link's provider tokens, which are only - // known once its manifest arrives — the callback is registered before the link can drop. - const rebind: Array<() => void> = []; const link = await connectUntilDeadline(mesh, bus, logger, () => connectLink({ url: teapot.url, @@ -844,29 +825,14 @@ async function spliceRemoteScopes( heartbeatMs: mesh.heartbeatMs, reconnect: mesh.reconnect, onManifestChange: mesh.onManifestChange, - onReconnect: () => invalidateRemoteBindings(rebind), logger, bus, }), ); meshLinks.push(link); - // `buildRemote` no longer returns `providers` — every exported token is a step now (Task 1 - // refuses a provider export at boot). Kept as an empty array, not deleted, so the loop below - // still typechecks; Task 3 removes the loop itself along with the rest of provider-splicing. - const providers: RemoteScopeNode[] = []; const { steps, routes } = buildRemote(link); const origin = `mesh:${teapot.url}`; - for (const provider of providers) { - registry.providerNodes.push({ name: provider.name, needs: [], provides: [provider.name], origin }); - registry.providerMeta.set(provider.name, { optional: false }); - registry.setRunner(provider.name, provider.run); - // A remote app-scope value is resolved once at boot and frozen into the container, so it - // would keep answering from a cache the teapot no longer stands behind. Re-registering - // gives the binding a factory that re-runs the RPC, and `register` drops the memo with it. - rebind.push(() => container.register(provider.name, 'app', () => provider.run({}))); - } - for (const step of steps) { registry.stepNodes.push({ name: step.name, needs: [], provides: [step.name], origin }); registry.setRunner(step.name, step.run); @@ -1088,8 +1054,8 @@ function buildMeshControl( registry: Registry, deps: { container: Container; orderedProviders: GraphNode[]; orderedSteps: GraphNode[]; deps: PipelineDeps }, ): MeshControl | undefined { - const { exportedProviders, exportedSteps, exportedRoutes, routePlans, runners } = registry; - const hasExports = exportedProviders.length || exportedSteps.length || exportedRoutes.length; + const { exportedSteps, exportedRoutes, routePlans, runners } = registry; + const hasExports = exportedSteps.length || exportedRoutes.length; if (hasExports && !mesh?.secret) { throw new Error('mesh: exports declared (export: true) but no mesh.secret configured to gate the control channel'); diff --git a/test/mesh/integration.test.ts b/test/mesh/integration.test.ts index ae5bd11..10d154c 100644 --- a/test/mesh/integration.test.ts +++ b/test/mesh/integration.test.ts @@ -93,6 +93,55 @@ describe('mesh skeleton integration', () => { tServer.close(); cServer.close(); }); + it('re-runs the RPC on every request rather than caching a boot value', async () => { + // A dedicated single-step teapot/teacup pair, not the shared TeapotModule above: exporting + // two scopes (config + auth) makes resolveScope re-run the whole step graph per scope RPC, + // which would make a call counter here track that unrelated behaviour instead of the thing + // this test guards. With one exported step, a per-request counter is unambiguous: a step + // re-runs where a provider used to be resolved once at boot and cached for the process + // lifetime — this is the regression guard for removing the remote app-scope splice, proving + // it does not quietly reintroduce a cached value. + let calls = 0; + @Step({ provides: 'counter', needs: [], export: true }) + class Counter { + run() { + calls += 1; + return { counter: calls }; + } + } + @Module({ mountpoint: '/api', steps: [Counter] }) + class CounterTeapotModule {} + + @Route('/local') + class CounterLocalCtl { + @Get('/counter') + counter(@needs('counter') counter: number) { + return { counter }; + } + } + @Module({ mountpoint: '/api', controllers: [CounterLocalCtl] }) + class CounterTeacupModule {} + + const teapot = createApp({ modules: [CounterTeapotModule], experimental: true, mesh: { secret: SECRET } }); + const tServer = await teapot.listen(0); + const tPort = (tServer.address() as any).port; + const url = `ws://127.0.0.1:${tPort}/__mesh__/control`; + + const teacup = createApp({ + modules: [CounterTeacupModule], + experimental: true, + mesh: { teapots: [{ url, secret: SECRET }] }, + }); + const cServer = await teacup.listen(0); + const cPort = (cServer.address() as any).port; + const hit = () => fetch(`http://127.0.0.1:${cPort}/api/local/counter`).then((r) => r.json() as Promise<{ counter: number }>); + + expect((await hit()).counter).toBe(1); + expect((await hit()).counter).toBe(2); + + tServer.close(); cServer.close(); + }); + it('mesh is gated behind experimental: true', () => { expect(() => createApp({ modules: [TeapotModule], mesh: { secret: SECRET } })).toThrow(/alpha feature/); });