Skip to content

Commit ee32adf

Browse files
committed
feat(spec)!: refuse a top-level flow node no edge path from start reaches
Claude-Session: https://claude.ai/code/session_01FEkQ5oEQNYg7j3ueo5t6pV Co-Authored-By: Claude <noreply@anthropic.com>
1 parent d3f3686 commit ee32adf

2 files changed

Lines changed: 190 additions & 15 deletions

File tree

‎packages/spec/src/automation/flow.test.ts‎

Lines changed: 116 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,12 +2367,20 @@ describe('FlowSchema — top-level node ids are unique', () => {
23672367
});
23682368

23692369
it('accepts the same flow once the ids are unique, keeping the nodes in authored order', () => {
2370-
const unique = flowWith([
2371-
{ id: 'start', type: 'start', label: 'Start' },
2372-
{ id: 'n', type: 'assignment', label: 'Assign A' },
2373-
{ id: 'n2', type: 'assignment', label: 'Assign B' },
2374-
{ id: 'end', type: 'end', label: 'End' },
2375-
]);
2370+
// `n2` is wired in: a node no edge reaches is refused at parse.
2371+
const unique = {
2372+
...flowWith([
2373+
{ id: 'start', type: 'start', label: 'Start' },
2374+
{ id: 'n', type: 'assignment', label: 'Assign A' },
2375+
{ id: 'n2', type: 'assignment', label: 'Assign B' },
2376+
{ id: 'end', type: 'end', label: 'End' },
2377+
]),
2378+
edges: [
2379+
{ id: 'e1', source: 'start', target: 'n' },
2380+
{ id: 'e2', source: 'n', target: 'n2' },
2381+
{ id: 'e3', source: 'n2', target: 'end' },
2382+
],
2383+
};
23762384
const result = FlowSchema.safeParse(unique);
23772385
expect(result.success).toBe(true);
23782386
if (!result.success) return;
@@ -2645,15 +2653,23 @@ describe('FlowSchema — one node-id space across the top-level nodes[] and ever
26452653
});
26462654

26472655
it('accepts a flow whose ids are unique across the whole flow, keeping every region node in authored order', () => {
2648-
const unique = flowWith([
2649-
{ id: 'start', type: 'start', label: 'Start' },
2650-
loopOver([step('sweep_first'), step('sweep_second')], [{ id: 'b1', source: 'sweep_first', target: 'sweep_second' }]),
2651-
{
2652-
id: 'p', type: 'parallel', label: 'Fan out',
2653-
config: { branches: [{ nodes: [step('left')] }, { nodes: [step('right')] }] },
2654-
},
2655-
{ id: 'end', type: 'end', label: 'End' },
2656-
]);
2656+
// `p` is wired in: a node no edge reaches is refused at parse.
2657+
const unique = {
2658+
...flowWith([
2659+
{ id: 'start', type: 'start', label: 'Start' },
2660+
loopOver([step('sweep_first'), step('sweep_second')], [{ id: 'b1', source: 'sweep_first', target: 'sweep_second' }]),
2661+
{
2662+
id: 'p', type: 'parallel', label: 'Fan out',
2663+
config: { branches: [{ nodes: [step('left')] }, { nodes: [step('right')] }] },
2664+
},
2665+
{ id: 'end', type: 'end', label: 'End' },
2666+
]),
2667+
edges: [
2668+
{ id: 'e1', source: 'start', target: 'n' },
2669+
{ id: 'e2', source: 'n', target: 'p' },
2670+
{ id: 'e3', source: 'p', target: 'end' },
2671+
],
2672+
};
26572673
const result = FlowSchema.safeParse(unique);
26582674
expect(result.success).toBe(true);
26592675
if (!result.success) return;
@@ -2852,3 +2868,88 @@ describe('FlowSchema — an edge resolves in its own graph, and is followed once
28522868
expect(defineFlow(flow).edges.map((e) => e.id)).toEqual(['e1', 'e2', 'e3']);
28532869
});
28542870
});
2871+
2872+
describe('FlowSchema — every top-level node is reachable from the start node', () => {
2873+
// A run starts at the `start` node and follows edges; nothing else schedules
2874+
// a top-level node. A node no edge path reaches parsed, published and never
2875+
// ran. Refused at `nodes[i]`, for each such node.
2876+
const start: FlowNode = { id: 'start', type: 'start', label: 'Start' };
2877+
const end: FlowNode = { id: 'end', type: 'end', label: 'End' };
2878+
const step = (id: string): FlowNode => ({ id, type: 'assignment', label: id });
2879+
const flowOf = (nodes: FlowNode[], edges: FlowEdge[]): Flow => ({
2880+
name: 'reach_flow',
2881+
label: 'Reach flow',
2882+
type: 'autolaunched',
2883+
nodes,
2884+
edges,
2885+
});
2886+
2887+
it('refuses an action node with no in-edge, anchored at the node, naming it and the start node', () => {
2888+
const result = FlowSchema.safeParse(flowOf([start, step('orphan'), end], [
2889+
{ id: 'e1', source: 'start', target: 'end' },
2890+
]));
2891+
expect(result.success).toBe(false);
2892+
if (result.success) return;
2893+
expect(result.error.issues.map((i) => [i.code, i.path])).toEqual([['custom', ['nodes', 1]]]);
2894+
const [issue] = result.error.issues;
2895+
expect(issue.message).toContain('Node `orphan` (`assignment`, `nodes[1]`) is not reachable');
2896+
expect(issue.message).toContain('from the start node `start`');
2897+
});
2898+
2899+
it('refuses every node of a disconnected chain — an in-edge from an unreachable node is no path', () => {
2900+
const result = FlowSchema.safeParse(flowOf([start, step('a'), step('b'), end], [
2901+
{ id: 'e1', source: 'start', target: 'end' },
2902+
{ id: 'e2', source: 'a', target: 'b' },
2903+
{ id: 'e3', source: 'b', target: 'end' },
2904+
]));
2905+
expect(result.success).toBe(false);
2906+
if (result.success) return;
2907+
expect(result.error.issues.map((i) => i.path)).toEqual([['nodes', 1], ['nodes', 2]]);
2908+
});
2909+
2910+
it('refuses through every door that parses a flow — defineFlow throws', () => {
2911+
expect(() => defineFlow(flowOf([start, step('orphan'), end], [
2912+
{ id: 'e1', source: 'start', target: 'end' },
2913+
]))).toThrow(/Node `orphan`/);
2914+
});
2915+
2916+
it('CONTROL — a node reached only by a `fault` edge or a conditional edge is reachable', () => {
2917+
const result = FlowSchema.safeParse(flowOf([start, step('work'), step('recover'), step('big'), end], [
2918+
{ id: 'e1', source: 'start', target: 'work' },
2919+
{ id: 'e2', source: 'work', target: 'end' },
2920+
{ id: 'e3', source: 'work', target: 'recover', type: 'fault' },
2921+
{ id: 'e4', source: 'recover', target: 'end' },
2922+
{ id: 'e5', source: 'start', target: 'big', condition: 'record.amount > 1' },
2923+
{ id: 'e6', source: 'big', target: 'end' },
2924+
]));
2925+
expect(result.success).toBe(true);
2926+
});
2927+
2928+
it('CONTROL — a `boundary_event`, and a node reached only through one, are not judged', () => {
2929+
const result = FlowSchema.safeParse(flowOf([
2930+
start,
2931+
step('work'),
2932+
{
2933+
id: 'on_error', type: 'boundary_event', label: 'On error',
2934+
boundaryConfig: { attachedToNodeId: 'work', eventType: 'error' },
2935+
},
2936+
step('recover'),
2937+
end,
2938+
], [
2939+
{ id: 'e1', source: 'start', target: 'work' },
2940+
{ id: 'e2', source: 'work', target: 'end' },
2941+
{ id: 'e3', source: 'on_error', target: 'recover' },
2942+
{ id: 'e4', source: 'recover', target: 'end' },
2943+
]));
2944+
expect(result.success).toBe(true);
2945+
});
2946+
2947+
it('CONTROL — a flow with no start node, and a graph with a dangling edge, are left to their own refusals', () => {
2948+
expect(FlowSchema.safeParse(flowOf([step('a'), end], [{ id: 'e1', source: 'a', target: 'end' }])).success)
2949+
.toBe(true);
2950+
const dangling = FlowSchema.safeParse(flowOf([start, end], [{ id: 'e1', source: 'start', target: 'node_1' }]));
2951+
expect(dangling.success).toBe(false);
2952+
if (dangling.success) return;
2953+
expect(dangling.error.issues.map((i) => i.path)).toEqual([['edges', 0, 'target']]);
2954+
});
2955+
});

‎packages/spec/src/automation/flow.zod.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,8 +1910,82 @@ export const FlowSchema = lazySchema(() => strictObject(
19101910
});
19111911
});
19121912
}
1913+
1914+
for (const refusal of unreachableTopLevelNodeRefusals(flow.nodes, flow.edges)) {
1915+
ctx.addIssue({ code: 'custom', path: ['nodes', refusal.index], message: refusal.message });
1916+
}
19131917
}));
19141918

1919+
/**
1920+
* The top-level nodes no edge path from the `start` node reaches, each with
1921+
* the refusal an author meets at `nodes[i]`.
1922+
*
1923+
* The engine runs a flow from its first `start` node and follows out-edges
1924+
* from there (`service-automation`'s `executeNode` → `traverseNext`); nothing
1925+
* else ever schedules a top-level node. So a node no edge path reaches — an
1926+
* edge forgotten while wiring, a node left behind when its in-edge was
1927+
* deleted — parses, registers, publishes and never runs. Refused here so the
1928+
* author meets it at every door the endpoint refusal above reaches.
1929+
*
1930+
* Scope, declared rather than discovered:
1931+
*
1932+
* - The TOP-LEVEL graph only. A region body (`loop` / `try_catch` /
1933+
* `parallel`) is already single-entry and acyclic under ADR-0031
1934+
* (`analyzeRegion`), and in a DAG with one source every node is reachable
1935+
* from it, so a region cannot hold an unreachable node.
1936+
* - A flow with no `start` node is not judged: it has no run to reach anything
1937+
* from, and the engine refuses to run it (`FLOW_NO_START_NODE`).
1938+
* - Every edge counts, whatever its `type` or `condition`: a `fault` edge or a
1939+
* conditional edge is a path the engine can take.
1940+
* - A graph holding an edge whose endpoint names no top-level node is not
1941+
* judged: the endpoint rule above refuses that edge, and the node it was
1942+
* meant to reach would otherwise be reported a second time, for the same
1943+
* fault, under a cure (add an edge) that is not the one to apply.
1944+
* - A `boundary_event` is exempt, and so is a node reached only through one:
1945+
* it is attached to its host by `boundaryConfig.attachedToNodeId`, not
1946+
* entered by an edge, and its own contract owns its refusal.
1947+
*/
1948+
function unreachableTopLevelNodeRefusals(
1949+
nodes: ReadonlyArray<{ id: string; type: string }>,
1950+
edges: ReadonlyArray<{ source: string; target: string }>,
1951+
): Array<{ index: number; message: string }> {
1952+
const start = nodes.find((node) => node.type === 'start');
1953+
if (start === undefined) return [];
1954+
const ids = new Set(nodes.map((node) => node.id));
1955+
if (edges.some((edge) => !ids.has(edge.source) || !ids.has(edge.target))) return [];
1956+
const outgoing = new Map<string, string[]>();
1957+
for (const edge of edges) {
1958+
const targets = outgoing.get(edge.source) ?? [];
1959+
targets.push(edge.target);
1960+
outgoing.set(edge.source, targets);
1961+
}
1962+
const reached = new Set<string>();
1963+
const visit = (from: readonly string[]): void => {
1964+
const queue = [...from];
1965+
while (queue.length > 0) {
1966+
const id = queue.shift()!;
1967+
if (reached.has(id)) continue;
1968+
reached.add(id);
1969+
queue.push(...(outgoing.get(id) ?? []));
1970+
}
1971+
};
1972+
visit([start.id]);
1973+
visit(nodes.filter((node) => node.type === 'boundary_event').map((node) => node.id));
1974+
const refusals: Array<{ index: number; message: string }> = [];
1975+
nodes.forEach((node, index) => {
1976+
if (reached.has(node.id)) return;
1977+
refusals.push({
1978+
index,
1979+
message:
1980+
`Node \`${node.id}\` (\`${node.type}\`, \`nodes[${index}]\`) is not reachable: no edge path leads to it ` +
1981+
`from the start node \`${start.id}\`. A run starts at the start node and follows edges, so this node ` +
1982+
'never runs, while the flow registers and reports itself as configured. Add an edge into it from a ' +
1983+
'node that runs, or delete the node.',
1984+
});
1985+
});
1986+
return refusals;
1987+
}
1988+
19151989
/**
19161990
* The edge list a `FlowGraph` was read from, as the author wrote it —
19171991
* `graph.path` is the key path from the flow root to the object holding it.

0 commit comments

Comments
 (0)