← sources

I built a hook that stops Claude Code when it drifts off the task, and hands the rule back so it fixes itself

source: reddit (reddit:ClaudeCode)published 2026-09-26 19:18ingested 2026-09-27 02:39

retained text

I’m Yerins Abraham, I build an AI support product (Lira) from Lagos and I use Claude Code every day. The thing that kept biting me wasn't bad code. It was Claude doing things I never asked for: touching files outside the task, adding a package, editing something CLAUDE.md said not to touch. Nothing fails when that happens. The build stays green.

So I built trackline. It runs as a Claude Code hook and checks every tool call against what you actually asked for and your project's rules. Five checks right now:

\- off-limits: writes to .env, keys, credential files  
\- dependency-added: a package you never mentioned  
\- scope: a write into an area your request didn't mention  
\- diff-size: a change way bigger than the request  
\- repetition: the same action over and over (usually means it's stuck)

By default it only writes things down and never interrupts you. You can turn on "auto" for off-limits, and then it blocks the action and tells Claude why. The question I had to answer before building any of this was: does Claude actually correct itself when a hook blocks it and explains? I tested it. 11 out of 11 times it did (Claude Code and Codex), and 0 times did it write the forbidden file.

What I learned:

\- The block message matters. "secrets.env is off limits, put it in config.local.json instead" works. I haven't tested a bare "no" yet.  
\- Speed matters more than I expected because it runs on every single tool call. My first version was Node: 88ms per call. Rewrote the hook in Go: 6.5ms.  
\- False alarms kill a tool like this on day one, so every check stays quiet unless it can name exactly what it saw. 0 false alarms across 23 ordinary actions in my testing.

What it can't do yet: the scope check stays silent if your request doesn't name a file or folder, and it only partly understands shell commands (installs, rm, mv, cp, redirects, sed -i).

I built most of it with Claude Code itself, which was a bit funny given what it's for.

Free and open source (Apache-2.0):

npm install -g trackline  
cd your-project  
trackline init

GitHub: https://github.com/yerinsabraham/trackline

Would love to hear what drift you see most often. That decides which check I build next.

document fetched 2026-09-27 00:37 · canonical https://reddit.com/r/ClaudeCode/comments/1wqz10o/i_built_a_hook_that_stops_claude_code_when_it