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.

desktop: a harness on the tab in front of you is being watched, not only the focused one

bitcoinbuilder3fb3869b5797
diff --git a/desktop/lib/screens/swarm_screen.dart b/desktop/lib/screens/swarm_screen.dart
index c5f9d71c5..b43cb0d81 100644
--- a/desktop/lib/screens/swarm_screen.dart
+++ b/desktop/lib/screens/swarm_screen.dart
@@ -245,6 +245,9 @@ class _SwarmScreenState extends State<SwarmScreen>
// Its own notifier, so marking one agent does not rebuild the workspace —
// which means the badge has to ask for its own redraw.
app.agentUnread.addListener(_unreadChanged);
+ // Coming back to the window puts the tab in front of the person again, and
+ // nothing in the app necessarily changes when that happens — so it is told.
+ _lifecycle = AppLifecycleListener(onResume: app.seeWatchedAgents);
app.addListener(_syncToolbarNotices);
_toolbarNotices.addListener(_toolbarNoticesChanged);
_syncToolbarNotices();
@@ -341,6 +344,7 @@ class _SwarmScreenState extends State<SwarmScreen>
app.removeListener(_recordNavigation);
app.removeListener(_observeLearning);
app.agentUnread.removeListener(_unreadChanged);
+ _lifecycle?.dispose();
if (widget.learning == null) _learning.dispose();
FocusManager.instance.removeListener(_restoreEmptyFocus);
FocusManager.instance.removeListener(_syncKeyContext);
@@ -598,6 +602,8 @@ class _SwarmScreenState extends State<SwarmScreen>
setState(() {});
}
+ AppLifecycleListener? _lifecycle;
+
void _unreadChanged() {
if (!mounted) return;
setState(() {});
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dart
index 39de66e43..83cd1071c 100644
--- a/desktop/lib/state/app_state.dart
+++ b/desktop/lib/state/app_state.dart
@@ -1669,6 +1669,10 @@ class AppNotifier extends ChangeNotifier {
// before the colours the panes actually use have moved.
grid.AppTheme.palette.addListener(_announceTerminalThemeEverywhere);
terminalThemeStore.addListener(_announceTerminalThemeEverywhere);
+ // The active tab is set from a dozen places — a tab click, a number key,
+ // closing a tab, a restored layout, a reveal. Listening here catches all of
+ // them, and a clear that finds nothing to clear notifies nobody.
+ addListener(seeWatchedAgents);
}
/// Through the local CLI in a desktop build; straight to the backend, signed, in a viewer.
@@ -6189,46 +6193,54 @@ class AppNotifier extends ChangeNotifier {
notifyListeners();
}
- /// Put one agent's news on screen, named the way the window names it.
- /// The agent the person is looking at RIGHT NOW, or null.
+ /// Every harness the person can see RIGHT NOW: each one with a pane on the tab
+ /// in front of them, while the window itself is in front.
///
- /// Both halves matter. The pane has to be the focused one, and the window has
- /// to be the front one — a pane keeps its focus while the app sits behind a
- /// browser, and treating that as "being watched" would swallow exactly the
- /// news this feature exists for.
+ /// The TAB, not the focused pane. A tab of three harnesses is three terminals
+ /// on screen at once, and the one holding the cursor is not the only one being
+ /// watched — a turn finishing in the pane beside it is visible the moment it
+ /// happens. The harnesses on other tabs are the ones nobody can see, and those
+ /// are what a notification is for.
///
- /// A seam, because the alternative is not testable: reaching it through real
- /// panes means `focusPane`, which announces the focus to the machine, which
- /// dials it — and a test without a live connection hangs rather than fails.
- late ({String machineId, String agentId})? Function() watchedAgent =
- _watchedFromFocus;
+ /// The window has to be in front as well. Every pane keeps its place on the
+ /// tab while the app sits behind a browser, and treating that as "being
+ /// watched" would swallow exactly the news this feature exists for.
+ ///
+ /// A seam, because reaching it through real panes means `focusPane`, which
+ /// announces the focus to the machine, which dials it — and a test with no
+ /// live connection hangs rather than fails.
+ late Iterable<({String machineId, String agentId})> Function()
+ watchedAgents = _visibleOnTab;
/// The real answer, reachable from a test without going through `focusPane`.
@visibleForTesting
- ({String machineId, String agentId})? watchedAgentFromFocusForTest() =>
- _watchedFromFocus();
-
- ({String machineId, String agentId})? _watchedFromFocus() {
- if (lifecycle() != AppLifecycleState.resumed) return null;
- final pane = focusedPane;
- final agentId = pane?.agentId;
- if (pane == null || agentId == null) return null;
- return (machineId: pane.machineId, agentId: agentId);
+ Iterable<({String machineId, String agentId})> visibleOnTabForTest() =>
+ _visibleOnTab();
+
+ Iterable<({String machineId, String agentId})> _visibleOnTab() {
+ if (lifecycle() != AppLifecycleState.resumed) return const [];
+ return [
+ for (final pane in activeSwarm.panes)
+ if (pane.agentId case final agentId?)
+ (machineId: pane.machineId, agentId: agentId),
+ ];
}
- bool _isBeingWatched(String machineId, String agentId) {
- final watched = watchedAgent();
- return watched != null &&
- watched.machineId == machineId &&
- watched.agentId == agentId;
- }
+ bool _isBeingWatched(String machineId, String agentId) => watchedAgents().any(
+ (w) => w.machineId == machineId && w.agentId == agentId,
+ );
- /// Clear whatever the person is currently looking at. Every route into a
- /// harness ends here, so none of them has to remember to.
- @visibleForTesting
- void seeWatchedAgent() {
- final watched = watchedAgent();
- if (watched != null) agentUnread.clear(watched.machineId, watched.agentId);
+ /// Clear the mark of every harness now on screen.
+ ///
+ /// The mark means "something happened on a harness you cannot see". Once it
+ /// is on screen that stops being true, however it got there — a tab switched
+ /// to, a pane opened onto this tab, the window brought back to the front. So
+ /// this runs on every change to the window rather than on each of those
+ /// routes, which are many and would each have to remember.
+ void seeWatchedAgents() {
+ for (final w in watchedAgents()) {
+ agentUnread.clear(w.machineId, w.agentId);
+ }
}
/// Where the app is. Injected so a test can say so without a real lifecycle.
@@ -6308,7 +6320,7 @@ class AppNotifier extends ChangeNotifier {
///
/// Reads the focused pane rather than taking an id, so every route into a
/// harness clears it without each one having to remember to.
- void _seeFocusedAgent() => seeWatchedAgent();
+ void _seeFocusedAgent() => seeWatchedAgents();
String? _eventAgentId(
MachineState machine,
diff --git a/desktop/test/pending_question_test.dart b/desktop/test/pending_question_test.dart
index fec6a90b8..625613a2c 100644
--- a/desktop/test/pending_question_test.dart
+++ b/desktop/test/pending_question_test.dart
@@ -447,7 +447,13 @@ void main() {
// A sound, a banner and a count are three ways of saying "look over here". All three are noise
// about the pane already in front of you.
- group('the watched harness', () {
+ // ── what the person can see ──────────────────────────────────────────────────────────────────
+ //
+ // A tab of three harnesses is three terminals on screen at once. A turn finishing in any of them
+ // is visible the moment it happens, so none of them earns a sound, a banner or a count. The
+ // harnesses on OTHER tabs are the ones nobody can see, and those are what a notification is for.
+
+ group('the visible tab', () {
late List<String> played;
const channel = MethodChannel('harness/swarm_tabs');
@@ -467,13 +473,23 @@ void main() {
.setMockMethodCallHandler(channel, null);
});
- /// [watching] is the agent in front of the person, or null for none.
+ TerminalSession session(String machineId, String agentId) => TerminalSession(
+ machineId: machineId,
+ agentId: agentId,
+ agentName: agentId,
+ engineId: 'codex',
+ send: (_, _) async => true,
+ sendBinary: (_) async => true,
+ );
+
+ /// The scenario, built with REAL tabs: A, B, C on tab X; D, E on tab Y; tab X in front.
///
- /// Injected rather than reached through a real pane: focusing one announces the focus to the
- /// machine, which dials it, and a test with no live connection HANGS rather than failing.
- /// That cost an hour to find, which is why the decision has a seam now.
- AppNotifier app({String? watching}) {
- final made = AppNotifier(
+ /// A stub connection is what lets tabs be built and switched without dialling a machine —
+ /// without one, announcing the new focus hangs the test rather than failing it.
+ ({AppNotifier app, String x, String y}) fiveHarnesses({
+ AppLifecycleState state = AppLifecycleState.resumed,
+ }) {
+ final app = AppNotifier(
config: AppConfig.dev,
authSession: AuthSession(),
alerts: AlertSounds(
@@ -483,144 +499,147 @@ void main() {
agentAlerts: AgentAlerts(
store: ScreenAlertStore(storage: _Memory())..value = true,
),
- );
- made.watchedAgent = () =>
- watching == null ? null : (machineId: 'm1', agentId: watching);
- made.machines = [_machine];
- made.machineStates['m1'] = MachineState(_machine)
+ connectionForTest: (_) => _Silent(),
+ )..lifecycle = () => state;
+ app.machines = [_machine];
+ app.machineStates['m1'] = MachineState(_machine)
..agents = [
- const Agent(id: 'a1', name: 'One', engine: 'codex'),
- const Agent(id: 'a2', name: 'Two', engine: 'codex'),
+ for (final id in ['A', 'B', 'C', 'D', 'E'])
+ Agent(id: id, name: id, engine: 'codex'),
];
- return made;
+ final x = app.activeSwarmId;
+ for (final id in ['A', 'B', 'C']) {
+ app.adoptSessionForTest(session('m1', id));
+ }
+ app.newSwarm(newTabPage: true);
+ final y = app.activeSwarmId;
+ for (final id in ['D', 'E']) {
+ app.adoptSessionForTest(session('m1', id));
+ }
+ app.selectSwarm(x, attachPending: false);
+ return (app: app, x: x, y: y);
}
- Future<void> finish(AppNotifier a, String agentId) =>
- a.handleMachineEventForTest('m1', {
+ Future<void> finish(AppNotifier a, String agentId, {String machineId = 'm1'}) =>
+ a.handleMachineEventForTest(machineId, {
'type': 'turn_ended',
'agentId': agentId,
'payload': {'agentId': agentId},
});
- test('says nothing at all about the pane in front of you', () async {
- final a = app(watching: 'a1');
- addTearDown(a.dispose);
- await finish(a, 'a1');
+ test('harnesses on the tab in front of you finish silently; one on another tab does not', () async {
+ final t = fiveHarnesses();
+ addTearDown(t.app.dispose);
+ expect(t.app.activeSwarmId, t.x);
+
+ await finish(t.app, 'A');
+ await finish(t.app, 'B');
await Future<void>.delayed(Duration.zero);
- expect(a.agentUnread.count, 0, reason: 'nothing to count');
- expect(a.agentAlerts.alerts, isEmpty, reason: 'nothing to show');
- expect(played, isEmpty, reason: 'nothing to hear');
- });
+ // A and B are on screen: nothing to count, show or hear — and that is true of B as much as
+ // of whichever pane holds the cursor.
+ expect(t.app.agentUnread.count, 0);
+ expect(t.app.agentAlerts.alerts, isEmpty);
+ expect(played, isEmpty);
- test('still speaks for a harness you are NOT in', () async {
- final a = app(watching: 'a1');
- addTearDown(a.dispose);
- await finish(a, 'a2');
+ await finish(t.app, 'D');
await Future<void>.delayed(Duration.zero);
- expect(a.agentUnread.kindFor('m1', 'a2'), AlertKind.done);
- expect(played, hasLength(1));
+ // D is on tab Y, which nobody can see.
+ expect(t.app.agentUnread.count, 1);
+ expect(t.app.agentUnread.kindFor('m1', 'D'), AlertKind.done);
+ expect(t.app.agentAlerts.alerts.single.agentId, 'D');
+ expect(played, [AlertKind.done.sound]);
});
- test('a question about the watched harness is silent too', () async {
- final a = app(watching: 'a1');
- addTearDown(a.dispose);
- await a.handleMachineEventForTest('m1', asked());
- await Future<void>.delayed(Duration.zero);
- expect(a.agentUnread.count, 0);
- expect(a.agentAlerts.alerts, isEmpty);
- expect(played, isEmpty);
+ test('a question on the visible tab is silent too; one on another tab is not', () async {
+ final t = fiveHarnesses();
+ addTearDown(t.app.dispose);
+ await t.app.handleMachineEventForTest('m1', asked(agentId: 'C'));
+ expect(t.app.agentUnread.count, 0);
+
+ await t.app.handleMachineEventForTest(
+ 'm1',
+ asked(agentId: 'E', requestId: 'q_e'),
+ );
+ expect(t.app.agentUnread.kindFor('m1', 'E'), AlertKind.needsYou);
});
- test('with nothing watched, everything is announced', () async {
- final a = app();
- addTearDown(a.dispose);
- await finish(a, 'a1');
- await Future<void>.delayed(Duration.zero);
- expect(a.agentUnread.kindFor('m1', 'a1'), AlertKind.done);
+ test('switching to the tab clears the marks of every harness on it', () async {
+ final t = fiveHarnesses();
+ addTearDown(t.app.dispose);
+ await finish(t.app, 'D');
+ await t.app.handleMachineEventForTest(
+ 'm1',
+ asked(agentId: 'E', requestId: 'q_e'),
+ );
+ expect(t.app.agentUnread.count, 2);
+
+ // Tab Y comes to the front. D and E are on screen now, so neither mark is true any more —
+ // with no pane clicked, no row, no banner.
+ t.app.selectSwarm(t.y, attachPending: false);
+ expect(t.app.agentUnread.count, 0);
+ });
+
+ test('switching away does not clear the tab being left', () async {
+ final t = fiveHarnesses();
+ addTearDown(t.app.dispose);
+ await finish(t.app, 'D');
+ // Going to Y and straight back to X: only Y's marks were seen.
+ t.app.selectSwarm(t.y, attachPending: false);
+ t.app.selectSwarm(t.x, attachPending: false);
+ await finish(t.app, 'E');
+ expect(t.app.agentUnread.kindFor('m1', 'E'), AlertKind.done);
+ expect(t.app.agentUnread.kindFor('m1', 'D'), isNull);
});
- test('the same agent id on ANOTHER machine is not the one you are watching', () async {
- // The key is the machine and the agent. Ignoring the machine would silence a harness on a
- // different computer that happens to share an id with the one in front of you.
- final a = app(watching: 'a1');
- addTearDown(a.dispose);
+ test('with the window behind another app, even the front tab is not being watched', () async {
+ // Every pane keeps its place on the tab while the app sits behind a browser, and treating
+ // that as being watched would swallow exactly the news this feature exists for.
+ final t = fiveHarnesses(state: AppLifecycleState.inactive);
+ addTearDown(t.app.dispose);
+ await finish(t.app, 'A');
+ expect(t.app.agentUnread.kindFor('m1', 'A'), AlertKind.done);
+ });
+
+ test('coming back to the window clears the marks on the tab in front of you', () async {
+ var state = AppLifecycleState.inactive;
+ final t = fiveHarnesses();
+ addTearDown(t.app.dispose);
+ t.app.lifecycle = () => state;
+ await finish(t.app, 'A'); // on the front tab, but nobody is looking at the window
+ await finish(t.app, 'D'); // on the other tab
+ expect(t.app.agentUnread.count, 2);
+
+ // The screen's lifecycle listener calls this on resume.
+ state = AppLifecycleState.resumed;
+ t.app.seeWatchedAgents();
+ expect(t.app.agentUnread.kindFor('m1', 'A'), isNull, reason: 'A is on screen again');
+ expect(t.app.agentUnread.kindFor('m1', 'D'), AlertKind.done, reason: 'D still is not');
+ });
+
+ test('the same agent id on another machine is not on this tab', () async {
+ // Visibility is the machine AND the agent. Matching on the id alone would silence a harness
+ // on a different computer that happens to share an id with one on screen.
+ final t = fiveHarnesses();
+ addTearDown(t.app.dispose);
const other = Machine(
machineId: 'm2',
authMode: MachineAuthMode.remote,
name: 'other',
);
- a.machines = [_machine, other];
- a.machineStates['m2'] = MachineState(other)
- ..agents = [const Agent(id: 'a1', name: 'One', engine: 'codex')];
-
- await a.handleMachineEventForTest('m2', {
- 'type': 'turn_ended',
- 'agentId': 'a1',
- 'payload': {'agentId': 'a1'},
- });
- await Future<void>.delayed(Duration.zero);
- expect(a.agentUnread.kindFor('m2', 'a1'), AlertKind.done);
- expect(a.agentUnread.kindFor('m1', 'a1'), isNull);
+ t.app.machines = [_machine, other];
+ t.app.machineStates['m2'] = MachineState(other)
+ ..agents = [const Agent(id: 'A', name: 'A', engine: 'codex')];
+ await finish(t.app, 'A', machineId: 'm2');
+ expect(t.app.agentUnread.kindFor('m2', 'A'), AlertKind.done);
});
- test('a focused pane is NOT watched while the window is behind another app', () async {
- // The default answer — no seam here, because this is the half that decides whether the
- // feature works at all. A pane keeps its focus the whole time the app sits behind a browser,
- // and treating that as "being watched" would swallow exactly the news this exists for.
- //
- // `adoptSessionForTest` focuses a tile WITHOUT announcing it to the machine; `focusPane`
- // announces, which dials, which hangs a test with no live connection.
- for (final (state, watched) in [
- (AppLifecycleState.resumed, true),
- (AppLifecycleState.inactive, false),
- (AppLifecycleState.hidden, false),
- ]) {
- final a = app();
- addTearDown(a.dispose);
- a.lifecycle = () => state;
- a.watchedAgent = a.watchedAgentFromFocusForTest;
- a.adoptSessionForTest(
- TerminalSession(
- machineId: 'm1',
- agentId: 'a1',
- agentName: 'One',
- engineId: 'codex',
- send: (_, _) async => true,
- sendBinary: (_) async => true,
- ),
- );
- expect(
- a.watchedAgent() != null,
- watched,
- reason: 'lifecycle $state',
- );
- }
- });
-
- test('switching to a marked harness clears it', () async {
- var watching = 'a1';
- final a = AppNotifier(
- config: AppConfig.dev,
- authSession: AuthSession(),
- alerts: AlertSounds(
- store: AlertSoundStore(storage: _Memory()),
- channel: channel,
- ),
- agentAlerts: AgentAlerts(store: ScreenAlertStore(storage: _Memory())),
+ test('the visible set is the whole front tab, not the focused pane', () {
+ final t = fiveHarnesses();
+ addTearDown(t.app.dispose);
+ expect(
+ t.app.visibleOnTabForTest().map((w) => w.agentId).toSet(),
+ {'A', 'B', 'C'},
);
- addTearDown(a.dispose);
- a.watchedAgent = () => (machineId: 'm1', agentId: watching);
- a.machines = [_machine];
- a.machineStates['m1'] = MachineState(_machine);
-
- // A finishes while you are in B.
- await finish(a, 'a2');
- expect(a.agentUnread.kindFor('m1', 'a2'), AlertKind.done);
-
- // Going to it is reading it — focus alone, with no row or banner clicked.
- watching = 'a2';
- a.seeWatchedAgent();
- expect(a.agentUnread.count, 0);
});
});
}