Files
PaperClipAI/tests/runner-e2e/redaction.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

403 lines
17 KiB
TypeScript

import { readDockerConfigForSecretScan, type DockerConfigCustody } from "./docker-config-custody.js";
import { createReadStream } from "node:fs";
import { lstat, readdir } from "node:fs/promises";
import path from "node:path";
import { createInterface } from "node:readline";
import { redactDiagnosticText } from "../../packages/adapter-utils/src/command-redaction.js";
import { CREDENTIAL_NAMES } from "./types.js";
const SECRET_SHAPES = [
/\b(?:github_pat_|ghp_)[A-Za-z0-9_]{16,}\b/g,
/\bsk-ant-[A-Za-z0-9_-]{16,}\b/g,
/\bsk-(?:proj-)?[A-Za-z0-9_-]{16,}\b/g,
/\b(?:openrouter|daytona)[-_]?(?:api)?[-_]?key["'=:\s]+[A-Za-z0-9._-]{12,}\b/gi,
] as const;
export function secretShapeObservations(text: string) {
const matches: Array<{ detector: number; start: number; end: number }> = [];
for (const [detector, pattern] of SECRET_SHAPES.entries()) {
for (const match of text.matchAll(new RegExp(pattern.source, pattern.flags))) {
if (matches.length >= 32) return { bounded: false, matches };
matches.push({ detector, start: match.index!, end: match.index! + match[0].length });
}
}
return { bounded: true, matches: matches.sort((a, b) => a.start - b.start || a.detector - b.detector) };
}
const SENSITIVE_JSON_KEY =
/^(?:api[-_]?key|access[-_]?token|refresh[-_]?token|auth(?:orization)?|bearer|client[-_]?secret|cookie|password|secret|token)$/i;
const SAFE_SECRET_SOURCES = new Set<string>([...CREDENTIAL_NAMES, "PAPERCLIP_AGENT_JWT_SECRET",
"PAPERCLIP_DECISION_SIGNING_SECRET", "PAPERCLIP_TOOL_ACTION_SIGNING_SECRET", "BETTER_AUTH_SECRET"]);
// Credential dictionaries can have account identities as keys. Only known
// schema fields, never arbitrary JSON keys, may become diagnostic labels.
const SAFE_SECRET_FIELDS = new Set(["tokens", "account_id", "access_token", "refresh_token", "id_token", "token",
"accountId", "accessToken", "refreshToken", "idToken", "accounts", "credentials", "auth", "user", "account",
"api_key", "apiKey", "client_id", "client_secret", "session", "cookie", "email", "username", "identity", "id",
"expires_at", "expiresAt", "last_refresh"]);
function secretEntries(value: string | undefined): Array<{ value: string; fieldPath: string }> {
const trimmed = value?.trim();
if (!trimmed) return [];
const entries = [{ value: trimmed, fieldPath: "" }];
if (trimmed.startsWith("{")) {
try {
const visit = (child: unknown, fieldPath: string, depth: number) => {
if (depth > 8) return;
if (typeof child === "string") {
if (child.length >= 8) entries.push({ value: child, fieldPath });
} else if (child && typeof child === "object") {
for (const [key, next] of Object.entries(child)) {
if (key.includes("::")) entries.push({ value: key, fieldPath: `${fieldPath}.[key]` });
const nextPath = Array.isArray(child) ? `${fieldPath}[${key}]`
: `${fieldPath}.${SAFE_SECRET_FIELDS.has(key) ? key : "[field]"}`;
visit(next, nextPath, depth + 1);
}
}
};
visit(JSON.parse(trimmed), "", 0);
} catch { /* Preserve ordinary non-JSON secrets. */ }
}
return entries;
}
export function normalizedSecrets(values: readonly (string | undefined)[]) {
return [...new Set(values.flatMap(value => secretEntries(value).map(entry => entry.value)))].sort((left, right) => right.length - left.length);
}
export interface SecretScanOptions {
includeShapes?: boolean;
sources?: Readonly<Record<string, string | undefined>>;
}
export interface SecretLeakMatch { reason: string; matchCategory?: string }
function secretMatchIndex(secrets: readonly string[], sources?: SecretScanOptions["sources"]) {
const labels = new Map<string, string>();
for (const [name, value] of Object.entries(sources ?? {})) {
const source = SAFE_SECRET_SOURCES.has(name) ? name : "other credential";
for (const entry of secretEntries(value)) {
if (!labels.has(entry.value)) labels.set(entry.value, source + entry.fieldPath);
}
}
return normalizedSecrets(secrets).map(value => ({ value, matchCategory: labels.get(value) ?? "unattributed credential" }));
}
function matchSecret(text: string, index: ReturnType<typeof secretMatchIndex>, options: SecretScanOptions): SecretLeakMatch | null {
for (const entry of index) {
if (text.includes(entry.value)) return { reason: "exact secret value",
...(options.sources ? { matchCategory: entry.matchCategory } : {}) };
}
if (options.includeShapes !== false) {
for (const pattern of SECRET_SHAPES) {
pattern.lastIndex = 0;
if (pattern.test(text)) return { reason: "secret-shaped value",
...(options.sources ? { matchCategory: "provider key shape" } : {}) };
}
}
return null;
}
/** Diagnostic classification carries no matched bytes or arbitrary JSON keys. */
export function findSecretLeakMatch(value: string | Buffer, secrets: readonly string[], options: SecretScanOptions = {}) {
return matchSecret(Buffer.isBuffer(value) ? value.toString("utf8") : value, secretMatchIndex(secrets, options.sources), options);
}
export function isEphemeralCodexRuntimeAuthFile(
paperclipHome: string,
file: string,
) {
const relative = path.relative(paperclipHome, file).split(path.sep).join("/");
return (
/^instances\/[^/]+\/companies\/[^/]+\/agents\/[^/]+\/codex-home\/auth\.json$/.test(
relative,
) ||
/^instances\/[^/]+\/runtime\/paperclip-runner\/durable-sessions\/[^/]+\/codex-home\/auth\.json$/.test(
relative,
) ||
/^instances\/[^/]+\/runtime\/paperclip-runner\/acpx\/acpx\/[^/]+\/codex-home\/auth\.json$/.test(
relative,
)
);
}
export function isEphemeralCodexRuntimeIdentityDatabase(paperclipHome: string, file: string) {
return /^(?:logs|state)_\d+\.sqlite(?:-wal|-shm)?$/.test(path.basename(file)) &&
isEphemeralCodexRuntimeAuthFile(paperclipHome, path.join(path.dirname(file), "auth.json"));
}
function isEphemeralCodexRolloutFile(paperclipHome: string, file: string) {
const match = /^(.*\/codex-home)\/sessions\/\d{4}\/\d{2}\/\d{2}\/rollout-[\w-]+\.jsonl$/.exec(file.split(path.sep).join("/"));
return Boolean(match && isEphemeralCodexRuntimeAuthFile(paperclipHome, path.join(match[1]!, "auth.json")));
}
function privateCodexAccountIdentity(sources: SecretScanOptions["sources"]): string | undefined {
try {
const accountId = JSON.parse(sources?.CODEX_AUTH_JSON ?? "null")?.tokens?.account_id;
if (typeof accountId !== "string" || accountId.length < 8) return;
for (const [name, raw] of Object.entries(sources ?? {})) {
if (secretEntries(raw).some(entry => entry.value === accountId &&
(name !== "CODEX_AUTH_JSON" || entry.fieldPath !== ".tokens.account_id"))) return;
}
return accountId;
} catch { return; }
}
/** Browser navigation diagnostics need a route, never OAuth query credentials. */
export function browserDiagnosticUrl(value: string): string {
try {
const url = new URL(value);
return ["http:", "https:"].includes(url.protocol) ? url.origin + url.pathname : "[non-HTTP URL]";
} catch { return "[invalid URL]"; }
}
export function redactText(value: string, secrets: readonly string[]) {
let redacted = redactDiagnosticText(value, "[REDACTED]");
return redactKnownSecretsAndShapes(redacted, secrets);
}
function redactKnownSecretsAndShapes(
value: string,
secrets: readonly string[],
) {
let redacted = value;
for (const secret of normalizedSecrets(secrets))
redacted = redacted.split(secret).join("[REDACTED]");
for (const pattern of SECRET_SHAPES)
redacted = redacted.replace(pattern, "[REDACTED_SECRET_SHAPE]");
return redacted;
}
function redactStructuredText(value: string, secrets: readonly string[]) {
const knownSafe = redactKnownSecretsAndShapes(value, secrets);
if (
!/(?:Authorization\s*:\s*Bearer|(?:api[-_]?key|token|secret|password)\s*=)/i.test(
knownSafe,
)
) {
return knownSafe;
}
return redactKnownSecretsAndShapes(
redactDiagnosticText(knownSafe, "[REDACTED]"),
secrets,
);
}
export function findSecretLeak(
value: string | Buffer,
secrets: readonly string[],
options: { includeShapes?: boolean } = {},
): string | null {
return findSecretLeakMatch(value, secrets, options)?.reason ?? null;
}
/**
* Scan structured JSON one key/value string at a time. Scanning the serialized
* document for provider key shapes can join an object key, punctuation, and an
* unrelated value into a false positive that never existed in the payload.
*/
export function findSecretLeakInJsonValues(
value: unknown,
secrets: readonly string[],
options: { includeShapes?: boolean } = {},
): string | null {
if (typeof value === "string") return findSecretLeak(value, secrets, options);
if (Array.isArray(value)) {
for (const entry of value) {
const leak = findSecretLeakInJsonValues(entry, secrets, options);
if (leak) return leak;
}
return null;
}
if (value && typeof value === "object") {
for (const [key, entry] of Object.entries(value)) {
const keyLeak = findSecretLeak(key, secrets, options);
if (keyLeak) return keyLeak;
const valueLeak = findSecretLeakInJsonValues(entry, secrets, options);
if (valueLeak) return valueLeak;
}
}
return null;
}
export function sanitizeJson(
value: unknown,
secrets: readonly string[],
): unknown {
// Structured values are not shell diagnostics. Applying the command/JWT
// heuristic here would redact stable dotted identifiers such as our schema
// name. Exact loaded secrets and well-known provider key shapes are enough.
if (typeof value === "string") return redactStructuredText(value, secrets);
if (Array.isArray(value))
return value.map((entry) => sanitizeJson(entry, secrets));
if (value && typeof value === "object") {
return Object.fromEntries(
Object.entries(value).map(([key, entry]) => [
key,
SENSITIVE_JSON_KEY.test(key)
? "[REDACTED]"
: sanitizeJson(entry, secrets),
]),
);
}
return value;
}
/** Preserve the original outcome when the independent persistence gate wins. */
export function secretScanFailureEvidence(input: {
scanError: string;
finding?: SecretLeakMatch & { file: string; stateArea: string };
originalResult: { status: string; failureClass?: string; error?: string };
}, secrets: readonly string[]) {
return sanitizeJson({ schema: "paperclip.e2e.persisted-state-scan.v1", ...input }, secrets);
}
export function assertSecretFree(
value: string | Buffer,
secrets: readonly string[],
label: string,
options: { includeShapes?: boolean } = {},
) {
const leak = findSecretLeak(value, secrets, options);
if (leak) throw new Error(`Secret leak in ${label}: ${leak}`);
}
export function isEphemeralPostgresPidFile(paperclipHome: string, file: string): boolean {
const relative = path.relative(paperclipHome, file).split(path.sep).join("/");
return /^instances\/[^/]+\/db\/postmaster\.pid$/.test(relative);
}
export function isEphemeralPostgresScanFile(paperclipHome: string, file: string): boolean {
const relative = path.relative(paperclipHome, file).split(path.sep).join("/");
// A relation can be unlinked during PostgreSQL shutdown/checkpoint. Existing
// files are always scanned; this predicate only permits ENOENT after readdir.
return isEphemeralPostgresPidFile(paperclipHome, file) ||
/^instances\/[^/]+\/db\/base\/\d+\/\d+(?:_(?:fsm|vm|init))?(?:\.\d+)?$/.test(relative);
}
/** The product's embedded database files: the only persisted state that may
* hold a product-delivered credential (the saved chat reply and the run events
* it is built from). Run logs, server/HTTP logs and every other file stay
* strict. */
export function isProductDeliveredStateFile(paperclipHome: string, file: string): boolean {
const relative = path.relative(paperclipHome, file).split(path.sep).join("/");
return /^instances\/[^/]+\/db\//.test(relative);
}
/** Scan persisted state. A product-delivered value is held to every rule
* except inside the embedded database under `productStateRoot` (the isolated
* Paperclip home); every other secret is held to every rule everywhere. */
export async function findSecretLeakInPersistedState(
root: string,
secrets: readonly string[],
delivered: readonly string[],
options: Parameters<typeof findSecretLeakInDirectory>[2] & { productStateRoot?: string } = {},
) {
const { productStateRoot, ...scan } = options;
const deliveredValues = new Set(normalizedSecrets(delivered));
if (!productStateRoot || deliveredValues.size === 0) return findSecretLeakInDirectory(root, secrets, scan);
const strict = normalizedSecrets(secrets).filter(value => !deliveredValues.has(value));
const leak = await findSecretLeakInDirectory(root, strict, scan);
if (leak) return leak;
return findSecretLeakInDirectory(root, [...deliveredValues], { ...scan,
ignoreFile: file => scan.ignoreFile?.(file) === true || isProductDeliveredStateFile(productStateRoot, file) });
}
export async function findSecretLeakInDirectory(
root: string,
secrets: readonly string[],
options: {
includeShapes?: boolean;
sources?: SecretScanOptions["sources"];
ignoreFile?: (file: string) => boolean;
allowDisappearedFile?: (file: string) => boolean;
privateCodexRuntimeIdentityRoot?: string;
privateDockerDatabaseConfig?: DockerConfigCustody;
} = {},
): Promise<({ file: string } & SecretLeakMatch) | null> {
const index = secretMatchIndex(secrets, options.sources);
if (options.privateDockerDatabaseConfig && root !== path.join(options.privateDockerDatabaseConfig.temporaryRoot, "paperclip-home")) throw new Error("Docker config custody requires the isolated home scan");
const dockerConfigText = options.privateDockerDatabaseConfig ? await readDockerConfigForSecretScan(root, options.privateDockerDatabaseConfig) : undefined;
let privateIdentity: string | undefined;
if (options.privateCodexRuntimeIdentityRoot) {
if (path.resolve(options.privateCodexRuntimeIdentityRoot) !== path.resolve(root)) {
throw new Error("Private Codex identity policy requires the isolated home root");
}
const temporaryRoot = await lstat(path.dirname(root));
if (!temporaryRoot.isDirectory() || temporaryRoot.isSymbolicLink() || (temporaryRoot.mode & 0o077) !== 0) {
throw new Error("Private Codex identity policy requires an owner-only temporary root");
}
privateIdentity = privateCodexAccountIdentity(options.sources);
}
const overlap = Math.max(
256,
...index.map(entry => entry.value.length + 16),
);
const scan = async (
directory: string,
): Promise<{
file: string;
reason: string;
matchCategory?: string;
} | null> => {
const entries = await readdir(directory, { withFileTypes: true });
for (const entry of entries) {
const file = path.join(directory, entry.name);
if (entry.isDirectory()) {
const leak = await scan(file);
if (leak) return leak;
} else if (entry.isFile()) {
if (options.ignoreFile?.(file)) continue;
if (options.privateDockerDatabaseConfig && file === options.privateDockerDatabaseConfig.configPath) {
const match = matchSecret(dockerConfigText!, index, options);
if (match) return { file, ...match };
continue;
}
if (privateIdentity && options.privateCodexRuntimeIdentityRoot && isEphemeralCodexRolloutFile(options.privateCodexRuntimeIdentityRoot, file)) {
const stream = createReadStream(file);
const lines = createInterface({ input: stream, crlfDelay: Infinity });
try {
for await (const line of lines) {
if (Buffer.byteLength(line) > 8 * 1024 * 1024) throw new Error("Codex rollout record exceeds private scan bound");
let scanned = line;
try {
const record = JSON.parse(line);
if (record?.type === "session_meta" && record.payload?.creator_account_id === privateIdentity) {
delete record.payload.creator_account_id;
scanned = JSON.stringify(record);
}
} catch {}
const match = matchSecret(scanned, index, options);
if (match) return { file, ...match };
}
} finally { lines.close(); stream.destroy(); }
continue;
}
const fileIndex = privateIdentity && options.privateCodexRuntimeIdentityRoot &&
isEphemeralCodexRuntimeIdentityDatabase(options.privateCodexRuntimeIdentityRoot, file)
? index.filter(entry => entry.value !== privateIdentity) : index;
let carry = Buffer.alloc(0);
try {
for await (const chunk of createReadStream(file)) {
const data = Buffer.concat([
carry,
Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk),
]);
const match = matchSecret(data.toString("utf8"), fileIndex, options);
if (match) return { file, ...match };
carry = data.subarray(Math.max(0, data.length - overlap));
}
} catch (error) {
// PostgreSQL removes its PID file on shutdown, possibly after readdir.
// Existing contents are still scanned; only the caller's exact
// transient paths may disappear. Evidence and other I/O errors fail.
if ((error as NodeJS.ErrnoException)?.code !== "ENOENT" ||
!options.allowDisappearedFile?.(file)) throw error;
}
}
}
return null;
};
return scan(root);
}