Files
PaperClipAI/tests/runner-e2e/chat-catalog-domains/internal-routine-audit.ts
T
DottaandClaude Opus 5.5 f7a3497e9d fix(evals): compare browser zones by clock, wait for the reloaded profile name, bind accounting reads by returned run (#21760)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The chat-feature eval catalog runs real agent conversations and
grades the saved outcome
> - Chat-eval cohort 16 (OpenCode with a DeepSeek model) had three
failures that were not product failures of the chat agent
> - One setup check compared time zone names, one observer read a form
before it filled, and one grader accepted only exact query digests
> - An eval that fails valid behavior hides real regressions behind
noise, so each check must grade the outcome, not an incidental detail
> - This pull request fixes the three checks, and it fixes a first-frame
empty display name on the Profile page
> - The benefit is that these three cases now fail only for the behavior
they test

## Linked Issues or Issue Description

**What happened?**
Three chat-eval cases failed in cohort 16 (OpenCode,
`openrouter/deepseek/deepseek-v4.1-flash-1017`) on correct behavior. (1)
`activity.routine-audit`: setup threw "Browser timezone override did not
apply (America/Buenos_Aires)". At 11:00-11:59 UTC the picker selects
`America/Argentina/Buenos_Aires`, and Chrome reports that zone by its
alias `America/Buenos_Aires`. The check compared names. (2)
`navigation-preferences.edit-profile`: the name saved correctly, but the
observer read the display-name field once, right after reload, and got
an empty value. (3) `budgets-costs.spend-breakdown`: the answer was
correct, but the grader accepted company cost reports only for
`period=all` or `period=month`. The agent read `costs/by-agent` and
`costs/summary` for the requested week, and that range covers the
unpriced run.

**Expected behavior**
Each case passes when the product and the agent do the requested work.
Each case fails when they do not.

**Steps to reproduce**

1. Run `chat-features.runner-opencode.local.activity.routine-audit`
between 11:00 and 11:59 UTC.
2. Run
`chat-features.runner-opencode.local.navigation-preferences.edit-profile`
and look at `profileAfterReload`.
3. Grade the retained cohort 16 observation of
`budgets-costs.spend-breakdown` with the old grader.

**Additional context**
The run tool inputs are saved only as SHA-256 digests, so a grader binds
a read by recomputing the digest of inputs it can check. For the cohort
16 spend-breakdown reads, the inputs were recovered this way:
`from=2026-11-09T00:00:00.000Z`, `to=2026-11-16T00:00:00.000Z`.

## What Changed

- `internal-routine-audit.ts`: the browser override check compares the
UTC offset of the requested and reported zones, now and 182 days later,
instead of the zone names. An unknown zone or a different clock still
fails setup.
- `identity-profile.ts`: after reload, `settledFieldValue` waits up to
15 s for the display name to reach the saved value, then records what
the person sees. The grade still fails a name that does not arrive.
- `ProfileSettings.tsx`: unedited fields show the session's saved name
and image from the first frame. Before, a session restored from the
saved read cache rendered an empty name until an effect filled it.
- `identity-accounting.ts`: `readSavedAccounting` also accepts a company
cost report whose checked `from`/`to` covers the unpriced run's cost
event. The run log keeps only the input digest, so the grader recomputes
digests for covering ranges: UTC day boundaries (with or without
milliseconds or seconds, date-only, end of day), local midnights at each
whole-hour offset, and the run's own `startedAt` and cost-event
instants. A pending run (no cost event) counts only through
`costs/summary`, `costs/data-quality` or its own run record, because the
other reports sum cost events. A link to the run does not count, because
the run id is in the task description. The run-log redaction is not
changed.
- Oracle versions bumped: `routine-audit.v7`,
`spend-breakdown.structural.v6`, `identity-profile.v4`.

## Verification

- Red/green: each new test fails on the old code and passes on the new
code.
- `internal-remaining.test.ts`: every candidate zone, reported by its
own name, its ICU name and known browser aliases, passes at every hour
in January and July. A different clock, a zone with daylight saving time
and an unknown name fail.
- `identity-personal.test.ts`: the settled read returns the saved name
after empty reads, and returns a wrong name when the saved name never
arrives.
- `ProfileSettings.test.tsx`: the first rendered frame with a cached
session shows "Alex Chen", not `""`.
- `identity-accounting.test.ts`: the cohort 16 week-range reads pass, as
do other covering ranges. Negative controls fail: no reads, run-list
reads with unchecked inputs, ranges that exclude the run, another
company, a denied read, a read by another run, and a read of a different
run record with a link to the target run.
- The retained cohort 16 spend-breakdown observation passes the new
grader, and fails with its reads removed.
- `pnpm test:e2e:runner:unit`: 686 files passed. `pnpm
test:e2e:runner:typecheck` passed. `ui` ProfileSettings tests passed.
`scripts/check-token-gates.mjs` is clean.
- One narrow paid run of the three cases on this branch (result in a
comment below).

## Risks

- Low risk. The changes are in eval code, plus a small Profile page
state change.
- A covering range in a form outside the recomputed set (for example
08:00 at -03:00) is not recognized. That is a false negative, not a
false pass.
- The Profile page now keeps an unedited field in step with the session.
A refetch still resets drafts, as before.

## Model Used

- Claude, model ID `claude-opus-5-5`, used through Claude Code with tool
use and code execution.

## 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>
2026-11-15 22:05:47 -06:00

196 lines
18 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;
}
/** Minutes the zone's wall clock is ahead of UTC at the instant. */
export function utcOffsetMinutes(instant: Date, timezone: string) {
const parts = Object.fromEntries(new Intl.DateTimeFormat("en-US", { timeZone: timezone, year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", hourCycle: "h23" })
.formatToParts(instant).map(part => [part.type, part.value]));
const wall = Date.UTC(Number(parts.year), Number(parts.month) - 1, Number(parts.day), Number(parts.hour), Number(parts.minute));
return Math.round((wall - Math.floor(instant.getTime() / 60_000) * 60_000) / 60_000);
}
/**
* Whether the browser's reported zone keeps the same clock as the requested one. Browsers report a zone by
* their own canonical name, which may be an alias (Chrome reports America/Argentina/Buenos_Aires as
* America/Buenos_Aires), so names are not compared: the UTC offsets must agree now and half a year away.
*/
export function sameZoneClock(reported: string, requested: string, now: Date) {
const later = new Date(now.getTime() + 182 * 86_400_000);
try { return [now, later].every(instant => utcOffsetMinutes(instant, reported) === utcOffsetMinutes(instant, requested)); } catch { return false; }
}
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 (!sameZoneClock(browserTimezone, timezone, new Date())) throw new Error(`Browser timezone override did not apply (requested ${timezone}, browser reports ${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.v7", 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()];