Skip to content

[Other]: (perf) Agent CLI detection blocks the main process for ~1s per run (redundant sync PATH re-scan) #35562

Closed

Description

@ctolkien

Details

On Windows, every agent-detection pass blocks the Electron main process for about 1.1s. It runs synchronous statSync calls over the whole PATH a second time, after an async check has already searched it. Startup triggers it 2–3 times, so the main process is unresponsive for about 3s: no IPC is handled and no PTY data moves.

Evidence

I took a V8 CPU profile of the main process during the ACK-backpressured scenarios in artificial-opencode-terminal-load.spec.ts. This was an unminified e2e build on Windows, with 95 PATH dirs.

  • Agent detection took 15–19% of main-thread time in all three scenarios. PTY ingestion and delivery combined took about 2–5%.
  • Each pass was one continuous block of about 1.1s, and there were 2–3 passes in the first 7s.
  • One missed command costs 95 dirs × 4 names × ~60µs per stat ≈ 23ms. About 45 of the ~55 probe commands miss, which comes to about 1.1s.

Cause

  1. detectAgentCommandsOnHost first checks every probe command asynchronously with isCommandOnPath:
    const pathChecks = await Promise.all(
    commands.map(async (cmd) => ({
    cmd,
    installedOnPath: await isCommandOnPath(cmd)
    }))
    )
  2. That async check searches process PATH merged with the persisted Windows PATH (on POSIX and in WSL guests, the same PATH):
    return isCommandOnLocalPath(command, { env: buildLocalPreflightEnv() })

    const env = stringOnlyProcessEnv(process.env)
    // The running app must merge persisted Windows PATH to see newly installed CLIs.
    mergePersistedWindowsPath(env)
    return env
  3. Every miss then goes to the install-dir fallback:
    // Resolve cold-GUI install dirs once; use the same WSL-filtered PATH while retaining process PATH elsewhere.
    const guestMounts = readGuestWindowsDriveMounts()
    const installDirCommands = detectCommandsInInstallDirs(
    missedCommands,
    guestMounts.length > 0 ? buildLocalPreflightEnv(guestMounts)?.PATH : undefined
    )

    const resolvedCommands =
    pathEnv === undefined
    ? resolveCliCommands(commands)
    : resolveCliCommands(commands, { pathEnv })
  4. That fallback, resolveCliCommands, re-scans all PATH dirs synchronously before it gets to the install dirs:
    export function resolveCliCommands(
    commandNames: readonly string[],
    options: ResolveCommandOptions = {}
    ): Map<string, string> {
    const platform = options.platform ?? process.platform
    const pathEnv = options.pathEnv ?? process.env.PATH ?? process.env.Path ?? null
    const pathDirectories = splitPath(pathEnv)
    const homePath = options.homePath ?? homedir()
    const installDirectories = getCliInstallDirectories(platform, homePath)
    const resolved = new Map<string, string>()
    for (const commandName of new Set(commandNames)) {
    const executableNames = getExecutableNames(platform, commandName)
    const pathCandidate = findFirstExecutable(platform, pathDirectories, executableNames)
    const installCandidate =
    pathCandidate ?? findFirstExecutable(platform, installDirectories, executableNames)
    resolved.set(commandName, installCandidate ?? commandName)
    }

    function isRunnableCommand(platform: NodeJS.Platform, candidate: string): boolean {
    try {
    const stats = statSync(candidate)
    if (!stats.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
    }
    }
  5. The main process has no in-flight sharing for preflight:detectAgents:
    ipcMain.handle('preflight:detectAgents', async (_event, args?: AgentDetectionRequest) =>
    detectAgentsOnHost({ kind: 'local', context: args }, detectionOptions(args))
    )

    The renderer de-duplicates per context key, but "enabled only" and "include disabled" use different keys, and the skills panel makes its own request:
    const pending = window.api.preflight
    .detectAgents(includeDisabled ? context : { ...context, enabledOnly: true })

Mostly impacts Windows users with long PATHs (evidently, me). On macOS and Linux the extra scan is much cheaper (shorter PATH, faster stat), but the work is just as redundant there.

Activity

  1. ctolkien commented on Oct 29, 2026

    @ctolkien
    ContributorAuthor

    Fixed upstream by #34636 (6af1ef0). Re-profiled the same scenarios on f5c84ea: agent-detection main-thread blocking dropped from ~3.3s to ~0.35s per window, and the longest block went from ~1.1s to ~250ms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions