From note to hook

A rule I give a coding agent starts as a sentence in a note. If the agent keeps breaking it, the rule moves up. Firmer wording first, then a line in a skill, then a hook that refuses the command before it runs. Where a rule lives decides how often it holds.

Every agent session starts with no memory of the last one. Whatever it should know about my work, my repos and my habits has to be in a file it reads. So the files matter, and so does where they live.

This is about two things I got wrong and fixed. The first was where the notes lived: inside checkouts I delete all the time. The second was where the rules lived: in sentences, which an agent can forget halfway through a long session.

Agents wrote much of the tooling here, with me directing. My part was deciding what had to be remembered, and how hard each rule had to be enforced.

Notes in a folder I delete

I work in parallel checkouts: one full copy of a three-repo stack per task, made with one command and removed when the task is done. Each copy had its own docs folder, and the agent wrote its notes into it.

That meant two problems. Deleting a checkout deleted whatever notes had been written only there. And every copy carried the same docs again. At one count, 11 checkouts held 92 MB of docs, and about 83 MB of that was duplicates.

Toggle between the two setups, then delete a checkout in each.

11 checkouts · 92 MB of docs · 83 MB of it copies

knowledge reponot there yet

Every checkout carries its own copy of the docs, plus a few edits that exist only there (amber).

I compared three fixes. Symlinks alone would stop the copying, but one bad command could still lose everything they pointed at. Copy-on-write clones would save the disk space and keep the drift. The third was a git repo for the notes, linked into every checkout, with removal made fail-closed: removing a checkout archives first, and stops if the archive fails.

I picked the third and did it in one day in July. The 14 copies that had drifted apart were merged by hand. About 30 memory files from different places became one set. The checkout tool learned to link _docs and tools into every new copy. And Claude Code’s memory directory was pointed at the repo, so every session in every checkout writes to the same place.

The checkout tool got a list of things to link rather than copy. Adding a shared folder later is one line.

Today the repo holds 845 doc files, 82 MB, and 20 tool folders. It’s tracked in git: 367 commits on 46 different days.

One fact per file

The memory itself is plain Markdown. One fact per file, with a short header saying what it is, and an index file that lists one line per note. The agent reads the index at the start of a session and opens the notes that matter for the task.

---
name: confirm-before-commit
description: Ask before every commit, push or deploy
type: feedback
---
Ask me before each commit, push or deploy, every time.

**Why:** an agent once read "here are the screenshots" as approval.
**How to apply:** show the diff, ask, and wait for a plain yes.
Related: [[no-trailer-in-commits]]

That’s an illustrative note, but the shape is the real one. Feedback notes carry two extra lines: why the rule exists and how to apply it. Without the why, an agent follows the letter of a rule and misses the cases it was written for. Notes link to each other with wikilinks, so a note about deploys can point at the note about confirmations.

I chose files over a database because the agent already knows how to use them. It can search them, read them, diff them and fix them, with tools it has anyway. So can I.

a feedback note, for exampleAsk before each commit, push or deploy. Screenshots are not approval.Why: an agent once took them as a yes.How to apply: show the diff, ask, wait.

87 notes in all. The counts are real; the example wording is written for this page.

Most of the 87 notes are project facts. The 12 feedback notes are where rules start.

The commit count tells a small story too. By month: 305 in July, 42 in August, 1 in September, 16 in October and 3 so far in November. The burst is the merge. After it, the commit rate fell because a note doesn’t need a commit to be useful. The next session reads it either way.

The ladder

Most rules stay a note forever, and that’s fine. Some don’t hold. When an agent keeps breaking a rule, the rule climbs.

  1. A memory note. One sentence and a why. Read at the start of a session.
  2. Firmer wording. Same place, stronger words, often the story of what went wrong.
  3. A skill instruction. A line inside the skill for the workflow the rule belongs to, loaded only when that skill runs.
  4. A hook. A script the harness runs on every tool call. It can refuse the call outright.

Each rung is read at a different moment, and that’s the whole difference. A note is in context at the start, and a long session can drift a long way from the start. A skill line arrives exactly when its workflow begins. A hook doesn’t depend on what the agent remembers. It runs every time.

