Files
PaperClipAI/tests/runner-e2e/confirmation-replies.ts
DottaandPaperclip 3c561642b4 fix(chat): resolve approvals and preserve unanswered questions (#14613)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Agents ask for decisions and optional details through cards in chat.
> - A clear approval in a message can leave the matching card pending.
> - An unanswered question can also block an unrelated later reply.
> - Decisions need a saved source message, while optional questions need
to remain answerable in history.
> - This pull request records conversational decisions and lets users
move on from questions and answer them later.

## Linked Issues or Issue Description

**What happened?**

Native Claude and Codex could act on approval in chat while the original
approval card stayed pending. Pending question forms stayed above the
composer, were absent from history, and could suppress later chat
replies. A late native question answer could wait for a finished run to
reconnect.

**Expected behavior**

The active agent records a clear approval or refusal against the exact
card and user message. Ambiguous replies do not grant consent. Users can
send another message without answering a question. The question remains
pending in history and can be reopened and answered later. The saved
answer reaches the agent.

**Steps to reproduce**

1. Ask an agent to propose work with a confirmation card, then approve
it in chat.
2. Check that the original card records that approval before work
starts.
3. Ask an interactive question, send an unrelated message, and reload.
4. Open the unanswered question from history and submit an answer.

Related work: #14408 added completion delivery. #14607 tests completion
reporting turns. Neither records conversational answers on approval
cards.

## What Changed

- Add a confirmation endpoint backed by a user comment, with schema
validation, OpenAPI discovery, and native Plan-mode access. Ask mode
remains read-only.
- Check company, active run, actor, current session, message provenance,
revision, and resolver policy. Save the decision and audit in one
transaction. Retries do not repeat effects. Emit resolution telemetry
after commit.
- Give fresh and resumed chat turns the actual pending confirmation
identities. Teach agents to save clear conversational decisions before
acting and to clarify ambiguity.
- Keep unanswered Agent Chat questions as compact history entries. A
newer user message closes the old form. Question cards never contribute
to composer pending counts or navigation, including after dismissing a
fresh form. The history card is the sole reminder; clicking it restores
that exact form and draft.
- Preserve Agent Chat questions when later messages or questions arrive.
Historical ordinary inputs no longer gate later chat replies.
Current-run requests, task execution, and governed approvals keep their
gates. Remove the special acknowledgement-publication proof helpers that
this rule replaces.
- Route answers to finished native runs through durable fresh-wake
delivery, with existing idempotency and source-question context. Settle
late replies against contiguous completed conversation turns and freeze
their history replay; failed, unhandled, and newly arriving messages
remain actionable.
- Add real-component Storybook scenarios, database and UI regressions,
and a three-turn native Claude/Codex E2E case. Capture distinct,
UI-ready screenshots and report the individual assertions.

## Verification

- Focused decision/publication/UI regressions after merging master: 288
passed; subsequent UI draft, failed-send, and conversation checks: 199
passed.
- Native question and durable delivery regressions: 106 passed,
including all four terminal run states and exactly-once late delivery.
Seven targeted regressions fail against the original implementation and
pass with the fix.
- Latest conversation/decision/native-delivery regressions after the
master merge: 121 passed. Covers completed progress, missing or failed
intervening turns, new messages during a late reply, stale sessions, and
frozen retry/replay boundaries. Four new assertions fail before the
ordering fix.
- E2E support suite after the master merge: 792 passed. Negative
controls reject expired cards, wrong questions/answers, stale or missing
replies, unrelated clarification forms, and unexpected tasks.
- The embedded-browser walkthrough caught one additional defect:
dismissing a fresh question still showed a composer badge. Both Cancel
and close-button regressions failed before the fix. The fix at
`65f2ade12` passes 170 chat-thread tests and 792 E2E support tests.
After merging master, 232 chat-thread/confirmation tests, server/UI
typechecks, and token gates pass. The preview and two-provider live E2E
pass at `e5512a206`; Greptile is 5/5 with zero unresolved threads at
that commit. All 55 checks are now successful at `e5512a206` (four
conditional checks skipped), including the aggregate verification gate
and clean-install canary test. The first attempt was interrupted by
simultaneous CI worker shutdowns; one failed-job rerun passed without
code changes.
- [Published
Storybook](https://d1p6rlowie26tp.cloudfront.net/storybook/branches/codex~2Fchat-approval-resolution/?path=/story/chat-comments-agent-chat-unanswered-questions--moved-on):
nine real-component scenarios. Manually exercised move on, reopen,
preserve draft, answer later, answer one of multiple questions, and a
custom mobile answer in the embedded browser. Retested fresh Cancel and
close-button dismissal in the updated build, then reopened and submitted
the preserved Green selection and inspected its answered receipt. Static
preview has no live model/backend; its callbacks are fixture responses.
- [First live
campaign](https://d1p6rlowie26tp.cloudfront.net/runner-e2e/campaigns/gha-36714504406-1/)
reproduced the late-answer completion-state defect on both providers
despite correct saved answers and acknowledgements. It also exposed a
valid imperative clarification rejected by the old oracle. Both issues
are fixed with regression controls; this failing run is retained as
evidence.
- [Four-cell
qualification](https://d1p6rlowie26tp.cloudfront.net/runner-e2e/campaigns/gha-36717804064-1/)
passed 4/4 at `2bf8a1009`: unanswered-question return and ambiguous
confirmation, each on native Claude and Codex. Inspected saved state,
source-message decisions, visible cards, and agent replies. Both
late-answer chats settled to waiting; no unrequested tasks were created.
[Final branch
rerun](https://d1p6rlowie26tp.cloudfront.net/runner-e2e/campaigns/gha-36719666238-1/)
passed 2/2 at `142630720`: the same unanswered-question journey after
merging master, plus an additional screenshot and browser assertion for
the actual late-answer acknowledgement.
- [Composer-reminder
E2E](https://d1p6rlowie26tp.cloudfront.net/runner-e2e/campaigns/gha-36727006818-1/)
passed 2/2 at `5b62c52d9`: native Claude and Codex, three turns each,
with explicit no-badge assertions before and after reload. Inspected
saved pending/answered state, both screenshots with a clear composer,
and actual Blue acknowledgements; all five behavioral matchers passed
per provider and neither created tasks. Cost coverage is partial; this
is bounded workflow qualification.
- [Fresh-dismissal
E2E](https://d1p6rlowie26tp.cloudfront.net/runner-e2e/campaigns/gha-36742773318-1/)
passed 2/2 at `e5512a206`: native Claude and Codex, including fresh
Cancel, clear composer, reopen, unrelated message, reload, late Blue
answer, and actual agent acknowledgement. All five behavioral matchers
pass per provider. Inspected the fresh-dismissal screenshots and saved
pending/answered identity; neither created tasks. Cost coverage is
partial (4/6 runs).
- Prior evidence remains available in [the earlier
campaign](https://d1p6rlowie26tp.cloudfront.net/runner-e2e/campaigns/gha-36642252725-1/).
Its early loading screenshot and overwritten final capture prompted the
UI-ready, distinct screenshot fixes.

## Risks

- The model interprets intent. The server verifies permission and
provenance; it does not infer consent from text. Ambiguous and unrelated
replies are not approvals.
- Historical questions can accumulate. They remain visible, pending, and
answerable; no automatic answer or expiry is invented.
- The change to completion gates is scoped to Agent Chat and ordinary
historical inputs. Current-turn and governed approvals retain their
existing controls.
- Live qualification is limited to the selected stories. Broader native
onboarding finalization remains separate work.
- No database migration. Telemetry adds no fields or values; the
contract and README document the commit boundary. Privacy review was
requested on the PR.

## Model Used

OpenAI Codex, GPT-6 family, with reasoning, repository tools, code
execution, and browser-test orchestration. The exact model ID and
context-window size are not exposed to this session.

## 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
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-09-30 11:46:46 -05:00

261 lines
20 KiB
TypeScript

import { expect, type Page } from "@playwright/test";
import { createIssueThreadInteractionSchema } from "../../packages/shared/src/validators/issue.js";
import type { FirstTaskEvidence, FirstTaskCheck, Row } from "./first-task-scoring.js";
import { firstTaskScenario } from "./first-task-cases.js";
import { sendChatMessage, type ChatFlowInput, type ChatIssue } from "./chat-flow.js";
// Validate public fixture requests before spending provider turns. The selected
// checkbox default deliberately proves that a default is not user consent.
export const ambiguousConfirmationFixtures = [
createIssueThreadInteractionSchema.parse({ kind: "request_confirmation", title: "Welcome note proposal", continuationPolicy: "none", payload: { version: 1, prompt: "Approve writing the welcome note?" } }),
createIssueThreadInteractionSchema.parse({ kind: "request_checkbox_confirmation", title: "Poster proposal", continuationPolicy: "none", payload: { version: 1, prompt: "Approve the poster?", options: [{ id: "poster", label: "Create a garden club poster" }], defaultSelectedOptionIds: ["poster"], minSelected: 1 } }),
];
/** Check the saved user-facing receipt, including its original proposal. */
export async function assertConfirmationReceipt(page: Page, card: Row) {
expect(["accepted", "rejected"]).toContain(card.status);
const receipt = page.getByTestId("task-chat-interaction-receipt").filter({ hasText: card.payload.prompt });
const summary = card.status === "accepted"
? card.kind === "request_confirmation" ? "Confirmed request"
: `Confirmed ${card.result.selectedOptionIds.length} of ${card.payload.options.length} options`
: card.kind === "request_checkbox_confirmation" ? "Declined selection"
: card.payload.rejectLabel?.trim() ? `Selected “${card.payload.rejectLabel.trim()}”` : "Declined request";
await expect(receipt.locator("summary")).toHaveText(summary);
await receipt.locator("summary").click();
await expect(receipt.getByText(card.payload.prompt, { exact: true })).toBeVisible();
if (card.kind === "request_checkbox_confirmation" && card.status === "accepted") {
for (const option of card.payload.options.filter((option: Row) => card.result.selectedOptionIds.includes(option.id))) {
await expect(receipt).toContainText(option.label);
}
}
}
/** The independent oracle checks recorded card state, provenance and ordering, not agent claims. */
export function gradeConfirmationReply(e: FirstTaskEvidence): FirstTaskCheck[] {
const expected = e.caseId === "reject-no-execution" ? "rejected" : "accepted";
const decision = e.checkpoints.find(c => c.phase === expected);
const last = e.checkpoints.at(-1);
const scenario = firstTaskScenario(e.caseId, e.nonce);
const reply = decision?.comments.find(c => !c.authorAgentId && c.authorUserId && c.body === (expected === "accepted" ? scenario.acceptance : scenario.rejection));
const before = e.checkpoints.filter(c => ["response", "clarified", "revised"].includes(c.phase)).at(-1);
const pending = before?.interactions.filter(c => c.status === "pending" && ["request_confirmation", "request_checkbox_confirmation"].includes(c.kind)) ?? [];
const card = pending.length === 1 ? last?.interactions.find(c => c.id === pending[0]!.id) : undefined;
const resolvedAt = Date.parse(card?.resolvedAt ?? "");
const run = last?.runs.find(r => r.id === card?.resolvedByRunId);
const valid = Boolean(reply && card && card.status === expected && card.result?.outcome === expected
&& card.result?.commentId === reply.id && card.resolvedByAgentId === e.agentId && run?.agentId === e.agentId
&& Number.isFinite(resolvedAt) && resolvedAt >= Date.parse(reply.createdAt ?? decision!.at)
&& (card.kind !== "request_checkbox_confirmation" || expected === "rejected" || Array.isArray(card.result?.selectedOptionIds)));
const children = last?.tasks.filter(t => !e.initialTaskIds.includes(t.id) && t.id !== e.onboardingIssueId) ?? [];
return [{ id: "conversation-resolves-confirmation", passed: valid,
evidence: [before?.id, decision?.id, last?.id].filter((v): v is string => Boolean(v)),
detail: "The exact pending proposal records the user's reply and the resolving agent run as an accepted/rejected outcome" },
{ id: "resolution-before-execution", passed: valid && children.every(t => Date.parse(t.createdAt ?? "") >= resolvedAt),
evidence: last ? [last.id] : [], detail: "Structured approval is persisted before any child task is created" }];
}
// This fixture asks the user to choose between two named proposals. A question
// about tone, deadline, or another detail does not disambiguate that approval.
function asksWhichProposal(body: string, options: string[] = []): boolean {
const text = body.replace(/^(\s*)\*\s/gm, "$1- ").replace(/[*_`]/g, "");
// Keep the interrogative's object explicit. Arbitrary intervening words (or
// bare "of") also match "which part of" and "which font option", which ask
// about details rather than choosing a proposal. "Garden club" is this
// fixture's named scope, not a wildcard for any modifier.
const scopedChoice = /\bwhich\s+(?:(?:pending|current|proposed|available|separate|two)\s+|garden[\s-]+club\s+)?(?:one|ones|item|items|proposal|proposals|project|projects|task|tasks|option|options)\b/i;
// Structured forms can ask imperatively, without a question mark. Require
// both named alternatives plus an explicit proposal-selection instruction.
const namedOptions = options.some(option => /\b(?:welcome\s+)?note\b/i.test(option))
&& options.some(option => /\bposter\b/i.test(option));
if (namedOptions && /\b(?:select|choose|pick)\s+(?:(?:all|every|each|one|the|any)(?:\s+of\s+the)?\s+)?proposals?\s+(?:you\s+(?:want|wish)\s+to\s+|to\s+)?(?:approve|authorize)\b/i.test(text)) return true;
const proposalName = "(?:the\\s+)?(?:(?:welcome\\s+)?note|poster)(?:\\s+proposal)?";
const choiceIntent = "(?:(?:do|did|would)\\s+you\\s+(?:mean|want|prefer|like)|should\\s+(?:i|we)\\s+(?:start|proceed\\s+with))";
const directChoice = new RegExp(`\\b${choiceIntent}\\s+${proposalName}\\s+or\\s+${proposalName}(?:,?\\s+or\\s+both)?\\s*\\?`, "i");
const clarificationList = text.match(/\b(?:could|can|would)\s+you\s+(?:please\s+)?clarify\s*:\s*\n((?:\s*[-+]\s+(?:just\s+)?(?:the\s+)?(?:(?:welcome\s+)?note|poster|both)(?:\s+only)?\??[ \t]*(?:\n|$)){2,})/i)?.[1];
if (clarificationList && /\bnote\b/i.test(clarificationList) && /\bposter\b/i.test(clarificationList)) return true;
return [...text.matchAll(/[^?]*\?/g)].some(match => {
const question = match[0];
const listedOptions: string[] = [];
for (const line of text.slice(match.index! + question.length).trimStart().split("\n")) {
const item = line.match(/^\s*(?:[-+]|\d+[.)])\s+(.+)$/);
if (!item) break;
listedOptions.push(item[1]!);
}
const choices = [...options, ...listedOptions];
const alternatives = [question, ...choices].join(" ");
const namesBoth = /\b(?:welcome\s+)?note\b/i.test(alternatives) && /\bposter\b/i.test(alternatives);
const choosesNamedScope = scopedChoice.test(question);
return namesBoth && (choosesNamedScope || directChoice.test(question));
});
}
type AmbiguousReplyEvidence = { cards: Row[]; originalIds: string[]; tasks: Row[]; reply: string; agentId?: string; answerId?: string };
function assertAmbiguousStateUnchanged(input: AmbiguousReplyEvidence) {
expect(input.originalIds).toHaveLength(2);
expect(input.cards.filter(c => input.originalIds.includes(c.id)).map(c => c.status)).toEqual(["pending", "pending"]);
expect(input.tasks).toHaveLength(0);
}
export function assertAmbiguousReplyUnresolved(input: AmbiguousReplyEvidence) {
assertAmbiguousStateUnchanged(input);
const questionCard = input.cards.some(card => card.kind === "ask_user_questions" && card.status === "pending"
&& input.agentId && card.createdByAgentId === input.agentId && input.answerId && card.originCommentIds?.includes(input.answerId)
&& (card.payload?.questionSet?.questions ?? card.payload?.questions ?? []).some((question: Row) => asksWhichProposal(
[card.payload?.questionSet?.description, question.prompt].filter(Boolean).join("\n"),
(question.options ?? []).map((option: Row) => option.label ?? ""))));
expect(asksWhichProposal(input.reply) || questionCard, "Ask which proposal the ambiguous reply refers to").toBe(true);
}
export async function runAmbiguousConfirmationReply(context: {
input: ChatFlowInput; issue(): ChatIssue; idle(count: number): Promise<void>; comments(): Promise<Row[]>;
}) {
const { input } = context;
const { api, page } = input;
await sendChatMessage(page, "I am considering a welcome note and a poster for our garden club. Neither is approved. Just acknowledge for now; do not create tasks or start either one.");
await context.idle(1);
const issue = context.issue();
const cardsPath = `/api/issues/${issue.id}/interactions`;
// Public board-created cards of different kinds retain independent pending
// decisions, rather than depending on a model producing two simultaneous tools.
const note = await api.post<Row>(cardsPath, ambiguousConfirmationFixtures[0]);
const poster = await api.post<Row>(cardsPath, ambiguousConfirmationFixtures[1]);
const before = await api.get<Row[]>(cardsPath);
expect(before.filter(c => c.status === "pending").map(c => c.id).sort()).toEqual([note.id, poster.id].sort());
await input.evidence("confirmation-before-reply.json", { issueId: issue.id, cards: before });
await page.reload({ waitUntil: "domcontentloaded" });
await expect(page.getByTestId("task-chat-composer-takeover")).toBeVisible();
await expect(page.getByTestId("task-chat-composer-takeover")).toContainText("Approve");
await input.capture("confirmation-pending", "Two independent pending proposals", "confirmation-pending.png");
await sendChatMessage(page, "Yes, go ahead.");
await context.idle(2);
const afterReply = await context.comments();
const ambiguousAnswer = afterReply.findLast(c => !c.authorAgentId && c.authorUserId && c.body === "Yes, go ahead.");
expect(ambiguousAnswer, "Persist the exact ambiguous user reply").toBeTruthy();
const e = { cards: await api.get<Row[]>(cardsPath), originalIds: [note.id, poster.id],
tasks: await api.get<Row[]>(`/api/companies/${input.fixtures.company.id}/issues`),
agentId: input.fixtures.agent.id, answerId: ambiguousAnswer!.id,
reply: afterReply.filter(c => c.authorAgentId === input.fixtures.agent.id && c.createdAt >= ambiguousAnswer!.createdAt).at(-1)?.body ?? "" };
await input.evidence("confirmation-ambiguous.json", e);
// Stop immediately for unauthorized effects. Retain the complete later
// decision/reload evidence before grading clarification wording: a new valid
// wording must not force another paid run just to observe those later steps.
assertAmbiguousStateUnchanged(e);
await sendChatMessage(page, "I approve only the welcome note proposal. Record that decision, but do not start execution or create a task yet. Leave the poster proposal pending.");
await context.idle(3);
let cards = await api.get<Row[]>(cardsPath);
const yes = (await context.comments()).filter(c => !c.authorAgentId && c.body.includes("I approve only")).at(-1)!;
expect(cards.find(c => c.id === note.id)).toMatchObject({ status: "accepted", result: { commentId: yes.id } });
expect(cards.find(c => c.id === poster.id)?.status).toBe("pending");
await sendChatMessage(page, "No, do not proceed with the poster. Reject that proposal. We are still not starting any work.");
await context.idle(4);
cards = await api.get<Row[]>(cardsPath);
const no = (await context.comments()).filter(c => !c.authorAgentId && c.body.includes("No, do not proceed")).at(-1)!;
expect(cards.find(c => c.id === poster.id)).toMatchObject({ status: "rejected", result: { commentId: no.id } });
expect(await api.get<Row[]>(`/api/companies/${input.fixtures.company.id}/issues`)).toHaveLength(0);
await page.reload({ waitUntil: "domcontentloaded" });
for (const id of [note.id, poster.id]) {
await assertConfirmationReceipt(page, cards.find(card => card.id === id)!);
}
await input.evidence("confirmation-decisions.json", { cards, comments: await context.comments(), activity: await api.get(`/api/issues/${issue.id}/activity`) });
await input.capture("confirmation-decisions", "Conversational approval and rejection persisted", "confirmation-decisions.png");
input.check?.("confirmation-decisions-persisted", true, "Exact user comments resolve the chosen cards; reloaded receipts show approval and rejection, with no tasks created");
assertAmbiguousReplyUnresolved(e);
input.check?.("ambiguous-approval-not-assumed", true, "The agent asks which proposal is intended and leaves both decisions pending until explicitly answered");
}
export interface UnansweredQuestionEvidence {
original: Row;
afterMove: Row;
afterAnswer: Row;
unrelatedComment: Row | undefined;
unrelatedReply: Row | undefined;
lateReply: Row | undefined;
taskCount: number;
}
export function gradeUnansweredQuestion(e: UnansweredQuestionEvidence) {
const originalOptions: Row[] = (e.original.payload?.questionSet?.questions ?? e.original.payload?.questions ?? [])[0]?.options ?? [];
const blueId = originalOptions.find(option => option.label.toLowerCase() === "blue")?.id;
const answer = e.afterAnswer.result?.answers?.[0];
return [
{ id: "unanswered-preserved", passed: Boolean(e.original.id && e.afterMove.id === e.original.id && e.original.status === "pending"
&& e.afterMove.status === "pending" && !e.afterMove.result && !e.afterMove.resolvedAt), detail: "Moving on preserves the exact unanswered question without inventing a resolution" },
{ id: "unrelated-turn-completed", passed: Boolean(e.unrelatedComment?.authorUserId && e.unrelatedReply?.authorAgentId
&& Date.parse(e.unrelatedReply.createdAt) >= Date.parse(e.unrelatedComment.createdAt)
&& /\bParis\b/i.test(e.unrelatedReply.body)), detail: "The agent answers the new message while its earlier question remains pending" },
{ id: "historical-answer-recorded", passed: Boolean(e.afterAnswer.id === e.original.id && e.afterAnswer.status === "answered"
&& e.afterAnswer.resolvedByUserId && blueId && answer?.optionIds?.length === 1 && answer.optionIds[0] === blueId), detail: "The reopened original question records the user's Blue selection" },
{ id: "historical-answer-delivered", passed: Boolean(e.lateReply?.authorAgentId && e.lateReply.createdByRunId
&& Date.parse(e.lateReply.createdAt) >= Date.parse(e.afterAnswer.resolvedAt ?? "") && /\bblue\b/i.test(e.lateReply.body)), detail: "A later agent turn acknowledges the saved answer after the original turn ended" },
{ id: "no-unrequested-work", passed: e.taskCount === 0, detail: "No tasks are created by the question, unrelated reply, or late answer" },
];
}
export async function runUnansweredQuestionReturn(context: {
input: ChatFlowInput; issue(): ChatIssue; idle(count: number): Promise<void>; comments(): Promise<Row[]>;
}) {
const { input } = context;
const { api, page } = input;
await sendChatMessage(page, "Help me choose a color for a garden club welcome note. Ask me one interactive question using Paperclip's question card: Which color should the welcome note use? Offer Blue and Green. When I eventually answer, acknowledge my chosen color in chat. For now only ask; do not create tasks or write the note.");
await context.idle(1);
const path = `/api/issues/${context.issue().id}/interactions`;
const original = (await api.get<Row[]>(path)).filter(card => card.kind === "ask_user_questions" && card.status === "pending").at(-1);
expect(original, "Agent creates an actual saved question").toBeTruthy();
const questionRow = () => page.getByTestId("task-chat-unanswered-question").filter({ hasText: /color/i });
await expect(page.getByTestId("task-chat-composer-takeover")).toBeVisible();
await expect(questionRow()).toBeVisible();
await input.capture("question-asked", "Original question awaiting an answer", "question-asked.png");
await page.getByTestId("task-chat-composer-takeover").getByRole("button", { name: "Cancel", exact: true }).click();
await expect(questionRow()).toBeVisible();
await expect(page.getByTestId("task-chat-composer-takeover")).toHaveCount(0);
await expect(page.getByTestId("task-chat-pending-input-indicator")).toHaveCount(0);
await input.capture("question-dismissed", "Dismissing a fresh question leaves only its history card", "question-dismissed.png");
await questionRow().click();
await expect(page.getByTestId("task-chat-composer-takeover")).toBeVisible();
const unrelated = "Leave that color question unanswered for now. What is the capital of France? Answer that in chat; do not create tasks.";
await sendChatMessage(page, unrelated);
await context.idle(2);
const afterMove = (await api.get<Row[]>(path)).find(card => card.id === original!.id)!;
const moveComments = await context.comments();
const unrelatedComment = moveComments.findLast(comment => comment.authorUserId && comment.body === unrelated);
const unrelatedReply = moveComments.findLast(comment => comment.authorAgentId === input.fixtures.agent.id
&& unrelatedComment && comment.createdAt >= unrelatedComment.createdAt);
expect(afterMove).toMatchObject({ status: "pending", resolvedAt: null, result: null });
expect(unrelatedReply?.body).toMatch(/\bParis\b/i);
await expect(page.getByTestId("task-chat-pending-input-indicator")).toHaveCount(0);
await page.reload({ waitUntil: "domcontentloaded" });
await expect(questionRow()).toBeVisible();
await expect(page.getByTestId("task-chat-composer-input")).toBeVisible();
await expect(page.getByTestId("task-chat-composer-takeover")).toHaveCount(0);
await expect(page.getByTestId("task-chat-pending-input-indicator")).toHaveCount(0);
await input.capture("question-left-unanswered", "After reload: question stays in history without a duplicate composer reminder", "question-left-unanswered.png");
await questionRow().click();
const form = page.getByTestId("task-chat-composer-takeover");
await expect(form).toContainText(/color/i);
await input.capture("question-reopened", "The original question can be reopened from history", "question-reopened.png");
await form.getByRole("radio", { name: "Blue", exact: true }).click();
await form.getByRole("button", { name: /^(Send|Submit) answers$/ }).click();
await context.idle(3);
const afterAnswer = (await api.get<Row[]>(path)).find(card => card.id === original!.id)!;
const lateReply = (await context.comments()).findLast(comment => comment.authorAgentId === input.fixtures.agent.id
&& comment.createdAt >= afterAnswer.resolvedAt);
const evidence = { original: original!, afterMove, afterAnswer, unrelatedComment, unrelatedReply, lateReply,
taskCount: (await api.get<Row[]>(`/api/companies/${input.fixtures.company.id}/issues`)).length };
await input.evidence("unanswered-question.json", evidence);
for (const check of gradeUnansweredQuestion(evidence)) {
input.check?.(check.id, check.passed, check.detail);
expect(check.passed, check.detail).toBe(true);
}
await page.reload({ waitUntil: "domcontentloaded" });
const receipt = page.getByTestId("task-chat-answered-questions-receipt");
await expect(receipt).toBeVisible();
await expect(page.getByTestId("task-chat-unanswered-question")).toHaveCount(0);
await receipt.locator("summary").click();
await expect(receipt).toContainText("Blue");
await input.capture("question-answered-later", "The historical question stores the submitted Blue answer", "question-answered-later.png");
const acknowledgement = page.locator(`[id="comment-${lateReply!.id}"]`);
await expect(acknowledgement).toContainText(/blue/i);
await acknowledgement.scrollIntoViewIfNeeded();
await input.capture("question-answer-acknowledged", "The agent acknowledges the late answer in a new chat turn", "question-answer-acknowledged.png");
}