Files
PaperClipAI/server
7e025ae65f fix(workspaces): start a new remote workspace when the task's saved one is retired (#23655)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Before a run starts, Paperclip prepares the task's execution
workspace. With remote workspace storage, it reserves the task's saved
remote workspace.
> - A task stays bound to its workspace after that workspace is archived
or its cleanup fails.
> - For a retired host workspace, the next run already creates a new
workspace. For a retired remote workspace, preparation sent the retired
workspace to the reservation. The reservation refused it with "Workspace
ownership changed." (`remote_workspace_retired`, since #23582).
> - So the task could never run again. Each wake, recovery, or Retry
failed in setup with the same conflict.
> - This pull request makes a retired remote workspace act like a
retired host workspace: the run starts a new remote workspace.
> - The benefit is that a reopened task with an archived remote
workspace runs again, and the retired workspace and its saved snapshot
stay unchanged.

## Linked Issues or Issue Description

**What happened**
A task's own remote workspace was archived by hand (no terminal-cleanup
reason) and later moved to remote storage. When the task was opened
again, every run failed in setup with "Workspace ownership changed." The
task was then escalated for recovery, and the next wake failed in the
same way.

**Expected behavior**
A retired workspace is not reused. The run starts a new workspace for
the task, as it does for a host workspace. A native recovery or a saved
Retry that is bound to the retired workspace still stops with the
conflict.

**Steps to reproduce**
1. Bind a task to its own remote-storage workspace. Set the workspace
status to `archived` or `cleanup_failed`.
2. Start a run for the task with a Paperclip Runner agent in a sandbox
environment.
3. Setup fails with a 409 conflict, "Workspace ownership changed."
(`remote_workspace_retired`).

**Paperclip version or commit**
master at 0208bbfbf7.

Refs #23582 (gave this conflict its stable code). Related: #21954
(releases a binding to *another* task's closed workspace; this change is
about the task's *own* retired remote workspace).

## What Changed

- `remote-workspaces.ts`: add `isRetiredExecutionWorkspaceStatus`
(`archived` or `cleanup_failed`). When the caller requests no workspace,
`reserveRemoteWorkspace` does not fall back to an issue binding to a
retired workspace. It creates a new workspace and moves the issue
binding to it. An explicitly requested retired workspace still gets the
`remote_workspace_retired` conflict.
- `heartbeat/workspace-preparation.ts`: do not offer a retired
remote-storage workspace for reuse. The retired workspace still decides
the storage authority, so the task stays on remote storage even when the
instance does not enroll new tasks. This does not apply to a native
recovery, a persisted native execution input, or a saved Retry/fresh
attempt. Those stay bound to the workspace that they admitted.
- Tests: three preparation tests (`archived`, `cleanup_failed`, and
`archived` with new-task remote storage off) and one native-recovery
safety test in `workspace-preparation.test.ts`. Two reservation tests in
`remote-workspaces.integration.test.ts`.

## Verification

- Red before the fix: `cd server && npx vitest run
src/services/heartbeat/workspace-preparation.test.ts -t "saved remote
workspace is"` failed with `Workspace ownership changed.` for both
statuses. With only the reservation change removed, the two new
`remote-workspaces.integration.test.ts` tests also failed with
`Workspace ownership changed.`
- Green after the fix: `npx vitest run
src/services/heartbeat/workspace-preparation.test.ts
src/services/remote-workspaces.integration.test.ts -t "saved remote
workspace is|native recovery bound to a task workspace|stable conflict
code|when no workspace is requested"`: 8 of 8 pass. The storage-off case
failed before the follow-up commit (the run fell back to the host path).
- The tests check that the new workspace is active and bound to the
task, that the binding revision goes up by one, and that the retired row
and its snapshot metadata do not change. The existing explicit-request
conflict test still passes, and a native recovery still gets the
conflict.
- Full files: `npx vitest run
src/services/heartbeat/workspace-preparation.test.ts
src/services/remote-workspaces.integration.test.ts
src/services/workspace-recovery-admission.test.ts`: 433 of 433 pass
(Linux x64, Node 24.23.0, embedded Postgres).

## Risks

- Behavior change: a task whose own remote workspace is retired now
continues in a new workspace. Files that were only in the retired
workspace's snapshot are not restored into the new workspace. This is
the same as for a retired host workspace. The retired snapshot is not
deleted.
- A native recovery or a saved Retry still stops on a retired workspace,
so a run that is in progress does not change its workspace silently.
- No schema or API change.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), with tool use and code execution
in a sandbox.

## 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
- [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-03 13:45:10 -08:00
..