Here is one rule’s real path. I didn’t want trailer lines naming a tool in my commit messages. Step through it.

Commit messages carry no trailer lines naming a tool.

The first two rungs failed the same way. The agent knew the rule when the session started. Deep into the work, it wrote a commit the way it usually writes commits, and the habit won.

The third rung didn’t fit this rule. A skill is right when a rule belongs to one workflow, like a check that only matters during a release. This rule was about every commit, made from any workflow or none. So it went straight to the harness.

A note asks. A hook decides. The agent still sees the rule either way: the hook’s refusal names the note.

Writing the hook

Claude Code runs PreToolUse hooks before a tool call happens. A hook gets the call as JSON on stdin. If it exits with code 2, the call is refused, and whatever it printed to stderr goes back to the agent as the reason.

This is an illustrative version of that kind of hook, not the one I run:

#!/bin/sh
# PreToolUse hook, matched on Bash. The tool call arrives as JSON on stdin.
cmd=$(jq -r '.tool_input.command // empty')

case "$cmd" in
  *"git commit"*) ;;   # only commits are checked
  *) exit 0 ;;
esac

if printf '%s\n' "$cmd" | grep -qiE '^[[:space:]]*made-with:'; then
  echo "blocked: no trailer lines in commits. See memory/no-trailer-in-commits.md" >&2
  exit 2               # 2 refuses the call; stderr goes back to the agent
fi
exit 0

And it’s registered once, in settings:

{
  "hooks": {
    "PreToolUse": [
      { "matcher": "Bash",
        "hooks": [{ "type": "command", "command": "~/.claude/hooks/no-trailer.sh" }] }
    ]
  }
}

Three things I’d keep from it.

The message names the note. A bare refusal makes the agent guess and try variations. A refusal that says what’s wrong and where the rule is written gets a correct retry, usually on the first go.

The pattern stays narrow. It checks commits and nothing else. A hook that blocks too much gets in the way of honest work, and a hook that’s in the way gets switched off.

Decide what happens when the check itself breaks. In this sketch, if jq is missing the command reads as empty and the call goes through. For a trailer, that’s fine: the worst case is a line I remove by hand. A hook guarding something that can’t be undone should fail the other way, and refuse when it isn’t sure.

Which rules climb

I don’t keep a table of which rule sits on which rung. What I have is a test for deciding, and the rules that taught it to me. Some of the rules I’ve added this way:

  • Ask before each commit, push or deploy. This came from an agent that treated “here are screenshots” as approval.
  • Ask before creating issues in the tracker.
  • Smoke tests use cheap models, picked by label.
  • No reproduction files in product commits.
  • Before editing a bug, check whether it’s already fixed on the current branch.

The test has two questions. Can a program check the rule from the tool call alone? And does one slip cost more than a wrong block now and then?

“No trailer lines” passes both: it’s a pattern in a command, and a slip lands in shared history. “No reproduction files in product commits” could pass too, if the files have a recognisable name or folder. “Ask before each commit” fails the first question. A script can see that a commit is about to happen, but it can’t see whether I said yes in the conversation. That one stays in prose, written as firmly as I can, and I watch it. “Check whether it’s already fixed” fails both: it needs judgement, and a slip costs some wasted work, not damage.

So the ladder isn’t a promotion everyone gets. Rules climb only as far as a check can follow them. Past that, they stay sentences, and the job is to write the sentence well: the rule, why it exists, how to apply it.

Hooks can also travel with a skill pack, so a team gets the rule and its enforcement together.

What changed

Notes now outlive the checkouts they were written in. Every session, in every copy, starts from the same 87 notes. And the few rules that kept failing in prose no longer depend on an agent remembering them at the right moment.

The general lesson is short. Link shared state into anything you throw away; never copy it in. And if a written rule keeps failing, stop rewording it. Move it out of the prompt and into the harness, if a program can check it. The same idea, applied to an agent’s output instead of its commands, is in the gates I put around an agent.