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
Restore closed work into its existing swarm
deed727360565b5
diff --git a/desktop/lib/shortcuts/app_shortcuts.dart b/desktop/lib/shortcuts/app_shortcuts.dartindex db1ce9fda..009900828 100644--- a/desktop/lib/shortcuts/app_shortcuts.dart+++ b/desktop/lib/shortcuts/app_shortcuts.dart@@ -276,7 +276,7 @@ const List<AppShortcut> kAppShortcuts = [AppShortcut(action: ShortcutAction.closePane,activator: SingleActivator(LogicalKeyboardKey.keyW, meta: true),- label: 'Close the focused pane',+ label: 'Remove the focused agent from this swarm',group: ShortcutGroup.panes,),AppShortcut(diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dartindex d3b8fba5a..3443d0ce6 100644--- a/desktop/lib/state/app_state.dart+++ b/desktop/lib/state/app_state.dart@@ -373,9 +373,11 @@ class AppNotifier extends ChangeNotifier {List.unmodifiable(_closedHistory.reversed);List<ClosedSwarm> get closedSwarms =>List.unmodifiable(_closedHistory.reversed.whereType<ClosedSwarm>());- bool get canReopenClosedSwarm =>- _closedHistory.any((entry) => entry is ClosedSwarm) &&- swarms.length < maxSwarms;+ bool get canReopenClosedSwarm {+ final saved = _closedHistory.whereType<ClosedSwarm>().lastOrNull;+ return saved != null && _canReopenSwarm(saved);+ }+bool get canReopenLastClosed =>_closedHistory.isNotEmpty &&canReopenClosed(_closedHistory.last.historyId);@@ -385,7 +387,7 @@ class AppNotifier extends ChangeNotifier {final entry = _closedHistory.where((entry) => entry.historyId == historyId).firstOrNull;- if (entry is ClosedSwarm) return swarms.length < maxSwarms;+ if (entry is ClosedSwarm) return _canReopenSwarm(entry);if (entry is! ClosedAgent) return false;final target = swarms.where((s) => s.id == entry.swarmId).firstOrNull;return target == null@@ -398,6 +400,21 @@ class AppNotifier extends ChangeNotifier {);}+ bool _canReopenSwarm(ClosedSwarm saved) {+ if (_disposed) return false;+ final target = swarms.where((swarm) => swarm.id == saved.id).firstOrNull;+ if (target == null) return swarms.length < maxSwarms;+ final present = {+ for (final pane in target.panes) (pane.machineId, pane.agentId),+ };+ final missing = {+ for (final pane in saved.panes)+ if (!present.contains((pane.machineId, pane.agentId)))+ (pane.machineId, pane.agentId),+ };+ return target.panes.length + missing.length <= maxPanes;+ }+void _rememberClosed(ClosedWork entry) {_closedHistory.add(entry);if (_closedHistory.length > maxClosedSwarms) _closedHistory.removeAt(0);@@ -543,25 +560,57 @@ class AppNotifier extends ChangeNotifier {}void reopenClosedSwarm({String? historyId}) {- if (_disposed || !canReopenClosedSwarm) return;+ if (_disposed) return;final index = _closedHistory.lastIndexWhere((entry) =>entry is ClosedSwarm &&(historyId == null || entry.historyId == historyId),);if (index < 0) return;- final saved = _closedHistory.removeAt(index) as ClosedSwarm;+ final saved = _closedHistory[index] as ClosedSwarm;+ if (!_canReopenSwarm(saved)) return;+ _closedHistory.removeAt(index);if (swarms.length == 1 && saved.replacesUntouchedWelcome(swarms.single)) {swarms.clear();}final pool = {for (final pane in allPanes) (pane.machineId, pane.agentId): pane,};- var id = saved.id;- while (swarms.any((swarm) => swarm.id == id)) {- id = 'swarm-${_nextSwarmId++}';+ final target = swarms.where((swarm) => swarm.id == saved.id).firstOrNull;+ if (target != null) {+ // Reopening an individual agent may have restored this swarm already.+ // Reunite its missing views without cloning the tab or overwriting edits+ // made since then. Live peers keep their terminal, draft and selection.+ final present = {+ for (final pane in target.panes) (pane.machineId, pane.agentId),+ };+ final previousCount = target.panes.length;+ for (final entry in saved.panes) {+ if (!present.add((entry.machineId, entry.agentId))) continue;+ target.panes.add(+ pool.putIfAbsent(+ (entry.machineId, entry.agentId),+ () => TerminalPane(+ id: _nextPaneId++,+ machineId: entry.machineId,+ agentId: entry.agentId,+ )..composerVisible = entry.composerVisible,+ ),+ );+ }+ if (target.panes.length != previousCount) {+ // An old manual shape for this count describes different membership.+ // Keep current presets/pins and use the normal layout for added views.+ target.paneSizes.remove('${target.panes.length}:manual');+ target.arranged = null;+ target.arrangedKey = null;+ }+ target.focusedPaneId ??= target.panes.firstOrNull?.id;+ _paneFocusRequest++;+ selectSwarm(target.id);+ return;}- final restored = Swarm(id: id, name: saved.name)+ final restored = Swarm(id: saved.id, name: saved.name)..gridColumns = saved.gridColumns..presets.addAll(saved.presets)..paneSizes.addAll(saved.paneSizes);diff --git a/desktop/lib/widgets/pane_actions_menu.dart b/desktop/lib/widgets/pane_actions_menu.dartindex f6d24f1b7..24f4e5236 100644--- a/desktop/lib/widgets/pane_actions_menu.dart+++ b/desktop/lib/widgets/pane_actions_menu.dart@@ -74,10 +74,9 @@ class _PaneActionsMenuState extends State<PaneActionsMenu> {widget.onToggleComposer!,),if (widget.onClose != null)- _action('Close agent', Icons.close, widget.onClose!),- // Last, after a line, and in the danger colour: Close puts the tile- // away and Delete ends the agent. Two verbs one row apart that both- // start with the pane vanishing need more between them than a label.+ _action('Remove from swarm', Icons.close, widget.onClose!),+ // Removing a view keeps the agent running. Separate the destructive+ // agent operation with a divider and the danger colour.if (widget.onDelete != null) ...[const AppMenuDivider(),AppMenuItem(diff --git a/desktop/macos/Runner/SwarmTitlebar.swift b/desktop/macos/Runner/SwarmTitlebar.swiftindex bb7f27ce8..fe07e46a4 100644--- a/desktop/macos/Runner/SwarmTitlebar.swift+++ b/desktop/macos/Runner/SwarmTitlebar.swift@@ -214,7 +214,7 @@ final class SwarmTitlebar: NSObject, NSMenuItemValidation, NSMenuDelegate {add(agent, "Zoom Agent", "", "zoomPane")add(agent, "Pin or Unpin Agent", "", "pinPane")agent.addItem(.separator())- add(agent, "Close Agent", "w", "closePane", [.command, .shift])+ add(agent, "Remove Agent from Swarm", "w", "closePane", [.command, .shift])install(agent, at: 2)rebuildHistoryMenu()diff --git a/desktop/test/swarm_closed_agents_test.dart b/desktop/test/swarm_closed_agents_test.dartindex 03cb46056..a249c935f 100644--- a/desktop/test/swarm_closed_agents_test.dart+++ b/desktop/test/swarm_closed_agents_test.dart@@ -5,13 +5,15 @@ import 'package:flutter/services.dart';import 'package:flutter_test/flutter_test.dart';import 'package:harness/core/models.dart';import 'package:harness/state/app_state.dart';+import 'package:harness/state/pane_preset.dart';import 'package:harness/state/swarm.dart';import 'package:harness/state/swarm_navigation.dart';+import 'package:harness/terminal/terminal_binary.dart';import 'package:harness/terminal/terminal_session.dart';import 'swarm_interactions_test.dart' show chord;import 'swarm_screen_test.dart' show mount, terminal;-import 'swarm_state_test.dart' show createApp;+import 'swarm_state_test.dart' show createApp, MemoryStore;void main() {test(@@ -83,28 +85,173 @@ void main() {},);- test('a closed parent gets a swarm when its agent alone is reopened', () async {- final app = createApp();+ test(+ 'a closed parent gets a swarm when its agent alone is reopened',+ () async {+ final app = createApp();+ addTearDown(app.dispose);+ app.renameSwarm(app.activeSwarmId, 'Workshop');+ final origin = app.activeSwarmId;+ await app.addAgentToSwarm('m', 'a0');+ await app.addAgentToSwarm('m', 'a1');+ await app.closePane(app.panes.first.id);+ final closure = app.closedHistory.single.historyId;+ await app.closeSwarm(origin);+ expect(app.reopenClosed(historyId: closure), isTrue);+ expect(app.activeSwarmId, origin);+ expect(app.activeSwarm.name, 'Workshop');+ expect(app.panes.single.agentId, 'a0');+ expect(app.closedHistory.single, isA<ClosedSwarm>());+ expect(app.reopenClosed(historyId: closure), isFalse);+ final restored = app.activeSwarm;+ // Restoring the remaining group returns to the same workspace.+ expect(app.reopenClosed(), isTrue);+ expect(app.activeSwarm, same(restored));+ expect(app.panes.map((pane) => pane.agentId), ['a0', 'a1']);+ expect(+ app.swarms.where((swarm) => swarm.name == 'Workshop'),+ hasLength(1),+ );+ expect(app.swarms.map((s) => s.id).toSet().length, app.swarms.length);+ },+ );++ test('swarm recovery keeps newer choices and shared live sessions', () async {+ final store = MemoryStore();+ final app = createApp(store: store);addTearDown(app.dispose);- app.renameSwarm(app.activeSwarmId, 'Workshop');+ final input = <TerminalBinaryFrame>[];+ final first = app.adoptSessionForTest(terminal('a0', input));+ final second = app.adoptSessionForTest(terminal('a1', input));final origin = app.activeSwarmId;+ app.renameSwarm(origin, 'Workshop');+ app.newSwarm(name: 'Live work');+ final peer = app.activeSwarm;await app.addAgentToSwarm('m', 'a0');await app.addAgentToSwarm('m', 'a1');- await app.closePane(app.panes.first.id);- final closure = app.closedHistory.single.historyId;+ app.selectSwarm(origin);+ await app.closePane(first.id);+ final agentClosure = app.closedHistory.single.historyId;await app.closeSwarm(origin);- expect(app.reopenClosed(historyId: closure), isTrue);- expect(app.activeSwarmId, origin);- expect(app.activeSwarm.name, 'Workshop');- expect(app.panes.single.agentId, 'a0');- expect(app.closedHistory.single, isA<ClosedSwarm>());- expect(app.reopenClosed(historyId: closure), isFalse);- // Restoring the separately closed group cannot duplicate a Swarm identity.- expect(app.reopenClosed(), isTrue);- expect(app.panes.single.agentId, 'a1');- expect(app.swarms.map((s) => s.id).toSet().length, app.swarms.length);+ final swarmClosure = app.closedHistory.first.historyId;+ expect(app.reopenClosed(historyId: agentClosure), isTrue);+ final restored = app.activeSwarm;+ app.renameSwarm(origin, 'Current work');+ await app.addAgentToSwarm('m', 'a2');+ app.focusPane(first.id);+ app.togglePinPane(first.id);+ app.toggleZoomPane();+ app.toggleComposer(first.id);+ app.setPreset(3, PanePreset.oneOverTwo);+ second.session!.terminal.write('Keep this output');+ final liveSession = second.session;+ final before = app.swarms.toList();++ expect(app.reopenClosed(historyId: swarmClosure), isTrue);+ expect(app.swarms, before);+ expect(app.activeSwarm, same(restored));+ expect(restored.name, 'Current work');+ expect(restored.panes.map((pane) => pane.agentId), ['a0', 'a2', 'a1']);+ expect(restored.panes.last, same(second));+ expect(peer.panes.last.session, same(liveSession));+ expect(+ second.session!.terminal.buffer.getText(),+ contains('Keep this output'),+ );+ expect(app.focusedPaneId, first.id);+ expect(app.zoomedPaneId, first.id);+ expect(app.isPanePinned(first), isTrue);+ expect(first.composerVisible, isTrue);+ expect(app.presetFor(3), PanePreset.oneOverTwo);+ expect(input, isEmpty);+ expect(app.closedHistory, isEmpty);++ await app.flushPaneLayout();+ final reloaded = createApp(store: store);+ addTearDown(reloaded.dispose);+ await reloaded.restorePaneLayoutForTest();+ expect(+ reloaded.swarms.map((swarm) => swarm.id),+ before.map((swarm) => swarm.id),+ );+ expect(reloaded.activeSwarm.name, 'Current work');+ expect(reloaded.panes.map((pane) => pane.agentId), ['a0', 'a2', 'a1']);});+ test(+ 'swarm recovery fits at the tab limit without duplicating agents',+ () async {+ final app = createApp();+ addTearDown(app.dispose);+ await app.addAgentToSwarm('m', 'a0');+ await app.addAgentToSwarm('m', 'a1');+ final origin = app.activeSwarmId;+ await app.closePane(app.panes.first.id);+ final agentClosure = app.closedHistory.single.historyId;+ await app.closeSwarm(origin);+ final swarmClosure = app.closedHistory.first.historyId;+ expect(app.reopenClosed(historyId: agentClosure), isTrue);+ final restored = app.activeSwarm;+ await app.addAgentToSwarm('m', 'a1');+ final existing = restored.panes.toList();+ while (app.swarms.length < AppNotifier.maxSwarms) {+ app.newSwarm();+ }+ expect(app.canReopenClosedSwarm, isTrue);+ expect(app.canReopenClosed(swarmClosure), isTrue);+ app.reopenClosedSwarm();+ expect(app.activeSwarm, same(restored));+ expect(restored.panes, existing);+ expect(app.swarms, hasLength(AppNotifier.maxSwarms));+ expect(app.closedHistory, isEmpty);+ },+ );++ test(+ 'a full partial swarm keeps its recovery until every view fits',+ () async {+ final app = createApp();+ addTearDown(app.dispose);+ await app.addAgentToSwarm('m', 'a0');+ await app.addAgentToSwarm('m', 'a1');+ await app.addAgentToSwarm('m', 'a2');+ final origin = app.activeSwarmId;+ await app.closePane(app.panes.first.id);+ final agentClosure = app.closedHistory.single.historyId;+ await app.closeSwarm(origin);+ final swarmClosure = app.closedHistory.first.historyId;+ expect(app.reopenClosed(historyId: agentClosure), isTrue);+ final restored = app.activeSwarm;+ for (var i = 3; i < AppNotifier.maxPanes + 1; i++) {+ await app.addAgentToSwarm('m', 'a$i');+ }+ final before = restored.panes.toList();+ expect(before, hasLength(AppNotifier.maxPanes - 1));+ expect(app.canReopenClosed(swarmClosure), isFalse);+ expect(app.canReopenClosedSwarm, isFalse);+ app.reopenClosedSwarm(historyId: swarmClosure);+ expect(app.reopenClosed(historyId: swarmClosure), isFalse);+ expect(restored.panes, before);+ expect(app.closedHistory.single.historyId, swarmClosure);++ await app.closePane(restored.panes.last.id);+ expect(app.reopenClosed(historyId: swarmClosure), isTrue);+ expect(app.activeSwarm, same(restored));+ expect(restored.panes, hasLength(AppNotifier.maxPanes));+ expect(+ restored.panes.map((pane) => pane.agentId).toSet(),+ hasLength(AppNotifier.maxPanes),+ );+ expect(+ restored.panes+ .skip(AppNotifier.maxPanes - 2)+ .map((pane) => pane.agentId),+ ['a1', 'a2'],+ );+ expect(app.closedHistory.single, isA<ClosedAgent>());+ },+ );+test('agent recovery can fit at the tab limit when swarm recovery cannot',() async {@@ -229,4 +376,38 @@ void main() {await tester.pumpWidget(const SizedBox());app.dispose();});++ testWidgets('keyboard swarm recovery returns input to its existing view', (+ tester,+ ) async {+ final app = createApp();+ final input = <TerminalBinaryFrame>[];+ final otherInput = <TerminalBinaryFrame>[];+ final first = app.adoptSessionForTest(terminal('a0', input));+ final second = app.adoptSessionForTest(terminal('a1', otherInput));+ final origin = app.activeSwarmId;+ app.newSwarm(name: 'Live work');+ await app.addAgentToSwarm('m', 'a0');+ await app.addAgentToSwarm('m', 'a1');+ app.selectSwarm(origin);+ await app.closePane(first.id);+ final agentClosure = app.closedHistory.single.historyId;+ await app.closeSwarm(origin);+ expect(app.reopenClosed(historyId: agentClosure), isTrue);+ final restored = app.activeSwarm;+ await mount(tester, app);++ await chord(tester, LogicalKeyboardKey.keyT, shift: true);+ await tester.pump();+ expect(app.activeSwarm, same(restored));+ expect(restored.panes, [first, second]);+ expect(app.swarms, hasLength(2));+ expect(app.focusedPaneId, first.id);+ await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft);+ await tester.pump(const Duration(milliseconds: 10));+ expect(input.single.bytes, [27, 91, 68]);+ expect(otherInput, isEmpty);+ await tester.pumpWidget(const SizedBox());+ app.dispose();+ });}diff --git a/desktop/test/terminal_panel_presentation_test.dart b/desktop/test/terminal_panel_presentation_test.dartindex 91e978da2..5e9161baa 100644--- a/desktop/test/terminal_panel_presentation_test.dart+++ b/desktop/test/terminal_panel_presentation_test.dart@@ -37,7 +37,7 @@ void main() {await tester.pump();await tester.tap(find.byTooltip('Actions for ${session.agentName}'));await tester.pump();- await tester.tap(find.text('Close agent'));+ await tester.tap(find.text('Remove from swarm'));await tester.pump();expect(closed, [1]);session.agentName = 'Renamed terminal';diff --git a/docs/harness-agent-lifecycle.md b/docs/harness-agent-lifecycle.mdindex a0c947539..56559e6dc 100644--- a/docs/harness-agent-lifecycle.md+++ b/docs/harness-agent-lifecycle.md@@ -12,7 +12,7 @@ useful separation and add an explicit way to finish work for now.| Action | Meaning || --- | --- |-| Remove from swarm | Remove this view only. The agent and its other swarm memberships keep working. This clarifies today's Close agent label. |+| Remove from swarm | Remove this view only. The agent and its other swarm memberships keep working. This is the existing view action, now labeled consistently in the pane menu, native menu and keyboard help. || Archive agent | Stop its runtime, preserve its conversation reference and launch context, and move it out of the active collection. This affects the agent across swarms, not only one pane. || Resume | Restore the archived conversation in a runtime and add it to the chosen swarm. Reuse the same Harness agent identity. || Delete agent | Keep the separate explicit removal action. Do not treat ordinary closure or archiving as permission to delete project files or provider transcripts. |@@ -34,6 +34,17 @@ useful separation and add an explicit way to finish work for now.policies can come later. Quiet terminal output does not establish that anagent has finished working.+## Existing view recovery++The September 14 continuation clarifies the existing removal action and fixes+partial History recovery. If someone reopens an individual agent before its+original swarm, reopening that swarm restores only its missing views into the+same tab. It preserves the current name, focus, presets, pins and shared live+sessions rather than creating another same-named swarm. Recovery checks available+pane capacity before making any change and keeps the history entry if it cannot+fit. This does not archive, stop, or restart agent runtimes; Archive/Resume above+remains a proposal.+## What implementation must preserveArchive must be durable on the agent's machine, shared with other clients and