r/codereview • u/Present_Giraffe_5224 • 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 aneslint-disable/@ts-ignore/noqaetc. 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 sinceHEAD.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:
- 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? parseHunksinlib.mjs**.** It parsesgit diff -U0output to find changed lines. Edge cases I've missed (renames, binary files, odd file names)?- Speed. The PostToolUse hook runs after every single edit. Anything obviously slow?
- 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
View all comments
1
u/CCodeSI 27d ago
Read through the four scripts. Nicely tight, and the
stop_hook_activeretry-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.mjsentirely. It only inspectsEdit/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: inlint-turn.mjs, rungit diff -U0on the turn's files and test only the added (+) lines againstSUPPRESSION. 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-lineabove 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(onlypylint: disableis matched)# pyright: ignore,# mypy: ignore-errors// NOLINT/NOLINTNEXTLINE(clang-tidy is uppercase;\/\/\s*nolintis case-sensitive)#pragma warning disable(C#),/* tslint:disable */eslintConfigin package.json,[tool.ruff]/per-file-ignoresin pyproject.toml. Those change rules without touching a lint config file.False positive you'll hit:
eslint-disablementioned 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.
parseHunksand quoted paths.core.quotepath=offstops 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.