diff --git a/server/src/__tests__/issue-recovery-actions.test.ts b/server/src/__tests__/issue-recovery-actions.test.ts index 36c2d056587..18dbd995d89 100644 --- a/server/src/__tests__/issue-recovery-actions.test.ts +++ b/server/src/__tests__/issue-recovery-actions.test.ts @@ -48,6 +48,8 @@ import { caseHandoffService } from "../services/case-handoffs.js"; import { instanceSettingsService } from "../services/instance-settings.js"; import { withNativeWorkspaceFinalizationOwnership } from "../services/native-runtime/native-workspace-finalization-ownership.js"; import { noticeMetadataReferencesRecoveryAction } from "../services/recovery/successful-run-handoff.js"; +import { getExecutionBlocker } from "../services/execution-blocker.js"; +import { CONVERSATION_CONTINUATION_POLICY } from "../services/conversation-continuation.js"; const external = resolveExternalTestDatabaseUrl("PAPERCLIP_RECOVERY_ACTION_TEST_DATABASE_URL"); const embeddedPostgresSupport = external ? { supported: true, reason: undefined } : await getEmbeddedPostgresTestSupport(); @@ -2148,6 +2150,77 @@ describeEmbeddedPostgres("issue recovery actions", () => { expect((await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.id, action!.id)))[0]).toEqual(recorded); }); + async function seedHeldBlockedTask() { + const fixture = await seedCompany(); + const failedRunId = randomUUID(), agentRunId = randomUUID(); + await seedHeartbeatRun({ companyId: fixture.companyId, agentId: fixture.coderId, runId: failedRunId, + issueId: fixture.sourceIssueId, status: "failed" }); + await seedHeartbeatRun({ companyId: fixture.companyId, agentId: fixture.coderId, runId: agentRunId }); + await db.update(issues).set({ status: "blocked" }).where(eq(issues.id, fixture.sourceIssueId)); + const [hold] = await db.insert(issueRecoveryActions).values({ + companyId: fixture.companyId, sourceIssueId: fixture.sourceIssueId, kind: "active_run_watchdog", + status: "resolved", outcome: "blocked", ownerType: "board", returnOwnerAgentId: fixture.coderId, + cause: "provider_failure_meaning_unverified", fingerprint: failedRunId, + nextAction: "Preserve recorded work without replay.", + evidence: { runId: failedRunId, automaticRecovery: { replay: "blocked", actionOutcome: "unknown" } }, + }).returning(); + const agent = createApp({ type: "agent", source: "agent_jwt", companyId: fixture.companyId, + agentId: fixture.coderId, runId: agentRunId }); + return { ...fixture, hold: hold!, agent }; + } + + async function seedLiveOwnerRun(f: Awaited>) { + await db.insert(heartbeatRuns).values({ id: randomUUID(), companyId: f.companyId, agentId: f.coderId, + invocationSource: "manual", status: "failed", processPid: process.pid, contextSnapshot: { issueId: f.sourceIssueId }, + resultJson: { conversationContinuation: CONVERSATION_CONTINUATION_POLICY } }); + expect(await getExecutionBlocker(db, f.companyId, f.sourceIssueId)) + .toMatchObject({ cause: "execution_owner_active", recoveryActionId: null }); + } + + it.each([ + ["status change", (f: Awaited>) => + request(f.agent).patch(`/api/issues/${f.sourceIssueId}`).send({ status: "in_progress" })], + ["reopen", (f: Awaited>) => + request(f.agent).patch(`/api/issues/${f.sourceIssueId}`).send({ reopen: true, comment: "Reopening the case." })], + ["resume comment", (f: Awaited>) => + request(f.agent).post(`/api/issues/${f.sourceIssueId}/comments`).send({ body: "Continuing the case.", resume: true })], + ["checkout", (f: Awaited>) => + request(f.agent).post(`/api/issues/${f.sourceIssueId}/checkout`).send({ agentId: f.coderId, expectedStatuses: ["blocked"] })], + ["completion that its review stage hands to a reviewer", async (f: Awaited>) => { + await db.update(issues).set({ executionPolicy: { mode: "normal", commentRequired: false, + stages: [{ id: randomUUID(), type: "review", participants: [{ type: "agent", agentId: f.managerId }] }] } }) + .where(eq(issues.id, f.sourceIssueId)); + return request(f.agent).patch(`/api/issues/${f.sourceIssueId}`).send({ status: "done" }); + }], + ["status change while a live previous run also owns the task", async (f: Awaited>) => { + await seedLiveOwnerRun(f); + return request(f.agent).patch(`/api/issues/${f.sourceIssueId}`).send({ status: "todo" }); + }], + ])("keeps a task blocked behind a no-replay hold when its agent tries a %s", async (_entry, send) => { + const f = await seedHeldBlockedTask(); + const response = await send(f); + expect(response.status).toBe(409); + expect(response.body.details).toMatchObject({ recoveryActionId: f.hold.id, nextAction: expect.stringContaining(f.hold.nextAction) }); + expect((await db.select().from(issues).where(eq(issues.id, f.sourceIssueId)))[0]!.status).toBe("blocked"); + }); + + it("lets the agent close a held task, or move it on once the hold no longer blocks replay", async () => { + const closed = await seedHeldBlockedTask(); + await request(closed.agent).patch(`/api/issues/${closed.sourceIssueId}`).send({ status: "cancelled" }).expect(200); + const released = await seedHeldBlockedTask(); + await db.update(issueRecoveryActions).set({ evidence: { runId: released.hold.evidence.runId } }) + .where(eq(issueRecoveryActions.id, released.hold.id)); + await request(released.agent).patch(`/api/issues/${released.sourceIssueId}`).send({ status: "todo" }).expect(200); + expect((await db.select().from(issues).where(eq(issues.id, released.sourceIssueId)))[0]!.status).toBe("todo"); + }); + + it("does not hold a blocked task while only a live previous run still owns it", async () => { + const f = await seedHeldBlockedTask(); + await db.delete(issueRecoveryActions).where(eq(issueRecoveryActions.id, f.hold.id)); + await seedLiveOwnerRun(f); + await request(f.agent).patch(`/api/issues/${f.sourceIssueId}`).send({ status: "todo" }).expect(200); + }); + async function seedReconciledDelivery() { const fixture = await seedCompany(); const { companyId, coderId, sourceIssueId } = fixture; diff --git a/server/src/routes/issues.ts b/server/src/routes/issues.ts index 0da962ebb8e..0df3332f67d 100644 --- a/server/src/routes/issues.ts +++ b/server/src/routes/issues.ts @@ -7280,6 +7280,22 @@ export function issueRoutes( return false; } + async function assertAgentMayLeaveBlocked( + req: Request, + res: Response, + issue: { id: string; companyId: string; status: string }, + nextStatus: string, + ) { + if (req.actor.type !== "agent" || issue.status !== "blocked" || nextStatus === "blocked" || isClosedIssueStatus(nextStatus)) return true; + const hold = await getExecutionBlocker(db, issue.companyId, issue.id, { recoveryHoldOnly: true }); + if (!hold?.recoveryActionId) return true; + res.status(409).json({ + error: `Issue stays blocked until its execution recovery hold is released. ${hold.nextAction}`, + details: { issueId: issue.id, recoveryActionId: hold.recoveryActionId, cause: hold.cause, nextAction: hold.nextAction }, + }); + return false; + } + async function assertExplicitResumeIntentAllowed( req: Request, res: Response, @@ -15179,6 +15195,7 @@ export function issueRoutes( } const nextStatus = updateFields.status ?? existing.status; + if (!(await assertAgentMayLeaveBlocked(req, res, existing, nextStatus))) return; if (updateFields.unblockDescriptor && nextStatus !== "blocked") { throw unprocessable("unblockDescriptor requires blocked status"); } @@ -17116,6 +17133,7 @@ export function issueRoutes( const checkoutRunId = requireAgentRunId(req, res); if (req.actor.type === "agent" && !checkoutRunId) return; + if (req.body.expectedStatuses.includes("blocked") && !(await assertAgentMayLeaveBlocked(req, res, issue, "in_progress"))) return; // Reopen the closed isolated workspace only after the run-id gate passes. A // rejected checkout must not rebuild and republish the workspace as active. @@ -19507,6 +19525,12 @@ export function issueRoutes( .json({ error: "Issue follow-up blocked by unresolved blockers" }); return; } + if ( + effectiveMoveToTodoRequested && + !hasUnresolvedFirstClassBlockers && + !(await assertAgentMayLeaveBlocked(req, res, issue, "todo")) + ) + return; if ( !(await assertCrossIssueInfluenceWithinRunCap( req, diff --git a/server/src/services/execution-blocker.ts b/server/src/services/execution-blocker.ts index fdc644f4418..f73e3794f4e 100644 --- a/server/src/services/execution-blocker.ts +++ b/server/src/services/execution-blocker.ts @@ -339,6 +339,7 @@ export async function getExecutionBlocker(db: Db, companyId: string, issueId: st lockedIssue?: Pick; /** The native run being dispatched; its own scheduled resume does not block it. */ resumingNativeRunId?: string | null; + recoveryHoldOnly?: boolean; }): Promise { const lockedIssue = options?.lockedIssue; const conversation = lockedIssue?.id === issueId && lockedIssue.companyId === companyId @@ -370,7 +371,7 @@ export async function getExecutionBlocker(db: Db, companyId: string, issueId: st eq(issueComments.id, conversation.conversationBoundaryCommentId), )).limit(1) : []; - const ownership = await getExecutionOwnershipBlocker(db, companyId, issueId); + const ownership = options?.recoveryHoldOnly ? null : await getExecutionOwnershipBlocker(db, companyId, issueId); if (ownership) return ownership; const [action] = restoreHold ? [restoreHold] : await db.select().from(issueRecoveryActions).where(and( eq(issueRecoveryActions.companyId, companyId),