Files
PaperClipAI/doc
DottaandClaude Opus 5.5 10a7d039c8 fix(dispatch): keep a workspace wait parked while an ended writer's save settles (#19285)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - A run that finds its task workspace held by another run waits as a
`workspace_busy` scheduled retry. #19143 keeps that wait as one parked
retry while the holder is live.
> - A remote task workspace stays held by its last writer run until the
writer's save and sandbox release settle. This hold continues after the
writer run ends, for example when a reassignment stops the writer.
> - The parked-wait check counted only a `running` holder. For an ended
writer whose save had not settled, it released the waiter at each due
check. Preparation deferred again, cancelled the run, and created a new
retry run.
> - After a mid-run reassignment, the new owner got five cancelled
`workspace_busy` runs in eleven minutes, and the task did no work.
> - This pull request makes the parked wait use the same writer-hold
rule as workspace reservation, so the waiter stays parked until the hold
releases.
> - The benefit is one durable wait, one start when the hold releases,
and no churn of cancelled runs.

## Linked Issues or Issue Description

Refs #19143 (parks a workspace-busy wait). Refs #19184 (releases a
recovered hold for a new assignee after reassignment).

**What happened?**
A board user reassigned a task in the middle of a run. The old run was
cancelled with `issue_reassigned`. The new assignee's runs were each
created, started and cancelled with `workspace_busy`: "Shared execution
workspace is busy: run … ended, but its workspace save has not settled".
This repeated every 1–2 minutes (five cancelled runs in 11 minutes).
Every cancellation named the stopped run of the previous assignee as the
holder. The new assignee's other tasks were not the holder.

**Expected behavior**
The reassigned task waits as one parked retry. It starts once when the
stopped writer's save and sandbox release settle. The wait stops at the
one-hour holder cutoff.

**Steps to reproduce**
1. Give a task a remote execution workspace whose writer run has ended,
but whose save has not settled (`writerRunId` set, `dirty: true`).
2. Wake the task's assignee. The run defers with `workspace_busy` and
schedules a retry.
3. Make the retry due and promote scheduled retries. Before this fix,
the retry is promoted and dispatched, defers again, and creates another
run.

## What Changed

- New `server/src/services/remote-workspace-writer-hold.ts`:
`readRemoteWorkspaceWriterHold` holds the rule for who holds a remote
workspace (a running writer, or an ended writer whose save or sandbox
release has not settled). The code is moved from
`reserveRemoteWorkspace` without a behavior change.
- `reserveRemoteWorkspace` uses that function. It throws the same errors
as before.
- `parkWorkspaceBusyRetryInTx` (from #19143): when the recorded holder
is not running, it reads the same hold for the waited execution
workspace. If the recorded holder is an ended writer that still holds
it, the retry stays parked. The cutoff clock starts at the writer's end,
the same value that reservation measures. Earlier waiters keep their
arrival order.
- New `WORKSPACE_BUSY_SETTLING_RECHECK_MS` (60 s). Settling has no
single completion point that can wake the waiter, so a wait behind a
settling writer re-checks every minute. Each check creates no run. In
the promotion transaction it reads the workspace, the writer run, its
task, and the writer's leases (one row per sandbox lease of that run).
The checks last only while the wait exists, up to the one-hour cutoff.
- `doc/execution-semantics.md`: documents the parked wait behind a
settling writer.

## Verification

- Red before the fix: `cd server && npx vitest run
src/__tests__/heartbeat-remote-workspace-busy.test.ts`. The two new
tests failed: the due retry was promoted (`expected [ … ] to not include
'<retry id>'`).
- Green after the fix, with:
- `parks one waiter while an ended writer's save settles, then admits it
once the hold releases`: three due checks keep one parked run, each
re-check is bounded by the writer's end plus one hour, there is one
`workspace_busy` cancellation in total and no new retry run. After the
hold releases, the next check promotes the same run.
- `stops parking behind an ended writer once its unsettled save reaches
the holder cutoff`: parked before the cutoff, promoted after it
(preparation then fails with the existing unfinished-writer error).
- Existing suites pass: `heartbeat-workspace-busy.test.ts` (live-holder
parking, arrival order, stop, workspace move), `modules/run-dispatch`,
`remote-workspace-reservation.test.ts`,
`remote-workspaces.integration.test.ts`,
`isolated-remote-workspace-source.test.ts`,
`heartbeat/workspace-preparation.test.ts` (476 tests in all).
- `tsc --noEmit` for `server` shows no errors in the changed files.
- Not done: a live run on staging. The fix is not deployed.

## Risks

- Low. Parking applies only to a retry that recorded a `workspace_busy`
wait on an execution workspace whose recorded writer is that holder. If
the hold settles, moves to another run, or the task changes workspace,
the retry is released to normal dispatch as before.
- The wait stays bounded by the existing one-hour holder cutoff from the
writer's end. After that, preparation fails as before. A user can still
stop the parked run.
- One writer per workspace is unchanged. Reservation still decides
admission with the same rule.
- The one-minute re-check runs only while a wait exists. It is not idle
polling.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used through Claude
Code with tool use (shell, file edits, browser reads of staging
diagnostics).

## 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
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] 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: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 11:48:29 -06:00
..
…
…
…
…
…