Files
orca/src/main/ipc/preflight-command-exec.test.ts
T
Neil c3a1694b1d perf(preflight): read the WSL mount table once, and make launch agree with detection (#16053)
* 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 <every agent>`, 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.

* 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.

* 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.
2026-08-23 02:58:40 -07:00

62 lines
2.1 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { buildPosixCommandPathLookupScript } from '../../shared/posix-command-path-lookup'
const { runPreflightCommandInWslMock } = vi.hoisted(() => ({
runPreflightCommandInWslMock: vi.fn()
}))
vi.mock('./preflight-wsl-command', () => ({
runPreflightCommandInWsl: runPreflightCommandInWslMock
}))
import { isCommandOnPath } from './preflight-command-exec'
describe('isCommandOnPath', () => {
const sentinel = '__ORCA_PREFLIGHT_COMMAND_PATH__'
beforeEach(() => {
runPreflightCommandInWslMock.mockReset()
})
afterEach(() => {
vi.restoreAllMocks()
})
it('uses the shared literal lookup and accepts a sentinel-prefixed POSIX path', async () => {
vi.spyOn(process, 'platform', 'get').mockReturnValue('win32')
runPreflightCommandInWslMock.mockResolvedValue({
stdout: `shell startup chatter\n${sentinel}/home/user/.local/bin/codex\n`,
stderr: ''
})
const found = await isCommandOnPath('codex', { distro: 'Ubuntu' })
expect(found).toBe(true)
expect(runPreflightCommandInWslMock).toHaveBeenCalledOnce()
const [, command] = runPreflightCommandInWslMock.mock.calls[0] as [{ distro: string }, string]
expect(command).toContain(
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')
)
})
it.each([
['/absolute/startup/chatter', false],
[`${sentinel}relative/path`, false],
['codex', false],
["alias codex='codex --wrapped'", false]
])('parses WSL lookup output %s as available: %s', async (stdout, expected) => {
vi.spyOn(process, 'platform', 'get').mockReturnValue('win32')
runPreflightCommandInWslMock.mockResolvedValue({ stdout: `${stdout}\n`, stderr: '' })
await expect(isCommandOnPath('codex', { distro: 'Ubuntu' })).resolves.toBe(expected)
})
})