A rule written in a prompt is a request. Agents follow it most of the time, and "most of the time" was not good enough for two specific mistakes. One of them happened five times after the rule was written. Both are now checked by a script that runs before the tool call and can refuse it, with a reason the agent reads and acts on.

The problem

Both mistakes come from an agent doing the natural thing.

Editing the wrong copy. A plan names files by absolute path. The paths were written while looking at the main tree. A worker running in an isolated worktree receives the plan, opens the file at the path it was given, and edits the main tree. The change is real, it is in the wrong place, and the worktree that was supposed to hold it is empty. The main tree now has uncommitted edits that belong to a task nobody is tracking there.

Staging everything. A worktree needs the dependency folder and the environment file of the main checkout, so they are symlinked in. git add -A stages the symlinks. When that branch is merged, git replaces the real folders in the main tree with what the branch says they are. In June 2026 this deleted real directories on a merge.

The first response to each was a sentence in the worker’s instructions: use the worktree path, name the files you add. The edit mistake came back five times.

What a hook is

The agent tool can run a command before each tool call. The command receives the call as JSON on standard input: which tool, with what arguments, from which working directory. It can let the call proceed, or answer with a denial and a reason. The agent sees the reason as the result of its call.

That last part is what makes hooks better than a failed command. The agent is not left with a cryptic error. It is told what it tried, why that is refused, and what to do instead.

Two flows. guard-main-tree: the agent calls Edit or Write with an absolute file path; the PreToolUse hook asks whether the path is inside a product repository's code folders; if yes, it asks whether that repository has a live worktree, using git worktree list; if yes, it denies with the reason and the worktree path to use. A note says docs, content and tmp folders pass, and so does everything when no worktree exists. guard-git-add-all: the agent calls Bash with git add -A, --all or dot; the PreToolUse hook asks whether the command stages everything; if yes, it asks whether this is inside a worktree, by working directory or by a path in the command; if yes, it denies and tells the agent to name the changed files instead. A note says in the main tree the hook does nothing, since only a worktree holds the symlinks that make staging everything dangerous.
Figure 1. The two hooks. Each tests two conditions and denies only when both hold, so ordinary work is never interrupted.

Guarding the main tree

The first hook listens to the file-editing tools. It denies a write only when two things are true.

ConditionHow it is tested
The target is code in a product repositorythe path matches one of the product repositories and one of its code folders
That repository has a live worktreegit worktree list for the repository returns more than the main tree

If no worktree exists, nobody is working in isolation and an edit to the main tree is ordinary work. Documentation, content and scratch folders pass in every case, because editing those in place is allowed.

The denial names the file, lists the live worktrees and tells the agent to edit under the worktree path. For the rare case where I do want an edit in the main tree while a worktree is open, there is an explicit switch: a marker file that makes the hook step aside, which I create and remove by hand. An agent is not told to use it.

The comment at the top of the script records why it exists. Translated: workers under built-in isolation edited the main tree using the plan’s absolute paths, and it recurred five times. “A prompt could not stop it, so the harness does.”

Guarding the staging area

The second hook listens to shell commands.

pat='git([[:space:]]+-C[[:space:]]+[^[:space:]]+)?[[:space:]]+add([[:space:]]+-[a-zA-Z-]+)*[[:space:]]+(-A|--all|\.|\./)([[:space:]]|$|;|&|\|)'
if ! printf '%s' "$cmd" | grep -Eq "$pat"; then
  exit 0
fi

in_wt=0
case "$cwd" in *-worktrees/*|*/.claude/worktrees/*) in_wt=1;; esac
case "$cmd" in *-worktrees/*|*/.claude/worktrees/*) in_wt=1;; esac
[ "$in_wt" -eq 0 ] && exit 0

The pattern matches git add followed by -A, --all, . or ./, with or without a -C <path> in front. The match must end at whitespace, the end of the command or a shell separator, so git add ./src/file.py is not caught.

Whether the command runs inside a worktree is decided two ways, because an agent can be standing in the main tree and still point git at a worktree with -C. If neither the working directory nor the command mentions a worktree folder, the hook exits and the command runs.

Only then does it deny, with a message that names the incident and says to add the changed files by name. In the main tree the hook never interferes. Staging everything there is merely untidy. In a worktree it is the thing that deleted directories.

Why two conditions each

A hook that fires on every edit, or on every git add, would be turned off within a day. Each hook is narrow on purpose.

HookWould be too broad asNarrowed by
main tree“never edit the main tree”only code folders, only while a worktree is live
staging“never use git add -A”only inside a worktree

The narrowing is what lets them stay on permanently. Neither has needed an exception since, apart from the manual switch on the first one.

Failure modes

SituationWhat happens
The hook’s input is malformed or has no pathit exits and allows the call; a broken hook must not block all work
The JSON tool is missingsame: allow
The agent tries the same edit againsame denial, same reason
The agent switches to the worktree pathallowed
I need a main-tree edit during a taskI create the marker file, make the edit, remove the file

Failing open is a deliberate choice. These hooks guard against a known mistake. They are not a security boundary, and a hook that blocked every call when its own dependencies broke would cost more than the mistake it prevents.

Limits I know about

The main-tree hook matches repositories by folder name. One product repository was renamed after the hook was written, and the pattern still lists the old name, so that repository’s main tree is not covered today. I found this while writing this page. The fix is one word in a pattern, and the better fix is to derive the list from the worktrees that exist instead of naming repositories at all.

The staging hook reads the command as text. A command that stages everything through an alias or a script would pass. The forms that occurred in practice are the ones the pattern catches.

Both hooks cover mistakes I have already seen. They say nothing about the next one. The method is what carries over: when a written rule fails more than once, stop rewriting the sentence and find the place where the action can be checked.