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: restrict terminal attachment and takeover to panes in the focused tab (#288)
0xken151a1d3e0c977c
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dartindex d5416e96b..39de66e43 100644--- a/desktop/lib/state/app_state.dart+++ b/desktop/lib/state/app_state.dart@@ -1033,7 +1033,19 @@ class AppNotifier extends ChangeNotifier {if (attachPending) {for (final machine in machineStates.values) {// ⌘] / a tab clicked: a person is arriving at these tiles.- _attachPendingPanes(machine, retryExisting: false, intent: AttachIntent.person);+ _attachPendingPanes(+ machine,+ retryExisting: false,+ intent: AttachIntent.person,+ );+ }+ // A tile that was only WATCHING while this tab sat behind: arriving is+ // asking for its terminal, and the sweep above cannot do it — a watcher+ // is a live stream, so `_paneNeedsAttach` leaves it alone on purpose.+ for (final pane in panes) {+ if (pane.session?.watching != true) continue;+ if (!_canAttachPane(pane)) continue;+ unawaited(_reattachPane(pane, intent: AttachIntent.person));}}notifyListeners();@@ -10253,12 +10265,18 @@ class AppNotifier extends ChangeNotifier {// or discard its output while the machine is unavailable.if (!retryExisting && pane.session != null) continue;if (!_paneNeedsAttach(pane)) continue;- // A tile the app restored when it opened carries the gesture that opened- // it, once. Everything else attaching here is [AttachIntent.automatic]- // however this sweep was reached.- final paneIntent = pane.claimOnFirstAttach+ // A gesture reaches only the tab it was made in. A person opening the+ // app, switching to a tab or clicking a tile is asking about what is in+ // front of them; the tabs behind it are somebody else's business, and+ // two Macs sitting on two different tabs have to be able to work at the+ // same time. A tile the app restored carries the gesture that opened it+ // (once), but only once it is the tab being looked at — until then it+ // keeps the claim and watches.+ final inFocusedTab = panes.contains(pane);+ final asked = pane.claimOnFirstAttach || intent == AttachIntent.person;+ final paneIntent = asked && inFocusedTab? AttachIntent.person- : intent;+ : AttachIntent.automatic;// Nobody asked for this one, and this machine's CLI cannot open a// terminal without taking it from whoever has it: leave the tile as// intent (`pane_grid.dart` offers to open it) rather than pulling the@@ -10272,9 +10290,10 @@ class AppNotifier extends ChangeNotifier {// that refusal would leave the tile watching the terminal it was opened// to take, once the agent does come ready.if (!_canAttachPane(pane)) continue;- // Spent on the attach it pays for; a retry of that open is already the- // ordinary rule, and the session carries the claim from here.- pane.claimOnFirstAttach = false;+ // Spent on the attach it pays for, and only when it was used: a tile in+ // a tab nobody has opened yet keeps its claim for the switch that brings+ // it forward.+ if (paneIntent == AttachIntent.person) pane.claimOnFirstAttach = false;// Covers a tile that never attached AND one holding a stream the machine// lost. Only the first used to be covered, and the second is why a// reconnect left every tile but one frozen on "restoring terminal…":@@ -10321,9 +10340,16 @@ class AppNotifier extends ChangeNotifier {/// A person acted on this app — clicked a tile, brought the window forward/// — so every stream another client took from it comes back, not only the- /// tile they touched: being at this app is a fact about the app, not about- /// one pane. Every tab, since a tile parked behind another tab is this- /// app's too. Only ever from a user gesture — see+ /// tile they touched: being at this tab is a fact about the tab, not about+ /// one pane.+ ///+ /// THIS tab only. A tile parked behind another tab is not what the person+ /// is looking at, and taking its terminal too is how two Macs sitting on two+ /// different tabs ended up fighting over terminals neither of them had on+ /// screen. Switching to that tab is its own gesture, and brings its tiles+ /// back then.+ ///+ /// Only ever from a user gesture — see/// `_TerminalPanelState._autoTakeControl` for why a session or focus/// callback must never call this.///@@ -10333,7 +10359,7 @@ class AppNotifier extends ChangeNotifier {/// not moved — the tile the person is on stays the one they are on.Future<void> retakeTakenOverPanes() async {final reopening = <Future<void>>[];- for (final pane in allPanes) {+ for (final pane in panes) {final session = pane.session;if (session == null || session.readOnly) continue;// A watcher is the same thing from the other side: this window has thediff --git a/desktop/test/attach_intent_test.dart b/desktop/test/attach_intent_test.dartindex 1325c020b..b5ce0bbd6 100644--- a/desktop/test/attach_intent_test.dart+++ b/desktop/test/attach_intent_test.dart@@ -321,6 +321,18 @@ void main() {return storage;}+ /// Two tabs, `a0` on the one that will be showing and `a1` behind it.+ Future<MemoryStore> savedTwoTabs() async {+ final storage = MemoryStore();+ final store = PaneLayoutStore(storage: storage);+ final front = Swarm(id: 'swarm-1', name: 'Work', nameIsCustom: true)+ ..panes.add(TerminalPane(id: 1, machineId: 'm', agentId: 'a0'));+ final behind = Swarm(id: 'swarm-2', name: 'Build', nameIsCustom: true)+ ..panes.add(TerminalPane(id: 2, machineId: 'm', agentId: 'a1'));+ await store.saveSwarms([front, behind], 'swarm-1');+ return storage;+ }+/// The app, opened on that layout, with its machine ready to answer.Future<AppNotifier> opened({required MemoryStore storage,@@ -437,6 +449,154 @@ void main() {await finish(tester);});+ testWidgets('a tab nobody is looking at is left alone', (tester) async {+ final storage = await savedTwoTabs();+ app.dispose();+ app = await opened(storage: storage);+ await mount(tester);+ await machineAnswers(tester, about: 'a1');++ expect(+ conn.opens+ .where((o) => o['agentId'] == 'a0')+ .single+ .containsKey('takeover'),+ isFalse,+ reason: 'the tab in front took its terminal',+ );+ expect(+ conn.opens.any((o) => o['agentId'] == 'a1'),+ isFalse,+ reason: 'a tile in a tab behind is not opened at all',+ );+ expect(+ app.swarms+ .firstWhere((s) => s.id == 'swarm-2')+ .panes+ .single+ .claimOnFirstAttach,+ isTrue,+ reason: 'it keeps the claim for the switch that brings it forward',+ );+ await finish(tester);+ });++ testWidgets('switching to that tab is its own gesture', (tester) async {+ final storage = await savedTwoTabs();+ app.dispose();+ app = await opened(storage: storage);+ await mount(tester);+ await machineAnswers(tester, about: 'a1');+ conn.opens.clear();++ app.selectSwarm('swarm-2');+ await tester.pump(const Duration(milliseconds: 200));++ final asked = conn.opens.where((o) => o['agentId'] == 'a1');+ expect(asked, hasLength(1), reason: 'the tile asked again');+ expect(+ asked.single.containsKey('takeover'),+ isFalse,+ reason: 'and this time it asked for the terminal itself',+ );+ expect(+ app.swarms+ .firstWhere((s) => s.id == 'swarm-2')+ .panes+ .single+ .claimOnFirstAttach,+ isFalse,+ reason: 'spent on the attach it paid for',+ );+ await finish(tester);+ });++ testWidgets('a tile switched away from reconnects as a watcher', (+ tester,+ ) async {+ final storage = await savedTwoTabs();+ app.dispose();+ app = await opened(storage: storage);+ await mount(tester);+ await machineAnswers(tester, about: 'a1');+ // Open the second tab, then leave it: its tile now HAS a stream, so the+ // sweep keeps reaching it — politely, since nobody is looking at it.+ app.selectSwarm('swarm-2');+ await tester.pump(const Duration(milliseconds: 200));+ final behind = app.swarms+ .firstWhere((s) => s.id == 'swarm-2')+ .panes+ .single;+ await behind.session!.handleFrame('terminal_ready', {+ 'requestId': conn.opens.lastWhere(+ (o) => o['agentId'] == 'a1',+ )['requestId'],+ 'protocolVersion': 3,+ 'streamId': 'stream-a1',+ 'agentId': 'a1',+ });+ app.selectSwarm('swarm-1');+ await tester.pump(const Duration(milliseconds: 200));+ behind.session!.transportLost('Harness reconnected');+ await tester.pump();+ conn.opens.clear();++ await machineAnswers(tester, about: 'a1');++ final asked = conn.opens.where((o) => o['agentId'] == 'a1');+ expect(asked, isNotEmpty, reason: 'the tile did come back');+ expect(+ asked.every((o) => o['takeover'] == false),+ isTrue,+ reason: 'but it watches: the person is on another tab',+ );+ await finish(tester);+ });++ testWidgets('arriving at the tab takes back what it was watching', (+ tester,+ ) async {+ final storage = await savedTwoTabs();+ app.dispose();+ app = await opened(storage: storage);+ await mount(tester);+ await machineAnswers(tester, about: 'a1');+ // Open the second tab and let its tile become a watcher: the terminal is+ // somebody else's, so the daemon answers read-only.+ app.selectSwarm('swarm-2');+ await tester.pump(const Duration(milliseconds: 200));+ final behind = app.swarms+ .firstWhere((s) => s.id == 'swarm-2')+ .panes+ .single;+ await behind.session!.handleFrame('terminal_ready', {+ 'requestId': conn.opens.lastWhere(+ (o) => o['agentId'] == 'a1',+ )['requestId'],+ 'protocolVersion': 3,+ 'streamId': 'stream-a1',+ 'agentId': 'a1',+ 'readOnly': true,+ });+ behind.session!.status = TerminalSessionStatus.controlling;+ expect(behind.session!.watching, isTrue);+ app.selectSwarm('swarm-1');+ await tester.pump(const Duration(milliseconds: 200));+ conn.opens.clear();++ app.selectSwarm('swarm-2');+ await tester.pump(const Duration(milliseconds: 200));++ final asked = conn.opens.where((o) => o['agentId'] == 'a1');+ expect(asked, hasLength(1), reason: 'arriving asked again');+ expect(+ asked.single.containsKey('takeover'),+ isFalse,+ reason: 'and asked for the terminal itself this time',+ );+ await finish(tester);+ });+testWidgets('the claim is spent once, not carried into a reconnect', (tester,) async {diff --git a/desktop/test/retake_taken_over_panes_test.dart b/desktop/test/retake_taken_over_panes_test.dartindex 8341bff09..d40c4728c 100644--- a/desktop/test/retake_taken_over_panes_test.dart+++ b/desktop/test/retake_taken_over_panes_test.dart@@ -76,15 +76,22 @@ void main() {},);- test('a pane behind another tab is this app\'s too', () async {+ // A tab behind this one is not what the person is looking at, and taking its+ // terminal too is how two Macs on two different tabs ended up fighting over+ // terminals neither had on screen. Switching to that tab is its own gesture.+ test('a pane behind another tab is left where it is', () async {app.adoptSessionForTest(session('a0'));app.newSwarm();app.adoptSessionForTest(session('a1'));expect(app.activeSwarm.panes.map((p) => p.agentId), ['a1']);await app.retakeTakenOverPanes();- expect(opens('a0'), ['terminal_open']);- expect(opens('a1'), ['terminal_open']);+ expect(opens('a1'), ['terminal_open'], reason: 'the tab being looked at');+ expect(+ opens('a0'),+ isEmpty,+ reason: 'the tab behind it is somebody else\'s',+ );});test('a pane the daemon would refuse keeps its band', () async {