Files
orca/src/shared/cli-fs-reads.ts
NeilandChad Tolkien b95b11cc6e perf(codex): resolve the Codex CLI once per spawn recheck, off the main thread on Windows (#35967)
Every native terminal spawn schedules a Codex hook reconcile, which resolved
the Codex path twice with a synchronous stat scan of PATH and install dirs
(~50ms main-process block per spawn on Windows with a long PATH and no Codex).

The reconcile now resolves the path once and passes it to both the live
lookup and the saved-answer read. On Windows, where each stat is slow, that
lookup runs every read (PATH candidates, nvm dirs and aliases, install dirs)
through fs/promises; macOS and Linux keep the sync scan, now run once. Sync
and async share one lookup that yields its reads, so they cannot diverge.

Hook rechecks stay immediate on every spawn and nothing is cached. A spawn
that lands before a run's hook-file pass (while it is still finding Codex or
waiting for its answer) gets one trailing rerun, so an install or upgrade
that moved Codex mid-run cannot leave hooks unchecked.

Fixes #35565

Co-authored-by: Chad Tolkien <chad@sodadigital.com.au>
2026-10-29 06:58:26 -07:00

96 lines
2.7 KiB
TypeScript

import { accessSync, constants, readFileSync, readdirSync, statSync } from 'node:fs'
import { access, readdir, readFile, stat } from 'node:fs/promises'
/** One filesystem read a CLI lookup needs; lookups yield these so sync and async callers share one walk. */
export type CliFsRead =
| { kind: 'runnable'; platform: NodeJS.Platform; path: string }
/** A regular file's contents, else null. */
| { kind: 'file'; path: string }
/** Subdirectory names, else null. */
| { kind: 'dirs'; path: string }
export type CliFsReadResult = boolean | string | string[] | null
export type CliFsReads<T> = Generator<CliFsRead, T, CliFsReadResult>
export function isRunnableCommand(platform: NodeJS.Platform, candidate: string): boolean {
try {
if (!statSync(candidate).isFile()) {
return false
}
if (platform === 'win32') {
return true
}
// Skip nonexecutables/placeholders now so GUI probing reaches a runnable CLI instead of later EACCES/EISDIR.
accessSync(candidate, constants.X_OK)
return true
} catch {
return false
}
}
async function isRunnableCommandAsync(
platform: NodeJS.Platform,
candidate: string
): Promise<boolean> {
try {
if (!(await stat(candidate)).isFile()) {
return false
}
if (platform !== 'win32') {
await access(candidate, constants.X_OK)
}
return true
} catch {
return false
}
}
function readSync(read: CliFsRead): CliFsReadResult {
try {
switch (read.kind) {
case 'runnable':
return isRunnableCommand(read.platform, read.path)
case 'file':
return statSync(read.path).isFile() ? readFileSync(read.path, 'utf8') : null
case 'dirs':
return readdirSync(read.path, { withFileTypes: true })
.filter((entry) => entry.isDirectory())
.map((entry) => entry.name)
}
} catch {
return null
}
}
async function readAsync(read: CliFsRead): Promise<CliFsReadResult> {
try {
switch (read.kind) {
case 'runnable':
return await isRunnableCommandAsync(read.platform, read.path)
case 'file':
return (await stat(read.path)).isFile() ? await readFile(read.path, 'utf8') : null
case 'dirs':
return (await readdir(read.path, { withFileTypes: true }))
.filter((entry) => entry.isDirectory())
.map((entry) => entry.name)
}
} catch {
return null
}
}
export function runCliFsReadsSync<T>(reads: CliFsReads<T>): T {
let step = reads.next()
while (!step.done) {
step = reads.next(readSync(step.value))
}
return step.value
}
export async function runCliFsReadsAsync<T>(reads: CliFsReads<T>): Promise<T> {
let step = reads.next()
while (!step.done) {
step = reads.next(await readAsync(step.value))
}
return step.value
}