mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-04 21:21:27 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The restart-chaos harness (`tests/restart-chaos`) proves that agent work survives a control-plane release swap > - Local waves grade each task with rules R1–R6. R1 is the time budget, with credit for a provably fair queue wait (#22446, #22452) > - Hosted mode (`hosted.ts`) runs the same proof on a real test staging instance that takes automatic releases > - Hosted mode passed a run when every task finished hands-off. It did not apply R1, and banners, failed health probes, duplicates and the swap shape were only reported > - The next acceptance gate needs 10 consecutive hosted releases with 100% hands-off, 0 Retry banners and 0 failed health probes, so the exit code must enforce these rules > - This pull request adds hosted R1 and a strict "Gate D" verdict to the hosted grade > - The benefit is that one hosted run gives a single pass or fail with the same R1 rule as the local waves ## Linked Issues or Issue Description Refs #22446 Refs #22452 Refs #21153 **What happened?** A hosted run (`hosted.ts --hosted-releases N`) exited 0 when every started task finished without a person. The run did not grade R1. A Retry banner that cleared by itself, a failed `/api/health` probe, a duplicated agent comment, a stop-then-start release or a release with no live run did not fail the run. These were only lines in `SUMMARY.md`. **Expected behavior** A hosted run passes only when every task finishes hands-off and inside the R1 budget (with the same fair queue-wait credit as the local waves), no Retry banner shows, no health probe fails, no work is duplicated, no release is stop-then-start, and each release carries at least one live run. **Steps to reproduce** 1. Run `hosted.ts --hosted-releases 1` against a test staging instance whose deploy is stop-then-start. 2. Let all tasks finish by themselves after the outage. 3. See exit 0, although health probes failed during the release. **Paperclip version or commit** `95ba0a7de6` (master) ## What Changed - `hosted-gate.ts` (new): `hostedR1` builds the R1 input from API rows and calls `gradeR1` from `grader.ts`. It uses the staging-mix budget (`stagingMixBudgetMs`), outage time only for a release whose health probes failed before the new build answered, and the fair queue-wait proof (`queueWaitFairnessFrom`) from the company run list and each agent's `maxConcurrentRuns`. A follow-up run's wait is not credited, because the API has no wake evidence. `hostedGate` lists seven checks and passes only when all hold. - `hosted-readers.ts`: run rows keep `invocationSource`, `retryOfRunId` and the wake comment ids. - `hosted-grade.ts`: grades every started task with R1, adds the `Gate D` block to `SUMMARY.md`, adds `gate` and `r1` to `hosted-release-swap.json`, and returns the gate as the pass. - `hosted-main.ts`: passes the task records to the grade. - `README.md`: documents the gate and the API proxies for per-release hand-off and reach. ## Verification - `npx vitest run --config tests/restart-chaos/vitest.config.ts tests/restart-chaos/hosted` passes (8 files, 56 tests). The new `hosted-gate.test.ts` covers R1 pass and fail, a fair and an unfair queue wait, outage credit, Retry and open tasks, and each gate check. - `npx tsc -p tests/restart-chaos/tsconfig.json --noEmit` is clean. - No hosted run was started for this change. It changes only how a finished run is graded. ## Risks - Low risk. The change is in the test harness only. - Hosted runs that passed before can now fail on R1, a banner, a failed probe, a duplicate, a stop-then-start release or an unloaded release. This is intended. - A follow-up run's queue wait gets no credit on hosted runs, so R1 can be stricter than in local waves when follow-ups queue for a long time. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used in Claude Code with tool use and code execution. ## 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>
166 lines
11 KiB
TypeScript
166 lines
11 KiB
TypeScript
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { afterEach, describe, expect, it } from "vitest";
|
|
import { ChaosApiError } from "./api.js";
|
|
import { HostedApi, requestSamples } from "./hosted-api.js";
|
|
import { parseHostedCommand } from "./hosted-config.js";
|
|
import { gradeHosted } from "./hosted-grade.js";
|
|
import { hostedTarget } from "./hosted-guard.js";
|
|
import { arrivalsDone, finalizeHosted, keepPolling, observedClicks } from "./hosted-main.js";
|
|
import { hostedCompanyRuns, hostedIssues, runEvents, RUN_PAGE, ISSUE_PAGE, EVENT_PAGE } from "./hosted-readers.js";
|
|
import { continuity } from "./hosted-release.js";
|
|
import { cleanupHosted, setupHosted, statePath } from "./hosted-setup.js";
|
|
import type { HostedTaskRecord } from "./hosted-workload.js";
|
|
|
|
type Json = Record<string, any>;
|
|
const dirs: string[] = [];
|
|
afterEach(async () => { for (const dir of dirs.splice(0)) await rm(dir, { recursive: true, force: true }); });
|
|
const tempDir = async () => { const dir = await mkdtemp(path.join(os.tmpdir(), "hosted-main-")); dirs.push(dir); return dir; };
|
|
const host = "release-proof-1.staging.paperclip.app";
|
|
|
|
describe("hosted run control", () => {
|
|
it("ends a fixed batch only when the plan is used up and no task is in flight", () => {
|
|
expect(arrivalsDone(0, 5, 0, [])).toBe(false);
|
|
expect(arrivalsDone(0, 0, 1, [])).toBe(false);
|
|
expect(arrivalsDone(0, 0, 0, [])).toBe(true);
|
|
expect(arrivalsDone(2, 0, 0, [])).toBe(false);
|
|
});
|
|
|
|
it("stops the observer before the grade reads Retry clicks, so a click during the drain fails the task", async () => {
|
|
const dir = await tempDir();
|
|
const record = { issueId: "i1", retryClicks: [] } as unknown as HostedTaskRecord;
|
|
const api = {
|
|
get: async <T>(route: string) => (route.includes("heartbeat-runs") ? [{ id: "r1", agentId: "a1", issueId: "i1", status: "succeeded", createdAt: 1, startedAt: 2, finishedAt: 3 }]
|
|
: route.includes("/issues?") ? [{ id: "i1", title: "a", status: "done", assigneeAgentId: "a1" }] : route === "/api/issues/i1" ? { status: "done" } : []) as T,
|
|
post: async <T>() => ({}) as T,
|
|
};
|
|
let stopped = false;
|
|
const graded = await gradeHosted({ api, companyId: "c1", agentIds: ["a1"], releases: [], health: () => continuity([]), apiContinuity: () => continuity([]),
|
|
stopObserver: async () => { stopped = true; record.retryClicks.push({ at: 0, accepted: true, via: "observer" }); },
|
|
observedClicks: () => observedClicks([record]), lastReleaseAt: Date.now() - 1_000, settleMs: 5, drainMs: 5, dir, cancelled: () => false, record: () => {}, pollMs: 5, tasks: () => [], wantedReleases: 0 });
|
|
expect(stopped).toBe(true);
|
|
expect(graded.headline).toBe("0 / 1 tasks completed hands-off");
|
|
});
|
|
|
|
it("stops background polling promptly and never polls after stop", async () => {
|
|
let polls = 0;
|
|
const polling = keepPolling({ poll: async () => { polls += 1; } }, 60_000);
|
|
await new Promise(resolve => setTimeout(resolve, 20));
|
|
const started = Date.now();
|
|
await polling.stop();
|
|
const after = polls;
|
|
await new Promise(resolve => setTimeout(resolve, 20));
|
|
expect(Date.now() - started).toBeLessThan(1_000);
|
|
expect(polls).toBe(after);
|
|
});
|
|
|
|
it("cleans up what a failed setup already created, from the saved state, keeping a terminated agent that has cost history", async () => {
|
|
const dir = await tempDir();
|
|
const writes: string[] = [];
|
|
let agentStatus = "idle";
|
|
let lifecycleReads = 0;
|
|
const api = {
|
|
get: async <T>(route: string) => {
|
|
if (route.endsWith("/environments")) return [{ id: "e1", driver: "sandbox" }] as T;
|
|
if (route.endsWith("/agents")) return [{ id: "a1", name: "RC rc-x opencode" }] as T;
|
|
if (route === "/api/agents/a1") return { id: "a1", status: agentStatus, lifecycleState: agentStatus === "terminated" && lifecycleReads++ > 0 ? "terminated" : "terminating" } as T;
|
|
if (route.endsWith("/secrets")) return [{ id: "s1", name: "restart-chaos rc-x OPENROUTER_API_KEY" }] as T;
|
|
if (route.endsWith("/projects")) return [{ id: "p1", name: "Restart chaos rc-x" }] as T;
|
|
return [] as T;
|
|
},
|
|
post: async <T>(route: string) => {
|
|
writes.push(`POST ${route}`);
|
|
if (route.endsWith("/secrets")) return { id: "s1" } as T;
|
|
if (route.endsWith("/projects")) return { id: "p1" } as T;
|
|
if (route.endsWith("/terminate")) { agentStatus = "terminated"; return {} as T; }
|
|
throw new Error("agent creation failed");
|
|
},
|
|
patch: async <T>(route: string) => { writes.push(`PATCH ${route}`); return {} as T; },
|
|
delete: async (route: string) => {
|
|
writes.push(`DELETE ${route}`);
|
|
if (route === "/api/agents/a1") throw new ChaosApiError(409, `DELETE ${route} returned 409: {"code":"agent_cost_history_retained"}`);
|
|
},
|
|
};
|
|
const options = parseHostedCommand(["--hosted-base-url", `https://${host}`, "--hosted-allow", host, "--hosted-label", "rc-x"]);
|
|
await expect(setupHosted({ api, options, companyId: "c1", dir, env: { OPENROUTER_API_KEY: "k" }, record: () => {} })).rejects.toThrow(/agent creation failed/);
|
|
expect(JSON.parse(await readFile(statePath(dir), "utf8"))).toMatchObject({ companyId: "c1", projectId: "p1", secrets: [{ id: "s1" }] });
|
|
writes.length = 0;
|
|
expect(await finalizeHosted({ api, keep: false, label: "rc-x", dir, state: null, record: () => {}, terminatePollMs: 1 })).toMatchObject({ errors: [] });
|
|
expect(lifecycleReads).toBeGreaterThan(1);
|
|
expect(writes).toEqual(["PATCH /api/agents/a1", "POST /api/agents/a1/terminate", "DELETE /api/agents/a1", "DELETE /api/secrets/s1", "PATCH /api/projects/p1"]);
|
|
expect(await finalizeHosted({ api, keep: true, label: "rc-x", dir, state: null, record: () => {} })).toBeNull();
|
|
});
|
|
it("collects every page of a run agent's tasks before hiding any, so none is skipped as the list shrinks", async () => {
|
|
const hidden = new Set<string>();
|
|
const tasks = Array.from({ length: ISSUE_PAGE + 7 }, (_, index) => ({ id: `t${index}`, assigneeAgentId: "a1" }));
|
|
const api = {
|
|
get: async <T>(route: string) => {
|
|
if (route.endsWith("/agents")) return [{ id: "a1", name: "RC rc-x opencode", status: "terminated" }] as T;
|
|
if (route.includes("/issues?")) { const offset = Number(/offset=(\d+)/.exec(route)![1]); return tasks.filter(task => !hidden.has(task.id)).slice(offset, offset + ISSUE_PAGE) as T; }
|
|
if (route === "/api/agents/a1") return { id: "a1", status: "terminated" } as T;
|
|
return [] as T;
|
|
},
|
|
post: async <T>() => ({}) as T,
|
|
patch: async <T>(route: string) => { hidden.add(route.split("/").at(-1)!); return {} as T; },
|
|
delete: async () => {},
|
|
};
|
|
const report = await cleanupHosted({ api, label: "rc-x", companyId: "c1", state: null, record: () => {} });
|
|
expect(report).toMatchObject({ tasksArchived: ISSUE_PAGE + 7, errors: [] });
|
|
expect(hidden.size).toBe(ISSUE_PAGE + 7);
|
|
});
|
|
|
|
it("reports a cleanup read that failed instead of counting the object as removed", async () => {
|
|
const writes: string[] = [];
|
|
const unavailable = (route: string) => new ChaosApiError(503, `GET ${route} returned 503`);
|
|
const api = {
|
|
get: async <T>(route: string) => {
|
|
if (route.endsWith("/agents")) return [{ id: "a1", name: "RC rc-x opencode", status: "paused" }] as T;
|
|
if (route === "/api/agents/a1" || route.endsWith("/secrets")) throw unavailable(route);
|
|
return [] as T;
|
|
},
|
|
post: async <T>(route: string) => { writes.push(`POST ${route}`); return {} as T; },
|
|
patch: async <T>(route: string) => { writes.push(`PATCH ${route}`); return {} as T; },
|
|
delete: async (route: string) => { writes.push(`DELETE ${route}`); },
|
|
};
|
|
const report = await cleanupHosted({ api, label: "rc-x", companyId: "c1", state: null, record: () => {} });
|
|
expect(report.agentsRemoved).toBe(0);
|
|
expect(report.errors).toEqual([expect.stringMatching(/^remove agent a1: GET \/api\/agents\/a1 returned 503/), expect.stringMatching(/^read secrets: /)]);
|
|
expect(writes).toEqual(["PATCH /api/agents/a1"]);
|
|
});
|
|
});
|
|
|
|
describe("hosted readers collect complete history", () => {
|
|
it("pages through tasks and run events, and falls back to per-task runs when the company run list is full", async () => {
|
|
const issues = Array.from({ length: ISSUE_PAGE + 3 }, (_, index) => ({ id: `i${index}`, title: "t", status: "done", assigneeAgentId: "a1" }));
|
|
const seen: string[] = [];
|
|
const api = { get: async <T>(route: string) => {
|
|
seen.push(route);
|
|
if (route.includes("/issues?")) { const offset = Number(/offset=(\d+)/.exec(route)![1]); return issues.slice(offset, offset + ISSUE_PAGE) as T; }
|
|
if (route.includes("heartbeat-runs?")) return Array.from({ length: RUN_PAGE }, (_, index) => ({ id: `r${index}`, agentId: "a1", issueId: "i1", status: "succeeded" })) as T;
|
|
if (route === "/api/issues/i0/runs") return [{ runId: "old", agentId: "a1", status: "failed", errorCode: "process_lost" }, { runId: "foreign", agentId: "x", status: "failed" }] as T;
|
|
if (route.startsWith("/api/issues/")) return [] as T;
|
|
if (route.includes("/events?")) { const after = Number(/afterSeq=(\d+)/.exec(route)![1]); return (after < EVENT_PAGE
|
|
? Array.from({ length: EVENT_PAGE }, (_, index) => ({ seq: after + index + 1, createdAt: index })) : [{ seq: after + 1, createdAt: 9 }]) as T; }
|
|
return [] as T;
|
|
} };
|
|
expect(await hostedIssues(api, "c1", ["a1"])).toHaveLength(ISSUE_PAGE + 3);
|
|
const runs = await hostedCompanyRuns(api, "c1", ["a1"]);
|
|
expect(runs).toHaveLength(RUN_PAGE + 1);
|
|
expect(runs.find(run => run.runId === "old")).toMatchObject({ issueId: "i0", status: "failed", errorCode: "process_lost" });
|
|
expect(runs.some(run => run.runId === "foreign")).toBe(false);
|
|
expect(await runEvents(api, "r1")).toHaveLength(EVENT_PAGE + 1);
|
|
});
|
|
|
|
it("counts a refused request as a failure in continuity, not only server errors", async () => {
|
|
const statuses = [200, 401, 404, 429, 503];
|
|
const api = new HostedApi(hostedTarget(`https://${host}`, host), "t", 1_000, (async () => new Response("{}", { status: statuses.shift()! })) as unknown as typeof fetch);
|
|
await api.get("/api/health");
|
|
await api.get("/api/health").catch(() => undefined);
|
|
expect(continuity(requestSamples(api.requests)).failures).toBeGreaterThanOrEqual(1);
|
|
expect(api.requests[1]).toMatchObject({ ok: false, reachable: true, status: 401 });
|
|
await api.get("/api/companies/c1/assigned-work-invariant").catch(() => undefined);
|
|
expect(api.requests[2]).toMatchObject({ ok: true, status: 404 });
|
|
});
|
|
});
|