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(desk): a document only reorders a tab's tiles where the desk's order moved

0xdiego25d2eace541131
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dart
index bd6a4407f..887849a50 100644
--- a/desktop/lib/state/app_state.dart
+++ b/desktop/lib/state/app_state.dart
@@ -8632,7 +8632,10 @@ class AppNotifier extends ChangeNotifier {
.where((t) => !doc.tabs.any((d) => d.id == t.id))
.toList();
_desk.revision = doc.revision;
- _desk.synced = doc.tabs;
+ // What this window believes at the join is its own layout: the desk's
+ // order then wins where the two differ (it moved while this window was
+ // away), and the seed is the desk learning the rest.
+ _desk.synced = own;
if (unknown.isNotEmpty) {
_desk.pending.add({
'op': 'seed',
@@ -8647,6 +8650,14 @@ class AppNotifier extends ChangeNotifier {
await _deskFlush();
}
+ static bool _sameKeys(List<String> a, List<String> b) {
+ if (a.length != b.length) return false;
+ for (var i = 0; i < a.length; i++) {
+ if (a[i] != b[i]) return false;
+ }
+ return true;
+ }
+
/// Called from [_persistLayout]: whatever just changed, said to the desk as ops.
void _deskQueueDiff() {
if (!_desk.enabled) return;
@@ -8736,8 +8747,9 @@ class AppNotifier extends ChangeNotifier {
if (doc.revision < _desk.revision) return;
_desk.revision = doc.revision;
final target = applyDeskOps(doc.tabs, _desk.pending);
+ final believed = _desk.synced;
_desk.synced = target;
- _deskReconcile(target);
+ _deskReconcile(target, believed: believed);
}
/// Make `swarms` say what [target] says, and no more: tabs the desk closed go
@@ -8745,8 +8757,19 @@ class AppNotifier extends ChangeNotifier {
/// opened arrive as intent and attach as their machines answer, names the
/// person chose follow, order follows. Focus, zoom, sizes, pins, viewers and
/// this window's own local-only tabs are left exactly where they were.
- void _deskReconcile(List<DeskTab> target) {
+ ///
+ /// [believed] is the desk as this window last knew it. A tab's pane order is
+ /// touched only where the desk's order MOVED since then — a drag on another
+ /// Mac — and then only among the agent panes, each keeping its slot's size
+ /// and its pin, the way a local drag does. Rebuilding the order from every
+ /// document that arrived (the 15 s poll included) was what shuffled tiles
+ /// and their sizes under a person's hands (owner, 2026-09-21).
+ void _deskReconcile(
+ List<DeskTab> target, {
+ List<DeskTab> believed = const [],
+ }) {
final targetById = {for (final t in target) t.id: t};
+ final believedById = {for (final t in believed) t.id: t};
final before = _deskProjection();
final released = <TerminalPane>[];
@@ -8792,14 +8815,10 @@ class AppNotifier extends ChangeNotifier {
}
}
}
+ String keyOf(TerminalPane p) => '${p.machineId}\u0000${p.agentId}';
for (var i = 0; i < tab.panes.length; i++) {
final ref = tab.panes[i];
- final existing = swarm.panes
- .where(
- (p) => p.machineId == ref.machineId && p.agentId == ref.agentId,
- )
- .firstOrNull;
- if (existing != null) continue;
+ if (swarm.panes.any((p) => keyOf(p) == ref.key)) continue;
// One TerminalPane per (machine, agent) across tabs — the same rule the
// restore keeps — so a second tab showing an agent reuses its stream.
final pane =
@@ -8814,38 +8833,58 @@ class AppNotifier extends ChangeNotifier {
machineId: ref.machineId,
agentId: ref.agentId,
);
- swarm.panes.insert(i.clamp(0, swarm.panes.length), pane);
- swarm.focusedPaneId ??= pane.id;
- }
- // Order among agent panes, viewers riding beside their owners.
- final agentOrder = {
- for (var i = 0; i < tab.panes.length; i++) tab.panes[i].key: i,
- };
- final ordered = swarm.panes.where((p) => p.agentId != null).toList()
- ..sort(
- (a, b) => (agentOrder['${a.machineId}\u0000${a.agentId}'] ?? 0)
- .compareTo(agentOrder['${b.machineId}\u0000${b.agentId}'] ?? 0),
- );
- final rebuilt = <TerminalPane>[];
- for (final pane in ordered) {
- rebuilt.add(pane);
- rebuilt.addAll(
- swarm.panes.where(
- (v) =>
- v.isWeb &&
- v.machineId == pane.machineId &&
- v.ownerAgentId == pane.agentId,
- ),
+ // After the agent pane the desk lists before it, whatever else (a
+ // viewer, an empty tile) sits between; at the end when it is first.
+ final after = i == 0
+ ? -1
+ : swarm.panes.indexWhere((p) => keyOf(p) == tab.panes[i - 1].key);
+ swarm.panes.insert(
+ after < 0 && i > 0 ? swarm.panes.length : after + 1,
+ pane,
);
+ swarm.focusedPaneId ??= pane.id;
}
- for (final pane in swarm.panes) {
- if (!rebuilt.contains(pane)) {
- rebuilt.add(pane);
+ // Order among the agent panes: the desk's, but only where the desk's own
+ // order moved since this window last agreed with it. The panes keep
+ // their slots (so their sizes) and everything that is not an agent pane
+ // stays exactly where it was; a pin follows its pane, as it does on a
+ // local drag.
+ final wantedOrder = [
+ for (final ref in tab.panes)
+ if (swarm.panes.any((p) => keyOf(p) == ref.key)) ref.key,
+ ];
+ final known = believedById[tab.id]?.panes.map((p) => p.key).toSet();
+ final knownOrder = known == null
+ ? null
+ : [
+ for (final ref in believedById[tab.id]!.panes)
+ if (wantedOrder.contains(ref.key)) ref.key,
+ ];
+ final moved =
+ knownOrder == null ||
+ !_sameKeys(knownOrder, wantedOrder.where(known!.contains).toList());
+ if (moved) {
+ final slots = <int>[];
+ for (var i = 0; i < swarm.panes.length; i++) {
+ if (swarm.panes[i].agentId != null) slots.add(i);
+ }
+ final byKey = {for (final p in swarm.panes) keyOf(p): p};
+ final inOrder = [for (final k in wantedOrder) byKey[k]!];
+ if (slots.length == inOrder.length) {
+ final pinnedAt = <TerminalPane, int>{};
+ for (var j = 0; j < slots.length; j++) {
+ final pane = inOrder[j];
+ final was = swarm.panes.indexOf(pane);
+ final slot = swarm.pinnedSlots[pane.id] ?? pane.pinnedSlot;
+ if (slot != null && slot == was) pinnedAt[pane] = slots[j];
+ swarm.panes[slots[j]] = pane;
+ }
+ for (final entry in pinnedAt.entries) {
+ swarm.pinnedSlots[entry.key.id] = entry.value;
+ if (hasNavigationRail) entry.key.pinnedSlot = entry.value;
+ }
}
}
- swarm.panes
- ..clear()
- ..addAll(rebuilt);
if (!swarm.nameIsCustom &&
swarm.titleAgentId == null &&
swarm.panes.isNotEmpty) {
diff --git a/desktop/test/desk_sync_test.dart b/desktop/test/desk_sync_test.dart
index dac278c93..b725de807 100644
--- a/desktop/test/desk_sync_test.dart
+++ b/desktop/test/desk_sync_test.dart
@@ -1,11 +1,15 @@
// The desk: the account's tabs, the same on every computer. The pure half
// (diff and replay) and the window's half (a document becomes `swarms`, an
// edit becomes ops, and what a window keeps for itself stays put).
+import 'dart:ui' show Rect;
+
import 'package:flutter_test/flutter_test.dart';
import 'package:harness/api/api_client.dart';
import 'package:harness/auth/auth_session.dart';
import 'package:harness/core/config.dart';
import 'package:harness/state/desk_sync.dart';
+import 'package:harness/state/pane_arrangement.dart';
+import 'package:harness/state/terminal_pane.dart';
import 'swarm_state_test.dart' show createApp;
@@ -336,6 +340,160 @@ void main() {
);
});
+ test('a document that did not move a tab\'s order leaves this window\'s tiles, sizes and pins alone', () async {
+ final api = _DeskApi()
+ ..doc = DeskDoc(
+ revision: 1,
+ tabs: [
+ tab('d1', name: 'One', custom: true, agents: ['a1', 'a2']),
+ ],
+ );
+ final app = createApp()..api = api;
+ addTearDown(app.dispose);
+ await app.deskStartForTest();
+ app.selectSwarm('d1');
+ final d1 = app.activeSwarm;
+ // This window's own furniture: a viewer between the two agents, a size the
+ // person dragged, a pin on the second agent.
+ final viewer = TerminalPane(
+ id: 900,
+ machineId: 'm',
+ kind: PaneKind.web,
+ ownerAgentId: 'a1',
+ url: 'http://127.0.0.1:1/',
+ );
+ d1.panes.insert(1, viewer);
+ final sizes = PaneArrangement([
+ Rect.fromLTWH(0, 0, 0.5, 1),
+ Rect.fromLTWH(0.5, 0, 0.5, 0.5),
+ Rect.fromLTWH(0.5, 0.5, 0.5, 0.5),
+ ]);
+ d1.savePaneSizes('3:manual', sizes);
+ app.togglePinPane(d1.panes[2].id);
+ await Future<void>.delayed(Duration.zero);
+ api.batches.clear();
+
+ // The 15 s poll, and a push about another tab: the same order for this one.
+ api.doc = DeskDoc(
+ revision: 5,
+ tabs: [
+ tab('d1', name: 'One', custom: true, agents: ['a1', 'a2']),
+ tab('d2', name: 'Two', custom: true, agents: ['a3']),
+ ],
+ );
+ await app.deskFetchForTest();
+ await app.handleEventForTest('m', {
+ 'type': 'desk_changed',
+ 'payload': {'revision': 6},
+ });
+ await Future<void>.delayed(Duration.zero);
+
+ expect(d1.panes.map((p) => p.agentId ?? 'viewer'), [
+ 'a1',
+ 'viewer',
+ 'a2',
+ ]);
+ expect(identical(d1.paneSizes['3:manual'], sizes), isTrue);
+ expect(d1.pinnedSlots, {d1.panes[2].id: 2});
+ expect(api.batches, isEmpty, reason: 'nothing to say back');
+ });
+
+ test('a reorder made elsewhere moves the agent panes into their slots and nothing else', () async {
+ final api = _DeskApi()
+ ..doc = DeskDoc(
+ revision: 1,
+ tabs: [
+ tab('d1', name: 'One', custom: true, agents: ['a1', 'a2', 'a3']),
+ ],
+ );
+ final app = createApp()..api = api;
+ addTearDown(app.dispose);
+ await app.deskStartForTest();
+ app.selectSwarm('d1');
+ final d1 = app.activeSwarm;
+ final viewer = TerminalPane(
+ id: 900,
+ machineId: 'm',
+ kind: PaneKind.web,
+ ownerAgentId: 'a1',
+ url: 'http://127.0.0.1:1/',
+ );
+ d1.panes.insert(1, viewer);
+ final sizes = PaneArrangement([
+ Rect.fromLTWH(0, 0, 0.5, 1),
+ Rect.fromLTWH(0.5, 0, 0.5, 0.5),
+ Rect.fromLTWH(0.5, 0.5, 0.25, 0.5),
+ Rect.fromLTWH(0.75, 0.5, 0.25, 0.5),
+ ]);
+ d1.savePaneSizes('4:manual', sizes);
+ final a3 = d1.panes[3];
+ app.togglePinPane(a3.id); // pinned in slot 3
+ await Future<void>.delayed(Duration.zero);
+ api.batches.clear();
+
+ // The other Mac dragged a3 to the front.
+ api.doc = DeskDoc(
+ revision: 7,
+ tabs: [
+ tab('d1', name: 'One', custom: true, agents: ['a3', 'a1', 'a2']),
+ ],
+ );
+ await app.handleEventForTest('m', {
+ 'type': 'desk_changed',
+ 'payload': {'revision': 7},
+ });
+ await Future<void>.delayed(Duration.zero);
+
+ // Agent panes take the agent slots (0, 2, 3) in the desk's order; the
+ // viewer keeps slot 1; the size arrangement is untouched; the pin
+ // followed a3 to its new slot.
+ expect(d1.panes.map((p) => p.agentId ?? 'viewer'), [
+ 'a3',
+ 'viewer',
+ 'a1',
+ 'a2',
+ ]);
+ expect(identical(d1.paneSizes['4:manual'], sizes), isTrue);
+ expect(d1.pinnedSlots, {a3.id: 0});
+ expect(app.deskSyncForTest.pending, isEmpty);
+ expect(
+ api.batches,
+ isEmpty,
+ reason: 'the order came from the desk; nothing to send back',
+ );
+ });
+
+ test(
+ 'at the join the desk\'s order wins over the layout this window restored',
+ () async {
+ final id = newDeskId();
+ final api = _DeskApi()
+ ..doc = DeskDoc(
+ revision: 3,
+ tabs: [
+ tab(id, name: 'One', custom: true, agents: ['a2', 'a1']),
+ ],
+ );
+ final app = createApp()..api = api;
+ addTearDown(app.dispose);
+ // Restored from before: the same tab, the other way round.
+ app.newSwarm(name: 'One');
+ final local = app.activeSwarm..id = id;
+ await app.addAgentToSwarm('m', 'a1', swarmId: id);
+ await app.addAgentToSwarm('m', 'a2', swarmId: id);
+ expect(local.panes.map((p) => p.agentId), ['a1', 'a2']);
+
+ await app.deskStartForTest();
+ expect(app.swarms.where((s) => s.id == id).length, 1);
+ expect(local.panes.map((p) => p.agentId), ['a2', 'a1']);
+ expect(
+ api.batches.expand((b) => b).where((o) => o['op'] == 'pane.move'),
+ isEmpty,
+ reason: 'this window changed nothing',
+ );
+ },
+ );
+
test('keeps its ops while the backend is unreachable and sends them once it is back', () async {
final api = _DeskApi();
final app = createApp()..api = api;