Add graphify PreToolUse hook to .codex/hooks.json. A project-scoped install emits the bare command, since .codex/hooks.json is then committed and an installing machine's path is wrong there (#3129).
(project_dir: Path, project: bool = False)
| 2051 | break |
| 2052 | return (found or "graphify").replace("\\", "/") |
| 2053 | def _install_codex_hook(project_dir: Path, project: bool = False) -> None: |
| 2054 | """Add graphify PreToolUse hook to .codex/hooks.json. |
| 2055 | |
| 2056 | A project-scoped install emits the bare command, since .codex/hooks.json is |
| 2057 | then committed and an installing machine's path is wrong there (#3129). |
| 2058 | """ |
| 2059 | hooks_path = project_dir / ".codex" / "hooks.json" |
| 2060 | hooks_path.parent.mkdir(parents=True, exist_ok=True) |
| 2061 | |
| 2062 | existing = _read_settings_for_merge(hooks_path) |
| 2063 | |
| 2064 | graphify_exe = _resolve_graphify_exe(project=project) |
| 2065 | if " " in graphify_exe and not graphify_exe.startswith('"'): |
| 2066 | # Mirror _claude_pretooluse_hooks/_gemini_hook: a resolved path with |
| 2067 | # spaces must be quoted, or every shell that runs the hook command |
| 2068 | # (Git Bash, cmd.exe, sh) word-splits it at the first space and the |
| 2069 | # hook can never fire. |
| 2070 | graphify_exe = f'"{graphify_exe}"' |
| 2071 | hook_entry = { |
| 2072 | "hooks": { |
| 2073 | "PreToolUse": [ |
| 2074 | { |
| 2075 | "matcher": "Bash", |
| 2076 | "hooks": [{"type": "command", "command": f"{graphify_exe} hook-check"}], |
| 2077 | } |
| 2078 | ] |
| 2079 | } |
| 2080 | } |
| 2081 | |
| 2082 | hooks = existing.setdefault("hooks", {}) |
| 2083 | if not isinstance(hooks, dict): |
| 2084 | _refuse_to_modify(hooks_path) |
| 2085 | pre_tool = hooks.setdefault("PreToolUse", []) |
| 2086 | if not isinstance(pre_tool, list): |
| 2087 | _refuse_to_modify(hooks_path) |
| 2088 | hooks["PreToolUse"] = _strip_graphify_hooks(pre_tool) |
| 2089 | hooks["PreToolUse"].extend(hook_entry["hooks"]["PreToolUse"]) |
| 2090 | _write_settings_with_backup(hooks_path, existing) |
| 2091 | print( |
| 2092 | f" .codex/hooks.json -> PreToolUse hook registered ({graphify_exe} hook-check" |
| 2093 | " - intentional no-op; Codex Desktop rejects additionalContext on PreToolUse," |
| 2094 | " so graph guidance comes from AGENTS.md)" |
| 2095 | ) |
| 2096 | |
| 2097 | |
| 2098 | def _uninstall_codex_hook(project_dir: Path) -> None: |