diff --git a/server/src/__tests__/heartbeat-process-recovery.helper.ts b/server/src/__tests__/heartbeat-process-recovery.helper.ts index 9b62be444ca..b93961705b6 100644 --- a/server/src/__tests__/heartbeat-process-recovery.helper.ts +++ b/server/src/__tests__/heartbeat-process-recovery.helper.ts @@ -7836,7 +7836,7 @@ describeEmbeddedPostgres(`heartbeat orphaned process recovery (part ${part}/4)`, expect(repairs).toHaveLength(1); const repair = await heartbeat.getRun(repairs[0].runId!); expect(repair?.contextSnapshot).toMatchObject({ - legacyDispositionEpisode: { id: runId, attempt: 1, maxAttempts: 2 }, + legacyDispositionEpisode: { id: runId, attempt: 1, maxAttempts: 5 }, dispositionRepairInstruction: LEGACY_DISPOSITION_REPAIR_INSTRUCTION, }); expect({ status: repair?.status, errorCode: repair?.errorCode, error: repair?.error }) @@ -7904,7 +7904,7 @@ describeEmbeddedPostgres(`heartbeat orphaned process recovery (part ${part}/4)`, expect(repairs).toHaveLength(1); const repair = await heartbeat.getRun(repairs[0].runId!); expect(repair?.contextSnapshot).toMatchObject({ - legacyDispositionEpisode: { id: runId, attempt: 1, maxAttempts: 2 }, + legacyDispositionEpisode: { id: runId, attempt: 1, maxAttempts: 5 }, dispositionRepairInstruction: LEGACY_DISPOSITION_REPAIR_INSTRUCTION, }); expect(repair?.status).toBe("succeeded"); @@ -7995,7 +7995,7 @@ describeEmbeddedPostgres(`heartbeat orphaned process recovery (part ${part}/4)`, expect(repairs).toHaveLength(1); const repair = await heartbeat.getRun(repairs[0].runId!); expect(repair?.contextSnapshot).toMatchObject({ - legacyDispositionEpisode: { id: runId, attempt: 1, maxAttempts: 2 }, + legacyDispositionEpisode: { id: runId, attempt: 1, maxAttempts: 5 }, dispositionRepairInstruction: LEGACY_DISPOSITION_REPAIR_INSTRUCTION, }); expect(repair?.status).toBe("succeeded"); @@ -8050,7 +8050,7 @@ describeEmbeddedPostgres(`heartbeat orphaned process recovery (part ${part}/4)`, expect(repairs).toHaveLength(1); const repair = await heartbeat.getRun(repairs[0].runId!); expect(repair?.contextSnapshot).toMatchObject({ - legacyDispositionEpisode: { id: runId, attempt: 1, maxAttempts: 2 }, + legacyDispositionEpisode: { id: runId, attempt: 1, maxAttempts: 5 }, dispositionRepairInstruction: LEGACY_DISPOSITION_REPAIR_INSTRUCTION, }); expect(repair?.status).toBe("succeeded"); diff --git a/server/src/__tests__/legacy-continuation-authority.test.ts b/server/src/__tests__/legacy-continuation-authority.test.ts index 2581f087528..2e21adcfccc 100644 --- a/server/src/__tests__/legacy-continuation-authority.test.ts +++ b/server/src/__tests__/legacy-continuation-authority.test.ts @@ -142,7 +142,7 @@ describe("legacy continuation persisted authority", () => { await f.createRecovery().reconcileLegacyContinuation(f.runId); expect(await f.runs()).toHaveLength(2); const repair = (await f.runs()).find(r => r.id !== f.runId)!; - expect(repair.contextSnapshot).toMatchObject({ legacyDispositionEpisode: { id: f.runId, attempt: 1, maxAttempts: 2 } }); + expect(repair.contextSnapshot).toMatchObject({ legacyDispositionEpisode: { id: f.runId, attempt: 1, maxAttempts: 5 } }); }); it("does not roll back the ledger when a fast repair finishes before enqueue returns", async () => { const f = await fixture(); @@ -157,6 +157,13 @@ describe("legacy continuation persisted authority", () => { }); it("keeps the same bounded episode across restart and commentary, then escalates only after agent attempts", async () => { const f = await fixture(); + // Stamp a genuine pre-upgrade episode on the source run: this test + // pins the legacy bounded behavior, which now applies only to runs + // that carry a legacy episode (marker-less runs get the documented + // five-attempt budget instead). + await db.update(heartbeatRuns).set({ + contextSnapshot: { issueId: f.issueId, legacyDispositionEpisode: { id: f.runId, attempt: 0, maxAttempts: 2 } }, + }).where(eq(heartbeatRuns.id, f.runId)); await f.createRecovery().reconcileLegacyContinuation(f.runId); const first = (await f.runs()).find(r => r.id !== f.runId)!; await f.finish(first); @@ -173,6 +180,11 @@ describe("legacy continuation persisted authority", () => { }); it.each(["silent", "comments", "tool-calls"])("ACCT-02 full recovery sweep cannot replenish an exhausted episode with %s", async noise => { const f = await fixture(); + // Stamp a genuine pre-upgrade episode: exhaustion finality is pinned + // here under the legacy bound (marker-less runs now get five attempts). + await db.update(heartbeatRuns).set({ + contextSnapshot: { issueId: f.issueId, legacyDispositionEpisode: { id: f.runId, attempt: 0, maxAttempts: 2 } }, + }).where(eq(heartbeatRuns.id, f.runId)); await f.createRecovery().reconcileLegacyContinuation(f.runId); const first = (await f.runs()).find(r => r.id !== f.runId)!; await f.finish(first); @@ -237,6 +249,11 @@ describe("legacy continuation persisted authority", () => { it.each(["foreign-source", "changed-episode", "exhausted-slot"])("ACCT-04 rejects a delayed repair with %s", async mutation => { const f = await fixture(); + // Stamp a genuine pre-upgrade episode so the exhausted-slot mutation + // (attempt 3) exceeds the episode bound it is rejected against. + await db.update(heartbeatRuns).set({ + contextSnapshot: { issueId: f.issueId, legacyDispositionEpisode: { id: f.runId, attempt: 0, maxAttempts: 2 } }, + }).where(eq(heartbeatRuns.id, f.runId)); await f.createRecovery().reconcileLegacyContinuation(f.runId); const first = (await f.runs()).find(r => r.id !== f.runId)!; await f.finish(first); diff --git a/server/src/services/recovery/legacy-continuation.test.ts b/server/src/services/recovery/legacy-continuation.test.ts index e4cd9a38374..355c8b479db 100644 --- a/server/src/services/recovery/legacy-continuation.test.ts +++ b/server/src/services/recovery/legacy-continuation.test.ts @@ -49,4 +49,28 @@ describe("legacy continuation authority", () => { it("leaves native finalization to its own authority", () => { expect(decideLegacyContinuation({ ...input, run: { ...input.run, runtimeMode: "native" } }).kind).toBe("skip"); }); + it("gives a run with no legacy markers the documented five-attempt budget", () => { + // Such a run has spent nothing under the legacy scheme, so the legacy + // cap must not apply to it through the episode fallback. + expect(legacyDispositionEpisode({ id: "run-1" })) + .toMatchObject({ id: "run-1", attempt: 0, maxAttempts: 5 }); + expect(legacyDispositionEpisode({ id: "run-1", contextSnapshot: {} })) + .toMatchObject({ id: "run-1", attempt: 0, maxAttempts: 5 }); + expect(legacyDispositionEpisode({ id: "run-1", contextSnapshot: { issueId: "issue" }, continuationAttempt: 0 })) + .toMatchObject({ id: "run-1", attempt: 0, maxAttempts: 5 }); + }); + it("keeps the budget a persisted episode record was written with", () => { + expect(legacyDispositionEpisode({ id: "old", contextSnapshot: { legacyDispositionEpisode: { id: "ep", attempt: 1, maxAttempts: 2 } } })) + .toMatchObject({ id: "ep", attempt: 1, maxAttempts: 2 }); + expect(legacyDispositionEpisode({ id: "new", contextSnapshot: { legacyDispositionEpisode: { id: "ep", attempt: 1, maxAttempts: 5 } } })) + .toMatchObject({ id: "ep", attempt: 1, maxAttempts: 5 }); + }); + it("still derives an episode for marked or partly spent pre-upgrade runs", () => { + expect(legacyDispositionEpisode({ id: "old", contextSnapshot: { dispositionRepairFingerprint: "old-episode" } })) + .toMatchObject({ id: "old-episode", attempt: 0, maxAttempts: 2 }); + expect(legacyDispositionEpisode({ id: "old", contextSnapshot: { livenessContinuationSourceRunId: "source-run" } })) + .toMatchObject({ id: "source-run", attempt: 0, maxAttempts: 2 }); + expect(legacyDispositionEpisode({ id: "old", continuationAttempt: 1 })) + .toMatchObject({ id: "old", attempt: 1, maxAttempts: 2 }); + }); }); diff --git a/server/src/services/recovery/legacy-continuation.ts b/server/src/services/recovery/legacy-continuation.ts index dc319eaedd3..f84155340fe 100644 --- a/server/src/services/recovery/legacy-continuation.ts +++ b/server/src/services/recovery/legacy-continuation.ts @@ -1,4 +1,5 @@ import { createHash } from "node:crypto"; +import { DISPOSITION_REPAIR_MAX_ATTEMPTS } from "./disposition-repair.js"; /** This contract deliberately has no narrative, liveness label or progress count. */ export interface LegacyContinuationInput { @@ -43,13 +44,31 @@ export function legacyDispositionEpisode(run: { const context = record(run.contextSnapshot); const episode = record(context.legacyDispositionEpisode); if (typeof episode.id === "string" && episode.id) { - return { id: episode.id, attempt: count(episode.attempt), maxAttempts: Math.max(1, Math.min(LEGACY_DISPOSITION_REPAIR_MAX_ATTEMPTS, count(episode.maxAttempts) || LEGACY_DISPOSITION_REPAIR_MAX_ATTEMPTS)) }; + // The persisted record is authoritative for its own budget. Records + // written under the legacy scheme carry its cap and keep it, so a + // pre-upgrade episode is never granted a fresh budget. Records + // written under the documented scheme carry that budget and keep it + // too; clamping them here would silently re-cap new episodes at the + // legacy allowance from their second attempt on. + return { id: episode.id, attempt: count(episode.attempt), maxAttempts: Math.max(1, count(episode.maxAttempts) || LEGACY_DISPOSITION_REPAIR_MAX_ATTEMPTS) }; } // Pre-upgrade repairs have already consumed their old (possibly tighter) // allowance. A classifier/key migration must not grant them a fresh budget. const handoff = context.wakeReason === "finish_successful_run_handoff" || context.handoffRequired === true; const oldRecovery = context.source === "issue.productive_terminal_continuation_recovery"; const attempt = Math.max(count(run.continuationAttempt), count(context.livenessContinuationAttempt), count(context.dispositionRepairAttempt), handoff ? count(context.handoffAttempt) || 1 : 0, oldRecovery ? 1 : 0); + // A run with no legacy markers and no consumed attempts has spent + // nothing under any older scheme, so it is a new episode and gets the + // documented disposition-repair budget, not the legacy cap. The episode + // object still exists: repair dispatch relies on it for the persisted + // binding, the issue state guard, and source-identity checks, and those + // semantics must not depend on which budget a run falls under. + const hasLegacyMarker = handoff || oldRecovery + || typeof context.dispositionRepairFingerprint === "string" + || typeof context.livenessContinuationSourceRunId === "string"; + if (!hasLegacyMarker && attempt === 0) { + return { id: run.id, attempt: 0, maxAttempts: DISPOSITION_REPAIR_MAX_ATTEMPTS }; + } return { id: typeof context.dispositionRepairFingerprint === "string" ? context.dispositionRepairFingerprint : typeof context.livenessContinuationSourceRunId === "string" ? context.livenessContinuationSourceRunId : run.id, attempt,