Files
PaperClipAI/tests/runner-e2e/chat-campaign-coordinator.ts
T
f4337bb5fb test(evals): land the chat catalog eval harness on master (#19614)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The chat-feature evals measure whether agents complete real chat
requests through the product
> - The chat catalog eval harness and its repairs live on the
`chat-eval/master-candidate` branch, not on master
> - Today's work made 139 of 242 chat evals pass in verified single-case
live runs. Each product fix landed on master as its own pull request
> - The eval-side changes (setups, graders, fault seams, evidence
capture) still exist only on the candidate
> - This pull request lands the harness on master. Master stays the
source of truth for product behavior
> - The benefit is one tree: evals run against master commits, and
product fixes reach the evals as soon as they merge

## Linked Issues or Issue Description

Refs #19441, Refs #19599, Refs #20270, Refs #20334, Refs #20342, Refs
#20344, Refs #20354, Refs #20360, Refs #20373, Refs #20382, Refs #20409

**What existing behavior does this improve?**
The chat catalog eval harness and the product it evaluates live on a
long-running branch outside master. Eval fixes do not reach master, so
master-based eval runs use old setups and graders.

**Subsystem affected**
`tests/runner-e2e` chat catalog harness, plus a small set of product
changes that exist only on the candidate.

**Proposed change**
Land the harness on master. Every product fix from this eval work
already merged to master as its own pull request. Conflicts keep
master's product behavior.

## What Changed

- Most of the diff (808 of 851 files) is under `tests/runner-e2e/`:
chat-catalog domains, scenarios, descriptors, the TestOps manifest,
private evidence tooling, and their tests.
- Graders now check saved state, setup literals and rendered or clicked
links. Many keyword and regex checks on free-form replies are removed.
Each changed grader has a bumped `oracleVersion` and calibration tests
(a valid reply, a reworded valid reply, and broken variants that must
fail).
- The shared link helper `hasLink` no longer treats a list link (for
example `/COM/issues?view=mine`) as a link to every task without a URL
key.
- Setup fixes build real state through supported product routes: routine
history, agent grants through the board grant route, budget holds with a
real cap, and a storage-layer archive fault for
`inbox-search.archive-partial-failure`.
- Browser boundary journeys (sign-in, chat-disabled, onboarding) now
write the standard pass evidence files.
- Retargeted cases, approved by the maintainer:
`agent-chat.chat-disabled` (Conference Room gate),
`agents.restore-instructions` (no saved instruction history),
`access.manage-membership` (member permission edits stay board-only).
- The private evidence trusted files (`manual-private-handoff.ts`,
`manual-private-retention.ts`, `manual-private-report-assets.ts`) are
byte-identical to master.
- Product files outside `tests/` (43 files, +1,328 −49) carry
candidate-only changes that master does not yet have. Most are tests.
The main code changes are in `request-human-input.ts`,
`heartbeat-run-summary.ts`, `native-completion-feedback.ts`,
`native-run-finalizer.ts`, `openapi.ts`, `secrets.ts` and
`IssueChatLongThreadPerf.tsx`.

## Verification

- `pnpm vitest run chat-catalog-domains chat-catalog-contract.test.ts
chat-catalog-consistency.test.ts unit-launcher.test.mjs
--testTimeout=180000` in `tests/runner-e2e`: 269 files pass, 3,550 tests
pass.
- `node --test chat-full-catalog-testops-manifest.test.mjs`: 10 pass.
- `node tests/runner-e2e/source-sdk-public-inputs.mjs <HEAD> --check`:
no change.
- `tsc` for `server` and `tests/runner-e2e`: clean.
- Live single-case verification: 139 of 242 cases passed on paid runs,
and a person read the saved evidence for each. These are single-case
runs, not one full-cohort run.

## Risks

- Large diff. Most of it is test-only, but the 43 non-test files change
product code paths. Review those files first.
- Some graders are now less strict on reply wording. A person must read
replies for meaning on those cases.
- 42 cases need disposable outside accounts (Notion, Slack, GitHub,
Daytona and others) and stay blocked. `access.invite-teammate` stays
failing until local agent isolation lands, because the runner's durable
state keeps a live invite token.
- The paid workflow compares the trusted private-evidence files with
master. They are unchanged, so this risk is low.

## Model Used

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

## 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>
Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-11-10 08:02:11 -06:00

281 lines
19 KiB
TypeScript

import { createHash, randomUUID } from "node:crypto";
import { closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, realpathSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
import path from "node:path";
import { campaignProductIdentityEligible, completeCampaignAttempt, reserveCampaignAttempt, type CampaignAttempt, type ChatCampaign } from "./chat-campaign.js";
type Completion = Parameters<typeof completeCampaignAttempt>[1];
export type CoordinatorStopReason = "cleanup_failure" | "secret_leak" | "shared_infrastructure" | "coordinator_failure";
export interface CampaignQualificationReview {
evidencePath: string; receiptSha256: string;
previous: { definitionDigest: string; graderDigest: string };
next: { definitionDigest: string; graderDigest: string };
}
export interface ReviewedCampaignSelection { scenarioId: string; attempt: CampaignAttempt }
export interface CampaignLaunchPermit {
reservationId: string; scenarioId: string; ordinal: 1 | 2; executionId: string;
campaignId: string; args: readonly string[]; maxParallel: 1; automaticRetries: 0;
}
interface Entry {
permit: CampaignLaunchPermit; reservationDigest: string; reservedAttempt: CampaignAttempt;
phase: "reserved" | "launch_uncertain" | "completed";
completionDigest?: string;
}
interface State {
version: 1; selectionDigest: string; entries: Record<string, Entry>;
stop: { reason: CoordinatorStopReason; detail: string } | null;
failures: Array<{ scenarioId: string; phase: "reserve" | "launch" | "completion"; evidencePath: string; summary: string }>;
}
interface Pending { before: string; after: string; manifest: string; state: State }
export interface CampaignCoordinatorOptions {
manifestPath: string; controlDirectory: string; batchId: string;
/** Defaults to the repository root above tests/runner-e2e. */
evidenceRoot?: string;
selections: readonly ReviewedCampaignSelection[];
/** Controlled filesystem fault points for offline crash tests only. */
now?: () => string;
fault?: (stage: "intent" | "manifest" | "state") => void;
}
const hash = (value: string) => createHash("sha256").update(value).digest("hex");
const json = (value: unknown) => `${JSON.stringify(value, null, 2)}\n`;
function reviewedIdentity(selections: readonly ReviewedCampaignSelection[]) {
return selections.map(({ scenarioId, attempt }) => ({ scenarioId, attempt: { ...attempt, startedAt: null } }));
}
function read<T>(filename: string): T { return JSON.parse(readFileSync(filename, "utf8")); }
function atomicWrite(filename: string, bytes: string) {
const temporary = `${filename}.${randomUUID()}.tmp`;
const fd = openSync(temporary, "wx", 0o600);
try { writeFileSync(fd, bytes); fsyncSync(fd); } finally { closeSync(fd); }
renameSync(temporary, filename);
const directory = openSync(path.dirname(filename), "r");
try { fsyncSync(directory); } finally { closeSync(directory); }
}
/** One ledger owner, two independent one-case launchers. Never steals a stale lock.
* A crashed owner requires independent process/resource reconciliation before an
* operator removes its lock. Restart never replays a launch, even if no PID was saved.
* Reconciliation/manual ledger writers must remain paused for this owner's lifetime.
*/
export class ChatCampaignCoordinator {
private tail: Promise<unknown> = Promise.resolve();
private readonly plans: ReviewedCampaignSelection[];
private readonly statePath: string;
private readonly intentPath: string;
private readonly lockPath: string;
private readonly owner = randomUUID();
private poisoned = false;
private closed = false;
private inFlight = 0;
private constructor(private readonly options: CampaignCoordinatorOptions) {
this.plans = structuredClone([...options.selections]);
this.statePath = path.join(options.controlDirectory, "state.json");
this.intentPath = path.join(options.controlDirectory, "pending.json");
// Lock belongs to the manifest, not a caller-chosen batch directory.
this.lockPath = `${options.manifestPath}.coordinator-lock`;
}
static open(options: CampaignCoordinatorOptions) {
if (!/^[a-zA-Z0-9_-]{1,100}$/.test(options.batchId) || !options.selections.length
|| new Set(options.selections.map(x => x.scenarioId)).size !== options.selections.length)
throw new Error("Invalid reviewed campaign selection");
for (const { scenarioId, attempt } of options.selections) {
if (!/^[a-z0-9-]+\.[a-z0-9-]+$/.test(scenarioId) || attempt.environment !== "local"
|| attempt.automaticRetries !== 0 || attempt.outcome !== "running" || !attempt.productIdentity
|| attempt.productIdentity.kind !== "source-build" || !attempt.databaseLane)
throw new Error("Two-lane coordinator requires reviewed local source-build provenance");
}
mkdirSync(options.controlDirectory, { recursive: true, mode: 0o700 });
options = { ...options, manifestPath: realpathSync(options.manifestPath), controlDirectory: realpathSync(options.controlDirectory) };
const coordinator = new ChatCampaignCoordinator(options);
const fd = openSync(coordinator.lockPath, "wx", 0o600);
try { writeFileSync(fd, json({ version: 1, owner: coordinator.owner, pid: process.pid,
batchId: options.batchId, controlDirectory: path.resolve(options.controlDirectory), createdAt: new Date().toISOString() })); fsyncSync(fd); }
finally { closeSync(fd); }
try {
const selectionDigest = hash(json({ batchId: options.batchId, manifestPath: path.resolve(options.manifestPath), selections: reviewedIdentity(coordinator.plans) }));
if (!existsSync(coordinator.statePath)) atomicWrite(coordinator.statePath, json({ version: 1, selectionDigest, entries: {}, stop: null, failures: [] } satisfies State));
const state = read<State>(coordinator.statePath);
if (state.version !== 1 || state.selectionDigest !== selectionDigest) throw new Error("Coordinator selection changed");
coordinator.recover();
return coordinator;
} catch (error) {
// Retain ownership on recovery failure: no second writer may guess what happened.
coordinator.poisoned = true;
throw error;
}
}
private assertOwner() {
if (this.closed || this.poisoned || read<{ owner: string }>(this.lockPath).owner !== this.owner)
throw new Error("Coordinator ownership unavailable");
}
private serial<T>(operation: () => T): Promise<T> {
const result = this.tail.then(() => { this.assertOwner(); return operation(); });
this.tail = result.catch(() => undefined);
return result;
}
private recover() {
let state = read<State>(this.statePath);
if (existsSync(this.intentPath)) {
const pending = read<Pending>(this.intentPath), actual = hash(readFileSync(this.options.manifestPath, "utf8"));
if (actual !== pending.before && actual !== pending.after) throw new Error("Unrecognized manifest after interrupted transaction");
// Never replay an uncommitted reservation or completion on restart. If the
// manifest commit landed, recover its corresponding state, then stop.
if (actual === pending.after) state = pending.state;
state.stop ??= { reason: "coordinator_failure", detail: "Interrupted ledger transaction; independent reconciliation required" };
atomicWrite(this.statePath, json(state)); unlinkSync(this.intentPath);
}
if (Object.values(state.entries).some(x => x.phase !== "completed")) {
state.stop ??= { reason: "coordinator_failure", detail: "Reserved or possibly launched execution; no automatic recovery launch" };
atomicWrite(this.statePath, json(state));
}
}
private commit(manifestBefore: string, manifest: ChatCampaign, state: State) {
const bytes = json(manifest);
try {
atomicWrite(this.intentPath, json({ before: hash(manifestBefore), after: hash(bytes), manifest: bytes, state } satisfies Pending));
this.options.fault?.("intent");
if (hash(readFileSync(this.options.manifestPath, "utf8")) !== hash(manifestBefore)) throw new Error("Manifest changed outside coordinator");
atomicWrite(this.options.manifestPath, bytes); this.options.fault?.("manifest");
atomicWrite(this.statePath, json(state)); this.options.fault?.("state");
unlinkSync(this.intentPath);
} catch (error) { this.poisoned = true; throw error; }
}
reserve(scenarioId: string): Promise<CampaignLaunchPermit> {
return this.serial(() => {
const state = read<State>(this.statePath), plan = this.plans.find(x => x.scenarioId === scenarioId);
if (!plan) throw new Error("Scenario not reviewed");
if (state.stop) throw new Error("Admissions stopped");
const reservationId = hash(json({ batchId: this.options.batchId, scenarioId, ordinal: plan.attempt.ordinal }));
const reservationDigest = hash(json(reviewedIdentity([plan])));
if (state.entries[reservationId]) {
if (state.entries[reservationId]!.reservationDigest !== reservationDigest) throw new Error("Reservation identity conflict");
return structuredClone(state.entries[reservationId]!.permit);
}
if (Object.values(state.entries).filter(x => x.phase !== "completed").length >= 2) throw new Error("Two lanes already reserved");
const before = readFileSync(this.options.manifestPath, "utf8"), manifest = JSON.parse(before) as ChatCampaign;
const row = manifest.scenarios.find(x => x.id === scenarioId);
if (!row || row.executionId !== plan.attempt.executionId || row.qualifyingDefinitionDigest !== plan.attempt.definitionDigest
|| row.qualifyingGraderDigest !== plan.attempt.graderDigest
|| !campaignProductIdentityEligible(row, plan.attempt, manifest.baselineRevision)) throw new Error("Reviewed selection differs from authoritative ledger");
const reservedAttempt = { ...structuredClone(plan.attempt), startedAt: this.options.now?.() ?? new Date().toISOString() };
if (!Number.isFinite(Date.parse(reservedAttempt.startedAt))) throw new Error("Invalid reservation clock");
reserveCampaignAttempt(row, reservedAttempt);
const permit: CampaignLaunchPermit = { reservationId, scenarioId, ordinal: plan.attempt.ordinal, executionId: plan.attempt.executionId,
campaignId: `${this.options.batchId}-${reservationId.slice(0, 20)}`, maxParallel: 1, automaticRetries: 0,
args: ["--id", plan.attempt.executionId, "--max-parallel", "1", "--max-automatic-retries", "0"] };
state.entries[reservationId] = { permit, reservationDigest, reservedAttempt: structuredClone(reservedAttempt), phase: "reserved" };
this.commit(before, manifest, state);
return structuredClone(permit);
});
}
async launchOnce<T>(permit: CampaignLaunchPermit, launchOnly: (permit: CampaignLaunchPermit) => Promise<T>): Promise<T> {
await this.serial(() => {
const state = read<State>(this.statePath), entry = state.entries[permit.reservationId];
if (state.stop || !entry || entry.phase !== "reserved" || json(entry.permit) !== json(permit)) throw new Error("Launch is not admissible");
// Persist uncertainty BEFORE calling spawn. A crash in this gap consumes
// the permit; it can never be replayed based on absent PID alone.
entry.phase = "launch_uncertain"; atomicWrite(this.statePath, json(state)); this.inFlight++;
});
try { return await launchOnly(structuredClone(permit)); }
finally { this.inFlight--; }
}
complete(permit: CampaignLaunchPermit, completion: Completion, stop?: { reason: CoordinatorStopReason; detail: string }, qualificationReview?: CampaignQualificationReview): Promise<void> {
return this.serial(() => {
const state = read<State>(this.statePath), entry = state.entries[permit.reservationId];
if (!entry || json(entry.permit) !== json(permit)) throw new Error("Completion identity conflict");
const completionDigest = hash(json({ completion, stop: stop ?? null, qualificationReview: qualificationReview ?? null }));
if (entry.phase === "completed") {
if (entry.completionDigest !== completionDigest) throw new Error("Completion content conflict");
const current = read<ChatCampaign>(this.options.manifestPath).scenarios.find(x => x.id === permit.scenarioId)?.attempts.at(-1);
if (!current || current.ordinal !== permit.ordinal || current.executionId !== permit.executionId
|| Object.entries(completion).some(([key, value]) => json(current[key as keyof CampaignAttempt]) !== json(value)))
throw new Error("Completed ledger differs from receipt");
return;
}
if (entry.phase !== "launch_uncertain") throw new Error("No admitted launcher for completion");
const before = readFileSync(this.options.manifestPath, "utf8"), manifest = JSON.parse(before) as ChatCampaign;
const row = manifest.scenarios.find(x => x.id === permit.scenarioId), plan = this.plans.find(x => x.scenarioId === permit.scenarioId)!;
const actual = row?.attempts.at(-1);
if (!row || !actual || actual.ordinal !== permit.ordinal || json(actual) !== json(entry.reservedAttempt)) throw new Error("Reserved attempt changed");
completeCampaignAttempt(row, structuredClone(completion));
if (qualificationReview) {
const review = qualificationReview;
if (completion.outcome !== "passed" || !completion.evidencePaths.includes(review.evidencePath)
|| !review.evidencePath.startsWith("tests/runner-e2e/results/") || review.evidencePath.split("/").includes("..")
|| review.previous.definitionDigest !== plan.attempt.definitionDigest || review.previous.graderDigest !== plan.attempt.graderDigest
|| json(review.previous) === json(review.next) || !review.next.definitionDigest || !review.next.graderDigest)
throw new Error("Invalid qualifying identity review");
const bytes = readFileSync(path.join(this.options.evidenceRoot ?? path.resolve(path.dirname(this.options.manifestPath), "../.."), review.evidencePath), "utf8");
const retained = JSON.parse(bytes);
if (hash(bytes) !== review.receiptSha256 || retained.id !== permit.scenarioId || retained.ordinal !== permit.ordinal
|| json(retained.previous) !== json(review.previous) || json(retained.next) !== json(review.next))
throw new Error("Qualifying identity review receipt mismatch");
row.qualifyingDefinitionDigest = null; row.qualifyingGraderDigest = null;
}
state.stop ??= stop ?? (completion.cleanup !== "passed" ? { reason: "cleanup_failure", detail: "Measured cleanup did not pass" } : null);
// Stop is durable before the completion/summary commit or any lane refill.
if (state.stop) atomicWrite(this.statePath, json(state));
entry.phase = "completed"; entry.completionDigest = completionDigest;
this.commit(before, manifest, state);
});
}
stop(reason: CoordinatorStopReason, detail: string): Promise<void> {
return this.serial(() => { const state = read<State>(this.statePath); state.stop ??= { reason, detail }; atomicWrite(this.statePath, json(state)); });
}
snapshot(): Promise<Readonly<State>> { return this.serial(() => read<State>(this.statePath)); }
/** Finite waves avoid refilling while an already settled peer awaits grading.
* readCompletion owns authentic result/usage and normalized selected-closure
* fingerprint validation (not unrelated raw descriptor/manifest bytes). It must
* distinguish actual secret matches from safe, bound-only ZIP omissions.
*/
async run<T>(input: {
launchOnly: (permit: CampaignLaunchPermit) => Promise<T>;
/** Parent redacts and retains the real failure privately before returning a safe reference. */
retainFailure: (permit: CampaignLaunchPermit | null, error: unknown, phase: "reserve" | "launch" | "completion") => Promise<{ evidencePath: string; summary: string }>;
readCompletion: (permit: CampaignLaunchPermit, result: T) => Promise<{ completion: Completion; stop?: { reason: CoordinatorStopReason; detail: string }; qualificationReview?: CampaignQualificationReview }>;
}) {
const unretained: unknown[] = [];
const existing = await this.snapshot();
const remaining = this.plans.filter(plan => !Object.values(existing.entries).some(entry => entry.permit.scenarioId === plan.scenarioId && entry.phase === "completed"));
for (let index = 0; index < remaining.length; index += 2) {
if ((await this.snapshot()).stop) break;
const settled = await Promise.allSettled(remaining.slice(index, index + 2).map(async plan => {
let permit: CampaignLaunchPermit | null = null;
let phase: "reserve" | "launch" | "completion" = "reserve";
try {
permit = await this.reserve(plan.scenarioId); phase = "launch";
const result = await this.launchOnce(permit, input.launchOnly); phase = "completion";
const observed = await input.readCompletion(permit, result);
await this.complete(permit, observed.completion, observed.stop, observed.qualificationReview);
} catch (error) {
try {
const retained = await input.retainFailure(permit, error, phase);
if (!retained.evidencePath.startsWith("tests/runner-e2e/results/") || retained.evidencePath.split("/").includes("..") || !retained.summary.trim())
throw new Error("Failure requires a sanitized retained evidence reference");
if (!this.poisoned) await this.serial(() => {
const state = read<State>(this.statePath);
state.failures.push({ scenarioId: plan.scenarioId, phase, ...retained });
state.stop ??= { reason: "coordinator_failure", detail: `${retained.summary} (${retained.evidencePath})` };
atomicWrite(this.statePath, json(state));
});
else unretained.push(error); // WAL/lock remain authoritative; expose original error to owner.
} catch (retentionError) {
unretained.push(error, retentionError);
if (!this.poisoned) await this.stop("coordinator_failure", "Failure evidence retention failed; inspect returned errors");
}
}
}));
for (const result of settled) if (result.status === "rejected") unretained.push(result.reason);
if (unretained.length) throw new AggregateError(unretained, "Coordinator failure requires independent reconciliation; no further admission");
if (this.poisoned) throw new Error("Coordinator ledger transaction interrupted");
if ((await this.snapshot()).stop) break;
}
return this.snapshot();
}
close(): Promise<void> {
return this.serial(() => {
if (this.inFlight) throw new Error("Cannot release coordinator with launchers in flight");
if (existsSync(this.intentPath)) throw new Error("Cannot release interrupted transaction");
unlinkSync(this.lockPath); this.closed = true;
});
}
}