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): a Swarm closed while it opens never reads, and a quick reopen keeps its view (#2046)
0xken15acf261cd2f86
diff --git a/desktop/lib/super_tabs/client.dart b/desktop/lib/super_tabs/client.dartindex 852b0d75c..2d6262376 100644--- a/desktop/lib/super_tabs/client.dart+++ b/desktop/lib/super_tabs/client.dart@@ -311,6 +311,10 @@ class SuperTabClient {/// 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>{};++ /// How many times each Swarm was closed here: an open a close overtook+ /// stops before it makes a controller.+ final _closes = <String, int>{};SuperTabController? peek(String id) => _loaded[id];Iterable<SuperTabController> get loadedControllers => _loaded.values;@@ -378,6 +382,7 @@ class SuperTabClient {_controllers.clear();_loaded.clear();_closedSnapshots.clear();+ _closes.clear();if (account != null &&onTabs != null &&discoveryInterval > Duration.zero) {@@ -534,14 +539,21 @@ class SuperTabClient {return SuperTabSnapshot.fromJson(parts.state);}- Future<SuperTabController> open(String id) =>- _controllers.putIfAbsent(id, () {- final generation = _generation;- return _open(id).catchError((Object failure, StackTrace stack) {- if (_generation == generation) _controllers.remove(id);- Error.throwWithStackTrace(failure, stack);- });+ Future<SuperTabController> open(String id) => _controllers.putIfAbsent(+ id,+ () {+ final generation = _generation;+ late final Future<SuperTabController> opening;+ opening = _open(id).catchError((Object failure, StackTrace stack) {+ // An open since a close has its own entry.+ if (_generation == generation && identical(_controllers[id], opening)) {+ _controllers.remove(id);+ }+ Error.throwWithStackTrace(failure, stack);});+ return opening;+ },+ );/// A tab closed here: its controller stops polling, notifying and saving,/// and the next [open] reads the Swarm afresh. What changed since the last@@ -550,25 +562,18 @@ class SuperTabClient {if (_viewSaves.containsKey(id)) _saveView(id);final future = _controllers.remove(id);final loaded = _loaded.remove(id);+ // One that has read nothing yet keeps what the last close kept.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 _) {}),- );+ // Still reading its saved state: that open stops ([_open]).+ if (future != null && loaded == null) _closes[id] = (_closes[id] ?? 0) + 1;}Future<SuperTabController> _open(String id) async {final account = _account;final generation = _generation;+ final closes = _closes[id];if (account == null) {throw const SuperTabFailure('SIGNED_OUT', 'Sign in to use Swarm.');}@@ -577,6 +582,9 @@ class SuperTabClient {if (generation != _generation) {throw const SuperTabFailure('ACCOUNT_CHANGED', 'The account changed.');}+ if (_closes[id] != closes) {+ throw const SuperTabFailure('CLOSED', 'This Swarm was closed.');+ }final saved = raw == null? <String, dynamic>{}: superTabMap(jsonDecode(raw));@@ -624,6 +632,8 @@ class SuperTabClient {onSelection?.call(controller);}if (controller.snapshot case final snapshot?) {+ // Its own state replaces what a close kept.+ _closedSnapshots.remove(id);final change = (lastChangedAt: controller.lastChangedAt,mentionsWaiting: snapshot.mentionsWaiting,@@ -647,7 +657,6 @@ 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_reading_client_test.dart b/desktop/test/super_tab_reading_client_test.dartindex 81aad6c5d..76eebdc61 100644--- a/desktop/test/super_tab_reading_client_test.dart+++ b/desktop/test/super_tab_reading_client_test.dart@@ -1,3 +1,5 @@+import 'dart:async';+import 'package:fake_async/fake_async.dart';import 'package:flutter_test/flutter_test.dart';import 'package:harness/super_tabs/client.dart';@@ -63,15 +65,39 @@ class _Backend {paths.where((p) => Uri.parse(p).path == '/api/super-tabs/list').length;}+/// Holds the next read of a Swarm's saved state until [release]d: an open+/// that has not landed yet.+class _HeldStore extends MemoryStore {+ Completer<void>? _held;+ bool _holdsNext = false;++ void holdNextOpen() {+ _held = Completer<void>();+ _holdsNext = true;+ }++ void release() => _held!.complete();++ @override+ Future<String?> read(String key) async {+ if (_holdsNext && key == 'super_tab.owner.tab') {+ _holdsNext = false;+ await _held!.future;+ }+ return super.read(key);+ }+}+SuperTabClient _client(_Backend backend, {+ MemoryStore? storage,SuperTabReading? reading,void Function(SuperTabMention)? onMention,void Function(SuperTabController)? onSnapshot,}) {final client = SuperTabClient(transport: backend.call,- storage: MemoryStore(),+ storage: storage ?? MemoryStore(),onTabs: (_, _) {},onMention: onMention,onSnapshot: onSnapshot,@@ -292,18 +318,92 @@ void main() {});});- test('a Swarm released while it opens stops once it has opened', () {+ test('a Swarm released while it opens never reads; its open fails', () {fakeAsync((async) {final backend = _Backend();final client = _client(backend, reading: SuperTabReading.desktop)..bind('owner');- client.open('tab');+ Object? failure;+ client.open('tab').then<void>((_) {}, onError: (Object e) => failure = e);client.release('tab');+ async.elapse(const Duration(seconds: 10));+ expect(backend.states('tab'), 0);+ expect(+ failure,+ isA<SuperTabFailure>().having((f) => f.code, 'code', 'CLOSED'),+ );+ expect(client.peek('tab'), isNull);+ client.dispose();+ });+ });++ test('a Swarm closed and opened again before its first open lands polls '+ 'once, with the new view', () {+ fakeAsync((async) {+ final backend = _Backend();+ final storage = _HeldStore()..holdNextOpen();+ final client = _client(+ backend,+ reading: SuperTabReading.desktop,+ storage: storage,+ )..bind('owner');+ client.open('tab').ignore();+ async.flushMicrotasks();+ client.release('tab');+ SuperTabController? reopened;+ client.open('tab').then((c) => reopened = c);async.elapse(const Duration(seconds: 1));+ storage.release();+ async.elapse(const Duration(seconds: 1));+ expect(reopened, isNotNull);+ expect(client.peek('tab'), same(reopened));backend.paths.clear();+ async.elapse(const Duration(seconds: 9));+ expect(backend.states('tab'), 3);+ client.dispose();+ });+ });++ test('a Swarm closed again before its reopen lands still protects its '+ 'Product Manager', () {+ fakeAsync((async) {+ final backend = _Backend();+ final server = backend.servers['tab']!;+ server.state['agents'] = [+ {+ 'id': 'pm',+ 'name': 'Quinn',+ 'machineId': 'm',+ 'harnessId': 'native-pm',+ 'contributorId': 'owner',+ 'configuration': {'engine': 'claude'},+ },+ ];+ (server.state['conversations'] as List)[0]['leadId'] = 'pm';+ final storage = _HeldStore();+ final client = _client(+ backend,+ reading: SuperTabReading.desktop,+ storage: storage,+ )..bind('owner');+ client.open('tab');+ async.elapse(const Duration(seconds: 1));+ client.release('tab');+ expect(+ client.leadProtectionReasonForHarness('m', 'native-pm'),+ isNotNull,+ );+ storage.holdNextOpen();+ client.open('tab').ignore();+ async.flushMicrotasks();+ client.release('tab');+ storage.release();async.elapse(const Duration(seconds: 10));- expect(backend.paths, isEmpty);expect(client.peek('tab'), isNull);+ expect(+ client.leadProtectionReasonForHarness('m', 'native-pm'),+ isNotNull,+ );client.dispose();});});