r/codereview • • 27d ago

[JavaScript/Node] Review my Claude Code hooks that lint every file an agent edits (~330 lines, zero deps)

I built codemyrules, an open-source agent skill that turns the rules in AGENTS.md / CLAUDE.md into ESLint or ast-grep rules. The part I'd like reviewed is the feedback loop: three small Node scripts that run as Claude Code hooks, plain ESM, no dependencies, tests with node:test.

  • guard.mjs (PreToolUse, 68 lines): asks the user before the agent adds an eslint-disable / @ts-ignore / noqa etc. or edits a lint config.
  • lint-edited.mjs (PostToolUse, 39 lines): runs the fast linters on the edited file and blocks only on errors in lines changed since HEAD.
  • lint-turn.mjs (Stop, 40 lines): runs all linters on every file edited in the turn.
  • lib.mjs (181 lines): diff hunk parsing, linter output parsing, config, the secret-file deny list.

Code: https://github.com/clirimajeti/codemyrules/tree/main/skills/codemyrules/scripts

What I'd especially like eyes on:

  1. The suppression regex in guard.mjs**.** Can you think of a way an agent could silence a rule that it misses, or a false positive that would annoy you?
  2. parseHunks in lib.mjs**.** It parses git diff -U0 output to find changed lines. Edge cases I've missed (renames, binary files, odd file names)?
  3. Speed. The PostToolUse hook runs after every single edit. Anything obviously slow?
  4. Failure modes. A broken hook must never block the agent forever or leak a credential file. Anything that could?

Run the tests with node --test skills/codemyrules/scripts/test/*.test.mjs. Honest feedback welcome.

0 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/Present_Giraffe_5224 27d ago

This is the best review I've had on this, thank you. I went through every point and fixed them all:

  1. Shell edits: you were right, and it was worse than that, because files changed through Bash never reached the end-of-turn check either. I didn't diff at Stop, since that would re-flag the user's own uncommitted suppressions every turn. Instead a new hook runs before and after each Bash and NotebookEdit call. Before, it saves the dirty files with git hash-object -w; after, it diffs every file the command changed and checks it like an edit. It can't ask first, since the change already happened, so it blocks and tells the agent to undo it or ask. About 70ms per call.
  2. A suppression on its own line is now keyed together with the next line of code, so moving it onto other code asks.
  3. Added your patterns, plus rubocop, phpstan/psalm/phpcs and C# [SuppressMessage]. Those were embarrassing gaps, since I claim Ruby, PHP and C# support. Prose files are skipped now. For package.json / pyproject.toml I compare only the lint sections (eslintConfig, [tool.ruff] and so on), so bumping a dependency doesn't ask.
  4. Confirmed the quoting bug. ls-files had the same problem for untracked files. Both are fixed: the diff path is unquoted and ls-files uses -z.
  5. Reproduced the SIGTERM hang (4s instead of 0.3s). Now it's SIGKILL with one shared budget per hook.
  6. ESLint in the fast tier already goes through eslint_d, but good call.

1

u/CCodeSI 27d ago

Thanks, and impressive turnaround! The before/after snapshot around Bash is a better fix than what we suggested: it catches the change no matter how it was made, and it doesn't re-flag the user's own uncommitted suppressions. 70ms per call is a fair price for that.

Comparing only the lint sections of package.json and pyproject.toml is a nice touch too, a guard that asks on every dependency bump would get turned off within a day.