HOOK · quality-cd

Stop hook: eval gate

A Stop hook that refuses to let the session end while the project's regression evals fail, with a per-session attempt cap so a broken suite cannot loop forever.

✓ verified 2026-07-18 · stack: claude-code · source

The graduation rule says a capability eval that starts passing consistently becomes a regression eval, run on every meaningful change. This hook makes “every meaningful change” mechanical: the agent cannot declare a session finished while the regression suite is red. Exit code 2 on a Stop hook prevents Claude from stopping and feeds the failures back as context (hooks).

.claude/settings.json (fragment)
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "node .claude/hooks/eval-gate.mjs",
"timeout": 120
}
]
}
]
}
}
.claude/hooks/eval-gate.mjs
#!/usr/bin/env node
// Stop gate: block session end until the regression evals pass.
// Exit 2 = do not stop (stderr goes back to the agent as feedback).
// A per-session attempt cap stops a genuinely broken suite from trapping
// the session in a loop.
import { execSync } from 'node:child_process';
import { readFileSync, writeFileSync, existsSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
const EVAL_CMD = process.env.EVAL_CMD ?? 'npm run --silent eval:regression';
const MAX_ATTEMPTS = 3;
const input = JSON.parse(readFileSync(0, 'utf8'));
const marker = join(tmpdir(), `eval-gate-${input.session_id}`);
const attempts = existsSync(marker) ? Number(readFileSync(marker, 'utf8')) : 0;
if (attempts >= MAX_ATTEMPTS) {
// Let the session end; a suite that stays red after three passes needs a
// human, not a trapped agent. The message still lands in the transcript.
console.error(`eval-gate: still red after ${MAX_ATTEMPTS} attempts; handing to a human.`);
process.exit(0);
}
try {
execSync(EVAL_CMD, { stdio: ['ignore', 'pipe', 'pipe'], timeout: 100_000 });
process.exit(0); // green: allow the stop
} catch (err) {
writeFileSync(marker, String(attempts + 1));
const tail = `${err.stdout ?? ''}\n${err.stderr ?? ''}`.trim().split('\n').slice(-30).join('\n');
console.error(`Regression evals are failing. Fix the causes, never the evals themselves.\n${tail}`);
process.exit(2); // red: block the stop, feed failures back
}

What it does

  • On every attempted session end, runs the project’s regression eval command (EVAL_CMD, defaulting to npm run eval:regression).
  • Green suite: the session ends normally. Red suite: exit 2 blocks the stop and the last thirty lines of failure output return to the agent as feedback, so the next turn starts with the evidence.
  • A marker file caps the loop at three blocked stops per session. After that it stands aside with a transcript note: a suite that stays red after three attempts needs a human decision, not a trapped agent.
  • The feedback line states the test-edit prohibition, because the shortest path to green is deleting the eval, and Anthropic’s harness guidance is blunt about it: “It is unacceptable to remove or edit tests” (effective harnesses). Pair this hook with a PreToolUse guard on the eval files themselves for enforcement rather than exhortation.

Failure modes

  • Slow suites hit the hook timeout. The settings entry allows 120 seconds and the script kills the run at 100. Point EVAL_CMD at the regression suite only, never the full capability suite: regression evals are the fast, near-100% set by design.
  • Stop hooks fire on every stop. Including stops where the user asked the agent to halt. The attempt cap keeps that annoyance bounded; if it grates, gate on a marker your CI or task runner sets when eval-relevant files changed.
  • The marker survives in tmp. Attempt counts are per session ID, so a resumed session inherits its previous count. That is usually the behaviour you want; delete the marker file if not.
  • Exit 2 semantics are Stop-specific. On PostToolUse, exit 2 cannot block (the tool already ran); this gate belongs on Stop precisely because that is where blocking still changes the outcome.

Verified against the Stop hook exit-code semantics, JSON input fields, and settings shape at code.claude.com/docs/en/hooks, 2026-07-18.