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.

Preserve regular tabs while SuperTab discovery races restoration (#1591)

Deea1c34427755f
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dart
index cc3088588..551c8c001 100644
--- a/desktop/lib/state/app_state.dart
+++ b/desktop/lib/state/app_state.dart
@@ -1427,6 +1427,9 @@ class AppNotifier extends ChangeNotifier {
CurrentUserProfile? get currentUser => _currentUser;
set currentUser(CurrentUserProfile? profile) {
if (_currentUser?.id != profile?.id) {
+ // Initial identity loading may follow a restored Swarm; leaving a known
+ // account must not reveal the feature to the next one.
+ if (_currentUser != null) _superTabsEntered = false;
_closeAttachmentSessions();
_closeOwnerMemories();
deviceHosts.clear();
@@ -1710,10 +1713,14 @@ class AppNotifier extends ChangeNotifier {
upload: uploadWith(() => api),
discoveryInterval: kUnderTest ? Duration.zero : const Duration(seconds: 15),
onTabs: (tabs, hidden) {
- // The browser can learn its account before the saved layout finishes
- // loading. Discovery must not persist over that workspace during boot.
+ // Discovery can finish while an already signed-in machine is re-seating
+ // its saved desk, too. Publishing a SuperTab then advances the layout
+ // revision and replaces regular tabs before their restore can finish.
if (_disposed ||
status != AppStatus.authenticated ||
+ _paneLayoutRestores != 0 ||
+ _workspaceCleanup != null ||
+ !_superTabsEntered ||
currentUser?.id == null) {
return;
}
@@ -1756,7 +1763,12 @@ class AppNotifier extends ChangeNotifier {
_announceOpenPanesToDial();
},
onSnapshot: (controller) {
- if (_disposed || controller.accountId != currentUser?.id) return;
+ if (_disposed ||
+ _paneLayoutRestores != 0 ||
+ _workspaceCleanup != null ||
+ controller.accountId != currentUser?.id) {
+ return;
+ }
var shown = false;
for (final tab in swarms.where((s) => s.superTabId == controller.tabId)) {
if (tab.name != controller.snapshot!.name || !tab.nameIsCustom) {
@@ -1787,6 +1799,12 @@ class AppNotifier extends ChangeNotifier {
);
Map<String, String> _superTabNames = {};
+ bool _superTabsEntered = false;
+
+ /// Membership alone must not reveal the hidden feature in a regular workspace.
+ /// The hidden shortcut, an explicit open, or a restored Swarm supplies that intent.
+ void enterSuperTabs() => _superTabsEntered = true;
+
final StreamController<String> _superTabClosures =
StreamController<String>.broadcast();
@@ -1877,6 +1895,7 @@ class AppNotifier extends ChangeNotifier {
void bindSuperTab(String swarmId, String tabId, String name) {
final swarm = swarms.where((s) => s.id == swarmId).firstOrNull;
if (swarm == null) return;
+ enterSuperTabs();
// A discovery response can beat the creation response back to this
// computer. Keep the lead form's tab and retain any already-open panes.
for (final duplicate
@@ -1906,6 +1925,7 @@ class AppNotifier extends ChangeNotifier {
/// instead of being left behind. Any history entry for the tab is dropped, so
/// Reopen later cannot add a second view.
void openSuperTab(String tabId, String name, {String? intoSwarmId}) {
+ enterSuperTabs();
_rememberSuperTabVisibility(tabId, true);
_closedHistory.removeWhere(
(entry) => entry is ClosedSwarm && entry.superTabId == tabId,
@@ -1953,6 +1973,7 @@ class AppNotifier extends ChangeNotifier {
void markSuperTabDraft(String swarmId) {
final swarm = swarms.where((s) => s.id == swarmId).firstOrNull;
if (swarm == null) return;
+ enterSuperTabs();
swarm.kind = 'superTab';
_persistLayout();
notifyListeners();
@@ -18454,6 +18475,7 @@ class AppNotifier extends ChangeNotifier {
}
int _layoutRevision = 0;
+ int _paneLayoutRestores = 0;
Future<void> flushPaneLayout() => workspaceEnabled?.call() == false
? Future<void>.value()
@@ -19152,6 +19174,18 @@ class AppNotifier extends ChangeNotifier {
if (workspaceEnabled?.call() == false) return;
final store = _paneLayout;
if (store == null) return;
+ _paneLayoutRestores++;
+ try {
+ await _readPaneLayout(store, claimOnAttach: claimOnAttach);
+ } finally {
+ _paneLayoutRestores--;
+ }
+ }
+
+ Future<void> _readPaneLayout(
+ PaneLayoutStore store, {
+ required bool claimOnAttach,
+ }) async {
// Before any chat is restored: its Finder comes back with its agent.
await finderRoots.load();
final initialSwarm = activeSwarm;
@@ -19335,6 +19369,7 @@ class AppNotifier extends ChangeNotifier {
swarms
..clear()
..addAll(restored);
+ if (restored.any((swarm) => swarm.isSuperTab)) enterSuperTabs();
_activeSwarmId = restored.any((s) => s.id == saved['activeId'])
? saved['activeId'] as String
: restored.first.id;
diff --git a/desktop/test/super_tab_discovery_test.dart b/desktop/test/super_tab_discovery_test.dart
index d71a297d5..a21f36507 100644
--- a/desktop/test/super_tab_discovery_test.dart
+++ b/desktop/test/super_tab_discovery_test.dart
@@ -8,6 +8,7 @@ import 'package:harness/state/app_state.dart';
import 'package:harness/state/pane_layout_store.dart';
import 'package:harness/screens/swarm_screen.dart';
import 'package:harness/state/swarm.dart';
+import 'package:harness/state/terminal_pane.dart';
import 'package:harness/super_tabs/client.dart';
import 'package:harness/ws/ws_conn.dart';
@@ -47,6 +48,22 @@ class _Catalog {
}
}
+class _RestoringStore extends MemoryStore {
+ final readingLayout = Completer<void>();
+ final releaseLayout = Completer<void>();
+ bool holdLayout = false;
+
+ @override
+ Future<String?> read(String key) async {
+ final value = await super.read(key);
+ if (holdLayout && key == 'swarm_layout_v1') {
+ readingLayout.complete();
+ await releaseLayout.future;
+ }
+ return value;
+ }
+}
+
class _Connection extends WsConn {
_Connection(this.catalog)
: super(
@@ -78,7 +95,11 @@ class _Connection extends WsConn {
}
}
-AppNotifier _computer(_Catalog catalog, MemoryStore store) {
+AppNotifier _computer(
+ _Catalog catalog,
+ MemoryStore store, {
+ bool enteredSuperTabs = true,
+}) {
final connection = _Connection(catalog);
final app = createApp(
store: store,
@@ -91,6 +112,7 @@ AppNotifier _computer(_Catalog catalog, MemoryStore store) {
id: 'owner',
);
+ if (enteredSuperTabs) app.enterSuperTabs();
addTearDown(app.dispose);
return app;
}
@@ -107,6 +129,63 @@ Future<void> _close(AppNotifier app, Swarm tab) async {
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
+ test('account membership cannot reveal SuperTabs before deliberate entry', () async {
+ final store = MemoryStore();
+ final app = _computer(_Catalog(), store, enteredSuperTabs: false);
+ app.newSwarm(name: 'Regular work');
+ final regular = app.activeSwarm;
+ await app.superTabs.refreshTabs();
+ expect(app.swarms.where((s) => s.isSuperTab), isEmpty);
+ expect(app.activeSwarm, same(regular));
+ // The hidden shortcut marks a draft. It permits discovery without replacing
+ // or navigating away from the existing regular work.
+ app.newSwarm(newTabPage: true);
+ app.markSuperTabDraft(app.activeSwarmId);
+ final draft = app.activeSwarm;
+ await app.superTabs.refreshTabs();
+ expect(app.swarms, contains(regular));
+ expect(app.activeSwarm, same(draft));
+ expect(app.swarms.where((s) => s.superTabId != null).single.name, 'Pilot');
+ });
+
+ test(
+ 'restoring an already-open SuperTab keeps discovery available',
+ () async {
+ final store = MemoryStore();
+ await PaneLayoutStore(storage: store).saveSwarms([
+ Swarm(id: 'work', name: 'Regular work'),
+ Swarm(id: 'pilot', name: 'Pilot', kind: 'superTab')
+ ..superTabId = '0123456789abcdef01234567',
+ ], 'work');
+ final app = _computer(_Catalog(), store, enteredSuperTabs: false);
+ app.currentUser = null;
+ await app.restorePaneLayoutForTest();
+ app.currentUser = const CurrentUserProfile(
+ id: 'owner',
+ email: '[email protected]',
+ );
+ await app.superTabs.refreshTabs();
+ expect(app.swarms.map((s) => s.id), ['work', 'pilot']);
+ expect(app.activeSwarmId, 'work');
+ },
+ );
+
+ test(
+ 'entering SuperTabs for one account does not reveal them to another',
+ () async {
+ final app = _computer(_Catalog(), MemoryStore());
+ app.currentUser = const CurrentUserProfile(
+ id: 'other',
+ email: '[email protected]',
+ );
+ await app.superTabs.refreshTabs();
+ expect(app.swarms.where((s) => s.isSuperTab), isEmpty);
+ app.enterSuperTabs();
+ await app.superTabs.refreshTabs();
+ expect(app.swarms.where((s) => s.isSuperTab), hasLength(1));
+ },
+ );
+
testWidgets(
'another computer opens the discovered tab and its shared messages',
(tester) async {
@@ -156,6 +235,43 @@ void main() {
},
);
+ test('discovery cannot replace regular tabs during an authenticated layout restore', () async {
+ final store = _RestoringStore();
+ final layout = PaneLayoutStore(storage: store);
+ await layout.saveSwarms([
+ Swarm(id: 'work', name: 'Existing work')
+ ..panes.add(TerminalPane(id: 1, machineId: 'm', agentId: 'a0')),
+ Swarm(id: 'notes', name: 'Notes')
+ ..panes.add(TerminalPane(id: 2, machineId: 'm', agentId: 'a1')),
+ ], 'notes');
+ final saved = store.values['swarm_layout_v1'];
+ final app = _computer(_Catalog(), store);
+ store.holdLayout = true;
+ final restoring = app.restorePaneLayoutForTest();
+ await store.readingLayout.future;
+ List<Swarm>? discoveredDuringRestore;
+ String? savedDuringRestore;
+ try {
+ // Re-seating a signed-in machine restores through the same path while
+ // the workspace is already authenticated. The boot status is no fence.
+ await app.superTabs.refreshTabs();
+ discoveredDuringRestore = app.swarms.where((s) => s.isSuperTab).toList();
+ savedDuringRestore = store.values['swarm_layout_v1'];
+ } finally {
+ store.holdLayout = false;
+ store.releaseLayout.complete();
+ await restoring;
+ }
+ expect(app.swarms.map((s) => s.id), ['work', 'notes']);
+ expect(app.activeSwarmId, 'notes');
+ expect(app.allPanes.map((p) => p.agentId), ['a0', 'a1']);
+ expect(discoveredDuringRestore, isEmpty);
+ expect(savedDuringRestore, saved);
+ await app.superTabs.refreshTabs();
+ expect(app.swarms.map((s) => s.name), ['Existing work', 'Notes', 'Pilot']);
+ expect(app.activeSwarmId, 'notes');
+ });
+
test(
'two computers discover the same tab without navigation or a local copy',
() async {