Files
PaperClipAI/tests/runner-e2e/agent-filesystem-sandbox.ts
T
DottaandClaude Opus 5.5 218eb4ec8b test(runner-e2e): hide the eval harness from local native agents (#20044)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Runner E2E evals run real agents against a real Paperclip server,
then grade the result
> - A local cell runs the product from the same checkout as the eval, as
the same OS user
> - In run 42472276601 an OpenCode agent read the eval's own scenario
YAML and grader from that checkout
> - An agent that can read the oracle can steer toward it instead of the
user's request, so the eval stops measuring the product
> - This pull request hides the eval harness from every local native
agent process and removes eval names from fixtures
> - The benefit is that a passing chat eval again shows product
behavior, not oracle access

## Linked Issues or Issue Description

Refs #19614 (the chat catalog eval harness that these graders belong
to).

**What happened**
In run 42472276601, cell
`chat-features.runner-opencode.local.access.cli-authorization`
(OpenCode, DeepSeek), the agent printed its environment and saw the
checkout path in `PATH` and `PAPERCLIP_RUNTIME_NOTIFICATION_HELPER`.
OpenCode's `read` and `grep` tools were denied by its
`external_directory` policy. The agent then used its `bash` tool with
absolute paths, which that policy does not gate. It listed
`/home/runner/_work/paperclip/paperclip`, read `docs/cli/*.md` and
`cli/src/commands/client/*.ts`, ran `grep -rn` over `tests/runner-e2e`,
and found the query in `chat-full-catalog-testops-manifest.json`. It
then read `tests/runner-e2e/chat-scenarios/access.yaml`, the manifest
entry, and the case grader
`chat-catalog-domains/identity-cli-authorization.ts`. Its reasoning
said: "This is the exact test case!" Separately, fixture company names
contained the case id, for example `Runner E2E
chat-features.runner-opencode.local.access.private-resource …`, and an
agent noticed that it was in a test.

**Expected behavior**
The agent under test cannot read the eval's scenarios, fixtures, graders
or captured evidence. It keeps everything that it needs: its workspace,
its home, the product skills, the product packages and temporary files.
The names it can see do not contain case ids, "E2E" or "test".

**Steps to reproduce**
1. Dispatch `runner-full-stack-e2e.yml` for any
`chat-features.runner-opencode.local.*` cell on master.
2. In the agent run, use bash to execute `grep -rn expected_outcome
/home/runner/_work/paperclip/paperclip/tests/runner-e2e/chat-scenarios`.
3. The command returns the scenario expectations. With this change, the
command returns `Permission denied` and exit code 2.

## What Changed

