Skip to content
suit Artifact [9a72f6b1f6]
Login

Artifact [9a72f6b1f6]

Artifact 9a72f6b1f6acb9fbca0d85781e339f5766ac8b3615d596bcd6f651a4f548699b:


# 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](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` (or `TMPDIR`), 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.