fix(workspaces): let the full-disk read-only save inventory a large workspace (#22546)

## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - After a remote run, Paperclip saves the sandbox workspace. When the
sandbox disk is full, #19465 copies a verified read-only source to the
controller and seals the archive there.
> - Before any transfer, the fallback inventories the sandbox tree. That
inventory refuses with `budget` once it has scanned 100,000 directory
entries.
> - Normal capture has no entry cap. Its sealed manifest is limited to
64 MiB, and the fallback seals with the same codec, so the codec already
bounds the archive.
> - #19619 made the fallback's reason visible. On staging, a full-disk
save then stopped seven times at `stage=inspect reason=budget_exceeded`.
Normal capture later saved the same workspace with 83,417 entries once
space was freed.
> - This pull request raises the scanned-entry bound so the fallback
accepts any tree normal capture can archive.
> - The benefit is that a full sandbox disk no longer blocks the save of
a large workspace, and the fallback still fails closed on runaway trees.

## Linked Issues or Issue Description

Refs #19465
Refs #19619

**What happened?**

On staging, a retained sandbox with a full disk was re-saved every ~30
minutes. Each attempt recorded:

`remote_workspace_sandbox_disk_full: the sandbox disk is full; free
space in the sandbox before saving the workspace;
remote_workspace_readonly_capture_failed: stage=inspect
reason=budget_exceeded transport=provider_error`

The fallback never started a transfer. After the provider cache was
reclaimed, normal capture saved the workspace (83,417 entries, 3.08 GB).

**Expected behavior**

The read-only fallback accepts the same trees as normal capture. Its
whole-tree work stays bounded by the shared 64 MiB manifest limit, the
command timeout, and the existing verification deadline.

**Steps to reproduce**

1. Create a workspace with more than 100,000 directory entries.
2. Call `readWorkspaceInventory` on it (the same function the sandbox
script runs).
3. On `master` it throws `remote_workspace_readonly_capture_budget`.
With this change it returns the inventory.

## What Changed

- `readWorkspaceInventory`: the whole-tree scanned-entry bound goes from
100,000 to 2,000,000. There is no separate per-directory bound: normal
capture has none, and a per-directory bound rejected flat directories
that normal capture archives. The smallest manifest entry
(`{"path":"a","type":"symlink","target":"b"}`) is about 41 bytes, so a
64 MiB manifest holds at most about 1.6 million entries. The bound
remains as a stop for runaway fan-out.
- Unchanged: the 64-repository bound, the Git config size bounds, the
refspec and settings bounds, the source-safety checks, the command
timeout, and the two-minute verification deadline. A budget or deadline
failure still keeps the disk-full code and the retained source.
- New test `remote-workspace-readonly-inventory-entries.test.ts`: a
101,202-entry tree (relative symlinks to a sibling file) is inventoried
in sandbox mode and in controller-copy mode, and both results match. A
second test checks that one directory with 100,002 entries is accepted
in both modes.

## Verification

- Red on `master` (`7671e0cf0d`): the new test fails with
`remote_workspace_readonly_capture_budget`, the error staging recorded.
- Green with this change: both new tests pass; both fail on `master`
with `remote_workspace_readonly_capture_budget`.
- Review follow-ups: `a0945b14` removed the added code comments
(CONTRIBUTING.md) and tried a per-directory bound; `335af975` removed
that bound again after review showed it rejects valid flat directories.
Related suites: 181 passed, 5 failed; the 3 listed below plus two 15 s
timeouts (`restores offline partial Git state…`, `…partial-sparse…`)
that are also flaky on unchanged `master` in this sandbox.
- From `server/`: `npx vitest run
src/services/remote-workspace-readonly-inventory.test.ts
src/services/remote-workspace-readonly-capture-budget.test.ts
src/services/remote-workspace-readonly-capture-cause.test.ts
src/services/remote-workspace-transport.test.ts`: 183 passed, 3 failed.
The 3 failures (`rejects an unsafe restore lock…: directory_shared`,
`does not run ambient checkout filters…`, `uses trusted SSH rewrites
only for clone…`) fail the same way with unchanged `master` source in
this sandbox, because its Git wrapper blocks clones. CI is the authority
for them.
- A full typecheck was not run locally (it runs out of memory in this
sandbox); CI typecheck and build must pass.
- Environment: Linux, Node v25.9.0, filtered `pnpm install` of the
server package.

## Risks

- Medium-low. The fallback can now spend longer inventorying a very
large tree. The command timeout and the verification deadline still
bound that time, and an expired deadline fails closed with the disk-full
code and the source retained.
- Per-directory names are held only while that directory is visited. The
capture codec already reads each directory's names in full and holds an
entry for every path in the tree when it seals the controller copy, so
the inventory never needs more memory than sealing the same tree. The
exclusion list keeps its 64 KiB bound.
- No retry, recovery or receipt behavior changes.

## Model Used

- Provider: Anthropic. Model: Claude Opus 5.5 (`claude-opus-5-5`), used
through Claude Code with tool use (shell, file editing, test runner,
GitHub connector). The exact context window is not shown in this
session.

## 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 (the new test passes; the
three clone/lock failures also fail on unchanged `master` here)
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes (no
change needed: no document states the entry bound)
- [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>
This commit is contained in:
authored and GitHub committed 2026-11-22 09:47:26 -08:00
1 parent 7671e0cf0d
commit be08705f66
2 files changed
+44 -1

No files matched your search

@@ -0,0 +1,43 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, expect, it } from "vitest";
import { readWorkspaceInventory } from "./remote-workspace-readonly-inventory.js";
const DIRECTORIES = 101;
const LINKS_PER_DIRECTORY = 1_000;
let root: string;
beforeEach(async () => {
root = await fs.realpath(await fs.mkdtemp(path.join(os.tmpdir(), "readonly-inventory-entries-")));
});
afterEach(async () => {
await fs.rm(root, { recursive: true, force: true });
});
async function writeLinks(parent: string, count: number) {
await fs.mkdir(parent);
await fs.writeFile(path.join(parent, "target"), "x");
await Promise.all(Array.from({ length: count }, (_, link) => fs.symlink("target", path.join(parent, `l${link}`))));
}
it("inventories a workspace with more than 100,000 entries", async () => {
for (let directory = 0; directory < DIRECTORIES; directory++) {
await writeLinks(path.join(root, `d${String(directory).padStart(3, "0")}`), LINKS_PER_DIRECTORY);
}
const inventory = await readWorkspaceInventory(root);
expect(inventory.entries).toBe(DIRECTORIES * (LINKS_PER_DIRECTORY + 2));
expect(inventory.entries).toBeGreaterThan(100_000);
expect(inventory.byteSize).toBe(DIRECTORIES);
expect(inventory.exclude).toEqual([".paperclip-runtime"]);
expect(await readWorkspaceInventory(root, true)).toEqual(inventory);
}, 240_000);
it("inventories one directory with more than 100,000 names", async () => {
await writeLinks(path.join(root, "wide"), 100_000);
const inventory = await readWorkspaceInventory(root);
expect(inventory.entries).toBe(100_002);
expect(await readWorkspaceInventory(root, true)).toEqual(inventory);
}, 240_000);
@@ -24,7 +24,7 @@ export async function readWorkspaceInventory(root: string, controllerCopy = fals
if (!before.isDirectory() || before.isSymbolicLink() || await fs.realpath(directory) !== directory) fail();
const names: string[] = [];
for await (const entry of await fs.opendir(directory)) {
check(); if (++scannedEntries > 100_000) budget(); names.push(entry.name);
check(); if (++scannedEntries > 2_000_000) budget(); names.push(entry.name);
}
const inGit = relative.split("/").includes(".git");
const repository = !inGit && names.includes(".git") ? directory : parentRepository;