From e02c7cc60c40310b487a72b489d6c7014dde4442 Mon Sep 17 00:00:00 2001 From: Safi Date: Sat, 2 May 2026 13:57:19 +0100 Subject: [PATCH] Fix Codex PreToolUse hook on Windows by delegating to graphify hook-check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The python3 -c "..." approach still failed on Windows Conda (no python3 shim) and PowerShell (JSON curly brace/quote parsing). Replace the inline command with 'graphify hook-check' — a new shell-agnostic subcommand that prints the hookSpecificOutput JSON if graph.json exists and exits 0 silently if not. Works on PowerShell, cmd.exe, macOS, and Linux with no quoting or interpreter-name issues. Users must re-run 'graphify codex install' to regenerate the hook. Co-Authored-By: Claude Sonnet 4.6 --- graphify/__main__.py | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/graphify/__main__.py b/graphify/__main__.py index 1277bff..94d1f69 100644 --- a/graphify/__main__.py +++ b/graphify/__main__.py @@ -718,15 +718,11 @@ _CODEX_HOOK = { "hooks": [ { "type": "command", - # Use Python for the file check so the hook works on Windows - # (cmd.exe has no [ -f ] builtin; Python is always available). - "command": ( - "python3 -c \"" - "import pathlib,json,sys; " - "p=pathlib.Path('graphify-out/graph.json'); " - r"print(json.dumps({'hookSpecificOutput':{'hookEventName':'PreToolUse','additionalContext':'graphify: Knowledge graph exists. Read graphify-out/GRAPH_REPORT.md for god nodes and community structure before searching raw files.'}})) if p.exists() else None" - "\"" - ), + # Use the graphify CLI itself so the hook is shell-agnostic: + # no [ -f ] bash syntax, no python3 vs python Conda issue, + # no JSON escaping inside PowerShell strings. Works on + # Windows (PowerShell/cmd.exe), macOS, and Linux. + "command": "graphify hook-check", } ], } @@ -1453,6 +1449,25 @@ def main() -> None: print("Nothing to update or rebuild failed — check output above.", file=sys.stderr) sys.exit(1) + elif cmd == "hook-check": + # Shell-agnostic PreToolUse hook entry point for Codex (and any platform + # where embedding Python/bash inline in a JSON hook command is fragile). + # Prints the hookSpecificOutput JSON if graph.json exists, exits 0 silently + # if not. Works on Windows PowerShell, cmd.exe, macOS, and Linux. + graph = Path("graphify-out") / "graph.json" + if graph.exists(): + import json as _json + print(_json.dumps({ + "hookSpecificOutput": { + "hookEventName": "PreToolUse", + "additionalContext": ( + "graphify: Knowledge graph exists. " + "Read graphify-out/GRAPH_REPORT.md for god nodes and " + "community structure before searching raw files." + ), + } + })) + sys.exit(0) elif cmd == "check-update": if len(sys.argv) < 3: print("Usage: graphify check-update ", file=sys.stderr)