Files
PaperClipAI/tests/restart-chaos/remote-oracle.test.ts
DottaandClaude Opus 5.5 0c7a628fd1 test(chaos): read remote R1 files from the execution workspace and record why a read failed (#20440)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The restart chaos harness grades Daytona R1 tasks by reading their
files through `GET /issues/:id/file-resources/content`
> - In replays w-20261110T010031-s9913 and w-20261110T012332-s9950 the
tasks recovered (R1, R3, R6 pass), but all 506 content reads returned
422, even after the closing save left the workspace clean (`snapshotId`
set, no writer, `location: null`)
> - The harness discarded each error, so the waves could not show which
422 the server returned
> - This pull request asks for the execution workspace explicitly and
records the last error per file in the task record and the timeline
> - The benefit is that the next wave names the exact cause instead of a
blind `module_present` failure

## Linked Issues or Issue Description

Follow-up to #20348.

**What happened?**
Every file read for a finished Daytona R1 task returned 422, and the
response was discarded.

**Expected behavior**
Failed reads leave their reason in the evidence.

**Actual behavior**
Only the outcome, a `null` file, was kept.

## What Changed

- File reads add `workspace=execution`, so the server reads the issue's
execution workspace and not the `auto` choice.
- `readRemoteWorkspaceFile` and `captureRemoteWorkspaceFiles` report
each failure through a callback. The observer keeps the last message per
file in `record.remoteFileErrors` and logs `remote_file_read_failed`
once per distinct message.
- The remote-workspace file reader stays in `remote-workspace-files.ts`;
`observer.ts` is at 993 lines.

## Verification

- `npx vitest run --config tests/restart-chaos/vitest.config.ts`: 263
passed. The test server now requires `workspace=execution`, so the
existing remote tests were red before, and a new test records the 422
`workspace_unavailable` message.
- `node scripts/typecheck-test-projects.mjs tests/restart-chaos`: clean.

## Risks

- If a Daytona task's files only exist in a project workspace rather
than its execution workspace, an explicit `execution` selector reports
that clearly instead of falling back.

## Model Used

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

## 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-09 21:34:35 -06:00

177 lines
8.9 KiB
TypeScript

import { createServer, type Server } from "node:http";
import { once } from "node:events";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { ChaosApi } from "./api.js";
import { TaskObserver } from "./observer.js";
import type { ChaosLab } from "./seed.js";
import { Timeline } from "./timeline.js";
import type { TaskPlan } from "./types.js";
const nonce = "abcd1234";
const modulePath = `packages/shared/src/chaos/slug-${nonce}.ts`;
const testPath = `packages/shared/src/chaos/slug-${nonce}.test.ts`;
const remoteFiles: Record<string, string> = {
[modulePath]: `export function chaosSlug(input: string): string {
return input.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
}
`,
[testPath]: `import { test } from "node:test";
import assert from "node:assert/strict";
import { chaosSlug } from "./slug-${nonce}.ts";
test("slug", () => {
assert.equal(chaosSlug("Hello Chaos World"), "hello-chaos-world");
assert.equal(chaosSlug(" a__b "), "a-b");
assert.equal(chaosSlug("x"), "x");
});
`,
};
const lab = { companyId: "c1", projectId: "p1", environmentId: "e1", environmentKind: "daytona", fixtureRepositoryPath: "/x",
agents: { codex: { id: "a1", name: "Chaos codex" }, claude: { id: "a2", name: "Chaos claude" } } } as unknown as ChaosLab;
const plan: TaskPlan = { taskId: "t01", index: 0, workload: "R1", adapter: "codex", fault: "K1", fireAfterMs: 999_999,
holdMs: 0, followUpAfterMs: null, nonce };
let server: Server;
let sandboxReleased = false;
let unavailableReads = 0;
let baseUrl: string;
beforeAll(async () => {
server = createServer((incoming, outgoing) => {
const url = new URL(incoming.url ?? "/", "http://x");
if (url.pathname === "/api/issues/i1/file-resources/content" && url.searchParams.get("download") === "true") {
if (url.searchParams.get("workspace") !== "execution") { outgoing.writeHead(422, { "content-type": "application/json" }).end(JSON.stringify({ code: "no_local_workspace" })); return; }
if (incoming.headers.origin !== baseUrl) { outgoing.writeHead(403).end(); return; }
if (unavailableReads > 0) { unavailableReads -= 1; outgoing.writeHead(422, { "content-type": "application/json" }).end(JSON.stringify({ code: "workspace_unavailable" })); return; }
if (sandboxReleased) { outgoing.writeHead(422, { "content-type": "application/json" }).end(JSON.stringify({ code: "workspace_unavailable" })); return; }
const body = remoteFiles[url.searchParams.get("path") ?? ""];
outgoing.writeHead(body ? 200 : 404, { "content-type": "text/plain" }).end(body ?? "");
return;
}
outgoing.writeHead(404).end();
});
server.listen(0, "127.0.0.1"); await once(server, "listening");
baseUrl = `http://127.0.0.1:${(server.address() as { port: number }).port}`;
});
afterAll(async () => { server.closeAllConnections(); server.close(); });
describe("oracle for a remote workspace", () => {
it("reads the task's files through the board API and runs the oracle commands on a local copy", async () => {
const api = {
baseUrl,
async get(route: string) {
if (route.endsWith("/comments")) return [];
return { id: "i1", status: "done", currentExecutionWorkspace: { cwd: "/home/daytona/workspaces/not-on-this-host" } };
},
async post() { return { id: "i1", identifier: "CHA-1" }; },
getText: (route: string) => new ChaosApi(baseUrl).getText(route),
} as unknown as ChaosApi;
const observer = new TaskObserver(plan, api, lab, new Timeline(null), { serverReady: true, faultInProgress: false });
await observer.launch();
sandboxReleased = false;
const artifacts = await observer.collectArtifacts();
expect(artifacts.files).toEqual(remoteFiles);
expect(artifacts.commands.node_test).toMatchObject({ exitCode: 0 });
expect(artifacts.commands.slug?.stdout).toContain("hello-chaos-world");
}, 60_000);
it("skips the local pre-fault listing for a workspace that lives in the sandbox, instead of failing R2", async () => {
const api = {
baseUrl,
async get(route: string) { return route.endsWith("/runs") ? [] : { id: "i1", status: "in_progress", currentExecutionWorkspace: { cwd: "/home/daytona/workspaces/not-on-this-host" } }; },
async post() { return { id: "i1", identifier: "CHA-1" }; },
} as unknown as ChaosApi;
const observer = new TaskObserver(plan, api, lab, new Timeline(null), { serverReady: true, faultInProgress: false });
await observer.launch();
await observer.poll(observer.record.createdAt! + 1_000);
await observer.snapshotBeforeFault();
expect(observer.record).toMatchObject({ preFaultFiles: null, remoteWorkspace: true });
expect(observer.record.preFaultSnapshotError).toBeUndefined();
});
it("captures the task's files when it first sees the task done, before the sandbox is released", async () => {
const api = {
baseUrl,
async get(route: string) {
if (route.endsWith("/runs") || route.endsWith("/comments")) return [];
return { id: "i1", status: "done", currentExecutionWorkspace: { cwd: "/home/daytona/workspaces/not-on-this-host" } };
},
async post() { return { id: "i1", identifier: "CHA-1" }; },
getText: (route: string) => new ChaosApi(baseUrl).getText(route),
} as unknown as ChaosApi;
const observer = new TaskObserver(plan, api, lab, new Timeline(null), { serverReady: true, faultInProgress: false });
await observer.launch();
sandboxReleased = false;
await observer.poll(observer.record.createdAt! + 1_000);
sandboxReleased = true;
try {
const artifacts = await observer.collectArtifacts();
expect(artifacts.files).toEqual(remoteFiles);
expect(artifacts.commands.node_test).toMatchObject({ exitCode: 0 });
} finally { sandboxReleased = false; }
}, 60_000);
it("waits for the closing save to make a remote workspace readable again before grading its files", async () => {
const api = {
baseUrl,
async get(route: string) {
if (route.endsWith("/comments")) return [];
return { id: "i1", status: "done", currentExecutionWorkspace: { cwd: "/home/daytona/workspaces/not-on-this-host" } };
},
async post() { return { id: "i1", identifier: "CHA-1" }; },
getText: (route: string) => new ChaosApi(baseUrl).getText(route),
} as unknown as ChaosApi;
const observer = new TaskObserver(plan, api, lab, new Timeline(null), { serverReady: true, faultInProgress: false });
await observer.launch();
sandboxReleased = false;
unavailableReads = 2;
const artifacts = await observer.collectArtifacts();
expect(unavailableReads).toBe(0);
expect(artifacts.files).toEqual(remoteFiles);
}, 60_000);
it("does not wait on a file the agent never wrote", async () => {
const api = {
baseUrl,
async get(route: string) {
if (route.endsWith("/comments")) return [];
return { id: "i1", status: "done", currentExecutionWorkspace: { cwd: "/home/daytona/workspaces/not-on-this-host" } };
},
async post() { return { id: "i1", identifier: "CHA-1" }; },
getText: (route: string) => new ChaosApi(baseUrl).getText(route.replace(/slug-/g, "missing-")),
} as unknown as ChaosApi;
const observer = new TaskObserver(plan, api, lab, new Timeline(null), { serverReady: true, faultInProgress: false });
await observer.launch();
const started = Date.now();
const artifacts = await observer.collectArtifacts();
expect(Object.values(artifacts.files)).toEqual([null, null]);
expect(Date.now() - started).toBeLessThan(10_000);
}, 60_000);
it("records why a remote file could not be read", async () => {
const api = {
baseUrl,
async get(route: string) {
if (route.endsWith("/comments") || route.endsWith("/runs")) return [];
return { id: "i1", status: "done", currentExecutionWorkspace: { cwd: "/home/daytona/workspaces/not-on-this-host" } };
},
async post() { return { id: "i1", identifier: "CHA-1" }; },
getText: (route: string) => new ChaosApi(baseUrl).getText(route),
} as unknown as ChaosApi;
const timeline = new Timeline(null);
const observer = new TaskObserver(plan, api, lab, timeline, { serverReady: true, faultInProgress: false });
await observer.launch();
sandboxReleased = true;
try {
await observer.poll(observer.record.createdAt! + 1_000);
await observer.poll(observer.record.createdAt! + 5_000);
} finally { sandboxReleased = false; }
expect(observer.record.remoteFileErrors?.[modulePath]).toMatch(/422.*workspace_unavailable/);
expect(timeline.entries.filter(entry => entry.kind === "remote_file_read_failed" && entry.path === modulePath)).toHaveLength(1);
await observer.poll(observer.record.createdAt! + 9_000);
expect(observer.record.remoteFiles?.[modulePath]).toBe(remoteFiles[modulePath]);
const artifacts = await observer.collectArtifacts();
expect(artifacts.files[modulePath]).toBe(remoteFiles[modulePath]);
expect(observer.record.remoteFileErrors?.[modulePath]).toBeUndefined();
}, 60_000);
});