diff --git a/src/app/index.ts b/src/app/index.ts index 06b37f5..b359ac2 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -695,12 +695,17 @@ function finalizeGraph( } } - assertNeedsSatisfiable(routePlans, providerNodes, stepNodes); + assertNeedsSatisfiable(routePlans, providerNodes, stepNodes, missingNote); return { orderedProviders, orderedSteps }; } /** Throws if any route needs a key that nothing (local or mesh) provides, suggesting the nearest match. */ -function assertNeedsSatisfiable(routePlans: RoutePlan[], providerNodes: GraphNode[], stepNodes: GraphNode[]): void { +function assertNeedsSatisfiable( + routePlans: RoutePlan[], + providerNodes: GraphNode[], + stepNodes: GraphNode[], + missingNote?: (key: string) => string, +): void { const producedKeys = new Set([ ...providerNodes.flatMap((node) => node.provides), ...stepNodes.flatMap((node) => node.provides), @@ -726,8 +731,13 @@ function assertNeedsSatisfiable(routePlans: RoutePlan[], providerNodes: GraphNod } const hint = nearest(need, allowed); + // "local or mesh" claims the mesh was consulted and came up empty — true only when every + // teapot connected. When one didn't, say "connected mesh" instead so the message doesn't + // assert a search that never happened, and let missingNote name which teapots were away. + const scope = missingNote ? 'local or connected mesh' : 'local or mesh'; throw new Error( - `handler '${plan.handlerName}' needs '${need}' but nothing (local or mesh) provides it${hint ? ` — did you mean '${hint}'?` : ''}`, + `handler '${plan.handlerName}' needs '${need}' but nothing (${scope}) provides it` + + `${hint ? ` — did you mean '${hint}'?` : ''}${missingNote?.(need) ?? ''}`, ); } } diff --git a/src/mesh/link.ts b/src/mesh/link.ts index ff50981..e02662a 100644 --- a/src/mesh/link.ts +++ b/src/mesh/link.ts @@ -451,7 +451,7 @@ export function connectLink(args: { }; /** Adopt an established session, or refuse it when its manifest no longer backs the graph. */ - const adopt = (session: Session, _attempt: number): boolean => { + const adopt = (session: Session): boolean => { const missing = missingFromManifest(link.manifest, session.manifest); if (missing.length > 0) { @@ -491,7 +491,7 @@ export function connectLink(args: { return; } - if (!adopt(session, attempt)) scheduleRetry(attempt); + if (!adopt(session)) scheduleRetry(attempt); } catch { // openSession's own abort listener already scheduled the retry through `onEnd`; a rejection // that never opened a socket (a bad URL, DNS) needs one scheduled here instead. @@ -501,7 +501,7 @@ export function connectLink(args: { return openSession(sessionArgs(() => endSession(0))).then((session) => { link.manifest = session.manifest; - adopt(session, 0); + adopt(session); return link; }); diff --git a/test/mesh/boot-retry.test.ts b/test/mesh/boot-retry.test.ts index 5b0bf2e..5a68b93 100644 --- a/test/mesh/boot-retry.test.ts +++ b/test/mesh/boot-retry.test.ts @@ -102,14 +102,40 @@ describe('mesh boot retry', () => { try { // exhausting the budget is no longer fatal by itself (that's the point of this change) — - // the boot still fails here because `who` needs 'auth' and nothing local provides it. - await expect(teacup.fetch(new Request('http://x/api/local/who'))).rejects.toThrow(/mesh/); + // the boot still fails here because `who` needs 'auth' and nothing local provides it. The + // exact message, not a bare /mesh/ match (which would also match this message's own + // "(local or connected mesh)" wording for an unrelated reason). + await expect(teacup.fetch(new Request('http://x/api/local/who'))).rejects.toThrow( + /needs 'auth' but nothing \(local or connected mesh\) provides it[\s\S]*did not connect/i, + ); expect(lines.some((l) => l.includes('starting without it'))).toBe(true); } finally { await teacup.close(); } }, 15_000); + it('names the absent teapot when a route handler needs its token directly', async () => { + // `who` needs 'auth' via `@needs`, not through a `@Step` — this goes through + // `assertNeedsSatisfiable`, a different check than `topoSort`'s, and must carry the same note. + const teacup = createApp({ + modules: [TeacupModule], + experimental: true, + mesh: { + teapots: [{ url: 'ws://127.0.0.1:9/x', secret: 's' }], + secret: 's', + bootTimeoutMs: 300, + }, + }); + + try { + await expect(teacup.ready()).rejects.toThrow( + /needs 'auth' but nothing \(local or connected mesh\) provides it[\s\S]*did not connect.*127\.0\.0\.1:9/i, + ); + } finally { + await teacup.close(); + } + }, 15_000); + it('does not retry a wrong secret — the teapot refused, and it will refuse again', async () => { const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: 'good' } }); const server = await teapot.listen(0); @@ -193,7 +219,9 @@ describe('mesh boot retry', () => { }); try { - await expect(teacup.fetch(new Request('http://x/api/local/who'))).rejects.toThrow(/mesh/); + await expect(teacup.fetch(new Request('http://x/api/local/who'))).rejects.toThrow( + /needs 'auth' but nothing \(local or connected mesh\) provides it[\s\S]*did not connect/i, + ); expect(lines.filter((l) => l.includes('of boot budget left'))).toEqual([]); } finally { await teacup.close();