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.

fix(desktop): a closed Swarm tab stops polling, notifying and rebuilding (#2037)

0xken1587157f1b28fa
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dart
index 1fb391dfe..ccc296b7f 100644
--- a/desktop/lib/state/app_state.dart
+++ b/desktop/lib/state/app_state.dart
@@ -3613,6 +3613,7 @@ class AppNotifier extends ChangeNotifier {
final removed = swarms.removeAt(index);
if (removed.superTabId case final tabId?) {
_rememberSuperTabVisibility(tabId, false);
+ superTabs.release(tabId);
}
Swarm? replacement;
if (swarms.isEmpty) {
diff --git a/desktop/lib/super_tabs/client.dart b/desktop/lib/super_tabs/client.dart
index 61f09005b..062a81841 100644
--- a/desktop/lib/super_tabs/client.dart
+++ b/desktop/lib/super_tabs/client.dart
@@ -233,14 +233,19 @@ class SuperTabClient {
Object? discoveryError;
final _controllers = <String, Future<SuperTabController>>{};
final _loaded = <String, SuperTabController>{};
+
+ /// The last state of each Swarm closed here ([release]): a closed view
+ /// still protects its Product Managers, without reading anything more.
+ final _closedSnapshots = <String, SuperTabSnapshot>{};
SuperTabController? peek(String id) => _loaded[id];
Iterable<SuperTabController> get loadedControllers => _loaded.values;
/// Native close actions name the machine's harness id, not the Swarm agent id.
String? leadProtectionReasonForHarness(String machineId, String harnessId) {
- for (final controller in loadedControllers) {
- final snapshot = controller.snapshot;
- if (snapshot == null) continue;
+ for (final snapshot in [
+ for (final controller in loadedControllers) ?controller.snapshot,
+ ..._closedSnapshots.values,
+ ]) {
for (final agent in snapshot.agents) {
if (agent.machineId != machineId || agent.harnessId != harnessId) {
continue;
@@ -294,6 +299,7 @@ class SuperTabClient {
}
_controllers.clear();
_loaded.clear();
+ _closedSnapshots.clear();
if (account != null &&
onTabs != null &&
discoveryInterval > Duration.zero) {
@@ -458,6 +464,29 @@ class SuperTabClient {
});
});
+ /// A tab closed here: its controller stops polling, notifying and saving,
+ /// and the next [open] reads the Swarm afresh. What changed since the last
+ /// view write is kept now, as when a tab goes away.
+ void release(String id) {
+ if (_viewSaves.containsKey(id)) _saveView(id);
+ final future = _controllers.remove(id);
+ final loaded = _loaded.remove(id);
+ if (loaded?.snapshot case final snapshot?) _closedSnapshots[id] = snapshot;
+ loaded?.dispose();
+ if (loaded != null) unawaited(loaded.flush().catchError((Object _) {}));
+ if (future == null || loaded != null) return;
+ // Still opening: it is stopped once it lands. An open since has its own.
+ unawaited(
+ future
+ .then((c) async {
+ if (identical(_loaded[id], c)) _loaded.remove(id);
+ c.dispose();
+ await c.flush();
+ })
+ .catchError((Object _) {}),
+ );
+ }
+
Future<SuperTabController> _open(String id) async {
final account = _account;
final generation = _generation;
@@ -533,6 +562,7 @@ class SuperTabClient {
SuperTabDecisions.of(controller).addListener(changed);
}
_tagCounts.remove(id);
+ _closedSnapshots.remove(id);
_loaded[id] = controller;
void drawSaved(Map<String, dynamic>? view) {
if (view != null && generation == _generation) {
diff --git a/desktop/test/super_tab_dial_test.dart b/desktop/test/super_tab_dial_test.dart
index 803ecfe21..f4368acc5 100644
--- a/desktop/test/super_tab_dial_test.dart
+++ b/desktop/test/super_tab_dial_test.dart
@@ -122,7 +122,7 @@ void main() {
});
test('a pick for a closed Swarm opens it first', () async {
- final (f, c) = await open();
+ final (f, closed) = await open();
f.app.newSwarm(newTabPage: true);
await f.app.closeSwarm(
f.app.swarms.firstWhere((s) => s.superTabId == tabId).id,
@@ -132,6 +132,9 @@ void main() {
await settle();
final tab = f.app.swarms.singleWhere((s) => s.superTabId == tabId);
expect(f.app.activeSwarmId, tab.id);
+ // Closing let go of the Swarm: it opened again with a fresh read.
+ final c = f.app.superTabs.peek(tabId)!;
+ expect(c, isNot(same(closed)));
expect(c.selectedId, 'design-lead');
// Opening the Swarm announces too, with the tab's old project; each of
// those is the pick's, up to the one that names the picked project.
diff --git a/desktop/test/super_tab_discovery_test.dart b/desktop/test/super_tab_discovery_test.dart
index 003baa124..a148bafc9 100644
--- a/desktop/test/super_tab_discovery_test.dart
+++ b/desktop/test/super_tab_discovery_test.dart
@@ -22,6 +22,7 @@ class _Catalog {
{'id': '0123456789abcdef01234567', 'name': 'Pilot'},
];
final paths = <String>[];
+ final reads = <String>[];
final server = ConversationServer();
String? fail;
Completer<void>? hold;
@@ -32,6 +33,7 @@ class _Catalog {
Map<String, dynamic>? body,
}) async {
if (Uri.parse(path).path != '/api/super-tabs/list') {
+ reads.add(path);
return server.request(method, path, body: body);
}
expectSync(method, 'GET');
@@ -443,6 +445,43 @@ void main() {
await tester.pump();
});
+ testWidgets(
+ 'a closed Swarm reads and rebuilds nothing; Reopen reads it again',
+ (tester) async {
+ final catalog = _Catalog();
+ catalog.server.state['id'] = catalog.tabs.single['id'];
+ final app = _computer(catalog, MemoryStore());
+ await mount(tester, app);
+ await app.superTabs.refreshTabs();
+ await tester.pump();
+ final tab = app.swarms.singleWhere((s) => s.isSuperTab);
+ await tester.tap(find.byKey(ValueKey('tab-label:${tab.id}')));
+ await tester.pumpAndSettle();
+ expect(catalog.reads, isNotEmpty);
+ await _close(app, tab);
+ await tester.pump(const Duration(seconds: 1));
+ var rebuilds = 0;
+ void rebuilt() => rebuilds++;
+ app.addListener(rebuilt);
+ addTearDown(() => app.removeListener(rebuilt));
+ catalog.server.append('Said after the tab closed');
+ catalog.reads.clear();
+ await tester.pump(const Duration(seconds: 10));
+ expect(catalog.reads, isEmpty);
+ expect(rebuilds, 0);
+ await tester.tap(find.text('Reopen'));
+ await tester.pumpAndSettle();
+ expect(catalog.reads, isNotEmpty);
+ expect(
+ find.text('Said after the tab closed', findRichText: true),
+ findsOneWidget,
+ );
+ await tester.pumpWidget(const SizedBox.shrink());
+ app.superTabs.dispose();
+ await tester.pump();
+ },
+ );
+
testWidgets('the notice goes after about eight seconds', (tester) async {
final app = _computer(_Catalog(), MemoryStore());
await mount(tester, app);
diff --git a/desktop/test/super_tab_reading_client_test.dart b/desktop/test/super_tab_reading_client_test.dart
index e6fa8f54d..e6dbed03e 100644
--- a/desktop/test/super_tab_reading_client_test.dart
+++ b/desktop/test/super_tab_reading_client_test.dart
@@ -67,12 +67,14 @@ SuperTabClient _client(
_Backend backend, {
SuperTabReading? reading,
void Function(SuperTabMention)? onMention,
+ void Function(SuperTabController)? onSnapshot,
}) {
final client = SuperTabClient(
transport: backend.call,
storage: MemoryStore(),
onTabs: (_, _) {},
onMention: onMention,
+ onSnapshot: onSnapshot,
discoveryInterval: const Duration(seconds: 15),
);
if (reading != null) client.reading = reading;
@@ -255,4 +257,53 @@ void main() {
client.dispose();
});
});
+
+ test('Desktop: a released Swarm reads and rebuilds nothing; opening it '
+ 'again reads afresh', () {
+ fakeAsync((async) {
+ final backend = _Backend();
+ var snapshots = 0;
+ final client = _client(
+ backend,
+ reading: SuperTabReading.desktop,
+ onSnapshot: (_) => snapshots++,
+ )..bind('owner');
+ SuperTabController? first;
+ client.open('tab').then((c) => first = c);
+ async.elapse(const Duration(seconds: 1));
+ expect(snapshots, greaterThan(0));
+ client.release('tab');
+ async.flushMicrotasks();
+ backend.servers['tab']!.append('Said after the tab closed');
+ backend.paths.clear();
+ snapshots = 0;
+ async.elapse(const Duration(seconds: 10));
+ expect(backend.paths, isEmpty);
+ expect(snapshots, 0);
+ expect(client.peek('tab'), isNull);
+ SuperTabController? second;
+ client.open('tab').then((c) => second = c);
+ async.elapse(const Duration(seconds: 1));
+ expect(second, isNot(same(first)));
+ expect(backend.states('tab'), greaterThan(0));
+ expect(snapshots, greaterThan(0));
+ client.dispose();
+ });
+ });
+
+ test('a Swarm released while it opens stops once it has opened', () {
+ fakeAsync((async) {
+ final backend = _Backend();
+ final client = _client(backend, reading: SuperTabReading.desktop)
+ ..bind('owner');
+ client.open('tab');
+ client.release('tab');
+ async.elapse(const Duration(seconds: 1));
+ backend.paths.clear();
+ async.elapse(const Duration(seconds: 10));
+ expect(backend.paths, isEmpty);
+ expect(client.peek('tab'), isNull);
+ client.dispose();
+ });
+ });
}