Skip to content

feat(questions): answer a multi-question AskUserQuestion form - #1332

Open
RonenMars wants to merge 6 commits into
mainfrom
feat/multi-question-answer
Open

RonenMars wants to merge 6 commits into
mainfrom
feat/multi-question-answer

Conversation

@RonenMars

Copy link
Copy Markdown
Owner

Summary

POST /api/sessions/:id/prompt/answer can now answer a Claude Code AskUserQuestion form with several questions.
The request body is unchanged, and promptContract.multiQuestion: true advertises the support.

answer(multi-question form)
  validate every answer          # a refusal writes nothing
  lock the session's input
  read the screen
    if the form is scrolled: grow the terminal to 100 rows, once
  if the screen is not this form, or it has previews, or it still does not fit
    refuse, nothing typed
  visit every tab and the review before choosing anything
  for each key
    send it, then wait for the screen to show exactly its effect
    on a mismatch or timeout: stop, answer partly_answered
  submit, confirm the form closed
  restore the terminal size, unlock
  • Two new 409 codes: form_not_ready (nothing written, the prompt stays open) and partly_answered (some keys written, not retryable, finish in the terminal).
  • While a form is being driven, other input, raw keys and resizes are refused, and screen scrapes of the form's tabs do not reach clients.
  • atomicAnswer: true stays, and partly_answered is its one documented exception.
  • A scrolled or preview form raises no screen card.
    This covers both overflow layouts: a more line row, and a scrolled option list under a tab bar that stays put.
  • The streamer may send terminal_resize twice around an answer.
  • Free text, "Type something", "Chat about this", Codex, Cursor and Copilot are out of scope.

This also fixes a bug that is on main today.
A terminal resize while a question was pending could cancel it, so its answer got 409 prompt_cancelled.
Claude Code repaints in several chunks after a resize, and a read between them saw no menu.
The screen is now left unread for 300 ms after a size change, except for OSC 777 notifications.

Refs #1044

Evidence

  • Before: the two resize tests fail with main's pty-manager.ts.
    A real run at 30 rows got 409 prompt_cancelled 3 out of 3 times on this branch without the fix.
    At 12 rows, before the scrolled-list fix, the answer got 409 form_not_ready 2 out of 2 times with nothing typed.
    Replaying those recordings through the old detector gives a card with options missing.
    After: real runs against Claude Code 2.1.332, comparing the transcript's recorded answers with the chosen options.
Terminal rows Prompt Runs passed Resize
12 four questions 2 of 2 grew to 100, back to 12
16 four questions 1 of 1 none
24 tall form 1 of 1 grew to 100, back to 24
30 four questions 1 of 1 none
  • Mutants, run on each part as it was written, each killed by a test: overflow detection 7/7, form parser 27/27, wiring 48/48, resize hold 9/9, scrolled option list 10/10.
  • Full local suite on the final commit (macOS, Node 24.15.0): 456 files passed and 1 skipped, 5035 tests passed and 3 skipped, no timeouts.
  • npm run build and npm run lint exit 0 (30 warnings, 10 infos).

Not verified:

  • The web client was not run.
  • Forms taller than 100 rows are untested.
  • Codex, Cursor and Copilot repaint behaviour after a resize was not checked.
  • Fixtures come from Claude Code 2.1.332 only.

Known limits:

  • The resize hold is a fixed window, so a repaint that starts after it, or one that no resize caused, is read as it arrives.
  • After partly_answered, a form finished in the terminal unlocks the composer only at the next 60 second check.
  • A pty-host with live sessions keeps its old detectors until those sessions end.
  • Mobile releases older than the client resize handling can render garbled output while the terminal is grown.
  • tb-mobile main does not yet handle partly_answered, form_not_ready or multiQuestion; that is the next part of the work.

Merge Danger

Door: two-way.

The change is additive on the wire and adds no schema or stored data.
An answer that has already typed into a terminal cannot be taken back, which is why every key is checked against the screen before the next one.

Blast Radius: Claude sessions.

The input lock sits in LiveSessionManager, which every provider goes through, but only a multi-question answer takes it.
The resize hold and the unreadable-form check change how every Claude screen is read.

…ion form

A form taller than the terminal loses its tab bar and question, and stale scrollback above it supplied another tab's question, so clients could get a card that mixed two tabs.
A preview form produced option labels with the preview box in them.
Both now report a live question with an empty list, so the push and the later close still happen and no card is opened.
…e screen

A tab is returned only when its question text, option labels and single cursor match the prompt's questions.
A scrolled or preview form is reported as such, and anything else that does not match reads as null.
Every key is planned from the screen as it is, and the next one is written only after the screen shows what that key should have done.
Answers are checked before the first byte, and the review must list exactly the chosen options before it is submitted.
A stop after the first byte is reported as partial, since a key cannot be taken back.
`POST /api/sessions/:id/prompt/answer` now answers a Claude Code form with more than one question, one verified key at a time.
Every response is validated before the first byte, and a refusal writes nothing.
While the form is driven, no other writer can reach the session, and screen and transcript prompt events are dropped.
A form that scrolled gets a terminal 100 rows tall, and the previous size is restored afterwards.
A first question that is not on screen answers 409 `form_not_ready` and leaves the prompt open.
A stop after the first key answers 409 `partly_answered`, which is not retryable: the user finishes the form in the terminal.
`/api/info` advertises `promptContract.multiQuestion`.
Refs #1044
A mutation pass showed which guards in the form answer path no test depended on, so each now has one.
The answered-question hook is removed because the existing menu-gone callback already closes the push notification.
The tab bar position check is removed because a bar below the footer leaves no rows to read.

Refs #1044
… list

Claude Code clears the screen and repaints it in several chunks after a resize.
A read between them found no menu and cancelled the pending question, so its answer got 409 prompt_cancelled.
The screen is now left unread for 300 ms after a size change, except for OSC 777 notifications.

At 12 rows the option list can scroll while the tab bar and question stay, with no "more line" row.
That frame raised a card with options missing and never grew the terminal.
A form now counts as unreadable when the rows above "Chat about this" do not number down to 1.

Refs #1044
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant