diff --git a/server/src/__tests__/released-writer-escalation.test.ts b/server/src/__tests__/released-writer-escalation.test.ts index f70b474b8d2..dc5d65436f9 100644 --- a/server/src/__tests__/released-writer-escalation.test.ts +++ b/server/src/__tests__/released-writer-escalation.test.ts @@ -185,6 +185,22 @@ describePostgres("a task stranded behind its own workspace's ended writer", () = expect(wakesFor(f.task.id)).toHaveLength(1); }, 60_000); + it("the assigned-work sweep leaves the escalation to this pass while the hold refuses a start", async () => { + const f = await seedStrandedTask({ laterDecision: null }); + const sweep = recoveryService(db, { enqueueWakeup: enqueueWakeup as never }).assignedWork; + + expect(await sweep.checkIssue({ companyId: f.companyId, issueId: f.task.id })) + .toMatchObject({ outcome: "left_to_hold_owner", verdict: { reason: "recovery_block" } }); + expect(await issue(f.task.id)).toMatchObject({ status: "blocked" }); + expect(await action(f.actionId)).toMatchObject({ status: "active", ownerType: "board" }); + expect(wakesFor(f.task.id)).toEqual([]); + + await f.releaseWriter(); + expect(await sweep.checkIssue({ companyId: f.companyId, issueId: f.task.id })) + .toMatchObject({ outcome: "repaired", verdict: { reason: "recovery_block" } }); + expect(wakesFor(f.task.id)).toHaveLength(1); + }, 60_000); + it("delivers a wake that a crash left pending, and keeps backing off a refused one", async () => { const f = await seedStrandedTask({ laterDecision: null }); await f.releaseWriter(); diff --git a/server/src/services/recovery/assigned-work-sweep.ts b/server/src/services/recovery/assigned-work-sweep.ts index c44e5514197..ee4977f9371 100644 --- a/server/src/services/recovery/assigned-work-sweep.ts +++ b/server/src/services/recovery/assigned-work-sweep.ts @@ -8,6 +8,7 @@ import { approvals, caseHandoffs, companies, + executionWorkspaces, heartbeatRuns, issueApprovals, issueRecoveryActions, @@ -17,6 +18,7 @@ import { nativeRunFinalizations, type Db, } from "@paperclipai/db"; +import { readRemoteWorkspaceAuthority } from "@paperclipai/shared"; import { logger } from "../../middleware/logger.js"; import { logActivity, publishActivity, type ActivityPublication } from "../activity-log.js"; import { evaluateAgentInvokability } from "../agent-invokability.js"; @@ -45,6 +47,8 @@ import { continueSweepTaskFromSavedWork } from "./sweep-saved-work-continuation. import { isSetupFailureBeforeProviderWork, setupFailureBeforeProviderWorkSql } from "../continuation-cause.js"; import { STRANDED_CONTINUATION_MAX_AGE_MS, STRANDED_KIND, STRANDED_PRE_PROVIDER_SETUP_MAX_AGE_MS, strandedHoldContinuesAutomatically, type StrandedHoldRun } from "./stranded-hold-continuation.js"; +import { readRemoteWorkspaceWriterHold, REMOTE_WORKSPACE_WRITER_PENDING_MESSAGE, + remoteWorkspaceHoldRefusesStart } from "../remote-workspace-writer-hold.js"; import { continuationActivityMessage, continuationAllowance, @@ -337,6 +341,22 @@ export function assignedWorkSweep(db: Db, deps: AssignedWorkDeps) { executionRecovery: sql`${heartbeatRuns.resultJson}->'executionRecovery'` }) .from(heartbeatRuns).where(and(eq(heartbeatRuns.companyId, companyId), inArray(heartbeatRuns.id, strandedRunIds))) : []) .map(({ id, ...run }) => [id, run])); + const writerHeldIssueIds = [...actionMap.entries()].filter(([, action]) => { + const run = strandedRuns.get(String(action.evidence?.latestRunId ?? "")); + return run?.errorCode === "setup_failed" && run.error === REMOTE_WORKSPACE_WRITER_PENDING_MESSAGE; + }).map(([issueId]) => issueId); + const writerRefusedIssueIds = new Set(); + if (writerHeldIssueIds.length) { + const bound = await database.select({ issueId: issues.id, metadata: executionWorkspaces.metadata }).from(issues) + .innerJoin(executionWorkspaces, and(eq(executionWorkspaces.companyId, issues.companyId), + eq(executionWorkspaces.id, issues.executionWorkspaceId))) + .where(and(eq(issues.companyId, companyId), inArray(issues.id, writerHeldIssueIds))); + for (const row of bound) { + const authority = readRemoteWorkspaceAuthority(row.metadata); + if (authority && remoteWorkspaceHoldRefusesStart( + await readRemoteWorkspaceWriterHold(database, { companyId, authority }), at.getTime())) writerRefusedIssueIds.add(row.issueId); + } + } const agentsById = new Map(); const companyAgents = agentRows.length ? await database.select({ id: agents.id, companyId: agents.companyId, name: agents.name, reportsTo: agents.reportsTo, status: agents.status }).from(agents).where(eq(agents.companyId, companyId)) : []; @@ -468,7 +488,7 @@ export function assignedWorkSweep(db: Db, deps: AssignedWorkDeps) { reviewPending: executionState.status === "pending" || Boolean(executionState.reviewRequest), activeRecoveryAction: action ? { id: action.id, cause: action.cause, ownerType: action.ownerType, resumeAt: PROVIDER_LIMIT_CAUSES.includes(action.cause) && interruption?.nextRetryAt ? new Date(interruption.nextRetryAt) : null, - continuedBySweep: strandedHoldContinuesAutomatically(action, + continuedBySweep: !writerRefusedIssueIds.has(issue.id) && strandedHoldContinuesAutomatically(action, strandedRuns.get(String(action.evidence?.latestRunId ?? "")) ?? null, issue.assigneeAgentId, at) } : null, agent: agent ? { status: agent.status, invokable: agent.invokable, updatedAt: agent.updatedAt } : null, companyPaused: company?.status === "paused",