Skip to content

fix(codex): make Windows hook commands PowerShell-safe - #1585

Merged
SaulMoro merged 2 commits into
Tencent:mainfrom
huong8373tt-beep:fix/codex-powershell-hook-command-1578
Oct 30, 2026
Merged

SaulMoro merged 2 commits into
Tencent:mainfrom
huong8373tt-beep:fix/codex-powershell-hook-command-1578

Conversation

@huong8373tt-beep

@huong8373tt-beep huong8373tt-beep commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #1578.

Codex's Windows hook payload can be executed by an explicitly selected PowerShell session, not only by the %COMSPEC% fallback. The generated commandWindows built-in form previously exposed cmd syntax (& and exit 0) directly to PowerShell, which caused a BackgroundJob table to be written to hook stdout instead of the expected hook response.

This change keeps the portable POSIX command unchanged and wraps the Windows built-in dispatch in an explicit cmd.exe /d /s /c invocation. Team-hook dispatcher commands remain bare so their blocking exit status and stderr continue to reach Codex.

Validation

  • npx.cmd vitest run src/__tests__/hooks.test.ts -t "Codex format" — 3 passed
  • npx.cmd vitest run src/__tests__/builtin-hooks.test.ts src/__tests__/codex-team-hooks.test.ts — 23 passed
  • npm run typecheck — passed
  • npm run lint — 0 warnings / 0 errors
  • npm run build — passed
  • git diff --check — passed
  • Built CLI verification: teamai init --self --agent codex --force generated the expected commandWindows value in a disposable workspace. The unrelated registration push was intentionally pointed at a non-routable fixture remote; local hook generation and assertion completed.

The full provider/agent matrix and a fresh Codex session are left to CI because they require the corresponding host installation.

Disclosure and authorization

Slai Agent Runtime performed the investigation, implementation, and test execution. The account owner authorized this contribution and is responsible for its content.

Real-CLI record (added by a reviewer)

Built CLI, commit 66403882 (merge-base cfd798e4), macOS, Node 24.21.0, PowerShell 7.5.2.

- npm ci && npm run build: exit 0 at head and at the merge-base.
- Fresh git repo with an unreachable origin, isolated HOME, TEAMAI_NONINTERACTIVE=1:
    teamai init --self --agent codex --force --provider git   -> exit 0
  Head writes, for Stop:
    "command": "bash -lc \"teamai hook-dispatch stop --tool codex 2>/dev/null\" || true"
    "commandWindows": "cmd.exe /d /s /c \"teamai hook-dispatch stop --tool codex 2>nul & exit /b 0\""
  The merge-base writes "...2>nul & exit 0" for commandWindows; command is identical.
- Over a file written by the merge-base build: teamai hooks inject (head) -> 7 groups rewritten, no old form left;
  teamai hooks remove (head) -> all 7 removed; teamai doctor reports no member hooks.
- pwsh -NoProfile -Command '<commandWindows>', with a stand-in cmd.exe:
    merge-base string -> BackgroundJob table on stdout, rc 0
    head string      -> hook output only, rc 0
Windows was not run; cmd.exe and Windows PowerShell 5.1 are covered by reasoning only.

@huong8373tt-beep

Copy link
Copy Markdown
Contributor Author

The PR is now open as a Draft. The implementation keeps the POSIX command unchanged, makes Codex's generated Windows built-in command safe when the selected shell is PowerShell, and leaves team-hook dispatcher commands bare so blocking exit codes/stderr are preserved.

The local built-CLI check generated the expected commandWindows value in a disposable self-mode workspace. The fixture remote was intentionally non-routable, so its unrelated registration push was not part of the validation.

@github-actions

Copy link
Copy Markdown

No findings in the specified PR diff.

The PR description meets the testing requirement: it records a build and a representative real-CLI run verifying the generated Codex commandWindows value. Deferring the full provider/agent matrix and a fresh Codex session to CI is not blocking.

Review was read-only; I did not run, build, or install anything from the PR.

@huong8373tt-beep

Copy link
Copy Markdown
Contributor Author

Updated the stale Codex retired-Skill fixture and its reconciliation expectation in 66403882 so they assert the new PowerShell-safe commandWindows wrapper rather than the previous bare cmd syntax.

Validation at this head:

  • focused hook tests: 6 passed
  • npm run typecheck
  • npm run lint (0 warnings / 0 errors)
  • npm run build
  • git diff --cached --check

The full CI matrix has been retriggered by the push.

@SaulMoro
SaulMoro marked this pull request as ready for review October 30, 2026 13:47
@github-actions

Copy link
Copy Markdown

No findings in the specified PR diff.

The PR description meets the testing requirement: it records a successful build and a representative real-CLI run verifying the generated Codex commandWindows value. Deferring the full provider/agent matrix and a fresh Codex session to CI is not blocking.

The earlier pass reported no findings; there are none to mark resolved. Review was read-only—I did not run, build, or install anything from the PR.

@SaulMoro SaulMoro left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This was generated by AI during triage.

Thanks, this fixes #1578. Checked with the built CLI and PowerShell 7.5.2 (on macOS, with a stand-in cmd.exe): the reporter's probe and main's commandWindows string both print a BackgroundJob table under pwsh -NoProfile -Command, and the new cmd.exe /d /s /c "... & exit /b 0" string prints only the hook's output, exit 0. teamai hooks inject rewrites an old-form .codex/hooks.json with no duplicates, hooks remove still recognizes the old entries as teamai's, and Claude and CodeBuddy output is unchanged. The POSIX command is unchanged. CI is green and the CI review reported no findings; I agree. I added that record to the body. Windows itself was not run: the cmd.exe side is reasoned from Codex 0.166.0's runner, which passes the string to cmd unescaped.

Non-blocking:

  • [P2 non-blocking] The new comment says the bare team dispatcher keeps the blocking contract. Under an explicit PowerShell shell, pwsh -Command turns a child's exit 2 into exit 1, so a blocking team hook reaches Codex as a plain failure. That was already the case before this PR; the comment should say the contract holds under cmd.
  • [P3 nit] codex-internal and tcodex share the Codex format and get the wrapper too; the tests cover only codex.
  • [P3 nit] The currentCodexWindows test helper re-derives the expected string instead of asserting the literal.

Approving. Merges once CI is green at this head.

@SaulMoro
SaulMoro merged commit 0a8230d into Tencent:main Oct 30, 2026
20 checks passed
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
Codex 0.166.0 on Windows can run `commandWindows` through the session's PowerShell instead of cmd.exe. The built-in form `... 2>nul & exit 0` then started a PowerShell background job and printed its table to the hook's stdout, so Stop returned invalid JSON. The built-in Windows dispatch is now wrapped in `cmd.exe /d /s /c`; team-hook dispatchers and the POSIX `command` are unchanged.

Closes Tencent#1578
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.

[bug] Codex 0.166.0 on Windows: cmd-style hooks emit BackgroundJob output when executed through PowerShell

3 participants