Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

feat(mobile): raise the keyboard when an agent asks a question

tony0ec3a1906e03
diff --git a/mobile/lib/phone/terminal_page.dart b/mobile/lib/phone/terminal_page.dart
index 84ab7241c..eb1a4ebce 100644
--- a/mobile/lib/phone/terminal_page.dart
+++ b/mobile/lib/phone/terminal_page.dart
@@ -18,6 +18,8 @@ import 'package:harness_mobile/terminal/image_transcode.dart';
import 'package:harness_mobile/terminal/terminal_font_store.dart';
import 'package:harness_mobile/terminal/terminal_theme.dart';
import 'package:harness_mobile/terminal/terminal_theme_store.dart';
+import 'package:harness_mobile/terminal/question_pane.dart';
+import 'package:harness_mobile/terminal/question_pane_watcher.dart';
import 'package:harness_mobile/terminal/terminal_session.dart';
import 'package:harness_mobile/widgets/engine_identity.dart'
show engineIdentity;
@@ -395,6 +397,104 @@ class _TerminalPageState extends State<TerminalPage>
/// the notifier declined without ever changing a status.
bool _takingControl = false;
+ /// Reads this page's own terminal buffer for an open question dialog.
+ ///
+ /// ⚠️ **Built late and only once**, when the agent's engine is first known:
+ /// the engine decides whether there is anything to look for at all, and it
+ /// arrives with the agent rather than with the page. Null until then, and for
+ /// every engine this parser is not verified against — which keeps the
+ /// keyboard from being raised over a dialog nobody can read.
+ QuestionPaneWatcher? _questionWatcher;
+
+ /// The engine [_questionWatcher] was built for, so a page that somehow
+ /// re-opens on a different engine rebuilds it rather than reading a Codex
+ /// dialog with Claude's rules.
+ QuestionEngine? _questionEngine;
+
+ /// The dialog the keyboard was last raised for.
+ ///
+ /// ⚠️ **This is what stops the keyboard fighting the person.** The watcher
+ /// reports every change of dialog, and a multi-question exchange changes it
+ /// several times; raising on each one would shove the keyboard back up
+ /// seconds after somebody put it away. One raise per question, and a question
+ /// is new only when its text or its options differ.
+ String? _questionRaisedFor;
+
+ /// Point the question watcher at this page's current terminal.
+ ///
+ /// Called from `build`, where both the engine and the session are known, and
+ /// cheap to call on every frame: [QuestionPaneWatcher.attach] returns at once
+ /// for a terminal it already holds.
+ void _syncQuestionWatcher(String? engineId, TerminalSession? session) {
+ final engine = questionEngineOf(engineId);
+ if (engine != _questionEngine) {
+ _questionEngine = engine;
+ _questionWatcher?.removeListener(_onQuestionPane);
+ _questionWatcher?.dispose();
+ // Nothing to watch for on an engine this parser does not know: leave the
+ // watcher null so not even a listener is attached.
+ _questionWatcher = engine == null
+ ? null
+ : (QuestionPaneWatcher(engine: engine)..addListener(_onQuestionPane));
+ }
+ _questionWatcher?.attach(session?.terminal);
+ }
+
+ /// The agent just asked something, or stopped asking.
+ ///
+ /// ⚠️ **Raising the keyboard is the whole feature, and it must happen at
+ /// most once per question.** An agent that blocks mid-turn is waiting on a
+ /// keystroke, and on a phone that keystroke is unreachable until the keyboard
+ /// is up — so the page opens it rather than making the person find the
+ /// terminal and tap it. But a person who puts the keyboard away during a
+ /// question has said they are not answering yet, and a watcher that raised it
+ /// again on the next repaint would be arguing with them.
+ void _onQuestionPane() {
+ if (!mounted) return;
+ final view = _questionWatcher?.view;
+ final open = view != null && view.answerable;
+ setState(() {
+ // Cleared as the dialog goes, so the NEXT question raises the keyboard
+ // again even if it words itself identically.
+ if (!open) _questionRaisedFor = null;
+ });
+ if (!open) return;
+ final key = view.fingerprint;
+ if (_questionRaisedFor == key) return;
+ _questionRaisedFor = key;
+ _raiseKeyboardForQuestion();
+ }
+
+ /// Open the keyboard because an agent is waiting on an answer.
+ ///
+ /// ⚠️ **Not [_raiseKeyboard], and synchronous where that one is not.** That
+ /// one belongs to a TAP on the terminal, and awaits the mic's transcript to
+ /// drain into the prompt first, because tapping mid-sentence is asking to
+ /// finish that sentence. Nothing was said here — the agent asked — so
+ /// draining voice would paste a half-spoken phrase into an answer the person
+ /// has not started, and with nothing to await this runs in one frame.
+ void _raiseKeyboardForQuestion() {
+ final facts = _readFacts();
+ final session = facts.session;
+ // A terminal this page only watches cannot be typed into, so a keyboard
+ // over it would be a keyboard that does nothing.
+ if (session == null || !session.acceptsInput) return;
+ // Already up on THIS page, or on its way: nothing to do, and calling again
+ // would restart the settle hold for no reason.
+ //
+ // ⚠️ `_ownsInput`, not the screen-wide `_keyboardIsUp`. That flag is true
+ // whenever any page's keyboard is showing — it exists to hold the keyboard
+ // across a swipe — so reading it here would skip the raise on a page swiped
+ // to while a keyboard belonging to the page behind it was still up.
+ if (_keyboardRequested || _ownsInput) return;
+ // The controls somebody reaches for next are in the header.
+ _chrome.reveal();
+ setState(() => _keyboardRequested = true);
+ // The keyboard is on its way and the key bar opens with it — see
+ // [_raiseKeyboard], which holds the resize the same way.
+ _armSettle();
+ }
+
_PageFacts _readFacts() {
final notifier = widget.notifier;
final pane = notifier.panes
@@ -496,6 +596,8 @@ class _TerminalPageState extends State<TerminalPage>
@override
void dispose() {
widget.notifier.removeListener(_onNotifier);
+ _questionWatcher?.removeListener(_onQuestionPane);
+ _questionWatcher?.dispose();
_cancelSettle();
_searchHoldTimer?.cancel();
_chrome.dispose();
@@ -986,6 +1088,15 @@ class _TerminalPageState extends State<TerminalPage>
final canCreate =
machine != null &&
phoneMachineStatusOf(machine) == PhoneMachineStatus.ready;
+ // Read this page's own buffer for a dialog, and raise the keyboard when one
+ // appears. Must run on every build: the session arrives a frame or two
+ // after the page, and the engine with the agent.
+ //
+ // ⚠️ **The buffer is the only source there is on a phone.** The daemon
+ // already detects the same dialog and publishes `commander_question`, but
+ // that frame goes out device-and-loopback only — a phone attached over the
+ // relay never receives it. See [QuestionPaneWatcher].
+ _syncQuestionWatcher(agent?.engine, session);
// Read once: the body reserves it, and search subtracts what the body
// already took. See [_navigationBar].
final navigationBar = _navigationBar;
@@ -1467,7 +1578,10 @@ class _TerminalPageState extends State<TerminalPage>
// door rather than a list of its own — the lists belong on the pages behind them, where they
// have room for every row and do not push the rest of this sheet down.
sections: [
- PhoneSheetSection(caption: 'Harness', actions: [..._agentActions(agent)]),
+ PhoneSheetSection(
+ caption: 'Harness',
+ actions: [..._agentActions(agent)],
+ ),
PhoneSheetSection(
caption: 'App',
actions: [
diff --git a/mobile/lib/terminal/question_pane.dart b/mobile/lib/terminal/question_pane.dart
new file mode 100644
index 000000000..bed9c0ae6
--- /dev/null
+++ b/mobile/lib/terminal/question_pane.dart
@@ -0,0 +1,511 @@
+/// Read an open question dialog straight off the terminal's own buffer.
+///
+/// ⚠️ **Why this exists at all.** The daemon already watches the pane and
+/// shapes the question — but it publishes it on `commander_question`, which
+/// goes out over `sendCommander` (`webEligible: false`) and `sendLocal`
+/// (loopback only). A phone attached over the relay is in neither audience, so
+/// that frame never arrives here. Until `commander_question` is carried over an
+/// encrypted up-type, the buffer this app already holds is the only source it
+/// has.
+///
+/// ⚠️ **This does NOT feed `blockedAgents`.** The amber ring on a tile, and the
+/// Needs input picker, still read the daemon's frames — an agent this window is
+/// not looking at has no buffer here to read, and letting a pane-derived
+/// question drive that shared state would make a ring that only exists while
+/// its agent happens to be on screen. What this feeds is the keyboard on the
+/// page whose buffer it read, and nothing else.
+///
+/// Ported from the daemon's `parseQuestionPane` (cli/src/lib/askQuestion.ts).
+/// Kept deliberately narrow: Claude Code and Codex only. Every other engine
+/// returns null and keeps exactly the behaviour it has today.
+library;
+
+import 'package:xterm/xterm.dart';
+
+/// Which CLI painted the dialog — the two this parser is verified against.
+///
+/// The engine matters for more than the frame: Codex highlights on a digit and
+/// commits on Enter, while Claude's digit both selects and submits. [QuestionPaneView.enterSubmits]
+/// carries that difference out.
+enum QuestionEngine { claude, codex }
+
+/// Map the engine id the app already carries onto the two this parser knows.
+///
+/// ⚠️ Unknown ids return null on purpose — `commandcode` and the rest paint
+/// dialogs this parser has never been checked against, and guessing at one
+/// would put a pad over a screen it cannot actually drive.
+QuestionEngine? questionEngineOf(String? engineId) => switch (engineId) {
+ 'claude' => QuestionEngine.claude,
+ 'codex' => QuestionEngine.codex,
+ _ => null,
+};
+
+/// One selectable row of the dialog.
+class QuestionPaneRow {
+ const QuestionPaneRow({
+ required this.number,
+ required this.label,
+ required this.checked,
+ });
+
+ /// The digit to press.
+ final String number;
+
+ final String label;
+
+ /// `[✔]` rather than `[ ]` — only ever set on a multi-select.
+ final bool checked;
+}
+
+/// A dialog found on the pane.
+class QuestionPaneView {
+ const QuestionPaneView({
+ required this.question,
+ required this.rows,
+ required this.multi,
+ required this.enterSubmits,
+ required this.partial,
+ });
+
+ /// Empty when [partial] — the dialog is open but its question has scrolled
+ /// out of the buffer.
+ final String question;
+
+ final List<QuestionPaneRow> rows;
+
+ /// Rows render as `[ ]` / `[✔]`: a digit toggles rather than answers.
+ final bool multi;
+
+ /// The footer says a digit only highlights and Enter commits — Codex's
+ /// `request_user_input`. False where one digit selects and submits, which is
+ /// every Claude dialog.
+ final bool enterSubmits;
+
+ /// The dialog is on screen but its top — the question and its first rows — is
+ /// above the buffer. Enough to know a dialog is STILL OPEN, and not enough to
+ /// show as a question.
+ ///
+ /// ⚠️ Measured by the daemon on Codex: it anchors its footer and lets a short
+ /// pane cut the top off. A caller must treat this as "keep whatever is
+ /// showing", never as "no dialog".
+ final bool partial;
+
+ /// A question worth putting a pad over: open, and with its text in hand.
+ bool get answerable => !partial && question.isNotEmpty && rows.isNotEmpty;
+
+ /// Changes whenever the dialog does — used to tell a redraw of the SAME
+ /// question from a new one.
+ String get fingerprint =>
+ '$question|${rows.map((r) => '${r.number}.${r.label}').join('|')}';
+}
+
+/// Rows that exist in every dialog but can never BE an answer.
+final RegExp _chatRow = RegExp(
+ r'^chat about (this|these)$',
+ caseSensitive: false,
+);
+
+/// The free-text row. It opens an editor instead of choosing, so it must never
+/// be offered as a selectable label. Claude writes "Type something.", Codex
+/// "Type something..." — one trailing dot or three.
+final RegExp _typeRow = RegExp(
+ r'^(type something\.{0,3}|type your own( answer)?|other \(type your (own|answer)\))$',
+ caseSensitive: false,
+);
+
+/// The bottom of a live dialog. Each CLI words its own.
+///
+/// `enter to submit answer` is Codex's `request_user_input`; the rest are
+/// Claude's. Missing one means the dialog is never seen at all.
+final RegExp _footer = RegExp(
+ r'enter to (select|confirm|submit)|enter\s+(submit|confirm|toggle)',
+ caseSensitive: false,
+);
+
+/// Codex's footer, which also says a digit alone does not commit.
+final RegExp _enterSubmitsFooter = RegExp(
+ r'enter to submit answer',
+ caseSensitive: false,
+);
+
+/// `❯ 1. Label` — with the caret optional and any checkbox peeled off.
+final RegExp _row = RegExp(r'^\s*[❯›>]?\s*(\d+)\.\s+(.+?)\s*$');
+
+final RegExp _checkbox = RegExp(r'^\[([^\]])\]\s*(.*)$');
+
+/// A row drawn as a checkbox, which makes the whole dialog multi-select.
+final RegExp _checkboxRow = RegExp(r'^\s*[❯›>]?\s*\d+\.\s+\[');
+
+/// The tab bar above a multi-question dialog, a rule, or a checkbox chip —
+/// whichever comes first walking up is the top of this frame.
+final RegExp _frameTop = RegExp(r'[←→]');
+final RegExp _chipTop = RegExp(r'^[☐☒✔✓]');
+final RegExp _ruleTop = RegExp(r'^[─━-]{6,}$');
+
+/// Codex draws an option's description on the SAME line as its label, padded
+/// into a second column (`1. Red Creates a bold, high-contrast`). Claude
+/// puts it on the line below, where it is not a row at all and is skipped.
+///
+/// ⚠️ **Two spaces, not three.** Codex pads to a column, so a label as long as
+/// its column leaves only the single gap the padding guarantees — measured on
+/// `__fixtures__/question-codex.txt`, where `2. Green Creates a fresh…` has
+/// exactly two and a three-space rule left the description glued to the label.
+/// A real label never holds a double space: the dialog collapses its own
+/// whitespace before it paints.
+final RegExp _codexDescription = RegExp(r'^(.+?)\s{2,}\S');
+
+/// Codex's own chrome, drawn directly above a question with no blank line to
+/// separate it: the prompt caret, an event bullet, or the status line under the
+/// composer. Each marks the top of the dialog's frame.
+///
+/// ⚠️ Without this the wrap walk had nothing to stop it — Codex paints no rule
+/// and no tab bar — and it swallowed the whole transcript into the question.
+final RegExp _codexChrome = RegExp(
+ r'^\s*[›•❯>]|^\s*(Question \d+/\d+|Working \(|Ask Codex)',
+);
+
+/// Lines that may sit under a live dialog's footer without meaning the engine
+/// has moved on: a second footer line, a bare prompt caret, or a rule.
+///
+/// ⚠️ **The footer WRAPS, and its tail is not a hint word to be listed.** On
+/// a 47-column phone pane `Enter to select · ↑/↓ to navigate · Esc to cancel`
+/// breaks after `Esc to`, leaving a line that is just `cancel` — and a rule
+/// built from the words this parser happened to have seen rejected it, decided
+/// the dialog was stale, and hid the pad over a live question. Measured on pane
+/// %11 while the dialog was open.
+///
+/// So the test is SHAPE, not vocabulary: the continuation of a wrapped footer
+/// is a short scrap of prose with no sentence-ending punctuation. Engine output
+/// under a dead dialog is a real line — long, or punctuated, or marked with the
+/// glyph its CLI prefixes results with. [_footerTailWords] is what keeps this
+/// from swallowing one.
+final RegExp _trailingChrome = RegExp(
+ r'^(option \d+/\d+|[\u276f\u203a>]\s*|[\u2502\u251c\u2514\u2500\u2014\-_\s]+)$',
+ caseSensitive: false,
+);
+
+/// A line that reads as the tail of a footer the pane wrapped.
+///
+/// ⚠️ **Not a length test.** The first version of this allowed a short,
+/// unpunctuated scrap — enough for `cancel`, and nothing else. Then a pane
+/// wrapped `Enter to select · Tab/Arrow keys to navigate · ctrl+g to edit in
+/// Vim · Esc to cancel` onto a second line that was 37 characters and six
+/// words, the test rejected it, and the pad vanished the moment the caret
+/// reached the last row. Measured on pane %12.
+///
+/// A footer says what keys do, so its continuation carries the same marks the
+/// footer itself does: the `·` it separates hints with, or a `key to verb`
+/// phrase such as `Esc to cancel`. Engine output under a dead dialog has
+/// neither.
+bool _looksLikeFooterTail(String line) {
+ // Its own output glyph rules a line out whatever else it holds — every CLI
+ // prefixes results with one, and no wrapped footer starts with one.
+ if (_outputGlyph.hasMatch(line)) return false;
+ if (RegExp(r'[.!?\u3002]$').hasMatch(line)) return false;
+ if (_footerHint.hasMatch(line)) return true;
+ // A footer can wrap onto a single orphan word — `… · Esc to` breaks and leaves
+ // just `cancel`, which carries no hint of its own. One bare word is not a
+ // line of output either, so it is allowed through on its shape alone.
+ return !line.contains(' ') && line.length <= 16;
+}
+
+/// The glyphs a CLI prefixes its own output with.
+final RegExp _outputGlyph = RegExp(
+ r'^[\u23fa\u2022\u2713\u2717\u203a\u276f>\u2731\u250c\u2514#\[]',
+);
+
+/// What a footer's continuation looks like: a `·` separator, or a phrase
+/// naming a key and what it does.
+final RegExp _footerHint = RegExp(
+ r'\u00b7|\b(to (select|cancel|confirm|submit|toggle|navigate|edit|add|interrupt|view|expand)|esc|enter|tab|ctrl\+|shift\+|\u2191/\u2193|\u2190/\u2192)\b',
+ caseSensitive: false,
+);
+
+/// The review step's own anchor, in place of the footer it does not paint.
+final RegExp _reviewPrompt = RegExp(
+ r'ready to submit your answers',
+ caseSensitive: false,
+);
+
+/// The row that makes a review a review.
+final RegExp _submitRow = RegExp(r'^submit answers$', caseSensitive: false);
+
+/// How far under its prompt a review's rows may run.
+const int _maxReviewScan = 10;
+
+/// How far above the footer a dialog's rows may run before this stops looking.
+const int _maxRowScan = 40;
+
+/// How far above the rows the question may sit.
+const int _maxQuestionScan = 12;
+
+/// How many lines a wrapped question may span.
+///
+/// ⚠️ A real wrapped question is two or three lines on a 47-column phone
+/// pane. More than this is the walk having escaped a frame whose top it could
+/// not recognise, and the tail alone beats a question with the transcript in
+/// it.
+const int _maxWrapLines = 4;
+
+/// Read the current dialog off [terminal]'s buffer, or null if there is none.
+///
+/// ⚠️ **Anchored to the LAST footer, not the first.** The buffer holds
+/// scrollback — old dialogs, old plan text — and the live dialog is always the
+/// lowest one. Reading upward from the bottom is what keeps an answered
+/// question in the scrollback from being shown as an open one.
+QuestionPaneView? readQuestionPane(Terminal terminal, QuestionEngine engine) {
+ final lines = _visibleLines(terminal);
+ if (lines.isEmpty) return null;
+ return parseQuestionLines(lines, engine);
+}
+
+/// The buffer as plain text, oldest line first.
+///
+/// ⚠️ **Reads the buffer, not the viewport.** Someone scrolled up to read
+/// history is still being asked the question, and a pad that vanished when they
+/// scrolled would be a pad that leaves exactly when it is being read about.
+List<String> _visibleLines(Terminal terminal) {
+ final buffer = terminal.buffer;
+ final lines = buffer.lines;
+ final total = lines.length;
+ if (total == 0) return const [];
+ // The dialog is at the bottom; everything far above it is scrollback that
+ // only slows the walk down.
+ final from = total - _bufferScan < 0 ? 0 : total - _bufferScan;
+ final out = <String>[];
+ for (var i = from; i < total; i++) {
+ out.add(lines[i].toString().replaceAll('\u00a0', ' ').trimRight());
+ }
+ return out;
+}
+
+/// How much of the buffer to read back.
+///
+/// ⚠️ The daemon captures 60 lines and the phone negotiates a 50-line pty, so
+/// 80 covers a dialog that begins above the fold with room to spare, without
+/// walking scrollback that cannot hold the live dialog anyway.
+const int _bufferScan = 80;
+
+/// The parser proper, over lines already stripped of styling.
+///
+/// Separated from [readQuestionPane] so the shape can be reasoned about against
+/// a captured pane without a live terminal.
+QuestionPaneView? parseQuestionLines(
+ List<String> lines,
+ QuestionEngine engine,
+) {
+ // ⚠️ **The review screen paints no footer**, so it has to be found first or
+ // not at all. After the last question the dialog becomes "Ready to submit
+ // your answers?" over `1. Submit answers` / `2. Cancel` — still open, still
+ // waiting, still driven by the same arrows and Enter. Anchoring only on a
+ // footer made the pad vanish the moment the caret reached Submit, which is
+ // exactly when it is still needed. Measured on pane %8.
+ final review = _parseReview(lines);
+ if (review != null) return review;
+
+ final footer = lines.lastIndexWhere((l) => _footer.hasMatch(l));
+ if (footer < 0) return null;
+ // ⚠️ **A footer is not proof the dialog is still open.** The daemon captures
+ // the live pane, where an answered dialog is simply gone. This reads a
+ // BUFFER, which keeps it in scrollback forever — and anchoring on the last
+ // footer alone showed the pad again over a question answered minutes ago,
+ // with its options still tappable. Measured against a real pane with the
+ // answer and two more turns appended below it.
+ //
+ // What separates the two is what sits UNDER the footer: a live dialog is the
+ // last thing on screen, so only blanks and its own trailing chrome may follow
+ // it. One line of ordinary output down there means the engine has moved on.
+ if (!_isLastThingOnScreen(lines, footer)) return null;
+
+ final rows = <QuestionPaneRow>[];
+ var checkbox = false;
+ var start = -1;
+ for (var i = footer - 1; i >= 0 && footer - i <= _maxRowScan; i--) {
+ final row = _parseRow(lines[i], engine);
+ if (row == null) continue;
+ rows.insert(0, row);
+ if (_checkboxRow.hasMatch(lines[i])) checkbox = true;
+ if (row.number == '1') {
+ start = i;
+ break;
+ }
+ }
+
+ final enterSubmits = _enterSubmitsFooter.hasMatch(lines[footer]);
+ // Rows in view but no "1." above them: the dialog's top is out of the buffer.
+ // Only Codex does this, and only its footer proves the dialog is still there.
+ if (rows.isNotEmpty && start < 0 && enterSubmits) {
+ return QuestionPaneView(
+ question: '',
+ rows: rows,
+ multi: checkbox,
+ enterSubmits: true,
+ partial: true,
+ );
+ }
+ if (start < 0 || rows.isEmpty) return null;
+
+ // The question is the nearest real text line above the rows.
+ //
+ // ⚠️ **Stop at the frame's top, never walk past it.** Mid-repaint the
+ // question line can be blank for one read, and walking on would pick up the
+ // PREVIOUS question still in scrollback and pair a stale title with live
+ // options. An empty result just means "look again".
+ var question = '';
+ var questionAt = -1;
+ for (var i = start - 1; i >= 0 && start - i <= _maxQuestionScan; i--) {
+ final line = lines[i].trim();
+ if (line.isEmpty) continue;
+ if (_frameTop.hasMatch(line) ||
+ _chipTop.hasMatch(line) ||
+ _ruleTop.hasMatch(line)) {
+ break;
+ }
+ question = line;
+ questionAt = i;
+ break;
+ }
+
+ // ⚠️ **The question can be WRAPPED, and the daemon does not stitch it.** A
+ // 47-column pane broke "…và dài bao lâu?" across two lines and the daemon
+ // announced the question as "lâu?" — see `[question] … asking the user ·
+ // "lâu?"` in harness.log. The line above is part of the question when it is
+ // prose rather than another row or the frame's top.
+ if (question.isNotEmpty) {
+ question = _stitchWrapped(lines, questionAt, question);
+ }
+
+ final answerable = rows
+ .where((r) => !_chatRow.hasMatch(r.label) && !_typeRow.hasMatch(r.label))
+ .toList(growable: false);
+ if (answerable.isEmpty) return null;
+
+ return QuestionPaneView(
+ question: question,
+ rows: answerable,
+ multi: checkbox,
+ enterSubmits: enterSubmits,
+ partial: false,
+ );
+}
+
+/// Walk up from the question's last line, gathering the lines it wrapped onto.
+///
+/// [tailAt] is the index [tail] was read from — NOT `start - 1`. The question
+/// search skips blank lines, so the two are different whenever a dialog puts a
+/// gap between its question and its first row, and walking from the wrong one
+/// appended the tail to itself ("…bao lâu? lâu?" on a live 47-column pane).
+///
+/// ⚠️ **Bounded by [_maxWrapLines], and that bound is load-bearing.** A dialog
+/// whose frame has no rule or tab bar above the question — Codex draws none —
+/// has nothing to stop this walk, and it ran to the top of the buffer and
+/// returned the entire screen as the question, transcript and all. Real wrapped
+/// questions are two or three lines; anything longer is the walk having escaped
+/// its frame, and the tail alone is the better answer.
+String _stitchWrapped(List<String> lines, int tailAt, String tail) {
+ if (tailAt <= 0) return tail;
+ final parts = <String>[tail];
+ for (var i = tailAt - 1; i >= 0 && tailAt - i <= _maxWrapLines; i--) {
+ final line = lines[i].trim();
+ // A blank line is the top of the question's own block in every dialog that
+ // leaves one.
+ if (line.isEmpty) break;
+ if (_frameTop.hasMatch(line) ||
+ _chipTop.hasMatch(line) ||
+ _ruleTop.hasMatch(line)) {
+ break;
+ }
+ // Another numbered row above means the question never wrapped — what is up
+ // there belongs to a different frame.
+ if (_row.hasMatch(line)) break;
+ // Codex paints its own chrome directly above the question with no blank
+ // line between: a prompt caret, a bullet, or its status/model line. Each is
+ // the top of the frame as surely as a rule would be.
+ if (_codexChrome.hasMatch(line)) break;
+ parts.insert(0, line);
+ }
+ return parts.join(' ');
+}
+
+/// The review step, which closes a multi-question dialog.
+///
+/// It has no footer of its own — `Ready to submit your answers?` and its two
+/// rows are the whole frame — so it is anchored on that line instead, and only
+/// when it is the last thing on screen.
+///
+/// Returned as an ordinary question: the pad drives it exactly as it drives any
+/// other, and calling it something else would only make the caller ask why.
+QuestionPaneView? _parseReview(List<String> lines) {
+ final at = lines.lastIndexWhere((l) => _reviewPrompt.hasMatch(l));
+ if (at < 0) return null;
+ final rows = <QuestionPaneRow>[];
+ var sawSubmit = false;
+ for (var i = at + 1; i < lines.length && i - at <= _maxReviewScan; i++) {
+ final line = lines[i].trim();
+ if (line.isEmpty) continue;
+ final row = _parseRow(lines[i], QuestionEngine.claude);
+ if (row == null) {
+ // Anything else under the prompt means this review has been answered and
+ // what follows is the engine's output.
+ if (_trailingChrome.hasMatch(line) || _looksLikeFooterTail(line)) {
+ continue;
+ }
+ return null;
+ }
+ rows.add(row);
+ if (_submitRow.hasMatch(row.label)) sawSubmit = true;
+ }
+ // Both halves matter: the prompt alone appears in scrollback too, and it is
+ // the Submit row still being drawn under it that says this one is live.
+ if (!sawSubmit || rows.isEmpty) return null;
+ return QuestionPaneView(
+ question: lines[at].trim(),
+ rows: rows,
+ multi: false,
+ enterSubmits: false,
+ partial: false,
+ );
+}
+
+/// Is the dialog ending at [footer] the last thing drawn on the pane?
+///
+/// Everything below it must be blank or the dialog's own trailing chrome.
+/// Codex draws TWO footer lines (`option 1/4 | tab to add notes` above
+/// `enter to submit answer | esc to interrupt`) and an empty prompt line can
+/// follow either CLI, so a handful of recognised lines are allowed through —
+/// but anything the engine printed afterwards is not.
+bool _isLastThingOnScreen(List<String> lines, int footer) {
+ for (var i = footer + 1; i < lines.length; i++) {
+ final line = lines[i].trim();
+ if (line.isEmpty) continue;
+ if (_footer.hasMatch(line)) continue;
+ if (_trailingChrome.hasMatch(line)) continue;
+ // The footer itself, wrapped. Not limited to the line directly under it:
+ // `Enter to select · Tab/Arrow keys to navigate · ctrl+g to edit in Vim ·
+ // Esc to cancel` takes three lines on a 47-column pane, and each carries
+ // the marks _looksLikeFooterTail reads.
+ if (_looksLikeFooterTail(line)) continue;
+ return false;
+ }
+ return true;
+}
+
+QuestionPaneRow? _parseRow(String line, QuestionEngine engine) {
+ final m = _row.firstMatch(line);
+ if (m == null) return null;
+ final raw = m.group(2)!;
+ final box = _checkbox.firstMatch(raw);
+ var label = (box != null ? box.group(2)! : raw).trim();
+ // Codex pads a description into a second column on the row itself; Claude
+ // never does, so this only ever runs where it was measured.
+ if (engine == QuestionEngine.codex) {
+ final split = _codexDescription.firstMatch(label);
+ if (split != null) label = split.group(1)!.trim();
+ }
+ if (label.isEmpty) return null;
+ return QuestionPaneRow(
+ number: m.group(1)!,
+ label: label,
+ checked: box != null && box.group(1)!.trim().isNotEmpty,
+ );
+}
diff --git a/mobile/lib/terminal/question_pane_watcher.dart b/mobile/lib/terminal/question_pane_watcher.dart
new file mode 100644
index 000000000..eb3a47715
--- /dev/null
+++ b/mobile/lib/terminal/question_pane_watcher.dart
@@ -0,0 +1,141 @@
+import 'dart:async';
+
+import 'package:flutter/foundation.dart';
+import 'package:xterm/xterm.dart';
+
+import 'question_pane.dart';
+
+/// Watches one terminal's buffer for an open question dialog.
+///
+/// ⚠️ **A local read, not a second source of truth.** The daemon's own watcher
+/// still runs, still drives `blockedAgents`, and still answers from wherever a
+/// person actually answers. This only tells the page it is attached to whether
+/// the pane in front of it is showing a dialog, because the daemon's frame
+/// cannot reach a phone on the relay — see [readQuestionPane].
+///
+/// ⚠️ **Debounced on both edges, and the two are not symmetric.** A redraw
+/// arrives as a burst of writes and a capture taken mid-repaint parses as
+/// no-dialog, so a question appearing waits for the buffer to settle
+/// ([_settle]) while a question LEAVING must additionally miss [_goneReads]
+/// consecutive reads. The daemon learned the same thing the same way — see
+/// `GONE_TICKS` in askQuestion.ts: announcing a close on the first miss yanked
+/// a live question off the screen.
+///
+/// ⚠️ **Whoever listens must treat a repeat as nothing new.** This notifies on
+/// every CHANGE of dialog, including one question replacing another inside the
+/// same exchange; a listener that raises the keyboard on each notification
+/// would raise it again on a keyboard the person had deliberately put away.
+class QuestionPaneWatcher extends ChangeNotifier {
+ QuestionPaneWatcher({required this.engine});
+
+ /// Which CLI's dialog to look for. Null for every engine this parser has not
+ /// been verified against — the watcher then does nothing at all, and the page
+ /// behaves exactly as it did before.
+ final QuestionEngine? engine;
+
+ Terminal? _terminal;
+ Timer? _debounce;
+ QuestionPaneView? _view;
+ int _misses = 0;
+
+ /// The dialog currently on the pane, or null.
+ QuestionPaneView? get view => _view;
+
+ /// How long the buffer must be quiet before it is read.
+ ///
+ /// ⚠️ Long enough to outlast a repaint, short enough that the pad follows the
+ /// dialog rather than trailing it. The daemon polls every 1500ms and is not
+ /// reading a live buffer; here the writes themselves say when to look.
+ static const Duration _settle = Duration(milliseconds: 120);
+
+ /// Consecutive reads with no dialog before one is declared gone.
+ ///
+ /// NOT 1 — see the class docblock.
+ static const int _goneReads = 2;
+
+ /// Start reading [terminal], or stop when it is null.
+ ///
+ /// Safe to call with the same terminal repeatedly: a page rebuilds often and
+ /// re-attaching on every frame would drop the miss count each time and make
+ /// the close debounce never finish.
+ void attach(Terminal? terminal) {
+ if (identical(_terminal, terminal)) return;
+ _terminal?.removeListener(_onOutput);
+ _debounce?.cancel();
+ _terminal = terminal;
+ _misses = 0;
+ if (terminal == null) {
+ _publish(null);
+ return;
+ }
+ if (engine == null) return;
+ terminal.addListener(_onOutput);
+ // The dialog may already be on the pane — a page opened onto an agent that
+ // is mid-question must show the pad without waiting for the next byte.
+ _read();
+ }
+
+ void _onOutput() {
+ if (engine == null) return;
+ _debounce?.cancel();
+ _debounce = Timer(_settle, _read);
+ }
+
+ void _read() {
+ final terminal = _terminal;
+ final engine = this.engine;
+ if (terminal == null || engine == null) return;
+ final found = readQuestionPane(terminal, engine);
+ // Open but scrolled past its own top: still a dialog, so whatever is
+ // showing keeps showing. Not a miss, and not something to announce.
+ if (found != null && found.partial) {
+ _misses = 0;
+ // Keep looking, for the same reason the miss branch below does: a partial
+ // dialog that is then dismissed leaves a quiet pane, and a watcher that
+ // only wakes on output would never see it go.
+ if (_view != null) {
+ _debounce?.cancel();
+ _debounce = Timer(_settle, _read);
+ }
+ return;
+ }
+ if (found == null || !found.answerable) {
+ // Never had one: nothing to debounce, and counting misses forever would
+ // be pure noise.
+ if (_view == null) return;
+ if (++_misses < _goneReads) {
+ // ⚠️ **Ask for the next read rather than waiting for one.** Reads are
+ // driven by terminal output, and the engine may print NOTHING after the
+ // dialog closes — Codex repaints once on `esc` and then goes quiet. The
+ // first read saw the dialog gone, the second never came, and the pad
+ // stayed up over an answered question until the next keystroke.
+ // Measured on a Codex pane after `×`.
+ _debounce?.cancel();
+ _debounce = Timer(_settle, _read);
+ return;
+ }
+ _misses = 0;
+ _publish(null);
+ return;
+ }
+ _misses = 0;
+ // The same dialog redrawn — a caret moving between rows rewrites the whole
+ // block. Publishing that would rebuild the pad on every arrow key.
+ if (_view?.fingerprint == found.fingerprint) return;
+ _publish(found);
+ }
+
+ void _publish(QuestionPaneView? next) {
+ if (_view == null && next == null) return;
+ _view = next;
+ notifyListeners();
+ }
+
+ @override
+ void dispose() {
+ _debounce?.cancel();
+ _terminal?.removeListener(_onOutput);
+ _terminal = null;
+ super.dispose();
+ }
+}