# Hooks

pic runs command hooks written for Claude Code. They live in `~/.pic/hooks.json`, and in the
workspace's `.pic/hooks.json` once you trust it (`pic trust yes`). Both files use Claude Code's
`hooks` object, and pic never writes them:

```json
{"hooks": {"PreToolUse": [{"matcher": "Bash|Read", "hooks": [{"type": "command", "command": "ctx gate", "timeout": 5}]}]}}
```

## What a hook receives

The events are SessionStart, UserPromptSubmit, PreToolUse, PostToolUse, PreCompact, Stop and
SessionEnd, with Claude Code's input on stdin plus `"harness":"pic"`. [The hook event
reference](../reference/hook-events.md) says when each fires. `session_id` is the conversation's id,
the same id its Pi session header carries. `transcript_path` is `~/.pic/pic.sqlite`, not the
journal, which pic keeps locked while it runs. Before every hook runs, the conversation's Pi lines
in that file are committed through its latest entry, even mid-request. The file is never locked
while a hook runs, so a hook can read the conversation there. `Edit` also matches as `MultiEdit`. A
hook runs with pic's whole environment, API keys included, plus `PIC_HOOK=1`. Only configure
commands you trust. A matcher accepts pic's tool names and Claude Code's (`Bash`, `Read`, `Edit`,
`Write`, `Grep`, `Glob`, `LS`), and `tool_input` carries `file_path` as well as `path`.

## What pic does with the output

pic reads exit 2, `additionalContext`, `permissionDecision` and `decision: block` the way Claude
Code does. A PreToolUse `ask` runs the call, because pic has no permission prompts. A PreToolUse
hook that allows a call can also rewrite it with `hookSpecificOutput.updatedInput`: the object is
merged into the call's input in the journal before the tool runs, so a resumed run executes the
rewrite and not the original. A PostToolUse hook can replace what the model sees of a finished
call with `hookSpecificOutput.updatedToolOutput`: the text parts of its `content` are written over
the call's result and the `tool_result` entry, in one transaction, so the original text is not left
behind. Context a hook adds is journaled, so a resumed conversation sends it again without running
the hook again. A hook that times out (default 10 s, at most 60), crashes or prints bad JSON is
ignored and recorded.

## Lifetime and crashes

A hook dies with pic, and Ctrl-C stops it. pic kills a hook's process group only at its timeout,
when pic dies, or on Ctrl-C. pic bounds the hook command, not what it backgrounds; use `async: true`
for work that should outlive the hook under a timeout. To clean up children yourself, `wait` for
them or `trap 'kill 0' EXIT`. Each run is journaled before it starts. If a crash cuts one off, it is
never run again and its output is not used. A tool call whose PreToolUse hook was cut off does not
run. A steer or follow-up sent with `--commands` is answered "queued" at once. Its prompt hooks run
before it can be delivered, after a crash too, and a block returns it (`queue_returned`, with the
reason). A follow-up sent after the reply has finished becomes the next request at once, so its
hooks can add context but cannot block it.

`pic hooks` lists the configured hooks, and `pic dump` lists every run.
