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.

feat: whitelist harness-created tmux sessions during agent discovery to ignore manual user sessions

0xken15a7483eb05e69
diff --git a/cli/src/cli.ts b/cli/src/cli.ts
index 7b695bb36..69dea594b 100644
--- a/cli/src/cli.ts
+++ b/cli/src/cli.ts
@@ -60,6 +60,7 @@ import { restartAgent, type RestartAgentDeps } from './lib/restartAgent.js'
import { claudeContinuation, findLiveSession } from './lib/sessionRepair.js'
import { TmuxBackend } from './lib/tmuxBackend.js'
import { createAndRegisterPane } from './lib/createAgentPane.js'
+import { buildHarnessSessionLabel } from './lib/harnessSessionLabel.js'
import { basename } from 'node:path'
import {
bypassPermissionActive,
@@ -3329,7 +3330,9 @@ async function runForeground(session: AuthSession): Promise<void> {
// Harness-created sessions are easy to distinguish from a user's organic tmux sessions while
// retaining the engine and a collision-resistant creation suffix for diagnostics. Computed
// before the grid block because a file-configured engine keys its config directory on it.
- const label = `harness-${engine}-${Date.now()}`.replace(/[^A-Za-z0-9_-]/g, '-')
+ // The `harness-` prefix is also discovery's whitelist (see `isHarnessSession` /
+ // `TmuxBackend.inventory()`) — every pane outside it is invisible to the daemon.
+ const label = buildHarnessSessionLabel(engine)
// A grid is the user's answer to "where should this run", so every way of not honouring it is a
// refusal rather than a fallback — an agent silently started on the engine's own login spends the
// wrong account and looks identical to one that worked.
diff --git a/cli/src/lib/harnessSessionLabel.ts b/cli/src/lib/harnessSessionLabel.ts
new file mode 100644
index 000000000..bb3c0fc48
--- /dev/null
+++ b/cli/src/lib/harnessSessionLabel.ts
@@ -0,0 +1,18 @@
+/**
+ * The tmux session-name convention for agent panes this daemon itself creates via `agent_create`
+ * (`createAgentPane.ts`).
+ *
+ * Discovery uses `isHarnessSession` as a whitelist (`TmuxBackend.inventory()`): a tmux pane whose
+ * session isn't named this way is invisible to the daemon, whether it's a session the user opened
+ * by hand or one an agent spawned itself with a nested `tmux new-session` — neither went through
+ * `agent_create`, so neither should ever appear as a discovered agent (issue autonomous-harness-desktop#6).
+ */
+export const HARNESS_SESSION_PREFIX = 'harness-'
+
+export function buildHarnessSessionLabel(engine: string, now: number = Date.now()): string {
+ return `${HARNESS_SESSION_PREFIX}${engine}-${now}`.replace(/[^A-Za-z0-9_-]/g, '-')
+}
+
+export function isHarnessSession(sessionName: string): boolean {
+ return sessionName.startsWith(HARNESS_SESSION_PREFIX)
+}
diff --git a/cli/src/lib/tmuxAgentDiscovery.spec.ts b/cli/src/lib/tmuxAgentDiscovery.spec.ts
index 5c4c77f79..5441bae17 100644
--- a/cli/src/lib/tmuxAgentDiscovery.spec.ts
+++ b/cli/src/lib/tmuxAgentDiscovery.spec.ts
@@ -13,7 +13,7 @@ import {
} from './tmuxAgentDiscovery.js'
const START = 'Mon Aug 10 12:00:00 2026'
-const pane = { tmuxPane: '%1', rootPid: 1, cwd: '/work/demo' }
+const pane = { tmuxPane: '%1', rootPid: 1, tmuxSessionName: 'harness-claude-1', cwd: '/work/demo' }
const row = (pid: number, parentPid: number, executable: string, args = executable): ProcessRow => ({
pid, parentPid, executable, args, startMarker: START,
})
@@ -27,9 +27,9 @@ const ownership = (cursor: string[] = [], grok: string[] = []): AgentCommandOwne
})
it('parses printable tmux separators without truncating a pipe in cwd', () => {
- expect(parsePanes('%1|42|/work/a|b\n%2|84|/tmp\n')).toEqual([
- { tmuxPane: '%1', rootPid: 42, cwd: '/work/a|b' },
- { tmuxPane: '%2', rootPid: 84, cwd: '/tmp' },
+ expect(parsePanes('%1|42|harness-claude-1|/work/a|b\n%2|84|mysession|/tmp\n')).toEqual([
+ { tmuxPane: '%1', rootPid: 42, tmuxSessionName: 'harness-claude-1', cwd: '/work/a|b' },
+ { tmuxPane: '%2', rootPid: 84, tmuxSessionName: 'mysession', cwd: '/tmp' },
])
})
@@ -245,7 +245,7 @@ describe('tmux process agent snapshot discovery', () => {
it('creates independent agents for two panes running the same engine', () => {
const result = discoverTmuxAgentsFromSnapshot(
- [pane, { tmuxPane: '%2', rootPid: 10, cwd: '/work/other' }],
+ [pane, { tmuxPane: '%2', rootPid: 10, tmuxSessionName: 'harness-claude-2', cwd: '/work/other' }],
[row(1, 0, 'zsh'), row(2, 1, 'claude'), row(10, 0, 'zsh'), row(11, 10, 'claude')],
900,
)
diff --git a/cli/src/lib/tmuxAgentDiscovery.ts b/cli/src/lib/tmuxAgentDiscovery.ts
index 88d6d5459..02d143dbd 100644
--- a/cli/src/lib/tmuxAgentDiscovery.ts
+++ b/cli/src/lib/tmuxAgentDiscovery.ts
@@ -31,6 +31,7 @@ import {
export interface TmuxPaneSnapshot {
tmuxPane: string
rootPid: number
+ tmuxSessionName: string
cwd: string
}
@@ -84,12 +85,18 @@ export function parsePanes(stdout: string): TmuxPaneSnapshot[] {
for (const line of stdout.split('\n')) {
const first = line.indexOf('|')
const second = first < 0 ? -1 : line.indexOf('|', first + 1)
- if (first < 0 || second < 0) continue
+ const third = second < 0 ? -1 : line.indexOf('|', second + 1)
+ if (first < 0 || second < 0 || third < 0) continue
const tmuxPane = line.slice(0, first)
const pidText = line.slice(first + 1, second)
const rootPid = Number(pidText)
if (!/^%\d+$/.test(tmuxPane) || !Number.isSafeInteger(rootPid) || rootPid <= 0) continue
- panes.push({ tmuxPane, rootPid, cwd: line.slice(second + 1) })
+ panes.push({
+ tmuxPane,
+ rootPid,
+ tmuxSessionName: line.slice(second + 1, third),
+ cwd: line.slice(third + 1),
+ })
}
return panes
}
@@ -100,9 +107,9 @@ export type TmuxPaneInventory =
/** One bounded tmux inventory read, shared by discovery and the neutral backend adapter. */
export async function listTmuxPanes(): Promise<TmuxPaneInventory> {
- // Printable delimiters survive tmux's POSIX-locale output sanitiser. Split only the two fixed
+ // 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}|#{pane_current_path}'], 2_000)
+ const result = await execText('tmux', ['list-panes', '-a', '-F', '#{pane_id}|#{pane_pid}|#{session_name}|#{pane_current_path}'], 2_000)
return result.ok ? { ok: true, panes: parsePanes(result.stdout) } : result
}
diff --git a/cli/src/lib/tmuxBackend.spec.ts b/cli/src/lib/tmuxBackend.spec.ts
index 8ce618d1e..687bf27ba 100644
--- a/cli/src/lib/tmuxBackend.spec.ts
+++ b/cli/src/lib/tmuxBackend.spec.ts
@@ -98,6 +98,35 @@ printf '%s\\n' "$*" >> "$TMUX_BACKEND_CALLS"
expect(readFileSync(calls, 'utf8').trim()).toBe('set-option -w -t %9 remain-on-exit on')
})
+ it('inventory only exposes panes whose session was named by agent_create (autonomous-harness-desktop#6)', async () => {
+ const dir = mkdtempSync(join(tmpdir(), 'tmux-backend-inventory-'))
+ dirs.push(dir)
+ const tmux = join(dir, 'tmux')
+ // Three panes: one from a daemon-created `harness-*` session (must be listed), one from a
+ // session the user opened by hand, and one from a session an agent spawned itself with a
+ // nested `tmux new-session` — neither of the latter two went through agent_create, so
+ // neither should ever reach discovery.
+ writeFileSync(tmux, `#!/bin/sh
+case "$1" in
+ list-panes)
+ printf '%%1|100|harness-claude-1699999999999|/work/demo\\n'
+ printf '%%2|200|mysession|/home/user\\n'
+ printf '%%3|300|child-of-agent|/work/demo\\n'
+ ;;
+esac
+`)
+ chmodSync(tmux, 0o700)
+ process.env.PATH = `${dir}${delimiter}${originalPath ?? ''}`
+
+ const result = await new TmuxBackend().inventory()
+
+ expect(result.state).toBe('available')
+ if (result.state !== 'available') return
+ expect(result.roots).toEqual([
+ { runtime: { backend: 'tmux', paneId: '%1' }, rootPid: 100, cwd: '/work/demo' },
+ ])
+ })
+
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/cli/src/lib/tmuxBackend.ts b/cli/src/lib/tmuxBackend.ts
index 4fcb742bf..95bfc1e41 100644
--- a/cli/src/lib/tmuxBackend.ts
+++ b/cli/src/lib/tmuxBackend.ts
@@ -31,6 +31,7 @@ import {
} from './tmux.js'
import { listTmuxPanes } from './tmuxAgentDiscovery.js'
import { terminalRouteKey } from './terminalRuntime.js'
+import { isHarnessSession } from './harnessSessionLabel.js'
const TMUX_KEYS: Record<TerminalLogicalKey, string> = {
enter: 'Enter',
@@ -229,11 +230,16 @@ export class TmuxBackend implements TerminalBackend<TmuxRuntimeRef> {
if (!result.ok) return { state: 'unavailable', reason: result.error }
return {
state: 'available',
- roots: result.panes.map((pane) => ({
- runtime: { backend: 'tmux' as const, paneId: pane.tmuxPane },
- rootPid: pane.rootPid,
- cwd: pane.cwd,
- })),
+ // Only panes from sessions this daemon itself named via `agent_create` are ever discoverable —
+ // a session the user opened by hand, or one an agent spawned itself with a nested
+ // `tmux new-session`, never gets surfaced (autonomous-harness-desktop#6).
+ roots: result.panes
+ .filter((pane) => isHarnessSession(pane.tmuxSessionName))
+ .map((pane) => ({
+ runtime: { backend: 'tmux' as const, paneId: pane.tmuxPane },
+ rootPid: pane.rootPid,
+ cwd: pane.cwd,
+ })),
}
}