From 2588ec1fb8afd59c70c8d7f24711b1b9b8ef612b Mon Sep 17 00:00:00 2001 From: Dotta Date: Thu, 12 Nov 2026 15:09:53 -0600 Subject: [PATCH] fix(recovery): promote saved input behind a run the fresh continuation replaced When the fallback continues a task with delivery saved_input, the person's deferred follow-up is the task's next turn. The stranded-queue pass refused to promote it because the replaced native run's finalization is terminal_failure (restart recovery ended it), not committed, and the release path would also have re-blocked the task with a reconciliation incident. Both now skip a run the fallback has superseded. Co-Authored-By: Claude Opus 5.5 --- server/src/modules/wake-queue/adapters/postgres.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/server/src/modules/wake-queue/adapters/postgres.ts b/server/src/modules/wake-queue/adapters/postgres.ts index 95341f6bf18..93e5c6c5496 100644 --- a/server/src/modules/wake-queue/adapters/postgres.ts +++ b/server/src/modules/wake-queue/adapters/postgres.ts @@ -18,6 +18,7 @@ import { nativeManualAdmissionHoldsExecution } from "../../../services/native-ru import { and, asc, eq, inArray, isNull, notInArray, or, sql } from "drizzle-orm"; import { readReleaseRun, type ReleaseRun } from "./release-run-read.js"; import { isGovernancePauseStop } from "../../../services/governance-pause-stop.js"; +import { agentRunSupersededByFreshContinuation } from "../../../agent-run-cancellation.js"; import type { Db } from "@paperclipai/db"; import { deliveredQueuedCommentIds, EXTERNAL_CHAT_MESSAGE_WAKE_MUTATION, EXTERNAL_CHAT_MESSAGE_WAKE_REASON } from "../../../services/queued-comment-delivery.js"; import { extractIssueReferenceIdentifiers, isUuidLike } from "@paperclipai/shared"; @@ -1284,7 +1285,10 @@ export function createPostgresWakeQueueAdapter(db: Db, deps: WakeQueuePostgresAd `, ); - if (input.resumeStrandedQueue && run.runtimeMode === "native") { + // A run the fresh-continuation fallback replaced is settled for good: its + // finalization may stay terminal_failure (restart recovery ended it), and + // the saved input queued behind it is now the task's next turn. + if (input.resumeStrandedQueue && run.runtimeMode === "native" && !agentRunSupersededByFreshContinuation(run)) { const [finalized] = contextIssueId && run.nativeIssueId === contextIssueId ? await tx.select({ runId: nativeRunFinalizations.runId }).from(nativeRunFinalizations).where(and( eq(nativeRunFinalizations.companyId, input.companyId), eq(nativeRunFinalizations.runId, run.id), @@ -1448,7 +1452,10 @@ export function createPostgresWakeQueueAdapter(db: Db, deps: WakeQueuePostgresAd && issueRow.status === "blocked" && issueRow.assigneeAgentId === run.agentId) { return { outcome: { kind: "released" }, postCommitEffects: [], run: runSnapshot }; } - if (!handoffAssigneeAgentId && await recordNativeTerminalRecoveryIfNeeded(tx, run, issueRow, input.now)) { + // The fallback already decided this run's continuation; a reconciliation + // incident would re-block the task it just released. + if (!handoffAssigneeAgentId && !agentRunSupersededByFreshContinuation(run) + && await recordNativeTerminalRecoveryIfNeeded(tx, run, issueRow, input.now)) { return { outcome: { kind: "released" }, postCommitEffects: [], run: runSnapshot }; }