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.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
fix(desktop): taking a terminal back needs a gesture aimed at a pane
0xdiego25bd8ffd1a31d7
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dartindex 462cc7785..c2c2ecf9d 100644--- a/desktop/lib/state/app_state.dart+++ b/desktop/lib/state/app_state.dart@@ -939,6 +939,7 @@ class AppNotifier extends ChangeNotifier {}_activeSwarmId = id;railFocused = false;+ _noteNavigation();final pane = focusedPane;selectedMachineId = pane?.machineId;_persistLayout();@@ -1015,6 +1016,7 @@ class AppNotifier extends ChangeNotifier {_activeSwarmId = owner.id;railFocused = false;selectedMachineId = machineId;+ _noteNavigation();_paneFocusRequest++;_persistLayout();_announceAppFocus();@@ -1309,6 +1311,42 @@ class AppNotifier extends ChangeNotifier {/// Explicit navigation must reveal and refocus even an already-selected pane.int get paneFocusRequest => _paneFocusRequest;+ bool _paneFocusByUser = true;++ /// Whether the latest navigation — [paneFocusRequest], a tab switch, a+ /// focus move — was a person's gesture on this app.+ ///+ /// False while it was a device's doing: the dial turning, a notification or+ /// question shown there, the WiFi device asking for an agent. Those move the+ /// view and the keyboard exactly as a click does, and must NOT take a+ /// terminal back from whichever client holds it — `_autoTakeControl` in+ /// `terminal_panel.dart` reads this before retaking. A question re-shown on+ /// the dial used to count as a click: the app took back every pane, the+ /// re-attach redrew the dialog, the daemon announced it as a new question,+ /// the dial beeped and asked again, 1.5s round, until the cable came out+ /// (owner, 2026-09-22).+ bool get paneFocusByUser => _paneFocusByUser;++ bool _navigatingFromDevice = false;++ /// Run [navigate] as the device's move, not a person's — see+ /// [paneFocusByUser]. Covers only the synchronous part: an `async`+ /// navigation's body runs up to its first `await` inside this, which is+ /// where every focus and tab change happens, and the attach it waits on+ /// afterwards is not the device's to be blamed for.+ T _fromDevice<T>(T Function() navigate) {+ _navigatingFromDevice = true;+ try {+ return navigate();+ } finally {+ _navigatingFromDevice = false;+ }+ }++ void _noteNavigation() {+ _paneFocusByUser = !_navigatingFromDevice;+ }+/// An explicit relayout reveals live output even in tiles whose rectangle/// does not change. This is view intent, so it is never persisted.int _paneLayoutRequest = 0;@@ -1765,6 +1803,7 @@ class AppNotifier extends ChangeNotifier {if (moved) _previousPaneId = focusedPaneId;focusedPaneId = paneId;selectedMachineId = focusedPane?.machineId;+ _noteNavigation();if (reveal) _paneFocusRequest++;if (zoomedPaneId != null) zoomedPaneId = paneId;// Announced even when this tile was ALREADY focused.@@ -7849,12 +7888,16 @@ class AppNotifier extends ChangeNotifier {/// unanswered question is re-shown on each reconnect, and a blink in the/// link used to open a row of tabs — on every Mac, once tabs were shared/// (owner, 2026-09-21).+ ///+ /// The device's move, never a person's — see [paneFocusByUser]: bringing+ /// the agent forward takes nothing back from another client, and the tile+ /// a tap opens claims only its own terminal.Future<void> openAgentFromDial(String machineId,String agentId, {bool fromQuestion = false,}) async {- if (revealAgentView(machineId, agentId)) {+ if (_fromDevice(() => revealAgentView(machineId, agentId))) {selectedMachineId = machineId;notifyListeners();return;@@ -7869,8 +7912,10 @@ class AppNotifier extends ChangeNotifier {// Its own tab. newSwarm reuses an unused start page when there is one, and// at the tab limit leaves the current tab selected — the agent then lands// there, with the usual capacity message if that tab is full.- newSwarm();- await addAgentToSwarm(machineId, agentId, swarmId: activeSwarmId);+ await _fromDevice(() {+ newSwarm();+ return addAgentToSwarm(machineId, agentId, swarmId: activeSwarmId);+ });}/// Enable-time fallback: preserve the user's current choice and acknowledge it.@@ -7898,21 +7943,46 @@ class AppNotifier extends ChangeNotifier {late Future<void> selection;_deviceFocusRevision = focusRevision;try {- selection = selectAgent(machineId, agentId);+ selection = focusAgentFromDevice(machineId, agentId);} finally {_deviceFocusRevision = null;}await selection;}- /// The dial turned to an agent. Ordinary selection, the same path a click on the rail takes.+ /// The dial turned to an agent. Focus, the same move a click on the rail makes — minus the retake.////// It used to take a `DeskEdge` and, for an agent with no tile, replace the pane at that end — the/// dial's carousel could walk past the end of the desk onto an unopened agent, and the edge said/// which tile it had walked off. The carousel walks only open panes now, so there is no off-desk/// landing left to place and nothing to replace.- Future<void> selectAgentFromDial(String machineId, String agentId) async {- await selectAgent(machineId, agentId);+ Future<void> selectAgentFromDial(String machineId, String agentId) =>+ focusAgentFromDevice(machineId, agentId);++ /// Bring [agentId]'s tile forward because a device asked — the dial's+ /// carousel, the WiFi device's focus. The tab switches, the tile gets the+ /// keyboard and `app_focus` goes back to the daemon, exactly as+ /// [selectAgent] does; what it does NOT do is reopen a stream another+ /// client holds. [selectAgent] reopens every dead pane it lands on, and a+ /// `takenOver` pane is dead by that measure — so a turn of the dial was a+ /// takeover, and a question re-shown there took the whole desk back from+ /// the other Mac (see [paneFocusByUser]). Here the band stays up with its+ /// button, and only a hand on this app presses it.+ ///+ /// A tile that never attached (its machine was offline when it was+ /// restored) is attached now, as the ordinary reconnect would: nobody else+ /// can be holding a stream this app never opened. No tile at all opens one,+ /// the way a rail click does — that is an open, not a focus, and it claims+ /// only its own terminal.+ Future<void> focusAgentFromDevice(String machineId, String agentId) async {+ final existing = paneOfAgent(machineId, agentId);+ if (existing == null) {+ await _fromDevice(() => addAgentToSwarm(machineId, agentId));+ return;+ }+ _fromDevice(() => revealAgentView(machineId, agentId));+ machineStates[machineId]?.activeAgentId = agentId;+ if (existing.session == null) await _attachSession(existing);}Future<void> selectAgent(String machineId, String agentId) async {@@ -9075,6 +9145,9 @@ class AppNotifier extends ChangeNotifier {if (!swarms.any((s) => s.id == _activeSwarmId)) {_activeSwarmId = swarms.first.id;selectedMachineId = focusedPane?.machineId;+ // Another Mac closed the tab this one was on. The tab that comes forward+ // instead is nobody's arrival here — see [paneFocusByUser].+ _paneFocusByUser = false;}// Streams nobody shows any more.@@ -9555,7 +9628,9 @@ class AppNotifier extends ChangeNotifier {// the tab: the desk changes, `_persistLayout` re-describes it, and the dial's ring and swarm// line follow from that — nothing is answered to the dial directly.final swarmId = payload['swarmId'];- if (swarmId is String && swarmId.isNotEmpty) selectSwarm(swarmId);+ if (swarmId is String && swarmId.isNotEmpty) {+ _fromDevice(() => selectSwarm(swarmId));+ }break;case 'dial_forked':// The dial forked an agent; the daemon already opened the pane on its@@ -9567,10 +9642,12 @@ class AppNotifier extends ChangeNotifier {final targetMachineId = _dialFocusMachine(payload, forkId);if (targetMachineId != null) {unawaited(- placeFork(- targetMachineId,- forkId,- sourceAgentId: forkSource is String ? forkSource : '',+ _fromDevice(+ () => placeFork(+ targetMachineId,+ forkId,+ sourceAgentId: forkSource is String ? forkSource : '',+ ),),);}diff --git a/desktop/lib/widgets/pane_grid.dart b/desktop/lib/widgets/pane_grid.dartindex 0d10b708e..598b71b0f 100644--- a/desktop/lib/widgets/pane_grid.dart+++ b/desktop/lib/widgets/pane_grid.dart@@ -1493,6 +1493,7 @@ class _PaneContent extends StatelessWidget {focusRequest: notifier.isPaneFocused(pane.id)? notifier.paneFocusRequest: 0,+ focusByUser: notifier.paneFocusByUser,visible: visible,compactHeader: swarmMode,composerVisible: pane.composerVisible,diff --git a/desktop/lib/widgets/terminal_panel.dart b/desktop/lib/widgets/terminal_panel.dartindex ff06979e6..05aeaf72f 100644--- a/desktop/lib/widgets/terminal_panel.dart+++ b/desktop/lib/widgets/terminal_panel.dart@@ -85,6 +85,12 @@ class TerminalPanel extends StatefulWidget {final bool showHeader;final int focusRequest;+ /// Whether the navigation that focused or showed this tile was a person's+ /// gesture on this app (`AppNotifier.paneFocusByUser`). A device's — the+ /// dial turning, a question shown there — moves the keyboard here just the+ /// same, but never retakes a terminal another client holds.+ final bool focusByUser;+/// Whether this tile's composer textbox is showing. Only consulted for a remote machine.final bool composerVisible;final bool readOnly;@@ -114,6 +120,7 @@ class TerminalPanel extends StatefulWidget {this.compactHeader = false,this.showHeader = true,this.focusRequest = 0,+ this.focusByUser = true,this.composerVisible = false,this.readOnly = false,this.notice,@@ -213,7 +220,6 @@ class _TerminalPanelState extends State<TerminalPanel>_focusNode.addListener(_handleFocusChange);_composerFocus.addListener(_handleComposerFocusChange);WidgetsBinding.instance.addObserver(this);- _lastLifecycle = WidgetsBinding.instance.lifecycleState;widget.session.attachViewport(this);widget.session.addListener(_onSessionChanged);terminalFontStore.addListener(_onFontChanged);@@ -254,22 +260,20 @@ class _TerminalPanelState extends State<TerminalPanel>@overridevoid didChangeAppLifecycleState(AppLifecycleState state) {_syncCursorBlink();- // The window came back to the front (⌘-Tab, a click on it): that is a- // person returning, and the tile they left focused takes its stream back.- // `inactive → resumed` only — the first `resumed` of a launch is nobody- // coming back, and a restored pane is not taken over at that point anyway.- final returned =- state == AppLifecycleState.resumed &&- _lastLifecycle != null &&- _lastLifecycle != AppLifecycleState.resumed;- _lastLifecycle = state;- if (returned && widget.focused) _autoTakeControl();- }-- /// Seeded from the binding at mount, so a tile opened while the window was- /// in the background still sees the `inactive → resumed` that brings it- /// forward, rather than mistaking it for the launch's first `resumed`.- AppLifecycleState? _lastLifecycle;+ // ⚠️ **The window coming forward is NOT a retake.** It used to be: the tile+ // the person left focused took its stream back on every `inactive →+ // resumed`, on the reading that somebody had returned to the app. But the+ // window comes forward for things nobody did to a pane — ⌘-Tab to read+ // something, a notification, a screen waking, another app handing focus+ // back — and every one of them pulled the terminal off the phone the+ // person was actually typing into. Measured 2026-09-22: a phone holding+ // one agent lost it 13s later to a window that was never touched, over and+ // over.+ //+ // Taking control is a gesture AIMED at a pane now: a click in it, ⏎ on its+ // band, its button, or a focus move (⌘1–9, ⌘]). Coming back to the window+ // and typing costs one ⏎, which is what the band promises anyway.+ }@overridevoid didUpdateWidget(TerminalPanel oldWidget) {@@ -315,9 +319,13 @@ class _TerminalPanelState extends State<TerminalPanel>_afterTerminalMounted();}// Shown again (a tab switched to, a zoom undone) while it is the tile the- // person left focused: they are back on it.- if (widget.visible && !oldWidget.visible && widget.focused) {- _autoTakeControl();+ // person left focused: they are back on it — unless a device switched the+ // tab, which is nobody being back anywhere.+ if (widget.visible &&+ !oldWidget.visible &&+ widget.focused &&+ widget.focusByUser) {+ _autoTakeControlAfterBuild();}if (oldWidget.viewportSize != widget.viewportSize ||oldWidget.layoutRequest != widget.layoutRequest) {@@ -329,7 +337,9 @@ class _TerminalPanelState extends State<TerminalPanel>(!oldWidget.focused || oldWidget.focusRequest != widget.focusRequest)) {_claimFocusAfterFrame();// ⌘1–9, ⌘], an attention jump, a click: the person chose this tile.- _autoTakeControl();+ // The dial choosing it is not that — the keyboard still lands here, the+ // stream stays with whoever has it (see `AppNotifier.paneFocusByUser`).+ if (widget.focusByUser) _autoTakeControlAfterBuild();}// Showing or hiding the box changes how many rows the terminal has. Re-measure so the remote// grid is resized to what is actually on screen.@@ -704,6 +714,14 @@ class _TerminalPanelState extends State<TerminalPanel>}}+ /// [_autoTakeControl] from `didUpdateWidget`, which runs inside the build:+ /// a reopen notifies the session's listeners at once, and a `setState` from+ /// there is the "called during build" error the crash log was full of.+ /// Deferred one frame, the retake lands on a built tree.+ void _autoTakeControlAfterBuild() {+ WidgetsBinding.instance.addPostFrameCallback((_) => _autoTakeControl());+ }+/// A keystroke was typed into a pane that cannot take it. Flash the banner/// and say so for a moment, rather than letting the key vanish in silence.void _nudgeControlBanner() {diff --git a/desktop/test/dial_desk_test.dart b/desktop/test/dial_desk_test.dartindex 8388a0222..c592fe84c 100644--- a/desktop/test/dial_desk_test.dart+++ b/desktop/test/dial_desk_test.dart@@ -15,6 +15,7 @@ import 'package:harness/core/config.dart';import 'package:harness/core/models.dart';import 'package:harness/state/app_state.dart';import 'package:harness/state/terminal_pane.dart';+import 'package:harness/terminal/terminal_session.dart';AppNotifier _notifier() => AppNotifier(config: AppConfig.dev,@@ -338,4 +339,113 @@ void main() {app.dispose();},);++ // ── the device focuses, a person takes ────────────────────────────────────+ //+ // A dial turned onto a pane another client holds, or a question shown there+ // for one, moves the focus and nothing else: the band stays up with its+ // button. It used to reopen — a takeover — and since the reopen redrew the+ // dialog the daemon was watching, the question came back as a new one and the+ // dial asked again, 1.5s round, until the cable came out (owner, 2026-09-22).+ group('a focus from the device never takes a terminal back', () {+ late AppNotifier app;+ final sent = <String, List<String>>{};++ TerminalSession taken(String id) {+ sent[id] = [];+ return TerminalSession(+ machineId: 'm1',+ agentId: id,+ agentName: id,+ engineId: 'claude',+ send: (type, _) async {+ sent[id]!.add(type);+ return true;+ },+ sendBinary: (_) async => true,+ )..status = TerminalSessionStatus.takenOver;+ }++ List<String> opens(String id) =>+ sent[id]!.where((t) => t == 'terminal_open').toList();++ setUp(() {+ sent.clear();+ app = _notifier();+ // Online and able, so a click WOULD reopen — that is the contrast.+ _machine(app, 'm1', ['a1', 'a2', 'a3'])+ ..nodeOnline = true+ ..terminalCapabilityAvailable = true;+ app.adoptSessionForTest(taken('a1'));+ app.adoptSessionForTest(taken('a2'));+ expect(app.focusedPane?.agentId, 'a2');+ });++ tearDown(() => app.dispose());++ test('the dial turning onto a taken pane only focuses it', () async {+ await dialFocus(app, 'a1');+ expect(app.focusedPane?.agentId, 'a1');+ expect(app.paneFocusByUser, isFalse);+ expect(opens('a1'), isEmpty);+ expect(opens('a2'), isEmpty);+ expect(+ app.paneOfAgent('m1', 'a1')!.session!.status,+ TerminalSessionStatus.takenOver,+ reason: 'the band stays; only a hand on this app presses its button',+ );+ });++ test('a question shown on the dial only brings the pane forward', () async {+ await app.handleEventForTest('m1', {+ 'type': 'dial_open',+ 'payload': {'machineId': 'm1', 'agentId': 'a1', 'reason': 'question'},+ });+ expect(app.focusedPane?.agentId, 'a1');+ expect(app.paneFocusByUser, isFalse);+ expect(opens('a1'), isEmpty);+ expect(opens('a2'), isEmpty);+ });++ test(+ 'a question for a pane on another tab switches to it, takes nothing',+ () async {+ final first = app.activeSwarmId;+ app.newSwarm();+ app.adoptSessionForTest(taken('a3'));+ app.selectSwarm(first);+ expect(app.paneFocusByUser, isTrue, reason: 'selectSwarm by hand');++ await app.handleEventForTest('m1', {+ 'type': 'dial_open',+ 'payload': {'machineId': 'm1', 'agentId': 'a3', 'reason': 'question'},+ });+ expect(app.activeSwarmId, isNot(first));+ expect(app.focusedPane?.agentId, 'a3');+ expect(app.paneFocusByUser, isFalse);+ for (final id in ['a1', 'a2', 'a3']) {+ expect(opens(id), isEmpty, reason: '$id stays with its holder');+ }+ },+ );++ test('the dial picking a tab is not a person arriving on it', () async {+ final first = app.activeSwarmId;+ app.newSwarm();+ await app.handleEventForTest('m1', {+ 'type': 'dial_swarm',+ 'payload': {'swarmId': first},+ });+ expect(app.activeSwarmId, first);+ expect(app.paneFocusByUser, isFalse);+ });++ test('a click on the same pane still takes it back', () async {+ // The contrast: the ordinary selection reopens a dead pane, and a taken+ // pane is dead by its measure. That is the person's path and stays.+ await app.selectAgent('m1', 'a1');+ expect(app.paneFocusByUser, isTrue);+ expect(opens('a1'), ['terminal_open']);+ });+ });}diff --git a/desktop/test/terminal_control_banner_test.dart b/desktop/test/terminal_control_banner_test.dartindex 1605f212f..6c24b30df 100644--- a/desktop/test/terminal_control_banner_test.dart+++ b/desktop/test/terminal_control_banner_test.dart@@ -95,6 +95,7 @@ void main() {bool focused = true,bool visible = true,int focusRequest = 0,+ bool focusByUser = true,double width = 900,Widget? beside,}) async {@@ -114,6 +115,7 @@ void main() {focused: focused,visible: visible,focusRequest: focusRequest,+ focusByUser: focusByUser,readOnly: readOnly,notice: notice,composerVisible: composerVisible,@@ -462,7 +464,11 @@ void main() {await finish(tester);});- testWidgets('the window coming back retakes the others too', (tester) async {+ testWidgets('the window coming back leaves the others alone too', (+ tester,+ ) async {+ // Same rule as this tile's own: the window arriving is not a gesture on+ // any pane. A click in this one still brings every other back with it.await pump(tester);takeOverOther();await tester.pump();@@ -470,6 +476,10 @@ void main() {await tester.pump();tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);await tester.pump();+ expect(opensOther(), isEmpty);++ await tester.tap(find.byType(TerminalView));+ await tester.pump();expect(opensOther(), ['terminal_open']);await finish(tester);});@@ -673,53 +683,75 @@ void main() {},);- testWidgets('a tile shown again unfocused stays as it is', (tester) async {- await pump(tester, visible: false, focused: false);+ testWidgets('a focus from the device takes nothing back', (tester) async {+ // The dial turning onto this tile, or a question shown there for it, bumps+ // `focusRequest` exactly as ⌘1 does — with `focusByUser` false. The+ // keyboard still lands here; the stream stays with whoever holds it, and+ // so does every other tile's (owner, 2026-09-22: a question re-shown on+ // the dial took the whole desk back, 1.5s round, until the cable came out).+ await pump(tester);takeOver();+ takeOverOther();await tester.pump();- await pump(tester, visible: true, focused: false);+ await pump(tester, focusRequest: 1, focusByUser: false);expect(opens(), isEmpty);+ expect(opensOther(), isEmpty);++ // Flipping `focused` on from the device is the same non-event.+ await pump(tester, focused: false, focusRequest: 1, focusByUser: false);+ await pump(tester, focused: true, focusRequest: 1, focusByUser: false);+ expect(opens(), isEmpty);++ // A person's ⌘1 after it takes back as ever.+ await pump(tester, focusRequest: 2);+ expect(opens(), ['terminal_open']);+ expect(opensOther(), ['terminal_open']);await finish(tester);});- testWidgets('the window coming back to the front takes control back', (+ testWidgets('the keyboard still lands on a tile the device focused', (tester,) async {+ // Focus-only is not band-only: the band's ⏎ is live at once, and pressing+ // it is the person taking back.await pump(tester);takeOver();await tester.pump();- // The launch's own first `resumed` is nobody coming back.- tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);- await tester.pump();+ await pump(tester, focusRequest: 1, focusByUser: false);expect(opens(), isEmpty);- tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);- await tester.pump();- tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);+ await tester.sendKeyEvent(LogicalKeyboardKey.enter);await tester.pump();expect(opens(), ['terminal_open']);await finish(tester);});- testWidgets(- 'a tile opened while the window was behind still sees it return',- (tester) async {- // Mounted with the window already inactive: the very next `resumed` IS- // the person coming back, not the launch's first one.- tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);- await pump(tester);- takeOver();- await tester.pump();- tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);- await tester.pump();- expect(opens(), ['terminal_open']);- await finish(tester);- },- );-- testWidgets('the window coming back leaves an unfocused tile alone', (+ testWidgets('a tab the device switched to is nobody arriving', (tester,) async {- await pump(tester, focused: false);+ await pump(tester, visible: false);+ takeOver();+ await tester.pump();+ await pump(tester, visible: true, focusByUser: false);+ expect(opens(), isEmpty);+ await finish(tester);+ });++ testWidgets('a tile shown again unfocused stays as it is', (tester) async {+ await pump(tester, visible: false, focused: false);+ takeOver();+ await tester.pump();+ await pump(tester, visible: true, focused: false);+ expect(opens(), isEmpty);+ await finish(tester);+ });++ testWidgets('the window coming forward takes nothing back', (tester) async {+ // It used to: the focused tile retook on every `inactive → resumed`. The+ // window comes forward for things nobody did to a pane — ⌘-Tab, a+ // notification, a screen waking — and each one pulled the terminal off the+ // phone the person was typing into (owner, 2026-09-22). The band stays;+ // ⏎ or a click takes back.+ await pump(tester);takeOver();await tester.pump();tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);@@ -727,6 +759,12 @@ void main() {tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);await tester.pump();expect(opens(), isEmpty);+ expect(hint, findsOneWidget, reason: 'the band is still offering ⏎');++ // And the gesture the band promises still works.+ await tester.sendKeyEvent(LogicalKeyboardKey.enter);+ await tester.pump();+ expect(opens(), ['terminal_open']);await finish(tester);});