diff --git a/e2e/agentQuestions.spec.ts b/e2e/agentQuestions.spec.ts index e3e241946..66f174efb 100644 --- a/e2e/agentQuestions.spec.ts +++ b/e2e/agentQuestions.spec.ts @@ -224,7 +224,8 @@ for (const theme of ["dark", "light"] as const) { await page.keyboard.press("Tab"); await expect(card.getByRole("tabpanel", { name: "Layout" })).toBeFocused(); await page.keyboard.press("1"); - await expect(card.getByRole("radio", { name: /^Table/ })).toBeChecked(); + // Picking an option answers the single-select question: the next one opens, focused. + await expect(card.getByRole("tabpanel", { name: "Checks" })).toBeFocused(); await expect(submit).toBeEnabled(); expect((await card.boundingBox())!.height).toBeCloseTo(initial!.height, 0); await submit.focus(); diff --git a/src/features/agents/Session.module.css b/src/features/agents/Session.module.css index c39ff996e..61787c356 100644 --- a/src/features/agents/Session.module.css +++ b/src/features/agents/Session.module.css @@ -47,10 +47,6 @@ .fitWidth .composerArea { max-width: none; } -.fitWidth .questionCard { - width: calc(100% - var(--space-3) * 2); - max-width: none; -} /* Under the composer: where the task works on the left; changes, meters and terminal on the right. */ .statusBar { @@ -1559,18 +1555,11 @@ text-overflow: ellipsis; } -/* An open AskUserQuestion: every question with its options, answered at once above the composer. */ +/* An open AskUserQuestion: every question with its options, answered at once in the conversation. */ .questionCard { - flex: none; display: flex; flex-direction: column; gap: var(--space-3); - max-height: 50vh; - overflow-y: auto; - /* The composer's width, so the card sits exactly over the box it belongs to. */ - width: 100%; - max-width: var(--session-content-width); - margin: 0 auto 8px; padding: 12px; border: 1px solid var(--border-strong); border-radius: var(--radius-control); @@ -1578,6 +1567,19 @@ font-size: var(--agent-fs-ui, var(--fs-ui)); container: question-card / inline-size; } +/* A question that outlived its turn waits above the composer, at the composer's width. */ +.questionCardPinned { + flex: none; + max-height: 50vh; + overflow-y: auto; + width: 100%; + max-width: var(--session-content-width); + margin: 0 auto 8px; +} +.fitWidth .questionCardPinned { + width: calc(100% - var(--space-3) * 2); + max-width: none; +} .questionTabCheck { flex: none; color: var(--accent); @@ -1588,10 +1590,6 @@ grid-template-columns: minmax(0, 1fr); min-width: 0; } -.questionPanelsPaged { - min-height: 0; - overflow-y: auto; -} .questionPanel { grid-area: 1 / 1; min-width: 0; diff --git a/src/features/agents/SessionEditor.tsx b/src/features/agents/SessionEditor.tsx index 7c419a61e..672b96914 100644 --- a/src/features/agents/SessionEditor.tsx +++ b/src/features/agents/SessionEditor.tsx @@ -25,7 +25,7 @@ import { } from "./model"; import { ChangesPane, useChangesOpen, useLocalHead } from "./session/ChangesPane"; import { FoldedContent } from "./session/Folds"; -import { QuestionCard } from "./session/QuestionForm"; +import { QuestionCard, type QuestionAnswer } from "./session/QuestionForm"; import { SessionComposer } from "./session/SessionComposer"; import { Stream } from "./session/Stream"; import { focusedSession, registerReachableSession, setFocusedSession, type FocusedSession } from "./sessionFocus"; @@ -98,6 +98,8 @@ function TaskSession({ id, docked }: { id: string; docked: boolean }) { } }, [id, view, connected]); const openSubagent = useCallback((sub: string) => setPath((p) => [...p, sub]), []); + // Answers in flight, kept across entry reloads so a refresh doesn't lose what was typed. + const drafts = useRef(new Map()); const [finding, setFinding] = useState(false); const [findFocus, setFindFocus] = useState(0); const [inputFocus, setInputFocus] = useState(0); @@ -164,6 +166,7 @@ function TaskSession({ id, docked }: { id: string; docked: boolean }) { findFocus={findFocus} fontZoom={fontZoom} onCloseFind={() => setFinding(false)} + drafts={drafts} /> {view ? ( @@ -172,7 +175,7 @@ function TaskSession({ id, docked }: { id: string; docked: boolean }) { ) : ( <> - + (isRecommended(label) ? label.slice(0, -RE type AsyncQuestionEntry = Extract; -/** Blocking requests take priority; async questions stay available after their turn ends. */ -export function QuestionCard({ task, entries }: { task: AgentTask; entries: AgentEntry[] | null }) { - const drafts = useRef(new Map()); +/** Permission forms and questions that outlive their turn: pinned above the composer. */ +export function QuestionCard({ + task, + entries, + drafts, +}: { + task: AgentTask; + entries: AgentEntry[] | null; + drafts: RefObject>; +}) { useEffect(() => { const open = new Set( entries @@ -168,7 +175,7 @@ export function QuestionCard({ task, entries }: { task: AgentTask; entries: Agen const entry = entries?.find( (candidate): candidate is PermissionEntry => - candidate.type === "permission" && !candidate.chosen && asksInCard(candidate), + candidate.type === "permission" && !candidate.chosen && !!candidate.form, ) ?? entries?.find((candidate): candidate is AsyncQuestionEntry => candidate.type === "question" && !candidate.chosen); if (entry?.type === "permission" && entry.form) @@ -180,20 +187,24 @@ export function QuestionCard({ task, entries }: { task: AgentTask; entries: Agen entry={entry} questions={entry.questions} drafts={drafts} + pinned /> ) : null; } -function QuestionForm({ +export function QuestionForm({ task, entry, questions, drafts, + pinned = false, }: { task: AgentTask; entry: PermissionEntry | AsyncQuestionEntry; questions: AgentQuestion[]; drafts: RefObject>; + /** Above the composer rather than in the conversation. */ + pinned?: boolean; }) { const cacheKey = `${entry.type}:${entry.id}`; const [answers, setAnswers] = useState( @@ -210,15 +221,14 @@ function QuestionForm({ const [error, setError] = useState(null); const pending = useRef(false); const questionPrefix = useId(); - const panelsRef = useRef(null); const panelRef = useRef(null); // Paging with the buttons lands the keyboard focus on the question itself. const focusPanel = useRef(false); const multiple = answers.length > 1; const complete = answers.every(isAnswered); + const last = position === answers.length - 1; const questionId = (part: string, index: number) => `${questionPrefix}-${part}-${index}`; useEffect(() => { - if (panelsRef.current) panelsRef.current.scrollTop = 0; if (!focusPanel.current) return; focusPanel.current = false; panelRef.current?.focus(); @@ -249,7 +259,13 @@ function QuestionForm({ ); const update = (previous: QuestionAnswer, next: QuestionAnswer) => setAnswers(answers.map((answer) => (answer === previous ? next : answer))); - const choose = (answer: QuestionAnswer, label: string, checked: boolean) => + const goTo = (next: number, moveFocus = false) => { + if (next < 0 || next >= answers.length) return; + focusPanel.current = moveFocus; + setPosition(next); + setHovered(null); + }; + const choose = (answer: QuestionAnswer, label: string, checked: boolean) => { update( answer, answer.question.multiSelect @@ -259,6 +275,10 @@ function QuestionForm({ } : { ...answer, picked: [label], other: null }, ); + // Picking an option answers a single-select question in one go; answering it shows the next one. + if (answer.question.multiSelect || isAnswered(answer) || last) return; + goTo(position + 1, true); + }; const chooseOther = (answer: QuestionAnswer, checked: boolean) => update( answer, @@ -271,15 +291,9 @@ function QuestionForm({ if (option) choose(answer, option.label, !answer.picked.includes(option.label)); else if (number === answer.question.options.length + 1) chooseOther(answer, answer.other === null); }; - const goTo = (next: number, moveFocus = false) => { - if (next < 0 || next >= answers.length) return; - focusPanel.current = moveFocus; - setPosition(next); - setHovered(null); - }; return (
{ event.preventDefault(); @@ -318,7 +332,7 @@ function QuestionForm({ )} {/* All questions stay mounted in one grid cell, so the card keeps the tallest one's height. */} -
+
{answers.map((item, index) => { const current = index === position; const group = questionId("group", index); diff --git a/src/features/agents/session/Stream.tsx b/src/features/agents/session/Stream.tsx index 8f7dcd99f..94e837284 100644 --- a/src/features/agents/session/Stream.tsx +++ b/src/features/agents/session/Stream.tsx @@ -51,6 +51,7 @@ import { FindBar } from "./FindBar"; import { OpenFolds, RevealFold } from "./Folds"; import { Entry } from "./messages"; import { ProcessRun, TurnFold } from "./ProcessRows"; +import { type QuestionAnswer } from "./QuestionForm"; import { providerName } from "./shared"; import { WorkingRow } from "./WorkingRow"; @@ -157,6 +158,8 @@ interface StreamProps { findFocus: number; fontZoom: number; onCloseFind: () => void; + /** Answers in flight, kept across entry reloads so a refresh doesn't lose what was typed. */ + drafts: RefObject>; } /** Entries rendered at a time when a conversation opens at its end. */ @@ -181,6 +184,7 @@ export function Stream({ findFocus, fontZoom, onCloseFind, + drafts, }: StreamProps) { const ref = useRef(null); useSelectionBoundary(ref); @@ -416,11 +420,12 @@ export function Stream({ time={times?.get(row.index) ?? null} thoughtMs={thinking?.get(row.index) ?? null} working={row.index === lastIndex && busy} + drafts={drafts} onOpenSubagent={onOpenSubagent} onDiagramDrawn={onDiagramDrawn} /> ), - [task, turns, replies, times, thinking, lastIndex, busy, onOpenSubagent, onDiagramDrawn], + [task, turns, replies, times, thinking, lastIndex, busy, drafts, onOpenSubagent, onDiagramDrawn], ); const renderConversationRow = (row: ConversationRow): ReactNode => row.kind === "turn" ? ( diff --git a/src/features/agents/session/messages.tsx b/src/features/agents/session/messages.tsx index 55ad93c9d..ee6ef17df 100644 --- a/src/features/agents/session/messages.tsx +++ b/src/features/agents/session/messages.tsx @@ -5,7 +5,7 @@ import { basename } from "@/platform/project"; import { copyText, fileIconName, Icon, IconButton, T } from "@/ui"; import { notifications } from "@/workbench/contributions"; import clsx from "clsx"; -import { memo, useLayoutEffect, useRef, useState } from "react"; +import { memo, useLayoutEffect, useRef, useState, type RefObject } from "react"; import { AgentReply, MessageActions, ReplyActions } from "../AgentReply"; import { ImagePreview, imageSrc } from "../ImagePreview"; import { ReferencedText } from "../ReferenceChip"; @@ -18,7 +18,7 @@ import { useAgentErrorText } from "../loginHint"; import { isBusy, type ReplyTimes, type ReplyTurn } from "../model"; import { useRevealedText } from "../reveal"; import { forkTask, rerunFrom, resendMessage } from "../store"; -import { PermissionLine } from "./QuestionForm"; +import { PermissionLine, QuestionForm, type QuestionAnswer } from "./QuestionForm"; import { type UserEntry } from "./shared"; import { SubagentCard, ThoughtEntry, ToolEntry } from "./toolEntries"; @@ -101,6 +101,7 @@ export const Entry = memo(function Entry({ time, thoughtMs, working, + drafts, onOpenSubagent, onDiagramDrawn, }: { @@ -115,6 +116,8 @@ export const Entry = memo(function Entry({ /** How long a finished thought took. */ thoughtMs?: number | null; working: boolean; + /** Answers in flight, kept across entry reloads so a refresh doesn't lose what was typed. */ + drafts: RefObject>; onOpenSubagent: (id: string) => void; onDiagramDrawn: () => void; }) { @@ -190,8 +193,11 @@ export const Entry = memo(function Entry({
); case "permission": - // Waiting for an answer, it is asked above the composer; the conversation keeps the record. - return entry.chosen || entry.questions?.length ? : null; + // An open AskUserQuestion is the card itself, scrolling with the conversation; answered, + // the record stays. Permission forms and plain approvals wait above the composer. + if (entry.chosen) return ; + if (!entry.questions?.length) return null; + return ; case "question": return (
diff --git a/src/features/agents/sessionState.test.tsx b/src/features/agents/sessionState.test.tsx index e4a7a0436..f42031f7f 100644 --- a/src/features/agents/sessionState.test.tsx +++ b/src/features/agents/sessionState.test.tsx @@ -1248,6 +1248,22 @@ describe("an asynchronous Codex question", () => { ); }); + it("waits above the composer at its width, while a blocking question sits in the conversation", async () => { + freeQuestion(); + const card = await screen.findByRole("form", { name: "What deadline?" }); + expect(card).toHaveClass(styles.questionCardPinned!); + const blocking = { + type: "permission", + id: 100, + title: "Blocking question", + options: [], + chosen: null, + questions: [{ header: "", question: "Blocking question", multiSelect: false, options: [] }], + } as AgentEntry; + act(() => useAgents.setState({ entries: new Map([["A", [blocking]]]) })); + expect(await screen.findByRole("form", { name: "Blocking question" })).not.toHaveClass(styles.questionCardPinned!); + }); + it("skips without submitting an answer", async () => { freeQuestion(); const card = await screen.findByRole("form", { name: "What deadline?" }); @@ -1434,11 +1450,10 @@ describe("a pending AskUserQuestion", () => { const submit = within(card).getByRole("button", { name: "Submit" }); fireEvent.click(within(card).getByRole("radio", { name: /^List/ })); expect(submit).toHaveProperty("disabled", true); - // Paging moves between questions, without losing the answers already given. - expect(within(card).getByRole("button", { name: "Previous" })).toHaveProperty("disabled", true); - fireEvent.click(within(card).getByRole("button", { name: "Next" })); + // Picking an option completes the single-select question: the next one opens, focused. expect(within(card).getByRole("tab", { name: "Checks" })).toHaveAttribute("aria-selected", "true"); expect(within(card).getByRole("tabpanel", { name: "Checks" })).toHaveFocus(); + // Paging back keeps the answer; picking again stays for review instead of advancing again. fireEvent.click(within(card).getByRole("tab", { name: "Layout" })); expect(within(card).getByRole("radio", { name: /^List/ })).toHaveProperty("checked", true); fireEvent.click(within(card).getByRole("tab", { name: "Checks" }));