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

1

u/CCodeSI 27d ago

Read through the four scripts. Nicely tight, and the stop_hook_active retry-once plus "linter failure is a note, not a block" are the right calls. Things worth looking at, roughly in order of impact:

1. Suppressions added through the shell skip guard.mjs entirely. It only inspects Edit/MultiEdit/Write. sed -i, echo '// eslint-disable' >> f, git apply, or a Python one-liner all add suppressions without a prompt. The route-proof check is on the result, not the tool call: in lint-turn.mjs, run git diff -U0 on the turn's files and test only the added (+) lines against SUPPRESSION. That catches every path an edit can take.

2. Moving a suppression within one edit passes the multiset check. Old text has // eslint-disable-next-line above line A, new text has the same line above line B: same multiset, but it now silences different code. Comparing each suppression line together with the line that follows it would catch that.

3. Patterns the regex misses:

  • # pylint: skip-file (only pylint: disable is matched)
  • # pyright: ignore, # mypy: ignore-errors
  • // NOLINT / NOLINTNEXTLINE (clang-tidy is uppercase; \/\/\s*nolint is case-sensitive)
  • #pragma warning disable (C#), /* tslint:disable */
  • Config that lives inside other files: eslintConfig in package.json, [tool.ruff] / per-file-ignores in pyproject.toml. Those change rules without touching a lint config file.

False positive you'll hit: eslint-disable mentioned in a Markdown doc or a string literal triggers the ask. Limiting the guard to the linters' configured extensions would cut most of that.

4. parseHunks and quoted paths. core.quotepath=off stops octal-escaping of non-ASCII, but git still C-quotes names containing ", \, tabs or newlines: +++ "b/we\"ird.ts". The ^b\/ strip fails, the key never matches a finding, and that file silently never blocks. Either unquote that form or test for it. Deletion-only and binary hunks look handled correctly. Renames are fine too, since +++ carries the new path either way.

5. Timeouts. spawnSync's timeout sends SIGTERM by default; a linter that ignores it keeps the hook waiting. killSignal: 'SIGKILL' makes the timeout hard. Also note the fast-tier timeout is per linter and sequential, so three fast linters can mean up to 30s after every edit; a single budget for the whole hook might be kinder.

6. Speed. The git calls are cheap; ESLint's cold start is what you'll feel on every edit. --cache (or eslint_d) usually takes it from a second or two to well under one.

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.

0

u/[deleted] 27d ago

[removed] — view removed comment

1

u/Present_Giraffe_5224 27d ago

Thanks. Deletion-only hunks and quoted names are covered by tests now. Diagnostics on lines you didn't touch are a deliberate trade-off: only errors on changed lines block, so the agent never fixes unrelated legacy code, and the end-of-turn check plus CI catch the rest.

0

u/[deleted] 27d ago

[removed] — view removed comment

1

u/Present_Giraffe_5224 27d ago

Thanks! Those were the right areas to poke at. Timeouts are now a hard kill with a shared budget, and a broken or slow linter is reported as a note and treated as clean, so it never blocks. Renames, paths with spaces and quoted names all have tests now. Suppressions in Markdown docs are skipped. One inside a string literal in code still asks, which I'd rather keep than miss a real one.