From 5ace566baa0412434a65e89b78e2d72bd3c75651 Mon Sep 17 00:00:00 2001 From: Daniil Pokrovsky Date: Fri, 4 Dec 2026 19:09:04 +0700 Subject: [PATCH] feat: implement local-fail-a-run-whose-delivery-halted-even-wh - Fail a run whose delivery halted even when the incomplete summary cannot be written --- .../src/lib/agent/run-harness-finalize.ts | 4 +- packages/code/src/lib/cli/context.ts | 6 ++ packages/code/src/lib/task/pipeline.ts | 11 ++- .../code/tests/pipeline-pr-delivery.test.ts | 77 ++++++++++++++++++- 4 files changed, 93 insertions(+), 5 deletions(-) diff --git a/packages/code/src/lib/agent/run-harness-finalize.ts b/packages/code/src/lib/agent/run-harness-finalize.ts index 2fd4f2eb..8b50f9f8 100644 --- a/packages/code/src/lib/agent/run-harness-finalize.ts +++ b/packages/code/src/lib/agent/run-harness-finalize.ts @@ -628,12 +628,12 @@ async function reportPipelineHalt( reason: string, options: { countAttempt?: boolean } = {}, ): Promise { - // The shutdown handler already reported the interruption; a second comment - // and a retry-gate entry would contradict it. + // The shutdown handler already reported this; a second report would contradict it. if (isShuttingDown()) { console.log(`⚠️ Pipeline halted during shutdown: ${reason}`); return; } + if (runContext.activeTaskContext) runContext.activeTaskContext.deliveryHalt = reason; // survives a failed file write const ctx = state.context; const { taskKey, tracker, task } = ctx; const report = `Pipeline stopped: ${reason}\n\n${state.output}`; diff --git a/packages/code/src/lib/cli/context.ts b/packages/code/src/lib/cli/context.ts index b250632c..cafcc951 100644 --- a/packages/code/src/lib/cli/context.ts +++ b/packages/code/src/lib/cli/context.ts @@ -41,6 +41,12 @@ export interface ActiveTaskContext { * with. */ noopDeliveryReason?: string; + /** + * Set when the delivery pipeline halted after the agent finished (verify, + * repair, or a plugin step): the halt reason. Kept in memory so the run + * escalates even when the incomplete-summary file cannot be written. + */ + deliveryHalt?: string; } /** A PR that was requested after a successful push but could not be created. */ diff --git a/packages/code/src/lib/task/pipeline.ts b/packages/code/src/lib/task/pipeline.ts index 44836985..69f2035b 100644 --- a/packages/code/src/lib/task/pipeline.ts +++ b/packages/code/src/lib/task/pipeline.ts @@ -303,7 +303,10 @@ export async function settleImplementationRun(input: { outcome: ReturnType["outcome"]; }): Promise { const { tracker, task, workflowKey, taskDir, outcome } = input; - const implementationIncomplete = input.incomplete; + // The in-memory halt flag backs up the incomplete-summary file, which may + // have failed to write. + const implementationIncomplete = + input.incomplete || runContext.activeTaskContext?.deliveryHalt !== undefined; // The agent's own outcome marker decides between success and escalation // for a clean run, and a requested PR that failed after the push turns it // into a failure. Resolve it before any tracker transition so a blocked or @@ -323,7 +326,11 @@ export async function settleImplementationRun(input: { : { status: "succeeded", reason: noopDeliveryReason }, runContext.activeTaskContext?.prDeliveryFailure?.message, ); - if (isMarkdownTaskTracker(tracker) && disposition.status === "succeeded") { + if ( + isMarkdownTaskTracker(tracker) && + disposition.status === "succeeded" && + !implementationIncomplete + ) { await tracker.markDoneIfSuccessful(workflowKey, taskDir); } // A closed stacked base escalates even without the incomplete summary (skipped during shutdown). diff --git a/packages/code/tests/pipeline-pr-delivery.test.ts b/packages/code/tests/pipeline-pr-delivery.test.ts index 69483efd..764944db 100644 --- a/packages/code/tests/pipeline-pr-delivery.test.ts +++ b/packages/code/tests/pipeline-pr-delivery.test.ts @@ -16,7 +16,12 @@ import { runContext } from "../src/lib/cli/context"; import type { PrDeliveryFailure } from "../src/lib/cli/context"; import { RUN_ORIGIN_ENV } from "../src/lib/observability/analytics"; import { getRetryState, recordIncompleteAttempt } from "../src/lib/state/retry-state"; -import { RunStore, beginRun, getActiveRunContext } from "../src/lib/state/run-recorder"; +import { + RunStore, + beginRun, + getActiveRunContext, + noteRunFailure, +} from "../src/lib/state/run-recorder"; import { WorkerState } from "../src/lib/state/worker-state"; import { prDeliveryFailuresKey } from "../src/lib/task/actioned-without-pr"; import { settleImplementationRun } from "../src/lib/task/pipeline"; @@ -258,6 +263,76 @@ describe("settleImplementationRun for a stacked run whose base closed", () => { }); }); +describe("settleImplementationRun for a delivery halt", () => { + function startHalted(taskKey: string, deliveryHalt: string | undefined): number { + beginRun({ origin: "task", taskKey }); + const runId = getActiveRunContext()!.id; + runContext.activeTaskContext = { + taskKey, + tracker: trackerStub(), + projectKey: "PROJ", + movedToInProgress: true, + ...(deliveryHalt === undefined ? {} : { deliveryHalt }), + }; + return runId; + } + + test("escalates and keeps a markdown task out of Done when the summary file is missing", async () => { + const doneCalls: string[] = []; + const runId = startHalted("PROJ-HALT", "Verification failed twice: ENOSPC"); + + await settleImplementationRun({ + tracker: markdownTrackerStub(doneCalls), + task: task as never, + workflowKey: "PROJ-HALT", + taskDir: workDir, + incomplete: false, + outcome: undefined, + }); + + const run = readRun(runId); + expect(run?.status).toBe("escalated"); + expect(run?.outcomeReason).toBe("implementation incomplete; handed back to a human"); + expect(doneCalls).toEqual([]); + }); + + test("a failure noted at the verify stage still wins over the default class", async () => { + const runId = startHalted("PROJ-HALT2", "Verification failed twice"); + noteRunFailure({ outcomeClass: "internal", stage: "verify", cause: "ENOSPC" }); + + await settleImplementationRun({ + tracker: trackerStub(), + task: task as never, + workflowKey: "PROJ-HALT2", + taskDir: workDir, + incomplete: true, + outcome: undefined, + }); + + const run = readRun(runId); + expect(run?.status).toBe("escalated"); + expect(run?.outcomeClass).toBe("internal"); + expect(run?.outcomeReason).toBe("verify: ENOSPC"); + }); + + test("without a halt a clean run still succeeds and marks the markdown task Done", async () => { + const doneCalls: string[] = []; + const runId = startHalted("PROJ-OK", undefined); + + await settleImplementationRun({ + tracker: markdownTrackerStub(doneCalls), + task: task as never, + workflowKey: "PROJ-OK", + taskDir: workDir, + incomplete: false, + outcome: undefined, + }); + + expect(readRun(runId)?.status).toBe("succeeded"); + expect(doneCalls).toEqual(["PROJ-OK"]); + }); +}); + describe("settleImplementationRun for a no-change run accepted on a noop marker", () => { test("records a succeeded run whose reason starts with noop:", async () => { const doneCalls: string[] = [];