Skip to content

fix(agent-guard): make the fresh-clone hook guard cross-platform (Windows) - #823

Merged
potiuk merged 1 commit into
apache:mainfrom
milamberspace:fix/agent-guard-hook-cross-platform
Jul 12, 2026
Merged

potiuk merged 1 commit into
apache:mainfrom
milamberspace:fix/agent-guard-hook-cross-platform

Conversation

@milamberspace

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #786 (merged): that PR guarded the committed agent-guard hook command with a POSIX shell conditional — [ -f … ] && python3 … || true — so the hook no-ops on a fresh clone before /magpie-setup has synced agent-guard.py in. But that form only works in a POSIX shell; on Windows (PowerShell, no Git Bash) the committed hook breaks.

Replace it with an inline python3 -c existence check that is cross-platform and needs no shell conditional:

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)"
  • no-ops (exit 0) when the script is absent (fresh clone);
  • runs the guard and propagates its exit code (so a deny still blocks) when present;
  • reads CLAUDE_PROJECT_DIR from the environment in Python, so it doesn't depend on $VAR vs %VAR% shell expansion.

Updated in the two places that ship the snippet: skills/setup/adopt.md and tools/agent-guard/README.md.

Found by an adopter (Apache JMeter) reviewing the same change downstream — the reviewer asked "Does this work with windows?" on the adoption PR.

Type of change

  • Bug fix (docs / committed template)

Test plan

Doc / template change. The guarded command is valid on both POSIX and Windows (pure python3 -c, no shell conditional); it no-ops when the script is missing and runs + propagates the guard's exit code when present.

…dows)

Follow-up to apache#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
@milamberspace

Copy link
Copy Markdown
Contributor Author

Heads-up for maintainers: this cross-platform fix may be superseded by #824.

That issue proposes writing the agent-guard hook into the gitignored, per-machine .claude/settings.local.json at setup (alongside the script) rather than committing it to .claude/settings.json. If that lands, the wiring only ever exists once the script is present, so no fresh-clone guard is needed at all — and the command reduces to a plain python3 ".../agent-guard.py", which is already cross-platform. In other words #824 would make both the #786 guard and this python3 -c rewrite unnecessary.

Happy to close this in favour of #824, or keep it as the interim fix if you would rather ship the smaller change first — your call.

@potiuk

potiuk commented Jul 11, 2026

Copy link
Copy Markdown
Member

Oh... Windows support :)

@potiuk
potiuk merged commit 7f4d40f into apache:main Jul 12, 2026
10 checks passed
AmirF194 added a commit to AmirF194/magpie that referenced this pull request Aug 26, 2026
…ettings.json

The adopt flow committed the agent-guard PreToolUse wiring into the
tracked .claude/settings.json while the script it invokes
(.claude/hooks/agent-guard.py) stayed gitignored, synced in only by
/magpie-setup. That split forced two prior workarounds for the same
root cause: apache#786 guarded the command against a fresh clone that has
the wiring but not yet the script, and apache#823 rewrote that guard for
Windows, where the POSIX conditional does not run.

Move the wiring itself into the gitignored, per-machine
settings.local.json, written by Step 12 pass 1 at the same moment
the script is deposited. Once wiring and script exist together on
the same schedule, the fresh-clone race is gone and the command
collapses to a plain `python3 "..."` call that runs the same on
POSIX and Windows, so both the apache#786 guard and the apache#823 rewrite
become unnecessary. This supersedes apache#823 and retro-justifies
removing the apache#786 guard.

Updates the merge/read/clean side for the same wiring across
adopt.md (Step 12 pass 1, Step 2), worktree-init.md (Step 1d),
unadopt.md, and verify.md, plus tools/agent-guard/README.md's
documented snippet. The idempotent-merge Python (skip if already
wired, preserve every other settings.local.json key and
hooks.PreToolUse matcher, leave a hand-edited/corrupt file alone) is
unchanged from the existing Step 12 pass 3 pattern for the
sandbox-allowlist entry.

Fixes apache#824

Generated-by: Claude Code (Sonnet 5)
potiuk pushed a commit that referenced this pull request Aug 27, 2026
…ettings.json (#1115)

* fix(setup): wire the agent-guard hook into settings.local.json, not settings.json

The adopt flow committed the agent-guard PreToolUse wiring into the
tracked .claude/settings.json while the script it invokes
(.claude/hooks/agent-guard.py) stayed gitignored, synced in only by
/magpie-setup. That split forced two prior workarounds for the same
root cause: #786 guarded the command against a fresh clone that has
the wiring but not yet the script, and #823 rewrote that guard for
Windows, where the POSIX conditional does not run.

Move the wiring itself into the gitignored, per-machine
settings.local.json, written by Step 12 pass 1 at the same moment
the script is deposited. Once wiring and script exist together on
the same schedule, the fresh-clone race is gone and the command
collapses to a plain `python3 "..."` call that runs the same on
POSIX and Windows, so both the #786 guard and the #823 rewrite
become unnecessary. This supersedes #823 and retro-justifies
removing the #786 guard.

Updates the merge/read/clean side for the same wiring across
adopt.md (Step 12 pass 1, Step 2), worktree-init.md (Step 1d),
unadopt.md, and verify.md, plus tools/agent-guard/README.md's
documented snippet. The idempotent-merge Python (skip if already
wired, preserve every other settings.local.json key and
hooks.PreToolUse matcher, leave a hand-edited/corrupt file alone) is
unchanged from the existing Step 12 pass 3 pattern for the
sandbox-allowlist entry.

Fixes #824

Generated-by: Claude Code (Sonnet 5)

* fix(setup): point the last worktree-init.md adopt.md reference at install.md

The #913 rebase repointed two of the three adopt.md links in this file
but missed the one inside the settings.local.json wiring bullet this PR
adds, which is what the CI link validator (prek, validate skills and
tool contracts) has been failing on.
@potiuk potiuk added the substrate:action-guard Tool substrate: deterministic pre-tool-use command guards label Sep 25, 2026
@potiuk potiuk added capability:platform Framework / agent substrate skills (install, verify, doctor, override, status, setup bootstrap) family:docs Docs, MISSION.md, READMEs family:setup setup-* skills family:tools tools/* labels Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

capability:platform Framework / agent substrate skills (install, verify, doctor, override, status, setup bootstrap) family:docs Docs, MISSION.md, READMEs family:setup setup-* skills family:tools tools/* substrate:action-guard Tool substrate: deterministic pre-tool-use command guards

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants