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: replace AgentNeighbourWarmer with AgentPanePruner for improved agent stream management

Dev jacob5e863e15c8d5
diff --git a/mobile/lib/phone/agent_neighbour_warmer.dart b/mobile/lib/phone/agent_neighbour_warmer.dart
deleted file mode 100644
index 8d87a4504..000000000
--- a/mobile/lib/phone/agent_neighbour_warmer.dart
+++ /dev/null
@@ -1,110 +0,0 @@
-import 'dart:async';
-
-import 'package:harness_mobile/state/app_state.dart';
-import 'package:harness_mobile/terminal/terminal_session.dart';
-
-import 'agent_swipe_list.dart';
-
-/// Opens the agents one swipe either side of the one on screen, and closes the ones further out.
-///
-/// ⚠️ **Why a swipe needs this.** A page is not built until the finger drags it in, and an agent
-/// nobody has opened yet had no stream at all until the swipe passed halfway: the page slid in as
-/// "Attaching…", the stream opened, and the agent's screen arrived as a keyframe a network round
-/// trip later — well after the swipe had finished. Opened in advance, the keyframe has long landed
-/// and the page slides in already showing its output.
-///
-/// ⚠️ **Only once the agent on screen is live, and a beat after.** Two reasons. Its size is the size
-/// the neighbours open at — every page of the pager shares it — and there is no size until its own
-/// keyframe has come back. And the home screen closes every pane but the one it opened as it builds
-/// a pager (`AgentHome._attachOnly`); by the time the agent on screen is live that has happened, so
-/// it cannot close what this opens. The beat keeps the neighbours' keyframes, parsed on the UI
-/// thread, off the frames where the landed page is still settling.
-///
-/// ⚠️ **A window of three, not everything ever swiped past.** The pager used to keep every agent it
-/// visited attached until it was thrown away; with neighbours opened on top of that, a lap of the
-/// list would leave a live stream per agent. Agents outside the window are closed as it moves.
-class AgentNeighbourWarmer {
- AgentNeighbourWarmer({
- required this.notifier,
- required this.list,
- required this.attached,
- });
-
- final AppNotifier notifier;
- final AgentSwipeList list;
-
- /// The pager's own record of what it opened — shared, so what this opens is closed with the rest
- /// when the pager goes, and what this closes is no longer on it.
- final Set<AgentRef> attached;
-
- /// How long the page on screen has been live before its neighbours are opened.
- static const delay = Duration(milliseconds: 600);
-
- Timer? _timer;
-
- /// The page whose neighbours are open, so every notifier change afterwards costs one comparison.
- int? _warmedPage;
-
- /// Warms [page]'s neighbours if [current] — the agent on it — is live and they are not warm yet.
- ///
- /// Cheap enough to call on every notifier change, which is how the pager calls it: that is the only
- /// signal that the agent on screen has gone live.
- void check({required int page, required AgentRef current}) {
- if (!list.wraps || _warmedPage == page || _timer != null) return;
- final session = notifier
- .paneOfAgent(current.machineId, current.agentId)
- ?.session;
- if (session == null ||
- session.status != TerminalSessionStatus.controlling) {
- return;
- }
- _timer = Timer(delay, () {
- _timer = null;
- _warmedPage = page;
- _warm(page, current, cols: session.cols, rows: session.rows);
- });
- }
-
- /// Forgets the page it was warming for — the pager has moved.
- void cancel() {
- _timer?.cancel();
- _timer = null;
- _warmedPage = null;
- }
-
- void dispose() => cancel();
-
- AgentRef _at(int page) {
- final entry = list.entries[page % list.entries.length];
- return (machineId: entry.machineId, agentId: entry.agent.id);
- }
-
- void _warm(
- int page,
- AgentRef current, {
- required int cols,
- required int rows,
- }) {
- // A set, so a two-agent pager — whose page before and page after are the same agent — opens it
- // once.
- final window = {current, _at(page - 1), _at(page + 1)};
- for (final agent in [...attached]) {
- if (window.contains(agent)) continue;
- attached.remove(agent);
- final pane = notifier.paneOfAgent(agent.machineId, agent.agentId);
- if (pane != null) unawaited(notifier.closePane(pane.id));
- }
- for (final agent in window) {
- if (agent == current) continue;
- attached.add(agent);
- unawaited(
- notifier.preattachAgent(
- agent.machineId,
- agent.agentId,
- cols: cols,
- rows: rows,
- ),
- );
- }
- }
-}
diff --git a/mobile/lib/phone/agent_pane_prune.dart b/mobile/lib/phone/agent_pane_prune.dart
new file mode 100644
index 000000000..9a67ccc7d
--- /dev/null
+++ b/mobile/lib/phone/agent_pane_prune.dart
@@ -0,0 +1,87 @@
+import 'dart:async';
+
+import 'package:harness_mobile/core/last_opened_agent.dart' show AgentRef;
+import 'package:harness_mobile/state/app_state.dart';
+
+/// Closes the streams of agents a pager is no longer showing.
+///
+/// [heldElsewhere] spares an agent another live pager is holding: the pager replacing this one can
+/// be showing an agent this one swiped past, and closing its pane after it mounted would pull the
+/// terminal out from under it.
+void releaseAgentPanes(
+ AppNotifier notifier,
+ Iterable<AgentRef> agents, {
+ required bool Function(AgentRef) heldElsewhere,
+}) {
+ for (final agent in agents) {
+ if (heldElsewhere(agent)) continue;
+ final pane = notifier.paneOfAgent(agent.machineId, agent.agentId);
+ if (pane == null) continue;
+ // Not awaited: `closePane` detaches the session and tells the daemon on its own, and every
+ // caller here is a callback that cannot wait for it.
+ unawaited(notifier.closePane(pane.id));
+ }
+}
+
+/// Keeps the phone down to ONE open agent — the one on screen — by closing the rest a beat after
+/// each swipe.
+///
+/// ⚠️ **Why an agent nobody is looking at must not stay open.** The daemon keeps a single controller
+/// per agent, so an open stream is a CLAIM on that agent's terminal: every agent the pager had been
+/// swiped to held one until the pager was thrown away, which on the phone is a whole session. A lap
+/// of the list left the desktop locked out of every agent on it. Closing hands each one straight
+/// back.
+///
+/// ⚠️ **A beat after the swipe, not on it.** `onPageChanged` fires at the halfway point of the
+/// settle, with the page being left still sliding off screen — closing its stream there empties the
+/// terminal the swipe is still showing, and it finishes the animation as an "Attaching…" skeleton.
+/// The beat also folds a fling across several pages into one prune, at the page it lands on.
+///
+/// The cost is that swiping back re-attaches rather than arriving on output already there. That is
+/// the trade this class exists to make: one remote stream per phone, not one per agent visited.
+class AgentPanePruner {
+ AgentPanePruner({
+ required this.notifier,
+ required this.attached,
+ required this.heldElsewhere,
+ });
+
+ final AppNotifier notifier;
+
+ /// The pager's own record of what it opened — shared, so what this closes is no longer on it and
+ /// what the pager adds is seen here.
+ final Set<AgentRef> attached;
+
+ final bool Function(AgentRef) heldElsewhere;
+
+ /// How long after landing on a page the agents behind it are closed.
+ static const delay = Duration(milliseconds: 400);
+
+ Timer? _timer;
+
+ /// Arms the prune for [current], the agent now on screen. Re-arming replaces the pending one, so
+ /// a flick through five pages prunes once.
+ void keepOnly(AgentRef current) {
+ _timer?.cancel();
+ _timer = Timer(delay, () {
+ _timer = null;
+ final leaving = [
+ for (final agent in attached)
+ if (agent != current) agent,
+ ];
+ if (leaving.isEmpty) return;
+ attached
+ ..clear()
+ ..add(current);
+ releaseAgentPanes(notifier, leaving, heldElsewhere: heldElsewhere);
+ });
+ }
+
+ /// Abandons the pending prune — the pager has moved, or is going.
+ void cancel() {
+ _timer?.cancel();
+ _timer = null;
+ }
+
+ void dispose() => cancel();
+}
diff --git a/mobile/lib/phone/agent_swipe.dart b/mobile/lib/phone/agent_swipe.dart
index 1a0797156..30d91bf83 100644
--- a/mobile/lib/phone/agent_swipe.dart
+++ b/mobile/lib/phone/agent_swipe.dart
@@ -4,7 +4,7 @@ import 'package:flutter/material.dart';
import 'package:harness_mobile/state/app_state.dart';
-import 'agent_neighbour_warmer.dart';
+import 'agent_pane_prune.dart';
import 'agent_swipe_list.dart';
import 'terminal_page.dart';
import 'voice_input_controller.dart';
@@ -78,30 +78,35 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {
agentId: widget.agentId,
);
- /// Every agent this pager attached, so leaving can detach them again.
+ /// Every agent this pager attached and has not closed yet — in practice the one on screen, and
+ /// the one behind it until [_pruner]'s beat has passed.
///
/// ⚠️ **Without this the pager leaks terminals.** The phone's old rule was one pane, enforced by
- /// closing every other one on the way in; a pager has to keep its neighbours attached, so nothing
- /// would ever close them. Each pane left behind is a remote stream with a 10,000-line scrollback
- /// and a heartbeat, held for a screen that is gone.
+ /// closing every other one on the way in; a pager attaches its own pages, so nothing else would
+ /// ever close them. Each pane left behind is a remote stream with a 10,000-line scrollback and a
+ /// heartbeat, held for a screen that is gone — and a claim on an agent the desktop then cannot
+ /// open, since the daemon keeps a single controller per agent.
///
/// Recorded rather than recomputed: by the time this page is disposed the list may have moved on,
/// and the panes to close are the ones actually opened, not the ones a fresh list would name.
- /// [_warmer] adds to it and prunes it as the pager moves — see [AgentNeighbourWarmer].
+ ///
+ /// ⚠️ **Only agents actually SWIPED TO go in here.** The pager used to open the agent one swipe
+ /// either side as well, so a page slid in already showing output instead of "Attaching…" — which
+ /// took the terminal of the two agents beside the one being read away from the desktop, and they
+ /// were agents nobody had asked for.
final Set<AgentRef> _attached = {};
- /// Opens the agents either side of the one on screen once it is live. Null for a pager with nothing
- /// to swipe to.
- late final AgentNeighbourWarmer? _warmer = widget.neighbours?.wraps == true
- ? AgentNeighbourWarmer(
- notifier: widget.notifier,
- list: widget.neighbours!,
- attached: _attached,
- )
- : null;
+ /// Closes every agent but the one on screen, a beat after each swipe. Null for a passthrough page,
+ /// which has no swipe to prune after. See [AgentPanePruner].
+ AgentPanePruner? _pruner;
- /// The only signal that the agent on screen has gone live — see [AgentNeighbourWarmer.check].
- void _checkNeighbours() => _warmer?.check(page: _page, current: _current);
+ /// Whether [agent] belongs to a pager OTHER than this one, which is then the one to close it.
+ ///
+ /// `pager != this` matters only while this pager is live — [_detachAll] is out of [_livePagers]
+ /// by the time it asks — and without it the prune would spare every agent on its own record.
+ bool _heldByAnotherPager(AgentRef agent) => _livePagers.any(
+ (pager) => pager != this && pager._attached.contains(agent),
+ );
/// Voice input for every page of this pager: a take in progress, and what has been heard so far,
/// survive a swipe the way a keyboard that is up does. Disposed with the pager, which is what
@@ -140,14 +145,16 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {
? _origin * neighbours.entries.length + start
: start;
_controller = PageController(initialPage: _page);
- widget.notifier.addListener(_checkNeighbours);
- _checkNeighbours();
+ _pruner = AgentPanePruner(
+ notifier: widget.notifier,
+ attached: _attached,
+ heldElsewhere: _heldByAnotherPager,
+ );
}
@override
void dispose() {
- widget.notifier.removeListener(_checkNeighbours);
- _warmer?.dispose();
+ _pruner?.dispose();
_voice.dispose();
_controller?.dispose();
// Out of the live set FIRST: [_detachAll] skips panes a live pager holds, and this one no
@@ -168,7 +175,8 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {
/// Closes what this pager opened, keeping the one the phone is still pointed at.
///
/// The agent last read stays attached — that is the pane the Agents tab's row now refers to, and
- /// re-opening it should be instant rather than a fresh "Attaching…". Everything swiped past goes.
+ /// re-opening it should be instant rather than a fresh "Attaching…". Anything [_pruner] has not
+ /// caught up with yet goes with it.
///
/// 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
@@ -180,9 +188,7 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {
/// 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.
+ /// ⚠️ And a pane a live pager has attached since is left alone — see [_heldByAnotherPager].
void _detachAll() {
final notifier = widget.notifier;
final keep = _current;
@@ -192,18 +198,7 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {
};
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));
- }
+ releaseAgentPanes(notifier, leaving, heldElsewhere: _heldByAnotherPager);
});
}
@@ -300,13 +295,13 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {
// Before the setState, so a host that rebuilds this pager in response already names the agent
// swiped to — told afterwards, it would rebuild still pointing at the previous one.
widget.onAgentChanged?.call(arrived);
- // Warm-up for the page just left is abandoned; the page arrived at is warmed once it is live.
- _warmer?.cancel();
setState(() {
_page = index;
_current = arrived;
});
- _checkNeighbours();
+ // The agents behind this page go back to whoever else wants them, once the swipe has settled —
+ // see [AgentPanePruner].
+ _pruner?.keepOnly(arrived);
// Attaching is what makes the terminal live, and it only happens once the page has SETTLED —
// `onPageChanged` fires at the halfway point of a settled swipe, not on every dragged pixel, so
// flicking across five agents attaches the ones passed through rather than all of them at once.
diff --git a/mobile/lib/phone/phone_navigation.dart b/mobile/lib/phone/phone_navigation.dart
index d8e33e981..42a28cb55 100644
--- a/mobile/lib/phone/phone_navigation.dart
+++ b/mobile/lib/phone/phone_navigation.dart
@@ -184,8 +184,8 @@ void openAgentPager(
///
/// [keepOthers] is what separates the two ways in. A page opened on its own keeps the phone's old
/// rule — one pane, because a second one attached behind a screen nobody can see is a terminal
-/// streaming for nothing. A PAGER deliberately keeps its neighbours attached: that is the whole
-/// point of swiping, and the panes it keeps are exactly the pages it has mounted.
+/// streaming for nothing. A PAGER does its own housekeeping instead: it closes the agent behind it
+/// a beat after each swipe, and the rest when it goes (see [AgentSwipeHost]).
///
/// `selectAgent` already does the right thing either way — it reuses an existing pane and only
/// reopens a session that died, so arriving back on a page already attached costs nothing.
diff --git a/mobile/lib/phone/terminal_page.dart b/mobile/lib/phone/terminal_page.dart
index 59c0dcc50..211df9c06 100644
--- a/mobile/lib/phone/terminal_page.dart
+++ b/mobile/lib/phone/terminal_page.dart
@@ -747,14 +747,22 @@ class _TerminalPageState extends State<TerminalPage>
.firstOrNull;
if (pane != null) {
_hadPane = true;
- } else if (_hadPane && widget.isActive) {
- // The pane this page was showing is gone — another tab opened a different agent, or the
- // agent was deleted. Leave rather than spin: there is nothing here to come back.
+ } else if (!widget.isActive) {
+ // ⚠️ **A parked page whose pane went away has to FORGET it ever had one.** It is the
+ // ordinary case now: the pager closes the agent behind it a beat after each swipe, so the
+ // phone holds only the one on screen (see [AgentPanePruner]). The flag means "this page's
+ // stream is live", and a parked page's is not — swiping back is what attaches it again.
+ // Left set, the page would land saying the agent had GONE for any frame that beats the
+ // attach to it, and the branch below takes the whole pager down for that.
+ _hadPane = false;
+ } else if (_hadPane) {
+ // The pane this page was showing is gone while it was the one being READ — another tab
+ // opened a different agent, or the agent was deleted. Leave rather than spin: there is
+ // nothing here to come back.
//
// ⚠️ Only the ACTIVE page may leave, and only it ever should. A page parked beside the one
// being read shares the route, so popping from there would take the whole pager down —
- // including the terminal actually on screen. A parked page whose pane went away simply
- // waits: swiping to it is what makes it attach again.
+ // including the terminal actually on screen.
_leave();
}
final session = pane?.session;
diff --git a/mobile/lib/state/app_state.dart b/mobile/lib/state/app_state.dart
index a7d740cfd..16297e585 100644
--- a/mobile/lib/state/app_state.dart
+++ b/mobile/lib/state/app_state.dart
@@ -5136,43 +5136,6 @@ class AppNotifier extends ChangeNotifier {
await addAgentToSwarm(machineId, agentId);
}
- /// Opens an agent's terminal WITHOUT selecting it — the phone's pager warming the agents one
- /// swipe either side of the one on screen, so a swipe lands on output rather than on
- /// "Attaching…" while a stream opens and its keyframe crosses the network.
- ///
- /// ⚠️ **Nothing about the selection moves, and that is the difference from [selectAgent].** Not
- /// the focused pane, the selected machine, the machine's active agent, the focus announced to the
- /// dial, nor the persisted layout: every one of those still names the agent being read, and a
- /// warm-up that moved them would have the dial and a relaunch follow an agent nobody opened.
- ///
- /// The stream opens at [cols]×[rows] rather than waiting for a view to measure it, because there
- /// IS no view — the page is not built until it is swiped to. The pager's pages all share one
- /// size, so the size of the page on screen is the right one, and the keyframe that comes back is
- /// already laid out for the page it will be drawn on.
- ///
- /// A pane that already holds a session is left alone. An agent that cannot be attached now — its
- /// machine offline or unlinked, its terminal unverified — gets no pane at all: an empty one would
- /// sit in the layout waiting for nothing.
- Future<void> preattachAgent(
- String machineId,
- String agentId, {
- required int cols,
- required int rows,
- }) async {
- if (_disposed || !_canAttachAgent(machineId, agentId)) return;
- var pane = paneOfAgent(machineId, agentId);
- if (pane == null) {
- if (panes.length >= maxPanes) return;
- pane = TerminalPane(
- id: _nextPaneId++,
- machineId: machineId,
- agentId: agentId,
- );
- panes.add(pane);
- }
- await _attachSession(pane, openAt: (cols: cols, rows: rows));
- }
-
/// Show a MACHINE in the grid, for the states that belong to the machine
/// rather than to any agent on it.
///
@@ -5349,13 +5312,7 @@ class AppNotifier extends ChangeNotifier {
/// Separate from [assignAgentToPane] because a restored tile takes this path
/// on its own, later, when its machine finally answers — the intent was
/// settled at launch, and nothing about the selection should move again then.
- ///
- /// [openAt] opens the stream at a size already known instead of waiting for a
- /// view to measure one — see [preattachAgent], whose pane has no view yet.
- Future<void> _attachSession(
- TerminalPane pane, {
- ({int cols, int rows})? openAt,
- }) async {
+ Future<void> _attachSession(TerminalPane pane) async {
if (_disposed || !allPanes.contains(pane) || pane.session != null) return;
final wantedAgentId = pane.agentId;
if (wantedAgentId == null) return;
@@ -5413,13 +5370,6 @@ class AppNotifier extends ChangeNotifier {
// blank "Attaching…" placeholder already covers this measurement, which lands within a frame or
// two of the panel mounting; `waitForViewportSize`'s own 2s timeout falls back to 80x24 only if
// the pane genuinely never gets laid out.
- //
- // ⚠️ Which is exactly the pane [preattachAgent] opens: it has no view until it is swiped to, so
- // waiting would sit out the whole 2s and then open at 80x24. It hands over the size instead.
- if (openAt != null) {
- await terminal.open(initialCols: openAt.cols, initialRows: openAt.rows);
- return;
- }
await terminal.open(waitForViewportSize: true);
}
diff --git a/mobile/test/agent_pager_attach_test.dart b/mobile/test/agent_pager_attach_test.dart
new file mode 100644
index 000000000..3535da1b2
--- /dev/null
+++ b/mobile/test/agent_pager_attach_test.dart
@@ -0,0 +1,165 @@
+import 'dart:async';
+
+import 'package:flutter/material.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/phone/agent_pane_prune.dart';
+import 'package:harness_mobile/phone/agent_swipe.dart';
+import 'package:harness_mobile/state/app_state.dart';
+
+import 'agent_pager_fixture.dart';
+
+/// The pager holds exactly the agent being READ: no other is opened, and the one swiped away from
+/// is closed.
+///
+/// ⚠️ **Why this has a test of its own.** The daemon keeps a single controller per agent, so an open
+/// stream is a claim on that agent's terminal. The pager used to open the agent one swipe either
+/// side to have its output ready before the swipe landed, and to keep every agent it had visited —
+/// so reading one agent on the phone took two more away from the desktop, and a lap of the list took
+/// them all.
+void main() {
+ Future<(AppNotifier, PagerConn)> pumpPager(WidgetTester tester) async {
+ final conn = PagerConn();
+ final app = pagerApp(conn);
+ addTearDown(app.dispose);
+ final list = pagerList(app);
+ await liveAgent(app, 'b');
+ await tester.pumpWidget(
+ MaterialApp(
+ home: AgentSwipeHost(
+ notifier: app,
+ machineId: 'm',
+ agentId: 'b',
+ neighbours: list,
+ ),
+ ),
+ );
+ await tester.pump();
+ return (app, conn);
+ }
+
+ /// The same pager PUSHED over a page, which is how search and the machines tab open it.
+ ///
+ /// The difference is what a page leaving can do: pushed, `_leave()` takes the route — and with it
+ /// every page of the pager — down. As a root it can only no-op, so the regression this guards
+ /// against is invisible there.
+ Future<(AppNotifier, PagerConn)> pushPager(WidgetTester tester) async {
+ final conn = PagerConn();
+ final app = pagerApp(conn);
+ addTearDown(app.dispose);
+ final list = pagerList(app);
+ await liveAgent(app, 'b');
+ final navigator = GlobalKey<NavigatorState>();
+ await tester.pumpWidget(
+ MaterialApp(
+ navigatorKey: navigator,
+ home: const Scaffold(body: Text('the list')),
+ ),
+ );
+ unawaited(
+ navigator.currentState!.push(
+ MaterialPageRoute(
+ builder: (_) => AgentSwipeHost(
+ notifier: app,
+ machineId: 'm',
+ agentId: 'b',
+ neighbours: list,
+ ),
+ ),
+ ),
+ );
+ await tester.pumpAndSettle();
+ return (app, conn);
+ }
+
+ /// A settled swipe, with the page it lands on answering as a machine does — [towards] the next
+ /// agent or back to the previous one.
+ ///
+ /// `pumpAndSettle` is deliberately not used: the page arrived at says "Attaching…" until its
+ /// keyframe lands, and that skeleton breathes for ever — see [goLive].
+ ///
+ /// The pumps run well past [AgentPanePruner.delay], so the agent left behind is closed by the time
+ /// this returns.
+ Future<void> swipeTo(
+ WidgetTester tester,
+ AppNotifier app,
+ String agentId, {
+ double towards = -400,
+ }) async {
+ await tester.fling(find.byType(PageView), Offset(towards, 0), 1000);
+ await tester.pump();
+ await tester.pump(const Duration(milliseconds: 500));
+ final session = app.paneOfAgent('m', agentId)?.session;
+ if (session != null) await goLive(session);
+ // Long enough for the keyframe's ack and the view's first resize, twice over: each coalescing
+ // window is armed by the frame before it, and the binding fails a test that leaves one pending.
+ await tester.pump(const Duration(milliseconds: 100));
+ await tester.pump(const Duration(milliseconds: 100));
+ // And past the prune, armed by the frame the page landed on.
+ await tester.pump(AgentPanePruner.delay);
+ await tester.pump();
+ }
+
+ testWidgets('leaves the agents either side of the one on screen alone', (
+ tester,
+ ) async {
+ final (app, conn) = await pumpPager(tester);
+
+ // Well past the beat the neighbours were once opened after.
+ await tester.pump(const Duration(seconds: 2));
+ await tester.pump();
+
+ expect(conn.opens, isEmpty, reason: 'no stream but the agent being read');
+ expect(app.paneOfAgent('m', 'a'), isNull);
+ expect(app.paneOfAgent('m', 'c'), isNull);
+ });
+
+ testWidgets('opens the agent swiped to, and only when it is swiped to', (
+ tester,
+ ) async {
+ final (app, conn) = await pumpPager(tester);
+
+ await swipeTo(tester, app, 'c');
+
+ expect({for (final open in conn.opens) open['agentId']}, {'c'});
+ expect(app.paneOfAgent('m', 'c')?.session, isNotNull);
+ expect(app.stateOf('m')?.activeAgentId, 'c', reason: 'the agent read');
+ expect(app.paneOfAgent('m', 'd'), isNull, reason: 'one swipe ahead');
+ expect(app.paneOfAgent('m', 'a'), isNull, reason: 'one swipe behind');
+ });
+
+ testWidgets('closes the agent swiped away from, once the swipe has settled', (
+ tester,
+ ) async {
+ final (app, _) = await pumpPager(tester);
+ expect(app.paneOfAgent('m', 'b'), isNotNull, reason: 'the agent read');
+
+ await swipeTo(tester, app, 'c');
+
+ expect(
+ app.paneOfAgent('m', 'b'),
+ isNull,
+ reason: 'handed back to whoever wants it next',
+ );
+ expect(app.paneOfAgent('m', 'c')?.session, isNotNull);
+ });
+
+ testWidgets(
+ 'an agent swiped back onto is attached again, in a pushed pager',
+ (tester) async {
+ final (app, _) = await pushPager(tester);
+ await swipeTo(tester, app, 'c');
+ expect(app.paneOfAgent('m', 'b'), isNull);
+
+ // The page for b is still mounted beside c, and its pane is the one just closed.
+ await swipeTo(tester, app, 'b', towards: 400);
+
+ expect(
+ find.byType(AgentSwipeHost),
+ findsOneWidget,
+ reason: 'a pane closed behind the pager is not the agent going away',
+ );
+ expect(app.paneOfAgent('m', 'b')?.session, isNotNull);
+ expect(app.paneOfAgent('m', 'c'), isNull, reason: 'closed in its turn');
+ },
+ );
+}
diff --git a/mobile/test/agent_preattach_test.dart b/mobile/test/agent_preattach_test.dart
deleted file mode 100644
index e4a34f5a1..000000000
--- a/mobile/test/agent_preattach_test.dart
+++ /dev/null
@@ -1,113 +0,0 @@
-import 'package:flutter/material.dart';
-import 'package:flutter_test/flutter_test.dart';
-import 'package:harness_mobile/phone/agent_neighbour_warmer.dart';
-import 'package:harness_mobile/phone/agent_swipe.dart';
-import 'package:harness_mobile/state/app_state.dart';
-
-import 'agent_pager_fixture.dart';
-
-void main() {
- group('preattachAgent', () {
- test('opens the stream at the size given, and selects nothing', () async {
- final conn = PagerConn();
- final app = pagerApp(conn);
- addTearDown(app.dispose);
- await liveAgent(app, 'a');
- final focused = app.focusedPaneId;
-
- await app.preattachAgent('m', 'b', cols: 46, rows: 38);
-
- expect(conn.opens.single['agentId'], 'b');
- expect(conn.opens.single['cols'], 46);
- expect(conn.opens.single['rows'], 38);
- expect(app.paneOfAgent('m', 'b')?.session, isNotNull);
- expect(
- app.focusedPaneId,
- focused,
- reason: 'focus stays on the agent read',
- );
- expect(app.stateOf('m')?.activeAgentId, 'a');
- });
-
- test('a pane already streaming is left alone', () async {
- final conn = PagerConn();
- final app = pagerApp(conn);
- addTearDown(app.dispose);
-
- await app.preattachAgent('m', 'b', cols: 46, rows: 38);
- await app.preattachAgent('m', 'b', cols: 46, rows: 38);
-
- expect(conn.opens, hasLength(1));
- });
-
- test('an agent that cannot be attached gets no pane', () async {
- final conn = PagerConn();
- final app = pagerApp(conn, online: false);
- addTearDown(app.dispose);
-
- await app.preattachAgent('m', 'b', cols: 46, rows: 38);
-
- expect(app.paneOfAgent('m', 'b'), isNull);
- expect(conn.opens, isEmpty);
- });
- });
-
- group('the pager', () {
- Future<(AppNotifier, PagerConn)> pumpPager(WidgetTester tester) async {
- final conn = PagerConn();
- final app = pagerApp(conn);
- addTearDown(app.dispose);
- final list = pagerList(app);
- await liveAgent(app, 'b');
- await tester.pumpWidget(
- MaterialApp(
- home: AgentSwipeHost(
- notifier: app,
- machineId: 'm',
- agentId: 'b',
- neighbours: list,
- ),
- ),
- );
- await tester.pump();
- return (app, conn);
- }
-
- testWidgets('opens the agents either side once the one on screen is live', (
- tester,
- ) async {
- final (app, conn) = await pumpPager(tester);
- expect(conn.opens, isEmpty, reason: 'not before the landed page settles');
-
- await tester.pump(AgentNeighbourWarmer.delay);
- await tester.pump();
-
- expect(
- {for (final open in conn.opens) open['agentId']},
- {'a', 'c'},
- reason: 'the pages one swipe left and one swipe right',
- );
- expect(app.stateOf('m')?.activeAgentId, 'a', reason: 'nothing selected');
- });
-
- testWidgets('closes what falls out of the window as the pager moves', (
- tester,
- ) async {
- final (app, _) = await pumpPager(tester);
- await tester.pump(AgentNeighbourWarmer.delay);
- await tester.pump();
- expect(app.paneOfAgent('m', 'a'), isNotNull);
- // c was warmed; its keyframe lands before the swipe reaches it, as in use.
- await goLive(app.paneOfAgent('m', 'c')!.session!);
-
- // One swipe right: from b to c, whose neighbours are b and d.
- await tester.fling(find.byType(PageView), const Offset(-400, 0), 1000);
- await tester.pumpAndSettle();
- await tester.pump(AgentNeighbourWarmer.delay);
- await tester.pump();
-
- expect(app.paneOfAgent('m', 'a'), isNull, reason: 'two swipes away now');
- expect(app.paneOfAgent('m', 'd')?.session, isNotNull);
- });
- });
-}
diff --git a/mobile/test/agent_swipe_keyboard_test.dart b/mobile/test/agent_swipe_keyboard_test.dart
index 3dacae8e0..8a2195991 100644
--- a/mobile/test/agent_swipe_keyboard_test.dart
+++ b/mobile/test/agent_swipe_keyboard_test.dart
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
-import 'package:harness_mobile/phone/agent_neighbour_warmer.dart';
import 'package:harness_mobile/phone/agent_swipe.dart';
import 'package:harness_mobile/state/app_state.dart';
import 'package:xterm/xterm.dart';
@@ -64,13 +63,17 @@ void main() {
tester,
) async {
final app = await pumpPager(tester);
- // The neighbour opens and answers before the swipe reaches it, as in use.
- await tester.pump(AgentNeighbourWarmer.delay);
- await tester.pump();
- await goLive(app.paneOfAgent('m', 'c')!.session!);
await tester.fling(find.byType(PageView), const Offset(-400, 0), 1000);
- await tester.pumpAndSettle();
+ await tester.pump();
+ await tester.pump(const Duration(milliseconds: 500));
+ // The page swiped to opens as it lands, and its machine answers — `pumpAndSettle` cannot be used
+ // while it is still "Attaching…", whose skeleton breathes for ever (see [goLive]).
+ await goLive(app.paneOfAgent('m', 'c')!.session!);
+ // Twice: each of the keyframe's coalescing windows is armed by the frame before it, and the
+ // binding fails a test that leaves one pending.
+ await tester.pump(const Duration(milliseconds: 100));
+ await tester.pump(const Duration(milliseconds: 100));
await raiseKeyboard(tester);
expect(terminalHasFocus(tester), isTrue);