From 8f58ff257508944d2165777b77ab9e437f5254d7 Mon Sep 17 00:00:00 2001 From: Milamber Date: Sat, 11 Jul 2026 20:25:20 +0200 Subject: [PATCH] fix(agent-guard): make the fresh-clone hook guard cross-platform (Windows) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up to #786: the POSIX `[ -f … ] && … || true` guard only works in a POSIX shell, so on Windows (PowerShell, no Git Bash) the committed hook breaks. Replace it with an inline `python3 -c` existence check that no-ops when agent-guard.py is absent and runs + propagates its exit code otherwise — cross-platform, no shell conditional, and reads CLAUDE_PROJECT_DIR from the environment in Python (no $VAR vs %VAR% expansion). Reported by an adopter (Apache JMeter) reviewing the same change downstream. Generated-by: Claude Opus 4.8 --- skills/setup/adopt.md | 15 ++++++++------- tools/agent-guard/README.md | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/skills/setup/adopt.md b/skills/setup/adopt.md index d09b6b674..d1515f178 100644 --- a/skills/setup/adopt.md +++ b/skills/setup/adopt.md @@ -1292,16 +1292,17 @@ Four passes, in this order: ```json { "matcher": "Bash", "hooks": [ { "type": "command", - "command": "[ -f \"$CLAUDE_PROJECT_DIR/.claude/hooks/agent-guard.py\" ] && python3 \"$CLAUDE_PROJECT_DIR/.claude/hooks/agent-guard.py\" || true", + "command": "python3 -c \"import os,sys,subprocess; p=os.path.join(os.environ.get('CLAUDE_PROJECT_DIR',''),'.claude','hooks','agent-guard.py'); sys.exit(subprocess.call([sys.executable,p]) if os.path.isfile(p) else 0)\"", "timeout": 30 } ] } ``` - The `[ -f … ] && … || true` guard makes the hook a **no-op on a - fresh clone** — before `/magpie-setup` has synced `agent-guard.py` - in (the script is gitignored framework code, not committed), so - without the guard the committed hook would exec a missing file on - every Bash call and a `PreToolUse` error can block the tool. Once - the script is present the guard passes and the hook runs normally. + The command no-ops when `agent-guard.py` is absent (a **fresh + clone**, before `/magpie-setup` has synced the gitignored script in) + and runs the guard otherwise — so the committed hook never execs a + missing file on a Bash call (a `PreToolUse` error can block the + tool). It is written as an inline `python3 -c` existence check + rather than a shell one-liner (`[ -f … ] && … || true`) so it works + on **Windows** (PowerShell) too, not only POSIX shells. Wiring happens **only once**; thereafter guards are added/removed purely by syncing `guards.d` — no settings.json diff --git a/tools/agent-guard/README.md b/tools/agent-guard/README.md index c41353885..686ce158c 100644 --- a/tools/agent-guard/README.md +++ b/tools/agent-guard/README.md @@ -111,7 +111,7 @@ The guard is registered as a `PreToolUse` hook on the `Bash` matcher in { "matcher": "Bash", "hooks": [ - { "type": "command", "command": "[ -f \"$CLAUDE_PROJECT_DIR/.claude/hooks/agent-guard.py\" ] && python3 \"$CLAUDE_PROJECT_DIR/.claude/hooks/agent-guard.py\" || true", "timeout": 30 } + { "type": "command", "command": "python3 -c \"import os,sys,subprocess; p=os.path.join(os.environ.get('CLAUDE_PROJECT_DIR',''),'.claude','hooks','agent-guard.py'); sys.exit(subprocess.call([sys.executable,p]) if os.path.isfile(p) else 0)\"", "timeout": 30 } ] } ]