diff --git a/server/src/__tests__/heartbeat-process-recovery.test.ts b/server/src/__tests__/heartbeat-process-recovery.test.ts index 19eec94b7dc..95ed9b49c06 100644 --- a/server/src/__tests__/heartbeat-process-recovery.test.ts +++ b/server/src/__tests__/heartbeat-process-recovery.test.ts @@ -15455,6 +15455,30 @@ describeEmbeddedPostgres("heartbeat orphaned process recovery", () => { expect(mockAdapterExecute).not.toHaveBeenCalled(); }); + it("does not queue a continuation from a latest run it read while that run was still finishing", async () => { + const { companyId, issueId, runId } = await seedStrandedIssueFixture({ + status: "in_progress", + runStatus: "succeeded", + livenessState: "advanced", + }); + await db.update(heartbeatRuns).set({ status: "running", finishedAt: null }).where(eq(heartbeatRuns.id, runId)); + await db.update(issues).set({ executionRunId: runId }).where(eq(issues.id, issueId)); + const enqueueWakeup = vi.fn(async () => null); + const recovery = recoveryService(db, { + enqueueWakeup, + afterStrandedLatestRunRead: async (read) => { + if (read.runId !== runId) return; + await db.update(heartbeatRuns).set({ status: "succeeded", finishedAt: new Date() }).where(eq(heartbeatRuns.id, runId)); + await db.update(issues).set({ executionRunId: null, checkoutRunId: null }).where(eq(issues.id, issueId)); + }, + }); + + const result = await recovery.reconcileStrandedAssignedIssues({ companyId, issueId }); + + expect(result.continuationRequeued).toBe(0); + expect(enqueueWakeup).not.toHaveBeenCalled(); + }); + it("leaves a chat turn that yielded to wait for a person without starting another run", async () => { const fixture = await seedNativePassiveBoardResponse("response_wake", { blockingWork: true, objectiveUnsatisfied: true }); const previousSettings = await instanceSettingsService(db).getExperimental(); diff --git a/server/src/services/recovery/service.ts b/server/src/services/recovery/service.ts index e9c5e148fd1..43c5e890322 100644 --- a/server/src/services/recovery/service.ts +++ b/server/src/services/recovery/service.ts @@ -998,6 +998,7 @@ export function recoveryService( settleLostRunProcess?: (scope: { companyId: string; runId: string; onDeferredDeadline?: (at: number) => void }) => Promise; beforeOrphanedRunTerminalWrite?: (runId: string) => Promise; beforeStaleLockClear?: (issueId: string) => Promise; + afterStrandedLatestRunRead?: (input: { issueId: string; runId: string | null }) => Promise; }, ) { const issuesSvc = issueService(db); @@ -4835,6 +4836,7 @@ export function recoveryService( } let latestRun = await getLatestIssueRun(issue.companyId, issue.id); + await deps.afterStrandedLatestRunRead?.({ issueId: issue.id, runId: latestRun?.id ?? null }); // The run's own executor still settles it: it schedules the bounded retry // and releases the claim itself. Escalating in that window clears the // claim the retry needs. Re-check once its controller lease lapses. @@ -4977,6 +4979,10 @@ export function recoveryService( result.skipped += 1; continue; } + if (issue.status !== "in_review" && latestRun && !isTerminalIssueRun(latestRun)) { + result.skipped += 1; + continue; + } if (await hasPendingWakeInteraction(issue.companyId, issue.id)) { result.skipped += 1;