HOOK · quality-cd
PreToolUse hook: block test edits
A PreToolUse hook that blocks the agent from editing test and eval files during implementation work, turning the test-edit prohibition from prose into a wall, with an explicit human override.
✓ verified 2026-07-18 · stack: claude-code · source
The shortest path from a red suite to a green one is editing the test, and Anthropic’s harness guidance had to say it in words: “It is unacceptable to remove or edit tests” (effective harnesses). Words hold on short tasks; the fiftieth turn is what hooks are for. A PreToolUse hook runs before the tool executes and exit 2 blocks the call with feedback (hooks); memory is context, hooks are enforcement, and the docs draw exactly that line.
{ "hooks": { "PreToolUse": [ { "matcher": "Edit|Write", "hooks": [ { "type": "command", "command": "node .claude/hooks/block-test-edits.mjs", "timeout": 10 } ] } ] }}#!/usr/bin/env node// PreToolUse gate: block edits to test and eval files unless the human// has explicitly opened the door for this session.// Exit 2 = block the tool call; stderr goes to the agent as feedback.import { readFileSync, existsSync } from 'node:fs';
const PROTECTED = [ /(^|\/)tests?\//, /\.(test|spec)\.[cm]?[jt]sx?$/, /(^|\/)evals\//, /(^|\/)src\/evalTest\//,];
// The override: a human runs `touch .allow-test-edits` when the task IS// changing tests (new feature specs, refactoring the suite). Delete it// when done; it is gitignored so it can never land in a PR.if (existsSync('.allow-test-edits')) process.exit(0);
const input = JSON.parse(readFileSync(0, 'utf8'));const file = input.tool_input?.file_path ?? '';
if (PROTECTED.some((rx) => rx.test(file))) { console.error( `Blocked: ${file} is a test/eval file. Fix the code, not the test. ` + `If changing tests is genuinely the task, ask the human to run ` + `"touch .allow-test-edits" and re-try.`, ); process.exit(2);}process.exit(0);.allow-test-editsWhat it does
- Blocks Edit and Write calls targeting test files, spec files, and eval suites before they execute; the agent receives the reason and the legitimate way forward as feedback, so a blocked edit becomes a conversation with the human rather than a silent workaround hunt.
- The override is a deliberate human act (
touch .allow-test-edits), file rather than environment variable so it is visible inls -a, and gitignored so it cannot ship. Test-editing sessions are legitimate; the gate only insists a person opened the door. - Pairs with the eval-gate Stop hook: one hook makes the suite unavoidable, this one makes it unfalsifiable. Together they close the loop the TDD-with-agents pattern depends on.
Failure modes
- Bash is the bypass. This hook sees Edit and Write;
sed -ithrough the Bash tool does not match. Add a Bash-matcher hook that greps the command for protected paths if your threat model includes determined workaround-seeking, and accept that pattern-matching shell strings is fragile (the security docs say as much). CI remains the outer gate that catches whatever slips through. - The override outlives the task. A forgotten
.allow-test-editsdisables the gate silently. Add its absence to your session-start checklist or a SessionStart hook that warns when it exists. - Protected patterns drift from the repo. New test layouts (a
__tests__/directory, a.featuresuite) need adding toPROTECTED; a gate that guards last year’s layout guards nothing. - Blocking frustrates legitimate red-to-green work. Writing a new failing test first is the workflow this site recommends; that session needs the override on from the start, which is the point: the human states the intent before the agent touches the suite.
Verified against PreToolUse blocking semantics (exit 2, stderr feedback), the Edit|Write matcher shape, and the memory-vs-hook enforcement line at code.claude.com/docs/en/hooks and code.claude.com/docs/en/memory, 2026-07-18.