From c20419e77bd7853528b50a6cbaa822154d5c1b6b Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 23 Aug 2026 01:23:24 -0700 Subject: [PATCH 1/3] perf(preflight): read the WSL mount table once per shell, not once per CLI The prelude is embedded in the lookup script, and the caller wraps that in `for cmd in `, so the unconditional assignment forked awk once per probed CLI -- 36 of them inside the distro against a 10s detection budget. The comment claimed it was read once outside the loop; it was not. `${x+set}` rather than `[ -n ... ]`: a host with no Windows mounts yields the empty string, which must still count as read. Pinned by counting real awk forks through /bin/sh with a stub on PATH, because nothing covered this expression at all -- a wrong-field mutation shipped green. Verified to bind: the unconditional form counts 4 for 4 commands. --- .../ipc/preflight-wsl-agent-detection.test.ts | 49 ++++++++++++++++++- src/shared/posix-command-path-lookup.ts | 13 +++-- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/main/ipc/preflight-wsl-agent-detection.test.ts b/src/main/ipc/preflight-wsl-agent-detection.test.ts index 648c7f4f543a..a3e1bac18e9d 100644 --- a/src/main/ipc/preflight-wsl-agent-detection.test.ts +++ b/src/main/ipc/preflight-wsl-agent-detection.test.ts @@ -1,5 +1,5 @@ import { execFileSync, type ExecFileSyncOptions } from 'node:child_process' -import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' @@ -291,3 +291,50 @@ describe('the PATH walk, run by a real POSIX shell', () => { } }) }) + +describe('the mount table read, counted against a real shell', () => { + const itPosix = process.platform === 'win32' ? it.skip : it + + const runWithCountingAwk = async (commands: string[]): Promise => { + const root = mkdtempSync(join(tmpdir(), 'orca-awk-')) + try { + const counter = join(root, 'count') + const bin = join(root, 'bin') + mkdirSync(bin, { recursive: true }) + // A stub that records each fork, then answers as awk would on a host + // with no Windows mounts. + writeFileSync(join(bin, 'awk'), `#!/bin/sh\necho x >> ${counter}\nexit 0\n`) + chmodSync(join(bin, 'awk'), 0o755) + writeFileSync(counter, '') + + runWslProcessMock.mockResolvedValue({ + environmentResolved: true, + code: 0, + stdout: '', + stderr: '', + timedOut: false + }) + await detectWslCommandsOnPath({ distro: 'Ubuntu' }, commands) + const script = String(runWslProcessMock.mock.calls.at(-1)?.[0].script) + const options: ExecFileSyncOptions = { + encoding: 'utf8', + env: { PATH: `${bin}:/usr/bin:/bin`, HOME: root } + } + execFileSync('/bin/sh', ['-c', script], options) + return readFileSync(counter, 'utf8').trim().split('\n').filter(Boolean).length + } finally { + rmSync(root, { recursive: true, force: true }) + } + } + + itPosix('reads /proc/mounts once, not once per probed command', async () => { + // The prelude is embedded in the lookup script, which the caller wraps in + // `for cmd in `. An unconditional assignment forked awk once + // per CLI -- 36 of them inside the distro against a 10s budget. + expect(await runWithCountingAwk(['claude', 'codex', 'gemini', 'opencode'])).toBe(1) + }) + + itPosix('still reads it once when only one command is probed', async () => { + expect(await runWithCountingAwk(['claude'])).toBe(1) + }) +}) diff --git a/src/shared/posix-command-path-lookup.ts b/src/shared/posix-command-path-lookup.ts index ac179f941fb9..ea09ca83e2fc 100644 --- a/src/shared/posix-command-path-lookup.ts +++ b/src/shared/posix-command-path-lookup.ts @@ -25,11 +25,18 @@ export function buildPosixCommandPathLookupScript( options: PosixCommandPathLookupOptions = {} ): string { const commandAssignment = buildCommandAssignment(target) - // `-t drvfs` is what WSL mounts a Windows drive as, wherever the automount - // root is; 9p/virtiofs cover the WSL2 shapes. Read once, outside the loop. + // `drvfs` is what WSL mounts a Windows drive as, wherever the automount root + // is; 9p/virtiofs cover the WSL2 shapes. + // + // `${x+set}` so the table is read once per SHELL, not once per lookup: the + // caller embeds this script inside `for cmd in `, so an + // unconditional assignment forked awk once per probed CLI -- 36 of them + // against a 10s budget. The variable outlives the iteration, so the second + // pass finds it set. Deliberately not `[ -n ... ]`: a host with no Windows + // mounts yields the empty string, which must still count as read. const mountPrelude = options.skipWindowsMountDirs ? [ - '_orca_win_mounts=$(awk \'$3 == "drvfs" || $3 == "9p" || $3 == "virtiofs" { print $2 }\' /proc/mounts 2>/dev/null)' + '[ "${_orca_win_mounts+set}" = set ] || _orca_win_mounts=$(awk \'$3 == "drvfs" || $3 == "9p" || $3 == "virtiofs" { print $2 }\' /proc/mounts 2>/dev/null)' ] : [] const skipMountComponent = options.skipWindowsMountDirs From 2448e1577f5b4c33ce9a3de9326e5698590acf29 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 23 Aug 2026 01:32:11 -0700 Subject: [PATCH 2/3] fix(wsl): make launch resolve the same binary detection reported Agent detection skips Windows mounts during the PATH walk; the Codex WSL command builder and the WSL branch of isCommandOnPath did not. So Orca could report the guest codex as installed and then launch the Windows one sitting ahead of it on PATH, or disagree with itself between preflight and detection about the same distro. Both now pass the same option. Verified on a real Windows host against a real WSL2 distro, with a Windows binary planted ahead of a guest one on PATH: plain `command -v orcaprobe` -> /mnt/c/Users/neil/orca-agree/orcaprobe this lookup -> /home/neil/.orca-agree/bin/orcaprobe That host reports /mnt/c as 9p, which the mount expression matches, so the fstype list is confirmed against hardware rather than fixtures. --- src/main/codex-accounts/wsl-codex-command.ts | 8 +++++++- src/main/ipc/preflight-command-exec.test.ts | 7 ++++++- src/main/ipc/preflight-command-exec.ts | 8 +++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/codex-accounts/wsl-codex-command.ts b/src/main/codex-accounts/wsl-codex-command.ts index af8ef64011ac..1831a2f215c4 100644 --- a/src/main/codex-accounts/wsl-codex-command.ts +++ b/src/main/codex-accounts/wsl-codex-command.ts @@ -77,7 +77,13 @@ export function buildWslCodexLoginArgs(distro: string, linuxHomePath: string): s } function buildCodexPathLookup(): string { - return buildPosixCommandPathLookupScript({ kind: 'literal', value: 'codex' }) + // Same skip as agent detection: otherwise detection can report the guest + // codex while this resolves the Windows one ahead of it on PATH, and Orca + // launches a different binary than the one it said was installed. + return buildPosixCommandPathLookupScript( + { kind: 'literal', value: 'codex' }, + { skipWindowsMountDirs: true } + ) } function buildWslCodexShellArgs(distro: string, command: string): string[] { diff --git a/src/main/ipc/preflight-command-exec.test.ts b/src/main/ipc/preflight-command-exec.test.ts index 204b8a5f6e16..63c2b35aceb4 100644 --- a/src/main/ipc/preflight-command-exec.test.ts +++ b/src/main/ipc/preflight-command-exec.test.ts @@ -35,7 +35,12 @@ describe('isCommandOnPath', () => { expect(runPreflightCommandInWslMock).toHaveBeenCalledOnce() const [, command] = runPreflightCommandInWslMock.mock.calls[0] as [{ distro: string }, string] expect(command).toContain( - buildPosixCommandPathLookupScript({ kind: 'literal', value: 'codex' }) + buildPosixCommandPathLookupScript( + { kind: 'literal', value: 'codex' }, + // The WSL branch skips Windows mounts, so detection and this check + // cannot disagree about the same distro. + { skipWindowsMountDirs: true } + ) ) expect(command).toContain( ['if [ -n "$resolved" ]; then', `printf '${sentinel}%s\\n' "$resolved"`, 'fi'].join('\n') diff --git a/src/main/ipc/preflight-command-exec.ts b/src/main/ipc/preflight-command-exec.ts index 9dbdbdd343d6..0d602b9827f5 100644 --- a/src/main/ipc/preflight-command-exec.ts +++ b/src/main/ipc/preflight-command-exec.ts @@ -100,7 +100,13 @@ export async function isCommandOnPath( const { stdout } = await execCommandInWsl( wslTarget, [ - buildPosixCommandPathLookupScript({ kind: 'literal', value: command }), + // Same skip as agent detection: without it this branch answers "yes" + // for a Windows binary reached through interop, so preflight and the + // detector disagree about the same distro. + buildPosixCommandPathLookupScript( + { kind: 'literal', value: command }, + { skipWindowsMountDirs: true } + ), 'if [ -n "$resolved" ]; then', `printf '${WSL_COMMAND_PATH_SENTINEL}%s\\n' "$resolved"`, 'fi' From efe5892aa0012003a1acaf97cf7dfa78e5012e27 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 23 Aug 2026 02:51:28 -0700 Subject: [PATCH 3/3] test(preflight): prove the memoised mount list applies past the first command Counting awk forks with a stub that reports no mounts cannot see what the hoist trades correctness for. A mutant that empties `_orca_win_mounts` inside the walk keeps the fork count at 1 and keeps every existing test green, while every agent after the first stops skipping /mnt. This runs two commands behind a stubbed Windows mount and asserts both resolve to the guest binary. Verified against that exact mutant. Credit: review counsel. --- .../ipc/preflight-wsl-agent-detection.test.ts | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/main/ipc/preflight-wsl-agent-detection.test.ts b/src/main/ipc/preflight-wsl-agent-detection.test.ts index a3e1bac18e9d..0f9cdb0d3506 100644 --- a/src/main/ipc/preflight-wsl-agent-detection.test.ts +++ b/src/main/ipc/preflight-wsl-agent-detection.test.ts @@ -337,4 +337,49 @@ describe('the mount table read, counted against a real shell', () => { itPosix('still reads it once when only one command is probed', async () => { expect(await runWithCountingAwk(['claude'])).toBe(1) }) + + itPosix('applies the memoised mount list to every command, not just the first', async () => { + // Counting forks with a stub that reports NO mounts cannot see the thing + // the hoist trades correctness for: a refactor that empties or clobbers + // the variable inside the walk keeps the fork count at 1 and keeps the + // single-command test green, while every agent after the first stops + // skipping /mnt. A surviving mutant proved that gap. + const root = mkdtempSync(join(tmpdir(), 'orca-memo-')) + try { + const win = join(root, 'winmnt/c/npm') + const guest = join(root, 'home/bin') + for (const [dir, body] of [ + [win, 'win'], + [guest, 'guest'] + ] as const) { + mkdirSync(dir, { recursive: true }) + for (const name of ['claude', 'codex']) { + writeFileSync(join(dir, name), `#!/bin/sh\necho ${body}\n`) + chmodSync(join(dir, name), 0o755) + } + } + runWslProcessMock.mockResolvedValue({ + environmentResolved: true, + code: 0, + stdout: '', + stderr: '', + timedOut: false + }) + await detectWslCommandsOnPath({ distro: 'Ubuntu' }, ['claude', 'codex']) + const script = String(runWslProcessMock.mock.calls.at(-1)?.[0].script).replace( + /_orca_win_mounts=\$\([^)]*\)/, + `_orca_win_mounts=${join(root, 'winmnt')}` + ) + const options: ExecFileSyncOptions = { + encoding: 'utf8', + env: { PATH: `${win}:${guest}:/usr/bin:/bin`, HOME: root } + } + const out = String(execFileSync('/bin/sh', ['-c', script], options)) + // BOTH must resolve behind the mount, not just the first. + expect(out).toContain(`__ORCA_AGENT_PATH__claude\t${join(guest, 'claude')}`) + expect(out).toContain(`__ORCA_AGENT_PATH__codex\t${join(guest, 'codex')}`) + } finally { + rmSync(root, { recursive: true, force: true }) + } + }) })