Skip to content

feat(agent-guard): run the guard from the installed plugin, not a per-worktree copy - #1170

Merged
potiuk merged 1 commit into
apache:mainfrom
potiuk:feat/agent-guard-plugin-hook
Sep 8, 2026
Merged

potiuk merged 1 commit into
apache:mainfrom
potiuk:feat/agent-guard-plugin-hook

Conversation

@potiuk

@potiuk potiuk commented Sep 8, 2026

Copy link
Copy Markdown
Member

Summary

  • 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 three separate mechanisms existed to seed every worktree — the post-checkout hook, the install Step 12 sync pass, and worktree-init Step 1d.
  • When that seeding misses, the failure is worse than an 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. This was found the hard way: a fresh agent worktree of an adopter repo where no shell command could run at all.
  • The new magpie-agent-guard plugin registers the PreToolUse hook 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 magpie plugin or on each family plugin would run it once per enabled plugin on every Bash call — 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 from family: frontmatter, shipping a tool instead of skills, reaching tools/agent-guard through a narrow symlink. check-family-plugins.py learns to generate and validate that shape.

Snapshot adopters get the same property

The settings.local.json wiring now resolves the engine inside the snapshot, and worktree-init Step 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 by upgrade, since that stale entry is the one that breaks every Bash call.

Skill-owned guards are discovered in place

guard_dirs() resolves the framework tree the engine is running from and scans every skills/*/guards in it, so a skill's guards are live wherever the framework is. The step that collected them into a local guards.d/ is gone. Discovery stays lazy — discover_guards() is still only called once a gh/git segment is seen, so the fast path for every other command is unchanged.

Type of change

  • Tool / bridge contract (tools/<system>/*.md)
  • Python package (tools/*/ with pyproject.toml)
  • Cross-cutting (RFC, AGENTS.md, sandbox, privacy-LLM)
  • Documentation (docs/, README.md, CONTRIBUTING.md)
  • CI / dev loop (prek, workflows, validators)

Test plan

  • prek run --all-files passes (including check-family-plugins, check-doc-sync, symlink-lint, skill-and-tool-validate, lychee, the workspace pytest/ruff/mypy)
  • For Python packages touched: uv run --project tools/agent-guard pytest tools/agent-guard/tests and uv run --project tools/dev pytest tools/dev/tests pass
  • Other: exercised end-to-end through the published plugin path (plugins/magpie-agent-guard/tools/agent-guard/src/agent_guard/__init__.py) with real PreToolUse payloads — a bundled guard denies (commit-trailer), a skill-owned guard denies (mention, discovered with no MAGPIE_GUARD_DIRS set), and an unguarded command takes the fast path.

New tests:

  • tools/agent-guard/tests/test_discovery.py — framework_root() from a checkout, None for 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, a skills key 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 stray plugins/magpie-* is still caught), and unowned_entries refusing to rmtree a 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

  • Sandbox — no new host access; the plugin's symlink exposes only tools/agent-guard, not the whole tools/ tree
  • Vendor neutrality — no new project-specific literals; check-placeholders passes
  • Conversational + correctable — $MAGPIE_GUARD_DIRS remains the escape hatch for guards kept outside the framework tree

Linked issues

Notes for reviewers (optional)

Two decisions worth a second opinion:

  1. Substrate plugin vs. hook on the all-in-one. Rejected wiring the hook into each family plugin (N× execution per Bash call) and rejected an in-engine dedupe for it: the only key derivable from a PreToolUse payload 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.
  2. framework_root() walks parents looking for skills/ + tools/agent-guard/ rather than counting parents[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 returns None for a standalone copy so it falls back to its own guards.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 its guards.d sibling.


Generated-by: Claude Code (Opus 5) following the guidelines

https://claude.ai/code/session_01GLcpHRb14rFsAnBR6NVBvf

…-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
potiuk merged commit fbe828e into apache:main Sep 8, 2026
11 checks passed
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)
@potiuk potiuk added substrate:action-guard Tool substrate: deterministic pre-tool-use command guards substrate:framework-dev Tool substrate: build / validate / eval the framework itself labels Sep 25, 2026
@potiuk potiuk added capability:platform Framework / agent substrate skills (install, verify, doctor, override, status, setup bootstrap) family:ci .github workflows, prek, validators 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:ci .github workflows, prek, validators family:docs Docs, MISSION.md, READMEs family:setup setup-* skills family:tools tools/* substrate:action-guard Tool substrate: deterministic pre-tool-use command guards substrate:framework-dev Tool substrate: build / validate / eval the framework itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant