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): send keys before IME resets, cut rebuilds

tony4a9f300bdfed
diff --git a/mobile/lib/phone/phone_search_controller.dart b/mobile/lib/phone/phone_search_controller.dart
index 3f1d7bb8f..36f7b87e9 100644
--- a/mobile/lib/phone/phone_search_controller.dart
+++ b/mobile/lib/phone/phone_search_controller.dart
@@ -1,6 +1,7 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
+import 'package:flutter/scheduler.dart';
import 'package:harness_mobile/core/phone_search_history.dart';
import 'package:harness_mobile/logging/typing_trace.dart';
@@ -309,7 +310,25 @@ class PhoneSearchController extends ChangeNotifier {
notifier.searchableMachineIds.contains(row.machineId),
};
+ /// The app changed while the box is open: the catalog is read again at the
+ /// start of the next frame, once.
+ ///
+ /// ⚠️ **Once a frame, not once a notify.** A machine coming back notifies a
+ /// dozen times in one go — its status, its link, its agents — and reading the
+ /// catalog after each one was 2–5ms apiece, 35ms of a single frame (measured
+ /// on a phone, 217 rows, 2026-10-05). The list could not show the steps
+ /// between anyway: it repaints once a frame.
void _rebuild() {
+ if (_rebuildFrame != null || _disposed) return;
+ _rebuildFrame = SchedulerBinding.instance.scheduleFrameCallback((_) {
+ _rebuildFrame = null;
+ if (!_disposed) _readCatalog();
+ });
+ }
+
+ int? _rebuildFrame;
+
+ void _readCatalog() {
final took = kTypingTrace ? (Stopwatch()..start()) : null;
final next = _cache.read(notifier);
// Nothing about the fleet changed shape, so the rows cannot have either —
@@ -547,6 +566,10 @@ class PhoneSearchController extends ChangeNotifier {
@override
void dispose() {
_disposed = true;
+ final rebuild = _rebuildFrame;
+ if (rebuild != null) {
+ SchedulerBinding.instance.cancelFrameCallbackWithId(rebuild);
+ }
_content
..removeListener(_contentChanged)
..dispose();
diff --git a/mobile/lib/phone/phone_search_results.dart b/mobile/lib/phone/phone_search_results.dart
index 2a2937436..a5052467c 100644
--- a/mobile/lib/phone/phone_search_results.dart
+++ b/mobile/lib/phone/phone_search_results.dart
@@ -140,6 +140,8 @@ class PhoneSearchResultsState extends State<PhoneSearchResults> {
void initState() {
super.initState();
final notifier = widget.notifier;
+ final sliding = widget.opening?.status;
+ _rowsDue = sliding == null || sliding == AnimationStatus.completed;
// ⚠️ **Opening the search is what re-reaches the fleet.** Until here the app
// has only the machines that happened to answer at launch, and a machine the
// account reported down was never even dialled. Asked on the way in, not
@@ -157,6 +159,7 @@ class PhoneSearchResultsState extends State<PhoneSearchResults> {
// the phone has; the reach only adds to them, a few hundred milliseconds later.
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
+ if (!_rowsDue) setState(() => _rowsDue = true);
final opening = widget.opening;
if (opening == null || opening.status == AnimationStatus.completed) {
unawaited(notifier.reachAllMachines());
@@ -175,6 +178,15 @@ class PhoneSearchResultsState extends State<PhoneSearchResults> {
/// The sheet still sliding up, waited on before the machines are reached — see [initState].
Animation<double>? _opening;
+ /// False on the sheet's first frame alone, while it is on its way up: the rows are built on the
+ /// next one.
+ ///
+ /// ⚠️ **One frame carried the sheet, its field and a screen of rows at once** — 16–23ms, a
+ /// dropped frame on every opening, most of it the rows being built and their text laid out
+ /// (measured on a phone, 2026-10-05). On that first frame the sheet has barely left the
+ /// screen's edge, so the list arriving a frame later is not something anyone sees.
+ bool _rowsDue = true;
+
/// Only `completed` ends the wait: a sheet dragged by a finger passes through every other
/// status on its way up, and one closed again before it was up takes this list — and the
/// listener, in [dispose] — down with it.
@@ -228,12 +240,14 @@ class PhoneSearchResultsState extends State<PhoneSearchResults> {
onSelected: (id) => setState(() => _tabId = id),
),
Expanded(
- child: _find(
- search,
- _narrowed(all, tab),
- tab: tab,
- everywhere: _sessions(all),
- ),
+ child: _rowsDue
+ ? _find(
+ search,
+ _narrowed(all, tab),
+ tab: tab,
+ everywhere: _sessions(all),
+ )
+ : const SizedBox.shrink(),
),
],
);
diff --git a/mobile/lib/phone/terminal_page.dart b/mobile/lib/phone/terminal_page.dart
index 75e1a6d5f..45bd5f619 100644
--- a/mobile/lib/phone/terminal_page.dart
+++ b/mobile/lib/phone/terminal_page.dart
@@ -782,6 +782,14 @@ class _TerminalPageState extends State<TerminalPage>
widget.notifier.addListener(_onNotifier);
}
+ @override
+ void didChangeDependencies() {
+ super.didChangeDependencies();
+ // Which of the page's rebuilds an inherited widget asked for — against
+ // `page.update` (its parent) and `page.build` (all of them).
+ if (kTypingTrace) typingCount('page.deps');
+ }
+
/// Releases the resize hold when this page is parked mid-animation.
///
/// A settle left running would end on a page that is no longer on screen, and
@@ -789,6 +797,7 @@ class _TerminalPageState extends State<TerminalPage>
@override
void didUpdateWidget(TerminalPage oldWidget) {
super.didUpdateWidget(oldWidget);
+ if (kTypingTrace) typingCount('page.update');
if (!identical(oldWidget.notifier, widget.notifier)) {
oldWidget.notifier.removeListener(_onNotifier);
widget.notifier.addListener(_onNotifier);
@@ -1904,9 +1913,7 @@ class _TerminalPageState extends State<TerminalPage>
child: const SizedBox.expand(),
),
),
- MediaQuery.removePadding(
- context: context,
- removeBottom: true,
+ _NoBottomPadding(
child: SafeArea(
bottom: false,
// ⚠️ **The one bottom inset that IS taken back, by hand rather than
@@ -2731,6 +2738,29 @@ class _TerminalPageState extends State<TerminalPage>
}
}
+/// The page's MediaQuery with the bottom inset taken out — see the note on
+/// `removePadding` in [_TerminalPageState.build].
+///
+/// ⚠️ **Its own widget so the PAGE does not depend on the MediaQuery.**
+/// `MediaQuery.removePadding` reads the whole of it through the context it is
+/// given, and given the page's, every change to any field rebuilt the page and
+/// the terminal under it — once a frame while a keyboard slid (measured on a
+/// phone, 2026-10-05: ~13 rebuilds a slide, all of them `didChangeDependencies`).
+/// Here only this rebuilds, and hands the same [child] on: what lies below is
+/// rebuilt only where it reads a value that actually changed.
+class _NoBottomPadding extends StatelessWidget {
+ const _NoBottomPadding({required this.child});
+
+ final Widget child;
+
+ @override
+ Widget build(BuildContext context) => MediaQuery.removePadding(
+ context: context,
+ removeBottom: true,
+ child: child,
+ );
+}
+
/// What the terminal shows when the agent it opened on is not there any more.
///
/// ⚠️ **This is the cost of opening from the cache, and the whole of it.** The
diff --git a/mobile/third_party/xterm/lib/src/ui/custom_text_edit.dart b/mobile/third_party/xterm/lib/src/ui/custom_text_edit.dart
index 5f4ca118a..4ca16f7ea 100644
--- a/mobile/third_party/xterm/lib/src/ui/custom_text_edit.dart
+++ b/mobile/third_party/xterm/lib/src/ui/custom_text_edit.dart
@@ -139,6 +139,7 @@ class CustomTextEditState extends State<CustomTextEdit> with TextInputClient {
inputTrace(() => 'ime set by embedder "${traceText(value.text)}"');
_settleRewrite();
_cancelPendingDeletes();
+ _dropPush();
_currentEditingState = value;
_terminalText = value.text;
_connection?.setEditingState(value);
@@ -158,10 +159,114 @@ class CustomTextEditState extends State<CustomTextEdit> with TextInputClient {
// What the keyboard was rewriting reaches the terminal before the buffer is emptied under it.
_settleRewrite();
_cancelPendingDeletes();
+ // Read before the mirror is emptied: the keyboard keeps it until the push lands.
+ final keyboardText = _keyboardText ?? _currentEditingState.text;
_currentEditingState = _initEditingState.copyWith();
_terminalText = _currentEditingState.text;
widget.onComposing(null, 0);
- _connection?.setEditingState(_currentEditingState);
+ _pushSoon(keyboardText);
+ }
+
+ /// AUTONOMOUS PATCH: what the keyboard's own buffer holds while this side's
+ /// state waits to be handed to it — see [_pushSoon]. Null when it has it.
+ String? _keyboardText;
+ Timer? _pushTimer;
+
+ /// Long enough to land behind the microtasks the keyboard's callback queued
+ /// — see [_pushSoon].
+ static const _pushDelay = Duration(milliseconds: 4);
+
+ /// AUTONOMOUS PATCH: hands the keyboard [_currentEditingState] a moment from
+ /// now, not from inside the callback that changed it.
+ ///
+ /// ⚠️ **The keystroke has to leave before the keyboard is handed its buffer.**
+ /// Dart's calls into iOS run on the main thread one after another, in the
+ /// order they were made, and `setEditingState` is the expensive one: the
+ /// keyboard reloads itself, 30–80ms on an iPhone 14. Made at once, the push
+ /// went ahead of the keystroke the same callback typed — whose send is still
+ /// a microtask then — so a Backspace that spent the padding waited 44–93ms to
+ /// leave the phone, and Return 105ms (measured on a phone, 2026-10-05). The
+ /// engine runs the callback's microtasks in a task it posts as the callback
+ /// returns; a timer's task is posted when it fires, so [_pushDelay] puts the
+ /// push behind the keystroke.
+ ///
+ /// Until it lands the keyboard still holds [keyboardText], and an edit it
+ /// makes to that is carried over by [_editOnStaleBuffer].
+ void _pushSoon(String keyboardText) {
+ if (!hasInputConnection) {
+ _dropPush();
+ return;
+ }
+ _keyboardText = keyboardText;
+ _pushTimer ??= Timer(_pushDelay, _pushNow);
+ }
+
+ void _pushNow() {
+ _pushTimer?.cancel();
+ _pushTimer = null;
+ if (_keyboardText == null) return;
+ _keyboardText = null;
+ if (!hasInputConnection) return;
+ inputTrace(
+ () => 'ime → keyboard "${traceText(_currentEditingState.text)}"',
+ );
+ _connection!.setEditingState(_currentEditingState);
+ }
+
+ /// Forgets a push that something else has made unnecessary — a push of its
+ /// own, or a connection gone.
+ void _dropPush() {
+ _pushTimer?.cancel();
+ _pushTimer = null;
+ _keyboardText = null;
+ }
+
+ /// AUTONOMOUS PATCH: an edit the keyboard made to the buffer it still had,
+ /// before [_pushSoon] handed it this side's.
+ ///
+ /// ⚠️ **Not diffed against the mirror.** The value is relative to
+ /// [_keyboardText] — after a submit, the line just sent — and diffed against
+ /// the emptied mirror it would type that line again. What the keyboard
+ /// changed is carried over onto the mirror instead, and the result goes on
+ /// as if the keyboard had sent it.
+ void _editOnStaleBuffer(TextEditingValue value) {
+ final stale = _keyboardText!;
+ if (value.text == stale) {
+ // Only a selection or a marked range moved, on a buffer being replaced.
+ inputTrace(
+ () => 'ime ignored: the keyboard has not been handed its buffer');
+ return;
+ }
+ final edit = _TextEdit.between(stale, value.text);
+ final mirror = _currentEditingState.text.runes.toList();
+ final removed = edit.removed.runes.length;
+ final kept = mirror.length > removed ? mirror.length - removed : 0;
+ final text = String.fromCharCodes(mirror.take(kept)) + edit.inserted;
+ // Marked text sits in what was inserted, at the end: it moves with it.
+ final shift = text.length - value.text.length;
+ final composing = value.composing;
+ final rebased = TextEditingValue(
+ text: text,
+ selection: TextSelection.collapsed(offset: text.length),
+ composing: composing.isValid &&
+ !composing.isCollapsed &&
+ composing.start + shift >= 0
+ ? TextRange(
+ start: composing.start + shift,
+ end: composing.end + shift,
+ )
+ : TextRange.empty,
+ );
+ inputTrace(
+ () => 'ime rebased onto the mirror: del=$removed'
+ ' ins="${traceText(edit.inserted)}" → "${traceText(text)}"',
+ );
+ _keyboardText = value.text;
+ _applyEditingValue(
+ rebased,
+ hasTextMutation: rebased.text != _currentEditingState.text,
+ );
+ _pushSoon(value.text);
}
void setEditableRect(Rect rect, Rect caretRect) {
@@ -305,6 +410,7 @@ class CustomTextEditState extends State<CustomTextEdit> with TextInputClient {
// setEditableRect(Rect.zero, Rect.zero);
+ _dropPush();
_connection!.setEditingState(_initEditingState);
}
}
@@ -379,6 +485,10 @@ class CustomTextEditState extends State<CustomTextEdit> with TextInputClient {
inputTrace(() => 'ime dropped: the newline echo of an action');
return;
}
+ if (_keyboardText != null) {
+ _editOnStaleBuffer(value);
+ return;
+ }
if (_holdRewrite(value)) return;
_applyEditingValue(
value,
@@ -512,7 +622,7 @@ class CustomTextEditState extends State<CustomTextEdit> with TextInputClient {
value.text != '${_initEditingState.text}\n') {
return false;
}
- _connection?.setEditingState(_currentEditingState);
+ _pushSoon(value.text);
return true;
}
@@ -599,8 +709,9 @@ class CustomTextEditState extends State<CustomTextEdit> with TextInputClient {
// The line goes as the keyboard ended it, before the action acts on it.
_settleRewrite();
// Captured before the handler runs: it is what the pty has been sent, and
- // what iOS is about to append its newline to. See [_pendingActionEcho].
- _pendingActionEcho = _currentEditingState.text;
+ // what iOS is about to append its newline to — its own buffer, if this
+ // side's has not reached it yet. See [_pendingActionEcho].
+ _pendingActionEcho = _keyboardText ?? _currentEditingState.text;
widget.onAction(action);
}
@@ -651,6 +762,7 @@ class CustomTextEditState extends State<CustomTextEdit> with TextInputClient {
composing: TextRange.empty,
);
_currentEditingState = next;
+ _dropPush();
_connection?.setEditingState(next);
_syncTerminalText(next.text);
return;