feat(app): add app.ready() so introspection works on any app

inspect()/graph()/explain() are synchronous, but a mesh graph is not knowable
until its teapots have been asked, so on a mesh app they threw and the caller had
no way to fix it. Code holding an `App` could not tell which kind it had — the
Liskov complaint from the audit, and the last of it: fetch/upgrade stopped
throwing when the boot moved into the gate.

app.ready() resolves the graph and returns. On a mesh app it connects the
teapots; on every other app it is a no-op. So one shape works for both:

    await app.ready();
    app.graph();

which is what actually restores substitutability — a uniform protocol, not a type
that describes the asymmetry.

Deliberately not a full boot: it does not run provider factories. Resolving the
graph and being ready to serve are different things, and a devtool drawing a
diagram should not open your database connections. Serving boots them too and
shares the same memoized step, so ready() then fetch() resolves the graph once.

The guards stay — sync code cannot await network I/O — but the message names the
fix instead of describing the trap.

createApp hit 26 on the complexity gate with ready() added. Both boot stages had
their own copy of the same memoisation, so it collapses into one `once` helper
used by both, which is a smaller createApp than before this change.

Refs .specs/2026-07-15-green-tea-solid-audit (App/Mesh LSP)

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-07-15 23:00:14 -05:00
1 parent 34dbb32b2c
commit 9886e53747
7 files changed
+147 -11

No files matched your search

+24 -8
View File
@@ -53,6 +53,21 @@ interface Registry {
setRunner(name: string, runner: Runner): void;
}
/**
* Memoizes an async step so repeat and concurrent callers share one run. Both boot stages need
* this: `fetch`, `upgrade`, `listen` and `ready` can each be the first to trigger them, and
* re-running would re-splice a mesh or re-run provider factories and their side effects.
*/
function once(run: () => Promise<void>): () => Promise<void> {
let promise: Promise<void> | undefined;
return (): Promise<void> => {
if (!promise) promise = run();
return promise;
};
}
/** Request-time helpers shared by the HTTP, WS and mesh route builders. */
interface PipelineDeps {
providedSeed(plan: RoutePlan): Promise<Record<string, unknown>>;
@@ -151,6 +166,11 @@ export function createApp(opts: {
});
};
// Memoized separately from the provider boot on purpose: resolving the graph and being ready
// to serve are different things. Introspection needs only the former, and must not run
// provider factories (and open their connections) as a side effect of drawing a diagram.
const ready = once(prepareGraph);
const inspect = (routePath: string): InspectLine[] => inspectRoute(routePlans, routePath, booted);
const graph = (): GraphView => buildGraphView(providerNodes, stepNodes, routePlans, booted);
const explain = (routePath: string): Explain => explainRoute(routePlans, routePath, booted);
@@ -182,7 +202,7 @@ export function createApp(opts: {
() => orderedProviders,
{ runners, container, providerMeta, bus },
degradedProviders,
prepareGraph,
ready,
);
const staticResolver = opts.static ? buildStaticResolver(opts.static) : undefined;
@@ -250,6 +270,7 @@ export function createApp(opts: {
return {
listen,
close,
ready,
fetch: fetchFn,
upgrade: upgradeFn,
inspect,
@@ -603,11 +624,9 @@ function makeAppBooter(
bus: Bus;
},
degradedProviders: string[],
/** Splices remote mesh scopes in and finalizes the graph. Must precede providers: remote nodes join the topo sort. */
/** Resolves the graph (splices remote mesh scopes, finalizes). Must precede providers: remote nodes join the topo sort. */
prepareGraph: () => Promise<void>,
): () => Promise<void> {
let bootPromise: Promise<void> | undefined;
const runBoot = async (): Promise<void> => {
await prepareGraph();
applyOverrides(overrides, registry);
@@ -621,10 +640,7 @@ function makeAppBooter(
}
};
return (): Promise<void> => {
if (!bootPromise) bootPromise = runBoot();
return bootPromise;
};
return once(runBoot);
}
/**