Files
PaperClipAI/tests/runner-e2e/chat-catalog-domains/internal-routine-audit.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

180 lines
17 KiB
TypeScript

import { createHash } from "node:crypto";
import { expect } from "@playwright/test";
import type { ChatCatalogCase } from "../chat-catalog-contract.js";
import { pollUntil } from "../api.js";
import { collectRunEvents } from "../run-observations.js";
import { fixtureWorker, makeDefinition } from "./internal-definition.js";
import { checks, companyPath, entity, hasLink, internalResultLink, observation, observeInternal, project, verifyInternalResult, type Entity, type InternalObservation } from "./internal-support.js";
interface RoutineRun extends Entity { routineId?: string; source?: string; triggeredAt?: string; completedAt?: string | null; linkedIssueId?: string | null; failureReason?: string | null }
interface RunEvent { seq?: number; eventType?: string; payload?: Record<string, unknown> | null }
interface RoutineAudit extends InternalObservation { routine: Entity; routineRuns: RoutineRun[]; executionTask?: Entity; leadEvents: RunEvent[]; observedAt: string }
export function localDay(instant: string, timezone: string) { return new Intl.DateTimeFormat("en-CA", { timeZone: timezone, year: "numeric", month: "2-digit", day: "2-digit" }).format(new Date(instant)); }
export function localClock(instant: Date, timezone: string) {
const parts = new Intl.DateTimeFormat("en-US", { timeZone: timezone, hour: "2-digit", minute: "2-digit", hourCycle: "h23" }).formatToParts(instant);
return { hour: Number(parts.find(part => part.type === "hour")!.value), minute: Number(parts.find(part => part.type === "minute")!.value) };
}
/**
* Real places without daylight saving time, one for every whole-hour UTC
* offset from -11 to +14, so one of them always reads 08:xx. The person is
* placed there, so "this morning" is literally true for their browser and for
* the routine's own schedule timezone; no history is backdated.
*/
export const MORNING_CANDIDATE_TIMEZONES = ["Pacific/Pago_Pago", "Pacific/Honolulu", "Pacific/Gambier", "Pacific/Pitcairn", "America/Phoenix", "America/Guatemala",
"America/Panama", "America/Puerto_Rico", "America/Argentina/Buenos_Aires", "Atlantic/South_Georgia", "Atlantic/Cape_Verde", "Africa/Abidjan", "Africa/Lagos",
"Africa/Johannesburg", "Africa/Nairobi", "Asia/Dubai", "Asia/Karachi", "Asia/Dhaka", "Asia/Bangkok", "Asia/Singapore", "Asia/Tokyo", "Australia/Brisbane",
"Pacific/Noumea", "Pacific/Tarawa", "Pacific/Tongatapu", "Pacific/Kiritimati"] as const;
export function morningTimezone(now: Date) {
const zone = MORNING_CANDIDATE_TIMEZONES.find(candidate => localClock(now, candidate).hour === 8);
if (!zone) throw new Error("No candidate timezone currently reads 08:xx");
return zone;
}
function morning(instant: string | undefined, timezone: string, observedAt: string) {
return !!instant && localDay(instant, timezone) === localDay(observedAt, timezone) && localClock(new Date(instant), timezone).hour < 12;
}
function canonicalJson(value: unknown): string {
if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`;
if (value && typeof value === "object") return `{${Object.keys(value).filter(key => (value as Record<string, unknown>)[key] !== undefined).sort()
.map(key => `${JSON.stringify(key)}:${canonicalJson((value as Record<string, unknown>)[key])}`).join(",")}}`;
return JSON.stringify(value) ?? "null";
}
/** Same digest the server stores for a redacted call_api input (canonical JSON, sha256). */
export function callApiInputDigest(input: Record<string, unknown>) { return createHash("sha256").update(canonicalJson(input), "utf8").digest("hex"); }
/**
* call_api inputs whose response carries this routine's run records (status and failure reason): its runs
* (the server clamps any limit to 1..200 and the fixture has one run, so every integer limit up to 10000,
* as a number or string, counts), its detail (recent runs), or the company routine list
* (last run), unfiltered or filtered to the routine's project. The company path parameter may be omitted;
* the server fills it from the caller's company. Empty query/path objects are equivalent to omitted ones.
*/
const readCache = new Map<string, ReadonlySet<string>>();
export function routineExecutionReads(companyId: string, routineId: string, projectId?: string): ReadonlySet<string> {
const key = JSON.stringify([companyId, routineId, projectId ?? null]); const cached = readCache.get(key); if (cached) return cached;
const list = "GET /api/companies/{companyId}/routines"; const runs = "GET /api/routines/{id}/runs"; const detail = "GET /api/routines/{id}";
const variants = (operationId: string, paths: Array<Record<string, unknown> | undefined>, queries: Array<Record<string, unknown> | undefined>) =>
paths.flatMap(pathParams => queries.map(query => ({ operationId, ...(pathParams ? { pathParams } : {}), ...(query ? { query } : {}) })));
const limits = Array.from({ length: 10_001 }, (_, index) => index).flatMap(limit => [{ limit }, { limit: String(limit) }]);
const routinePath = [{ id: routineId }];
const reads = new Set([
...variants(runs, routinePath, [undefined, {}, ...limits]),
...variants(detail, routinePath, [undefined, {}]),
...variants(list, [{ companyId }, undefined, {}], [undefined, {}, ...(projectId ? [{ projectId }] : [])]),
].map(callApiInputDigest));
readCache.set(key, reads); return reads;
}
function prp(row: RunEvent) {
const envelope = (row.payload?.prpEvent ?? {}) as Record<string, unknown>;
const payload = (envelope.payload ?? {}) as Record<string, unknown>;
return { envelope, payload, item: (payload.item ?? {}) as Record<string, unknown> };
}
/**
* The chat run read this routine's run records through the product. call_api
* redacts payloads but keeps the input digest, so a call counts only when its
* input digest is one of this routine's execution reads and its paired result
* succeeded. A dedicated tool's unredacted output must carry the failed
* routine-run id. The agent's own wording is never matched.
*/
export function readFailedExecution(events: readonly RunEvent[], failedRunId: string, expectedReads: ReadonlySet<string>) {
if (!failedRunId) return false;
const uses = new Set(events.map(prp).filter(({ item }) => item.type === "tool_use" && item.name === "call_api"
&& expectedReads.has(String((item.input as Record<string, unknown> | undefined)?.apiPayloadSha256))).map(({ item }) => String(item.id)));
return events.some(row => {
const { envelope, payload, item } = prp(row); const result = (item.result ?? {}) as Record<string, unknown>;
if (item.type === "tool_result" && uses.has(String(item.tool_use_id)) && result.ok === true && result.status === 200) return true;
return row.eventType === "tool.execution.completed" && envelope.sourceKind === "runner" && payload.status === "completed"
&& typeof payload.output === "string" && payload.output.includes(failedRunId);
});
}
function routineLinks(o: InternalObservation, routineId: string) {
return o.links.filter(href => { try { const u = new URL(href, o.origin); return u.origin === new URL(o.origin).origin && u.pathname.split("/").includes(routineId) && u.pathname.includes("/routines/"); } catch { return false; } });
}
export function gradeRoutineAudit(value: unknown) {
const c = checks(); const o = observation(value) as RoutineAudit | undefined;
c.add("actual-routine-context", !!o && !!o.routine && Array.isArray(o.routineRuns) && Array.isArray(o.leadEvents) && typeof o.observedAt === "string"
&& typeof o.state.timezone === "string" && typeof o.state.failedRunId === "string", "Saved routine definition, execution history, the chat run's tool events and the person's timezone are required.");
if (!o || !o.routine || !Array.isArray(o.routineRuns) || !Array.isArray(o.leadEvents) || typeof o.state.timezone !== "string" || typeof o.state.failedRunId !== "string") return c.result();
const zone = o.state.timezone; const original = entity(o.state, "routine"); const failed = o.routineRuns.find(row => row.id === o.state.failedRunId);
const edit = o.activity.find(row => row.id === o.state.editEventId);
c.add("real-edit-then-failed-morning-run", o.routine.companyId === o.companyId && !!failed && failed.routineId === original.id && failed.source === "schedule" && failed.status === "failed"
&& morning(failed.triggeredAt, zone, o.observedAt) && !!failed.linkedIssueId && o.executionTask?.id === failed.linkedIssueId && o.executionTask.status === "blocked"
&& !!edit && edit.entityId === original.id && edit.action === "routine.updated" && edit.actorType === "user" && !!edit.createdAt && Date.parse(edit.createdAt) < Date.parse(failed.triggeredAt!)
&& !o.routineRuns.some(row => (row.status === "completed" || row.status === "succeeded") && morning(row.triggeredAt ?? row.createdAt, zone, o.observedAt)),
"A person edited the current-company routine before its real scheduled run this morning (person's timezone); that run failed because its execution task was blocked, and no morning run succeeded.");
c.add("read-only-definition-and-execution", original.updatedAt === o.routine.updatedAt && original.description === o.routine.description
&& o.routineRuns.length === Number(o.state.routineRunCount) && o.executionTask?.status === "blocked"
&& !o.activity.some(row => row.agentId === o.leadId && (row.entityId === original.id || row.entityId === failed?.linkedIssueId)),
"The audit neither manufactures a run, edits the routine, nor changes the failed execution's task.");
// The reply's claims are read by a person, not matched by wording: the checks above and below prove the
// saved outcome, that the chat run read it, and that the person can open the evidence from the answer.
c.add("read-actual-execution-record", readFailedExecution(o.leadEvents, o.state.failedRunId, routineExecutionReads(o.companyId, original.id, (o.state.project as Entity | undefined)?.id)),
"The chat run read the routine's run records (runs, detail recent runs or list last run), not only the definition or edit history.");
c.add("routine-or-execution-linked", routineLinks(o, original.id).length > 0 || (!!o.executionTask && hasLink(o, "issues", o.executionTask)),
"The answer links the real routine or the failed run's execution task, so the person can inspect the evidence.");
return c.result();
}
const ORIGINAL_DESCRIPTION = "Post a three-line launch readiness summary as a comment on this task, then mark this task done.";
const EDITED_DESCRIPTION = "Read this task's launch-readiness document and post a three-line launch readiness summary from it as a comment on this task, then mark this task done. "
+ "If this task has no launch-readiness document, do not write or guess a summary and do not create the document: comment that the launch-readiness document is missing and set this task to blocked. Do not create other tasks or change the routine.";
function routineAudit(): ChatCatalogCase {
const base = makeDefinition("activity.routine-audit", async ctx => {
const { api, page } = ctx.input;
const timezone = morningTimezone(new Date());
// The person's browser lives in the routine's timezone, where it is now morning.
const cdp = await page.context().newCDPSession(page); await cdp.send("Emulation.setTimezoneOverride", { timezoneId: timezone });
const browserTimezone = await page.evaluate(() => Intl.DateTimeFormat().resolvedOptions().timeZone);
if (browserTimezone !== timezone) throw new Error(`Browser timezone override did not apply (${browserTimezone})`);
const p = await project(ctx, "Launch"); const worker = await fixtureWorker(ctx);
// The harness funds the company and lead before setup; fund the routine's worker the same way so its
// unpriced setup turn does not budget-pause it before the query.
await api.post(`${companyPath(ctx)}/budgets/policies`, { scopeType: "agent", scopeId: worker.id, amount: 500, hardStopEnabled: true, isActive: true, unpricedUsagePolicy: "allow" });
const routine = await api.post<Entity>(`${companyPath(ctx)}/routines`, { title: "Daily launch readiness", projectId: p.id, assigneeAgentId: worker.id, description: ORIGINAL_DESCRIPTION });
// A daily schedule in the person's timezone, firing at the next real minute that leaves room for the edit.
const fireAt = new Date(Date.now() + (new Date().getUTCSeconds() >= 45 ? 180_000 : 120_000)); const fire = localClock(fireAt, timezone);
if (fire.hour >= 12) throw new Error("Scheduled morning run would fall after noon");
const trigger = await api.post<Entity & { nextRunAt?: string }>(`/api/routines/${routine.id}/triggers`, { kind: "schedule", cronExpression: `${fire.minute} ${fire.hour} * * *`, timezone, enabled: true });
await api.patch(`/api/routines/${routine.id}`, { description: EDITED_DESCRIPTION });
const edit = (await api.get<Entity[]>(`${companyPath(ctx)}/activity?limit=1000`)).find(row => row.entityId === routine.id && row.action === "routine.updated" && row.actorType === "user");
if (!edit) throw new Error("Real routine edit was not recorded");
const invocation = await pollUntil({ label: "real scheduled morning routine invocation", deadlineAt: Date.now() + 240_000, intervalMs: 2000,
load: () => api.get<RoutineRun[]>(`/api/routines/${routine.id}/runs`), accept: rows => rows.some(row => row.source === "schedule" && !!row.linkedIssueId),
reject: rows => rows.some(row => row.source === "schedule" && row.status === "failed" && !row.linkedIssueId) ? "Scheduled dispatch failed before creating its execution task" : undefined });
const scheduled = invocation.find(row => row.source === "schedule" && !!row.linkedIssueId)!;
const outcome = await pollUntil({ label: "worker blocks the edited routine's execution task and the invocation fails", deadlineAt: Date.now() + 180_000, intervalMs: 2000,
load: async () => ({ runs: await api.get<RoutineRun[]>(`/api/routines/${routine.id}/runs`), task: await api.get<Entity>(`/api/issues/${scheduled.linkedIssueId}`), native: await ctx.allRuns() }),
accept: v => v.task.status === "blocked" && v.runs.some(row => row.id === scheduled.id && row.status === "failed")
&& v.native.length === 1 && v.native[0]!.agentId === worker.id && v.native[0]!.status === "succeeded",
reject: v => v.task.status === "done" ? "Worker completed the task without its required document"
: v.native.some(run => run.agentId !== worker.id) ? "Setup woke an agent other than the routine's worker"
: v.native.length > 1 ? "Worker needed more than one execution"
: v.native.some(run => ["failed", "cancelled", "timed_out"].includes(run.status ?? "")) ? "Worker execution did not finish normally" : undefined });
const now = new Date().toISOString(); const failed = outcome.runs.find(row => row.id === scheduled.id)!;
if (!morning(failed.triggeredAt, timezone, now) || Date.parse(edit.createdAt!) >= Date.parse(failed.triggeredAt!)) throw new Error("Scheduled run is not a real morning run after the edit");
const saved = await api.get<Entity>(`/api/routines/${routine.id}`);
await ctx.input.evidence("chat-catalog-routine-audit-setup.json", { companyId: ctx.input.fixtures.company.id, timezone, trigger, edit, runs: outcome.runs, task: outcome.task, now });
return { project: p, worker: { id: worker.id }, routine: saved, timezone, editEventId: edit.id, failedRunId: failed.id, executionTaskId: failed.linkedIssueId, routineRunCount: outcome.runs.length, now };
}, gradeRoutineAudit);
return { ...base, oracleVersion: "paperclip.chat-catalog.internal.routine-audit.v6", authentication: "human-session", expectedRunCount: 2, minimumRunCount: 2, attemptTimeoutMs: 720_000,
sourceFiles: [...base.sourceFiles, "chat-catalog-domains/internal-routine-audit.ts", "run-observations.ts"],
async observe(ctx, state): Promise<RoutineAudit> {
const id = entity(state, "routine").id; const o = await observeInternal(ctx, state); const leadEvents: RunEvent[] = [];
for (const run of o.runs.filter(row => row.agentId === o.leadId)) leadEvents.push(...await collectRunEvents<RunEvent>((afterSeq, limit) =>
ctx.input.api.getRunObservation(`/api/heartbeat-runs/${run.id}/events?afterSeq=${afterSeq}&limit=${limit}`)));
return { ...o, routine: await ctx.input.api.get(`/api/routines/${id}`), routineRuns: await ctx.input.api.get(`/api/routines/${id}/runs`),
executionTask: await ctx.input.api.get<Entity>(`/api/issues/${String(state.executionTaskId)}`), leadEvents, observedAt: new Date().toISOString() };
},
async verifyResult(ctx, state, value) {
const o = value as RoutineAudit; const routine = entity(state, "routine"); const source = ctx.input.page.url();
for (const href of routineLinks(o, routine.id)) {
await internalResultLink(ctx, o, href).first().click();
await ctx.input.page.waitForURL(url => url.pathname.split("/").includes(routine.id));
await expect(ctx.input.page.getByRole("heading", { name: routine.title!, exact: true }).first()).toBeVisible();
await ctx.input.page.goto(source); await expect(ctx.input.page.getByTestId("task-chat-composer-input")).toBeVisible();
}
if (o.executionTask && hasLink(o, "issues", o.executionTask)) await verifyInternalResult(ctx, state, { ...o, tasks: o.tasks.filter(row => row.id === o.executionTask!.id) });
else if (!routineLinks(o, routine.id).length) throw new Error("Missing actual routine or execution task result link");
},
};
}
export const routineAuditCases: readonly ChatCatalogCase[] = [routineAudit()];