Repository navigation
perf(preflight): read the WSL mount table once, and make launch agree with detection #16053
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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,95 @@ 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<number> => { | ||
| 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 <every agent>`. 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) | ||
| }) | ||
|
|
||
| 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 } | ||
|
Comment on lines
+369
to
+375
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Pass the mount path through the environment. Line 371 inserts Proposed fix- `_orca_win_mounts=${join(root, 'winmnt')}`
+ '_orca_win_mounts="$ORCA_TEST_WIN_MOUNT"'
...
- env: { PATH: `${win}:${guest}:/usr/bin:/bin`, HOME: root }
+ env: {
+ PATH: `${win}:${guest}:/usr/bin:/bin`,
+ HOME: root,
+ ORCA_TEST_WIN_MOUNT: join(root, 'winmnt')
+ }As per coding guidelines, commands and scripts must work on macOS, Linux, and Windows. 🧰 Tools🪛 ast-grep (0.45.1)[warning] Importing child_process exposes a command-execution surface; ensure any command/argument built from input is validated, and prefer execFile/spawn with an argument array over exec. (detect-child-process-typescript) Source: Coding guidelines |
||
| } | ||
| 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 }) | ||
| } | ||
| }) | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Pass the counter path through the environment.
Line 306 inserts
counterinto shell source without quoting. IfTMPDIRcontains whitespace, the stub writes to the wrong path and this test reports zero invocations.Proposed fix
As per coding guidelines, commands and scripts must work on macOS, Linux, and Windows.
📝 Committable suggestion
🧰 Tools
🪛 ast-grep (0.45.1)
[warning] Importing child_process exposes a command-execution surface; ensure any command/argument built from input is validated, and prefer execFile/spawn with an argument array over exec.
Context: import { execFileSync, type ExecFileSyncOptions } from 'node:child_process'
Note: [CWE-78] Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection').
(detect-child-process-typescript)
Source: Coding guidelines