Files
core/src/app/introspect.ts
T
chrnx 9886e53747 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>
2026-07-15 23:00:14 -05:00

87 lines
3.3 KiB
TypeScript

import type { GraphNode } from '../graph';
import type { GraphView } from '../graph-viz';
import type { InspectLine, Explain, ExplainNode, RoutePlan } from './types';
/** Lists the provider/step/handler chain for a route, in execution order. */
export function inspectRoute(routePlans: RoutePlan[], routePath: string, booted: boolean): InspectLine[] {
if (!booted)
throw new Error(
'inspect() unavailable until a mesh app has booted: a mesh graph is not knowable until its teapots are asked. Call `await app.ready()` first — it is a no-op on a non-mesh app',
);
const plan = routePlans.find((candidate) => candidate.pattern === routePath);
if (!plan) throw new Error(`no route: ${routePath}`);
return [
...plan.providers.map((provider) => ({
name: provider.name,
kind: 'provider' as const,
origin: provider.origin,
})),
...plan.steps.map((step) => ({ name: step.name, kind: 'step' as const, origin: step.origin })),
{ name: plan.handlerName, kind: 'handler' as const, origin: plan.origin },
];
}
/** Builds the full dependency-graph view: every provider/step node plus each route's chain. */
export function buildGraphView(
providerNodes: GraphNode[],
stepNodes: GraphNode[],
routePlans: RoutePlan[],
booted: boolean,
): GraphView {
if (!booted)
throw new Error(
'graph() unavailable until a mesh app has booted: a mesh graph is not knowable until its teapots are asked. Call `await app.ready()` first — it is a no-op on a non-mesh app',
);
return {
nodes: [
...providerNodes.map((node) => ({
name: node.name,
kind: 'provider' as const,
origin: node.origin,
needs: node.needs,
provides: node.provides,
})),
...stepNodes.map((node) => ({
name: node.name,
kind: 'step' as const,
origin: node.origin,
needs: node.needs,
provides: node.provides,
})),
],
routes: routePlans.map((plan) => ({
pattern: plan.pattern,
method: plan.method,
transport: plan.transport,
chain: [...plan.providers.map((node) => node.name), ...plan.steps.map((node) => node.name), plan.handlerName],
})),
};
}
/** Explains a single route: its match criteria and the ordered chain with each node's needs/provides. */
export function explainRoute(routePlans: RoutePlan[], routePath: string, booted: boolean): Explain {
if (!booted)
throw new Error(
'explain() unavailable until a mesh app has booted: a mesh graph is not knowable until its teapots are asked. Call `await app.ready()` first — it is a no-op on a non-mesh app',
);
const plan = routePlans.find((candidate) => candidate.pattern === routePath);
if (!plan) throw new Error(`no route: ${routePath}`);
const toExplainNode = (node: GraphNode, kind: 'provider' | 'step'): ExplainNode => ({
name: node.name,
kind,
origin: node.origin,
needs: node.needs,
provides: node.provides,
});
return {
pattern: plan.pattern,
method: plan.method,
transport: plan.transport,
chain: [
...plan.providers.map((node) => toExplainNode(node, 'provider')),
...plan.steps.map((node) => toExplainNode(node, 'step')),
{ name: plan.handlerName, kind: 'handler', origin: plan.origin, needs: plan.needs, provides: [] },
],
};
}