Skip to content

Add PostToolUse hook for live .agent/rules context injection - #3

Open
finneyai-app[bot] wants to merge 2 commits into
mainfrom
feat/agent-rules-hook
Open

Add PostToolUse hook for live .agent/rules context injection#3
finneyai-app[bot] wants to merge 2 commits into
mainfrom
feat/agent-rules-hook

Conversation

@finneyai-app

@finneyai-app finneyai-app Bot commented Jul 30, 2026

Copy link
Copy Markdown

Summary

  • Adds agent-rules-hook, a Claude Code PostToolUse hook that injects a matching .agent/rules/*.md rule's body into the agent's context whenever a Read/Write/Edit touches a file its globs (and filter) match — a continuous complement to the existing diff-review CLI/slash command, reusing the same rule-parsing, glob-matching, and filter-execution engine. Informational only; reviewSkip doesn't exclude a rule from injection; each rule is injected at most once per session (deduped by AgentRule.filePath, best-effort — persistence failures degrade dedup, never suppress injection).
  • New agent-rules setup CLI subcommand merges the hook into a project's .claude/settings.json: idempotent (checked against the exact matcher, not just the command string), never throws on a malformed existing file, leaves other hooks/settings untouched.
  • Fixes a pre-existing gap on main: git-dependency installs ("@casa/agent-rules": "github:...") shipped an empty dist/, since nothing built it post-clone. dist/ is now committed and a prepare: yarn build script rebuilds it as a fallback. (This fix — plus AgentRule.filePath and the zod peer-dependency switch — existed only on an abandoned, unmerged branch that a downstream consumer was pinned directly to instead of main; reconciled here.)
  • zod is now a peerDependency (^4.0.0) instead of a bundled dependency, so consumers that already depend on zod don't get a second, separate copy installed alongside their own.
  • New exports: buildHookContext, toRepoRelativePath, mergeHookSettings, HOOK_MATCHER, HOOK_COMMAND, resolveTransport, ResolveOptions, ResolvedTransport.

Test plan

  • yarn lint / yarn format:check / yarn typecheck — clean
  • yarn test — 78/78 passing (26 new: hook-context, hook-state, settings)
  • yarn builddist/ regenerated and committed
  • yarn smoke (9/9), yarn hook-smoke (11/11 — including a forced unwritable-tmp-dir case), yarn pack:smoke (6/6, packaged install of the real tarball)
  • Verified live against a real downstream consumer (a large yarn-workspaces monorepo): bin resolution, agent-rules setup idempotency against a hand-edited settings.json, and the hook actually firing in a real, authenticated Claude Code session — confirmed via the session transcript showing hook_successPostToolUse:Read → this hook's exact additionalContext output getting folded into context.

Co-Authored-By: Claude Sonnet 5 noreply@anthropic.com

browep added 2 commits July 31, 2026 08:51
Adds a second, continuous consumption path for existing .agent/rules/*.md
rules alongside the diff-review CLI: a Claude Code PostToolUse hook
(`agent-rules-hook`) that injects a rule's body into the agent's context
whenever a Read/Write/Edit touches a file its globs (and filter) match.
Reuses the existing rule-parsing, glob-matching, and filter-execution
engine rather than duplicating it.

- src/hook.ts: PostToolUse stdin/stdout entrypoint (bin: agent-rules-hook).
  Fails open on any error (bad input, missing rules dir, filter crash) so
  it never breaks the tool call it fired on.
- src/hook-context.ts: rule discovery/matching for a single touched path.
  Dedup keys on each rule's path relative to rulesDir (not rule.name, which
  isn't guaranteed unique). Resolves a relative rulesDir against the
  project root, not the hook process's own cwd.
- src/hook-state.ts: best-effort once-per-session dedup state under the OS
  temp dir; read/write failures degrade dedup, never suppress injection.
- src/settings.ts + `agent-rules setup` CLI subcommand: merges the hook
  into a project's .claude/settings.json, idempotent (checked against the
  exact matcher, not just the command string), never throws on a malformed
  existing file, leaves other hooks/settings untouched.
- Tests: unit tests for all new modules plus a hermetic hook-smoke.sh
  end-to-end suite (dedup, setup, unwritable-tmp-dir fallback) wired into
  CI and Docker; pack-smoke.sh extended to cover the new bin and exports.
- Docs: README, CHANGELOG, and the requirements spec (R39-R44) updated.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Reconciles two fixes that existed only on an abandoned, unmerged branch
(feat/agent-rules-package) which casa's finney-actions was pinned directly
to instead of main — meaning main itself was broken for any git-dependency
consumer (confirmed empirically: dist/ came up empty after `yarn install`
in casa with this branch's commit pinned).

- Commit dist/ to the repo (drop it from .gitignore) and add a `prepare:
  yarn build` script, so a git-dependency install (`github:...#commit=`)
  actually ships working output instead of an empty/stale dist/.
- zod is now a peerDependency (^4.0.0) instead of a bundled dependency, so
  consumers that already depend on zod (casa's finney-actions uses ^4.3.6)
  don't get a second, separate copy installed alongside their own.
- Export resolveTransport/ResolveOptions/ResolvedTransport for programmatic
  reuse of the CLI's model-transport resolution.
- AgentRule.filePath: absolute source path, set by loadRules(); included in
  --list --output json.
- hook-context.ts: now keys session dedup on rule.filePath instead of a
  bespoke collectRuleFiles/parseRuleFile reimplementation kept solely to
  compute a path-based key — same fix as before, less duplicated code.
- pack-smoke.sh: fixed a tarball-name capture bug the new `prepare` script
  exposed (npm pack's build output was leaking into the captured filename);
  added resolveTransport to the exported-functions check.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@browep
browep force-pushed the feat/agent-rules-hook branch from 0bfefdd to 23cc4c8 Compare July 31, 2026 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant