From a1c191d58d7a43475546dd94b18b1b21d99dbb7b Mon Sep 17 00:00:00 2001 From: daniel <4980827+daniel-bavrin@users.noreply.github.com> Date: Wed, 18 Nov 2026 21:02:58 +0000 Subject: [PATCH] fix(codex): preserve Windows hook exit codes Run Windows hook interpreters with subprocess so the inline launcher code and child status survive. Forward decision and dependency gate statuses through PowerShell and cover both handoffs with regressions. --- CHANGELOG.md | 4 + docs/codex-plugin.md | 7 ++ .../hypermnesia-mcp-codex/hooks/hooks.json | 6 +- .../hypermnesia-mcp-codex/scripts/runtime.py | 6 +- .../test_codex_plugin_hooks_contract.py | 11 +++ tests_py/scripts/test_codex_runtime.py | 74 +++++++++++++++++-- 6 files changed, 98 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index beece8703..df4965483 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -592,6 +592,10 @@ adheres to [Semantic Versioning](https://semver.org/). longer reached the managed server. A failed backend switch now restores the earlier marker byte for byte instead of rebuilding it from its backend name, which wrote a `managed` marker without `root` that `read_backend_marker` rejects (review of #1029). +- Windows Codex plugin hooks now preserve command arguments and the wheel's exit + status. The two blocking `PreToolUse` hooks preserve exit code 2 through + PowerShell so Codex stops the tool call. The SessionEnd worker can now distinguish + a failed handler from a recorded session. - Docker (issue #704): `docker/entrypoint.sh` no longer gives `CLAUDE_CODE_OAUTH_TOKEN` to `python3 -c` as `sys.argv[1]`, where every local user could read it; the step reads the token from the environment. A missing `DATABASE_URL` now fails the start at an diff --git a/docs/codex-plugin.md b/docs/codex-plugin.md index e2484ff67..6d278993b 100644 --- a/docs/codex-plugin.md +++ b/docs/codex-plugin.md @@ -37,6 +37,13 @@ the hook module and wires the configured backend. SessionEnd first persists its event using the bundled standard-library queue, then invokes this same runtime in a replayable worker. No repository module can shadow the wheel. +On Windows, the dispatcher waits for the hook interpreter and returns its exit +status with stdin, stdout, and stderr inherited. The two blocking +`PreToolUse` hooks use Codex's `commandWindows` override to forward that status +through PowerShell with `exit $LASTEXITCODE`. This preserves the exit code that +[Codex uses to block the tool call](https://learn.chatgpt.com/docs/hooks#pretooluse) +when [PowerShell would otherwise return 1](https://github.com/openai/codex/issues/54072). + ## Event names and matchers Event names are Codex's own, verified against diff --git a/plugins/hypermnesia-mcp-codex/hooks/hooks.json b/plugins/hypermnesia-mcp-codex/hooks/hooks.json index d39567cfe..363db16ae 100644 --- a/plugins/hypermnesia-mcp-codex/hooks/hooks.json +++ b/plugins/hypermnesia-mcp-codex/hooks/hooks.json @@ -6,11 +6,13 @@ "hooks": [ { "type": "command", - "command": "python3 \"${PLUGIN_ROOT}/scripts/runtime.py\" decision_gate" + "command": "python3 \"${PLUGIN_ROOT}/scripts/runtime.py\" decision_gate", + "commandWindows": "python3 \"${PLUGIN_ROOT}/scripts/runtime.py\" decision_gate; exit $LASTEXITCODE" }, { "type": "command", - "command": "python3 \"${PLUGIN_ROOT}/scripts/runtime.py\" no_deps_gate" + "command": "python3 \"${PLUGIN_ROOT}/scripts/runtime.py\" no_deps_gate", + "commandWindows": "python3 \"${PLUGIN_ROOT}/scripts/runtime.py\" no_deps_gate; exit $LASTEXITCODE" } ] }, diff --git a/plugins/hypermnesia-mcp-codex/scripts/runtime.py b/plugins/hypermnesia-mcp-codex/scripts/runtime.py index 897248fe3..891258615 100644 --- a/plugins/hypermnesia-mcp-codex/scripts/runtime.py +++ b/plugins/hypermnesia-mcp-codex/scripts/runtime.py @@ -76,7 +76,11 @@ def main() -> None: os.execv(str(python), [str(python), "-I", "-m", "mcp_server", *arguments[1:]]) return command = "from mcp_server.hooks.entry import main; main()" - os.execv(str(python), [str(python), "-I", "-c", command, module]) + argv = [str(python), "-I", "-c", command, module] + if os.name == "nt": + # Windows execv splits the -c program and can mask the child failure. + raise SystemExit(subprocess.run(argv, check=False).returncode) + os.execv(str(python), argv) if __name__ == "__main__": diff --git a/tests_py/scripts/test_codex_plugin_hooks_contract.py b/tests_py/scripts/test_codex_plugin_hooks_contract.py index 8c2f06ab6..9383be07a 100644 --- a/tests_py/scripts/test_codex_plugin_hooks_contract.py +++ b/tests_py/scripts/test_codex_plugin_hooks_contract.py @@ -70,6 +70,17 @@ def test_codex_hook_commands_run_the_published_wheel_not_this_repository() -> No assert "uvx" not in command +def test_only_blocking_gates_relay_their_exit_codes_in_windows_powershell() -> None: + hooks = codex_hooks() + index = hook_index(hooks, CODEX_SUFFIX) + # source: https://github.com/openai/codex/issues/54072 + gates = [index[module][2] for module in ("decision_gate", "no_deps_gate")] + for hook in gates: + assert hook["commandWindows"] == hook["command"] + "; exit $LASTEXITCODE" + windows_hooks = [h for _, _, h in every_hook(hooks) if "commandWindows" in h] + assert windows_hooks == gates + + def test_codex_hooks_use_only_codex_event_names() -> None: """Codex has no Notification event; PreCompact is where a checkpoint taken before compaction belongs, which is compaction_checkpoint's own contract. diff --git a/tests_py/scripts/test_codex_runtime.py b/tests_py/scripts/test_codex_runtime.py index 44b961d10..d90ce9621 100644 --- a/tests_py/scripts/test_codex_runtime.py +++ b/tests_py/scripts/test_codex_runtime.py @@ -1,8 +1,11 @@ """No event-time dependency resolution. Source: native failure, 2026-10-02.""" import importlib.util +import json import os from pathlib import Path +import subprocess +import sys from unittest.mock import patch import pytest @@ -42,7 +45,7 @@ def test_wrong_version_refuses_stale_tool(tmp_path): runtime.installed_python("4.23.5") -def test_matching_runtime_execs_without_install_or_resolve(tmp_path): +def test_matching_runtime_runs_without_install_or_resolve(tmp_path): python = installed(tmp_path, "4.24.0") with ( patch.object(runtime.sys, "argv", [str(SCRIPT), "post_tool_capture"]), @@ -50,12 +53,69 @@ def test_matching_runtime_execs_without_install_or_resolve(tmp_path): patch.object(runtime.subprocess, "run") as run, patch.object(runtime.os, "execv") as execute, ): - runtime.main() - run.assert_not_called() - execute.assert_called_once() - assert execute.call_args.args[0] == str(python) - assert execute.call_args.args[1][-1] == "post_tool_capture" - assert execute.call_args.args[1][1] == "-I" + if os.name == "nt": + run.return_value.returncode = 0 + with pytest.raises(SystemExit) as result: + runtime.main() + assert result.value.code == 0 + execute.assert_not_called() + run.assert_called_once() + argv = run.call_args.args[0] + else: + runtime.main() + run.assert_not_called() + execute.assert_called_once() + assert execute.call_args.args[0] == str(python) + argv = execute.call_args.args[1] + assert argv[:2] == [str(python), "-I"] + assert argv[-1] == "post_tool_capture" + + +# Substitute the prepared interpreter lookup, then run the real wheel entry. +HOOK_BOOTSTRAP = """ +import runpy +import sys +from pathlib import Path + +sys.argv = sys.argv[1:] +main = runpy.run_path(sys.argv[0])["main"] +main.__globals__["installed_python"] = lambda _: Path(sys.executable) +main() +""" + + +@pytest.mark.parametrize(("flags", "exit_code"), [((), 2), (("--no-deps",), 0)]) +def test_hook_dispatch_preserves_stdin_and_exit_code(tmp_path, flags, exit_code): + command = " ".join( + ("pip", "install", "--require-hashes", *flags, "-r", "requirements.txt") + ) + event = { + "hook_event_name": "PreToolUse", + "tool_name": "Write", + "tool_input": {"file_path": "scripts/setup.sh", "content": command}, + "cwd": str(tmp_path), + } + env = { + key: value + for key, value in os.environ.items() + if not key.startswith("CORTEX_") and key != "DATABASE_URL" + } + env.update(CORTEX_CLAUDE_DIR=str(tmp_path), CORTEX_MEMORY_STORE_BACKEND="sqlite") + result = subprocess.run( + [sys.executable, "-c", HOOK_BOOTSTRAP, str(SCRIPT), "no_deps_gate"], + input=json.dumps(event), + encoding="utf-8", + capture_output=True, + cwd=tmp_path, + env=env, + timeout=20, + check=False, + ) + assert result.returncode == exit_code, result.stderr + if exit_code: + assert "[no-deps-gate] BLOCKED" in result.stderr + else: + assert result.stderr == "" def test_setup_uses_host_interpreter_and_binary_wheels():