mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-04 12:50:37 +01:00
## 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>
97 lines
16 KiB
TypeScript
97 lines
16 KiB
TypeScript
/** Finite test-server network loss for one actual self-addressed AgentMail send.
|
|
* No provider success, publication, delivery, or DB status is manufactured. */
|
|
import { createHash, randomUUID } from "node:crypto";
|
|
import { createRequire } from "node:module";
|
|
import { EventEmitter } from "node:events";
|
|
import { constants, openSync, closeSync, fstatSync, lstatSync, readFileSync, realpathSync } from "node:fs";
|
|
import { mkdir, writeFile, rename, readFile } from "node:fs/promises";
|
|
import path from "node:path";
|
|
export const lostEmailScenario = "recovery.unknown-email-outcome";
|
|
export interface EmailFaultRequest { schema: "paperclip.email-response-loss.v1"; executionId: string; attemptId: string; companyId: string; endpointId: string; agentId: string; sourceIssueId: string; inboxId: string; recipient: string; subject: string; textSha256: string; nonce: string; createdAt: number; expiresAt: number; dedicatedDisposableInbox: true }
|
|
export interface AcceptedEmailFault { schema: "paperclip.email-response-loss-accepted.v1"; executionId: string; attemptId: string; nonce: string; publicationId: string; providerMessageId: string; providerThreadId: string; upstreamStatus: number; acceptedAt: number; acceptedResponseSha256: string }
|
|
export interface EmailFaultStatus { schema: "paperclip.email-response-loss-status.v1"; executionId: string; attemptId: string; nonce: string; realSendsForwarded: number; sameKeyRetriesBlocked: number; receiptReadsBlocked: number; matchingNotificationsWithheld: number; unrelatedNotificationsForwarded: number; released: boolean; expired: boolean }
|
|
const hash = (bytes: string | Buffer) => createHash("sha256").update(bytes).digest("hex");
|
|
const uuid = (v: string) => /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i.test(v);
|
|
const keys = (value: unknown, expected: string[]) => !!value && typeof value === "object" && Object.keys(value).sort().join() === expected.sort().join();
|
|
export function emailFaultRequestMatches(value: EmailFaultRequest, expected: { executionId: string; attemptId: string }, now = Date.now()) {
|
|
return keys(value, ["schema", "executionId", "attemptId", "companyId", "endpointId", "agentId", "sourceIssueId", "inboxId", "recipient", "subject", "textSha256", "nonce", "createdAt", "expiresAt", "dedicatedDisposableInbox"]) && value.schema === "paperclip.email-response-loss.v1" && value.executionId === expected.executionId && value.executionId.endsWith(`.local.${lostEmailScenario}`) && value.attemptId === expected.attemptId && /^[a-f0-9]{32}$/.test(value.attemptId)
|
|
&& [value.companyId, value.endpointId, value.agentId, value.sourceIssueId, value.nonce].every(uuid) && value.dedicatedDisposableInbox === true && /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value.inboxId) && value.recipient === value.inboxId
|
|
&& value.subject.includes(value.nonce) && value.subject.length <= 200 && !/[\r\n]/.test(value.subject) && /^[a-f0-9]{64}$/.test(value.textSha256) && Number.isSafeInteger(value.createdAt) && Number.isSafeInteger(value.expiresAt) && value.createdAt <= now && now < value.expiresAt && value.expiresAt - value.createdAt <= 480000;
|
|
}
|
|
export function matchesFaultSend(request: EmailFaultRequest, url: URL, init: RequestInit | undefined) {
|
|
if (url.origin !== "https://api.agentmail.to" || url.pathname !== `/v0/inboxes/${encodeURIComponent(request.inboxId)}/messages/send` || url.search || init?.method !== "POST" || typeof init.body !== "string" || Buffer.byteLength(init.body) > 131072) return undefined;
|
|
try {
|
|
const body = JSON.parse(init.body), key = new Headers(init.headers).get("Idempotency-Key"), publication = body.headers?.["X-Paperclip-Publication-Id"];
|
|
return key && uuid(key) && publication === key && JSON.stringify(body.to) === JSON.stringify([request.recipient]) && !body.cc?.length && !body.bcc?.length && !body.attachments?.length && body.subject === request.subject && typeof body.text === "string" && hash(body.text) === request.textSha256 ? key : undefined;
|
|
} catch { return undefined; }
|
|
}
|
|
export function matchesFaultRead(request: EmailFaultRequest, url: URL, method: string) { return url.origin === "https://api.agentmail.to" && method === "GET" && (url.pathname === `/v0/inboxes/${encodeURIComponent(request.inboxId)}/messages` || url.pathname.startsWith(`/v0/inboxes/${encodeURIComponent(request.inboxId)}/messages/`) || url.pathname.startsWith(`/v0/inboxes/${encodeURIComponent(request.inboxId)}/threads/`)); }
|
|
export function faultNotification(value: unknown): { inboxId: string; messageId: string } | undefined {
|
|
const event = value as { event_type?: string; type?: string; message?: unknown; send?: unknown; delivery?: unknown; bounce?: unknown; complaint?: unknown; reject?: unknown };
|
|
const kind = event?.event_type ?? event?.type?.replace(/^message_/, "message."), item = kind === "message.sent" ? event.send ?? event.message : kind === "message.delivered" ? event.delivery ?? event.message : kind === "message.bounced" ? event.bounce ?? event.message : kind === "message.complained" ? event.complaint ?? event.message : kind === "message.rejected" ? event.reject ?? event.message : kind === "message.received" ? event.message : undefined;
|
|
const msg = item as { inbox_id?: unknown; message_id?: unknown } | undefined;
|
|
return typeof msg?.inbox_id === "string" && typeof msg.message_id === "string" ? { inboxId: msg.inbox_id, messageId: msg.message_id } : undefined;
|
|
}
|
|
export function emailFaultDirectory(temporaryRoot: string) { return path.join(temporaryRoot, "email-response-loss"); }
|
|
async function atomic(directory: string, name: string, value: unknown) { const filename = path.join(directory, `${name}.${randomUUID()}.tmp`); await writeFile(filename, JSON.stringify(value), { mode: 0o600, flag: "wx" }); await rename(filename, path.join(directory, name)); }
|
|
function privateRead<T>(filename: string): T {
|
|
const folder = path.dirname(filename), dir = lstatSync(folder), file = lstatSync(filename);
|
|
if (!dir.isDirectory() || dir.mode & 0o077 || dir.uid !== process.getuid?.() || realpathSync(folder) !== folder || !file.isFile() || file.isSymbolicLink() || file.nlink !== 1 || file.uid !== process.getuid?.() || file.mode & 0o077 || file.size > 32768) throw new Error("Email fault private control ownership changed");
|
|
const fd = openSync(filename, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
try { const before = fstatSync(fd); if (before.ino !== file.ino || before.dev !== file.dev) throw new Error("Private email fault control changed before read"); const bytes = readFileSync(fd), after = fstatSync(fd), named = lstatSync(filename); if (before.size !== after.size || before.mtimeMs !== after.mtimeMs || named.ino !== before.ino || named.dev !== before.dev) throw new Error("Private email fault control changed during read"); return JSON.parse(bytes.toString("utf8")) as T; } finally { closeSync(fd); }
|
|
}
|
|
/** Called ONLY by server-entry before production service construction. Other
|
|
* executions install no wrapper, timer, socket hook or private control files. */
|
|
export async function installOwnedEmailResponseLoss(input: { executionIds: string[]; attemptId: string; temporaryRoot: string }) {
|
|
if (!input.executionIds.some(id => id.endsWith(`.local.${lostEmailScenario}`))) return undefined;
|
|
if (input.executionIds.length !== 1 || !/^[a-f0-9]{32}$/.test(input.attemptId) || !path.isAbsolute(input.temporaryRoot) || realpathSync(input.temporaryRoot) !== input.temporaryRoot) throw new Error("Email transport loss requires its exact selected owned attempt");
|
|
const directory = emailFaultDirectory(input.temporaryRoot); await mkdir(directory, { mode: 0o700 });
|
|
const WebSocket = createRequire(new URL("../../../server/package.json", import.meta.url))("ws") as { prototype: EventEmitter };
|
|
const originalFetch = globalThis.fetch, originalEmit = WebSocket.prototype.emit;
|
|
let request: EmailFaultRequest | undefined, accepted: AcceptedEmailFault | undefined, publicationId: string | undefined, inflight: Promise<void> | undefined, settle: (() => void) | undefined;
|
|
let status: EmailFaultStatus | undefined, closed = false, pendingNotifications = 0, write = Promise.resolve();
|
|
const save = () => { if (status) { const snapshot = { ...status }; write = write.then(() => atomic(directory, "status.json", snapshot)); } };
|
|
const load = () => { if (request || closed) return request; try { const value = privateRead<EmailFaultRequest>(path.join(directory, "request.json")); if (!emailFaultRequestMatches(value, { executionId: input.executionIds[0]!, attemptId: input.attemptId })) throw new Error("Email network fault request binding is stale or undeclared"); request = value; status = { schema: "paperclip.email-response-loss-status.v1", executionId: value.executionId, attemptId: value.attemptId, nonce: value.nonce, realSendsForwarded: 0, sameKeyRetriesBlocked: 0, receiptReadsBlocked: 0, matchingNotificationsWithheld: 0, unrelatedNotificationsForwarded: 0, released: false, expired: false }; save(); return request; } catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined; throw error; } };
|
|
const release = async (expired = false) => { if (closed) return; closed = true; if (status) { status.released = true; status.expired = expired; save(); } if (globalThis.fetch === wrappedFetch) globalThis.fetch = originalFetch; if (WebSocket.prototype.emit === wrappedEmit) WebSocket.prototype.emit = originalEmit; clearInterval(timer); settle?.(); await write; await atomic(directory, "closed.json", { executionId: input.executionIds[0], attemptId: input.attemptId, nonce: request?.nonce ?? null, released: true, expired }); };
|
|
const wrappedFetch: typeof fetch = async (target, init) => {
|
|
const req = load(); if (!req || closed) return originalFetch(target, init);
|
|
if (Date.now() >= req.expiresAt) { await release(true); return originalFetch(target, init); }
|
|
const url = new URL(typeof target === "string" ? target : target instanceof URL ? target.href : target.url), method = init?.method ?? (target instanceof Request ? target.method : "GET"), candidate = matchesFaultSend(req, url, init);
|
|
if (matchesFaultRead(req, url, method) && publicationId) { status!.receiptReadsBlocked++; save(); await write; throw new TypeError("Selected inbox receipt transport unavailable during the declared bounded outage"); }
|
|
if (!candidate || publicationId && candidate !== publicationId) return originalFetch(target, init);
|
|
if (publicationId) { status!.sameKeyRetriesBlocked++; save(); await write; throw new TypeError("The original persisted send response remains unavailable during the bounded outage"); }
|
|
publicationId = candidate; inflight = new Promise(resolve => { settle = resolve; }); status!.realSendsForwarded++; save();
|
|
try {
|
|
const response = await originalFetch(target, init);
|
|
if (!response.ok) return response;
|
|
const reader = response.clone().body?.getReader(); if (!reader) throw new Error("Actual accepted provider response has no receipt body");
|
|
const chunks: Uint8Array[] = []; let size = 0;
|
|
try { while (true) { const next = await reader.read(); if (next.done) break; size += next.value.byteLength; if (size > 32768) throw new Error("Accepted AgentMail response exceeded the bounded private receipt"); chunks.push(next.value); } } finally { await reader.cancel(); }
|
|
const bytes = Buffer.concat(chunks);
|
|
const body = JSON.parse(Buffer.from(bytes).toString("utf8")) as { message_id?: unknown; thread_id?: unknown };
|
|
if (typeof body.message_id !== "string" || typeof body.thread_id !== "string" || !body.message_id || !body.thread_id || body.message_id.length > 998 || body.thread_id.length > 998) throw new Error("Real provider accepted send without a valid actual message/thread receipt");
|
|
accepted = { schema: "paperclip.email-response-loss-accepted.v1", executionId: req.executionId, attemptId: req.attemptId, nonce: req.nonce, publicationId, providerMessageId: body.message_id, providerThreadId: body.thread_id, upstreamStatus: response.status, acceptedAt: Date.now(), acceptedResponseSha256: hash(Buffer.from(bytes)) };
|
|
await atomic(directory, "accepted-private.json", accepted); await write;
|
|
throw new TypeError("The actual accepted send response was lost in the declared selected-inbox network outage");
|
|
} finally { settle?.(); }
|
|
};
|
|
const wrappedEmit: EventEmitter["emit"] = function (this: EventEmitter, event, ...args) {
|
|
const req = request;
|
|
if (event !== "message" || !req || closed || String((this as EventEmitter & { url?: string }).url) !== "wss://ws.agentmail.to/v0") return originalEmit.call(this, event, ...args);
|
|
const data = args[0]; if (!Buffer.isBuffer(data) && typeof data !== "string" || Buffer.byteLength(data as string | Buffer) > 131072) return originalEmit.call(this, event, ...args);
|
|
let notification: ReturnType<typeof faultNotification>; try { notification = faultNotification(JSON.parse(String(data))); } catch { return originalEmit.call(this, event, ...args); }
|
|
if (notification?.inboxId !== req.inboxId || !publicationId) return originalEmit.call(this, event, ...args);
|
|
const deliver = () => { if (!closed && accepted?.providerMessageId === notification!.messageId) { status!.matchingNotificationsWithheld++; save(); return; } status!.unrelatedNotificationsForwarded++; save(); originalEmit.call(this, event, ...args); };
|
|
if (!accepted && inflight) { if (++pendingNotifications > 128) { void release(true); return originalEmit.call(this, event, ...args); } void inflight.then(() => { pendingNotifications--; deliver(); }); return true; }
|
|
if (accepted?.providerMessageId === notification.messageId) { status!.matchingNotificationsWithheld++; save(); return true; }
|
|
return originalEmit.call(this, event, ...args);
|
|
};
|
|
globalThis.fetch = wrappedFetch; WebSocket.prototype.emit = wrappedEmit;
|
|
await atomic(directory, "installed.json", { schema: "paperclip.email-response-loss-installed.v1", executionId: input.executionIds[0], attemptId: input.attemptId, pid: process.pid });
|
|
const timer = setInterval(() => { void (async () => { const req = load(); if (closed || !req) return; let released: { executionId?: string; attemptId?: string; nonce?: string; release?: boolean } | undefined; try { released = privateRead(path.join(directory, "release.json")); } catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; } if (released) { if (!keys(released, ["executionId", "attemptId", "nonce", "release"]) || released.executionId !== req.executionId || released.attemptId !== req.attemptId || released.nonce !== req.nonce || released.release !== true) throw new Error("Email outage release does not bind this exact action"); await release(); } else if (Date.now() >= req.expiresAt) await release(true); })().catch(() => { void release(true); }); }, 200); timer.unref();
|
|
return { directory, close: release };
|
|
}
|
|
export async function armOwnedEmailResponseLoss(temporaryRoot: string, request: EmailFaultRequest) { if (!emailFaultRequestMatches(request, request)) throw new Error("Cannot arm an undeclared or unsafe email outage"); const directory = emailFaultDirectory(temporaryRoot); const info = lstatSync(directory); if (!info.isDirectory() || info.mode & 0o077 || info.uid !== process.getuid?.() || realpathSync(directory) !== directory) throw new Error("Selected test-server email fault hook is not installed in its owned private directory"); await atomic(directory, "request.json", request); }
|
|
export async function readOwnedEmailFault<T>(temporaryRoot: string, name: "accepted-private.json" | "status.json" | "closed.json" | "installed.json") { return privateRead<T>(path.join(emailFaultDirectory(temporaryRoot), name)); }
|
|
export async function releaseOwnedEmailResponseLoss(temporaryRoot: string, request: EmailFaultRequest) { await atomic(emailFaultDirectory(temporaryRoot), "release.json", { executionId: request.executionId, attemptId: request.attemptId, nonce: request.nonce, release: true }); const deadline = Date.now() + 10000; while (Date.now() < deadline) { try { const result = await readOwnedEmailFault<{ executionId: string; attemptId: string; nonce: string; released: boolean; expired: boolean }>(temporaryRoot, "closed.json"); if (result.executionId !== request.executionId || result.attemptId !== request.attemptId || result.nonce !== request.nonce || result.released !== true || result.expired !== false) throw new Error("The exact email fault did not release cleanly within its declared phase"); return; } catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; } await new Promise(resolve => setTimeout(resolve, 100)); } throw new Error("The exact owned email outage release was not confirmed"); }
|