Repository navigation
feat(agent-guard): run the guard from the installed plugin, not a per-worktree copy - #1170
Merged
Merged
Conversation
…-worktree copy
The guard was installed by copying the engine into `<repo>/.claude/hooks/`
and wiring `$CLAUDE_PROJECT_DIR/.claude/hooks/agent-guard.py` into a
gitignored `settings.local.json`. Both are per-worktree and neither travels
through git, so every worktree had to be seeded — by a best-effort
post-checkout hook, by a sync pass, and by `worktree-init` Step 1d.
When that seeding misses, the failure is not a quietly inactive guard. The
wiring still applies in the worktree (Claude Code resolves
`$CLAUDE_PROJECT_DIR` against it while loading the main checkout's settings),
so the hook names a script that is not there and **every** `Bash` call in that
worktree fails. Three separate mechanisms existed to paper over a shape that
should not need any of them: a worktree is an ordinary checkout.
The `magpie-agent-guard` plugin registers the `PreToolUse` hook in its own
manifest and resolves the engine under `${CLAUDE_PLUGIN_ROOT}`, so nothing is
repository-local and a worktree needs no treatment. It is a plugin of its own
rather than a hook on `magpie` or on each family plugin because Claude Code
merges hooks from every enabled plugin — wiring it into the families would run
the guard once per enabled family on every `Bash` call.
Snapshot adopters gain the same property without the plugin: the wiring now
points into the snapshot, which `worktree-init` Step 1 already symlinks into
every worktree.
Skill-owned guards follow the same principle. The engine resolves the
framework tree it is running from and scans every `skills/*/guards` in place,
so a skill's guards are live wherever the framework is and the step that
collected them into a local `guards.d/` is gone. Discovery stays lazy — it
runs only once a `gh`/`git` segment is seen, never on the fast path.
Generated-by: Claude Code (Opus 5) following the guidelines
Claude-Session: https://claude.ai/code/session_01GLcpHRb14rFsAnBR6NVBvf
potiuk
added a commit
that referenced
this pull request
Sep 8, 2026
…self-correcting (#1174) * fix(marketplace): re-sync magpie-agent-guard with the dev version stamp `#1169` stamped the framework version to `0.2.0.dev202609081000` across every ecosystem manifest. `#1170` landed right after it, adding the `magpie-agent-guard` plugin — a *new* file, branched before the bump and therefore carrying the previous stamp `0.2.0.dev202609080121`. New files do not conflict, so both merged cleanly and `main` was left with a family plugin one stamp behind the root manifest it inherits from. `check-family-plugins` has failed on `main` ever since, which is why an unrelated dependabot PR (#1167) shows a red `prek` job it did not cause. `python3 tools/dev/check-family-plugins.py --fix` output; no hand edits. Generated-by: Claude Code (Opus 5) * fix(prek): regenerate the family plugins instead of reporting them stale The `check-family-plugins` hook ran check-only, so a contributor whose manifests drifted got an instruction — "run `tools/dev/check-family-plugins.py --fix`" — rather than a fixed tree. The generated files are a pure function of the skills' `family:` frontmatter and `pyproject.toml`'s version, so there is nothing for them to decide: the fixer is the only correct response to every failure the check can report. Run the hook as `--fix`. prek fails a run whose hook modified files, so drift still stops the commit; it now arrives already corrected, needing only a re-stage — the same convention `end-of-file-fixer`, `trailing-whitespace`, and `add-license-headers` already follow here. `--fix`'s exit code is deliberately left alone: the release-bump procedure invokes it directly in plain shell blocks, where returning non-zero on a successful regeneration would read as failure. Scope note: this closes the hand-fix gap for anyone who runs the hooks, but it cannot catch the cross-PR case that broke `main` this morning — two PRs green in isolation, conflict-free on merge, out of sync in the result. Only a check on `main` (or a merge queue) sees that. Docs describing the hook as check-only updated to match. Generated-by: Claude Code (Opus 5)
potiuk
added a commit
to potiuk/magpie
that referenced
this pull request
Sep 9, 2026
Seven changes have landed behind the previous stamp: the agent-guard plugin move (apache#1170), the doctoc URL fix (apache#1171), the marketplace re-sync plus the self-correcting prek hook (apache#1174), the vetted command surface (apache#1176), and the three PRs that extended its catalogue from 19 to 58 operations (apache#1177, apache#1178, apache#1179). `claude plugin update` compares version strings, so none of that reaches an adopter until the stamp moves. Mechanical: `project.version` edited, then `tools/dev/check-family-plugins.py --fix` propagated it to the five ecosystem manifests and the eleven per-family plugin manifests plus the marketplace entries, and `uv lock` refreshed the workspace's own package version. Generated-by: Claude Code (Opus 5)
potiuk
added a commit
that referenced
this pull request
Sep 9, 2026
Seven changes have landed behind the previous stamp: the agent-guard plugin move (#1170), the doctoc URL fix (#1171), the marketplace re-sync plus the self-correcting prek hook (#1174), the vetted command surface (#1176), and the three PRs that extended its catalogue from 19 to 58 operations (#1177, #1178, #1179). `claude plugin update` compares version strings, so none of that reaches an adopter until the stamp moves. Mechanical: `project.version` edited, then `tools/dev/check-family-plugins.py --fix` propagated it to the five ecosystem manifests and the eleven per-family plugin manifests plus the marketplace entries, and `uv lock` refreshed the workspace's own package version. Generated-by: Claude Code (Opus 5)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
<repo>/.claude/hooks/and wiring$CLAUDE_PROJECT_DIR/.claude/hooks/agent-guard.pyinto a gitignoredsettings.local.json. Both are per-worktree and neither travels through git, so three separate mechanisms existed to seed every worktree — the post-checkout hook, the install Step 12 sync pass, andworktree-initStep 1d.$CLAUDE_PROJECT_DIRagainst it while loading the main checkout's settings), so the hook names a script that is not there and everyBashcall in that worktree fails. This was found the hard way: a fresh agent worktree of an adopter repo where no shell command could run at all.magpie-agent-guardplugin registers thePreToolUsehook in its own manifest and resolves the engine under${CLAUDE_PLUGIN_ROOT}. Nothing is repository-local, so a worktree is an ordinary checkout and all three seeding mechanisms are deleted.Why a separate plugin
Claude Code merges hooks from every enabled plugin. Putting the guard on the all-in-one
magpieplugin or on each family plugin would run it once per enabled plugin on everyBashcall — three enabled families means three Python startups per command. One dedicated owner runs it exactly once, whatever else is installed. It also keeps installing the guard an explicit opt-in, which matches the semantics it already had as an adopter-side file.The family plugins are generated skill-only subsets with no
tools/, so this is introduced as a substrate plugin: hand-declared rather than derived fromfamily:frontmatter, shipping a tool instead of skills, reachingtools/agent-guardthrough a narrow symlink.check-family-plugins.pylearns to generate and validate that shape.Snapshot adopters get the same property
The
settings.local.jsonwiring now resolves the engine inside the snapshot, andworktree-initStep 1 already symlinks.apache-magpie/into every worktree — so the path resolves there with nothing seeded. Adopters whose entry still names the retired per-repository copy are rewritten byupgrade, since that stale entry is the one that breaks everyBashcall.Skill-owned guards are discovered in place
guard_dirs()resolves the framework tree the engine is running from and scans everyskills/*/guardsin it, so a skill's guards are live wherever the framework is. The step that collected them into a localguards.d/is gone. Discovery stays lazy —discover_guards()is still only called once agh/gitsegment is seen, so the fast path for every other command is unchanged.Type of change
tools/<system>/*.md)tools/*/withpyproject.toml)docs/,README.md,CONTRIBUTING.md)prek, workflows, validators)Test plan
prek run --all-filespasses (includingcheck-family-plugins,check-doc-sync,symlink-lint,skill-and-tool-validate,lychee, the workspacepytest/ruff/mypy)uv run --project tools/agent-guard pytest tools/agent-guard/testsanduv run --project tools/dev pytest tools/dev/testspassplugins/magpie-agent-guard/tools/agent-guard/src/agent_guard/__init__.py) with realPreToolUsepayloads — a bundled guard denies (commit-trailer), a skill-owned guard denies (mention, discovered with noMAGPIE_GUARD_DIRSset), and an unguarded command takes the fast path.New tests:
tools/agent-guard/tests/test_discovery.py—framework_root()from a checkout,Nonefor a standalone single-file copy, skill guard dirs discovered with no configuration, env dirs searched first, dedup, a skill-owned guard denying with nothing pointing at it, and discovery surviving a symlinked plugin root (how the published plugin actually resolves).tools/dev/tests/test_check_family_plugins.py— the substrate rules: altered hook wiring, askillskey that should not be there, metadata not inherited from the root manifest, a repointed or de-symlinked tool link, an engine path that does not resolve, the orphan-rule exemption (while a genuinely strayplugins/magpie-*is still caught), andunowned_entriesrefusing tormtreea hand-added file.The engine-path-resolves check is the one worth keeping: a manifest and a symlink can both look right while the file the hook command names has moved, and that is the case where the guard silently never runs.
RFC-AI-0004 compliance
tools/agent-guard, not the wholetools/treecheck-placeholderspasses$MAGPIE_GUARD_DIRSremains the escape hatch for guards kept outside the framework treeLinked issues
Notes for reviewers (optional)
Two decisions worth a second opinion:
Bashcall) and rejected an in-engine dedupe for it: the only key derivable from aPreToolUsepayload is(session, cwd, command), so two identical commands in quick succession would let the second skip the guard — a real hole in a security control, in exchange for saving a plugin.framework_root()walks parents looking forskills/+tools/agent-guard/rather than countingparents[N]. Counting breaks the moment the engine is reached through the plugin's symlink or lives in a snapshot at a different depth; the pair-of-directories test holds for a plugin, a checkout, and a snapshot alike, and correctly returnsNonefor a standalone copy so it falls back to its ownguards.d.The user-scope secure setup (
~/.claude/scripts/agent-guard.py) is untouched — it is already repository- and worktree-independent, and a standalone copy legitimately still needs itsguards.dsibling.Generated-by: Claude Code (Opus 5) following the guidelines
https://claude.ai/code/session_01GLcpHRb14rFsAnBR6NVBvf