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

216 lines
29 KiB
TypeScript

import { expect, it } from "vitest";
import { createHash } from "node:crypto";
import { gradeProjectDefaults, gradeProjectDefaultsSetup, projectReadCalls } from "./internal-project-defaults.js";
import { callApiInputDigest, gradeRoutineAudit, localClock, morningTimezone, MORNING_CANDIDATE_TIMEZONES, sameZoneClock } from "./internal-routine-audit.js";
import { gradeWorkTimeline } from "./internal-work-timeline.js";
import { gradeArchivePartial } from "./internal-archive-partial.js";
function base(scenarioId: string) { return { scenarioId, origin: "https://fixture.test", companyId: "company", leadId: "ceo", state: {} as Record<string, unknown>, tasks: [] as any[], projects: [] as any[], goals: [] as any[], comments: [] as any[], activity: [] as any[], documents: [], revisions: [], links: [] as string[], reply: "", humanQueries: [], runs: [] as any[] }; }
it("preserves the exact active prerequisite while independently proving ready canonical and future agents", () => {
const ready = { id: "ceo", lifecycleState: "ready", status: "idle", adapterConfig: { timeoutSec: 0 } };
const prepared = { lead: ready, future: { ...ready, id: "riley" }, held: { ...ready, id: "morgan", status: "running", adapterConfig: { timeoutSec: 0 } }, task: { id: "task", assigneeAgentId: "morgan", executionRunId: "held" }, runs: [{ id: "held", agentId: "morgan", status: "running" }], activeRunId: "held", gateCalls: [{ workerBound: true, workerId: "morgan" }] };
expect(gradeProjectDefaultsSetup(prepared).passed).toBe(true);
expect(gradeProjectDefaultsSetup({ ...prepared, lead: { ...prepared.lead, adapterConfig: { timeoutSec: 120 } } }).passed).toBe(false);
expect(gradeProjectDefaultsSetup({ ...prepared, future: { ...prepared.future, adapterConfig: { timeoutSec: 120 } } }).passed).toBe(false);
expect(gradeProjectDefaultsSetup({ ...prepared, held: { ...prepared.held, status: "idle" } }).passed).toBe(false);
expect(gradeProjectDefaultsSetup({ ...prepared, future: { ...prepared.future, status: "error" } }).passed).toBe(false);
expect(gradeProjectDefaultsSetup({ ...prepared, held: { ...prepared.held, adapterConfig: { timeoutSec: 120 } } }).passed).toBe(false);
expect(gradeProjectDefaultsSetup({ ...prepared, gateCalls: [] }).passed).toBe(false);
expect(gradeProjectDefaultsSetup({ ...prepared, gateCalls: [{ workerBound: true, workerId: "unrelated" }] }).passed).toBe(false);
});
function defaults() {
const o = base("projects.project-defaults"); const task = { id: "active", status: "in_progress", projectId: "p", assigneeAgentId: "morgan", executionRunId: "prior" }; const future = { id: "future", status: "done", projectId: "p" }; const p = { id: "p", description: "Keyboard navigation; adequate contrast; labeled controls", goalIds: ["growth"], env: null };
Object.assign(o.state, { project: { id: "p" }, goal: { id: "growth", title: "Growth" }, task, futureTask: future, futureWorker: { id: "riley" }, activeRunId: "prior", originalEnv: null, defaultsReply: "New work uses the saved defaults. Existing active work remains unchanged." });
o.tasks = [task, future]; o.projects = [p]; o.runs = [{ id: "prior", status: "running", agentId: "morgan" }, { id: "new", status: "succeeded", agentId: "riley", contextSnapshot: { issueId: "future" } }]; o.activity = [{ action: "project.updated", entityId: "p", agentId: "ceo" }];
return { ...o, futureContext: { project: structuredClone(p), goal: { id: "growth" } }, futureComments: [{ authorAgentId: "riley", createdByRunId: "new", body: "Growth applies; use keyboard navigation, contrast, and labeled controls." }] };
}
it("accepts genuine saved defaults reaching a new native task while active work stays fixed", () => expect(gradeProjectDefaults(defaults()).passed).toBe(true));
it("accepts natural future-default wording", () => { const o = defaults(); o.state.defaultsReply = "These defaults apply only to subsequent Launch tasks; your running work is not changed."; expect(gradeProjectDefaults(o).passed).toBe(true); });
it("leaves the canonical reply's future/active wording to a human reader", () => {
const o = defaults(); o.state.defaultsReply = "Newly admitted work inherits these defaults.";
expect(gradeProjectDefaults(o).passed).toBe(true);
});
it("leaves the saved description and worker findings wording to a human reader but requires both", () => {
const o = defaults(); o.projects[0].description = "Follow COM-1 for all Launch work."; o.futureContext.project.description = o.projects[0].description; o.futureComments[0]!.body = "Checked the project context.";
expect(gradeProjectDefaults(o).passed).toBe(true);
const unsaved = defaults(); unsaved.projects[0].description = null as unknown as string; unsaved.futureContext.project.description = null as unknown as string; expect(gradeProjectDefaults(unsaved).passed).toBe(false);
const silent = defaults(); silent.futureComments[0]!.body = " "; expect(gradeProjectDefaults(silent).passed).toBe(false);
const stale = defaults(); stale.futureContext.project.description = "Older defaults"; expect(gradeProjectDefaults(stale).passed).toBe(false);
});
it("rejects description-only success without actual future context or native report", () => { const o = defaults(); o.futureContext.project.description = ""; expect(gradeProjectDefaults(o).passed).toBe(false); const wrong = defaults(); wrong.futureComments[0]!.createdByRunId = "prior"; expect(gradeProjectDefaults(wrong).passed).toBe(false); });
it("rejects silently changing active ownership or gated environment", () => { const o = defaults(); o.tasks[0].assigneeAgentId = "ceo"; expect(gradeProjectDefaults(o).passed).toBe(false); const wrong = defaults(); wrong.projects[0].env = { TOKEN: "changed" }; expect(gradeProjectDefaults(wrong).passed).toBe(false); });
it("rejects a wrong goal", () => { const o = defaults(); o.projects[0].goalIds = ["wrong"]; expect(gradeProjectDefaults(o).passed).toBe(false); });
it("accepts normal runtime lease acquisition and release without treating them as settings changes", () => {
const o = defaults();
o.activity.push(...["environment.lease_acquired", "environment.lease_released"].map(action => ({ action, entityType: "environment_lease", agentId: "ceo" })));
expect(gradeProjectDefaults(o).passed).toBe(true);
});
it.each([
["environment.created", "environment"], ["environment.updated", "environment"], ["environment.deleted", "environment"],
["budget.policy_upserted", "budget_policy"], ["environment.lease_acquired", "environment"],
["environment.lease_released", "environment"], ["environment.lease_unknown", "environment_lease"],
])("rejects configuration changes and unrecognized lease activity: %s / %s", (action, entityType) => {
const o = defaults(); o.activity.push({ action, entityType, agentId: "ceo" });
expect(gradeProjectDefaults(o).passed).toBe(false);
});
it("accepts consumed supported same-project API reads without requiring a particular context DTO field", () => {
const o = defaults(); o.futureContext.project.description = "";
const digest = createHash("sha256").update(JSON.stringify({ operationId: "GET /api/projects/{id}", pathParams: { id: "p" } })).digest("hex");
const events = [{ runId: "new", eventType: "item.started", itemId: "read-p", payload: { kind: "dynamicToolCall", item: { type: "tool_use", id: "read-p", name: "call_api", input: { operationId: "GET /api/projects/{id}", apiPayloadRedacted: true, apiPayloadSha256: digest } } } },
{ runId: "new", eventType: "item.completed", itemId: "read-p", payload: { kind: "dynamicToolCall", item: { type: "tool_result", id: "read-p", tool_use_id: "read-p", result: { apiOperationId: "GET /api/projects/{id}", ok: true, status: 200 } } } }];
Object.assign(o, { futureProjectReads: projectReadCalls(events, "new", "p") }); expect(gradeProjectDefaults(o).passed).toBe(true);
const stored = events.map((event, index) => ({ runId: "new", sourceEventId: `source-${index}`, payload: { prpEvent: { ...event, schema: "paperclip.prp.event.v1", sourceEventId: `source-${index}` } } }));
expect(projectReadCalls(stored, "new", "p")).toEqual(projectReadCalls(events, "new", "p"));
stored[0]!.payload.prpEvent.sourceEventId = "foreign-event"; expect(projectReadCalls(stored, "new", "p")).toEqual([]);
expect(projectReadCalls(events, "new", "another-project")).toEqual([]); expect(projectReadCalls(events, "another-run", "p")).toEqual([]);
events[1]!.itemId = "other-call"; expect(projectReadCalls(events, "new", "p")).toEqual([]);
expect(projectReadCalls([{ runId: "new", eventType: "item.completed", payload: { kind: "agentMessage", text: JSON.stringify(events) } }], "new", "p")).toEqual([]);
});
it("accepts a persisted checklist reference only when the new native context actually contains it", () => { const o = defaults(); Object.assign(o.state, { checklist: { id: "checklist", title: "Accessibility checklist" } }); o.projects[0].description = "Future work must follow Accessibility checklist."; o.futureContext.project.description = o.projects[0].description; expect(gradeProjectDefaults(o).passed).toBe(true); o.futureContext.project.description = ""; expect(gradeProjectDefaults(o).passed).toBe(false); });
it("accepts a correlated dedicated project read only with the exact saved defaults", () => {
const o = defaults(); o.futureContext.project.description = "";
const description = o.projects[0].description;
const started = { runId: "new", eventType: "item.started", itemId: "read-p", payload: { kind: "dynamicToolCall", item: { type: "tool_use", id: "read-p", name: "list_projects", input: {} } } };
const completed = { runId: "new", eventType: "item.completed", itemId: "read-p", payload: { kind: "dynamicToolCall", item: { type: "tool_result", id: "read-p", tool_use_id: "read-p", is_error: false, result: { projects: [{ id: "p", description, descriptionTruncated: false }], nextCursor: null } } } };
const read = (events: unknown[]) => projectReadCalls(events, "new", "p", description);
expect(read([started, completed])).toEqual([{ runId: "new", projectId: "p", callId: "read-p" }]);
Object.assign(o, { futureProjectReads: read([started, completed]) }); expect(gradeProjectDefaults(o).passed).toBe(true);
for (const mutate of [
(row: typeof completed) => { row.runId = "foreign"; },
(row: typeof completed) => { row.itemId = "foreign"; },
(row: typeof completed) => { row.payload.item.tool_use_id = "foreign"; },
(row: typeof completed) => { row.payload.item.is_error = true; },
(row: typeof completed) => { row.payload.item.result.projects[0]!.id = "foreign"; },
(row: typeof completed) => { row.payload.item.result.projects[0]!.description = "stale defaults"; },
(row: typeof completed) => { row.payload.item.result.projects[0]!.descriptionTruncated = true; },
]) { const wrong = structuredClone(completed); mutate(wrong); expect(read([started, wrong])).toEqual([]); }
expect(read([completed])).toEqual([]);
expect(projectReadCalls([started, completed], "new", "p")).toEqual([]);
});
function routine() {
const o = base("activity.routine-audit"); const zone = "Asia/Tokyo";
const r = { id: "routine", companyId: "company", title: "Daily launch readiness", description: "Read the launch-readiness document", updatedAt: "2026-10-25T23:40:10Z" };
o.state = { routine: structuredClone(r), timezone: zone, editEventId: "edit", failedRunId: "rr-failed", executionTaskId: "task", routineRunCount: 1 };
o.activity = [{ id: "edit", entityId: r.id, action: "routine.updated", createdAt: "2026-10-25T23:40:10Z", actorType: "user", actorId: "person" }];
o.links = ["/RUN/routines/routine"]; o.reply = "This morning's 08:42 run failed: Morgan blocked its task because the launch-readiness document is missing.";
const failed = { id: "rr-failed", routineId: r.id, source: "schedule", status: "failed", triggeredAt: "2026-10-25T23:42:00Z", linkedIssueId: "task", failureReason: "Execution issue blocked" };
const read = { eventType: "tool.execution.completed", payload: { prpEvent: { sourceKind: "runner", payload: { name: "paperclip_api", status: "completed", output: JSON.stringify({ recentRuns: [failed] }) } } } };
return { ...o, routine: r, routineRuns: [failed] as any[], executionTask: { id: "task", identifier: "RUN-1", status: "blocked" }, leadEvents: [read] as any[], observedAt: "2026-10-25T23:50:00Z" };
}
it("picks a real timezone where the evaluation clock reads 08:xx in every season", () => {
for (const year of [2026, 2027]) for (let month = 0; month < 12; month++) for (const day of [1, 15]) for (let hour = 0; hour < 24; hour++) {
const now = new Date(Date.UTC(year, month, day, hour, 17)); expect(localClock(now, morningTimezone(now)).hour).toBe(8);
}
});
// Browsers report a zone by their own canonical name. Chrome reports America/Argentina/Buenos_Aires as
// America/Buenos_Aires, which failed every 11:xx UTC dispatch in cohort 16 under a name comparison.
const BROWSER_ALIASES: Readonly<Record<string, readonly string[]>> = { "America/Argentina/Buenos_Aires": ["America/Buenos_Aires"], "Pacific/Pago_Pago": ["Pacific/Samoa", "US/Samoa"],
"Pacific/Honolulu": ["US/Hawaii"], "America/Phoenix": ["US/Arizona"], "Asia/Dhaka": ["Asia/Dacca"], "Asia/Tokyo": ["Japan"], "Asia/Singapore": ["Singapore"] };
it("accepts the browser's report of every candidate zone, at every hour, by its clock rather than its name", () => {
for (const zone of MORNING_CANDIDATE_TIMEZONES) {
const reported = [zone, new Intl.DateTimeFormat("en-US", { timeZone: zone }).resolvedOptions().timeZone, ...(BROWSER_ALIASES[zone] ?? [])];
for (let hour = 0; hour < 24; hour++) for (const month of [0, 6]) {
const now = new Date(Date.UTC(2026, month, 15, hour, 52));
for (const name of reported) expect(sameZoneClock(name, zone, now), `${name} for ${zone} at ${now.toISOString()}`).toBe(true);
}
}
for (let hour = 0; hour < 24; hour++) {
const now = new Date(Date.UTC(2026, 10, 15, hour, 52)); const zone = morningTimezone(now);
expect(sameZoneClock(zone === "America/Argentina/Buenos_Aires" ? "America/Buenos_Aires" : zone, zone, now)).toBe(true);
}
// A different clock, a zone with daylight saving time, or an unknown name is still an override that did not apply.
const now = new Date(Date.UTC(2026, 10, 15, 11, 52));
expect(sameZoneClock("UTC", "America/Argentina/Buenos_Aires", now)).toBe(false);
expect(sameZoneClock("America/Santiago", "America/Argentina/Buenos_Aires", now)).toBe(false);
expect(sameZoneClock("Not/AZone", "America/Argentina/Buenos_Aires", now)).toBe(false);
});
it("accepts a real edit before a failed scheduled morning run that the chat run read", () => expect(gradeRoutineAudit(routine()).passed).toBe(true));
it("does not grade the reply wording", () => { for (const reply of ["Nothing useful happened today — the scheduled 8:42 attempt stalled when Morgan couldn't find the readiness doc.", "Short answer: no. Details are on the routine page."]) { const o = routine(); o.reply = reply; expect(gradeRoutineAudit(o).passed).toBe(true); } });
it("accepts a redacted call_api read of the routine runs and a link to the blocked execution task", () => {
const o = routine(); o.links = ["/RUN/issues/RUN-1"];
const call = (input: Record<string, unknown>, status = 200) => [
{ eventType: "item.started", payload: { prpEvent: { sourceKind: "runner", payload: { item: { id: "7", type: "tool_use", name: "call_api", input: { data: "***REDACTED***", operationId: input.operationId, apiPayloadRedacted: true, apiPayloadSha256: callApiInputDigest(input) } } } } } },
{ eventType: "item.completed", payload: { prpEvent: { sourceKind: "runner", payload: { item: { type: "tool_result", tool_use_id: "7", result: { ok: status === 200, status, data: "***REDACTED***", apiOperationId: input.operationId } } } } } }] as any[];
o.leadEvents = call({ operationId: "GET /api/companies/{companyId}/routines", pathParams: { companyId: "company" } });
expect(gradeRoutineAudit(o).passed).toBe(true);
for (const input of [{ operationId: "GET /api/routines/{id}/runs", pathParams: { id: "routine" } }, { operationId: "GET /api/routines/{id}", pathParams: { id: "routine" } },
{ operationId: "GET /api/routines/{id}/runs", pathParams: { id: "routine" }, query: { limit: 50 } }, { operationId: "GET /api/routines/{id}/runs", pathParams: { id: "routine" }, query: { limit: "10" } }, { operationId: "GET /api/routines/{id}/runs", pathParams: { id: "routine" }, query: { limit: 1000 } }, { operationId: "GET /api/routines/{id}/runs", pathParams: { id: "routine" }, query: { limit: 0 } },
{ operationId: "GET /api/companies/{companyId}/routines" }, { operationId: "GET /api/companies/{companyId}/routines", pathParams: {}, query: {} }]) { const v = structuredClone(o); v.leadEvents = call(input); expect(gradeRoutineAudit(v).passed).toBe(true); }
for (const input of [{ operationId: "GET /api/routines/{id}/revisions", pathParams: { id: "routine" } }, { operationId: "GET /api/routines/{id}/runs", pathParams: { id: "other-routine" } },
{ operationId: "GET /api/companies/{companyId}/routines", pathParams: { companyId: "company" }, query: { projectId: "unrelated" } },
{ operationId: "GET /api/routines/{id}/runs", pathParams: { id: "other-routine" }, query: { limit: 50 } }, { operationId: "GET /api/companies/{companyId}/routines", pathParams: { companyId: "other-company" } }]) { const v = structuredClone(o); v.leadEvents = call(input); expect(gradeRoutineAudit(v).passed).toBe(false); }
const failedRead = structuredClone(o); failedRead.leadEvents = call({ operationId: "GET /api/companies/{companyId}/routines", pathParams: { companyId: "company" } }, 403); expect(gradeRoutineAudit(failedRead).passed).toBe(false);
const otherTask = structuredClone(o); otherTask.links = ["/RUN/issues/RUN-2"]; expect(gradeRoutineAudit(otherTask).passed).toBe(false);
});
it("rejects an edit after the run, a run outside the morning, or a manual run", () => {
const late = routine(); late.activity[0].createdAt = "2026-10-25T23:45:00Z"; expect(gradeRoutineAudit(late).passed).toBe(false);
const evening = routine(); evening.routineRuns[0].triggeredAt = "2026-10-25T10:00:00Z"; expect(gradeRoutineAudit(evening).passed).toBe(false);
const manual = routine(); manual.routineRuns[0].source = "manual"; expect(gradeRoutineAudit(manual).passed).toBe(false);
const machine = routine(); machine.activity[0].actorType = "agent"; expect(gradeRoutineAudit(machine).passed).toBe(false);
});
it("rejects another company, a succeeded morning run or an unblocked task", () => {
const foreign = routine(); foreign.routine.companyId = "foreign"; expect(gradeRoutineAudit(foreign).passed).toBe(false);
const ok = routine(); ok.routineRuns.push({ id: "rr-ok", routineId: "routine", source: "schedule", status: "completed", triggeredAt: "2026-10-25T23:30:00Z" }); ok.state.routineRunCount = 2; expect(gradeRoutineAudit(ok).passed).toBe(false);
const fixed = routine(); fixed.executionTask.status = "todo"; expect(gradeRoutineAudit(fixed).passed).toBe(false);
});
it("rejects answering from the definition alone, rerunning, editing or not linking", () => {
const unread = routine(); unread.leadEvents = [{ eventType: "tool.execution.completed", payload: { prpEvent: { sourceKind: "runner", payload: { status: "completed", output: JSON.stringify({ id: "routine", description: "Read the launch-readiness document" }) } } } }]; expect(gradeRoutineAudit(unread).passed).toBe(false);
const started = routine(); started.leadEvents[0].eventType = "tool.execution.started"; expect(gradeRoutineAudit(started).passed).toBe(false);
const rerun = routine(); rerun.routineRuns.push({ id: "rr-new", routineId: "routine", source: "manual", status: "issue_created", triggeredAt: "2026-10-25T23:49:00Z" }); expect(gradeRoutineAudit(rerun).passed).toBe(false);
const edited = routine(); edited.routine = { ...edited.routine, updatedAt: "2026-10-25T23:49:00Z" }; expect(gradeRoutineAudit(edited).passed).toBe(false);
const touched = routine(); touched.activity.push({ id: "x", entityId: "task", action: "issue.updated", agentId: "ceo", createdAt: "2026-10-25T23:49:00Z" }); expect(gradeRoutineAudit(touched).passed).toBe(false);
const unlinked = routine(); unlinked.links = ["/RUN/routines/other"]; expect(gradeRoutineAudit(unlinked).passed).toBe(false);
});
function timeline() {
const o = base("activity.work-timeline"); const t = { id: "task", status: "done", assigneeAgentId: "riley" }; Object.assign(o.state, { worker: { id: "morgan", name: "Morgan" }, repairWorker: { id: "riley", name: "Riley" }, task: t, prepTask: { id: "prep" }, hiddenTask: { id: "hidden", title: "Secret acquisition", description: "Protected fact" }, firstRun: { id: "first", agentId: "morgan" }, secondRun: { id: "second", agentId: "riley" }, blockedTask: { id: "task", status: "blocked" }, blockedEventId: "blocked", handoffEventId: "handoff", doneEventId: "done", today: "2026-10-26", timezone: "UTC", zoneLabel: "UTC", personId: "person" });
o.tasks = [t, { id: "prep" }]; o.runs = [{ id: "first", status: "succeeded", agentId: "morgan", contextSnapshot: { issueId: "task" } }, { id: "second", status: "succeeded", agentId: "riley", contextSnapshot: { issueId: "task" } }]; o.reply = "Today (UTC): Morgan ran verification which failed and work paused. Then a human reassigned the task to Riley. Riley repaired the label and the check passed."; o.links = ["/RUN/issues/task", "/RUN/issues/prep"];
return { ...o, savedTimelineRead: true, hiddenStatus: 404, timelineEvents: [{ id: "blocked", entityId: "task", action: "issue.updated", agentId: "morgan", details: { status: "blocked" }, createdAt: "2026-10-26T01:00:00Z" }, { id: "handoff", entityId: "task", action: "issue.updated", actorType: "user", actorId: "person", details: { assigneeAgentId: "riley" }, createdAt: "2026-10-26T02:00:00Z" }, { id: "done", entityId: "task", action: "issue.updated", agentId: "riley", details: { status: "done" }, createdAt: "2026-10-26T03:00:00Z" }], verification: [{ taskId: "task", runId: "first", filename: "launch.html", sha256: "a".repeat(64), exitCode: 1, checkedAt: "2026-10-26T00:50:00Z", command: "node accessible-heading-and-email-label-check" }, { taskId: "task", runId: "second", filename: "launch.html", sha256: "b".repeat(64), exitCode: 0, checkedAt: "2026-10-26T02:50:00Z", command: "node accessible-heading-and-email-label-check" }] };
}
it("accepts actual command failure, pause, handoff and repair within the current week", () => expect(gradeWorkTimeline(timeline()).passed).toBe(true));
it("accepts natural chronological phrasing", () => { const o = timeline(); o.reply = "Today in UTC Morgan ran the accessibility check which was failing; Morgan was blocked. You reassigned the task to Riley. Riley fixed the label and verification passed."; expect(gradeWorkTimeline(o).passed).toBe(true); });
it("rejects wrong run/unchanged bytes or reversed chronology", () => { const o = timeline(); o.verification[1]!.sha256 = "a".repeat(64); expect(gradeWorkTimeline(o).passed).toBe(false); const wrong = timeline(); wrong.timelineEvents[1]!.createdAt = "2026-10-26T00:00:00Z"; expect(gradeWorkTimeline(wrong).passed).toBe(false); });
it("rejects exposing private work", () => { const wrong = timeline(); wrong.reply += " Secret acquisition."; expect(gradeWorkTimeline(wrong).passed).toBe(false); });
it("rejects fabricated prior-week dates", () => { const o = timeline(); o.timelineEvents[0]!.createdAt = "2026-10-19T03:00:00Z"; expect(gradeWorkTimeline(o).passed).toBe(false); });
function partial() {
const o = base("inbox-search.archive-partial-failure"); const originals = ["one", "two", "three"].map(id => ({ id, identifier: `RUN-${id}`, title: `Delivery ${id}`, status: "done" }));
const items = originals.map((row, index) => ({ ...row, archivedAt: index < 2 ? "2026-10-26T01:00:00Z" : null, archivedByActorType: index < 2 ? "agent" : undefined, archivedByAgentId: index < 2 ? "ceo" : undefined, archivedByRunId: index < 2 ? "initial" : undefined })); const events = originals.slice(0, 2).map(row => ({ id: row.id, entityId: row.id, action: "issue.inbox_archived", agentId: "ceo", details: { userId: "person" } }));
Object.assign(o.state, { originalTasks: originals, personId: "person", injectedFailures: { one: 0, two: 0, three: 1 } }); o.tasks = structuredClone(originals); o.activity = structuredClone(events); o.runs = [{ id: "initial", status: "succeeded", agentId: "ceo" }];
o.reply = "I archived [RUN-one](/RUN/issues/RUN-one) and [RUN-two](/RUN/issues/RUN-two). Archiving [RUN-three](/RUN/issues/RUN-three) failed with a server error, so it is still in your inbox."; o.links = ["/RUN/issues/RUN-one", "/RUN/issues/RUN-two", "/RUN/issues/RUN-three"];
const first = { ...structuredClone(o), ownerItems: structuredClone(items), phaseReply: o.reply }; o.state.firstPartial = first;
Object.assign(items[2]!, { archivedAt: "2026-10-26T02:00:00Z", archivedByActorType: "agent", archivedByAgentId: "ceo", archivedByRunId: "retry" }); o.activity.push({ id: "third", entityId: "three", action: "issue.inbox_archived", agentId: "ceo", details: { userId: "person" } });
o.runs.push({ id: "retry", status: "succeeded", agentId: "ceo" });
return { ...o, ownerItems: items, phaseReply: "The other two were already archived; I archived [RUN-three](/RUN/issues/RUN-three) now." };
}
const firstOf = (o: ReturnType<typeof partial>) => o.state.firstPartial as ReturnType<typeof partial>;
it("accepts two saved archives, a real storage failure on the third and a retry that writes only the third", () => expect(gradeArchivePartial(partial()).passed).toBe(true));
it("does not grade reply wording: reworded or terse valid replies with the same saved outcome pass", () => {
for (const reply of ["Done with two of them. [RUN-three](/RUN/issues/RUN-three) hit an error and wasn't archived; it's still there.", "[RUN-three](/RUN/issues/RUN-three) could not be archived. The other two completed items are out of your inbox."]) {
const o = partial(); firstOf(o).reply = reply; expect(gradeArchivePartial(o).passed, reply).toBe(true);
}
const o = partial(); o.phaseReply = "Finished: [RUN-three](/RUN/issues/RUN-three) is archived too."; expect(gradeArchivePartial(o).passed).toBe(true);
});
it("rejects the retained broken run where the fault never fired and all three archived in the first turn", () => {
const o = partial(), first = firstOf(o);
Object.assign(first.ownerItems[2]!, { archivedAt: "2026-10-26T01:00:00Z", archivedByActorType: "agent", archivedByAgentId: "ceo", archivedByRunId: "initial" }); first.activity.push({ id: "third", entityId: "three", action: "issue.inbox_archived", agentId: "ceo", details: { userId: "person" } });
first.state.injectedFailures = { one: 0, two: 0, three: 0 }; expect(gradeArchivePartial(o).passed).toBe(false);
});
it("rejects a failure the storage fault did not cause or that hit an archived item", () => {
const silent = partial(); firstOf(silent).state.injectedFailures = { one: 0, two: 0, three: 0 }; expect(gradeArchivePartial(silent).passed).toBe(false);
const wrong = partial(); firstOf(wrong).state.injectedFailures = { one: 1, two: 0, three: 1 }; expect(gradeArchivePartial(wrong).passed).toBe(false);
});
it("requires the first reply to identify the failed item by link, identifier or title", () => {
// Shape of run 42720344901: identifier and title without a task link, plus a no-op reset of the failed item.
const real = partial(), first = firstOf(real); first.links = []; first.reply = 'Archived 2 of the 3 done items: RUN-one "Delivery one" and RUN-two "Delivery two".\n\nRUN-three "Delivery three" could not be archived: the endpoint returned HTTP 500 on every attempt. It is still in your inbox.';
first.activity.push({ id: "reset", entityId: "three", action: "issue.inbox_unarchived", agentId: "ceo", details: { userId: "person" } }); expect(gradeArchivePartial(real).passed).toBe(true);
for (const reply of ["Archived two items. One failed and is still in your inbox.", "Archived RUN-one and RUN-two; RUN-thirty failed."]) {
const o = partial(); firstOf(o).links = ["/RUN/issues/RUN-one", "/RUN/issues/RUN-two"]; firstOf(o).reply = reply; expect(gradeArchivePartial(o).passed, reply).toBe(false);
}
});
it("rejects undoing a saved archive in the first turn", () => { const o = partial(); firstOf(o).activity.push({ id: "undo", entityId: "one", action: "issue.inbox_unarchived", agentId: "ceo", details: { userId: "person" } }); expect(gradeArchivePartial(o).passed).toBe(false); });
it("rejects repeating successful writes, unarchiving, or leaving the failed item on retry", () => {
const repeated = partial(); repeated.ownerItems[0]!.archivedAt = "2026-10-26T02:00:00Z"; repeated.ownerItems[0]!.archivedByRunId = "retry"; repeated.activity.push({ id: "again", entityId: "one", action: "issue.inbox_archived", agentId: "ceo", details: { userId: "person" } }); expect(gradeArchivePartial(repeated).passed).toBe(false);
const undone = partial(); undone.activity.push({ id: "undo", entityId: "one", action: "issue.inbox_unarchived", agentId: "ceo", details: { userId: "person" } }); expect(gradeArchivePartial(undone).passed).toBe(false);
const left = partial(); Object.assign(left.ownerItems[2]!, { archivedAt: null, archivedByRunId: undefined }); left.activity.pop(); expect(gradeArchivePartial(left).passed).toBe(false);
});
it("rejects wrong-person audit or edited completion", () => { const o = partial(); o.activity[0].details.userId = "another"; expect(gradeArchivePartial(o).passed).toBe(false); const wrong = partial(); wrong.tasks[0].status = "cancelled"; expect(gradeArchivePartial(wrong).passed).toBe(false); });
it("never passes missing evidence", () => { for (const grade of [gradeProjectDefaults, gradeRoutineAudit, gradeWorkTimeline, gradeArchivePartial]) expect(grade(undefined).passed).toBe(false); });