Artifact 9a72f6b1f6acb9fbca0d85781e339f5766ac8b3615d596bcd6f651a4f548699b:
- File
docs/hooks.md
— part of check-in
[1165d70f02]
at
2026-11-15 16:50:31
on branch trunk
— Phase 8b: the hook dispatcher runs what is linked into an event's slot
A harness is told once to call suit-run hook EVENT. After that, which programs run for the event is decided by what suit has linked into slots/hook/EVENT: one program, or a folder of programs run one after another in byte order of name. An outfit names them with slot hook/EVENT = PATH or slot hook/EVENT/NAME = PATH. Asking for both forms of the same event is an error at plan time that names both outfits.
The dispatcher lives in suit-run, the small program, because some harnesses run a hook on every tool call. With no slot for the event it exits 0 at once, having read nothing. Otherwise standard input is read once into an unlinked temporary file, at most 16 MiB, and every program gets the same bytes from the start. A program that exits 2 stops the rest and the dispatcher exits 2. Otherwise all run and the result is the first non-zero status; a signal counts as 128 plus its number, and a program that cannot be run as 126. The dispatcher has no timeout of its own and prints nothing on success.
suit hook EVENT calls the same code. suit hooks lists what would run and changes nothing.
The speed budget has a second measurement: suit-run hook with no slot, 0.15 to 0.29 ms over an empty program against the 1 ms limit. The no-change run is unchanged and still passes. Stripped suit-run grew from 35064 to 35512 bytes.
1355 unit checks, 598 shell cases (was 479), 669 store cases, 756 placement cases (was 745) and 378 project cases, under clang, under gcc 16, with no user name set, and under a strict umask, in one run of make gate (35 minutes). 100% coverage in all 25 source files. No suite has fewer cases than at the last check-in. (user: dmestas, size: 4148) [annotate] [blame] [check-ins using]
Hooks
A harness runs hooks: programs it starts at events such as Stop or PreToolUse, passing a JSON document on standard input and reading the exit status, standard output and standard error. Suit tells the harness once, by a need, to call one fixed command for an event. After that, which programs run is decided by what suit has linked into slots.
The fixed command is suit-run hook EVENT, the dispatcher. It lives in the
small front program, not in suit, because a hook can run on every tool call
and must start fast (see decisions.md). suit hook EVENT in
the big program calls the same code, for convenience and testing.
Layout
An outfit says slot hook/EVENT = PATH for one program, or
slot hook/EVENT/NAME = PATH for several. suit apply links them at
$SUIT_HOME/slots/hook/EVENT and $SUIT_HOME/slots/hook/EVENT/NAME. For one
event, the slot is a link to one program or a folder of links, never both. If the
outfits that apply need both for one event, suit plan and suit apply stop
with an error naming both outfits. Nested slot names needed no code change in
placement; they are tested in test/place.sh.
Rules
- EVENT is 1 to 64 characters of letters, digits and
. _ -. Anything else is a usage error, exit 64. - Nothing at the slot: exit 0 at once. Standard input is not read and nothing is printed.
- Otherwise the programs are the one link (or file), or every entry of the
folder whose name does not start with
., in byte order of name. More than 64 is an error. - Standard input is read once, fully, into an unlinked temporary file in
$SUIT_HOME(orTMPDIR), up to 16 MiB. Over that is exit 65 naming the event, and no program runs. A terminal or closed standard input gives every program an empty input. - Each program runs directly, never through a shell, one at a time in order, with
the dispatcher's environment plus
SUIT_HOOK_EVENT=EVENT. Its standard input is the spooled file at offset 0, so every program gets the same bytes. Its standard output and standard error are the dispatcher's own. argv[0] is its path and argv[1] is EVENT. - A link whose target is missing or not executable prints
suit: hook EVENT/NAME: cannot run: REASON, counts as status 126, and the rest still run. - The dispatcher adds no output of its own on success.
- No timeout of its own: the harness has one.
Exit status
| Result | Dispatcher exits |
|---|---|
| No slot, or every program exits 0 | 0 |
| A program exits 2 | 2, at once; no later program runs |
| No 2, but some program exits non-zero | the first non-zero status |
| A program killed by a signal | counts as 128 plus the signal |
| A program cannot be started | counts as 126 |
| Bad EVENT | 64 |
| Input over 16 MiB | 65 |
Exit 2 means "block" in the harnesses that have blocking hooks, so nothing after a block runs.
Register once in a harness
The wardrobe holds needs/claude-stop-hook.md:
Open ~/.claude/settings.json and add to "hooks" an entry for "Stop" whose
command is "suit-run hook Stop". Keep every other setting as it is.
and the config says:
[need claude stop-hook]
prompt = needs/claude-stop-hook.md
done = ~/.claude/settings.json contains "suit-run hook Stop"
After that, slot hook/Stop/announce = files/tts/announce.sh in an outfit is all
it takes to run a program at Stop. suit hooks lists, one line per event and
program in order, EVENT NAME -> TARGET, with (missing) or
(not executable) where true. It changes nothing.
Speed
With no slot present, suit-run hook NOSUCHEVENT is checked by make
check-budget in the same way as the no-change run: the median of per-round
differences over the empty program must be under 1 ms. Measured on the
development machine: 0.15 to 0.29 ms (the no-change run: 0.47 to 0.83 ms).
suit-run grew from 35,064 to 35,512 bytes stripped.
Limits
- Programs run one after another, not at the same time.
- Several programs printing JSON to standard output for the same event will confuse a harness that parses it, so at most one program per event should print.
- There is no timeout of its own.