Skip to content

fix(cli): Fix Codex CLI execution issue in PowerShell with Hapi Codex#763

Merged
tiann merged 2 commits into
tiann:mainfrom
xuansheep:fix/codex-powershell-command-use
Jun 1, 2026
Merged

fix(cli): Fix Codex CLI execution issue in PowerShell with Hapi Codex#763
tiann merged 2 commits into
tiann:mainfrom
xuansheep:fix/codex-powershell-command-use

Conversation

@xuansheep
Copy link
Copy Markdown
Contributor

关联Issue: #762

本次提交代码使用 codex cli + gpt5.5 xhigh(plan)与high(implement)生成

改动摘要:

  • 新增 Codex 可执行文件解析逻辑,Windows 下优先解析 PATH 中的 codex,并识别 npm shim 对应的原生 codex.exe
  • 当 Windows shim 找不到原生 exe 时,回退为 node + codex.js,避免直接执行 shim 导致参数转义异常
  • codexLocal 和 codexVersion 统一使用同一套 Codex 命令解析结果,保证版本检查与实际启动行为一致
  • 更新 codexLocal、codexVersion、codexExecutable 相关测试,覆盖 Windows shim、原生 exe、node fallback 和非 Windows 场景

本地运行结果:
image

启动日志:
[11:38:28.890] Starting hapi CLI with args: ["bun","B:/~BUN/root/hapi.exe","codex"]
[11:38:29.444] [AUTO-START] Starting hub automatically...
[11:38:29.446] [SPAWN HAPI CLI] Spawning: hapi hub in D:*\hapi
[11:38:29.455] [AUTO-START] Hub process spawned with PID 35696
[11:38:30.296] [AUTO-START] Server ready after 840ms
[11:38:30.304] [codex] Starting with options: startedBy=terminal
[11:38:30.306] Using machineId: 2e446020-
---6fcfcb971c79
[11:38:30.449] [WORKTREE] Git probe miss in 123ms
[11:38:30.461] [START] Reporting session 08b621fa---***a8fcfd0561d1 to runner
[11:38:30.465] [CONTROL CLIENT] Runner is not running, file is stale
[11:38:30.466] [START] Failed to report to runner (may not be running): Runner is not running, file is stale
[11:38:30.467] [MessageQueue2] Initialized
[11:38:30.470] [Codex] Synced session config for keepalive: permissionMode=default, model=auto, modelReasoningEffort=default, collaborationMode=default
[11:38:30.471] [codex-loop] Iteration with mode: local
[11:38:30.487] Socket connected successfully
[11:38:30.508] [codex-local]: Started hapi MCP bridge server at http://127.0.0.1:59083/
[11:38:30.539] [hookServer] Started on port 59084
[11:38:30.540] [codex-local]: Started Codex SessionStart hook server on port 59084
[11:38:30.542] [codex-local]: launch
[11:38:30.544] [CodexLocal] Spawning codex with args: ["-c","mcp_servers.hapi.command="D:\\work\\code\\nodeJs\\hapi\\cli\\dist-exe\\bun-windows-x64\\hapi.exe"","-c","mcp_servers.hapi.args=['mcp','--url','http://127.0.0.1:59083/']","-c","hooks.SessionStart=[{ hooks = [{ type = "command", command = "\"D:\\\\work\\\\code\\\\nodeJs\\\\hapi\\\\cli\\\\dist-exe\\\\bun-windows-x64\\\\hapi.exe\" hook-forwarder --port 59084 --token 08d957975f31a374" }] }]","-c","hooks.state={"C:\\\\config.toml:session_start:0:0"={trusted_hash="sha256:f4255c0
2"}}","-c","developer_instructions="ALWAYS when you start a new chat, call the title tool to set a concise task title.\nPrefer calling functions.hapi__change_title.\nIf that exact tool name is unavailable, call an equivalent alias such as hapi__change_title, mcp__hapi__change_title, or hapi_change_title.\nIf the task focus changes significantly later, call the title tool again with a better title.\nWhen you create or find a local image file that the user should see, call functions.hapi__display_image with the image path. If that exact tool name is unavailable, use an equivalent alias such as hapi__display_image, mcp__hapi__display_image, or hapi_display_image.""]

可以成功运行codex cli

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Findings

  • [Major] 直接启动原生 codex.exe 会绕过 Codex launcher 的运行环境设置,证据 cli/src/codex/utils/codexExecutable.ts:73。当前 resolver 对 npm shim 优先返回 native exe,但 @openai/codex/bin/codex.js launcher 会在启动 native binary 前补充 bundled vendor/.../codex-pathPATH,并设置 CODEX_MANAGED_PACKAGE_ROOT/包管理器标记;绕过它后,Windows 用户可能能启动 Codex,但在依赖 bundled rg.exe 或资源路径时出现回归。
    Suggested fix:
    function resolveWindowsCandidate(candidate: string): CodexCommand | null {
        if (!existsSync(candidate)) {
            return null;
        }
    
        if (windowsPath.extname(candidate).toLowerCase() === '.exe') {
            return { command: candidate, args: [] };
        }
    
        const script = resolveShimScript(candidate);
        if (script) {
            return { command: 'node', args: [script] };
        }
    
        return null;
    }

Summary

  • Review mode: initial
  • 发现 1 个 Major 问题:Windows shim 路径绕过 Codex 官方 JS launcher,丢失其运行环境设置。建议对 shim 使用 node codex.js ...args,仍可避免 .cmd/PowerShell 参数转义问题,同时保留 Codex 自己的 PATH/resource 初始化。

Testing

  • Not run (automation; 当前 runner 中 bun 不可用:bun: command not found)

HAPI Bot

Comment thread cli/src/codex/utils/codexExecutable.ts Outdated

const nativeExecutable = resolveNativePackageExecutable(candidate);
if (nativeExecutable) {
return { command: nativeExecutable, args: [] };
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[Major] 这里把 npm shim 解析成原生 codex.exe 后,codexLocal 会直接用当前 process.env 启动它。这样绕过了 @openai/codex/bin/codex.js launcher;该 launcher 会把 bundled vendor/.../codex-path 加进 PATH,并设置 CODEX_MANAGED_PACKAGE_ROOT/包管理器标记。缺少这层环境后,Windows 用户可能能启动 Codex,但在依赖 bundled rg.exe 或资源路径时出现回归。

Suggested fix:

function resolveWindowsCandidate(candidate: string): CodexCommand | null {
    if (!existsSync(candidate)) {
        return null;
    }

    if (windowsPath.extname(candidate).toLowerCase() === '.exe') {
        return { command: candidate, args: [] };
    }

    const script = resolveShimScript(candidate);
    if (script) {
        return { command: 'node', args: [script] };
    }

    return null;
}

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Findings

  • None.

Summary

  • Review mode: follow-up after new commits
  • No blocking issues found in the updated diff. The prior launcher-bypass finding was addressed by resolving Windows npm shims to node codex.js, and the same resolved command is now used for the version check and local launch path.
  • Residual risk: Windows behavior was reviewed from source/tests only; this runner cannot execute the targeted Bun/Vitest suite.

Testing

  • Not run (automation; bun is not installed in this runner: /bin/bash: bun: command not found)

HAPI Bot

@tiann tiann merged commit d78cf4b into tiann:main Jun 1, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants