Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 75 additions & 4 deletions src/main/ipc/preflight-wsl-agent-detection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ describe('detectWslCommandsOnPath', () => {
await detectWslCommandsOnPath({ distro: 'Ubuntu' }, ['claude', 'codex'])

const { script } = lastSpec()
const lookupScript = buildPosixCommandPathLookupScript({
kind: 'shell-variable',
name: 'cmd'
})
const lookupScript = buildPosixCommandPathLookupScript(
{ kind: 'shell-variable', name: 'cmd' },
// The WSL probe opts into skipping Windows mounts mid-walk.
{ skipWindowsMountDirs: true }
)
expect(script).toContain(lookupScript)
expect(script).not.toContain('type -P')
})
Expand Down Expand Up @@ -132,6 +133,36 @@ describe('detectWslCommandsOnPath', () => {
})
})

it('does not veto a guest binary on an ordinary Linux mount under /mnt', async () => {
// The walk skips PATH components the guest reports as Windows drives. A name
// rule on top of that could only do harm: /mnt/d can be an ext4 volume, and
// vetoing a path the walk deliberately kept turns the false positive back
// into the #9725 false negative.
runWslProcessMock.mockResolvedValue({
environmentResolved: true,
code: 0,
stdout: '__ORCA_AGENT_PATH__claude\t/mnt/d/tools/claude\n',
stderr: '',
timedOut: false
})
await expect(detectWslCommandsOnPath({ distro: 'Ubuntu' }, ['claude'])).resolves.toEqual(
new Set(['claude'])
)
})

it('still counts a genuine guest install', async () => {
runWslProcessMock.mockResolvedValue({
environmentResolved: true,
code: 0,
stdout: '__ORCA_AGENT_PATH__claude\t/home/alice/.nvm/versions/node/v20.1.0/bin/claude\n',
stderr: '',
timedOut: false
})
expect(await detectWslCommandsOnPath({ distro: 'Ubuntu' }, ['claude'])).toEqual(
new Set(['claude'])
)
})

describe('the detection script itself, run by a real POSIX shell', () => {
// Not a mock: the fallback is shell globbing and `[ -x ]`, which only the
// shell can be trusted about. Skipped on Windows, where /bin/sh is absent.
Expand Down Expand Up @@ -220,3 +251,43 @@ describe('the detection script itself, run by a real POSIX shell', () => {
expect(await runScript()).toContain('__ORCA_AGENT_PATH__orca-fake-cli')
})
})

describe('the PATH walk, run by a real POSIX shell', () => {
const itPosix = process.platform === 'win32' ? it.skip : it

itPosix('finds the guest install behind a Windows binary that shadows it', async () => {
// WSL appends the Windows PATH, so a Windows `claude` sits ahead of an
// nvm one. Rejecting the resolved path afterwards cannot resume the walk,
// so it reports "not installed" for a user who has both -- turning the
// false positive into the #9725 false negative. Skipping the component
// mid-walk is what actually finds the guest binary.
const root = mkdtempSync(join(tmpdir(), 'orca-walk-'))
try {
const win = join(root, 'winmnt/c/npm')
const nvm = join(root, 'home/.nvm/bin')
for (const [dir, body] of [
[win, 'win'],
[nvm, 'guest']
] as const) {
mkdirSync(dir, { recursive: true })
writeFileSync(join(dir, 'claude'), `#!/bin/sh\necho ${body}\n`)
chmodSync(join(dir, 'claude'), 0o755)
}
const script = buildPosixCommandPathLookupScript(
{ kind: 'literal', value: 'claude' },
{ skipWindowsMountDirs: true }
// Stand in for /proc/mounts, which a test host does not have.
).replace(/_orca_win_mounts=\$\([^)]*\)/, `_orca_win_mounts=${join(root, 'winmnt')}`)
const options: ExecFileSyncOptions = {
encoding: 'utf8',
env: { PATH: `${win}:${nvm}:/usr/bin:/bin` }
}
const out = String(
execFileSync('/bin/sh', ['-c', `${script}\nprintf %s "$resolved"`], options)
)
expect(out).toBe(join(nvm, 'claude'))
} finally {
rmSync(root, { recursive: true, force: true })
}
})
})
12 changes: 8 additions & 4 deletions src/main/ipc/preflight-wsl-agent-detection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ export async function detectWslCommandsOnPath(
}

const commandList = uniqueCommands.map(shellQuote).join(' ')
const lookupScript = buildPosixCommandPathLookupScript({
kind: 'shell-variable',
name: 'cmd'
})
const lookupScript = buildPosixCommandPathLookupScript(
{ kind: 'shell-variable', name: 'cmd' },
// Skip Windows mounts DURING the walk, not after it: WSL appends the
// Windows PATH, so a Windows `claude` can shadow a real guest install, and
// discarding the result afterwards reports "not installed" for a user who
// has both -- the #9725 population the fallback dirs exist to serve.
{ skipWindowsMountDirs: true }
)
// Newlines keep the loop valid in every POSIX shell used here.
const script = [
// The same fallback the preflight command runner uses: append the
Expand Down
43 changes: 42 additions & 1 deletion src/shared/posix-command-path-lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,52 @@ export type PosixCommandPathLookupTarget =

const SHELL_VARIABLE_NAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/

export function buildPosixCommandPathLookupScript(target: PosixCommandPathLookupTarget): string {
export type PosixCommandPathLookupOptions = {
/**
* Skip PATH components that are Windows drives mounted into the guest.
*
* WSL appends the Windows PATH to the guest PATH, so a Windows `claude` can
* sit ahead of a real guest install. Rejecting the result AFTER the walk is
* not equivalent: the walk stops at the first hit, so discarding it reports
* "not installed" for a user who has both. Skipping the component keeps the
* walk going and finds the guest binary behind it.
*
* Matched by mount metadata, not by a `/mnt` name: the automount root is
* configurable, and `/mnt` is an ordinary directory name on a Linux box.
*/
skipWindowsMountDirs?: boolean
}

export function buildPosixCommandPathLookupScript(
target: PosixCommandPathLookupTarget,
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.
const mountPrelude = options.skipWindowsMountDirs
? [
'_orca_win_mounts=$(awk \'$3 == "drvfs" || $3 == "9p" || $3 == "virtiofs" { print $2 }\' /proc/mounts 2>/dev/null)'
]
: []
const skipMountComponent = options.skipWindowsMountDirs
? [
' for _orca_win_mount in $_orca_win_mounts; do',
' case "$_orca_lookup_component/" in',
' "$_orca_win_mount"/*) _orca_lookup_component= ;;',
' esac',
' done',
' if [ -z "$_orca_lookup_component" ]; then',
' [ -n "$_orca_lookup_has_more" ] || break',
' continue',
' fi'
]
: []
// Shell command resolution can be masked by aliases, functions, and builtins, so inspect PATH.
return [
`_orca_lookup_command=${commandAssignment}`,
'resolved=',
...mountPrelude,
'case "$_orca_lookup_command" in',
' */*)',
' case "$_orca_lookup_command" in',
Expand All @@ -34,6 +74,7 @@ export function buildPosixCommandPathLookupScript(target: PosixCommandPathLookup
' _orca_lookup_has_more=',
' ;;',
' esac',
...skipMountComponent,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -eu
printf '%s\n' '--- tracked files ---'
git ls-files 'src/shared/posix-command-path-lookup.ts' '*posix*command*' '*command*path*'
printf '%s\n' '--- file outline ---'
ast-grep outline src/shared/posix-command-path-lookup.ts
printf '%s\n' '--- relevant source ---'
cat -n src/shared/posix-command-path-lookup.ts
printf '%s\n' '--- related symbols and tests ---'
rg -n -C 4 'skipWindowsMountDirs|skipMountComponent|posix-command-path-lookup|PATH|PWD|mount' src test tests 2>/dev/null || true

Repository: stablyai/orca

Length of output: 50370


🏁 Script executed:

#!/bin/bash
set -eu
printf '%s\n' '--- target file ---'
wc -l src/shared/posix-command-path-lookup.ts
cat -n src/shared/posix-command-path-lookup.ts
printf '%s\n' '--- exact symbol references ---'
rg -n -C 3 'skipWindowsMountDirs|skipMountComponent|lookup.*Path|posix-command-path-lookup' \
  src/shared src/main src/renderer --glob '*.{ts,tsx,js,jsx}' 2>/dev/null || true
printf '%s\n' '--- candidate tests ---'
git ls-files | rg '(^|/)(.*(posix|command|path).*(test|spec)|.*(test|spec).*(posix|command|path))\.(ts|tsx|js|jsx)$' || true
printf '%s\n' '--- target-file references in tests ---'
rg -l 'posix-command-path-lookup|skipWindowsMountDirs|skipMountComponent' . \
  --glob '*.{ts,tsx,js,jsx}' --glob '!node_modules/**' --glob '!dist/**' --glob '!build/**' 2>/dev/null || true

Repository: stablyai/orca

Length of output: 39973


🏁 Script executed:

#!/bin/bash
set -eu
printf '%s\n' '--- focused lookup tests ---'
cat -n src/shared/posix-command-path-lookup.test.ts
printf '%s\n' '--- WSL detection implementation ---'
cat -n src/main/ipc/preflight-wsl-agent-detection.ts
printf '%s\n' '--- WSL detection tests around mount behavior ---'
sed -n '240,340p' src/main/ipc/preflight-wsl-agent-detection.test.ts | cat -n
printf '%s\n' '--- all fallback references ---'
rg -n -C 6 'buildPosixFallbackPathPrelude|fallback|resolved|skipWindowsMountDirs|detectWslCommandsOnPath' \
  src/main/ipc/preflight-wsl-agent-detection.ts src/main/ipc/preflight-wsl-agent-detection.test.ts \
  src/shared/posix-version-manager-bin-dirs.ts

Repository: stablyai/orca

Length of output: 49764


🏁 Script executed:

#!/bin/bash
set -eu

root="$(mktemp -d)"
trap 'rm -rf "$root"' EXIT

mount_root="$root/win-drive"
project="$mount_root/project"
guest="$root/guest/bin"
mkdir -p "$project" "$guest"

printf '#!/bin/sh\n' > "$project/claude"
printf '#!/bin/sh\n' > "$guest/claude"
chmod 755 "$project/claude" "$guest/claude"

run_lookup() {
  local path_value="$1"
  env PATH="$path_value" /bin/sh -c '
    _orca_win_mounts="$1"
    _orca_lookup_command=claude
    resolved=
    _orca_lookup_remaining=${PATH-}
    while :; do
      case "$_orca_lookup_remaining" in
        *:*)
          _orca_lookup_component=${_orca_lookup_remaining%%:*}
          _orca_lookup_remaining=${_orca_lookup_remaining#*:}
          _orca_lookup_has_more=1
          ;;
        *)
          _orca_lookup_component=$_orca_lookup_remaining
          _orca_lookup_has_more=
          ;;
      esac
      for _orca_win_mount in $_orca_win_mounts; do
        case "$_orca_lookup_component/" in
          "$_orca_win_mount"/*) _orca_lookup_component= ;;
        esac
      done
      if [ -z "$_orca_lookup_component" ]; then
        [ -n "$_orca_lookup_has_more" ] || break
        continue
      fi
      [ -n "$_orca_lookup_component" ] || _orca_lookup_component=.
      case "$_orca_lookup_component" in
        /*) _orca_lookup_candidate=$_orca_lookup_component/$_orca_lookup_command ;;
        *) _orca_lookup_candidate=${PWD%/}/$_orca_lookup_component/$_orca_lookup_command ;;
      esac
      if [ -x "$_orca_lookup_candidate" ] && [ ! -d "$_orca_lookup_candidate" ]; then
        resolved=$_orca_lookup_candidate
        break
      fi
      [ -n "$_orca_lookup_has_more" ] || break
    done
    printf "%s\n" "$resolved"
  ' sh "$mount_root"
}

printf '%s\n' '--- empty component from PWD inside custom mount ---'
(cd "$project" && run_lookup ":$guest")
printf '%s\n' '--- relative component from PWD at custom mount root ---'
(cd "$mount_root" && run_lookup "project:$guest")
printf '%s\n' '--- absolute mount component control ---'
(cd "$mount_root" && run_lookup "$project:$guest")

Repository: stablyai/orca

Length of output: 421


🏁 Script executed:

#!/bin/bash
set -eu

root="$(mktemp -d)"
trap 'rm -rf "$root"' EXIT
mount_root="$root/win-drive"
project="$mount_root/project"
guest="$root/guest/bin"
mkdir -p "$project" "$guest"
printf '#!/bin/sh\n' > "$project/claude"
printf '#!/bin/sh\n' > "$guest/claude"
chmod 755 "$project/claude" "$guest/claude"

(cd "$project" && env PATH=":$guest" /bin/sh -c '
  printf "PWD=%s\nPATH=%s\n" "$PWD" "$PATH"
  _orca_win_mounts="$1"
  _orca_lookup_remaining=${PATH-}
  _orca_lookup_component=${_orca_lookup_remaining%%:*}
  printf "raw-component=<%s>\n" "$_orca_lookup_component"
  [ -n "$_orca_lookup_component" ] || _orca_lookup_component=.
  _orca_lookup_candidate=${PWD%/}/$_orca_lookup_component/claude
  printf "candidate=%s exists=%s executable=%s\n" \
    "$_orca_lookup_candidate" \
    "$(test -e "$_orca_lookup_candidate"; echo "$?")" \
    "$(test -x "$_orca_lookup_candidate"; echo "$?")"
  printf "mount=%s\n" "$_orca_win_mounts"
  case "$_orca_lookup_component/" in
    "$_orca_win_mounts"/*) echo "raw mount match" ;;
    *) echo "raw mount miss" ;;
  esac
  if [ -x "$_orca_lookup_candidate" ] && [ ! -d "$_orca_lookup_candidate" ]; then
    printf "selected=%s\n" "$_orca_lookup_candidate"
  fi
' sh "$mount_root")

Repository: stablyai/orca

Length of output: 432


Normalize relative PATH components before mount filtering.

A relative component can resolve inside a Windows mount after the raw component check. For example, with PWD under a configurable mount root and PATH=project:/guest/bin, project/claude is selected from the Windows mount. Its custom-root, extensionless path passes the fallback filter and is reported as a guest installation. Resolve relative components against PWD before comparing them with mount paths, and add a regression test.

' [ -n "$_orca_lookup_component" ] || _orca_lookup_component=.',
' case "$_orca_lookup_component" in',
' /*) _orca_lookup_candidate=$_orca_lookup_component/$_orca_lookup_command ;;',
Expand Down