Add PostToolUse hook for live .agent/rules context injection - #3
Open
finneyai-app[bot] wants to merge 2 commits into
Open
Add PostToolUse hook for live .agent/rules context injection#3finneyai-app[bot] wants to merge 2 commits into
finneyai-app[bot] wants to merge 2 commits into
Conversation
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
force-pushed
the
feat/agent-rules-hook
branch
from
July 31, 2026 14:51
0bfefdd to
23cc4c8
Compare
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
agent-rules-hook, a Claude CodePostToolUsehook that injects a matching.agent/rules/*.mdrule's body into the agent's context whenever a Read/Write/Edit touches a file itsglobs(andfilter) 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;reviewSkipdoesn't exclude a rule from injection; each rule is injected at most once per session (deduped byAgentRule.filePath, best-effort — persistence failures degrade dedup, never suppress injection).agent-rules setupCLI 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.main: git-dependency installs ("@casa/agent-rules": "github:...") shipped an emptydist/, since nothing built it post-clone.dist/is now committed and aprepare: yarn buildscript rebuilds it as a fallback. (This fix — plusAgentRule.filePathand thezodpeer-dependency switch — existed only on an abandoned, unmerged branch that a downstream consumer was pinned directly to instead ofmain; reconciled here.)zodis now apeerDependency(^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.buildHookContext,toRepoRelativePath,mergeHookSettings,HOOK_MATCHER,HOOK_COMMAND,resolveTransport,ResolveOptions,ResolvedTransport.Test plan
yarn lint/yarn format:check/yarn typecheck— cleanyarn test— 78/78 passing (26 new: hook-context, hook-state, settings)yarn build—dist/regenerated and committedyarn 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)agent-rules setupidempotency against a hand-editedsettings.json, and the hook actually firing in a real, authenticated Claude Code session — confirmed via the session transcript showinghook_success→PostToolUse:Read→ this hook's exactadditionalContextoutput getting folded into context.Co-Authored-By: Claude Sonnet 5 noreply@anthropic.com