mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-29 15:52:36 +01:00
fix(workspaces): run the full-disk read-only inventory's Git reads under the sandbox storage guard (#23095)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - A Cloud sandbox saves the agent's workspace when a run ends. When the sandbox disk is full, a read-only fallback copies the workspace without writing to the sandbox (#19465). > - Before the copy, the fallback runs an inventory of the workspace inside the sandbox. For each Git repository, the inventory runs `git rev-parse`. For an untracked `node_modules` or virtual environment, it also runs `git ls-files`. > - Cloud sandboxes now put a Git storage guard first on `PATH`. The guard reads its limits from three environment settings: `PAPERCLIP_GIT_MIN_FREE_BYTES`, `PAPERCLIP_GIT_MIN_FREE_INODES` and `PAPERCLIP_GIT_MAX_GROWTH_BYTES`. Without them, it refuses every Git command as misconfigured (exit 75). > - The inventory gives Git a minimal environment, and that environment drops the three settings. Thus the guard refuses the first Git read, and the inventory refuses every Git workspace as an unsafe source. > - This pull request keeps the guard's three settings in the inventory's Git environment. It also records a guard refusal by its own reason. > - The benefit is that a full-disk save of a Git workspace can complete again. If the guard refuses Git below its free-space floor, the recorded cause says so. ## Linked Issues or Issue Description Refs #19465, #19619, #22546, #22848 **What happened?** On staging, full-disk workspace saves fail again on releases since the sandbox image added the Git storage guard. Nine saves from 2026-11-24 to 2026-11-27 recorded this cause: `remote_workspace_sandbox_disk_full: ...; remote_workspace_readonly_capture_failed: stage=inspect reason=unsafe_source transport=provider_error`. All of them ran on releases that contain #22546. Thus the earlier `budget_exceeded` refusal is fixed, and this refusal is the next one. **Expected behavior** The read-only inventory runs Git with the same guard settings that normal capture gets. Normal capture keeps `process.env` for Git, so the guard is configured there. A Git workspace that normal capture could save passes the inventory. **Steps to reproduce** 1. In a Cloud sandbox, put the storage guard first on `PATH`. Set its three settings in the sandbox environment (the image sets them for the sandbox process tree). 2. Run `readonlyWorkspaceInventoryScript()` on a Git workspace. 3. On `master`, the script exits 1 with `remote_workspace_readonly_capture_unsafe`. With this change, it returns the inventory. **Relevant logs or output** `paperclip_git_storage_limit: Git's storage guard is unavailable or misconfigured; Git did not complete.` The guard writes this message when its settings are missing. ## What Changed - `readWorkspaceInventory` copies the three guard settings from `process.env` into its Git environment when they are set. It copies no other variable. The Git isolation settings do not change. - When Git fails and the guard wrote a `paperclip_git_storage_limit:` line, the inventory reports `remote_workspace_readonly_capture_git_storage_limit` for a free-space, inode or growth refusal. It reports `remote_workspace_readonly_capture_git_storage_guard_unavailable` for any other guard refusal. All other Git failures still report `remote_workspace_readonly_capture_unsafe`. - The sandbox script passes these two fixed tokens through, the same as the budget token. It still writes no error text or paths. - `captureReadOnlyWorkspaceArchive` records them as `reason=git_storage_limit` and `reason=git_storage_guard_unavailable`. - The sandbox command filter (`READONLY_INVENTORY_FAILURE`) forwards the two new tokens. Without this, `command()` drops them and the fallback records `reason=operation_failed`. ## Verification - New `remote-workspace-readonly-inventory-git-guard.test.ts`: it puts a stand-in guard on `PATH` that behaves like the sandbox guard. All 3 tests fail on `master` with `remote_workspace_readonly_capture_unsafe`. All 3 pass with this change. - New case in `remote-workspace-readonly-capture-cause.test.ts`: a guard refusal of `ls-files` is recorded as `stage=inspect reason=git_storage_limit` before any transfer. I could not run this case locally because the sandbox has no full dependency install. CI verifies it. - Real guard check: I ran the inventory script from `master` and from this branch on a real 857 MB Paperclip workspace (18,849 entries). It ran through the actual sandbox guard, with the sandbox's own settings in the environment. `master` refused the workspace with `remote_workspace_readonly_capture_unsafe`. This branch returned the inventory. - A strict TypeScript check of the changed inventory module passes. CI runs the full typecheck. ## Risks - Low risk. Only the three guard settings are added to the Git environment. Git still runs with no system or global configuration, no hooks, no optional locks and no lazy fetch. - The guard still applies its free-space floor. Below the floor, it refuses `ls-files`. Thus a Git workspace with an untracked `node_modules` or virtual environment can still fail the fallback when free space is below the floor. The recorded cause now shows `reason=git_storage_limit` for that case, so a follow-up can use real evidence. - Outside a guarded sandbox, the settings are not set and nothing changes. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), with tool use and code execution in a Paperclip agent 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>
This commit is contained in:
4 files changed
+105
-5
No files matched your search
@@ -85,6 +85,30 @@ describe("recorded cause of a failed full-disk read-only capture", () => {
|
||||
} finally { await fs.chmod(secret, 0o600); }
|
||||
});
|
||||
|
||||
it.skipIf(process.platform === "win32")("names a sandbox Git storage guard refusal from the inventory before any transfer", async () => {
|
||||
const realGit = path.join((await exec("git", ["--exec-path"])).stdout.trim(), "git");
|
||||
await exec(realGit, ["-C", workspace, "init", "-q"]);
|
||||
await fs.writeFile(path.join(workspace, "package.json"), "{}\n");
|
||||
await fs.mkdir(path.join(workspace, "node_modules"));
|
||||
const bin = path.join(scratch, "bin");
|
||||
await fs.mkdir(bin);
|
||||
await fs.writeFile(path.join(bin, "git"), [
|
||||
"#!/bin/sh",
|
||||
"for arg in \"$@\"; do case \"$arg\" in ls-files)",
|
||||
" echo \"paperclip_git_storage_limit: The sandbox has too little free disk space or too few free inodes; Git did not start.\" >&2; exit 75;;",
|
||||
"esac; done",
|
||||
`exec ${JSON.stringify(realGit)} "$@"`,
|
||||
"",
|
||||
].join("\n"), { mode: 0o755 });
|
||||
vi.stubEnv("PATH", `${bin}${path.delimiter}${process.env.PATH}`);
|
||||
try {
|
||||
const remote = target();
|
||||
expect(remoteWorkspaceFailureCause(await capture(remote)))
|
||||
.toBe(`${DISK_FULL}; remote_workspace_readonly_capture_failed: stage=inspect reason=git_storage_limit transport=provider_error`);
|
||||
expect(remote.runner!.syncOut).not.toHaveBeenCalled();
|
||||
} finally { vi.unstubAllEnvs(); }
|
||||
});
|
||||
|
||||
it("does not attach a cause outside the fallback grammar", () => {
|
||||
const failure = new Error(DISK_FULL, { cause: new Error("remote_workspace_readonly_capture_failed: stage=inspect reason=x /private/path") });
|
||||
expect(remoteWorkspaceFailureCause(failure)).toBe(DISK_FULL);
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { readonlyWorkspaceInventoryScript } from "./remote-workspace-readonly-inventory.js";
|
||||
|
||||
const exec = promisify(execFile);
|
||||
const LIMITS = { PAPERCLIP_GIT_MIN_FREE_BYTES: "536870912", PAPERCLIP_GIT_MIN_FREE_INODES: "1024", PAPERCLIP_GIT_MAX_GROWTH_BYTES: "1073741824" };
|
||||
const BELOW_FLOOR = "9223372036854775807";
|
||||
|
||||
describe.skipIf(process.platform === "win32")("full-disk read-only inventory under the sandbox Git storage guard", () => {
|
||||
let scratch: string;
|
||||
let workspace: string;
|
||||
let guardBin: string;
|
||||
beforeEach(async () => {
|
||||
scratch = await fs.realpath(await fs.mkdtemp(path.join(os.tmpdir(), "remote-workspace-readonly-guard-")));
|
||||
workspace = path.join(scratch, "workspace");
|
||||
guardBin = path.join(scratch, "bin");
|
||||
await fs.mkdir(workspace);
|
||||
await fs.mkdir(guardBin);
|
||||
const realGit = path.join((await exec("git", ["--exec-path"])).stdout.trim(), "git");
|
||||
await fs.writeFile(path.join(guardBin, "git"), [
|
||||
"#!/bin/sh",
|
||||
"if [ -z \"$PAPERCLIP_GIT_MIN_FREE_BYTES\" ] || [ -z \"$PAPERCLIP_GIT_MIN_FREE_INODES\" ] || [ -z \"$PAPERCLIP_GIT_MAX_GROWTH_BYTES\" ]; then",
|
||||
" echo \"paperclip_git_storage_limit: Git's storage guard is unavailable or misconfigured; Git did not complete.\" >&2; exit 75",
|
||||
"fi",
|
||||
`if [ "$PAPERCLIP_GIT_MIN_FREE_BYTES" = "${BELOW_FLOOR}" ]; then`,
|
||||
" for arg in \"$@\"; do case \"$arg\" in ls-files)",
|
||||
" echo \"paperclip_git_storage_limit: The sandbox has too little free disk space or too few free inodes; Git did not start.\" >&2; exit 75;;",
|
||||
" esac; done",
|
||||
"fi",
|
||||
`exec ${JSON.stringify(realGit)} "$@"`,
|
||||
"",
|
||||
].join("\n"), { mode: 0o755 });
|
||||
const git = (...args: string[]) => exec(realGit, ["-C", workspace, ...args]);
|
||||
await git("init", "-q");
|
||||
await fs.writeFile(path.join(workspace, "package.json"), "{}\n");
|
||||
await fs.writeFile(path.join(workspace, "saved.txt"), "retained work\n");
|
||||
await git("add", "package.json", "saved.txt");
|
||||
await fs.mkdir(path.join(workspace, "node_modules"));
|
||||
await fs.writeFile(path.join(workspace, "node_modules", "dependency.js"), "generated\n");
|
||||
});
|
||||
afterEach(async () => { await fs.rm(scratch, { recursive: true, force: true }); });
|
||||
|
||||
const inspect = (limits: Record<string, string>) => exec(process.execPath, ["-e", readonlyWorkspaceInventoryScript(), workspace], {
|
||||
timeout: 15_000, env: { PATH: `${guardBin}${path.delimiter}${process.env.PATH}`, ...limits },
|
||||
});
|
||||
|
||||
it("inventories a Git workspace when the guard's limits come from the sandbox environment", async () => {
|
||||
const { stdout } = await inspect(LIMITS);
|
||||
const inventory = JSON.parse(stdout) as { entries: number; exclude: string[] };
|
||||
expect(inventory.exclude).toEqual([".paperclip-runtime", "node_modules"]);
|
||||
expect(inventory.entries).toBeGreaterThan(2);
|
||||
});
|
||||
|
||||
it("names a guard that refuses Git below its free-space floor instead of calling the source unsafe", async () => {
|
||||
await expect(inspect({ ...LIMITS, PAPERCLIP_GIT_MIN_FREE_BYTES: BELOW_FLOOR }))
|
||||
.rejects.toMatchObject({ code: 1, stderr: "remote_workspace_readonly_capture_git_storage_limit" });
|
||||
});
|
||||
|
||||
it("names a guard that cannot read its limits", async () => {
|
||||
await expect(inspect({}))
|
||||
.rejects.toMatchObject({ code: 1, stderr: "remote_workspace_readonly_capture_git_storage_guard_unavailable" });
|
||||
});
|
||||
});
|
||||
@@ -9,14 +9,22 @@ export async function readWorkspaceInventory(root: string, controllerCopy = fals
|
||||
const digest = crypto.createHash("sha256"), exclude = [".paperclip-runtime"];
|
||||
const repositories: string[] = [], configs: string[] = [];
|
||||
let entries = 0, scannedEntries = 0, byteSize = 0, configBytes = 0;
|
||||
const storageGuard = Object.fromEntries(["PAPERCLIP_GIT_MIN_FREE_BYTES", "PAPERCLIP_GIT_MIN_FREE_INODES", "PAPERCLIP_GIT_MAX_GROWTH_BYTES"]
|
||||
.flatMap(name => typeof process.env[name] === "string" ? [[name, process.env[name]]] : []));
|
||||
const gitRead = async (cwd: string, args: string[]) => {
|
||||
check();
|
||||
const { execFile } = await import("node:child_process");
|
||||
return new Promise<string>((resolve, reject) => execFile("git", ["-c", "core.fsmonitor=false", "-c", "core.hooksPath=/dev/null", "-C", cwd, ...args], {
|
||||
signal, timeout: 10_000, maxBuffer: 64 * 1024 * 1024,
|
||||
env: { PATH: process.env.PATH, GIT_CONFIG_NOSYSTEM: "1", GIT_CONFIG_GLOBAL: "/dev/null", GIT_CONFIG_COUNT: "0",
|
||||
env: { PATH: process.env.PATH, ...storageGuard, GIT_CONFIG_NOSYSTEM: "1", GIT_CONFIG_GLOBAL: "/dev/null", GIT_CONFIG_COUNT: "0",
|
||||
GIT_OPTIONAL_LOCKS: "0", GIT_NO_LAZY_FETCH: "1" },
|
||||
}, (error, result) => error ? reject(new Error("remote_workspace_readonly_capture_unsafe")) : resolve(result)));
|
||||
}, (error, result, stderr) => {
|
||||
if (!error) return resolve(result);
|
||||
const guard = /^paperclip_git_storage_limit:(.*)$/mu.exec(String(stderr ?? ""))?.[1];
|
||||
reject(new Error(guard === undefined ? "remote_workspace_readonly_capture_unsafe"
|
||||
: /too little free disk space|too few free inodes|stopped at its storage budget/u.test(guard)
|
||||
? "remote_workspace_readonly_capture_git_storage_limit" : "remote_workspace_readonly_capture_git_storage_guard_unavailable"));
|
||||
}));
|
||||
};
|
||||
const visit = async (relative: string, parentRepository: string | null = null) => {
|
||||
check();
|
||||
@@ -133,5 +141,5 @@ export async function readWorkspaceInventory(root: string, controllerCopy = fals
|
||||
export function readonlyWorkspaceInventoryScript() {
|
||||
const source = readWorkspaceInventory.toString().replaceAll("__vite_ssr_dynamic_import__", "import");
|
||||
// A refusal names only its fixed token or an errno-style code; never error text or paths.
|
||||
return `const __name=value=>value;const inspect=${source};inspect(process.argv[1]).then(value=>process.stdout.write(JSON.stringify(value))).catch(error=>{const code=typeof error?.code==='string'&&/^E[A-Z0-9]{2,15}$/.test(error.code)?error.code.toLowerCase():null;process.stderr.write(error?.message==='remote_workspace_readonly_capture_budget'?error.message:code?'remote_workspace_readonly_capture_fs_'+code:'remote_workspace_readonly_capture_unsafe');process.exitCode=1});`;
|
||||
return `const __name=value=>value;const inspect=${source};inspect(process.argv[1]).then(value=>process.stdout.write(JSON.stringify(value))).catch(error=>{const code=typeof error?.code==='string'&&/^E[A-Z0-9]{2,15}$/.test(error.code)?error.code.toLowerCase():null;process.stderr.write(/^remote_workspace_readonly_capture_(?:budget|git_storage_limit|git_storage_guard_unavailable)$/.test(error?.message)?error.message:code?'remote_workspace_readonly_capture_fs_'+code:'remote_workspace_readonly_capture_unsafe');process.exitCode=1});`;
|
||||
}
|
||||
@@ -85,7 +85,7 @@ const CAPTURE_FAILURE_CHECK = `const captureFailure=${remoteWorkspaceCaptureFail
|
||||
// structured fields from remoteWorkspaceFailureDetail. Anything else is dropped.
|
||||
const SANDBOX_CAPTURE_CAUSE = /^remote_workspace_capture_failed: stage=(?:helper|archive|publish)(?: (?:helper_)?(?:code|exit|signal|syscall|killed|program|name|reason)=[A-Za-z0-9_]{1,64}){0,16}$/;
|
||||
// The sandbox inventory script's own refusal: a fixed token or an errno code.
|
||||
const READONLY_INVENTORY_FAILURE = /^remote_workspace_readonly_capture_(?:unsafe|budget|fs_e[a-z0-9]{2,15})$/;
|
||||
const READONLY_INVENTORY_FAILURE = /^remote_workspace_readonly_capture_(?:unsafe|budget|git_storage_limit|git_storage_guard_unavailable|fs_e[a-z0-9]{2,15})$/;
|
||||
// Setup scripts name the step they were in plus closed, structured fields. A
|
||||
// reason is only a script's own snake_case refusal code; Git output selects a
|
||||
// fixed label and is never copied. The server drops anything outside this grammar.
|
||||
@@ -623,7 +623,8 @@ export async function captureReadOnlyWorkspaceArchive(input: {
|
||||
: message === "remote_workspace_readonly_capture_changed" ? "source_changed"
|
||||
: message === "remote_workspace_readonly_capture_budget" ? "budget_exceeded"
|
||||
: message === REMOTE_WORKSPACE_SANDBOX_DISK_FULL ? "disk_full"
|
||||
: /^remote_workspace_(?:readonly_capture|archive)_(fs_e[a-z0-9]{2,15})$/.exec(message)?.[1]
|
||||
: /^remote_workspace_readonly_capture_(git_storage_limit|git_storage_guard_unavailable)$/.exec(message)?.[1]
|
||||
?? /^remote_workspace_(?:readonly_capture|archive)_(fs_e[a-z0-9]{2,15})$/.exec(message)?.[1]
|
||||
?? (error instanceof RemoteWorkspaceOperationTimeoutError ? "timed_out" : "operation_failed");
|
||||
const { label, code } = sandboxProviderFailureLabel(error);
|
||||
throw new Error(`remote_workspace_readonly_capture_failed: stage=${stage} reason=${reason} transport=${label}${code ? ` code=${code}` : ""}`);
|
||||
|
||||
Reference in new issue
Block a user