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: preserve harness panes through transient sync

0xdiego25524c61c29dfc
diff --git a/cli/src/lib/tmuxAgentDiscovery.ts b/cli/src/lib/tmuxAgentDiscovery.ts
index 3e7ff73c4..5cd411216 100644
--- a/cli/src/lib/tmuxAgentDiscovery.ts
+++ b/cli/src/lib/tmuxAgentDiscovery.ts
@@ -76,6 +76,17 @@ export interface TmuxAgentDiscoveryDeps {
const MISS_LIMIT = 2
+/**
+ * `tmux list-panes` exits non-zero before the first server exists. That is an
+ * empty inventory, not a missing tmux binary or an unusable backend: the
+ * first `tmux new-session` starts the server itself. Keeping the distinction
+ * here prevents daemon startup from publishing a scary, and inaccurate,
+ * "tmux unavailable" state on a fresh machine.
+ */
+export function isNoTmuxServerError(error: string): boolean {
+ return /no server running on\s+\S+/i.test(error)
+}
+
function execText(
command: string,
args: string[],
@@ -126,7 +137,13 @@ export async function listTmuxPanes(): Promise<TmuxPaneInventory> {
// Printable delimiters survive tmux's POSIX-locale output sanitiser. Split only the three fixed
// separators so a legitimate `|` in pane_current_path remains part of the path.
const result = await execText('tmux', ['list-panes', '-a', '-F', '#{pane_id}|#{pane_pid}|#{session_name}|#{pane_current_path}'], 2_000)
- if (!result.ok) return result
+ if (!result.ok) {
+ // A server does not exist until the first Harness agent (or a user) opens
+ // a tmux session. Treating this as unavailable made a clean WSL install
+ // look broken even though create() can start that server normally.
+ if (isNoTmuxServerError(result.error)) return { ok: true, panes: [] }
+ return result
+ }
return { ok: true, panes: parsePanes(result.stdout).filter((pane) => isHarnessSession(pane.tmuxSessionName)) }
}
diff --git a/cli/src/lib/tmuxBackend.spec.ts b/cli/src/lib/tmuxBackend.spec.ts
index 9b998abbf..e80bb570c 100644
--- a/cli/src/lib/tmuxBackend.spec.ts
+++ b/cli/src/lib/tmuxBackend.spec.ts
@@ -168,6 +168,25 @@ esac
])
})
+ it('treats a fresh tmux installation with no server as an available empty inventory', async () => {
+ const dir = mkdtempSync(join(tmpdir(), 'tmux-backend-no-server-'))
+ dirs.push(dir)
+ const tmux = join(dir, 'tmux')
+ writeFileSync(tmux, `#!/bin/sh
+if [ "$1" = list-panes ]; then
+ printf 'no server running on /tmp/tmux-1000/default\\n' >&2
+ exit 1
+fi
+`)
+ chmodSync(tmux, 0o700)
+ process.env.PATH = `${dir}${delimiter}${originalPath ?? ''}`
+
+ await expect(new TmuxBackend().inventory()).resolves.toEqual({
+ state: 'available',
+ roots: [],
+ })
+ })
+
it('respawns a pane in place with -k, an optional cwd, and the exact argv, no shell', async () => {
const dir = mkdtempSync(join(tmpdir(), 'tmux-backend-respawn-'))
dirs.push(dir)
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dart
index 3d7896a42..bcfb1c095 100644
--- a/desktop/lib/state/app_state.dart
+++ b/desktop/lib/state/app_state.dart
@@ -6963,7 +6963,19 @@ class AppNotifier extends ChangeNotifier {
// already `loaded`, so this push is the only signal that it can attach now.
_attachPendingPanes(machine);
} else {
- await _removeAgent(machine, agent.id);
+ // A process replacement can briefly publish an agent before its
+ // terminal route is verified. `agent_synced` is a snapshot, not
+ // a deletion authority: removing every tile here turns that
+ // short gap into a lost workspace even though tmux and the
+ // agent are still alive. Keep the pane/layout intent and let a
+ // later available sync reattach it. A confirmed `agent_deleted`
+ // event remains the sole path that removes a person's panes.
+ _upsertAgent(machine, agent);
+ for (final pane in panesFor(machine.machine.machineId)) {
+ if (pane.agentId != agent.id) continue;
+ await _detachSession(pane, sendClose: false);
+ }
+ notifyListeners();
}
} catch (_) {
unawaited(_loadMachineData(machine, force: true));
diff --git a/desktop/test/agent_creation_recovery_test.dart b/desktop/test/agent_creation_recovery_test.dart
index ecd5157af..2f55e43fb 100644
--- a/desktop/test/agent_creation_recovery_test.dart
+++ b/desktop/test/agent_creation_recovery_test.dart
@@ -185,6 +185,30 @@ void main() {
app.dispose();
});
+ test('a creation receipt opens its exact agent instead of an older matching-engine pane', () async {
+ final connection = _Connection();
+ final app = createApp(connectionForTest: (_) => connection);
+ addTearDown(app.dispose);
+ final stale = app.adoptSessionForTest(
+ terminal('stale-codex', <TerminalBinaryFrame>[]),
+ );
+
+ final creating = app.createAgent('m', engine: 'codex', folder: '/work');
+ final request = connection.calls.single;
+ request.reply.complete({
+ 'creationId': request.creationId,
+ 'state': 'created',
+ 'agent': {'id': 'fresh-codex', 'name': 'Fresh Codex', 'engine': 'codex'},
+ });
+
+ expect(await creating, isNull);
+ expect(app.panes.map((pane) => pane.agentId), [
+ 'stale-codex',
+ 'fresh-codex',
+ ]);
+ expect(app.panes.first, same(stale));
+ });
+
for (final entry in ['Open', 'Split right', 'Split down']) {
testWidgets(
'New from $entry preserves its destination without stacking search',
diff --git a/desktop/test/web_pane_test.dart b/desktop/test/web_pane_test.dart
index 40defec0c..bc42ee14c 100644
--- a/desktop/test/web_pane_test.dart
+++ b/desktop/test/web_pane_test.dart
@@ -16,6 +16,7 @@ import 'swarm_state_test.dart' show createApp;
Map<String, dynamic> _frame(
String id, {
+ bool terminalAvailable = true,
String? viewerUrl,
String? viewerError,
String? viewerName,
@@ -31,16 +32,18 @@ Map<String, dynamic> _frame(
'viewerName': ?viewerName,
'verdict': ?verdict,
'terminal': {
- 'available': true,
- 'runtimes': [
- {'backend': 'tmux', 'paneId': '%1'},
- ],
+ 'available': terminalAvailable,
+ if (terminalAvailable)
+ 'runtimes': [
+ {'backend': 'tmux', 'paneId': '%1'},
+ ],
},
};
Future<void> _synced(
AppNotifier app,
String id, {
+ bool terminalAvailable = true,
String? viewerUrl,
String? viewerError,
String? viewerName,
@@ -50,6 +53,7 @@ Future<void> _synced(
'payload': {
'agent': _frame(
id,
+ terminalAvailable: terminalAvailable,
viewerUrl: viewerUrl,
viewerError: viewerError,
viewerName: viewerName,
@@ -62,6 +66,44 @@ List<TerminalPane> _viewers(AppNotifier app) =>
app.panes.where((pane) => pane.isWeb).toList();
void main() {
+ test('a transient terminal-unavailable sync retains the terminal pane until deletion', () async {
+ final app = createApp();
+ addTearDown(app.dispose);
+ final pane = app.adoptSessionForTest(
+ terminal('a0', <TerminalBinaryFrame>[]),
+ );
+
+ await _synced(app, 'a0', terminalAvailable: false);
+
+ expect(app.panes, [pane]);
+ expect(
+ app
+ .stateOf('m')!
+ .agents
+ .firstWhere((agent) => agent.id == 'a0')
+ .terminalAvailable,
+ isFalse,
+ );
+
+ await _synced(app, 'a0');
+ expect(app.panes, [pane]);
+ expect(
+ app
+ .stateOf('m')!
+ .agents
+ .firstWhere((agent) => agent.id == 'a0')
+ .terminalAvailable,
+ isTrue,
+ );
+
+ await app.handleEventForTest('m', {
+ 'type': 'agent_deleted',
+ 'agentId': 'a0',
+ 'payload': {'agentId': 'a0'},
+ });
+ expect(app.panes, isEmpty);
+ });
+
testWidgets(
'remote viewer update guidance can be dismissed and recovers to a forwarded URL',
(tester) async {