Skip to content

Experience-Driven Black-Box Cost Control for Coding Agents

Codex on Windows: give every hook a commandWindows

Aaronb7417d728fbe
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 6cc24b8..5c1fd65 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -65,7 +65,8 @@ jobs:
- run: npm install -g @openai/[email protected]
- run: python -m pip install pytest
# What Codex wrote for the marketplace goes out as a notice, and on failure the summary
- # lines as an annotation, both shown on the check run without signing in.
+ # lines and what Codex said about the hooks as annotations, all shown on the check run
+ # without signing in.
- run: |
set -o pipefail
status=0
@@ -74,6 +75,8 @@ jobs:
grep -h " wrote: " pytest.log | sort -u | sed 's/^/::notice title=codex config.toml::/' \
|| true
if [ "$status" -ne 0 ]; then
+ grep -h '^codex: ' pytest.log | sort -u | head -20 | sed 's/%/%25/g' \
+ | sed 's/^/::warning title=codex output::/' || true
msg=$(grep -E '^(FAILED|ERROR) |\.py:[0-9]+: ' pytest.log | head -60 \
| sed 's/%/%25/g' | awk '{printf "%s%%0A", $0}')
echo "::error title=pytest::${msg}"
diff --git a/plugin/README.md b/plugin/README.md
index a03558c..56fd4dd 100644
--- a/plugin/README.md
+++ b/plugin/README.md
@@ -151,7 +151,11 @@ start, once it has been quiet for ten minutes. `codex exec` sessions are learned
end.
**Update.** `git pull` in the clone, then `codex plugin add tokeneconomizer@tokeneconomizer`
-again to refresh Codex's installed copy (your hook approval is kept).
+again to refresh Codex's installed copy. Your hook approval is kept unless the update changed the
+hooks themselves; then Codex asks again (**Hooks need review**, or `/hooks`).
+
+**On Windows,** Codex runs each hook's `commandWindows` in its native shell rather than `sh`; it
+starts the hook with the `python` on PATH (3.10 or later), which `TE_PYTHON` does not change.
**Uninstall.**
diff --git a/plugin/hooks/codex-hooks.json b/plugin/hooks/codex-hooks.json
index 0010f84..51fe293 100644
--- a/plugin/hooks/codex-hooks.json
+++ b/plugin/hooks/codex-hooks.json
@@ -6,6 +6,7 @@
{
"type": "command",
"command": "\"${PLUGIN_ROOT:-$CLAUDE_PLUGIN_ROOT}/scripts/te-hook\" --host codex",
+ "commandWindows": "python -X --% -u -X utf8 -c e=__import__('os').environ;r=e.get('PLUGIN_ROOT')or(e['CLAUDE_PLUGIN_ROOT']);s=__import__('sys');s.argv[:]=[r+'/scripts/te_bootstrap.py','hook','--host','codex'];__import__('runpy').run_path(s.argv[0],run_name='__main__')",
"timeout": 15
}
]
@@ -17,6 +18,7 @@
{
"type": "command",
"command": "\"${PLUGIN_ROOT:-$CLAUDE_PLUGIN_ROOT}/scripts/te-hook\" --host codex",
+ "commandWindows": "python -X --% -u -X utf8 -c e=__import__('os').environ;r=e.get('PLUGIN_ROOT')or(e['CLAUDE_PLUGIN_ROOT']);s=__import__('sys');s.argv[:]=[r+'/scripts/te_bootstrap.py','hook','--host','codex'];__import__('runpy').run_path(s.argv[0],run_name='__main__')",
"timeout": 10
}
]
@@ -28,6 +30,7 @@
{
"type": "command",
"command": "\"${PLUGIN_ROOT:-$CLAUDE_PLUGIN_ROOT}/scripts/te-hook\" --host codex",
+ "commandWindows": "python -X --% -u -X utf8 -c e=__import__('os').environ;r=e.get('PLUGIN_ROOT')or(e['CLAUDE_PLUGIN_ROOT']);s=__import__('sys');s.argv[:]=[r+'/scripts/te_bootstrap.py','hook','--host','codex'];__import__('runpy').run_path(s.argv[0],run_name='__main__')",
"timeout": 60
}
]
@@ -39,6 +42,7 @@
{
"type": "command",
"command": "\"${PLUGIN_ROOT:-$CLAUDE_PLUGIN_ROOT}/scripts/te-hook\" --host codex",
+ "commandWindows": "python -X --% -u -X utf8 -c e=__import__('os').environ;r=e.get('PLUGIN_ROOT')or(e['CLAUDE_PLUGIN_ROOT']);s=__import__('sys');s.argv[:]=[r+'/scripts/te_bootstrap.py','hook','--host','codex'];__import__('runpy').run_path(s.argv[0],run_name='__main__')",
"timeout": 10
}
]
@@ -50,6 +54,7 @@
{
"type": "command",
"command": "\"${PLUGIN_ROOT:-$CLAUDE_PLUGIN_ROOT}/scripts/te-hook\" --host codex",
+ "commandWindows": "python -X --% -u -X utf8 -c e=__import__('os').environ;r=e.get('PLUGIN_ROOT')or(e['CLAUDE_PLUGIN_ROOT']);s=__import__('sys');s.argv[:]=[r+'/scripts/te_bootstrap.py','hook','--host','codex'];__import__('runpy').run_path(s.argv[0],run_name='__main__')",
"timeout": 3
}
]
diff --git a/tests/test_codex_cli.py b/tests/test_codex_cli.py
index d99f820..7f581b0 100644
--- a/tests/test_codex_cli.py
+++ b/tests/test_codex_cli.py
@@ -142,6 +142,10 @@ def test_a_codex_session_runs_the_hooks_and_is_learned_from(tmp_path, model):
# hooks, which is what the flag is for.
out = _codex(env, "exec", "--dangerously-bypass-hook-trust", "--skip-git-repo-check",
"Say hi.", cwd=str(proj), timeout=180)
+ # What Codex said about the hooks, for CI to show when this fails.
+ for line in (out.stdout + out.stderr).splitlines():
+ if "hook" in line.lower():
+ print(f"codex: {line}")
assert ("POST", "/v1/responses") in _Model.requests, (out.stdout[-1000:], out.stderr[-2000:])
root = env["TOKENECONOMIZER_HOME"]
diff --git a/tests/test_plugin_package.py b/tests/test_plugin_package.py
index d9b64d3..948ae35 100644
--- a/tests/test_plugin_package.py
+++ b/tests/test_plugin_package.py
@@ -66,8 +66,45 @@ def test_hook_files_run_the_launcher_on_every_event(name, host):
assert f"--host {host}" in h["command"]
if host == "copilot":
assert h["command"].startswith('"${PLUGIN_ROOT}/scripts/te-hook"')
- if event == "SessionEnd" and host == "codex":
+ if host == "codex":
+ assert h["commandWindows"] == CODEX_WINDOWS
+ if event == "SessionEnd":
assert h["timeout"] <= 3 # Codex's cap for SessionEnd
+ else:
+ assert "commandWindows" not in h
+
+
+# Codex on Windows runs a hook's `commandWindows` in a native shell, cmd.exe or PowerShell, where
+# `command`'s sh syntax means nothing (0.164.0). This one reads the same in both: Python finds the
+# plugin in the environment Codex sets, so no path or variable is on the line; the code has no
+# space, double quote, `%` or `$`; and `--%`, PowerShell's stop-parsing token, keeps its `;` and
+# parentheses from PowerShell, while cmd.exe passes it on to Python as `-X --%`, which Python
+# ignores (PowerShell drops it, so there `-X` takes the `-u` instead).
+CODEX_WINDOWS = (
+ "python -X --% -u -X utf8 -c e=__import__('os').environ;r=e.get('PLUGIN_ROOT')"
+ "or(e['CLAUDE_PLUGIN_ROOT']);s=__import__('sys');"
+ "s.argv[:]=[r+'/scripts/te_bootstrap.py','hook','--host','codex'];"
+ "__import__('runpy').run_path(s.argv[0],run_name='__main__')")
+
+
[email protected]("shell", ["cmd", "powershell"])
+def test_codex_windows_command_runs_the_hook_as_either_shell_passes_it(tmp_path, shell):
+ """Each shell's reading of `commandWindows`, as the argv Python receives: cmd.exe passes the
+ line on as it is, PowerShell without its `--%`. Neither has a quote to strip."""
+ argv = CODEX_WINDOWS.split()
+ if shell == "powershell":
+ argv.remove("--%")
+ assert argv[0] == "python"
+ work = tmp_path / "work"
+ work.mkdir()
+ payload = {"hook_event_name": "SessionStart", "session_id": "s", "cwd": str(work),
+ "source": "startup", "turn_id": "t"}
+ p = subprocess.run([sys.executable] + argv[1:], input=json.dumps(payload),
+ capture_output=True, text=True, timeout=60,
+ env=_hook_env(tmp_path, PLUGIN_ROOT=PLUGIN))
+ assert p.returncode == 0 and json.loads(p.stdout) == {}, p.stderr[-1000:]
+ assert list((tmp_path / "home" / "repos").glob("*/codex/sessions/s.json")) # it ran
+ assert not os.path.exists(tmp_path / "home" / "errors.log")
def test_marketplace_lists_the_plugin_directory():