From 8e1fff5045fc2144d05b04078aea9c9f2e3e2025 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 23 Sep 2026 06:29:21 +0000 Subject: [PATCH] test(service-automation): witness parallel fan-out structurally, not by clock MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The parallel-branches case graded a real `Date.now()` window against a fixed 100 ms bound, which is wrong in both directions at once: the sequential path it claims to rule out (two 10 ms branches, measured 23 ms here) fits well inside the bound, so the assertion stays green through the very regression it documents; and because the graded quantity is real elapsed time (measured 13 ms here), a contended runner reddens a correct parallel engine. Parallelism is observable structurally. The registered executor now records an enter/exit trace of the branch bodies, and the case asserts that everything preceding the first branch return is a branch ENTRY — true exactly when both bodies are in flight at once. A sequentialised fan-out traces `enter:branch_a, exit:branch_a, …` and fails at any runner speed; no clock is read and no bound is chosen, so runner speed cannot reach the assertion. Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01AhQASwqJr2Z7XfGWUdvnbF --- .../service-automation/src/engine.test.ts | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/packages/services/service-automation/src/engine.test.ts b/packages/services/service-automation/src/engine.test.ts index 00a3938c08b..d178c51e1f4 100644 --- a/packages/services/service-automation/src/engine.test.ts +++ b/packages/services/service-automation/src/engine.test.ts @@ -2322,12 +2322,19 @@ describe('AutomationEngine - Parallel Branch Execution', () => { it('should execute unconditional branches in parallel', async () => { const executionOrder: string[] = []; + // Entry/exit trace of the branch bodies, recorded by the executor the + // engine calls — the observable that makes the fan-out's SHAPE readable. + // `executionOrder` alone records only that both branches ran, which a + // sequential engine satisfies just as well. + const trace: string[] = []; engine.registerNodeExecutor({ type: 'script', async execute(node) { + trace.push(`enter:${node.id}`); const delay = (node.config as any)?.delay ?? 0; await new Promise(r => setTimeout(r, delay)); + trace.push(`exit:${node.id}`); executionOrder.push(node.id); return { success: true }; }, @@ -2351,17 +2358,32 @@ describe('AutomationEngine - Parallel Branch Execution', () => { ], }); - const start = Date.now(); const result = await engine.execute('parallel_flow'); - const elapsed = Date.now() - start; expect(result.success).toBe(true); // Both branches should execute (order may vary in parallel) expect(executionOrder).toContain('branch_a'); expect(executionOrder).toContain('branch_b'); - // Parallel execution should be faster than sequential (10+10=20ms) - // Allow generous margin but expect it's faster than fully sequential - expect(elapsed).toBeLessThan(100); // generous but parallel should be ~15ms + + // What makes this fan-out parallel is that both branch bodies are IN + // FLIGHT AT ONCE — a structural fact about what the engine did, in what + // order, and ⛔ never a wall-clock window. `Promise.all` over eagerly + // created tasks enters every branch body before awaiting any of them, + // so everything preceding the FIRST branch return is an entry, and both + // entries are among them. An engine that awaited the branches one at a + // time traces `enter:branch_a, exit:branch_a, enter:branch_b, …`, whose + // slice below holds a single entry — red at ANY runner speed. + // + // ⛔ A clocked bound cannot witness this property in either direction: + // the sequential path it claims to rule out (two 10 ms branches) fits + // inside any bound generous enough for a contended runner, so such an + // assertion is green on the regression AND red on a slow box. Nothing + // below reads a clock or picks a bound; a branch's delay only stretches + // the wait between its own entry and its own exit, and can never + // reorder an entry across a sibling's return. + const firstReturn = trace.findIndex(event => event.startsWith('exit:')); + const beforeFirstReturn = firstReturn === -1 ? trace : trace.slice(0, firstReturn); + expect([...beforeFirstReturn].sort()).toEqual(['enter:branch_a', 'enter:branch_b']); }); });