Wink Pings

The Agent Bypasses Your Rules at Step 40—The Problem Isn't the Model

A write-up on Harness Engineering: agent = model + harness. Rules in CLAUDE.md are just requests; hooks are the checks. 6 events, 3 ways to respond, and a Stop hook that makes “done” pass tests.

The original tweet shared a write-up on Harness Engineering. It breaks down coding agents such as Claude Code, Codex, and Gemini CLI, then boils it down to one line: agent = model + harness. The model collects the applause; the harness determines whether the agent can actually work. The original tweet included a video:

In the article, Valentin gives a very concrete scenario: you write “never run rm -rf” in CLAUDE.md, and forty steps later the agent still runs it. The reason isn't mysterious. Rules written into context are requests, and requests are weighed by the model alongside the other information it has. By step 40, it may treat that rule as an exception.

This is where the hook comes in. Rules ask the model to behave; hooks check whether it did. If it didn't, they stop the action. CLAUDE.md gets read into context and is good for project structure, common commands, and code style. Its ceiling is also right there: it cannot fail an action. If the model decides to make an exception this time, nothing stops it. A hook is a command bound to Claude Code lifecycle events; it runs when the event triggers. The model doesn't need to remember it, agree to it, or even know it exists.

The decision rule is simple: if violating the rule is just annoying, put it in CLAUDE.md; if violating it is costly, put it in a hook.

Hooks go in settings files: project-level .claude/settings.json, user-level ~/.claude/settings.json. Each hook specifies an event, an optional tool matcher, and the command to run. The hook receives JSON from stdin containing the tool name, tool input, session, and working directory. It reads the input, makes a decision, and returns.

Start with six events:

  • SessionStart: runs when a session starts, resumes, clears, or compacts. Good for loading live context such as open issues, the current branch, or what changed last night.
  • UserPromptSubmit: runs before Claude sees your message. You can add context to each prompt, or reject it outright.
  • PreToolUse: runs before a tool call and is the only gate. It can allow, ask, or deny.
  • PostToolUse: runs after a tool returns. The action has already happened; good for formatting, linting, and logging.
  • Stop: runs when Claude is about to finish. Block it, and Claude keeps working.
  • PreCompact: runs before context is trimmed. Write down the information you still want after compaction.

Two of these matter most: PreToolUse guards tool calls, and Stop guards “done”.

Hooks have three ways to respond. exit 0 with no output: no decision; go through the normal permission flow. exit 2: block, with the stderr content returned to Claude as the reason. exit 0 plus JSON: precise permissions; deny cancels the call and explains why, ask pops the normal confirmation, and allow skips confirmation, though deny rules still apply. Pick one style per hook; don't use exit 2 and JSON at the same time.

The rm example is worth looking at. The script reads stdin, extracts the command, and if it starts with rm, outputs “Blocked: rm is not allowed, move files to ./trash instead” to stderr and then exits 2. The key is the second half. If you only deny, the agent will try a different phrasing; if you give an alternative when denying, it will switch.

Fewer people write Stop hooks. Every agent says “done”; rarely is it asked to prove it. Stop runs when Claude is about to hand back the turn. Return decision: block and a reason, and Claude reads the reason and continues instead of ending. Put the test suite here, and only then is “done” not just a verbal claim. The script first checks stop_hook_active; if it is already continuing because of the stop hook, let it stop. When tests fail, send the failing tests and the last 20 lines of output back as the reason. There are two built-in brakes: stop_hook_active prevents infinite loops, and after eight consecutive continuations Claude Code ends the turn. The reason must be specific; don't just write “tests failed”. The agent can only fix what it can see.

There are five types of hooks: command runs a script, is the cheapest and fastest, and gives the same answer every time; http POSTs the same JSON to a URL, good for team-wide policy; mcp_tool calls a tool on a connected server; prompt sends the input to the model for a single judgment and returns JSON; agent launches a subagent that can read files and search before deciding, is the slowest, and is still marked experimental. Rule of thumb: if a rule can be described without “judge, evaluate, decide”, use a command hook. “Don’t write outside src/” is a command; “Does this change match the ticket?” is a prompt. Making every check a model call means paying judgment cost for a question with only one correct answer.

The rollout order can be very basic: find the rule you repeat most in chat, write it as a PreToolUse command hook, exit 2, and give an alternative in stderr. Then add a Stop hook that runs the fastest check—lint, type checking, or tests only on changed files. Then add a PostToolUse formatter to save reviews over whitespace. Put prompt and agent last, only for rules that scripts can't express clearly. Humans stay where hooks can't reach: merge, deploy, migration.

Three sentences sum up this discipline: rules the model can skip are suggestions; checks that cannot fail are decoration; the same correction twice should go into a hook.

In the comments, someone asked whether a Stop hook should block forever or let it pass when tests are flaky. The original article gives the built-in consecutive-attempt limit, but flakiness itself is still an engineering problem. A commenter said the same model with a different harness produces very different results, so when two teams benchmark “the same model” and their scores don't match, you have to look at the scaffolding. Someone else mentioned writing every assumption in a research project as a scripted test, able to run a 50-page summary and 200 citations, but this requires long-term investment and you still have to watch it while it runs. Most users want “clone GTA6, don't make mistakes, yolo”; these two things are not on the same engineering scale. Someone also posted an image:

评论区配图

The write-up mentioned in the original tweet is here, Valentin's hooks guide is here, and the discussion thread is included as well:

The model is responsible for looking good; the harness is responsible for being deliverable. When comparing agents, don't just look at the model name; first ask what's inside that shell.

发布时间: 2026-11-26 16:33