From 50b0c505ff2332174ecc72351f4c707bb71bc5a3 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Thu, 17 Sep 2026 23:39:32 +0100 Subject: [PATCH 1/2] feat: register Codex safety hooks --- .codex/hooks.json | 27 ++++++++++++++ AGENTS.md | 7 ++++ bin/mind_commit_guard.py | 7 +++- tests/test_mind_commit_guard.py | 66 +++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 .codex/hooks.json diff --git a/.codex/hooks.json b/.codex/hooks.json new file mode 100644 index 00000000..5a03f049 --- /dev/null +++ b/.codex/hooks.json @@ -0,0 +1,27 @@ +{ + "description": "Generated by PyAutoMind/scripts/repos_sync.py. Review and trust the current hook definition with Codex /hooks before relying on it.", + "hooks": { + "PreToolUse": [ + { + "matcher": "Bash", + "hooks": [ + { + "type": "command", + "command": "python3 \"$(git rev-parse --show-toplevel)/bin/mind_commit_guard.py\"", + "statusMessage": "Checking shared PyAutoMind commit safety" + } + ] + }, + { + "matcher": "^(send_later|subscribe_pr_activity|ScheduleWakeup|CronCreate|RemoteTrigger|mcp__.*(send_later|subscribe_pr_activity).*)$", + "hooks": [ + { + "type": "command", + "command": "bash \"$(git rev-parse --show-toplevel)/.claude/hooks/end-at-deliverable.sh\"", + "statusMessage": "Enforcing the end-at-deliverable policy" + } + ] + } + ] + } +} diff --git a/AGENTS.md b/AGENTS.md index 4e822dfb..8e37cb34 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -263,3 +263,10 @@ outlives the turn to wait for CI, a review or a merge: no `send_later`, no armed hourly check-ins on 2026-08-31, and a mobile `/prm` re-armed a 60-minute `send_later` hourly all night on 2026-09-03 with no task active, draining usage. + +Codex loads this repo's generated safety registrations from `.codex/hooks.json` +only after the project layer and exact current hook hash are reviewed and trusted +with `/hooks`; changed or untrusted hooks are skipped. The adapter registers the +shared-Mind commit guard and end-at-deliverable guard. It intentionally does not +copy the Claude remote-session Python `SessionStart` bootstrap; local Codex work +continues to source the workspace `activate.sh` normally. diff --git a/bin/mind_commit_guard.py b/bin/mind_commit_guard.py index f99a9de9..d1a4a360 100644 --- a/bin/mind_commit_guard.py +++ b/bin/mind_commit_guard.py @@ -248,9 +248,14 @@ def main() -> None: payload = json.loads(raw) except (json.JSONDecodeError, ValueError): _allow() + if not isinstance(payload, dict): + _allow() if payload.get("tool_name") != "Bash": _allow() - command = (payload.get("tool_input") or {}).get("command") or "" + tool_input = payload.get("tool_input") or {} + if not isinstance(tool_input, dict): + _allow() + command = tool_input.get("command") or "" cwd = payload.get("cwd") or "" try: reason = check_command(command, cwd) diff --git a/tests/test_mind_commit_guard.py b/tests/test_mind_commit_guard.py index 26ff1064..eff8bc5a 100644 --- a/tests/test_mind_commit_guard.py +++ b/tests/test_mind_commit_guard.py @@ -7,13 +7,25 @@ from __future__ import annotations +import json +import subprocess import sys from pathlib import Path +import pytest + sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "bin")) from mind_commit_guard import check_command # noqa: E402 MIND = "/home/jammy/Code/PyAutoLabs/PyAutoMind" +HOOK = Path(__file__).resolve().parents[1] / "bin" / "mind_commit_guard.py" + + +def run_codex_hook(payload): + body = payload if isinstance(payload, str) else json.dumps(payload) + return subprocess.run( + [sys.executable, str(HOOK)], input=body, capture_output=True, text=True + ) def test_bare_commit_in_mind_denied(): @@ -194,3 +206,57 @@ def test_simple_shapes_still_denied_after_v13(tmp_path): (mind / "active").mkdir(parents=True) assert check_command(f'cd {mind} && git commit -m "m"') is not None assert check_command(f'git -C {mind} commit -m "m" -- active/') is not None + + +def test_codex_pre_tool_use_payload_denies_unsafe_mind_commit(tmp_path): + mind = tmp_path / "PyAutoMind" + mind.mkdir() + result = run_codex_hook({ + "session_id": "codex-session", + "turn_id": "turn-1", + "tool_name": "Bash", + "tool_use_id": "call-1", + "cwd": str(mind), + "hook_event_name": "PreToolUse", + "tool_input": {"command": 'git commit -m "unsafe"'}, + }) + assert result.returncode == 0 + decision = json.loads(result.stdout)["hookSpecificOutput"] + assert decision["hookEventName"] == "PreToolUse" + assert decision["permissionDecision"] == "deny" + + +def test_codex_pre_tool_use_payload_allows_safe_mind_commit(tmp_path): + mind = tmp_path / "PyAutoMind" + mind.mkdir() + (mind / "active.md").write_text("task\n") + result = run_codex_hook({ + "turn_id": "turn-2", + "tool_name": "Bash", + "cwd": str(mind), + "hook_event_name": "PreToolUse", + "tool_input": { + "command": 'git commit -m "safe" -- active.md' + }, + }) + assert result.returncode == 0 + assert result.stdout == "" + + +@pytest.mark.parametrize("payload", ["not-json", "[]", '{"tool_name":"Bash","tool_input":7}']) +def test_codex_malformed_payload_fails_open_without_crashing(payload): + result = run_codex_hook(payload) + assert result.returncode == 0 + assert result.stdout == "" + assert result.stderr == "" + + +def test_codex_project_config_registers_mind_and_deliverable_guards(): + root = Path(__file__).resolve().parents[1] + config = json.loads((root / ".codex" / "hooks.json").read_text()) + groups = config["hooks"]["PreToolUse"] + commands = [group["hooks"][0]["command"] for group in groups] + assert [group["matcher"] for group in groups][0] == "Bash" + assert all("git rev-parse --show-toplevel" in command for command in commands) + assert any("bin/mind_commit_guard.py" in command for command in commands) + assert any("end-at-deliverable.sh" in command for command in commands) From 5bb3946e15df0bb38e966cf44bf060502b9351ee Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Thu, 17 Sep 2026 23:53:09 +0100 Subject: [PATCH 2/2] fix: classify Codex hooks as clone infrastructure --- agents/conductors/clone/_clone.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/agents/conductors/clone/_clone.py b/agents/conductors/clone/_clone.py index 6e4dc306..78d49680 100644 --- a/agents/conductors/clone/_clone.py +++ b/agents/conductors/clone/_clone.py @@ -92,8 +92,8 @@ ".github/*", # wiki-currency / citation workflows "wiki/README.md", "wiki/project/*", # project wiki rules + profile template "scripts/AGENTS.md", "scripts/CLAUDE.md", "scripts/README.md", - # Harness mirrors of the generic machinery (.claude/, .gemini/): - ".claude/hooks/*", ".claude/settings.json", ".gemini/*", + # Harness mirrors of the generic machinery (.claude/, .codex/, .gemini/): + ".claude/hooks/*", ".claude/settings.json", ".codex/hooks.json", ".gemini/*", ".claude/skills/_*", ".claude/skills/start-new-project*", ".claude/skills/contribute-upstream*", ]