- `tests/runner-e2e/agent-filesystem-sandbox.py` (new): applies a Linux
Landlock domain that grants filesystem access everywhere except the
hidden paths, sets `no_new_privs`, checks in-process that the hidden
paths are denied, then `execv`s the command with the same PID, argv and
environment.
- `tests/runner-e2e/agent-filesystem-sandbox.ts` (new): selects the
hidden paths (`tests/`, `.manual-private-trusted/`, the private evidence
directory, `RUNNER_TEMP`). It never hides the agent's temporary root,
the checkout root or the shared temporary directory. It runs a setup
probe, logs the result and fails closed in CI. Then it wraps each
`paperclip-runnerd` spawn of the E2E server.
- Follow-up: run 42445668869 showed that the hidden list missed two
places. A fixture worker listed Playwright's compiled copy of the
harness and graders with source maps
(`/tmp/playwright-transform-cache-1001/`). An agent in another run
listed the attempt root's `artifacts-private/`, `control/` and
`selected-private-inputs/` under `/tmp`. A sandboxed attempt now creates
its root in `/var/tmp`. The reason: Landlock hides a path by leaving its
ancestors ungranted, and agents must still create files in `/tmp`. The
launcher sets `PWTEST_CACHE_DIR` to a hidden child of that root and
removes the default cache. The hidden list covers the four private
children of the root, including children that are created after runnerd
starts. The probe also checks that the default Playwright cache is not
listable.
- `tests/runner-e2e/launch.ts`: passes the selection to the server
process. The selection applies only to Linux local native cells. It does
not apply to Codex cells (Codex's bubblewrap sandbox cannot mount under
Landlock), installed-release launches, legacy adapters or Daytona.
- `tests/runner-e2e/server-entry.ts`: installs the sandbox before the
product starts. The product server itself stays unrestricted because it
loads this entry from the harness.
- `tests/runner-e2e/fixture-names.ts` (new), `catalog.ts`,
`live-fixtures.ts`: fixture company, agent, environment, secret, AI
connection and project names and descriptions use a neutral word and an
8-character digest suffix. They do not contain case ids or "Runner E2E".
No grader or harness lookup reads these names.
- `tests/runner-e2e/agent-filesystem-sandbox.test.ts` (new): tests the
selection, the launch rewrite and the server-entry order. On Linux with
Landlock, it also tests that a sandboxed `grep` over a fake `tests/` dir
is denied while the product file stays readable.
- `tests/runner-e2e/SECURITY.md`: documents the boundary and its limits.
- The trusted files `manual-private-handoff.ts`,
`manual-private-retention.ts` and `manual-private-report-assets.ts` are
byte-identical to master.

## Verification

