Skip to content

One source for the hooks block

amplihack's hooks reach Claude Code through the hooks object in settings.json. Several files can hold that object, and several amplihack commands write them. This page explains how they are kept in agreement. The exact rules are in the hooks block reference.

The problem it addresses

Before issue #2279, the hook list existed in more than one form: the spec table used by amplihack install, a separate canonical contract used for validation, an XPIA branch in register_hooks, and ad hoc checks in the launch repair and doctor. Each copy could fall out of step with the others.

The checks were also weak. The launch repair and doctor both treated a file as healthy if any one amplihack hook was present. A file missing StopFailure alone, or holding an old binary path, passed both. Profile settings.json files were copied from the home file at seeding and then never compared again, so a profile seeded from a stripped home file stayed stripped.

The visible symptom was a home settings.json of about 1010 bytes whose hooks object held only a foreign PreToolUse entry (a Bash guard), while env, permissions and the status line were intact. Every amplihack install restored the block, and the backups it wrote show the stripped file each time, so something rewrote the file between installs. Sessions started in that state had no hooks, so a session stopped by a usage limit was not resumed by limit-resume.

The program that stripped the file

The writer is the session-start step migrate_global_hooks in amplihack-hooks, released up to and including 1.2.132. The evidence is a reproduction, not a PID capture.

No PID capture was made. fanotify and bpftrace need root, sudo on the host needs an interactive password, and inotify reports that a file changed but not which process changed it. A capture is possible only from a root shell.

What the step did. It read the "project" settings file as <current folder>/.claude/settings.json. If that file held amplihack hooks, it removed every amplihack hook from ~/.claude/settings.json and left every other key and every foreign hook alone. In a session started with $HOME as its folder, the "project" file is the home file, so the step found amplihack hooks in the file it was about to strip and stripped it. No separate project file is needed. A search for project files holding amplihack hooks therefore cannot find this case: the only file involved is ~/.claude/settings.json.

Reproduction. On 11 November 2026 the installed ~/.local/bin/amplihack-hooks 1.2.132 was run by hand in a temporary folder used as both HOME and the current folder. The input was a SessionStart event, and the folder held only .claude/settings.json with env, a foreign Bash PreToolUse guard and amplihack's PreToolUse and SessionStart entries. The step printed "Removed redundant global amplihack hooks" and rewrote the file to env plus the foreign guard, with keys sorted.

Match with the live file. At the same time, the live ~/.claude/settings.json (1010 bytes, last written 08:51:40 local, four minutes after the 08:47:44 install backup) had exactly that shape: env, permissions, the status line and a hooks object holding only the foreign Bash guard, with keys sorted.

What this does not rule out. A second writer, such as a long-running Claude Code session saving settings it loaded while the file was stripped, is not excluded by the reproduction. That is why the design below does not rely on the home file alone: launch passes the block with --settings, and the rotator's repair is counted. amplihack profile rotate status prints hooks repaired: N in the last 24h. With the session-start step fixed, that count should stay at none. A count above zero means another writer is still removing the hooks, and it should be traced from a root shell with fanotify.

The design

One definition, one merge, one comparison

install/hook_block.rs builds the block from AMPLIHACK_HOOK_SPECS. The same module provides the merge every writer calls and the drift comparison every reader calls. The separate canonical contract is gone; install validation now reports the output of the drift comparison. A change to the hook list is made in one table and reaches every writer and doctor without further edits.

The home file is never stripped

The session-start step no longer removes the global hooks. With project-local hooks present, both copies stay, and a session started in $HOME leaves the home file byte-equal. This keeps the home file in the state that install, the launch repair, the rotator cycle and doctor all expect: if the step still removed the hooks, each of those would put them back and the next session would take them out again, rewriting the file on every cycle.

Every writer heals, none creates

Install, update, the launch repair, profile refresh, profile seeding and the rotator cycle all call the same merge. Only install creates a settings file; the others leave an absent file absent. Each writes only when the drift comparison reports a difference, so a healthy file is never rewritten and its modification time stays put.

The rotator check runs when a cycle finishes, not on a timer. It follows the existing cycle cadence and adds no interval or retry. The rotator itself does not write settings.json during a swap; the check exists so that a file stripped by something else is restored within one cycle.

Older binaries do not write

Several amplihack versions can be installed on one machine at once. A binary older than the install stamp would write an older block over a newer one, so the launch repair, the rotator heal and the launch settings file are all skipped when the running binary is older. Doctor still reports the drift, and amplihack install from the current release fixes it. A Claude step with extra command hooks is the one case where an older binary writes a settings file: the launch's own file, which no other process reads. Its amplihack hooks run from the installed release.

A settings source outside the user file

Healing the home file narrows the window in which a session can start without hooks, but cannot close it while some other process keeps rewriting that file. For sessions started through amplihack, the block is also passed with claude --settings <file>. Claude Code treats that file as an additional settings source, merged with the user file, and outside the reach of whatever rewrites ~/.claude/settings.json.

The launch file and the home file are generated by the same code with the same binary path, so their hook commands are byte-identical. Claude Code runs a handler once when the same handler is defined in more than one settings file (https://code.claude.com/docs/en/hooks), so a session that receives both files runs each amplihack hook once.

The injection is skipped when it would conflict with an explicit choice: a user-supplied --settings, or the LiteLLM proxy and safe mode, which disable settings sources on purpose.

XPIA stays off

The block holds no XPIA hook, and the merge removes any wrapper that points at tools/xpia/hooks/. No writer touches env, so XPIA_ENABLED keeps whatever value, or absence, the user gave it.

Trade-offs

  • Lost updates. Two processes changing hooks at the same instant can overwrite each other. The window is small and the next heal corrects it; adding locks across Claude Code and amplihack was judged not worth the cost.
  • File mode. The atomic writer creates files with mode 0600, so the home settings.json becomes 0600 after its first repair. The file can contain tokens in env, so the narrower mode is intended.
  • Two copies of the hooks. A project with project-local hooks now has them in the home file as well. Where both name the same command string, the question is the same as for the launch file. Where the project-local file uses a different path, such as an older tools/amplihack/ command, Claude Code treats them as different hooks and runs both. The removal step existed to avoid this; keeping the home file intact was preferred to a session without hooks.
  • More writes. Repairing on any drift, rather than only on a fully missing block, writes more often. Writes are atomic and happen only on change.