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): terminal scrolls again after New Agent → back

bubu-dudue9ab9d1e7bf6
diff --git a/mobile/lib/phone/agent_swipe.dart b/mobile/lib/phone/agent_swipe.dart
index 8a3306a15..1a0797156 100644
--- a/mobile/lib/phone/agent_swipe.dart
+++ b/mobile/lib/phone/agent_swipe.dart
@@ -117,6 +117,7 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {
@override
void initState() {
super.initState();
+ _livePagers.add(this);
// A fresh pager is a fresh session: whatever the person did to the keyboard
// the last time they were in a terminal does not decide what this one does.
// See [resetKeyboardSession].
@@ -149,6 +150,9 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {
_warmer?.dispose();
_voice.dispose();
_controller?.dispose();
+ // Out of the live set FIRST: [_detachAll] skips panes a live pager holds, and this one no
+ // longer counts.
+ _livePagers.remove(this);
_detachAll();
// ⚠️ **The record is deliberately NOT cleared here, and it used to be.** The old rule was
// "leaving the terminal means the next launch starts on the list" — but there is no list to
@@ -169,20 +173,43 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {
/// Not awaited, and deliberately: `dispose` cannot wait, and `closePane` only has to be STARTED —
/// it detaches the session and tells the daemon on its own. The notifier outlives this widget, so
/// nothing here is torn down underneath it.
+ ///
+ /// ⚠️ **After the frame, never from `dispose` itself.** `closePane` notifies synchronously, and
+ /// `dispose` runs while the tree is locked: every listener on the notifier failed to mark itself
+ /// dirty ("setState() called when widget tree was locked") and MISSED that update — the pager
+ /// built in this one's place among them, whose terminal was left stuck on a stale build (it would
+ /// not scroll until it was opened again). Deferred, the close lands on an unlocked tree.
+ ///
+ /// ⚠️ And a pane a live pager has attached since is left alone: the pager replacing this one can
+ /// be showing one of the very agents swiped past here, and closing its pane after it mounted would
+ /// pull the terminal out from under it.
void _detachAll() {
final notifier = widget.notifier;
final keep = _current;
- for (final agent in _attached) {
- if (agent == keep) continue;
- final pane = notifier.panes
- .where(
- (p) => p.machineId == agent.machineId && p.agentId == agent.agentId,
- )
- .firstOrNull;
- if (pane != null) unawaited(notifier.closePane(pane.id));
- }
+ final leaving = {
+ for (final agent in _attached)
+ if (agent != keep) agent,
+ };
+ if (leaving.isEmpty) return;
+ WidgetsBinding.instance.addPostFrameCallback((_) {
+ for (final agent in leaving) {
+ if (_livePagers.any((pager) => pager._attached.contains(agent))) {
+ continue;
+ }
+ final pane = notifier.panes
+ .where(
+ (p) =>
+ p.machineId == agent.machineId && p.agentId == agent.agentId,
+ )
+ .firstOrNull;
+ if (pane != null) unawaited(notifier.closePane(pane.id));
+ }
+ });
}
+ /// Every pager currently mounted — what [_detachAll] asks before closing a pane it attached.
+ static final Set<_AgentSwipeHostState> _livePagers = {};
+
@override
Widget build(BuildContext context) {
final neighbours = widget.neighbours;
diff --git a/mobile/test/terminal_alt_scroll_test.dart b/mobile/test/terminal_alt_scroll_test.dart
new file mode 100644
index 000000000..d134872b1
--- /dev/null
+++ b/mobile/test/terminal_alt_scroll_test.dart
@@ -0,0 +1,52 @@
+import 'package:flutter/material.dart';
+import 'package:flutter_test/flutter_test.dart';
+// The view that turns an alternate-buffer drag into wheel events: it is not
+// exported, and the bug lived in its render object.
+// ignore: implementation_imports
+import 'package:xterm/src/ui/infinite_scroll_view.dart';
+
+/// A full-screen agent (Claude Code) is scrolled through [InfiniteScrollView],
+/// which reports each drag so it can be sent on as wheel events.
+///
+/// The reported case: open New Agent from the terminal, come back, and the
+/// terminal no longer scrolled — until the agent was opened again. The
+/// Scrollable had replaced its position (it does on every dependency change),
+/// and the view was still listening to the old one.
+void main() {
+ testWidgets('a drag still reports after the scroll position is replaced', (
+ tester,
+ ) async {
+ final offsets = <double>[];
+ var ratio = 2.0;
+ late StateSetter rebuild;
+ await tester.pumpWidget(
+ MaterialApp(
+ home: StatefulBuilder(
+ builder: (context, setState) {
+ rebuild = setState;
+ return MediaQuery(
+ data: MediaQuery.of(context).copyWith(devicePixelRatio: ratio),
+ child: InfiniteScrollView(
+ onScroll: offsets.add,
+ child: const SizedBox.expand(),
+ ),
+ );
+ },
+ ),
+ ),
+ );
+
+ await tester.drag(find.byType(InfiniteScrollView), const Offset(0, 120));
+ await tester.pumpAndSettle();
+ expect(offsets, isNotEmpty);
+
+ // A dependency of the Scrollable changes, so it builds a new position.
+ rebuild(() => ratio = 3.0);
+ await tester.pump();
+ offsets.clear();
+
+ await tester.drag(find.byType(InfiniteScrollView), const Offset(0, 120));
+ await tester.pumpAndSettle();
+ expect(offsets, isNotEmpty);
+ });
+}
diff --git a/mobile/third_party/xterm/README.autonomous.md b/mobile/third_party/xterm/README.autonomous.md
index 977881b9f..05f6d8281 100644
--- a/mobile/third_party/xterm/README.autonomous.md
+++ b/mobile/third_party/xterm/README.autonomous.md
@@ -212,3 +212,12 @@ it if one is dropped.
returns before the hash, the cache lookup and the draw. An underlined
space still draws, through the existing U+00A0 substitution. Regression:
`mobile/test/terminal_painter_test.dart`.
+15. **Alt-buffer scrolling survives a new scroll position**
+ (`lib/src/ui/infinite_scroll_view.dart`). `_RenderInfiniteScrollView`
+ listened for `_onScroll` on the position it was attached with, and its
+ `position` setter moved only the layout listener. The Scrollable replaces
+ its position whenever its dependencies change — a route pushed over the
+ terminal and popped is enough — so from then on no drag reached the
+ full-screen program as wheel events or arrow keys, until the view was
+ rebuilt. The setter now moves `_onScroll` with it. Regression:
+ `mobile/test/terminal_alt_scroll_test.dart`.
diff --git a/mobile/third_party/xterm/lib/src/ui/infinite_scroll_view.dart b/mobile/third_party/xterm/lib/src/ui/infinite_scroll_view.dart
index 9640a3cac..7561fcccd 100644
--- a/mobile/third_party/xterm/lib/src/ui/infinite_scroll_view.dart
+++ b/mobile/third_party/xterm/lib/src/ui/infinite_scroll_view.dart
@@ -76,9 +76,20 @@ class _RenderInfiniteScrollView extends RenderShiftedBox {
ViewportOffset _position;
set position(ViewportOffset value) {
if (_position == value) return;
- if (attached) _position.removeListener(markNeedsLayout);
+ // AUTONOMOUS PATCH: `_onScroll` moves to the new position too. The
+ // Scrollable replaces its position whenever its dependencies change (a
+ // route pushed over the terminal and popped is enough), and the listener
+ // left on the old one meant no gesture reached the program again — a
+ // full-screen agent that would not scroll until its view was rebuilt.
+ if (attached) {
+ _position.removeListener(markNeedsLayout);
+ _position.removeListener(_onScroll);
+ }
_position = value;
- if (attached) _position.addListener(markNeedsLayout);
+ if (attached) {
+ _position.addListener(markNeedsLayout);
+ _position.addListener(_onScroll);
+ }
markNeedsLayout();
}