From c59a56883cfc8573c056a96f5fa10cf76b72b685 Mon Sep 17 00:00:00 2001 From: paperclipbugbot Date: Wed, 2 Dec 2026 01:36:25 +0000 Subject: [PATCH] fix(recovery): leave a board escalation behind a refused workspace writer hold to the released-writer pass (#22103) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The recovery service has an assigned-work sweep. The sweep continues tasks that stopped without a clear owner. > - Stranded-work recovery can block a task with a board escalation. The sweep continues that task when the failed run's cause looks temporary. > - A setup failure on the task's own workspace writer hold ("The workspace still has an unfinished writer or save operation.") counts as temporary. Since #23051, such a failure before provider work stays eligible for 7 days. The sweep does not read the hold before it continues the task. > - Each continuation fails setup on the same hold. Recovery then escalates the task again, and the sweep continues it again at 2, 4, 8 and 15 minute spacing. > - The released-writer pass from #21221 already owns this escalation. It continues the task only when the hold releases. > - This pull request makes the sweep leave such an escalation to the released-writer pass while the hold still refuses a start. > - The benefit is that a held workspace no longer causes a loop of failed runs and repeated escalations. ## Linked Issues or Issue Description Refs #21221 Refs #21505 Refs #20273 Refs #23051 **What happened?** On staging, a task was blocked by stranded-work recovery with a board escalation. The assigned-work sweep continued it 5 times in 30 minutes. Each continuation failed during setup with "The workspace still has an unfinished writer or save operation." (HTTP 409, `remote_workspace_writer_pending`), before any provider work. Recovery escalated the task again after each failure. The loop stopped only when a person cancelled the task. This error group had 45 events on staging, and releases that already contain #21106, #21136 and #21221 still had events. **Expected behavior** While the task's workspace writer hold still refuses a start, the sweep must not continue the task. The released-writer pass must continue it one time, after the hold releases. **Steps to reproduce** 1. Put a task in `blocked` status with an active `stranded_assigned_issue` recovery action that the board owns. 2. Set the action's latest run to `setup_failed` with the writer-pending message and `executionRecovery: { kind: "bootstrap", providerWorkStarted: false }`. 3. Bind the task to an execution workspace whose remote workspace writer hold refuses a start (an ended writer run with dirty, unsaved state). 4. Run the assigned-work check for the task. Before this change, the outcome is `repaired` and a continuation is created. That continuation fails setup on the same hold. **Paperclip version or commit** `master` at 0d3ed4455. **Deployment mode** Hosted (cloud sandbox workspaces). ## What Changed - `server/src/services/recovery/assigned-work-sweep.ts`: For a board-owned stranded escalation whose latest run failed setup with the writer-pending message, the sweep reads the bound workspace's writer hold. When `remoteWorkspaceHoldRefusesStart()` is true, `continuedBySweep` is false. The sweep then gives the `left_to_hold_owner` outcome and does not continue the task. When the hold has released, the sweep continues the task as before. - `server/src/__tests__/released-writer-escalation.test.ts`: Add a regression test that reuses this suite's stranded-task fixture (an ended dirty writer, setup failures before provider work, and a board-owned stranded escalation). While the hold refuses a start, the assigned-work check gives `left_to_hold_owner`, the task stays blocked, the board action stays active, and there is no wake. After the hold releases, the check gives `repaired` and sends exactly one wake. The earlier version of this PR put the test in `failed-continuation-continues-again.test.ts`, which #22831 removed. ## Verification - `cd server && npx vitest run src/__tests__/released-writer-escalation.test.ts src/__tests__/assigned-work-invariant.test.ts`: 61/61 pass on this head. - The new test fails without the change: the outcome is `repaired`, but the test expects `left_to_hold_owner`. With the change, the test passes. - I could not run the full server `tsc` locally, because the sandbox's memory limit stopped it. CI must confirm typecheck, build and the full suite on this head. ## Risks - Low risk. The change applies only to board-owned stranded escalations whose latest run failed setup with the writer-pending message, and only while the hold refuses a start. - The sweep does one extra query (issues joined to execution workspaces) and one hold read for each matching task. The sweep makes these calls only when such tasks exist. - If the hold stays refused for a long time, the task stays blocked until the released-writer pass continues it. This is the intended owner. Before this change, the task looped through failed continuations instead. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used through Claude Code with tool use (shell, git, GitHub CLI and connector) and code execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [ ] I have updated relevant documentation to reflect my changes (not needed: no command or documented behavior changes) - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: paperclipbugbot <356415228+paperclipbugbot@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- .../released-writer-escalation.test.ts | 16 ++++++++++++++ .../services/recovery/assigned-work-sweep.ts | 22 ++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) 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",