Files
PaperClipAI/tests/runner-e2e/chat-catalog-domains/internal-inbox-search.ts
DottaandClaude Opus 5.5 65ff80a4bb fix(evals): grade chat acknowledgements, saved replies and links on saved state (#21000)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The chat eval catalog runs real chat journeys against real agents
and grades saved product state
> - A review of full cohort 3 found three cases whose oracle passed
wrong behavior, and eight passes whose final evidence saved no rendered
links
> - `companies.agent-observability` passed although the agent denied the
saved collector. `inbox-search.show-my-work` counted links that existed
only in run-stream prose. `instance-operations.restart-service` accepted
a reply whose only link was the `/<prefix>/` placeholder
> - The final evidence read ran on a page that had not rendered the
conversation yet, so it saved no links
> - This pull request grades those cases on saved-state reads, the saved
reply and resolved links, and reads final evidence from the rendered
conversation
> - The benefit is that these cases fail when the product is wrong and
keep their links when it is right

## Linked Issues or Issue Description

Refs #20998
Refs #20999

**What happened?**
- `companies.agent-observability`: the board saved and tested a
collector. Asked to acknowledge it, the agent said there was no endpoint
or credentials. The oracle graded only human-side state, so it passed.
- `inbox-search.show-my-work`: the saved reply said "Answered above"
over nothing. The links the oracle counted came from run-stream prose
that the bubble also shows.
- `instance-operations.restart-service`: the reply's only action link
was `/<prefix>/company/settings/instance`. The oracle did not check
links.
- Eight cohort-3 passes (`access.other-company`, `agents.agent-api-key`,
`assistant-connections.revoke-personal-grant`,
`budgets-costs.resolve-budget`, `recovery.retry-exhausted`,
`steering.stop-versus-pause`, `task-coordination.pause-tree`,
`teams.install-team`) saved a final observation with no links and a
failed grade. The graded reload had the links. The final evidence read
ran after `verifyResult` left the page mid-load, and an expired render
wait from an earlier document made the read instant.

**Expected behavior**
Each oracle fails the observed wrong behavior and passes valid, reworded
behavior. Final evidence holds the links that the conversation renders.

**Steps to reproduce**
Grade the retained cohort-3 observations with the old oracles. They
pass. Grade them with these oracles. The three wrong cases fail.

## What Changed

- `companies.agent-observability` (oracle v4): each acknowledgement turn
must read the saved trace export status successfully. The read is bound
to the company by its redaction-safe `call_api` receipt. A reply must
also render a link to `/<prefix>/company/settings` for the fixture
company. The reply's wording is not matched.
- `inbox-search.show-my-work` (oracle v5): actionable links must be
carried by the saved reply: a Markdown link target in the saved body, or
a task identifier written in it, which the conversation links. New
shared helper `savedReplyLinks`.
- `instance-operations.restart-service` (oracle v3): new check
`reply-links-resolve` rejects any rendered or saved link target that
keeps a template placeholder (`<...>` or `{...}`). New helpers
`markdownLinkTargets` and `unresolvedLinkTarget` in
`chat-rendered-links.ts`.
- Link capture: the final success-path evidence read reopens the
rendered conversation first. An expired render wait now belongs to its
loaded document, not to the Playwright page.
- Updated `chat-catalog-descriptors.json` for the three oracle versions
and source files. No scenario yaml changed, so the TestOps manifest does
not change.

## Verification

- `npx vitest run --config tests/runner-e2e/vitest.config.ts
tests/runner-e2e/chat-catalog-domains/identity-observability.test.ts
tests/runner-e2e/chat-catalog-domains/internal.test.ts
tests/runner-e2e/chat-catalog-domains/privileged-oracles.test.ts
tests/runner-e2e/chat-catalog-domains/privileged-restart-history.test.ts
tests/runner-e2e/chat-rendered-links.test.ts
tests/runner-e2e/chat-catalog-consistency.test.ts
tests/runner-e2e/chat-catalog-fingerprint.test.ts`
- Calibration tests for each case: a valid result, a reworded valid
result, and the broken cohort-3 behavior.
- Red/green: the new render-wait test fails on the old wait logic and
passes with this change.
- `npx tsc -p tests/runner-e2e/tsconfig.json --noEmit` passes. `node
tests/runner-e2e/source-sdk-public-inputs.mjs <HEAD> --check` reports no
change.
- A paid run on a combined branch with the two product fixes is recorded
in a comment on this pull request.

## Risks

- `companies.agent-observability` needs the agent-readable status route
from #20998. Without it, the case fails, which is correct.
- The show-my-work check needs links in the saved reply. Before #20999,
an OpenCode reply that left its answer in the run stream fails this
case.
- The final evidence read can wait up to 30 seconds to reopen the
conversation. The read is still bounded.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, with tool use and code
execution in Claude Code.

## 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-11 20:49:38 -06:00

174 lines
13 KiB
TypeScript

import { expect } from "@playwright/test";
import type { ChatCatalogCase, ChatCatalogContext } from "../chat-catalog-contract.js";
import { makeDefinition } from "./internal-definition.js";
import { internalResultLink, checks, entity, hasLink, observation, observeInternal, project, sameTask, savedReplyLinks, task, type Entity, type InternalObservation } from "./internal-support.js";
interface Probe { status: number; body: unknown }
interface InboxObservation extends InternalObservation {
personalItems: Entity[]; policy: { mode: string; allowedAgentIds: string[] }; personalProbe: Probe; responsibleUserProbe: Probe; unreadProbe: Probe; archiveProbe?: Probe;
}
const inboxProbeKeys = new WeakMap<ChatCatalogContext, Promise<{ id: string; token: string }>>();
async function inboxProbeKey(ctx: ChatCatalogContext) {
if (!ctx.input.privateSecrets) throw new Error("Agent probe requires an attempt-private secret redactor");
let pending = inboxProbeKeys.get(ctx);
if (!pending) {
pending = ctx.input.api.postSensitive<{ id: string; token: string }>(`/api/agents/${ctx.input.fixtures.agent.id}/keys`, { name: `inbox-scope-control-${ctx.input.nonce}` })
.then(key => { ctx.input.privateSecrets!.push(key.token); return key; });
inboxProbeKeys.set(ctx, pending);
}
return pending;
}
async function cleanupInboxProbe(ctx: ChatCatalogContext) {
const pending = inboxProbeKeys.get(ctx); inboxProbeKeys.delete(ctx);
if (pending) { const key = await pending; await ctx.input.api.delete(`/api/agents/${ctx.input.fixtures.agent.id}/keys/${key.id}`, { allowNotFound: true }); }
}
async function agentRequest(ctx: ChatCatalogContext, path: string, method = "GET", body?: unknown): Promise<Probe> {
const key = await inboxProbeKey(ctx);
const url = new URL(path, ctx.input.api.baseURL);
if (url.origin !== ctx.input.api.baseURL || !url.pathname.startsWith("/api/")) throw new Error("Inbox probe escaped fixture API");
const response = await fetch(url, { method, redirect: "error", signal: AbortSignal.timeout(15_000),
headers: { Authorization: `Bearer ${key.token}`, ...(body === undefined ? {} : { "content-type": "application/json" }) },
...(body === undefined ? {} : { body: JSON.stringify(body) }) });
return { status: response.status, body: await response.json() };
}
function minePath(ctx: ChatCatalogContext) {
return `/api/companies/${ctx.input.fixtures.company.id}/issues?touchedByUserId=me&inboxArchivedByUserId=me&includeAgentCreatedTasks=true&limit=500`;
}
function mineLink(o: InternalObservation) {
return o.links.some(href => {
try {
const url = new URL(href, o.origin);
return url.origin === o.origin && (/\/inbox\/mine\/?$/.test(url.pathname)
|| /\/(?:tasks|issues)\/?$/.test(url.pathname) && url.searchParams.get("view") === "mine");
} catch { return false; }
});
}
// A task link names one task. The shared hasLink also matches a collection page such as the
// Mine list (/<prefix>/issues?view=mine) to every row without a urlKey, so require a task segment.
function linksTask(o: InternalObservation, row: Entity): boolean {
return hasLink({ ...o, links: o.links.filter(href => {
try { const parts = new URL(href, o.origin).pathname.split("/"); return !!parts[parts.indexOf("issues") + 1]; } catch { return false; }
}) }, "issues", row);
}
function mentionsTask(reply: string, row: Entity): boolean {
return [row.id, row.title, row.identifier].some(literal => !!literal
&& new RegExp(`(?<![\\w-])${literal.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?![\\w-])`, "i").test(reply));
}
export function gradeInbox(value: unknown) {
const c = checks(); const o = observation(value) as InboxObservation | undefined;
c.add("independent-personal-inbox", !!o && Array.isArray(o.personalItems) && !!o.personalProbe && !!o.responsibleUserProbe && !!o.policy,
"The signed-in person's actual inbox, saved policy and agent access probe are required.");
if (!o || !Array.isArray(o.personalItems) || !o.personalProbe || !o.responsibleUserProbe || !o.policy) return c.result();
const originals = o.state.originalTasks as Entity[] | undefined;
c.add("task-state-and-inbox-preserved", !!originals && originals.every(row => sameTask(row, o.tasks.find(now => now.id === row.id)))
&& (o.state.originalPersonalIds as string[]).every(id => o.personalItems.some(row => row.id === id))
&& !o.activity.some(row => row.agentId === o.leadId && /issue\.inbox_(?:archived|unarchived)/.test(row.action ?? "")),
"No task or personal archive is mutated in a read-only or denied journey.");
if (o.scenarioId === "inbox-search.show-my-work") {
const accessible = o.responsibleUserProbe.status === 200 && Array.isArray(o.responsibleUserProbe.body);
const actionable = (originals ?? []).filter(row => row.id !== entity(o.state, "automatedTask").id && !["done", "cancelled"].includes(row.status ?? ""));
const body = o.responsibleUserProbe.body as Entity[];
// The links must be in the saved reply, not only in run-stream prose the bubble also shows.
const saved = { ...o, links: savedReplyLinks(o) };
c.add("responsible-user-scope-or-honest-limitation", accessible
? actionable.length > 0 && actionable.every(row => body.some(item => item.id === row.id) && linksTask(saved, row))
: o.personalProbe.status === 403 && [401, 403].includes(o.responsibleUserProbe.status) && mineLink(saved),
"Readable responsible-user records are summarized in the saved reply with exact actionable source links; only an actual denied aggregation supports the human-only limitation branch, whose saved reply must link Mine.");
const unreadCounts = [...o.reply.matchAll(/\b(\d+)\s+unread\b/gi)].map(match => Number(match[1]));
c.add("no-invented-unread-counts", unreadCounts.length === 0 || o.unreadProbe?.status === 200 && Array.isArray(o.unreadProbe.body)
&& unreadCounts.every(count => count === (o.unreadProbe.body as Entity[]).length),
"Any stated unread count matches the actual authorized unread aggregation, rather than an ordinary task queue.");
const unrelated = entity(o.state, "automatedTask");
const outsideInbox = (originals ?? []).filter(row => !o.personalItems.some(item => item.id === row.id));
c.add("real-person-scope", !o.personalItems.some(row => row.id === unrelated.id) && outsideInbox.some(row => row.id === unrelated.id)
&& outsideInbox.every(row => !linksTask(o, row) && !mentionsTask(o.reply, row)),
"Company work outside the person's real Mine view is neither linked nor named in an answer about their inbox.");
} else if (o.scenarioId === "inbox-search.archive-without-grant") {
const body = o.archiveProbe?.body as { code?: string } | undefined;
c.add("actual-archive-denial", o.policy.mode === "disabled" && o.archiveProbe?.status === 403 && body?.code === "inbox_management_disabled",
"An actual authenticated agent archive request for the named person is denied by the saved disabled policy.");
// No archive write (checked above) and the policy still disabled are the saved outcome. Whether
// the reply explains the missing authority and offers the Mine workflow or an access request is
// left to a human reader; a returned Mine link is clicked in verifyResult.
} else c.add("implemented-scenario", false, "This oracle does not implement this scenario.");
return c.result();
}
function inboxBoundary(scenarioId: string): ChatCatalogCase {
const base = makeDefinition(scenarioId, async ctx => {
try {
const session = await ctx.input.api.get<{ user?: { id: string } }>("/api/auth/get-session");
if (!session.user?.id) throw new Error("Personal inbox fixture requires a real signed-in person");
const p = await project(ctx); const tasks: Entity[] = [];
for (const [title, status] of [["Launch review", "in_review"], ["Blocked launch decision", "blocked"], ["Delivered launch copy", "done"], ["Cancelled launch experiment", "cancelled"]]) {
const t = await task(ctx, title!, p.id, { status });
await ctx.input.api.post(`/api/issues/${t.id}/comments`, { body: `I am participating in ${title}.` });
tasks.push(t);
}
await ctx.input.api.delete(`/api/issues/${tasks[0]!.id}/read`);
const automated = await agentRequest(ctx, `/api/companies/${ctx.input.fixtures.company.id}/issues`, "POST", {
title: `Unrelated automated maintenance ${ctx.input.nonce}`, projectId: p.id, status: "backlog",
});
if (automated.status !== 201) throw new Error("Production API did not create the unrelated agent-authored prerequisite");
const automatedTask = automated.body as Entity;
await ctx.input.api.putSensitive(`/api/companies/${ctx.input.fixtures.company.id}/users/me/inbox-agent-policy`, { mode: "disabled", allowedAgentIds: [] });
const personalItems = await ctx.input.api.get<Entity[]>(minePath(ctx));
if (!tasks.every(row => personalItems.some(item => item.id === row.id)) || personalItems.some(row => row.id === automatedTask.id))
throw new Error("Production Mine fixture does not reflect human participation independently of automated work");
return { project: p, task: tasks[0], doneTask: tasks[2], personId: session.user.id, originalTasks: [...tasks, automatedTask],
originalPersonalIds: personalItems.map(row => row.id), automatedTask };
} catch (error) { await cleanupInboxProbe(ctx); throw error; }
}, gradeInbox);
return { ...base, authentication: "human-session",
oracleVersion: scenarioId === "inbox-search.archive-without-grant" ? "paperclip.chat-catalog.inbox.archive-without-grant.v4" : "paperclip.chat-catalog.internal.show-my-work.v5",
async observe(ctx, state): Promise<InboxObservation> {
const o = await observeInternal(ctx, state);
const [personalItems, policy, personalProbe, responsibleUserProbe, unreadProbe] = await Promise.all([
ctx.input.api.get<Entity[]>(minePath(ctx)),
ctx.input.api.get<InboxObservation["policy"]>(`/api/companies/${ctx.input.fixtures.company.id}/users/me/inbox-agent-policy`),
agentRequest(ctx, minePath(ctx)),
agentRequest(ctx, minePath(ctx).replaceAll("=me", `=${encodeURIComponent(String(state.personId))}`)),
agentRequest(ctx, `/api/companies/${ctx.input.fixtures.company.id}/issues?unreadForUserId=${encodeURIComponent(String(state.personId))}&includeAgentCreatedTasks=true&limit=500`),
]);
const archiveProbe = scenarioId === "inbox-search.archive-without-grant"
? await agentRequest(ctx, `/api/issues/${entity(state, "doneTask").id}/inbox-archive`, "POST", { userId: state.personId }) : undefined;
return { ...o, personalItems, policy, personalProbe, responsibleUserProbe, unreadProbe, archiveProbe };
},
async verifyResult(ctx, _state, value) {
const o = value as InboxObservation; const href = o.links.find(link => mineLink({ ...o, links: [link] }));
if (!href && o.scenarioId === "inbox-search.show-my-work" && o.responsibleUserProbe.status === 200) {
await base.verifyResult(ctx, o.state, o); return;
}
if (!href && o.scenarioId !== "inbox-search.archive-without-grant") throw new Error("Missing rendered Mine link");
const page = ctx.input.page, source = page.url();
// Sidebar link names can carry badges ("Inbox 2 unread"), so match the leading label.
const sidebarLink = (label: string) => page.locator("aside").getByRole("link", { name: new RegExp(`^${label}(?![\\w-])`) }).filter({ visible: true }).first();
if (href) await internalResultLink(ctx, o, href).first().click();
else {
await sidebarLink("Inbox").click();
await page.waitForURL(url => /\/(?:decisions|inbox|issues)(?:\/|$)/.test(url.pathname));
// In the coordinator shell, Inbox is the decisions inbox; personal work is the Tasks "Mine" view.
if (/\/decisions\/?$/.test(new URL(page.url()).pathname)) await sidebarLink("Tasks").click();
}
const tabMine = page.getByRole("button", { name: "Mine", exact: true }).or(page.getByRole("link", { name: "Mine", exact: true }))
.or(page.getByRole("tab", { name: "Mine", exact: true }));
const viewMenu = page.getByRole("button", { name: /^Change view\b/ }).filter({ visible: true });
await expect(tabMine.or(viewMenu).first()).toBeVisible();
let mineView: "inbox-tab" | "tasks-view";
if (await viewMenu.first().isVisible()) {
mineView = "tasks-view";
if (!/currently Mine$/.test(await viewMenu.first().getAttribute("aria-label") ?? "")) {
await viewMenu.first().click(); await page.getByRole("menuitem", { name: /^Mine\b/ }).first().click();
}
await expect(viewMenu.first()).toHaveAttribute("aria-label", /currently Mine$/);
} else { mineView = "inbox-tab"; if (!href) await tabMine.first().click(); }
await expect(ctx.input.page.getByText("Blocked launch decision", { exact: true }).first()).toBeVisible();
await ctx.input.evidence("chat-catalog-personal-mine-opened.json", { personId: o.state.personId, url: ctx.input.page.url(), visible: true,
navigation: href ? "returned-link" : "independent-human-sidebar-workflow", mineView, responseAccessRequest: !href });
await ctx.input.page.goto(source, { waitUntil: "domcontentloaded" });
await expect(ctx.input.page.getByTestId("task-chat-composer-input")).toBeVisible();
},
async cleanup(ctx) { await cleanupInboxProbe(ctx); },
};
}
export const inboxSearchCases: readonly ChatCatalogCase[] = [inboxBoundary("inbox-search.show-my-work"), inboxBoundary("inbox-search.archive-without-grant")];