Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions docs/codex-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions plugins/hypermnesia-mcp-codex/hooks/hooks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
},
Expand Down
6 changes: 5 additions & 1 deletion plugins/hypermnesia-mcp-codex/scripts/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
11 changes: 11 additions & 0 deletions tests_py/scripts/test_codex_plugin_hooks_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
74 changes: 67 additions & 7 deletions tests_py/scripts/test_codex_runtime.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -42,20 +45,77 @@ 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"]),
patch.object(runtime.subprocess, "check_output", return_value=str(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():
Expand Down
Loading