Files
PaperClipAI/server
7636441c46 fix(recovery): hold an agent comment wake behind a refused workspace writer hold (#23475)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - A run fails setup with "The workspace still has an unfinished writer
or save operation." when the task's remote workspace is still held by an
earlier writer run that did not finish its save, and the hold refuses a
start.
> - #22103 and #23446 stopped the assigned-work sweep, the bounded retry
and stranded recovery from starting runs behind such a hold. On staging,
the failures are now single runs, not pairs.
> - The remaining single failures come from comment wakes. An agent
comments on a task (for example, an automated status note), and the
route wakes the assignee. The assignee's latest run on that task already
failed on the same refused hold, so the new run can only fail the same
way.
> - The blocker-stall wake already checks this case before it wakes a
waiter. The comment wake paths do not check it.
> - This pull request makes the comment wake paths skip the assignee
wake for an agent-authored comment while the assignee's latest run on
the task failed on that hold and the hold still refuses a start.
> - The benefit is no new failed run for each automated comment on a
held task. The comment stays on the task, and the existing recovery
continues the task when the hold settles.

## Linked Issues or Issue Description

Refs #22103
Refs #23446
Refs #21221

**What happened?**
On staging, after #22103 and #23446 were deployed, two runs failed setup
with the writer-pending message (`setup_failed`, before provider work).
Each run was started by an agent comment on a task: one through
`issue.comment`, one through `issue.comment.reopen`. Both tasks have
workspaces whose writer run ended long ago with an unsaved workspace, so
the hold refuses a start. In one task, the assignee's previous run had
already failed setup on the same hold. No retry followed either failure,
so #23446 works. But every new automated comment on such a task starts
one more run that fails.

**Expected behavior**
An agent comment does not start a run that can only fail setup on a
refused workspace hold. The comment is kept. The task continues through
stranded-work recovery and the released-writer pass when the hold
settles. A person's comment still wakes the assignee.

**Steps to reproduce**
1. Bind an in-progress task to an execution workspace whose remote
writer is an ended run with dirty, unsaved state, so the hold refuses a
start.
2. Fail the assignee's latest run on the task with `setup_failed` and
the writer-pending message.
3. As another agent, post a comment on the task (`POST
/api/issues/:id/comments`, or `PATCH /api/issues/:id` with `comment`).
4. Before this change, the route wakes the assignee, and the new run
fails setup on the same hold.

**Paperclip version or commit**
`master` at 5a956b724.

**Deployment mode**
Hosted (cloud sandbox workspaces).

## What Changed

- `server/src/services/remote-workspace-writer-hold.ts`: add
`agentTaskWorkspaceHoldRefusesStart(db, { companyId, issueId, agentId
})`. It reads the agent's latest run on the task (indexed on company,
`contextSnapshot.issueId` and `createdAt`). It returns true only when
that run failed setup on the writer hold and the hold still refuses a
start (`failedRunWorkspaceHoldRefusesStart` from #23446).
- `server/src/routes/issues.ts`: the POST comment and PATCH comment wake
paths skip the assignee wake when the actor is an agent and that check
is true. In the PATCH path, the same result (read once per request) also
holds the `issue_status_changed` wake, for example when an agent moves a
blocked task to `todo` with a comment. The check runs only when the
route would otherwise wake the assignee. If the check fails to read, the
wake goes through as before.
- Person comments, a settled hold, and a later run of the assignee on
the task all keep today's behavior.
- The rule needs evidence that reservation already refused this hold
(the latest run failed on it). Reservation can still release an
unchanged or superseded writer before it reads the hold, so a hold read
alone could hold a wake that reservation would admit.
- Tests: `released-writer-escalation.test.ts` checks the helper against
Postgres: true behind the refused hold, false for another agent, false
after the hold releases, and false after a later run.
`issue-comment-reopen-routes.test.ts` checks both wake paths: an agent
comment behind a refusing hold wakes nobody, a person's comment still
wakes the assignee and does not read the hold, and an agent comment
wakes the assignee when the hold does not refuse. A PATCH that moves a
blocked task to `todo` with an agent comment wakes the assignee zero
times behind a refusing hold and once otherwise.

## Verification

- `cd server && npx vitest run
src/__tests__/issue-comment-reopen-routes.test.ts
src/__tests__/released-writer-escalation.test.ts`: 123/123 and all
released-writer tests pass.
- With the route check disabled, the two "agent comment behind a
refusing hold" cases fail (one wake, expected none). With only the
status-wake check removed, the blocked-to-`todo` case fails.
- Related suites (11 files: issue comment routes, comment wakeups,
queued comments, scheduled retry routes, retry scheduling, assigned-work
invariant, released-writer escalation): 457/457 pass.

## Risks

- Low. The new read runs only for an agent-authored comment that would
wake the assignee. It is one indexed query, plus the workspace hold read
only when the latest run failed on the hold.
- An agent comment on a task behind a refused hold no longer starts a
run. The assignee reads the comment on its next run, which stranded-work
recovery and the released-writer pass start when the hold settles.
- Person comments are unchanged.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used through Claude
Code with tool use (shell, git, GitHub CLI) 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 <noreply@anthropic.com>
2026-12-02 02:26:46 -08:00
..