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.

Keep a machine profile from opening another computer's tab

Jasmine Naderi0ce565af7490
diff --git a/desktop/lib/settings/sections/profiles_section.dart b/desktop/lib/settings/sections/profiles_section.dart
index 238e0d152..b2d21f673 100644
--- a/desktop/lib/settings/sections/profiles_section.dart
+++ b/desktop/lib/settings/sections/profiles_section.dart
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
+import '../../core/models.dart';
import '../../shared/widgets/section_scaffold.dart';
import '../../shared/widgets/setting_row.dart';
import '../../state/app_state.dart';
@@ -18,6 +19,10 @@ class ProfilesSection extends StatelessWidget {
final selected = notifier.machineProfileId;
final localId = notifier.localMachineState?.machine.machineId;
final machines = notifier.machines;
+ final names = <String, int>{};
+ for (final machine in machines) {
+ names[machine.displayName] = (names[machine.displayName] ?? 0) + 1;
+ }
return SectionScaffold(
title: 'Profiles',
subtitle:
@@ -36,9 +41,11 @@ class ProfilesSection extends StatelessWidget {
for (final machine in machines)
SettingRow(
title: machine.displayName,
- detail: machine.machineId == localId
- ? 'This computer. Tabs whose agents are all here.'
- : 'That computer. Tabs whose agents are all there.',
+ detail: _detail(
+ machine,
+ localId: localId,
+ sharedName: (names[machine.displayName] ?? 0) > 1,
+ ),
control: _choice(
selected: selected == machine.machineId,
onPressed: () => notifier.setMachineProfile(machine.machineId),
@@ -51,6 +58,17 @@ class ProfilesSection extends StatelessWidget {
},
);
+ String _detail(Machine machine, {String? localId, required bool sharedName}) {
+ final where = machine.machineId == localId
+ ? 'This computer. Tabs whose agents are all here.'
+ : 'That computer. Tabs whose agents are all there.';
+ if (!sharedName) return where;
+ final short = machine.machineId.length > 8
+ ? machine.machineId.substring(0, 8)
+ : machine.machineId;
+ return '$where ($short)';
+ }
+
Widget _choice({required bool selected, required VoidCallback onPressed}) {
return OutlinedButton(
onPressed: selected ? null : onPressed,
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dart
index 029ebad51..b9287711b 100644
--- a/desktop/lib/state/app_state.dart
+++ b/desktop/lib/state/app_state.dart
@@ -1045,7 +1045,9 @@ class AppNotifier extends ChangeNotifier {
final view = profileSwarms;
if (view.isEmpty) return;
final index = view.indexWhere((swarm) => swarm.id == activeSwarmId);
- final next = index < 0 ? 0 : (index + delta) % view.length;
+ var next = index < 0 ? (delta < 0 ? -1 : 0) : index + delta;
+ next %= view.length;
+ if (next < 0) next += view.length;
selectSwarm(view[next].id);
}
@@ -1056,14 +1058,9 @@ class AppNotifier extends ChangeNotifier {
if (next == _machineProfileId) return;
_machineProfileId = next;
unawaited(_paneLayout?.saveMachineProfile(next));
- final view = profileSwarms;
- if (view.isEmpty) {
- newSwarm();
- return;
- }
- if (view.every((swarm) => swarm.id != _activeSwarmId)) {
- selectSwarm(view.first.id);
- return;
+ if (!profileSwarms.any((swarm) => swarm.id == _activeSwarmId)) {
+ _ensureProfileFocus();
+ _persistLayout();
}
notifyListeners();
}
@@ -1107,10 +1104,14 @@ class AppNotifier extends ChangeNotifier {
if (index < 0) return;
// Held ⌘W must not manufacture and close an endless sequence of blank
// welcome tabs, evicting the real work from recently closed history.
- if (swarms.length == 1 &&
- swarms.single.panes.isEmpty &&
- swarms.single.name == Swarm.defaultName &&
- swarms.single.presets.isEmpty) {
+ // Under a machine profile the hidden tabs do not count as something still
+ // open: the last visible welcome is the one this window can see.
+ final visible = profileSwarms;
+ if (visible.length == 1 &&
+ visible.single.id == id &&
+ visible.single.panes.isEmpty &&
+ visible.single.name == Swarm.defaultName &&
+ visible.single.presets.isEmpty) {
return;
}
final removed = swarms.removeAt(index);
@@ -1137,7 +1138,18 @@ class AppNotifier extends ChangeNotifier {
),
);
if (_activeSwarmId == id) {
- _activeSwarmId = swarms[index.clamp(0, swarms.length - 1)].id;
+ // The next full-list tab may belong to another computer. Land on the
+ // next tab this profile still shows, never on one it is hiding.
+ final neighbor = profileNeighborId(swarms, _machineProfileId, index);
+ if (neighbor != null) {
+ _activeSwarmId = neighbor;
+ } else if (swarms.isNotEmpty) {
+ final starter = Swarm(
+ id: _desk.enabled ? newDeskId() : 'swarm-${_nextSwarmId++}',
+ );
+ swarms.add(starter);
+ _activeSwarmId = starter.id;
+ }
}
_persistLayout();
notifyListeners();
@@ -9223,7 +9235,24 @@ class AppNotifier extends ChangeNotifier {
notifyListeners();
}
+ void _ensureProfileFocus() {
+ if (_machineProfileId == null || _machineProfileId!.isEmpty) return;
+ if (profileSwarms.any((swarm) => swarm.id == _activeSwarmId)) return;
+ final visible = swarmsForMachineProfile(swarms, _machineProfileId);
+ if (visible.isNotEmpty) {
+ _activeSwarmId = visible.first.id;
+ } else {
+ final starter = Swarm(
+ id: _desk.enabled ? newDeskId() : 'swarm-${_nextSwarmId++}',
+ );
+ swarms.add(starter);
+ _activeSwarmId = starter.id;
+ }
+ selectedMachineId = focusedPane?.machineId;
+ }
+
void _persistLayout() {
+ _ensureProfileFocus();
_draftSwarmReturns.removeWhere((id, _) {
final swarm = swarms.where((swarm) => swarm.id == id).firstOrNull;
return swarm == null ||
@@ -9396,11 +9425,17 @@ class AppNotifier extends ChangeNotifier {
_activeSwarmId = restored.any((s) => s.id == saved['activeId'])
? saved['activeId'] as String
: restored.first.id;
+ final restoredActive = _activeSwarmId;
+ // A saved active tab can belong to another computer. The profile
+ // choice has to win on launch, or the strip hides the tab on screen.
+ _ensureProfileFocus();
while (swarms.any((s) => s.id == 'swarm-$_nextSwarmId')) {
_nextSwarmId++;
}
_autoPickedAgent = true;
- if (hadDuplicateStarters) _persistLayout();
+ if (hadDuplicateStarters || _activeSwarmId != restoredActive) {
+ _persistLayout();
+ }
notifyListeners();
return;
}
diff --git a/desktop/lib/state/machine_profile.dart b/desktop/lib/state/machine_profile.dart
index d36d9277f..41d0c4f05 100644
--- a/desktop/lib/state/machine_profile.dart
+++ b/desktop/lib/state/machine_profile.dart
@@ -10,17 +10,37 @@ import 'swarm.dart';
bool swarmMatchesMachineProfile(Swarm swarm, String? machineId) {
if (machineId == null || machineId.isEmpty) return true;
if (swarm.isStore) return true;
- final ids = <String>{
+ final paneIds = <String>{
for (final pane in swarm.panes)
if (pane.machineId.isNotEmpty) pane.machineId,
- if (swarm.titleMachineId != null && swarm.titleMachineId!.isNotEmpty)
- swarm.titleMachineId!,
- if (swarm.orchestratorMachineId != null &&
- swarm.orchestratorMachineId!.isNotEmpty)
- swarm.orchestratorMachineId!,
};
- if (ids.isEmpty) return true;
- return ids.every((id) => id == machineId);
+ // Panes are the membership. A title left over from an agent that has since
+ // left the tab must not hide a tab whose remaining agents are all here.
+ if (paneIds.isNotEmpty) return paneIds.every((id) => id == machineId);
+ if (swarm.orchestratorMachineId != null &&
+ swarm.orchestratorMachineId!.isNotEmpty) {
+ return swarm.orchestratorMachineId == machineId;
+ }
+ if (swarm.titleMachineId != null && swarm.titleMachineId!.isNotEmpty) {
+ return swarm.titleMachineId == machineId;
+ }
+ return true;
+}
+
+/// After [closedIndex] is removed, the visible tab that should come forward.
+/// The hidden tabs in between are not candidates. Null when none are visible.
+String? profileNeighborId(
+ List<Swarm> swarms,
+ String? machineId,
+ int closedIndex,
+) {
+ Swarm? fallback;
+ for (var i = 0; i < swarms.length; i++) {
+ if (!swarmMatchesMachineProfile(swarms[i], machineId)) continue;
+ fallback = swarms[i];
+ if (i >= closedIndex) return swarms[i].id;
+ }
+ return fallback?.id;
}
/// Tabs this window shows for [machineId]. The underlying list is not copied
diff --git a/desktop/test/machine_profile_test.dart b/desktop/test/machine_profile_test.dart
index b216b21d7..046e40836 100644
--- a/desktop/test/machine_profile_test.dart
+++ b/desktop/test/machine_profile_test.dart
@@ -39,4 +39,20 @@ void main() {
expect(swarmMatchesMachineProfile(named, 'm1'), isFalse);
expect(swarmMatchesMachineProfile(named, 'm2'), isTrue);
});
+
+ test('a stale title does not hide a tab whose agents are all on this computer', () {
+ final local = tab('local', ['m1'])..titleMachineId = 'm2';
+ expect(swarmMatchesMachineProfile(local, 'm1'), isTrue);
+ expect(swarmMatchesMachineProfile(local, 'm2'), isFalse);
+ });
+
+ test('closing a visible tab does not land on a hidden one in between', () {
+ final local = tab('local', ['m1']);
+ final remote = tab('remote', ['m2']);
+ final store = Swarm(id: 'store', name: Swarm.storeName, kind: 'store');
+ final swarms = [local, remote, store];
+ swarms.removeAt(0);
+ expect(profileNeighborId(swarms, 'm1', 0), 'store');
+ expect(profileNeighborId([remote], 'm1', 0), isNull);
+ });
}
diff --git a/docs/app.md b/docs/app.md
index 7a5c37541..9368f98af 100644
--- a/docs/app.md
+++ b/docs/app.md
@@ -86,6 +86,8 @@ link flow; the CLI equivalent is under [The daemon and CLI](#the-daemon-and-cli)
Settings ▸ Profiles keeps that one sign-in and chooses which computer's tabs
this window draws. **All machines** is the shared desk. A computer's row hides
tabs whose agents run anywhere else. Those tabs stay on the account, so another
-window, or **All machines** here, still has them. A tab that mixes agents from
-two computers stays under **All machines** only. The Store and an empty tab
-remain in every profile.
+window, or **All machines** here, still has them. Closing the last visible tab
+opens an empty tab on this computer; it does not reveal a hidden one. A tab
+that mixes agents from two computers, or whose title still names another
+computer after its agents have all moved here, follows its agents. The Store
+and an empty tab remain in every profile.