fix(mesh): reconnect to a teapot that came back, refusing a manifest that no longer fits
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 15s
CI / build (pull_request) Successful in 24s
CI / runtimes (edge) (pull_request) Successful in 12s
CI / runtimes (deno) (pull_request) Successful in 18s

A dropped link stayed dead for the life of the process. Every RPC answered
503 until the teacup restarted, so deploying a teapot forced a restart of
every teacup depending on it and boot order became load-bearing — a worse
operational property than any gap the docs actually listed.

The Link now outlives any single socket, because the graph captured its rpc
closure at boot and a reconnect that produced a new Link would leave the graph
pointing at the dead one. Backoff doubles from 500ms to 30s with full jitter,
so teacups that went down together do not return in lockstep.

A returning teapot whose manifest no longer exports what the graph was
validated against is refused, not adopted: that graph is already serving, and
a token cannot be validated away after the fact. The link keeps retrying,
since a partial deploy may still restore it, and logs once per distinct
manifest rather than once per attempt. Refusal ships as a named policy
(onManifestChange: 'refuse') so hot reconciliation can arrive later as an
addition rather than as a change of what the default means — these are
separate processes on separate deploy cadences.

close() is terminal. closeLinks runs on shutdown, and a link that reconnected
after the application hung up would leave a process that cannot exit.

Also closes the documented gap where an app-scope export outlived its teapot
with a stale value: a successful reconnect re-registers those bindings, so the
next resolve re-runs the RPC. That invalidation is a named function rather
than inline, because it is the seam a future 'reconcile' reuses.

Tested on all three runtimes rather than on Node alone — Node's ws package,
Deno's global WebSocket and Bun's server-level socket handler are three
implementations, and swapping a socket under a live graph is where they stop
agreeing. 7 of the 9 new Node tests were verified to fail against the old
code; the 2 that pass are the ones that should, since the old code never
reconnected at all.

Mesh stays alpha.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-08-18 10:53:24 -05:00
1 parent 6672185ba2
commit 6ffbad9825
8 files changed
+819 -57

No files matched your search

+26 -1
View File
@@ -217,7 +217,7 @@ export function createApp(opts: {
// can only be built here, after finalize(), and never at construction time.
const prepareGraph = async (): Promise<void> => {
if (opts.mesh && !booted) {
const spliced = await spliceRemoteScopes(opts.mesh, bus, registry, logger);
const spliced = await spliceRemoteScopes(opts.mesh, bus, registry, logger, container);
remoteRoutes = spliced.remoteRoutes;
meshLinks.push(...spliced.meshLinks);
finalize();
@@ -615,12 +615,26 @@ function assertNeedsSatisfiable(routePlans: RoutePlan[], providerNodes: GraphNod
}
}
/**
* 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. */
async function spliceRemoteScopes(
mesh: MeshConfig,
bus: Bus,
registry: Registry,
logger: Logger,
container: Container,
): Promise<{ remoteRoutes: RouteDef[]; meshLinks: Link[] }> {
const remoteRoutes: RouteDef[] = [];
const meshLinks: Link[] = [];
@@ -628,11 +642,18 @@ async function spliceRemoteScopes(
try {
for (const teapot of mesh.teapots ?? []) {
// 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 connectLink({
url: teapot.url,
secret: teapot.secret,
timeoutMs: mesh.timeoutMs,
heartbeatMs: mesh.heartbeatMs,
reconnect: mesh.reconnect,
onManifestChange: mesh.onManifestChange,
onReconnect: () => invalidateRemoteBindings(rebind),
logger,
bus,
});
meshLinks.push(link);
@@ -643,6 +664,10 @@ async function spliceRemoteScopes(
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) {
+19
View File
@@ -103,6 +103,25 @@ export interface MeshConfig {
* `timeoutMs` first. Lower it to notice a dead teapot sooner, at the cost of more chatter.
*/
heartbeatMs?: number;
/**
* Reconnect to a teapot after its link drops (default: on). A dropped link used to stay dead for
* the life of the process, which meant deploying a teapot forced a restart of every teacup.
*
* `false` restores that fail-once behaviour. An object tunes the backoff, which doubles from
* `initialDelayMs` (500ms) up to `maxDelayMs` (30s) with jitter, so teacups that went down
* together do not come back in lockstep.
*/
reconnect?: boolean | { initialDelayMs?: number; maxDelayMs?: number };
/**
* What to do when a returning teapot's manifest no longer exports something the graph was
* validated against at boot (default: `'refuse'`).
*
* `'refuse'` hangs up on that session and keeps retrying — a partial deploy may still restore it.
* Serving against a manifest that no longer backs the graph would surface as a 500 that looks
* like application code. A future `'reconcile'` will rebuild the graph instead; it is named here
* rather than left implicit so that arriving is additive rather than a change of default.
*/
onManifestChange?: 'refuse';
}
/**