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): pager cap counts only the streams that stay

Dev jacobb22da53f2f0e
diff --git a/mobile/lib/phone/agent_pane_prune.dart b/mobile/lib/phone/agent_pane_prune.dart
index 87ebeef4e..c45907b0c 100644
--- a/mobile/lib/phone/agent_pane_prune.dart
+++ b/mobile/lib/phone/agent_pane_prune.dart
@@ -2,6 +2,7 @@ import 'dart:async';
import 'package:harness_mobile/core/last_opened_agent.dart' show AgentRef;
import 'package:harness_mobile/state/app_state.dart';
+import 'package:harness_mobile/state/terminal_pane.dart';
/// Closes the streams of agents a pager is no longer showing.
///
@@ -27,6 +28,31 @@ void releaseAgentPanes(
}
}
+/// How many of [panes] hold an agent stream that is STAYING open: every agent pane but the ones a
+/// pending [AgentPanePruner.keep] for [keeping] is about to close — on [attached], outside
+/// [keeping], and not spared by [heldElsewhere].
+///
+/// ⚠️ **Why the pending prune has to be discounted.** A pager's prefetch fires a quarter second
+/// after landing, before [AgentPanePruner.delay] has closed what the swipe left behind. Counted,
+/// those agents fill places under the pager's cap, the new ring's far side is never attached, and
+/// nothing re-arms it: the next page in the direction of travel comes up "Attaching…".
+int stayingAgentPanes(
+ Iterable<TerminalPane> panes, {
+ required Set<AgentRef> keeping,
+ required Set<AgentRef> attached,
+ required bool Function(AgentRef) heldElsewhere,
+}) {
+ bool closing(AgentRef agent) =>
+ attached.contains(agent) &&
+ !keeping.contains(agent) &&
+ !heldElsewhere(agent);
+ return panes.where((pane) {
+ final agentId = pane.agentId;
+ if (agentId == null) return false;
+ return !closing((machineId: pane.machineId, agentId: agentId));
+ }).length;
+}
+
/// Keeps the phone down to a HANDFUL of open agents — the one on screen, the ones a swipe away, and
/// the ones just left — by closing the rest a beat after each swipe.
///
diff --git a/mobile/lib/phone/agent_swipe.dart b/mobile/lib/phone/agent_swipe.dart
index efa7f6e81..1f4bc44ce 100644
--- a/mobile/lib/phone/agent_swipe.dart
+++ b/mobile/lib/phone/agent_swipe.dart
@@ -595,9 +595,14 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {
}
}
- /// How many agents the phone has streams for right now — every pane on the phone is a pager's.
- int _openPanes() =>
- widget.notifier.panes.where((pane) => pane.agentId != null).length;
+ /// How many agents the phone has streams for that are staying — every pane on the phone is a
+ /// pager's. Not the ones this pager's pending prune is about to close: see [stayingAgentPanes].
+ int _openPanes() => stayingAgentPanes(
+ widget.notifier.panes,
+ keeping: _keepSet(),
+ attached: _attached,
+ heldElsewhere: _heldByAnotherPager,
+ );
}
/// Whether a pager currently mounted is holding [agent] open — on screen, a swipe away, or just
diff --git a/mobile/test/agent_pane_staying_test.dart b/mobile/test/agent_pane_staying_test.dart
new file mode 100644
index 000000000..3885941a9
--- /dev/null
+++ b/mobile/test/agent_pane_staying_test.dart
@@ -0,0 +1,64 @@
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/core/last_opened_agent.dart' show AgentRef;
+import 'package:harness_mobile/phone/agent_pane_prune.dart';
+import 'package:harness_mobile/state/terminal_pane.dart';
+
+/// What a pager's cap counts: the streams that STAY, not the ones its pending prune is about to
+/// close. Counted, those left the ring's far side unattached after a swipe — see
+/// [stayingAgentPanes].
+void main() {
+ AgentRef ref(String agentId) => (machineId: 'm', agentId: agentId);
+
+ List<TerminalPane> panesFor(List<String?> agentIds) => [
+ for (final (i, agentId) in agentIds.indexed)
+ TerminalPane(id: i, machineId: 'm', agentId: agentId),
+ ];
+
+ int count(
+ List<String?> open, {
+ required Set<String> keeping,
+ required Set<String> attached,
+ Set<String> heldElsewhere = const {},
+ }) => stayingAgentPanes(
+ panesFor(open),
+ keeping: {for (final id in keeping) ref(id)},
+ attached: {for (final id in attached) ref(id)},
+ heldElsewhere: (agent) => heldElsewhere.contains(agent.agentId),
+ );
+
+ test('leaves out what the pager attached and no longer keeps', () {
+ // Landed on d: b was attached for the page before and falls out of d's keep-set.
+ expect(
+ count(
+ ['b', 'c', 'd', 'e'],
+ keeping: {'c', 'd', 'e'},
+ attached: {'b', 'c', 'd', 'e'},
+ ),
+ 3,
+ );
+ });
+
+ test('counts a pane this pager never attached — someone else closes it', () {
+ expect(
+ count(['x', 'd'], keeping: {'d'}, attached: {'d'}),
+ 2,
+ reason: 'x belongs to another pager or the desk, and stays open',
+ );
+ });
+
+ test('counts a pane the prune spares for another pager', () {
+ expect(
+ count(
+ ['b', 'd'],
+ keeping: {'d'},
+ attached: {'b', 'd'},
+ heldElsewhere: {'b'},
+ ),
+ 2,
+ );
+ });
+
+ test('never counts a machine tile', () {
+ expect(count([null, 'd'], keeping: {'d'}, attached: {'d'}), 1);
+ });
+}