Leonid Huopolainen
All notes

A rule asks, a hook forces

Translated from Russian. Original

A rule asks, a hook forces

Under the post about context, Konstantin wrote that CLAUDE.md is an unstable thing, and skills and hooks behave better. I agree. I have worked with Claude Code since March, the first hooks came at the end of June, and since then almost every important rule sooner or later moves into a hook

A hook is a small script that Claude Code runs by itself: before a command, before a file edit, before the agent finishes its answer. The model cannot forget it or "creatively reinterpret" it. If the hook says no, the action does not happen, the agent sees the reason and does it another way

This is how a rule lives for me. First it goes into context: into CLAUDE.md, into a skill, into a process description. For a while the agent follows it, then in a long session or in a subagent it gets lost. An incident happens, and the rule becomes a hook. Almost every hook has a note in its header about which case created it

The most useful one catches a promise instead of work. The agent spent 12 minutes on a task, gave a report and ended it with "starting the implementation, will send when there is something to show". And stopped. It started nothing, it just waited for me to write "start". It looks like work, but in fact the session is dead, and you learn about it an hour later. Now a hook looks at the end of the answer: if there is a promise and no action, the agent cannot stop and goes back to work

More examples:
– a session read a local copy of the code that was 700 commits behind main, and told me with confidence that the feature did not exist. Another one spent eight hours fixing what had already been fixed the day before. Now a hook does not let it read code from an old copy
– on a Mac, pkill reads flags after the pattern as more patterns. A subagent wanted to kill its own server and killed all terminal tabs, almost all Claude sessions, Chrome and the work messenger. A day later another subagent did the same. Now the hook blocks such a command
– a manual web server restart twice left prod without HTTPS. Now it goes only through a wrapper that checks the config and rolls back by itself if something fails
– in one week there were about 16 "done, checked" that nobody had opened. Half of the bugs were found by me or a client. Now you cannot say "done" about a page until it has been opened in a browser on phone and desktop
– in two weeks I asked "are you stuck?" 27 times. The agent started long work in the background and went silent. The rule "do not go silent" existed, but it depended on the model's memory. Now a hook marks every background task, and a separate watcher writes to me if it is quiet for 15 minutes

And there is a rule that so far lives only as text: before editing anything that is not in git, make a .bak. It came after the agent tested a token refresh on a live account, the old tokens died, there was no copy, and all the automation stopped. It is followed quite well, but it is already clear where it will go)

A hook can also fail if it again relies on the model. The first version of the hook before context compaction asked the agent to fill in a form: what is in progress, which branches, where it stopped. In a month there were 55 forms, all empty. I rewrote it so the hook collects the state by itself

So a rule explains why it is needed, and a hook makes sure it is followed. If a rule is important and has already been broken once, its place is in a hook

There is also a third part: guards. These are checks on every pull request that block the merge. For example, they catch when the agent weakened or removed a test to get green. But this is more about the development flow, not about Claude Code for non-code tasks, so I will write about it separately