From c782cfff133647ab56ae9e7dbbbed3eceed48193 Mon Sep 17 00:00:00 2001 From: cafitac Date: Mon, 27 Apr 2026 11:09:47 +0900 Subject: [PATCH] feat: add codex hook format and install_codex_hooks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - _make_hook_entry: hook_format="codex" → {"hooks": [...]} (no matcher) - codex.py: install_codex_hooks(project_root) writes Stop hook to .codex/hooks.json - Mirrors install_claude_hooks pattern Co-Authored-By: Claude Sonnet 4.6 --- src/agent_learner/adapters/codex.py | 19 ++++++++++++++++++- src/agent_learner/adapters/common.py | 4 ++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/agent_learner/adapters/codex.py b/src/agent_learner/adapters/codex.py index c3f74c4..b5f5660 100644 --- a/src/agent_learner/adapters/codex.py +++ b/src/agent_learner/adapters/codex.py @@ -3,7 +3,7 @@ import shlex from pathlib import Path -from .common import append_lines_if_missing, ensure_dir, merge_json_file, write_text +from .common import append_lines_if_missing, ensure_dir, merge_json_file, upsert_hook, write_text from agent_learner.core.storage import migrate_legacy_learning_assets @@ -336,3 +336,20 @@ def install_codex_adapter_with_scope(target_root: Path, *, scope: str = "project if scope == "project": written.extend(migrate_legacy_learning_assets(target_root)) return written + + +# --------------------------------------------------------------------------- +# Phase 2: lightweight hook installer (mirrors claude adapter pattern) +# --------------------------------------------------------------------------- + +_CODEX_HOOK_COMMAND = "agent-learner process --adapter codex --auto" + + +def install_codex_hooks(project_root: Path, *, scope: str = "project") -> Path: + """Install agent-learner Stop hook into .codex/hooks.json. + + Idempotent: updates existing agent-learner hook if present. + Preserves other Stop hooks. + """ + settings_path = project_root / ".codex" / "hooks.json" + return upsert_hook(settings_path, "Stop", _CODEX_HOOK_COMMAND, hook_format="codex") diff --git a/src/agent_learner/adapters/common.py b/src/agent_learner/adapters/common.py index 535a46b..8411ffe 100644 --- a/src/agent_learner/adapters/common.py +++ b/src/agent_learner/adapters/common.py @@ -79,6 +79,10 @@ def _make_hook_entry(command: str, *, hook_format: str) -> dict: "matcher": ".*", "hooks": [{"type": "command", "command": command}], } + if hook_format == "codex": + return { + "hooks": [{"type": "command", "command": command}], + } return {"command": command}