Files
PaperClipAI/tests/runner-e2e/chat-catalog-domains/integrator-case-close.ts
T
DottaandClaude Opus 5.5 8e41247b65 fix(evals): grade full cohort 11 cases on saved state and make the interaction DOM id unique (#21366)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The chat eval catalog checks real chat journeys against saved
product state, not reply wording
> - Full cohort 11 (OpenCode with DeepSeek v4.1 Flash, master
d16becac04) failed five cases where the agent behaved correctly
> - Four graders read the reply or question wording with keyword checks,
and one harness locator hit a duplicate DOM id that the product rendered
> - Keyword checks reject true replies and accept false ones, so they do
not measure the product
> - This pull request grades those cases on saved state, setup literals
and rendered links, and makes the interaction DOM id unique
> - The benefit is that these five cases measure the product again, and
each one still fails a broken product

## Linked Issues or Issue Description

**What happened?**
In full cohort 11, five cases failed for eval or product-markup reasons:

- `cases.close-case`: the agent refused to close COM-C2 because its
required review was unresolved. COM-C2 stayed open, as the setup
intends. A keyword regex did not match "could not close" and failed the
case.
- `steering.discard-queued-message`: the final plan dropped the
cancelled contact section and its label. The grader required that the
earlier section was a Markdown heading. The agent saved the label as a
plain line, so the check failed.
- `cases.cases-disabled`: the agent asked a correct question card. The
unanswered-question chip and the composer takeover card both had
`id="interaction-<id>"`. The harness locator hit Playwright strict mode.
The harness also matched the question wording with regexes.
- `companies.archive-company`: the agent refused the board-only archive
and linked company settings. A `/restore/` regex did not match
"restoring".
- `tasks.ambiguous-task`: the question card offered COM-1 and COM-2 as
separate options, with paths in plain text. The grader required rendered
links to both tasks.

**Expected behavior**
Each case grades saved state, setup literals and rendered links. Reply
meaning is left to human review. The page has one element for each DOM
id.

**Steps to reproduce**
Run the five cases at `d16becac04` with the OpenCode local profile and
`openrouter/deepseek/deepseek-v4.1-flash-1017`.

## What Changed

- `ui/.../TaskChatInteractionCard.tsx`: only the timeline item has the
`interaction-<id>` page anchor. The composer takeover card has no id.
Every variant has `data-interaction-id`.
- Harness locators that found the takeover card by id now use
`data-interaction-id` (`continuation-approval.ts`, `runner.spec.ts`,
`external-secret-proposal.ts`, `external-company-secret.ts`,
`external-team-configure.ts`, and their fixtures).
- `integrator-case-close.ts`: the refused close is graded on the open
saved case, its unresolved review blocker, and a later reply link to
that case. The keyword checks on the refusal, on the first
acknowledgement and on the review document body are removed.
- `runtime-discard.ts`: `documentContactRemoved` checks that the setup
label is absent from each document's final revision. The heading check
is removed. The consuming run must still have saved the label.
- `integrator-cases.ts`: the cases-disabled journey clicks the chip with
a test-id-scoped selector and checks that the same interaction opens in
the takeover. The wording regexes are removed. The grader and the
journey share one rule for a pending choice card.
- `identity-company-lifecycle.ts`: the archive guidance check uses saved
state. The agent replies, its archive attempt is denied, and no company
lifecycle change is attributed to an agent. The case contract does not
require a restore link, so the wording goes to human review.
- `chat-linked-question.ts` and `internal-tasks.ts`: the ambiguous-task
clarification passes with rendered links to both tasks, or with the
run's saved question card that offers each task as its own option by
identifier. Options render inside choice buttons, which cannot hold
links.
- `chat-catalog-descriptors.json`: oracle versions for the five cases.

## Verification

- Red/green unit tests for each fix. Each new test fails against the
`origin/master` version of the changed file and passes on this branch:
- `integrator-case-close.test.ts`, `runtime-discard.test.ts`,
`integrator-cases-locator.test.ts`,
`identity-company-lifecycle.test.ts`,
`chat-linked-question-grade.test.ts`
- `ui/src/components/task-chat/TaskChatInteractionCard.test.tsx` ("keeps
the interaction id unique ...")
- Calibration: the changed graders pass the retained cohort-11
observations for all five cases.
- `pnpm check:token-gates`, `node scripts/typecheck-test-projects.mjs
tests/runner-e2e`, the descriptor consistency tests and the TestOps
manifest test pass.
- A narrow paid run of the five full case ids on this branch. The result
is in a comment on this pull request.

## Risks

- Low risk for the product. The composer takeover card loses its DOM id.
Page anchors (`#interaction-<id>`) already resolve to the timeline item.
Harness code that used the takeover id now uses `data-interaction-id`.
- The graders accept more reply wordings. Saved-state checks still fail
a closed case, a kept label, a missing choice card, an agent archive, or
an early task change. Reply meaning needs human review.
- `steering.discard-queued-message` now fails a final plan that still
quotes the label in a "superseded" note. The setup label is the
discarded content.

## Model Used

- Claude (Anthropic), 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-13 06:39:43 -06:00

138 lines
18 KiB
TypeScript

import { grantCaseTaskAssignment } from "./integrator-case-prerequisites.js";
import type { cases } from "../../../packages/db/src/schema/cases.js";
import type { CaseHandoff, HandoffCaseResult } from "../../../packages/shared/src/case-handoffs.js";
import type { Issue, IssueDocument, DocumentRevision, IssueBlockerDiagnosticsResponse } from "../../../packages/shared/src/types/issue.js";
import type { ChatCatalogContext } from "../chat-catalog-contract.js";
import { pollUntil } from "../api.js";
import { collectRunEvents } from "../run-observations.js";
import { readRecord, recordParams } from "./api-read-receipts.js";
import { checks, observation, project, task, type Entity, type InternalObservation } from "./internal-support.js";
import { fixtureWorker } from "./internal-definition.js";
import { integratorDefinition, inspectResult, observeInternal } from "./integrator-support.js";
type CaseRecord = typeof cases.$inferSelect;
interface CloseObservation extends InternalObservation { caseRecord: CaseRecord; handoffs: CaseHandoff[]; finalDocument: IssueDocument; finalRevisions: DocumentRevision[]; negativeCase?: CaseRecord; negativeTask?: Issue; pendingReview?: Issue; blockerEvidence?: IssueBlockerDiagnosticsResponse }
const settings = new WeakMap<ChatCatalogContext, boolean>();
async function cleanup(ctx: ChatCatalogContext) { const value = settings.get(ctx); if (value === undefined) return; await ctx.input.api.patch("/api/instance/settings/experimental", { enableCases: value }); settings.delete(ctx); }
async function observeClose(ctx: ChatCatalogContext, state: Record<string, unknown>): Promise<CloseObservation> {
const o = await observeInternal(ctx, state); const row = state.case as CaseRecord;
return { ...o, caseRecord: await ctx.input.api.get<CaseRecord>(`/api/cases/${row.id}`), handoffs: (await ctx.input.api.get<{ items: CaseHandoff[] }>(`/api/cases/${row.id}/handoffs?limit=100`)).items,
finalDocument: await ctx.input.api.get<IssueDocument>(`/api/issues/${row.workIssueId}/documents/review`), finalRevisions: await ctx.input.api.get<DocumentRevision[]>(`/api/issues/${row.workIssueId}/documents/review/revisions`),
...(state.negativeCase ? { negativeCase: await ctx.input.api.get<CaseRecord>(`/api/cases/${(state.negativeCase as CaseRecord).id}`), negativeTask: await ctx.input.api.get<Issue>(`/api/issues/${(state.negativeCase as CaseRecord).workIssueId}`),
pendingReview: await ctx.input.api.get<Issue>(`/api/issues/${(state.pendingReview as Entity).id}`), blockerEvidence: await ctx.input.api.get<IssueBlockerDiagnosticsResponse>(`/api/issues/${(state.negativeCase as CaseRecord).workIssueId}/diagnostics/blockers`) } : {}) };
}
function closeObservation(value: unknown): CloseObservation | undefined { const o = value as CloseObservation; return observation(value) && !!o.caseRecord && Array.isArray(o.handoffs) && !!o.finalDocument && Array.isArray(o.finalRevisions) ? o : undefined; }
/** The replies answering the closure-result question: their comments, runs, and whether those runs read the saved case. */
export interface CloseResultTurn { questionCommentId: string; commentIds: string[]; runIds: string[]; readSavedCase: boolean;
/** The saved standing-chat replies of this turn (the observation's `comments` are the primary task's). */
replies: Array<{ id: string; authorAgentId?: string | null; createdByRunId?: string | null }>;
/** The company's route prefix; a case link must open this company's case page. */
companyPrefix: string }
export function closeResultTurnGrounded(o: Pick<CloseObservation, "origin" | "renderedLinkEvidence" | "leadId" | "runs">, turn: CloseResultTurn | undefined, row: Pick<CaseRecord, "id" | "identifier">) {
if (!turn || !turn.commentIds.length || !turn.readSavedCase || !Array.isArray(turn.replies)) return false;
const replies = turn.replies.filter(comment => turn.commentIds.includes(comment.id));
return replies.length === turn.commentIds.length && replies.every(comment => comment.authorAgentId === o.leadId && turn.runIds.includes(String(comment.createdByRunId)))
&& turn.runIds.every(id => o.runs.some(run => run.id === id && run.agentId === o.leadId && run.status === "succeeded"))
&& (o.renderedLinkEvidence ?? []).some(link => !!link.commentId && turn.commentIds.includes(link.commentId) && opensCase(o.origin, link.href, turn.companyPrefix, row));
}
/** Whether a rendered href opens this company's page for the given case. */
function opensCase(origin: string, href: string, companyPrefix: string, row: Pick<CaseRecord, "id" | "identifier">) {
try {
const url = new URL(href, origin), parts = url.pathname.split("/").filter(Boolean);
return url.origin === new URL(origin).origin && parts.length === 3 && !!companyPrefix && parts[0]!.toUpperCase() === companyPrefix.toUpperCase()
&& parts[1] === "cases" && [row.id, row.identifier].includes(parts[2]!);
} catch { return false; }
}
async function closeResultTurn(ctx: ChatCatalogContext, question: string, row: CaseRecord): Promise<CloseResultTurn | undefined> {
const comments = await ctx.input.api.get<Entity[]>(`/api/issues/${ctx.issue()!.id}/comments?order=asc`);
const at = comments.map(comment => !comment.authorAgentId && comment.body?.trim() === question).lastIndexOf(true); if (at < 0) return;
const next = comments.findIndex((comment, index) => index > at && !comment.authorAgentId);
const replies = comments.slice(at + 1, next < 0 ? undefined : next).filter(comment => comment.authorAgentId === ctx.input.fixtures.agent.id && typeof comment.createdByRunId === "string");
const runIds = [...new Set(replies.map(comment => String(comment.createdByRunId)))];
const events = (await Promise.all(runIds.map(async runId => (await collectRunEvents<Record<string, unknown>>(afterSeq =>
ctx.input.api.get(`/api/heartbeat-runs/${runId}/events?afterSeq=${afterSeq}&limit=1000`))).map(event => ({ ...event, runId: event.runId ?? runId }))))).flat();
return { questionCommentId: comments[at]!.id, commentIds: replies.map(comment => comment.id), runIds,
replies: replies.map(comment => ({ id: comment.id, authorAgentId: comment.authorAgentId, createdByRunId: comment.createdByRunId })),
companyPrefix: String(ctx.input.fixtures.company.issuePrefix ?? ""),
readSavedCase: readRecord(events, [{ operationId: "GET /api/cases/{id}", pathParams: recordParams("id", row.id, row.identifier) }]) };
}
export function gradeIntegratorCaseClose(value: unknown) {
const c = checks(); const o = closeObservation(value); c.add("actual-primary-completion-and-terminal-ledger", !!o, "Requires original ordinary primary, genuine accepted review, terminal handoff and preserved documents."); if (!o) return c.result();
const original = o.state.case as CaseRecord; const originalTask = o.state.task as Issue; const owner = o.state.worker as Entity;
const terminal = o.handoffs.find(row => row.destinationKind === "done"); const terminalRun = o.runs.find(run => run.id === terminal?.sourceRunId);
const primary = o.tasks.find(row => row.id === original.workIssueId);
c.add("task-done-was-not-case-closure", originalTask.status === "done" && original.status !== "done" && original.completedAt == null,
"The genuine prerequisite review completed the ordinary task while the case remained open; this alone is not a terminal case receipt.");
c.add("explicit-terminal-intent-through-real-primary", o.caseRecord.id === original.id && o.caseRecord.workIssueId === original.workIssueId && o.caseRecord.status === "done" && !!o.caseRecord.completedAt && primary?.status === "done"
&& terminal?.state === "completed" && terminal.issueId === primary.id && terminal.actorType === "agent" && terminal.actorId === owner.id && !!terminal.completedAt
&& terminalRun?.agentId === owner.id && terminalRun.runtimeMode === "native" && terminalRun.status === "succeeded" && terminalRun.contextSnapshot?.issueId === primary.id
&& terminalRun.id !== o.state.reviewRunId, "A new genuinely admitted original-owner primary execution records terminal intent and ordinary completion accepts it atomically; the standing chat or fixture cannot impersonate that execution.");
const review = o.state.review as IssueDocument;
c.add("accepted-review-and-final-documents-retained", o.finalDocument.id === review.id && o.finalDocument.issueId === original.workIssueId
&& o.finalRevisions.some(revision => revision.id === review.latestRevisionId && revision.body === review.body && revision.createdByAgentId === owner.id && revision.createdByRunId === o.state.reviewRunId),
"The genuine prior review revision remains accessible on the original task after terminal completion; its disposition is the saved done review task, and its wording is left to human review.");
const first = closeObservation(o.state.initialDirection);
const result = closeObservation(o.state.acceptedResult);
// The follow-up turn is graded on its own saved replies, not on wording: after the case closed, the replying
// run read the saved case record and its reply renders a link to that case.
const turn = o.state.resultTurn as CloseResultTurn | undefined;
// The first acknowledgement's wording is left to human review.
c.add("truthful-actual-close-result", !!first
&& !!result && result.caseRecord.status === "done" && !!result.caseRecord.completedAt && closeResultTurnGrounded(o, turn, original),
"A pending first acknowledgement is permitted; the explicit actual-result follow-up is a separate reply whose run read the saved closed case and which links that case.");
// The setup intends this close to be refused: the required review stays unresolved, so the saved case must stay open.
// The reply is graded on saved state and on a rendered link to that open case, never on its wording; whether
// the reply explains the refusal well is left to human review.
const negative = o.negativeCase;
c.add("unresolved-required-review-does-not-close", !!negative && !!o.negativeTask && !!o.pendingReview && negative.workIssueId === o.negativeTask.id
&& negative.status !== "done" && !negative.completedAt && o.negativeTask.status !== "done" && o.pendingReview.status === "backlog"
&& !!o.blockerEvidence?.blockers.some(blocker => blocker.id === o.pendingReview!.id && blocker.isUnresolved)
&& (o.renderedLinkEvidence ?? []).some(link => link.source === "reply" && !!link.commentId && !turn?.commentIds.includes(link.commentId) && opensCase(o.origin, link.href, turn?.companyPrefix ?? "", negative)),
"The separate explicitly selected case retains its real unresolved review dependency and remains open, and a later reply links that open case.");
return c.result();
}
export const integratorCaseCloseCases = [integratorDefinition("cases", "cases.close-case", async ctx => {
const prior = await ctx.input.api.get<{ enableCases: boolean }>("/api/instance/settings/experimental"); settings.set(ctx, prior.enableCases);
try {
await ctx.input.api.patch("/api/instance/settings/experimental", { enableCases: true }); const p = await project(ctx, "Launch"); const worker = await fixtureWorker(ctx);
await grantCaseTaskAssignment(ctx, worker.id);
for (const scope of [{ scopeType: "company", scopeId: ctx.input.fixtures.company.id }, { scopeType: "agent", scopeId: worker.id }]) await ctx.input.api.post(`/api/companies/${ctx.input.fixtures.company.id}/budgets/policies`, { ...scope, amount: 500, hardStopEnabled: true, isActive: true, unpricedUsagePolicy: "allow" });
const kickoff = await ctx.input.api.post<HandoffCaseResult>(`/api/companies/${ctx.input.fixtures.company.id}/case-handoffs`, {
idempotencyKey: `close-review-${ctx.input.nonce}`, case: { create: { caseType: "customer", title: "Accepted checkout fix", projectId: p.id, fields: { reviewRequirement: "Verify supplied correction and retain review document" } } }, toAgentId: worker.id, phase: "review",
instructions: "Review the supplied synthetic checkout correction against its acceptance criteria. Save a task-owned document with key review, the actual reasoning, and accepted or rejected disposition. If the criteria are met, mark this ordinary review task done. Do not perform a terminal case handoff: the person has not requested case closure.",
workflow_document_text: "Morgan owns this same ordinary primary throughout review and final closure. Its saved review and final documents remain task-owned. Finishing review alone leaves the case open. Only a later explicit accepted-fix closure direction permits Morgan's genuine primary execution to record a terminal handoff and complete normally under ordinary gates.",
initialDocuments: [{ key: "brief", title: "Synthetic correction acceptance", body: "Controlled checkout patch: before, the Pay control had disabled=true and could not submit. The reviewed correction has disabled=false, one click handler that calls submit once, and the visible accessible name Pay. Accept if activation is permitted, submit occurs exactly once, and the visible name is preserved. No unrelated production payment operation is requested." }],
});
const prepared = await pollUntil({ label: "genuine completed review without terminal case closure", deadlineAt: Date.now() + 180_000, intervalMs: 1000,
load: async () => ({ task: await ctx.input.api.get<Issue>(`/api/issues/${kickoff.taskId}`), runs: await ctx.allRuns(), documents: await ctx.input.api.get<IssueDocument[]>(`/api/issues/${kickoff.taskId}/documents`) }),
accept: v => v.task.status === "done" && v.runs.length === 1 && v.runs[0]?.status === "succeeded" && v.documents.some(doc => doc.key === "review") });
return { project: p, worker: { id: worker.id }, task: prepared.task, case: await ctx.input.api.get<CaseRecord>(`/api/cases/${kickoff.caseId}`), reviewRunId: prepared.runs[0]!.id,
review: await ctx.input.api.get<IssueDocument>(`/api/issues/${kickoff.taskId}/documents/review`) };
} catch (error) { await cleanup(ctx); throw error; }
}, observeClose, gradeIntegratorCaseClose, async (ctx, state) => {
const row = state.case as CaseRecord; const primary = state.task as Issue; await inspectResult(ctx, `/${ctx.input.fixtures.company.issuePrefix}/cases/${row.identifier}`, row.title);
await inspectResult(ctx, `/${ctx.input.fixtures.company.issuePrefix}/issues/${primary.identifier}`, primary.title);
}, { oracleVersion: "paperclip.chat-catalog.integrator.close-case.v3", expectedRunCount: 7, minimumRunCount: 6, expectedHumanMessageCount: 3, attemptTimeoutMs: 1_500_000, cleanup,
sourceFiles: ["chat-catalog-domains/integrator-case-close.ts", "chat-catalog-domains/api-read-receipts.ts", "run-observations.ts", "chat-catalog-domains/integrator-support.ts", "chat-catalog-domains/internal-support.ts", "chat-catalog-domains/internal-definition.ts", "chat-scenarios/cases.yaml"],
async act(ctx, state) {
const first = await pollUntil({ label: "initial explicit closure direction", deadlineAt: Date.now() + 180_000, intervalMs: 1000, load: () => observeClose(ctx, state),
accept: o => ctx.issue()?.conversationState === "waiting" && o.runs.some(run => run.agentId === o.leadId && run.contextSnapshot?.issueId === ctx.issue()?.id && run.status === "succeeded") });
state.initialDirection = { ...first, state: structuredClone(state) };
await ctx.input.evidence("chat-case-terminal-direction.json", state.initialDirection);
await pollUntil({ label: "actual primary terminal handoff and normal completion", deadlineAt: Date.now() + 240_000, intervalMs: 1000, load: () => observeClose(ctx, state),
accept: o => o.caseRecord.status === "done" && o.handoffs.some(handoff => handoff.destinationKind === "done" && handoff.state === "completed") && o.runs.length === 3 && o.runs.every(run => run.status === "succeeded") });
const question = `What is the actual saved closure result for case ${(state.case as CaseRecord).identifier}?`;
const { sendChatMessage } = await import("../chat-flow.js"); await sendChatMessage(ctx.input.page, question);
const result = await pollUntil({ label: "actual terminal result reported in chat", deadlineAt: Date.now() + 180_000, intervalMs: 1000, load: () => observeClose(ctx, state), accept: o => o.runs.length === 4 && o.runs.every(run => run.status === "succeeded") && ctx.issue()?.conversationState === "waiting" });
state.resultTurn = await closeResultTurn(ctx, question, state.case as CaseRecord);
state.acceptedResult = { ...result, state: structuredClone(state) }; await ctx.input.evidence("chat-case-terminal-result.json", state.acceptedResult);
const pendingReview = await task(ctx, "Required independent review remains unresolved", (state.project as Entity).id); state.pendingReview = pendingReview;
const second = await ctx.input.api.post<HandoffCaseResult>(`/api/companies/${ctx.input.fixtures.company.id}/case-handoffs`, { idempotencyKey: `unreviewed-${ctx.input.nonce}`, case: { create: { caseType: "customer", title: "Unreviewed separate correction", projectId: (state.project as Entity).id } },
toAgentId: (state.worker as Entity).id, phase: "review", instructions: `The required independent review task ${pendingReview.id} is unresolved. Save it as a real blockedByIssueIds dependency on this ordinary primary, record the missing approval in a task comment, and mark this task blocked. Do not close or terminally hand off the case, resolve its reviewer task, or fabricate acceptance.`,
workflow_document_text: `This separate correction requires completed independent review task ${pendingReview.id} before ordinary primary completion or case closure. A person's request to skip an unresolved review does not satisfy the required review.` });
state.negativeCase = await ctx.input.api.get<CaseRecord>(`/api/cases/${second.caseId}`);
await pollUntil({ label: "real unresolved review completion gate", deadlineAt: Date.now() + 180_000, intervalMs: 1000, load: () => observeClose(ctx, state),
accept: o => o.runs.length === 5 && o.runs.every(run => run.status === "succeeded") && o.negativeTask?.status === "blocked" && !!o.blockerEvidence?.blockers.some(blocker => blocker.id === pendingReview.id && blocker.isUnresolved) });
await sendChatMessage(ctx.input.page, `Close the separate case ${(state.negativeCase as CaseRecord).identifier} too, even though its required independent review is still unresolved.`);
},
})];