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.

fix(mobile): take control before a tapped answer or Interrupt

tony6f3b113ff8c1
diff --git a/mobile/lib/phone/terminal_page.dart b/mobile/lib/phone/terminal_page.dart
index 8a3f3cfdb..0a0c16c62 100644
--- a/mobile/lib/phone/terminal_page.dart
+++ b/mobile/lib/phone/terminal_page.dart
@@ -1139,7 +1139,7 @@ class _TerminalPageState extends State<TerminalPage>
for (final key in questionKeys(view)) {
if (key.number == band.key) {
HapticFeedback.selectionClick();
- _answer(view, key);
+ _tapAnswer(view, key);
return;
}
}
@@ -1155,7 +1155,7 @@ class _TerminalPageState extends State<TerminalPage>
for (final key in questionKeys(view)) {
if (key.number == number) {
HapticFeedback.selectionClick();
- _answer(view, key);
+ _tapAnswer(view, key);
return true;
}
}
@@ -1164,6 +1164,65 @@ class _TerminalPageState extends State<TerminalPage>
static final _optionLine = RegExp(r'^\s*(?:[❯›>]\s*)?(\d{1,2})[.)]\s');
+ /// A tapped answer: pressed at once on a terminal this phone holds, else once it is taken back.
+ ///
+ /// ⚠️ **A tap on an answer is a claim on the terminal, as speaking one is ([_sessionForInput]).**
+ /// The bands are drawn on a terminal only being watched too — after the computer took it, or
+ /// when it opened watching on a reconnect — and a tap there used to buzz and send nothing: the
+ /// band answers before the panel's own tap could take control, and a session that is not
+ /// [TerminalSession.acceptsInput] drops the keys.
+ void _tapAnswer(QuestionPaneView view, QuestionKey key) {
+ final session = widget.notifier
+ .paneOfAgent(widget.machineId, widget.agentId)
+ ?.session;
+ if (session != null && session.acceptsInput) {
+ _answer(view, key);
+ return;
+ }
+ unawaited(_answerOnceTaken(view, key));
+ }
+
+ /// Set while a tapped answer waits for the terminal to come back, so a second tap in that wait
+ /// does not press a second answer once it has.
+ bool _answerWaiting = false;
+
+ /// Takes the terminal back, then presses [key] — only on the same question still open: one
+ /// answered on the computer meanwhile has closed, and a digit then would land in the prompt.
+ ///
+ /// ⚠️ **Read off the screen the stream came back with, not the watcher's view.** Taking the
+ /// terminal back answers with a keyframe of the screen as it is now, and the watcher keeps a
+ /// dialog it no longer finds for one more read (its close debounce) — so a question the computer
+ /// answered in the meantime would still be the watcher's at the moment the press goes out.
+ Future<void> _answerOnceTaken(QuestionPaneView view, QuestionKey key) async {
+ if (_answerWaiting) return;
+ _answerWaiting = true;
+ try {
+ final session = await _sessionForInput(why: 'answer');
+ if (!mounted) return;
+ if (session == null) {
+ _flash('✗ not sent', error: true);
+ return;
+ }
+ final engine = _questionEngine;
+ final now = engine == null
+ ? null
+ : readQuestionPane(session.terminal, engine);
+ if (now == null ||
+ now.fingerprint != view.fingerprint ||
+ !_answersTappable(now)) {
+ return;
+ }
+ for (final pressed in questionKeys(now)) {
+ if (pressed.number == key.number) {
+ _answer(now, pressed);
+ return;
+ }
+ }
+ } finally {
+ _answerWaiting = false;
+ }
+ }
+
/// Presses an answer: its digit, and Return only where the dialog asks for one (Codex's
/// `request_user_input`) — a chosen Return, never a blind one. An MCP server's Accept or Decline
/// has no digit: the caret is moved onto it and Enter pressed — see [ElicitationPress].
@@ -1213,12 +1272,13 @@ class _TerminalPageState extends State<TerminalPage>
/// The pane's session, able to take input — taken back first when another client took the
/// terminal (the desktop, on a click there) while the take was being spoken. Speaking to a
- /// harness is as plain a claim on it as a tap or a scroll. Null when it cannot be had in 6s.
- Future<TerminalSession?> _sessionForInput() async {
+ /// harness is as plain a claim on it as a tap or a scroll, and so are a tapped answer and
+ /// Interrupt; [why] names which in the log. Null when it cannot be had in 6s.
+ Future<TerminalSession?> _sessionForInput({String why = 'voice'}) async {
TerminalSession? current() =>
widget.notifier.paneOfAgent(widget.machineId, widget.agentId)?.session;
if (current() case final session? when session.acceptsInput) return session;
- appLog.info('voice', 'terminal not ours at send — taking it back');
+ appLog.info(why, 'terminal not ours at send — taking it back');
await _takeControl();
for (var waited = 0; waited < 60 && mounted; waited++) {
if (current() case final session? when session.acceptsInput) {
@@ -1226,7 +1286,7 @@ class _TerminalPageState extends State<TerminalPage>
}
await Future<void>.delayed(const Duration(milliseconds: 100));
}
- appLog.warn('voice', 'terminal not back in 6s — take kept');
+ appLog.warn(why, 'terminal not back in 6s — take kept');
return null;
}
@@ -2722,12 +2782,7 @@ class _TerminalPageState extends State<TerminalPage>
icon: LucideIcons.octagonPause300,
label: 'Interrupt',
value: 'esc',
- onTap: () {
- final session = widget.notifier
- .paneOfAgent(widget.machineId, widget.agentId)
- ?.session;
- session?.terminal.keyInput(TerminalKey.escape);
- },
+ onTap: () => unawaited(_interrupt()),
),
// Where this agent runs, above the actions that act ON it: the desktop
// keeps it in the pane header, and this sheet is the phone's pane header.
@@ -2815,6 +2870,39 @@ class _TerminalPageState extends State<TerminalPage>
Future<void> _takeControl() =>
widget.notifier.selectAgent(widget.machineId, widget.agentId);
+ /// The sheet's Interrupt: Esc, on a terminal taken back first when another client holds it.
+ ///
+ /// ⚠️ **Offered while the terminal is only watched, so it takes it.** The row shows whenever the
+ /// agent works, and a terminal held by the computer drops a key the phone sends — so Interrupt,
+ /// wanted most when something has to stop now, used to do nothing there and say nothing.
+ Future<void> _interrupt() async {
+ final held = widget.notifier
+ .paneOfAgent(widget.machineId, widget.agentId)
+ ?.session;
+ if (held != null && held.acceptsInput) {
+ held.terminal.keyInput(TerminalKey.escape);
+ return;
+ }
+ if (_interruptWaiting) return;
+ _interruptWaiting = true;
+ try {
+ final session = await _sessionForInput(why: 'interrupt');
+ if (!mounted) return;
+ if (session == null) {
+ _flash('✗ not sent', error: true);
+ return;
+ }
+ session.terminal.keyInput(TerminalKey.escape);
+ } finally {
+ _interruptWaiting = false;
+ }
+ }
+
+ /// Set while an Interrupt waits for the terminal to come back, so a second one pressed in that
+ /// wait — the sheet opened again because nothing seemed to happen — adds no second Esc once it
+ /// has: on Claude Code's empty prompt, Esc twice opens the rewind to an earlier message.
+ bool _interruptWaiting = false;
+
/// Restarting is a round trip that can fail, and the phone has no status rail to fail into — so
/// the answer lands as a snackbar, which is the one surface a pushed page here always has.
Future<void> _restart() async {
diff --git a/mobile/test/phone_edge/focus_behavior_test.dart b/mobile/test/phone_edge/focus_behavior_test.dart
index 96c3ce23b..24be2417c 100644
--- a/mobile/test/phone_edge/focus_behavior_test.dart
+++ b/mobile/test/phone_edge/focus_behavior_test.dart
@@ -1,3 +1,5 @@
+import 'dart:convert';
+
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
@@ -15,6 +17,7 @@ import 'package:harness_mobile/phone/voice_mic_button.dart';
import 'package:harness_mobile/phone/voice_mic_face.dart';
import 'package:harness_mobile/state/app_state.dart';
import 'package:harness_mobile/state/pending_question.dart';
+import 'package:harness_mobile/terminal/terminal_binary.dart';
import 'package:harness_mobile/terminal/terminal_font_store.dart';
import 'package:harness_mobile/terminal/terminal_session.dart';
import 'package:xterm/xterm.dart';
@@ -386,6 +389,47 @@ void main() {
await focus.close();
});
+ // Offered whenever the agent works — the one row wanted most when something must stop — and on
+ // a terminal the computer took its Esc used to be dropped without a word.
+ testWidgets('Interrupt on a terminal the computer took takes it back, '
+ 'then sends esc', (tester) async {
+ final focus = await openMenu(tester, working: true);
+ await focus.takenByComputer();
+ await frames(tester);
+ final before = focus.opens;
+
+ await tester.tap(find.text('Interrupt'));
+ await frames(tester);
+ expect(focus.opens, greaterThan(before));
+ expect(focus.typed, isNot(contains('\x1b')));
+ // Nothing seemed to happen: the sheet opened again, Interrupt pressed again in the wait.
+ await tester.tap(find.byKey(const ValueKey('terminal-title')));
+ await frames(tester);
+ await tester.tap(find.text('Interrupt'));
+ await frames(tester);
+
+ await focus.answerOpen();
+ await tester.pump(const Duration(milliseconds: 250));
+ await frames(tester);
+ expect(focus.session.acceptsInput, isTrue);
+ // One Esc: a second, on Claude Code's empty prompt, opens its rewind.
+ expect(focus.typed.where((key) => key == '\x1b'), hasLength(1));
+ await focus.close();
+ });
+
+ testWidgets('Interrupt pressed twice on a terminal the phone holds sends '
+ 'esc twice, as before', (tester) async {
+ final focus = await openMenu(tester, working: true);
+ await tester.tap(find.text('Interrupt'));
+ await frames(tester);
+ await tester.tap(find.byKey(const ValueKey('terminal-title')));
+ await frames(tester);
+ await tester.tap(find.text('Interrupt'));
+ await frames(tester);
+ expect(focus.typed.where((key) => key == '\x1b'), hasLength(2));
+ await focus.close();
+ });
+
testWidgets('Restart that the machine refuses says why', (tester) async {
final focus = await openMenu(tester);
await tester.tap(find.text('Restart'));
@@ -454,6 +498,69 @@ void main() {
await focus.close();
});
+ // The answers stay drawn on a terminal the computer took, and a tap there used to buzz and
+ // press a digit the session then dropped. A tapped answer is a claim, as a spoken one is.
+ testWidgets('an answer tapped on a terminal the computer took takes it '
+ 'back, then presses it, once', (tester) async {
+ final focus = await asked(tester);
+ await focus.takenByComputer();
+ await frames(tester);
+ expect(focus.session.acceptsInput, isFalse);
+ final before = focus.opens;
+
+ await focus.tapRow("don't ask again");
+ await frames(tester);
+ // Asked for again, and nothing pressed into a terminal that would drop it.
+ expect(focus.opens, greaterThan(before));
+ expect(focus.typed, isNot(contains('2')));
+ // A second tap while it comes back adds no second answer.
+ await focus.tapRow("don't ask again");
+ await frames(tester);
+
+ // Still asking: the screen it comes back with is the same question.
+ await focus.answerOpen(screen: permissionDialog);
+ await tester.pump(const Duration(milliseconds: 250));
+ await frames(tester);
+ expect(focus.session.acceptsInput, isTrue);
+ expect(focus.typed.where((key) => key == '2'), hasLength(1));
+ await focus.close();
+ });
+
+ testWidgets('a terminal that does not come back: the tapped answer says it '
+ 'was not sent', (tester) async {
+ final focus = await asked(tester);
+ await focus.takenByComputer();
+ await frames(tester);
+
+ await focus.tapRow("don't ask again");
+ await frames(tester);
+ // No answer to the open: the take gives up after six seconds, and says so.
+ await tester.pump(const Duration(seconds: 6));
+ await tester.pump(const Duration(milliseconds: 200));
+ expect(find.text('✗ not sent'), findsOneWidget);
+ expect(focus.typed, isNot(contains('2')));
+ await focus.close();
+ });
+
+ testWidgets('a question answered on the computer while the phone takes '
+ 'the terminal back: nothing is pressed', (tester) async {
+ final focus = await asked(tester);
+ await focus.takenByComputer();
+ await frames(tester);
+
+ await focus.tapRow("don't ask again");
+ await frames(tester);
+ // Answered there meanwhile: the screen the terminal comes back with is the agent's prompt.
+ // The watcher still holds the dialog for one more read, so only the screen itself can say.
+ await focus.answerOpen(screen: '\x1b[2J\x1b[Hprompt> ');
+ await tester.pump(const Duration(milliseconds: 250));
+ await frames(tester);
+ expect(focus.session.acceptsInput, isTrue);
+ // A digit now would have been typed into the agent's prompt.
+ expect(focus.typed, isNot(contains('2')));
+ await focus.close();
+ });
+
testWidgets('a question with no words to spare: a voice that matches '
'nothing is not sent', (tester) async {
final focus = await _Focus.open(tester);
@@ -1204,11 +1311,7 @@ class _Focus {
final sent = <(String, Map<String, dynamic>)>[];
final session = await liveTerminal(app, 'a', sent: sent);
final typed = <String>[];
- final forward = session.terminal.onOutput;
- session.terminal.onOutput = (data) {
- typed.add(data);
- forward?.call(data);
- };
+ _record(session.terminal, typed);
final voice = edgeVoice();
final opened = <(String, String)>[];
final pushed = <Route<dynamic>>[];
@@ -1259,6 +1362,49 @@ class _Focus {
int get opens =>
conn.opens + sent.where((frame) => frame.$1 == 'terminal_open').length;
+ /// The computer taking the terminal: the daemon closes this phone's stream as taken over.
+ Future<void> takenByComputer() => session.handleFrame('terminal_closed', {
+ 'streamId': session.streamId,
+ 'code': 'TERMINAL_TAKEN_OVER',
+ 'reason': 'another client connected',
+ 'takenBy': {'machineId': 'm', 'label': 'MacBook'},
+ });
+
+ /// The daemon answering the session's latest open: a new stream, and its first screen — [screen],
+ /// the terminal as it stands on the computer now — after which the session holds the terminal.
+ Future<void> answerOpen({String screen = 'prompt> '}) async {
+ final open = sent.lastWhere((frame) => frame.$1 == 'terminal_open').$2;
+ final streamId = 'stream-${session.agentId}-again';
+ await session.handleFrame('terminal_ready', {
+ 'requestId': open['requestId'],
+ 'agentId': session.agentId,
+ 'protocolVersion': TerminalSession.protocolVersion,
+ 'streamId': streamId,
+ });
+ await session.handleBinary(
+ TerminalBinaryFrame(
+ kind: TerminalBinaryKind.keyframe,
+ streamId: streamId,
+ seq: 0,
+ bytes: Uint8List.fromList(utf8.encode(screen)),
+ compressed: false,
+ cols: session.terminal.viewWidth,
+ rows: session.terminal.viewHeight,
+ ),
+ );
+ // A keyframe swaps in a fresh terminal: what is typed into it is recorded too.
+ _record(session.terminal, typed);
+ }
+
+ /// Records into [typed] everything typed into [terminal], and passes it on.
+ static void _record(Terminal terminal, List<String> typed) {
+ final forward = terminal.onOutput;
+ terminal.onOutput = (data) {
+ typed.add(data);
+ forward?.call(data);
+ };
+ }
+
/// A screenful of output, so the terminal fills the page rather than sitting anchored at its
/// foot — where a tap on its middle would land on nothing.
void fill() {