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: preserve tab layout and focus when agents reconnect
dee652621c78381
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dartindex 251901b7b..d65a46750 100644--- a/desktop/lib/state/app_state.dart+++ b/desktop/lib/state/app_state.dart@@ -4862,11 +4862,14 @@ class AppNotifier extends ChangeNotifier {agent.terminalAvailable &&machine.terminalCapabilityAvailable) {machine.pendingOfflineAgentId = null;- // Only reattach the terminal if the user is still on THIS machine — recovery can finish- // well after the user has moved on to a different machine/agent, and forcing selectAgent- // here would yank their focus back to what they were looking at before, mid-navigation.- // The recovered agent still shows normally in the rail; they can click it themselves.- if (selectedMachineId == machineId) {+ // Loading already reattaches retained panes across every tab. Selecting that agent+ // again would insert it into the current tab and steal focus from the user's work.+ // Only fulfill a pending selection when it has no pane yet and the user is still on+ // this machine; a reconnect must preserve the layout and selection they left open.+ final hasPane = allPanes.any(+ (pane) => pane.machineId == machineId && pane.agentId == agentId,+ );+ if (!hasPane && selectedMachineId == machineId) {await selectAgent(machineId, agentId);} else {notifyListeners();diff --git a/desktop/test/swarm_reconnect_test.dart b/desktop/test/swarm_reconnect_test.dartnew file mode 100644index 000000000..06fa164d1--- /dev/null+++ b/desktop/test/swarm_reconnect_test.dart@@ -0,0 +1,112 @@+import 'package:flutter_test/flutter_test.dart';+import 'package:harness/terminal/terminal_session.dart';+import 'package:harness/ws/ws_conn.dart';++import 'swarm_state_test.dart' show createApp;++class _ReconnectConnection extends WsConn {+ _ReconnectConnection()+ : super(+ wsBaseUrl: '',+ autonomousEnv: 'test',+ machineId: 'm',+ accessTokenProvider: (_, _) async => '',+ onAuthFailure: (_) {},+ onEvent: (_) {},+ onStatus: (_) {},+ );+ final opened = <String>[];+ @override+ Future<void> waitUntilReady({required Duration timeout}) async {}+ @override+ Future<Map<String, dynamic>> request(+ String type, {+ Map<String, dynamic> payload = const {},+ Duration timeout = const Duration(seconds: 20),+ }) async => type == 'agents_list'+ ? {+ 'agents': [+ for (final id in ['a0', 'a1'])+ {+ 'id': id,+ 'name': id,+ 'engine': 'codex',+ 'terminal': {+ 'runtimes': [+ {'backend': 'tmux', 'paneId': id == 'a0' ? '%1' : '%2'},+ ],+ },+ },+ ],+ }+ : {'protocolVersion': 3, 'backend': 'tmux', 'available': true};+ @override+ Future<bool> sendTerminalFrame(+ String type,+ Map<String, dynamic> payload,+ ) async {+ if (type == 'terminal_open') opened.add(payload['agentId'] as String);+ return true;+ }+}++void main() {+ testWidgets(+ 'reconnection restores each tab without inserting or focusing another agent',+ (tester) async {+ final connection = _ReconnectConnection();+ final app = createApp(connectionForTest: (_) => connection);+ addTearDown(app.dispose);+ final machine = app.machineStates['m']!..nodeOnline = true;+ TerminalSession terminal(String id) => TerminalSession(+ machineId: 'm',+ agentId: id,+ agentName: id,+ engineId: 'codex',+ send: connection.sendTerminalFrame,+ sendBinary: (_) async => true,+ )..status = TerminalSessionStatus.controlling;+ app.adoptSessionForTest(terminal('a0'));+ final first = app.activeSwarm;+ app.newSwarm(name: 'Second');+ final secondPane = app.adoptSessionForTest(terminal('a1'));+ final second = app.activeSwarm;+ app.selectedMachineId = 'm';+ final before = [+ for (final tab in app.swarms) [for (final pane in tab.panes) pane.id],+ ];++ await app.handleEventForTest('m', {+ 'type': 'node_status',+ 'payload': {'online': false},+ });+ expect(machine.pendingOfflineAgentId, 'a0');+ expect(+ app.allPanes.every(+ (pane) => pane.session?.status == TerminalSessionStatus.error,+ ),+ isTrue,+ );+ await app.handleEventForTest('m', {+ 'type': 'node_status',+ 'payload': {'online': true},+ });+ for (var i = 0; i < 30; i++) {+ await tester.pump(const Duration(milliseconds: 100));+ }++ expect(machine.pendingOfflineAgentId, isNull);+ expect(connection.opened.toSet(), {'a0', 'a1'});+ expect([+ for (final tab in app.swarms) [for (final pane in tab.panes) pane.id],+ ], before);+ expect(first.panes.single.agentId, 'a0');+ expect(second.panes.single, same(secondPane));+ expect(app.activeSwarm, same(second));+ expect(app.focusedPane, same(secondPane));+ for (final pane in app.allPanes) {+ await pane.session?.close();+ }+ },+ );+}