diff --git a/src/manager.ts b/src/manager.ts index 1e7e77c..21c65cf 100644 --- a/src/manager.ts +++ b/src/manager.ts @@ -618,6 +618,9 @@ export class SubagentManager { } private finalize(execution: Execution, status: JobStatus): void { + // One terminal transition per job: cancellation commits the status from + // requestCancellation, and a run supervisor's later wake-up finalize is a + // no-op rather than a second checkpoint and journal entry. if (isTerminalJob(execution.status)) return; execution.status = execution.cancelRequested && status !== "interrupted" ? "cancelled" : status; execution.finishedAt = this.now(); diff --git a/test/workflow-journal.test.ts b/test/workflow-journal.test.ts index 2529303..1c7b776 100644 --- a/test/workflow-journal.test.ts +++ b/test/workflow-journal.test.ts @@ -19,7 +19,7 @@ import { import { prepareWorkflowInvocation, type PrepareContext } from "../src/prepare.ts"; import { parseWorkflow, type WorkflowPlan, type WorkflowSource } from "../src/workflow-plan.ts"; import { emptyUsage, type JobSnapshot, type TaskSnapshot, type TaskStatus, type WorkflowJournal, type WorkflowJournalItem, type WorkflowJournalStep } from "../src/types.ts"; -import { FakeRunner, waitForSnapshot } from "./helpers.ts"; +import { FakeRunner, invocation, waitForSnapshot } from "./helpers.ts"; const tempRoots: string[] = []; @@ -396,6 +396,50 @@ test("a cancelled run journals once, with every unfinished step settled cancelle ); }); +test("cancelling while children are FIFO-queued finalizes once and persists one terminal checkpoint", async () => { + const { ctx } = makeWorkspace(); + const plan = makePlan([ + { id: "scan", agent: "scout", task: "scan" }, + { id: "audit", agent: "scout", task: "audit {steps.scan.output}", needs: ["scan"] }, + ]); + const runner = new FakeRunner(); + const events: string[] = []; + const journals: WorkflowJournal[] = []; + const checkpoints: JobSnapshot[] = []; + const manager = new SubagentManager(runner, { + maxWorkers: 2, + onJobEvent: (event) => { + events.push(`${event.jobId}:${event.kind}`); + if (event.kind === "finalized") checkpoints.push(manager.get(event.jobId)); + }, + onWorkflowJournal: (journal) => journals.push(journal), + }); + // Both workers are busy, so the run's first step waits in the shared FIFO + // while its second still waits on the DAG. + manager.startAsync(invocation()); + manager.startAsync(invocation()); + const started = manager.startWorkflow( + await prepareWorkflowInvocation(plan, {}, ctx, { agentScope: "project", confirmProjectAgents: false }), + ); + assert.deepEqual(manager.get(started.id).results.map((row) => row.status), ["queued", "queued"]); + + manager.cancel(started.id); + assert.equal((await waitForSnapshot(manager, started.id, 1)).status, "cancelled"); + + const runEvents = events.filter((event) => event.startsWith(`${started.id}:`)); + assert.deepEqual( + runEvents.filter((event) => event.endsWith(":finalized")), + [`${started.id}:finalized`], + "the supervisor's wake-up finalize is a no-op", + ); + assert.equal(journals.length, 1, "one terminal journal record"); + assert.equal(journals[0].status, "cancelled"); + const runCheckpoints = checkpoints.filter((checkpoint) => checkpoint.id === started.id); + assert.equal(runCheckpoints.length, 1, "one terminal checkpoint"); + assert.equal(runCheckpoints[0].status, "cancelled"); + await manager.shutdown(); +}); + test("an each-step journals per-item rows alongside its aggregate step record", async () => { const { ctx } = makeWorkspace(); const plan = makePlan([