Artifact 1d057fa988be79c6f19b9120e0532f2195d16a45b551381cf7f09d66021ec408:
- File
docs/hooks.md
— part of check-in
[d03820d0a9]
at
2026-11-15 17:53:12
on branch trunk
— Hook programs get the event's input read-only
The dispatcher gave every program the spooled input through the one descriptor it wrote with, so a program that wrote to its standard input could change the bytes the next program read. The spool is now opened a second time, read-only, before it is unlinked; the copy is written through the first descriptor, which is then closed, and the programs inherit the read-only one. If the second open fails the message is cannot reopen the temporary file, the exit status is 1, no program runs, and no temporary file is left.
8 new shell cases: a program that tries to write to its input fails and the next program still reads the original bytes, and the new failure in the home folder and in the temporary folder.
1355 unit checks, 606 shell cases (was 598), 669 store cases, 756 placement cases 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. The speed budget still passes. No suite has fewer cases than at the last check-in. (user: dmestas, size: 4179) [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. The file is read-only to them. 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.