Topic 2 of 6 · 5 min read
Running formatters and tests after agent edits
Goal: Configure an after-edit hook that formats and checks only the changed files and returns failures to the agent in a form it can act on.
An after-edit hook should do a small, fast job on the file that just changed, and when it fails, tell the agent exactly what to fix.
Check yourself · 1 of 3
Which input should an after-edit formatting hook use to find the file?
Why it matters
Agents write code that is correct in spirit and untidy in detail: wrong quotes, missing semicolons, a type error in a line it just touched. If you catch these on each edit, the agent fixes them while the context is fresh. If you catch them at the end, you get a long list and a slow loop. But a hook that runs your whole test suite after every edit makes the session crawl and burns tokens on noise.
How to do it
- Use a
PostToolUsehook with matcherEdit|Write. - Read the JSON on standard input and take
tool_input.file_path. Work on that one file, not the repo. - Skip files you do not handle. A markdown file does not need the TypeScript checker.
- Run the fast tools only: a formatter, a linter on one file, maybe a type check of the project if it takes a few seconds.
- Keep the full test suite for the end, in a
Stophook, or leave it to CI. - On failure, print a short message to standard error and exit with code 2. In Claude Code this feeds the message to the model, which then fixes the problem. Exit 0 with nothing printed when all is well.
Make the failure message useful. Include the file, the line and the rule. Cut it to the first 20 lines, because a 3,000-line log teaches nothing.
Before and after
Before: the hook runs npm test after every edit. A change to a README takes 90 seconds, and the agent reads 400 lines of output it does not need.
After: the hook runs prettier --write on the edited file, then eslint on that file only. A change to a README is skipped by extension. A lint error comes back as three lines naming the file and rule.
What goes wrong
- The formatter rewrites the file, then the agent's next edit uses text that no longer matches, and the edit fails. Format with the same settings you use in the repo, and expect the agent to re-read the file.
- The hook exits 2 on a warning, and the agent loops on a style nit nobody cares about. Block only on errors.
- The command is not installed on someone's machine. The hook fails silently, or noisily on every edit. Check for the tool and say so once.
Examples
Format and lint one file
#!/usr/bin/env bash
file=$(jq -r '.tool_input.file_path // empty')
case "$file" in
*.ts|*.tsx) ;;
*) exit 0 ;;
esac
npx prettier --write "$file" >/dev/null
out=$(npx eslint "$file" 2>&1) || {
echo "$out" | head -n 20 >&2
exit 2
}
It ignores other file types, formats quietly, and returns at most 20 lines of lint errors to the agent.
Tests at the end
A Stop hook runs npm test -- --changed once, when the agent says it is done. If tests fail, the hook returns the failing names so the agent keeps working. See the debugging topic for how to stop this from looping.
Key points
- Run the hook on the changed file only, taken from
tool_input.file_path, not on the whole repository. - Keep after-edit work fast: format and lint now, run the full suite at the end or in CI.
- Exit with code 2 and a short message on standard error so the agent sees the failure and fixes it.
- Trim output to the useful first lines; a long log wastes context and hides the error.
- Block on errors only, never on warnings, or the agent will loop on style.
Common mistakes
- Running the entire test suite after every edit and making each step slow.
- Returning raw tool output with hundreds of lines instead of the first useful ones.
- Blocking on warnings so the agent chases formatting nits.
- Not skipping file types the tool does not understand.
Check yourself · 2 of 3
A hook runs the full test suite after every edit and sessions feel slow. What is the best fix?
Check yourself · 3 of 3
A lint hook fails. How should it report the problem so the agent can act?
Practice items, not counted towards your mastery.
Start here
- Hooks reference (Claude Code docs) (opens in a new tab)DocsFree
Every event, matcher, input field and exit-code rule, straight from the vendor.
- Automate workflows with hooks (Claude Code docs) (opens in a new tab)DocsFree
Worked examples: formatting after edits, blocking commands, desktop notifications.
- Hooks (Gemini CLI docs) (opens in a new tab)DocsFree
Shows the same idea in another agent, so you can compare event names and decision formats.