Files
PaperClipAI/server
2bd225ac0f fix(workspaces): keep a run going when a retired recovered copy cannot be removed (#21509)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - With remote (sandbox) execution, preparation can attach saved work
of an interrupted run under `.paperclip-recovered/<operation>`. After a
run that was shown that copy succeeds, the next preparation retires the
copy and removes it from the sandbox (#19979).
> - The removal is only a save-size cleanup. A retired copy is never
shown to an agent again.
> - When the sandbox refuses the removal (the proof file changed, the
path is not a plain directory, or a file cannot be deleted), the whole
run preparation fails with `remote_workspace_operation_failed:
operation=recovery_attachment exit=1`. The sandbox script drops its own
reason, so the failure tells nobody why.
> - Each later preparation that restores the same save meets the same
copy and fails the same way. The task cannot start.
> - This pull request makes a refused removal of a retired copy
non-fatal and makes the sandbox report a closed reason.
> - The benefit is that the run continues with the copy left in place,
and the log names why the sandbox refused.

## Linked Issues or Issue Description

Refs #19979

**What happened?**
On hosted staging, four runs of four different tasks failed in setup
with `remote_workspace_operation_failed: operation=recovery_attachment
exit=1`. The stack goes through `retainRemoteRecoveryAttachments` and
`removeRemoteRecoveryAttachment`. The run stopped before any provider
work. The error has no reason, because the sandbox script writes only
`remote_workspace_recovery_attachment_invalid` and the server accepts no
cause for this operation.

**Expected behavior**
A retired recovered copy that the sandbox will not remove stays in
place. The run continues. The log names a closed reason for the refusal.
Timeouts and provider transport failures still stop the run.

**Steps to reproduce**
1. Record a retired recovered copy in the workspace metadata
(`remoteWorkspaceRecoveredFiles` with `retired: true`).
2. In the sandbox, put a different `provenance.json` in
`.paperclip-recovered/<operation>`, or replace that directory with a
symlink.
3. Start the next run in that workspace. Preparation calls
`retainRemoteRecoveryAttachments`.
4. Preparation fails with `remote_workspace_operation_failed:
operation=recovery_attachment exit=1`.

**Relevant logs or output**
`RemoteWorkspaceOperationError: remote_workspace_operation_failed:
operation=recovery_attachment exit=1` at `realizeForRun` ->
`prepareRemoteWorkspace` -> `retainRemoteRecoveryAttachments` ->
`removeRemoteRecoveryAttachment`.

## What Changed

- The recovered-copy sandbox script now writes
`remote_workspace_recovery_attachment_invalid: reason=<closed reason>[
code=<errno>]`. The reasons are `invalid_attachment`,
`unsafe_attachment`, `missing_workspace`, `attachment_proof_changed`, or
`filesystem`. It never writes a path or file text.
- `command()` accepts only that exact grammar as the cause for the
`recovery_attachment` operation. Any other output is still dropped.
- New `isRemoteRecoveryAttachmentRefusal()` identifies a refusal that
the sandbox reported with an exit status. Timeouts and provider
transport errors do not match.
- `retainRemoteRecoveryAttachments` catches only that refusal when it
removes a retired copy. It logs a warning with code
`remote_workspace_retired_copy_kept` and the closed cause. The files
stay in place. The retired record stays, so the copy is never shown
again, and the next preparation tries the removal again.
- New `remote-workspace-recovery-attachments.test.ts` runs the real
sandbox script.

## Verification

- `cd server && npx vitest run
src/services/remote-workspace-recovery-attachments.test.ts`
- Before the fix: 4 of 6 fail. The retain test fails with the production
message `remote_workspace_operation_failed:
operation=recovery_attachment exit=1`.
  - After the fix: 6 of 6 pass.
- Safety cases in the same file:
  - A retired copy with its exact proof is still removed.
- A changed proof or a symlinked directory leaves every file in place,
also outside the workspace.
  - A provider failure (the runner throws) still fails preparation.
- `npx vitest run src/services/remote-workspace-transport.test.ts`: all
recovery-attachment cases pass. On this machine, three unrelated Git and
restore-lock cases fail with and without this change, because of the
local environment.
- `npx vitest run src/services/workspace-recovery-preparation.test.ts`:
81 of 81 pass.
- `tsc --noEmit -p server`: no errors in the changed files.

## Risks

- Low risk. Only the cleanup of an already-retired copy becomes
tolerant. Live recovered copies, attachment import, and the
`hasRemoteRecoveryAttachment` check keep their current behavior.
- A copy that cannot be removed stays in the saved workspace. Saves are
bigger for that workspace, which is the behavior from before #19979. The
warning makes this visible.
- The sandbox now writes a short closed reason to stderr. The server
accepts only the exact grammar, so no paths or file contents reach the
record.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used through Claude
Code with tool use (shell, file edits, test runs).

## 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
- [ ] 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: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: paperclipbugbot <356415228+paperclipbugbot@users.noreply.github.com>
Co-authored-by: paperclipbugbot <bots+paperclipbugbot@paperclip.ing>
2026-12-05 04:16:35 -08:00
..