diff --git a/scripts/approved_plan_continuity_gate.mjs b/scripts/approved_plan_continuity_gate.mjs index 38df9bd..8e93082 100755 --- a/scripts/approved_plan_continuity_gate.mjs +++ b/scripts/approved_plan_continuity_gate.mjs @@ -3,6 +3,23 @@ import fs from 'node:fs'; const LEGAL_TERMINAL_STATES = new Set(['waiting_user', 'blocked', 'pending_verification']); +function isNonEmptyString(value) { + return typeof value === 'string' && value.trim().length > 0; +} + +function isObject(value) { + return value != null && typeof value === 'object' && !Array.isArray(value); +} + +function hasValidDispatchReceipt(receipt) { + if (!isObject(receipt)) return false; + if (!isNonEmptyString(receipt.planId)) return false; + if (!isNonEmptyString(receipt.currentTask)) return false; + if (!isObject(receipt.nextDerivedAction)) return false; + if (!isNonEmptyString(receipt.dispatchedAt)) return false; + return true; +} + function parseArgs(argv) { let inputPath = null; let compact = false; @@ -59,7 +76,7 @@ function evaluateContinuity(payload) { const taskComplete = payload?.taskState === 'complete'; const nextAction = payload?.nextDerivedAction ?? payload?.derivedAction ?? null; const nextActionKnown = nextAction != null; - const hasDispatchReceipt = payload?.dispatchReceipt != null; + const hasDispatchReceipt = hasValidDispatchReceipt(payload?.dispatchReceipt ?? null); const closureState = payload?.replyClosureState ?? null; const isLegalTerminalState = LEGAL_TERMINAL_STATES.has(closureState); diff --git a/scripts/test_approved_plan_continuity_gate.mjs b/scripts/test_approved_plan_continuity_gate.mjs index 3183ced..bb8ac25 100644 --- a/scripts/test_approved_plan_continuity_gate.mjs +++ b/scripts/test_approved_plan_continuity_gate.mjs @@ -149,13 +149,11 @@ const tests = [ }); if (result.status !== 0 && result.status !== null) { - throw new Error(`expected controlled execution, got status=${result.status} -${result.stderr || result.stdout}`); + throw new Error(`expected controlled execution, got status=${result.status}\n${result.stderr || result.stdout}`); } if (!result.json || typeof result.json !== 'object') { - throw new Error(`expected JSON output -stdout=${result.stdout}`); + throw new Error(`expected JSON output\nstdout=${result.stdout}`); } if (result.json.ok !== false) { @@ -170,6 +168,54 @@ stdout=${result.stdout}`); } }, }, + { + name: 'continuity: fails when dispatchReceipt is a fake non-null object without minimum receipt fields', + run() { + const fixture = createFixture({ + 'input.json': { + planId: 'plan-fake-dispatch-receipt', + currentTask: 'task-6fake', + taskState: 'complete', + nextDerivedAction: { + type: 'message_subagent', + task: 'continue with task-7fake', + }, + replyClosureState: 'completed', + dispatchReceipt: { + fake: true, + }, + }, + }); + + try { + const result = runGate({ + args: ['--compact', '--input', fixture.path('input.json')], + }); + + if (result.status !== 0 && result.status !== null) { + throw new Error(`expected controlled execution, got status=${result.status}\n${result.stderr || result.stdout}`); + } + + if (!result.json || typeof result.json !== 'object') { + throw new Error(`expected JSON output\nstdout=${result.stdout}`); + } + + if (result.json.ok !== false) { + throw new Error(`expected continuity failure ok=false for fake dispatch receipt, got ${JSON.stringify(result.json)}`); + } + + if (result.json.verdict !== 'continuity_failure') { + throw new Error(`expected verdict=continuity_failure for fake dispatch receipt, got ${JSON.stringify(result.json.verdict)}`); + } + + if (result.json.reason !== 'missing_dispatch_receipt') { + throw new Error(`expected reason=missing_dispatch_receipt for fake dispatch receipt, got ${JSON.stringify(result.json.reason)}`); + } + } finally { + fixture.cleanup(); + } + }, + }, { name: 'continuity: passes when task is complete, next action is known, and a dispatch receipt already exists', @@ -202,13 +248,11 @@ stdout=${result.stdout}`); }); if (result.status !== 0 && result.status !== null) { - throw new Error(`expected controlled execution, got status=${result.status} -${result.stderr || result.stdout}`); + throw new Error(`expected controlled execution, got status=${result.status}\n${result.stderr || result.stdout}`); } if (!result.json || typeof result.json !== 'object') { - throw new Error(`expected JSON output -stdout=${result.stdout}`); + throw new Error(`expected JSON output\nstdout=${result.stdout}`); } if (result.json.ok !== true) { @@ -220,6 +264,53 @@ stdout=${result.stdout}`); }, }, + { + name: 'continuity: passes when planner returns derivedAction and a bound dispatch receipt already exists', + run() { + const fixture = createFixture({ + 'input.json': { + planId: 'plan-derived-action-with-bound-dispatch', + currentTask: 'task-6c', + taskState: 'complete', + derivedAction: { + type: 'message_subagent', + task: 'continue with task-7c', + }, + replyClosureState: 'completed', + dispatchReceipt: { + planId: 'plan-derived-action-with-bound-dispatch', + currentTask: 'task-6c', + nextDerivedAction: { + type: 'message_subagent', + task: 'continue with task-7c', + }, + dispatchedAt: '2026-04-24T12:05:00+08:00', + }, + }, + }); + + try { + const result = runGate({ + args: ['--compact', '--input', fixture.path('input.json')], + }); + + if (result.status !== 0 && result.status !== null) { + throw new Error(`expected controlled execution, got status=${result.status}\n${result.stderr || result.stdout}`); + } + + if (!result.json || typeof result.json !== 'object') { + throw new Error(`expected JSON output\nstdout=${result.stdout}`); + } + + if (result.json.ok !== true) { + throw new Error(`expected continuity pass ok=true when derivedAction has bound dispatch receipt, got ${JSON.stringify(result.json)}`); + } + } finally { + fixture.cleanup(); + } + }, + }, + { name: 'continuity: passes when task is complete, next action is known, no dispatch receipt exists, and closure is waiting_user', run() { @@ -243,13 +334,11 @@ stdout=${result.stdout}`); }); if (result.status !== 0 && result.status !== null) { - throw new Error(`expected controlled execution, got status=${result.status} -${result.stderr || result.stdout}`); + throw new Error(`expected controlled execution, got status=${result.status}\n${result.stderr || result.stdout}`); } if (!result.json || typeof result.json !== 'object') { - throw new Error(`expected JSON output -stdout=${result.stdout}`); + throw new Error(`expected JSON output\nstdout=${result.stdout}`); } if (result.json.ok !== true) { @@ -284,13 +373,11 @@ stdout=${result.stdout}`); }); if (result.status !== 0 && result.status !== null) { - throw new Error(`expected controlled execution, got status=${result.status} -${result.stderr || result.stdout}`); + throw new Error(`expected controlled execution, got status=${result.status}\n${result.stderr || result.stdout}`); } if (!result.json || typeof result.json !== 'object') { - throw new Error(`expected JSON output -stdout=${result.stdout}`); + throw new Error(`expected JSON output\nstdout=${result.stdout}`); } if (result.json.ok !== true) { @@ -325,13 +412,11 @@ stdout=${result.stdout}`); }); if (result.status !== 0 && result.status !== null) { - throw new Error(`expected controlled execution, got status=${result.status} -${result.stderr || result.stdout}`); + throw new Error(`expected controlled execution, got status=${result.status}\n${result.stderr || result.stdout}`); } if (!result.json || typeof result.json !== 'object') { - throw new Error(`expected JSON output -stdout=${result.stdout}`); + throw new Error(`expected JSON output\nstdout=${result.stdout}`); } if (result.json.ok !== true) {