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(cli): start agents outside replaced workspaces

leoe539942b5ccd
diff --git a/cli/src/cli.ts b/cli/src/cli.ts
index 6e383ac2a..7a2bc33fb 100644
--- a/cli/src/cli.ts
+++ b/cli/src/cli.ts
@@ -3353,11 +3353,12 @@ async function runForeground(session: AuthSession): Promise<void> {
// only `invalid x-api-key`. Nothing is cleared when no grid is in play: an agent on its own login
// is supposed to use exactly these variables.
const clearEnv = gridLaunch ? gridConflictingEnvToClear(gridLaunch) : undefined
- const launchOptions = { bypassPermission, extraArgs: gridLaunch?.args, installIfMissing, clearEnv }
+ const launchOptions = { bypassPermission, extraArgs: gridLaunch?.args, installIfMissing, clearEnv, cwd }
const command = buildEngineCommandArgv(engine, launchOptions)
const argv = buildEngineLaunchArgv(engine, launchOptions)
const spawned = await tmuxBackend.create({
- cwd,
+ // The login shell starts somewhere stable; its argv enters the requested workspace after rc files.
+ cwd: homedir(),
label,
command: argv,
...(gridLaunch ? { env: gridLaunch.env } : {}),
@@ -3474,7 +3475,7 @@ async function runForeground(session: AuthSession): Promise<void> {
respawn: async (argv) => {
const result = await tmuxBackend!.respawn(runtime, {
command: argv,
- ...(session.cwd ? { cwd: session.cwd } : {}),
+ cwd: homedir(),
...(launch.env ? { env: launch.env } : {}),
})
return result.state === 'succeeded'
@@ -3498,6 +3499,7 @@ async function runForeground(session: AuthSession): Promise<void> {
},
buildArgv: (opts) => buildEngineLaunchArgv(session.engine, {
...opts,
+ ...(session.cwd ? { cwd: session.cwd } : {}),
...(launch.extraArgs?.length ? { extraArgs: launch.extraArgs } : {}),
// A pane swap onto a grid has to clear the same vendor credentials a fresh create does, for the
// same reason and against the same failure: an engine re-exec'd with the grid's variables still
diff --git a/cli/src/lib/engineLaunch.spec.ts b/cli/src/lib/engineLaunch.spec.ts
index 8a9fbb8d9..2d49fd6eb 100644
--- a/cli/src/lib/engineLaunch.spec.ts
+++ b/cli/src/lib/engineLaunch.spec.ts
@@ -26,6 +26,15 @@ describe('buildEngineLaunchArgv', () => {
])
})
+ it('enters the workspace only after interactive startup has completed', () => {
+ const argv = buildEngineLaunchArgv('claude', { cwd: '/work/project' }, '/bin/zsh')
+ expect(argv).toEqual([
+ '/bin/zsh', '-lic',
+ 'if ! cd -- "$1"; then printf \'%s\\n\' \'harness: the selected working directory is unavailable.\' >&2; exit 1; fi\nshift\nexec "$@"',
+ 'harness-engine', '/work/project', engineBin('claude'),
+ ])
+ })
+
it('falls back to direct execution when no absolute shell is available', () => {
expect(buildEngineLaunchArgv('claude', {}, '')).toEqual([engineBin('claude')])
expect(buildEngineLaunchArgv('claude', {}, 'zsh')).toEqual([engineBin('claude')])
diff --git a/cli/src/lib/engineLaunch.ts b/cli/src/lib/engineLaunch.ts
index b5e20542f..ae61b62a3 100644
--- a/cli/src/lib/engineLaunch.ts
+++ b/cli/src/lib/engineLaunch.ts
@@ -72,6 +72,8 @@ export interface LaunchCommandOptions {
* machine keeps everything it had.
*/
clearEnv?: readonly string[]
+ /** Workspace entered after interactive-shell startup, not before it. */
+ cwd?: string
}
/**
@@ -187,12 +189,18 @@ export function buildEngineLaunchArgv(
// but an install script that probes for credentials to configure itself would, and the whole point
// of this launch is that the agent's environment is the one the user asked for.
const prelude = clearEnvPrelude(opts.clearEnv)
+ // rc files (notably nvm) call getcwd() before running this command. Start the shell in a safe
+ // directory and enter the selected workspace only after those files have loaded: an IDE can replace
+ // a workspace inode between the desktop picker resolving it and tmux spawning the pane.
+ const cwdPrelude = opts.cwd
+ ? `if ! cd -- "$1"; then printf '%s\\n' 'harness: the selected working directory is unavailable.' >&2; exit 1; fi\nshift\n`
+ : ''
const body = opts.installIfMissing
? installIfMissingThenExecScript(opts.installIfMissing, runtimeNode)
: opts.installFirst
? installThenExecScript(opts.installFirst)
: 'exec "$@"'
- return [interactive.path, ...interactive.args, prelude + body, 'harness-engine', ...command]
+ return [interactive.path, ...interactive.args, prelude + cwdPrelude + body, 'harness-engine', ...(opts.cwd ? [opts.cwd] : []), ...command]
}
/**
diff --git a/cli/src/lib/tmuxOnPath.spec.ts b/cli/src/lib/tmuxOnPath.spec.ts
index 6ccb24297..c78a46661 100644
--- a/cli/src/lib/tmuxOnPath.spec.ts
+++ b/cli/src/lib/tmuxOnPath.spec.ts
@@ -86,6 +86,23 @@ describe('ensureTmuxOnPath', () => {
expect(env.PATH?.split(delimiter)[0]).toBe(binDir)
})
+ it('finds tmux after startup chatter from the login shell', async () => {
+ const binDir = fakeTmux()
+ const shell = join(scratch('tmux-onpath-chatty-shell-'), 'zsh')
+ writeFileSync(shell, `#!/bin/sh
+shift
+printf '%s\\n' 'Now using node v25.7.0'
+PATH="${binDir}" exec /bin/sh -c "$@"
+`)
+ chmodSync(shell, 0o700)
+ const env: NodeJS.ProcessEnv = { PATH: '/nonexistent-for-this-test' }
+
+ const outcome = await ensureTmuxOnPath(env, shell)
+
+ expect(outcome).toMatchObject({ state: 'adopted', path: join(binDir, 'tmux') })
+ expect(env.PATH?.split(delimiter)[0]).toBe(binDir)
+ })
+
it('leaves an already-working PATH untouched', async () => {
const binDir = fakeTmux()
const env: NodeJS.ProcessEnv = { PATH: binDir }
diff --git a/cli/src/lib/tmuxOnPath.ts b/cli/src/lib/tmuxOnPath.ts
index 97a885c0d..8f435429f 100644
--- a/cli/src/lib/tmuxOnPath.ts
+++ b/cli/src/lib/tmuxOnPath.ts
@@ -66,8 +66,12 @@ function probe(shellPath: string, args: readonly string[], command: string): Pro
[...args, RESOLVE_SCRIPT, 'harness-tmux-probe', command],
{ timeout: 5_000 },
(error, stdout) => {
- const first = String(stdout ?? '').trim().split('\n')[0]?.trim() ?? ''
- resolve(!error && isAbsolute(first) ? first : null)
+ // Login rc files are allowed to be chatty. In particular, nvm commonly prints a
+ // "Now using node …" banner before `command -v` writes the actual path. Looking only at
+ // stdout's first line then made a perfectly installed Homebrew tmux appear absent whenever
+ // Harness was started by the desktop app's minimal PATH.
+ const found = String(stdout ?? '').split('\n').map((line) => line.trim()).find(isAbsolute)
+ resolve(!error && found ? found : null)
},
)
})