diff --git a/server/src/__tests__/recovery-stale-issue-lock-sweep.test.ts b/server/src/__tests__/recovery-stale-issue-lock-sweep.test.ts index ec74dd0e310..8b3f105f88c 100644 --- a/server/src/__tests__/recovery-stale-issue-lock-sweep.test.ts +++ b/server/src/__tests__/recovery-stale-issue-lock-sweep.test.ts @@ -31,7 +31,13 @@ vi.mock("../telemetry.ts", () => ({ getTelemetryClient: () => mockTelemetryClien import { heartbeatService } from "../services/heartbeat.ts"; import { DELIVERY_QUEUES } from "../services/delivery-work-notifications.js"; -import { recoveryService } from "../services/recovery/service.ts"; +import { + ISSUE_TERMINAL_MIN_RUN_AGE_MS, + recoveryService, +} from "../services/recovery/service.ts"; + +const orphanedRunStartedAt = () => + new Date(Date.now() - ISSUE_TERMINAL_MIN_RUN_AGE_MS - 5_000); const external = resolveExternalTestDatabaseUrl("PAPERCLIP_STALE_LOCK_TEST_DATABASE_URL"); const embeddedPostgresSupport = external ? { supported: true, reason: undefined } : await getEmbeddedPostgresTestSupport(); @@ -76,7 +82,7 @@ describeEmbeddedPostgres("recovery sweepStaleIssueLocks", () => { await tempDb?.cleanup(); }); - async function seed() { + async function seed(options?: { runningRunStartedAt?: Date }) { const companyId = randomUUID(); const agentId = randomUUID(); const failedRunId = randomUUID(); @@ -114,7 +120,7 @@ describeEmbeddedPostgres("recovery sweepStaleIssueLocks", () => { agentId, status: "running", invocationSource: "manual", - startedAt: new Date(), + startedAt: options?.runningRunStartedAt ?? new Date(), }, ]); @@ -277,7 +283,7 @@ describeEmbeddedPostgres("recovery sweepStaleIssueLocks", () => { }); it("does not terminalize a session-goal control run solely because its issue is done", async () => { - const { companyId, agentId, runningRunId } = await seed(); + const { companyId, agentId, runningRunId } = await seed({ runningRunStartedAt: orphanedRunStartedAt() }); const issueId = randomUUID(); await db .update(heartbeatRuns) @@ -319,7 +325,7 @@ describeEmbeddedPostgres("recovery sweepStaleIssueLocks", () => { runtimeMode?: "native" | "legacy"; variant?: "ordinary" | "cancelled" | "wrong_child" | "wrong_reason" | "wrong_agent" | "unfinished_child" | "foreign_child"; } = {}) { - const { companyId, agentId, runningRunId } = await seed(); + const { companyId, agentId, runningRunId } = await seed({ runningRunStartedAt: orphanedRunStartedAt() }); const issueId = randomUUID(); const childIssueId = randomUUID(); const variant = options.variant; @@ -662,7 +668,7 @@ describeEmbeddedPostgres("recovery sweepStaleIssueLocks", () => { }); async function seedPendingNativeResult(phase: string, options: { terminalIssue?: boolean; deferCoordinator?: boolean } = {}) { - const fixture = await seed(); + const fixture = await seed(options.terminalIssue ? { runningRunStartedAt: orphanedRunStartedAt() } : undefined); const { companyId, agentId, runningRunId } = fixture; const issueId = randomUUID(); const contractId = randomUUID(); @@ -790,7 +796,9 @@ describeEmbeddedPostgres("recovery sweepStaleIssueLocks", () => { // the in-memory handle and the recorded pid can both persist. The // process-death authority misses this case. The issue-terminal authority // catches it: the issue reached "done" while the run row stayed "running". - const { companyId, agentId, runningRunId } = await seed(); + const { companyId, agentId, runningRunId } = await seed({ + runningRunStartedAt: orphanedRunStartedAt(), + }); // process.pid is the live test process, so isPidAlive returns true. await db .update(heartbeatRuns) @@ -851,7 +859,9 @@ describeEmbeddedPostgres("recovery sweepStaleIssueLocks", () => { }); it("terminalizes a running run to cancelled when its issue is cancelled (reuse-lease path)", async () => { - const { companyId, agentId, runningRunId } = await seed(); + const { companyId, agentId, runningRunId } = await seed({ + runningRunStartedAt: orphanedRunStartedAt(), + }); await db .update(heartbeatRuns) .set({ processPid: process.pid }) @@ -892,6 +902,79 @@ describeEmbeddedPostgres("recovery sweepStaleIssueLocks", () => { ); }); + it("does not terminalize a freshly started run under a momentarily terminal issue (reassignment hand-off)", async () => { + const { companyId, agentId, runningRunId } = await seed(); + const issueId = randomUUID(); + await db.insert(issues).values({ + id: issueId, + companyId, + title: "Hand-off — issue momentarily done while the next run starts", + status: "done", + priority: "high", + assigneeAgentId: agentId, + checkoutRunId: runningRunId, + executionRunId: runningRunId, + executionLockedAt: new Date(), + }); + + const heartbeat = heartbeatService(db); + const deadlines: number[] = []; + const result = await heartbeat.sweepStaleIssueLocks({ onDeferredDeadline: (at) => deadlines.push(at) }); + + expect(result).toEqual({ cleared: 0, issueIds: [], terminalizedRunIds: [] }); + + const [run] = await db + .select({ status: heartbeatRuns.status, startedAt: heartbeatRuns.startedAt }) + .from(heartbeatRuns) + .where(eq(heartbeatRuns.id, runningRunId)); + expect(run?.status).toBe("running"); + expect(deadlines).toContain(run!.startedAt!.getTime() + ISSUE_TERMINAL_MIN_RUN_AGE_MS); + + const lock = await db + .select({ + checkoutRunId: issues.checkoutRunId, + executionRunId: issues.executionRunId, + }) + .from(issues) + .where(eq(issues.id, issueId)) + .then((rows) => rows[0]); + expect(lock).toEqual({ + checkoutRunId: runningRunId, + executionRunId: runningRunId, + }); + }); + + it("still settles a freshly started run under a terminal issue once its process is gone", async () => { + const { companyId, agentId, runningRunId } = await seed(); + await db + .update(heartbeatRuns) + .set({ processPid: 2_000_000_000, processHostHash: currentProcessSpaceHash(), updatedAt: new Date() }) + .where(eq(heartbeatRuns.id, runningRunId)); + const issueId = randomUUID(); + await db.insert(issues).values({ + id: issueId, + companyId, + title: "Fresh run, dead process, issue done", + status: "done", + priority: "high", + assigneeAgentId: agentId, + checkoutRunId: runningRunId, + executionRunId: runningRunId, + executionLockedAt: new Date(), + }); + + const result = await heartbeatService(db).sweepStaleIssueLocks(); + + expect(result.terminalizedRunIds).toEqual([runningRunId]); + const [run] = await db.select({ status: heartbeatRuns.status, errorCode: heartbeatRuns.errorCode }) + .from(heartbeatRuns).where(eq(heartbeatRuns.id, runningRunId)); + expect(run).toEqual({ status: "failed", errorCode: "process_lost" }); + const [lock] = await db.select({ checkoutRunId: issues.checkoutRunId, executionRunId: issues.executionRunId }) + .from(issues).where(eq(issues.id, issueId)); + expect(lock.checkoutRunId).not.toBe(runningRunId); + expect(lock.executionRunId).not.toBe(runningRunId); + }); + it("does not terminalize a running run whose process is alive and whose issue is not terminal", async () => { const { companyId, agentId, runningRunId } = await seed(); // process.pid is the live test process, so isPidAlive returns true. @@ -930,7 +1013,9 @@ describeEmbeddedPostgres("recovery sweepStaleIssueLocks", () => { }); it("does not terminalize or clear issue locks when an ownership hold arrives after the sweep snapshot", async () => { - const { companyId, agentId, runningRunId } = await seed(); + const { companyId, agentId, runningRunId } = await seed({ + runningRunStartedAt: orphanedRunStartedAt(), + }); const issueId = randomUUID(); await db .update(heartbeatRuns) @@ -1024,7 +1109,7 @@ describeEmbeddedPostgres("recovery sweepStaleIssueLocks", () => { // A stale lock on a terminal issue and the real lock on an active issue can // point at the same running run. The terminal reference alone must not // terminalize the run, because the run is still live for the active issue. - const { companyId, agentId, runningRunId } = await seed(); + const { companyId, agentId, runningRunId } = await seed({ runningRunStartedAt: orphanedRunStartedAt() }); // process.pid is the live test process, so isPidAlive returns true. await db .update(heartbeatRuns) @@ -1084,7 +1169,7 @@ describeEmbeddedPostgres("recovery sweepStaleIssueLocks", () => { // fallback in terminalizeOrphanedRunningRun could read that terminal status // and terminalize the run. An active issue still owns the run, so the sweep // must suppress the fallback and keep the run live. - const { companyId, agentId, runningRunId } = await seed(); + const { companyId, agentId, runningRunId } = await seed({ runningRunStartedAt: orphanedRunStartedAt() }); // process.pid is the live test process, so isPidAlive returns true. await db .update(heartbeatRuns) diff --git a/server/src/services/recovery/service.ts b/server/src/services/recovery/service.ts index a02a151b952..47328f591fe 100644 --- a/server/src/services/recovery/service.ts +++ b/server/src/services/recovery/service.ts @@ -691,6 +691,8 @@ const CONTINUATION_RECOVERY_DEFAULT_MAX_ATTEMPTS = 1; const CONTINUATION_RECOVERY_TRANSIENT_BASE_BACKOFF_MS = 60_000; export const PROVIDER_QUOTA_RECOVERY_DEFAULT_BACKOFF_MS = PROVIDER_LIMIT_DEFAULT_COOLDOWN_MS; +export const ISSUE_TERMINAL_MIN_RUN_AGE_MS = 60_000; + const PROVIDER_QUOTA_ERROR_RE = /(?:you(?:'|’)ve hit your (?:\w+ )?limit|usage limit(?: reached| exceeded)?|provider quota|quota (?:limit )?exceeded|model (?:is )?at capacity)/i; const CONFIGURATION_INCOMPLETE_ERROR_RE = @@ -6705,6 +6707,24 @@ export function recoveryService( else if (issueStatus === "cancelled") issueTerminalStatus = "cancelled"; } + if (issueTerminalStatus) { + const startedMs = (run.startedAt ?? run.createdAt).getTime(); + const ageMs = Number.isFinite(startedMs) ? Date.now() - startedMs : null; + if (ageMs !== null && ageMs < ISSUE_TERMINAL_MIN_RUN_AGE_MS) { + logger.warn( + { + runId: run.id, + issueId, + ageMs, + minAgeMs: ISSUE_TERMINAL_MIN_RUN_AGE_MS, + }, + "skipped issue-terminal terminalization: run is younger than the grace period and is most likely a fresh reassignment hand-off", + ); + options?.onDeferredDeadline?.(startedMs + ISSUE_TERMINAL_MIN_RUN_AGE_MS); + issueTerminalStatus = null; + } + } + // Process-death authority. The run is live while either its adapter process // or the enclosing heartbeat execution/finalization still owns it. Check // that full in-process lifecycle first, then the recorded pid and process