- `pnpm exec vitest run --config tests/runner-e2e/vitest.config.ts
tests/runner-e2e/agent-filesystem-sandbox.test.ts
tests/runner-e2e/live-fixtures.test.ts tests/runner-e2e/catalog.test.ts
tests/runner-e2e/codex-ci-sandbox.test.ts
tests/runner-e2e/server-entry.test.ts`: 151 passed, 1 skipped. The
Landlock test is skipped on macOS. `npx tsc --noEmit -p
tests/runner-e2e` shows no errors in the changed files.
- Paid run
[42480314919](https://github.com/paperclipai/paperclip/actions/runs/42480314919)
for `chat-features.runner-opencode.local.agents.change-manager` ran on
branch `chat-eval/eval-isolation-verify`, which is this commit
cherry-picked onto the chat catalog candidate. The cell passed. Its
`server.log` records:
- `probe …
"harnessFile":"denied","harnessDirectory":"denied","productFile":"readable","temporaryWrite":"allowed"`,
and `denied` for each of `…/paperclip/tests`,
`…/.manual-private-trusted`,
`…/_temp/manual-private-evidence-*/capture`,
`…/_temp/manual-private-evidence-*` and `…/_temp`. `"failures":[]`.
  - `runnerd launched inside the sandbox (pid 3548)`.
- The fixture company was `Company cc369a2e`. No captured snapshot
contains "Runner E2E".
- The probe is a regression check on every local native cell: a missing
or ineffective sandbox stops the attempt in CI before an agent starts.

## Risks

- Landlock does not hide that a path exists. `stat` of
`tests/runner-e2e` still succeeds (the probe logs `harnessDirectoryStat:
visible`). Reading and listing are denied.
- Directories above a hidden path (for example `/`, `/home/runner` and
the checkout root) cannot be listed. New top-level entries in those
directories are not accessible after launch. Agent state, `HOME` and
temporary files are in `/tmp`, which stays fully granted.
- Not covered: Codex cells, legacy adapters, installed-release launches
and macOS. An agent that can reach the Docker daemon can still mount the
checkout into a container. `ps` still shows the launcher's command line,
which includes the case id. The temporary root
(`paperclip-runner-e2e-*`) and instance id (`runner-e2e-*`) in the
agent's paths still say "e2e". The chat catalog candidate depends on
those prefixes, so this PR does not rename them.
- Python 3 must be present on Linux runners. Ubuntu runners have it, and
the harness already uses it.

## Model Used

- Anthropic Claude Opus 5.5 (`claude-opus-5-5`), agentic coding with
tool use (shell, file edits, GitHub CLI).

## 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>
2026-11-08 20:10:21 -06:00

311 lines
15 KiB
TypeScript

import childProcess, { execFileSync, type SpawnOptions } from "node:child_process";
import { accessSync, constants, existsSync, readFileSync, realpathSync, statSync } from "node:fs";
import { syncBuiltinESMExports } from "node:module";
import os from "node:os";
import { promisify } from "node:util";
import path from "node:path";
import { requiresCodexCiSandbox } from "./codex-ci-sandbox.js";
import type { MatrixExecution } from "./types.js";
/**
* The agent under test must not read the eval: its scenarios, fixtures,
* graders or captured evidence. A local cell runs the product from the same
* checkout and as the same OS user as the harness, so file modes cannot
* separate them. The server launches every native agent through runnerd; the
* E2E server entry starts runnerd inside a Linux Landlock domain that hides
* these paths from runnerd and all of its descendants (provider, tools, shell).
* The product server itself stays unrestricted because it loads its E2E entry
* from the harness.
*/
export const AGENT_FILESYSTEM_SANDBOX_ENV = "PAPERCLIP_RUNNER_E2E_AGENT_FS_SANDBOX";
export const RUNNERD_EXECUTABLE_NAME = "paperclip-runnerd";
export interface AgentFilesystemSandboxConfig {
hidden: string[];
required: boolean;
}
function within(candidate: string, root: string) {
return candidate === root || candidate.startsWith(`${root}${path.sep}`);
}
/** Harness-private directories inside the attempt's temporary root. */
export const HARNESS_PRIVATE_TEMPORARY_CHILDREN = [
"artifacts-private",
"control",
"selected-private-inputs",
// Playwright's compiled copy of the harness and graders, with source maps.
"playwright-transform-cache",
] as const;
/** Playwright otherwise caches compiled harness code in the shared temporary directory. */
export function playwrightCacheDirectory(temporaryRoot: string) {
return path.join(temporaryRoot, "playwright-transform-cache");
}
/** Playwright's default cache location for this user, which the launcher removes. */
export function defaultPlaywrightCacheDirectory(temporaryDirectory = os.tmpdir()) {
return path.join(temporaryDirectory, `playwright-transform-cache-${process.geteuid?.() ?? ""}`);
}
export function agentFilesystemSandboxApplies(input: {
platform: NodeJS.Platform;
executions: readonly MatrixExecution[];
installedRelease: boolean;
}) {
if (input.platform !== "linux" || input.installedRelease) return false;
if (!input.executions.some((execution) => execution.environment.id === "local" && execution.profile.generation === "native")) return false;
// Codex's own bubblewrap sandbox mounts, which Landlock forbids. Those cells
// keep their existing isolation until Codex permission profiles hide the eval.
return !input.executions.some(requiresCodexCiSandbox);
}
/**
* Landlock can only hide a path by leaving its ancestors ungranted, and an
* ungranted directory accepts no new entries. Hiding the attempt's private
* children of a root under /tmp would stop agents creating files in /tmp, so a
* sandboxed attempt keeps its root in /var/tmp instead.
*/
export function agentTemporaryParent(applies: boolean, sharedTemporary = os.tmpdir()) {
if (applies) {
try {
accessSync("/var/tmp", constants.W_OK | constants.X_OK);
return realpathSync("/var/tmp");
} catch {
// Fall back to the shared directory; its private children stay visible.
}
}
return realpathSync(sharedTemporary);
}
/** Selects the paths hidden from local native agents, or null when not applicable. */
export function agentFilesystemSandboxConfig(input: {
platform: NodeJS.Platform;
env: NodeJS.ProcessEnv;
executions: readonly MatrixExecution[];
repositoryRoot: string;
temporaryRoot: string;
privateDir: string;
evidenceRoot?: string;
installedRelease: boolean;
sharedTemporary?: string;
gitStores?: readonly string[];
}): AgentFilesystemSandboxConfig | null {
if (!agentFilesystemSandboxApplies(input)) return null;
const sharedTemporary = path.resolve(input.sharedTemporary ?? (input.env.TMPDIR?.trim() || os.tmpdir()));
const required = input.env.GITHUB_ACTIONS === "true";
// Harness material that must be hidden. Git holds the committed scenarios
// and graders, so `git show HEAD:tests/...` would otherwise bypass `tests/`.
const essential = [
path.join(input.repositoryRoot, "tests"),
...(input.gitStores ?? gitStorePaths(input.repositoryRoot)),
path.join(input.repositoryRoot, ".manual-private-trusted"),
...HARNESS_PRIVATE_TEMPORARY_CHILDREN.map((child) => path.join(input.temporaryRoot, child)),
input.privateDir,
];
const optional = [input.evidenceRoot, input.env.RUNNER_TEMP];
const hidden: string[] = [];
const exposed: string[] = [];
for (const [candidate, mustHide] of [...essential.map((value) => [value, true] as const), ...optional.map((value) => [value, false] as const)]) {
const value = candidate?.trim();
if (!value || !path.isAbsolute(value)) continue;
const resolved = path.resolve(value);
// Never hide a path that holds the agent's own state, or one beneath the
// shared temporary directory: its ancestors would lose new entries.
if (within(input.temporaryRoot, resolved) || within(input.repositoryRoot, resolved)
|| within(resolved, sharedTemporary) || within(sharedTemporary, resolved)) {
if (mustHide) exposed.push(resolved);
continue;
}
if (!hidden.includes(resolved)) hidden.push(resolved);
}
if (exposed.length) {
const message = `agent_filesystem_sandbox_incomplete: cannot hide ${exposed.join(", ")}; keep the checkout and attempt root outside ${sharedTemporary}`;
// A required sandbox fails closed rather than run an eval agents can read.
if (required) throw new Error(message);
console.warn(`[agent-filesystem-sandbox] ${message}`);
}
return hidden.length ? { hidden, required } : null;
}
export function parseAgentFilesystemSandboxConfig(value: string | undefined): AgentFilesystemSandboxConfig | null {
if (!value?.trim()) return null;
const parsed = JSON.parse(value) as Partial<AgentFilesystemSandboxConfig>;
if (!Array.isArray(parsed.hidden) || parsed.hidden.some((entry) => typeof entry !== "string" || !path.isAbsolute(entry)) || typeof parsed.required !== "boolean") {
throw new Error(`${AGENT_FILESYSTEM_SANDBOX_ENV} is malformed`);
}
return { hidden: parsed.hidden, required: parsed.required };
}
export const agentFilesystemSandboxHelper = path.join(import.meta.dirname, "agent-filesystem-sandbox.py");
/** Rewrites one launch so the helper applies the domain and then execs it unchanged. */
export function sandboxedInvocation(
config: AgentFilesystemSandboxConfig,
python: string,
command: string,
args: readonly string[],
env: NodeJS.ProcessEnv,
): { command: string; args: string[] } {
// Python may coerce a C locale by exporting LC_CTYPE; restore the caller's value.
const locale = env.LC_CTYPE === undefined ? ["--unset", "LC_CTYPE"] : ["--set", `LC_CTYPE=${env.LC_CTYPE}`];
return {
command: python,
args: [
"-I", "-S", agentFilesystemSandboxHelper,
...(config.required ? ["--required"] : []),
...locale,
...config.hidden.flatMap((hidden) => ["--hide", hidden]),
"--", command, ...args,
],
};
}
/** Matches runnerd by executable name, whether absolute or resolved through PATH. */
export function isRunnerdLaunch(command: unknown) {
return typeof command === "string" && path.basename(command) === RUNNERD_EXECUTABLE_NAME;
}
/**
* The checkout's Git store holds the committed scenarios and graders. A linked
* worktree's `.git` is a file that points into the main repository, so hide the
* worktree's gitdir and the shared common directory as well.
*/
export function gitStorePaths(repositoryRoot: string): string[] {
const dotGit = path.join(repositoryRoot, ".git");
const stores = [dotGit];
try {
if (!statSync(dotGit).isFile()) return stores;
const pointer = /^gitdir:\s*(.+)$/m.exec(readFileSync(dotGit, "utf8"))?.[1]?.trim();
if (!pointer) return stores;
const gitDir = path.resolve(repositoryRoot, pointer);
stores.push(gitDir);
const common = readFileSync(path.join(gitDir, "commondir"), "utf8").trim();
if (common) stores.push(path.resolve(gitDir, common));
} catch {
// A missing .git or commondir leaves only the paths found so far.
}
return stores;
}
export function pythonExecutable() {
for (const candidate of ["/usr/bin/python3", "/usr/local/bin/python3"]) {
if (existsSync(candidate)) return candidate;
}
return "python3";
}
/** Proves, before any agent starts, that a sandboxed process cannot read the eval. */
export function probeAgentFilesystemSandbox(
config: AgentFilesystemSandboxConfig,
repositoryRoot: string,
python = pythonExecutable(),
env: NodeJS.ProcessEnv = process.env,
playwrightCache = defaultPlaywrightCacheDirectory(),
) {
const harness = path.join(repositoryRoot, "tests", "runner-e2e");
const script = [
'out=""; add() { out="$out$1=$2;"; }',
'if cat "$1" >/dev/null 2>&1; then add harnessFile readable; else add harnessFile denied; fi',
'if ls "$2" >/dev/null 2>&1; then add harnessDirectory listable; else add harnessDirectory denied; fi',
'if test -e "$2"; then add harnessDirectoryStat visible; else add harnessDirectoryStat absent; fi',
'if cat "$3" >/dev/null 2>&1; then add productFile readable; else add productFile denied; fi',
'probe=$(mktemp) && rm -f "$probe" && add temporaryWrite allowed || add temporaryWrite denied',
'if ls "$4" >/dev/null 2>&1; then add playwrightDefaultCache listable; else add playwrightDefaultCache denied; fi',
'shift 4; for hidden in "$@"; do if ls "$hidden" >/dev/null 2>&1; then add "hidden:$hidden" listable; else add "hidden:$hidden" denied; fi; done',
'printf "%s\\n" "$out"',
].join("\n");
const invocation = sandboxedInvocation(config, python, "/bin/sh", [
"-c", script, "sh",
path.join(harness, "agent-filesystem-sandbox.ts"),
harness,
path.join(repositoryRoot, "packages", "paperclip-runner", "package.json"),
playwrightCache,
...config.hidden,
], env);
const output = execFileSync(invocation.command, invocation.args, { encoding: "utf8", cwd: "/", env, timeout: 30_000 });
const result = Object.fromEntries(output.trim().split(";").filter(Boolean).map((entry) => {
const separator = entry.lastIndexOf("=");
return [entry.slice(0, separator), entry.slice(separator + 1)];
}));
const failures = [
result.harnessFile !== "denied" && "harness file is readable",
result.harnessDirectory !== "denied" && "harness directory is listable",
result.productFile !== "readable" && "product package is unreadable",
result.temporaryWrite !== "allowed" && "temporary directory is not writable",
result.playwrightDefaultCache !== "denied" && "Playwright's default transform cache is listable",
...config.hidden.map((hidden) => result[`hidden:${hidden}`] === "listable" && `${hidden} is listable`),
].filter((failure): failure is string => Boolean(failure));
return { result, failures };
}
/**
* Wraps runnerd launches made by this process. Runs only in the E2E server
* entry; the wrapper execs runnerd, so its PID, argv, cwd and environment are
* the ones the product requested.
*/
export function installAgentFilesystemSandbox(repositoryRoot: string, env: NodeJS.ProcessEnv = process.env) {
const config = parseAgentFilesystemSandboxConfig(env[AGENT_FILESYSTEM_SANDBOX_ENV]);
delete env[AGENT_FILESYSTEM_SANDBOX_ENV];
if (!config) return null;
const python = pythonExecutable();
let probe: ReturnType<typeof probeAgentFilesystemSandbox>;
try {
probe = probeAgentFilesystemSandbox(config, repositoryRoot, python);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
if (config.required) throw new Error(`agent_filesystem_sandbox_unavailable: ${message}`);
console.warn(`[agent-filesystem-sandbox] disabled: ${message}`);
return null;
}
console.log(`[agent-filesystem-sandbox] probe ${JSON.stringify({ hidden: config.hidden, required: config.required, ...probe })}`);
if (probe.failures.length) {
if (config.required) throw new Error(`agent_filesystem_sandbox_ineffective: ${probe.failures.join("; ")}`);
console.warn(`[agent-filesystem-sandbox] disabled: ${probe.failures.join("; ")}`);
return null;
}
const launchers = childProcess as unknown as Record<RunnerdLaunchApi, (...args: unknown[]) => unknown>;
for (const api of RUNNERD_LAUNCH_APIS) launchers[api] = wrapRunnerdLaunch(launchers[api], config, python, api);
syncBuiltinESMExports();
return config;
}
/** Every child_process API that can start an executable directly. */
export const RUNNERD_LAUNCH_APIS = ["spawn", "spawnSync", "execFile", "execFileSync"] as const;
export type RunnerdLaunchApi = typeof RUNNERD_LAUNCH_APIS[number];
/**
* Wraps one launch API so runnerd starts inside the sandbox and other launches
* pass through. A runnerd launch the wrapper cannot rewrite fails closed.
*/
export function wrapRunnerdLaunch<T extends (...args: any[]) => unknown>(original: T, config: AgentFilesystemSandboxConfig, python: string, api: RunnerdLaunchApi = "spawn", wrapCustom = true): T {
const wrapped = function (this: unknown, command: unknown, ...rest: unknown[]) {
if (!isRunnerdLaunch(command)) return Reflect.apply(original, this, [command, ...rest]);
const hasArgs = Array.isArray(rest[0]);
const args = hasArgs ? rest[0] as string[] : [];
const afterArgs = hasArgs ? rest.slice(1) : rest;
const options = afterArgs[0] && typeof afterArgs[0] === "object" ? afterArgs[0] as SpawnOptions : undefined;
if (options?.shell) throw new Error("agent_filesystem_sandbox: refusing to start runnerd through a shell, outside the sandbox");
const invocation = sandboxedInvocation(config, python, command as string, args, options?.env ?? process.env);
const result = Reflect.apply(original, this, [invocation.command, invocation.args, ...afterArgs]);
const pid = (result as { pid?: number } | undefined)?.pid;
console.log(`[agent-filesystem-sandbox] runnerd launched inside the sandbox via ${api}${pid ? ` (pid ${pid})` : ""}`);
return result;
} as T;
// execFile's promisified form resolves { stdout, stderr } through this hook;
// keep it, wrapped the same way, so promisify(execFile) keeps its shape.
const custom = (original as unknown as Record<symbol, unknown>)[promisify.custom];
if (wrapCustom && typeof custom === "function") {
Object.defineProperty(wrapped, promisify.custom, {
value: wrapRunnerdLaunch(custom as (...args: any[]) => unknown, config, python, api, false),
configurable: true,
});
}
return wrapped;
}
/** Back-compatible name for the spawn wrapper. */
export function wrapRunnerdSpawn(original: typeof childProcess.spawn, config: AgentFilesystemSandboxConfig, python: string) {
return wrapRunnerdLaunch(original, config, python, "spawn");
}