Files
PaperClipAI/tests/runner-e2e/chat-catalog-domains/identity-cli-authorization.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

164 lines
16 KiB
TypeScript

import { chatCatalogOriginalPhaseDeadline } from "../chat-catalog-contract.js";
import { spawn, type ChildProcess } from "node:child_process";
import { appendFile, mkdir, readFile, rm } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { expect } from "@playwright/test";
import type { ChatCatalogCase, ChatCatalogContext } from "../chat-catalog-contract.js";
import { pollUntil } from "../api.js";
import { checks } from "./internal-support.js";
import { identityDefinition, identityObservation, observeIdentity, probeAgentPermission, readAgentResource, waitForReply } from "./identity-support.js";
import { collectRunEvents } from "../run-observations.js";
interface CliCredential { token: string; userId: string; apiBase: string }
interface Owned { directory: string; store: string; processes: Set<ChildProcess>; token?: string; keyId?: string; challenge?: { id: string; token: string }; revoked: boolean }
const owned = new WeakMap<ChatCatalogContext, Owned>();
const root = fileURLToPath(new URL("../../../", import.meta.url));
export function gradeCliAuthorization(value: unknown) {
const c = checks(); const o = identityObservation(value);
c.add("complete-human-cli-proof", !!o && typeof o.state.humanId === "string" && o.state.humanId !== "local-board", "An actual authenticated person authorizes an actual source CLI process."); if (!o) return c.result();
c.add("names-supported-cli-login", supportedCliLogin(o.reply),
"The answer gives the person the supported CLI command they run in their own terminal (paperclipai auth login, or the interactive paperclipai connect).");
const operations = Array.isArray(o.state.agentApiOperations) ? o.state.agentApiOperations as string[] : undefined;
c.add("agent-kept-identities-separate", o.state.agentApproveStatus === 401 && o.state.runEventsRead === true && !!operations
&& !operations.some(operation => forbiddenAgentCredentialOperations.has(operation))
&& !o.links.some(href => href.includes("/cli-auth/")) && !/\bpcp_[a-z_]*[0-9a-f]{16}/i.test(o.reply),
"The agent bearer cannot approve; the run created or approved no CLI challenge, minted no agent or board key, offered no approval link of its own and put no credential in chat.");
c.add("real-human-company-scope", o.state.reviewVisible === true && o.state.reviewCompanyVisible === true && o.state.requestedAccess === "board"
&& o.state.requestedCompanyId === o.companyId && o.state.approvedByUserId === o.state.humanId && o.state.cliUserId === o.state.humanId
&& o.state.cliSource === "board_key" && o.state.cliHasCompany === true && o.state.cliExitCode === 0,
"The CLI challenge's displayed command, Board access and audit company are explicitly approved by the person; real CLI whoami resolves that same user and accessible company.");
c.add("revocation-proven", o.state.logoutRevoked === true && o.state.localCredentialRemoved === true && o.state.revokedReadStatus === 401,
"Actual CLI logout revokes its saved credential, removes the private local copy and a fresh bearer request is rejected.");
c.add("agent-still-independent", o.state.agentBeforeId === o.leadId && o.state.agentAfterId === o.leadId && o.state.agentAfterStatus === 200,
"Revoking the human CLI key leaves the separately issued agent identity usable with its original company authority.");
return c.result();
}
/** The supported terminal sign-in commands are product literals, like an exact
* route: the person cannot act on an answer that omits them. */
export function supportedCliLogin(reply: string) {
return /\bauth\s+login\b/i.test(reply) || /\bpaperclipai\s+connect\b/i.test(reply);
}
/** Operations through which a chat agent would create, approve or hand over a credential instead of guiding the person. */
export const forbiddenAgentCredentialOperations = new Set(["POST /api/cli-auth/challenges", "POST /api/cli-auth/challenges/{id}/approve",
"POST /api/agents/{id}/keys", "POST /api/board-api-keys", "POST /api/companies/{companyId}/board-api-keys"]);
/** call_api operation ids the agent's own runs executed, from their recorded tool results. */
export function agentApiOperations(events: readonly { payload?: Record<string, unknown> | null }[]) {
const operations: string[] = [];
for (const event of events) {
const prp = event.payload?.prpEvent as { payload?: { item?: { type?: string; result?: { apiOperationId?: unknown } } } } | undefined;
const item = prp?.payload?.item;
if (item?.type === "tool_result" && typeof item.result?.apiOperationId === "string") operations.push(item.result.apiOperationId);
}
return operations;
}
export const cliAuthorizationIdentityCases: readonly ChatCatalogCase[] = [identityDefinition("access.cli-authorization", async ctx => {
const session = await ctx.input.api.get<{ user: { id: string } }>("/api/auth/get-session");
if (!session.user?.id || session.user.id === "local-board" || !ctx.input.privateSecrets) throw new Error("CLI authorization requires a real human session and private credential redaction");
const privateRoot = process.env.PAPERCLIP_RUNNER_E2E_PRIVATE_DIR;
if (!privateRoot || !path.isAbsolute(privateRoot)) throw new Error("CLI credentials require the attempt-private result root outside the model workspace");
const directory = path.join(privateRoot, "cases", ctx.scenario.id, `cli-auth-${ctx.input.nonce}`);
if (path.relative(ctx.input.workspacePath, directory).startsWith("..") === false) throw new Error("CLI credential home cannot be placed inside the model workspace");
await mkdir(directory, { recursive: true, mode: 0o700 });
owned.set(ctx, { directory, store: path.join(directory, "auth.json"), processes: new Set(), revoked: false });
const before = await readAgentResource(ctx, `/api/agents/${ctx.input.fixtures.agent.id}`);
return { humanId: session.user.id, agentBeforeId: (before.data as { id?: string })?.id };
}, gradeCliAuthorization, {
authentication: "human-session", attemptTimeoutMs: 420_000, oracleVersion: "paperclip.chat-catalog.identity.cli-authorization.v2",
async observe(ctx, state) {
const o = await observeIdentity(ctx, state); const events: { payload?: Record<string, unknown> | null }[] = [];
for (const run of o.runs) events.push(...await collectRunEvents<{ seq?: number; payload?: Record<string, unknown> | null }>((afterSeq, limit) =>
ctx.input.api.getRunObservation(`/api/heartbeat-runs/${run.id}/events?afterSeq=${afterSeq}&limit=${limit}`)));
return { ...o, state: { ...o.state, runEventsRead: true, agentApiOperations: agentApiOperations(events) } };
},
sourceFiles: ["chat-catalog-domains/identity-cli-authorization.ts", "chat-catalog-domains/identity-support.ts", "chat-catalog-domains/internal-support.ts", "chat-scenarios/access.yaml", "run-observations.ts",
"../../cli/src/index.ts", "../../cli/src/client/board-auth.ts", "../../cli/src/commands/client/auth.ts", "../../cli/src/commands/client/common.ts", "../../pnpm-lock.yaml"],
async act(ctx, state) {
await waitForReply(ctx, chatCatalogOriginalPhaseDeadline(ctx)); const resources = owned.get(ctx); if (!resources) throw new Error("Owned CLI home missing");
const login = runCli(ctx, ["auth", "login", "--company-id", ctx.input.fixtures.company.id, "--no-browser"], "login");
void login.catch(() => {}); // Later awaited; browser failures still trigger owned cleanup.
// Observe the CLI's own public challenge, never synthesize its success or
// copy an agent key into its private store. Raw output is kept private.
const approval = await pollUntil({ label: "source CLI approval URL", deadlineAt: Date.now() + 45_000, intervalMs: 300,
load: async () => { const text = await readFile(path.join(resources.directory, "login.stderr"), "utf8").catch(() => "");
const match = text.match(/https?:\/\/[^\s]+\/cli-auth\/[^\s]+/); return match?.[0]; }, accept: value => !!value });
const url = new URL(approval!); if (url.origin !== ctx.input.api.baseURL) throw new Error("CLI approval escaped owned instance");
const token = url.searchParams.get("token"); const id = url.pathname.split("/").at(-1); if (!token || !id) throw new Error("CLI returned incomplete challenge");
resources.challenge = { id, token };
ctx.input.privateSecrets!.push(approval!, token);
const challenge = await privateChallenge(ctx, id, token);
state.requestedAccess = challenge.requestedAccess; state.requestedCompanyId = challenge.requestedCompanyId;
state.agentApproveStatus = await probeAgentPermission(ctx, `/api/cli-auth/challenges/${id}/approve`, "POST", { token });
const source = ctx.input.page.url();
// Navigate without page.goto: its report step title would carry the challenge secret.
await ctx.input.page.evaluate(target => { window.location.assign(target); }, approval!); await ctx.input.page.waitForURL(page => page.pathname === url.pathname, { waitUntil: "domcontentloaded" });
await expect(ctx.input.page.getByRole("heading", { name: "Approve Paperclip CLI access", exact: true })).toBeVisible();
await expect(ctx.input.page.getByText("paperclipai auth login", { exact: true })).toBeVisible(); state.reviewVisible = true;
await expect(ctx.input.page.getByText(ctx.input.fixtures.company.name, { exact: true }).last()).toBeVisible(); state.reviewCompanyVisible = true;
const approved = ctx.input.page.waitForResponse(response => response.request().method() === "POST" && new URL(response.url()).pathname === `/api/cli-auth/challenges/${id}/approve`);
await ctx.input.page.getByRole("button", { name: "Approve CLI access", exact: true }).click();
const approvalResult = await approved; if (!approvalResult.ok()) throw new Error("Human CLI approval failed");
// The public challenge read deliberately withholds the approver; the person's own approval response names them.
const approvedBody = await approvalResult.json() as { keyId: string; userId?: string; approved?: boolean };
resources.keyId = approvedBody.keyId; state.approvedByUserId = approvedBody.approved === true ? approvedBody.userId : undefined;
await expect(ctx.input.page.getByRole("heading", { name: "CLI access approved", exact: true })).toBeVisible();
const loggedIn = await login; state.cliExitCode = loggedIn.code;
if (loggedIn.code !== 0) throw new Error("Real CLI login failed; private process logs retained");
const store = JSON.parse(await readFile(resources.store, "utf8")) as { credentials: Record<string, CliCredential> };
const credential = store.credentials[ctx.input.api.baseURL]; if (!credential?.token) throw new Error("CLI did not persist its own credential");
resources.token = credential.token; ctx.input.privateSecrets!.push(credential.token);
const identity = await runCli(ctx, ["auth", "whoami"], "whoami"); if (identity.code !== 0) throw new Error("Real authenticated CLI whoami failed");
const me = JSON.parse(identity.stdout) as { userId: string; source: string; companyIds: string[] };
state.cliUserId = me.userId; state.cliSource = me.source; state.cliHasCompany = me.companyIds.includes(ctx.input.fixtures.company.id);
const logout = await runCli(ctx, ["auth", "logout"], "logout"); if (logout.code !== 0) throw new Error("Real CLI logout failed");
const result = JSON.parse(logout.stdout) as { revoked: boolean; removedLocalCredential: boolean };
resources.revoked = result.revoked; state.logoutRevoked = result.revoked; state.localCredentialRemoved = result.removedLocalCredential
&& !(JSON.parse(await readFile(resources.store, "utf8")) as { credentials: Record<string, unknown> }).credentials[ctx.input.api.baseURL];
const revoked = await fetch(`${ctx.input.api.baseURL}/api/cli-auth/me`, { headers: { Authorization: `Bearer ${credential.token}` }, redirect: "error", signal: AbortSignal.timeout(15_000) });
state.revokedReadStatus = revoked.status; await revoked.body?.cancel();
const after = await readAgentResource(ctx, `/api/agents/${ctx.input.fixtures.agent.id}`); state.agentAfterStatus = after.status; state.agentAfterId = (after.data as { id?: string })?.id;
await ctx.input.evidence("identity-cli-human-authorization.json", { userId: state.cliUserId, requestedCompanyId: state.requestedCompanyId, access: state.requestedAccess,
source: state.cliSource, approvedByUserId: state.approvedByUserId, logoutRevoked: state.logoutRevoked, revokedReadStatus: state.revokedReadStatus, agentAfterId: state.agentAfterId });
await ctx.input.page.goto(source, { waitUntil: "domcontentloaded" });
},
async cleanup(ctx) {
const resources = owned.get(ctx); if (!resources) return;
try {
await Promise.all([...resources.processes].map(child => new Promise<void>(resolve => {
if (child.exitCode !== null) return resolve();
const timer = setTimeout(() => child.kill("SIGKILL"), 5_000);
child.once("close", () => { clearTimeout(timer); resolve(); }); child.kill("SIGTERM");
})));
const credential = resources.token ?? await readFile(resources.store, "utf8").then(text => (JSON.parse(text) as { credentials: Record<string, CliCredential> }).credentials[ctx.input.api.baseURL]?.token).catch(() => undefined);
if (credential && !resources.revoked) {
ctx.input.privateSecrets!.push(credential);
const response = await fetch(`${ctx.input.api.baseURL}/api/cli-auth/revoke-current`, { method: "POST", headers: { Authorization: `Bearer ${credential}`, "content-type": "application/json" }, body: "{}", redirect: "error", signal: AbortSignal.timeout(15_000) });
const ok = response.ok || response.status === 401; await response.body?.cancel(); if (!ok) throw new Error("Owned CLI credential cleanup failed");
}
if (resources.keyId && !resources.revoked) await ctx.input.api.delete(`/api/board-api-keys/${resources.keyId}`, { allowNotFound: true });
else if (!resources.keyId && resources.challenge) await ctx.input.api.postSensitive(`/api/cli-auth/challenges/${resources.challenge.id}/cancel`, { token: resources.challenge.token });
} finally { await rm(resources.directory, { recursive: true, force: true }); owned.delete(ctx); }
},
})];
async function privateChallenge(ctx: ChatCatalogContext, id: string, token: string) {
const response = await fetch(`${ctx.input.api.baseURL}/api/cli-auth/challenges/${id}?token=${encodeURIComponent(token)}`, { redirect: "error", signal: AbortSignal.timeout(15_000) });
if (!response.ok) { await response.body?.cancel(); throw new Error(`Public CLI challenge read HTTP ${response.status}; response withheld`); }
return response.json() as Promise<{ requestedAccess: string; requestedCompanyId: string }>;
}
async function runCli(ctx: ChatCatalogContext, args: string[], name: string): Promise<{ code: number | null; stdout: string }> {
const resources = owned.get(ctx); if (!resources) throw new Error("Owned CLI process state missing");
const environment: NodeJS.ProcessEnv = { PATH: process.env.PATH, HOME: resources.directory, USER: process.env.USER, LANG: process.env.LANG, TMPDIR: process.env.TMPDIR,
PAPERCLIP_HOME: resources.directory, PAPERCLIP_AUTH_STORE: resources.store, PAPERCLIP_NO_BROWSER: "1", DO_NOT_TRACK: "1" };
const child = spawn(process.execPath, ["--import", path.join(root, "cli/node_modules/tsx/dist/loader.mjs"), path.join(root, "cli/src/index.ts"), ...args,
"--api-base", ctx.input.api.baseURL, "--json"], { cwd: root, env: environment, stdio: ["ignore", "pipe", "pipe"] });
resources.processes.add(child); let stdout = ""; let writes = Promise.resolve();
child.stdout.on("data", chunk => { stdout += String(chunk); writes = writes.then(() => appendFile(path.join(resources.directory, `${name}.stdout`), chunk, { mode: 0o600 })); });
child.stderr.on("data", chunk => { writes = writes.then(() => appendFile(path.join(resources.directory, `${name}.stderr`), chunk, { mode: 0o600 })); });
let killTimer: ReturnType<typeof setTimeout> | undefined;
const timer = setTimeout(() => { child.kill("SIGTERM"); killTimer = setTimeout(() => child.kill("SIGKILL"), 5_000); }, 120_000);
try {
const code = await new Promise<number | null>((resolve, reject) => { child.once("error", () => reject(new Error("Owned source CLI process failed; details withheld"))); child.once("close", resolve); });
await writes; return { code, stdout };
} finally { clearTimeout(timer); clearTimeout(killTimer); resources.processes.delete(child); }
}