Files
PaperClipAI/scripts/scale-ramp/fake-paperclip.ts
T
DottaandClaude Opus 5.5 328a954cc1 test(scaling): add a real-agent ramp harness for hosted instances (#21695)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The control plane must run 1,000 agents at once on one workspace
(`doc/plans/2026-11-13-thousand-concurrent-agents.md`)
> - Above about 50 agents, the evidence is a load model only. Nobody has
run 200 or 1,000 real agents
> - Phase 3 of the plan proves the finish line with real runs on hosted
instances, in a fixed order
> - This pull request adds the harness for those runs. It does not run
it against any real instance
> - The benefit is that each proof step is one command with a written
cost estimate, explicit pass/fail criteria, and a guaranteed cleanup

## Linked Issues or Issue Description

**What problem does this solve?**
The 1,000-agent plan needs real runs at 50, 200, 500 and 1,000 agents.
No tool exists that creates real agents and tasks on a hosted instance,
ramps concurrency, measures the control plane, and judges the plan's
finish line.

**Proposed solution**
A script under `scripts/` that ramps real agents through the product
API, stops at the first failed criterion, writes a machine-readable
result, and always cleans up what it created.

**Alternatives considered**
Extend the restart-chaos hosted mode. That mode grades release swaps at
up to 30 concurrent tasks. The ramp reuses its target guard and its
cleanup order, but it has a different workload, different measures and a
different stop rule.

## What Changed

- `scripts/scale-agents-ramp.ts` and `scripts/scale-ramp/`: the harness.
It reads the base URL from a flag or the environment and the credentials
from the environment only. It creates or reuses a company, then creates
agents, tasks, a project and an optional provider secret. It ramps
concurrency in steps (default 10, 50, 100, 200, 500, 1,000) and keeps
each agent on one small deterministic task at a time.
- It measures per step: tasks started, finished, failed and needing
Retry; failed runs; peak running agents; queue wait; time to first token
and first activity (from sampled run events); run duration; board GET
latency. An optional metrics endpoint supplies event-loop, statement and
pool figures. A missing source is reported as "not measured", never as
zero.
- It evaluates the criteria from the plan's finish line plus harness
criteria, writes them into `result.json` and `SUMMARY.md`, and stops at
the first failure.
- Cleanup runs in a `finally`: pause agents, cancel live runs, archive
tasks, terminate and delete agents, delete the stored secret, archive
the project and the created company, and wait for sandbox leases.
`--cleanup-only <run id>` finishes a cleanup after a crash. `--dry-run`
prints the plan and the estimate and makes no request.
- Target guard: `--target-kind` is required and must match the host.
Shared staging and production hosts need a second explicit flag. The
script asks for the host name before it starts unless `--yes` is given.
A run above the spend threshold needs `--confirm-spend <max USD>`.
- `scripts/scale-ramp/scale-ramp.test.ts`: 28 tests against an
in-process fake API with a virtual clock. They cover ramp logic, stop on
failure, criteria evaluation, cleanup after a mid-run crash and an
interrupt, `--cleanup-only` with and without a state file, dry run, the
production-host guard, the spend gate, and that credentials never appear
in output or files.
- `scripts/scale-ramp/tsconfig.json` is added to the test programs that
CI typechecks. The test file joins the `test:release-registry` node test
list, which CI runs.
- `doc/plans/2026-11-13-thousand-concurrent-agents.md`: a Phase 3
runbook with commands, cost, criteria, proof order, target prerequisites
and open risks.

## Verification

- `node scripts/ci-profile/node-test.mjs
scripts/scale-ramp/scale-ramp.test.ts
scripts/typecheck-test-programs.test.mjs`: 47 tests pass.
- `node node_modules/typescript/bin/tsc -p
scripts/scale-ramp/tsconfig.json`: no errors.
- `node cli/node_modules/tsx/dist/cli.mjs scripts/scale-agents-ramp.ts
--base-url https://scale1.test.paperclip.app --target-kind test-stack
--steps 50 --hold-minutes 30 --dry-run` prints the plan, the estimate
and the criteria, and makes no request.
- The harness was not run against any real instance. That needs the
owner's go-ahead.

## Risks

- Low risk for the product: the change adds a script, tests and docs
only. No server code changes.
- A real run creates real agents, tasks and sandboxes and spends money.
The guard, the typed confirmation, the spend gate, the task cap and the
recorded-spend stop limit this. The runbook lists the open risks.
- The cost defaults are assumptions until a first real run records
actual spend.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, via Claude Code with
tool use (shell, file edits).

## 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-14 20:10:51 -06:00

309 lines
13 KiB
TypeScript

import type { Clock, Fetcher } from "./client.ts";
type Json = Record<string, any>;
export class VirtualClock implements Clock {
t = Date.parse("2026-11-14T10:00:00.000Z");
now(): number {
return this.t;
}
async sleep(ms: number): Promise<void> {
this.t += ms;
await Promise.resolve();
}
}
export interface FakeConfig {
token: string;
origin: string;
cloudManaged: boolean;
startDelayMs: number;
runDurationMs: number;
capacity: number;
failWhenRunningAbove: number;
retryOnFailure: boolean;
costPerRunUsd: number;
boardLatencyMs: (running: number) => number;
failRequest: (method: string, path: string, count: number) => { status: number; body: string } | null;
metricsUrl: string | null;
metrics: (running: number) => Record<string, unknown> | null;
}
interface FakeRun {
id: string;
agentId: string;
issueId: string;
status: string;
createdAt: number;
startedAt: number | null;
finishedAt: number | null;
willFail: boolean;
}
export interface RequestLog {
method: string;
url: string;
authorization: string | null;
body: string | null;
}
const json = (status: number, body: unknown) => new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } });
export class FakePaperclip {
readonly config: FakeConfig;
readonly requests: RequestLog[] = [];
readonly companies = new Map<string, Json>();
readonly agents = new Map<string, Json>();
readonly issues = new Map<string, Json>();
readonly runs = new Map<string, FakeRun>();
readonly secrets = new Map<string, Json>();
readonly projects = new Map<string, Json>();
readonly memberCompanyIds: string[] = [];
readonly environmentId = "00000000-0000-4000-8000-0000000000e1";
private seq = 0;
private counts = new Map<string, number>();
readonly clock: VirtualClock;
constructor(clock: VirtualClock, config: Partial<FakeConfig> = {}) {
this.clock = clock;
this.config = {
token: "board-token-value",
origin: "https://scale1.test.paperclip.app",
cloudManaged: false,
startDelayMs: 2_000,
runDurationMs: 60_000,
capacity: Number.POSITIVE_INFINITY,
failWhenRunningAbove: Number.POSITIVE_INFINITY,
retryOnFailure: false,
costPerRunUsd: 0.01,
boardLatencyMs: () => 40,
failRequest: () => null,
metricsUrl: null,
metrics: () => null,
...config,
};
}
private id(): string {
this.seq += 1;
return `00000000-0000-4000-8000-${this.seq.toString(16).padStart(12, "0")}`;
}
addMemberCompany(name: string): string {
const id = this.id();
this.companies.set(id, { id, name, status: "active" });
this.memberCompanyIds.push(id);
return id;
}
addNoiseRuns(count: number): void {
for (let index = 0; index < count; index += 1) {
const id = this.id();
this.runs.set(id, { id, agentId: "noise-agent", issueId: "noise-issue", status: "succeeded", createdAt: this.clock.now() + 1e10 + index,
startedAt: null, finishedAt: null, willFail: false });
}
}
running(): number {
return [...this.runs.values()].filter(run => run.status === "running").length;
}
liveRuns(): FakeRun[] {
return [...this.runs.values()].filter(run => ["queued", "running"].includes(run.status));
}
advance(): void {
const now = this.clock.now();
for (const run of [...this.runs.values()].sort((a, b) => a.createdAt - b.createdAt)) {
if (run.status === "running" && run.startedAt !== null && now >= run.startedAt + this.config.runDurationMs) {
run.finishedAt = run.startedAt + this.config.runDurationMs;
const issue = this.issues.get(run.issueId)!;
if (run.willFail) {
run.status = "failed";
issue.interruption = this.config.retryOnFailure ? { state: "failed", canRetry: true, retryWait: false } : null;
} else {
run.status = "succeeded";
issue.status = "done";
}
}
}
for (const run of [...this.runs.values()].sort((a, b) => a.createdAt - b.createdAt)) {
if (run.status !== "queued" || now < run.createdAt + this.config.startDelayMs) continue;
if (this.running() >= this.config.capacity) break;
run.status = "running";
run.startedAt = Math.max(run.createdAt + this.config.startDelayMs, now - 1_000);
run.willFail = this.running() > this.config.failWhenRunningAbove;
this.issues.get(run.issueId)!.status = "in_progress";
}
}
private runRow(run: FakeRun): Json {
const iso = (value: number | null) => value === null ? null : new Date(value).toISOString();
return {
id: run.id, agentId: run.agentId, issueId: run.issueId, status: run.status, createdAt: iso(run.createdAt), startedAt: iso(run.startedAt),
finishedAt: iso(run.finishedAt), costUsd: run.status === "succeeded" || run.status === "failed" ? this.config.costPerRunUsd : null,
};
}
readonly fetch: Fetcher = async (url, init) => {
const method = String(init.method ?? "GET");
const headers = (init.headers ?? {}) as Record<string, string>;
const body = typeof init.body === "string" ? init.body : null;
this.requests.push({ method, url, authorization: headers.authorization ?? null, body });
if (this.config.metricsUrl && url === this.config.metricsUrl) {
this.advance();
const sample = this.config.metrics(this.running());
return sample ? json(200, sample) : json(503, { error: "unavailable" });
}
const parsed = new URL(url);
if (parsed.origin !== this.config.origin) throw new Error(`fake: request outside ${this.config.origin}: ${parsed.origin}`);
if (headers.authorization !== `Bearer ${this.config.token}`) return json(401, { error: "unauthorized" });
const route = parsed.pathname;
const key = `${method} ${route.replace(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/g, ":id")}`;
const count = (this.counts.get(key) ?? 0) + 1;
this.counts.set(key, count);
const injected = this.config.failRequest(method, key.slice(method.length + 1), count);
if (injected) return new Response(injected.body, { status: injected.status });
this.advance();
return this.route(method, route, parsed.searchParams, body ? JSON.parse(body) as Json : {});
};
private route(method: string, route: string, query: URLSearchParams, body: Json): Response {
const parts = route.split("/").filter(Boolean).slice(1);
const now = this.clock.now();
const [a, b, c] = parts;
if (method === "GET" && a === "cli-auth" && b === "me") return json(200, { companyIds: this.memberCompanyIds });
if (a === "companies" && !b) {
if (method === "GET") return json(200, [...this.companies.values()]);
if (this.config.cloudManaged) return json(403, { error: "Company creation is managed by Paperclip Cloud", code: "cloud_managed" });
const id = this.id();
this.companies.set(id, { id, name: body.name, status: "active" });
this.memberCompanyIds.push(id);
return json(201, this.companies.get(id));
}
if (a === "companies" && b) {
const companyId = b;
if (!this.companies.has(companyId)) return json(404, { error: "Company not found" });
if (!c && method === "GET") return json(200, this.companies.get(companyId));
if (c === "archive" && method === "POST") {
this.companies.get(companyId)!.status = "archived";
return json(200, this.companies.get(companyId));
}
if (c === "environments") return json(200, [{ id: this.environmentId, driver: "sandbox", status: "active" }]);
if (c === "secrets") {
if (method === "GET") return json(200, [...this.secrets.values()].filter(secret => secret.companyId === companyId).map(({ value: _value, ...rest }) => rest));
const id = this.id();
this.secrets.set(id, { id, companyId, name: body.name, value: body.value });
return json(201, { id, name: body.name });
}
if (c === "projects") {
if (method === "GET") return json(200, [...this.projects.values()].filter(project => project.companyId === companyId));
const id = this.id();
this.projects.set(id, { id, companyId, name: body.name, archivedAt: null });
return json(201, this.projects.get(id));
}
if (c === "agents") {
if (method === "GET") return json(200, [...this.agents.values()].filter(agent => agent.companyId === companyId));
const id = this.id();
this.agents.set(id, { id, companyId, name: body.name, status: "idle", lifecycleState: null, payload: body });
return json(201, this.agents.get(id));
}
if (c === "issues") {
if (method === "POST") {
const id = this.id();
this.issues.set(id, { id, companyId, title: body.title, description: body.description, status: "todo", assigneeAgentId: body.assigneeAgentId,
projectId: body.projectId ?? null, hiddenAt: null, interruption: null });
const agent = this.agents.get(body.assigneeAgentId);
if (agent && agent.status !== "paused" && agent.status !== "terminated") {
const runId = this.id();
this.runs.set(runId, { id: runId, agentId: body.assigneeAgentId, issueId: id, status: "queued", createdAt: now, startedAt: null, finishedAt: null, willFail: false });
}
return json(201, this.issues.get(id));
}
let list = [...this.issues.values()].filter(issue => issue.companyId === companyId);
const ids = query.get("ids");
if (ids) list = list.filter(issue => ids.split(",").includes(issue.id));
const assignee = query.get("assigneeAgentId");
if (assignee) list = list.filter(issue => issue.assigneeAgentId === assignee);
return json(200, list.slice(0, Number(query.get("limit") ?? 500)));
}
if (c === "heartbeat-runs") {
let list = [...this.runs.values()].sort((x, y) => y.createdAt - x.createdAt);
const agentId = query.get("agentId");
if (agentId) list = list.filter(run => run.agentId === agentId);
return json(200, list.slice(0, Math.min(1_000, Number(query.get("limit") ?? 200))).map(run => this.runRow(run)));
}
if (["dashboard", "live-runs", "sidebar-badges"].includes(c ?? "")) {
this.clock.t += this.config.boardLatencyMs(this.running());
return json(200, c === "live-runs" ? this.liveRuns().map(run => this.runRow(run)) : {});
}
}
if (a === "agents" && b) {
const agent = this.agents.get(b);
if (!agent) return json(404, { error: "Agent not found" });
if (method === "PATCH") {
Object.assign(agent, body);
return json(200, agent);
}
if (method === "POST" && c === "terminate") {
agent.status = "terminated";
agent.lifecycleState = "terminated";
return json(200, agent);
}
if (method === "DELETE") {
this.agents.delete(b);
return json(200, { ok: true });
}
}
if (a === "issues" && b) {
const issue = this.issues.get(b);
if (!issue) return json(404, { error: "Issue not found" });
if (method === "GET" && !c) return json(200, issue);
if (method === "GET" && c === "runs") return json(200, [...this.runs.values()].filter(run => run.issueId === b).map(run => ({ ...this.runRow(run), runId: run.id })));
if (method === "PATCH") {
Object.assign(issue, body);
return json(200, issue);
}
}
if (a === "heartbeat-runs" && b) {
const run = this.runs.get(b);
if (!run) return json(404, { error: "Run not found" });
if (method === "POST" && c === "cancel") {
if (["queued", "running"].includes(run.status)) {
run.status = "cancelled";
run.finishedAt = now;
}
return json(200, this.runRow(run));
}
if (method === "GET" && c === "events") {
if (Number(query.get("afterSeq") ?? 0) > 0 || run.startedAt === null) return json(200, []);
const delay = run.startedAt - run.createdAt;
return json(200, [
{ seq: 1, eventType: "run.performance.span", createdAt: new Date(run.startedAt).toISOString(), payload: { span: "heartbeat.queue", startOffsetMs: 0, durationMs: delay } },
{ seq: 2, eventType: "run.performance.span", createdAt: new Date(run.startedAt + 3_000).toISOString(),
payload: { span: "task.time_to_first_agent_activity", startOffsetMs: 0, durationMs: delay + 3_000 } },
{ seq: 3, eventType: "run.performance.span", createdAt: new Date(run.startedAt + 5_000).toISOString(),
payload: { span: "task.time_to_first_token", startOffsetMs: 0, durationMs: delay + 5_000 } },
]);
}
}
if (a === "secrets" && b && method === "DELETE") {
this.secrets.delete(b);
return json(200, { ok: true });
}
if (a === "projects" && b && method === "PATCH") {
const project = this.projects.get(b);
if (!project) return json(404, { error: "Project not found" });
Object.assign(project, body);
return json(200, project);
}
if (a === "environments" && b === this.environmentId && c === "leases") {
return json(200, this.liveRuns().map(run => ({ id: run.id, issueId: run.issueId, status: "active" })));
}
return json(404, { error: `fake: no route for ${method} ${route}` });
}
}