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: say nothing about the harness you are looking at, and clear its mark when you switch to it

bitcoinbuilderd4d31447a463
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dart
index 1f362f9f5..89379b6cf 100644
--- a/desktop/lib/state/app_state.dart
+++ b/desktop/lib/state/app_state.dart
@@ -7,7 +7,8 @@ import 'package:dio/dio.dart';
import 'dart:ui' show Color;
import 'package:flutter/foundation.dart';
-import 'package:flutter/widgets.dart' show BuildContext, StringCharacters;
+import 'package:flutter/widgets.dart'
+ show AppLifecycleState, BuildContext, StringCharacters, WidgetsBinding;
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../analytics/analytics.dart';
@@ -1052,6 +1053,7 @@ class AppNotifier extends ChangeNotifier {
if (owner.focusedPaneId != pane.id) {
owner.previousPaneId = owner.focusedPaneId;
owner.focusedPaneId = pane.id;
+ _seeFocusedAgent();
}
if (owner.zoomedPaneId != null) owner.zoomedPaneId = pane.id;
_activeSwarmId = owner.id;
@@ -1869,6 +1871,11 @@ class AppNotifier extends ChangeNotifier {
if (moved) _previousPaneId = focusedPaneId;
focusedPaneId = paneId;
selectedMachineId = focusedPane?.machineId;
+ // Switching TO a harness is how its news gets read. Not only the routes
+ // that go through a banner or the Harnesses list: clicking the tile, the
+ // rail, ⌘-number, the dial — all of them arrive here, and all of them mean
+ // the same thing to somebody who was told this one wanted them.
+ _seeFocusedAgent();
_noteNavigation();
if (reveal) _paneFocusRequest++;
if (zoomedPaneId != null) zoomedPaneId = paneId;
@@ -6069,11 +6076,61 @@ class AppNotifier extends ChangeNotifier {
}
/// 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.
+ ///
+ /// 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.
+ ///
+ /// 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 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);
+ }
+
+ bool _isBeingWatched(String machineId, String agentId) {
+ final watched = watchedAgent();
+ return watched != null &&
+ watched.machineId == machineId &&
+ watched.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);
+ }
+
+ /// Where the app is. Injected so a test can say so without a real lifecycle.
+ AppLifecycleState? Function() lifecycle =
+ () => WidgetsBinding.instance.lifecycleState;
+
void _raiseAlert(MachineState machine, String agentId, AlertKind kind) {
+ // Nothing at all for the agent on screen in front of you. A sound, a banner
+ // and a count are three ways of saying "look over here", and all three are
+ // noise about the pane you are already in.
+ if (_isBeingWatched(machine.machine.machineId, agentId)) return;
final agent = machine.agents.where((a) => a.id == agentId).firstOrNull;
// Before the banner and outside its switch: the mark is what the window can
// still say when somebody has turned the interrupting halves off.
agentUnread.mark(machine.machine.machineId, agentId, kind);
+ alerts.play(kind);
agentAlerts.post(
AgentAlert(
machineId: machine.machine.machineId,
@@ -6118,6 +6175,12 @@ class AppNotifier extends ChangeNotifier {
void markAgentSeen(String machineId, String agentId) =>
agentUnread.clear(machineId, agentId);
+ /// Whatever tile is now in front of the person has been seen.
+ ///
+ /// 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();
+
String? _eventAgentId(
MachineState machine,
Map<String, dynamic> event,
@@ -10446,10 +10509,7 @@ class AppNotifier extends ChangeNotifier {
// Only a NEW question earns a sound. The daemon re-announces every open one after a
// reconnect and when attaching to a turn that was already mid-dialog, and a window
// that beeped at those would sound an alarm every time the network hiccuped.
- if (!repeat) {
- alerts.play(AlertKind.needsYou);
- _raiseAlert(machine, agentId, AlertKind.needsYou);
- }
+ if (!repeat) _raiseAlert(machine, agentId, AlertKind.needsYou);
}
}
break;
@@ -10502,9 +10562,6 @@ class AppNotifier extends ChangeNotifier {
case 'turn_ended':
final agentId = _eventAgentId(machine, event, payload);
if (agentId != null) {
- // The work someone was waiting on is on screen. Raised whether or not the pane is in
- // front: the whole point is the agent that finished while you were looking elsewhere.
- alerts.play(AlertKind.done);
_raiseAlert(machine, agentId, AlertKind.done);
_cancelTurnActivity(machine.machine.machineId, agentId);
} else {
diff --git a/desktop/test/pending_question_test.dart b/desktop/test/pending_question_test.dart
index 2592a5b0b..fec6a90b8 100644
--- a/desktop/test/pending_question_test.dart
+++ b/desktop/test/pending_question_test.dart
@@ -3,12 +3,16 @@
// what decides whether a row can lie: a question that outlives its dialog, a
// wait clock that resets itself, a stale close wiping a live question.
import 'package:flutter/services.dart';
+import 'package:flutter/widgets.dart' show AppLifecycleState;
import 'package:flutter_test/flutter_test.dart';
import 'package:harness/auth/auth_session.dart';
import 'package:harness/core/config.dart';
import 'package:harness/core/models.dart';
import 'package:harness/state/app_state.dart';
+import 'package:harness/ws/ws_conn.dart';
+import 'package:harness/state/terminal_pane.dart';
+import 'package:harness/terminal/terminal_session.dart';
import 'package:harness/core/local_key_value_store.dart';
import 'package:harness/notify/agent_alerts.dart';
import 'package:harness/notify/alert_sounds.dart';
@@ -28,6 +32,34 @@ class _Memory implements LocalKeyValueStore {
Future<void> delete(String key) async => values.remove(key);
}
+/// A connection that answers nothing and dials nowhere.
+///
+/// `focusPane` announces the new focus to the machine, and without this the notifier builds a real
+/// `WsConn` that never connects — the test then hangs rather than failing, which is how this was
+/// found.
+class _Silent extends WsConn {
+ _Silent()
+ : super(
+ wsBaseUrl: 'ws://fixture.invalid',
+ autonomousEnv: 'test',
+ machineId: 'm1',
+ accessTokenProvider: (_, _) async => '',
+ onAuthFailure: (_) {},
+ onEvent: (_) {},
+ onStatus: (_) {},
+ );
+
+ @override
+ Future<bool> sendTerminalFrame(String type, Map<String, dynamic> payload) async => true;
+
+ @override
+ Future<Map<String, dynamic>> request(
+ String type, {
+ Map<String, dynamic> payload = const {},
+ Duration timeout = const Duration(seconds: 20),
+ }) async => const {};
+}
+
const _machine = Machine(
machineId: 'm1',
authMode: MachineAuthMode.remote,
@@ -409,4 +441,186 @@ void main() {
expect(played, isEmpty);
});
});
+
+ // ── the harness you are already looking at ────────────────────────────────────────────────────
+ //
+ // 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', () {
+ late List<String> played;
+ const channel = MethodChannel('harness/swarm_tabs');
+
+ setUp(() {
+ played = [];
+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
+ .setMockMethodCallHandler(channel, (call) async {
+ if (call.method == 'playAlert') {
+ played.add((call.arguments as Map)['sound'] as String);
+ }
+ return null;
+ });
+ });
+
+ tearDown(() {
+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
+ .setMockMethodCallHandler(channel, null);
+ });
+
+ /// [watching] is the agent in front of the person, or null for none.
+ ///
+ /// 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(
+ config: AppConfig.dev,
+ authSession: AuthSession(),
+ alerts: AlertSounds(
+ store: AlertSoundStore(storage: _Memory())..value = true,
+ channel: channel,
+ ),
+ 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)
+ ..agents = [
+ const Agent(id: 'a1', name: 'One', engine: 'codex'),
+ const Agent(id: 'a2', name: 'Two', engine: 'codex'),
+ ];
+ return made;
+ }
+
+ Future<void> finish(AppNotifier a, String agentId) =>
+ a.handleMachineEventForTest('m1', {
+ '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');
+ 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');
+ });
+
+ test('still speaks for a harness you are NOT in', () async {
+ final a = app(watching: 'a1');
+ addTearDown(a.dispose);
+ await finish(a, 'a2');
+ await Future<void>.delayed(Duration.zero);
+ expect(a.agentUnread.kindFor('m1', 'a2'), AlertKind.done);
+ expect(played, hasLength(1));
+ });
+
+ 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('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('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);
+ 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);
+ });
+
+ 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())),
+ );
+ 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);
+ });
+ });
}