# 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}]}]}}
```

Each hook takes these fields:

| Field | Type | Default | |
|---|---|---|---|
| `type` | string | | `command`; other types are skipped |
| `command` | string | | run with `/bin/sh -c` in the workspace, the event's JSON on stdin |
| `timeout` | seconds | 10 | at most 60; its process group is killed then |
| `async` | boolean | false | not waited for; its output is not used |
| `required` | boolean | false | fails closed ([required hooks](#required-hooks)) |

## 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; the whole of a long output, which `read` could fetch, goes with it. A hook reads the
conversation in `transcript_path` up to the results its PostToolUse hooks have not finished with,
so the original is not written there either. 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, unless it is required.

## Required hooks

A hook with `"required": true` fails closed. It is always waited for (`async` is ignored), and it
must finish: exit 0 with nothing or JSON on stdout, or block on purpose with exit 2. A timeout, any
other exit, a crash of the hook or of pic, Ctrl-C, or stdout that is not JSON means it did not.

- A required PreToolUse hook that does not finish denies the call; the model is told the hook did
  not finish.
- A required PostToolUse hook that does not finish withholds the output: the model sees `[output
  withheld: the required hook COMMAND did not finish]` in its place. pic writes that notice over
  the result before the hook starts and puts the output (or the hook's replacement) back only once
  every required hook has finished, so a crash at any point leaves the notice.
- A command you run yourself (`!COMMAND`, `pic bash`) gets PostToolUse as tool `Bash`, with
  `tool_input.command` and `tool_response.content`, before its output is recorded: what the hooks
  leave is all that is recorded and sent. Its `tool_use_id` and `session_id` are empty, and context
  or a block it returns is recorded but not sent.

Elsewhere `required` has no effect.

```json
{"hooks": {"PostToolUse": [{"hooks": [{"type": "command", "command": "ctx guard post",
                                       "required": true}]}]}}
```

### What you can still see

Requests are built from the journal, so the model only ever gets what the hooks left. What you
watch is not: while a call runs and until its PostToolUse hooks finish, the terminal, `pic attach`,
the `--json` and RPC event streams (`tool_execution_update` and `tool_execution_end`), and a phone
paired with `pic remote` can show the raw output, and what they have shown stays on screen. A
follower that reads the journal (`pic attach`, the phone) shows the result as it was when it read
it, which is the raw text only if it read it before the hooks began. Opening the conversation again
shows what the hooks left. For `!COMMAND` the output streams as it runs, and once it ends pic shows
and records what the hooks left.

## Keeping secrets out of the model

Whatever a tool reads goes to the model, and stays in every later request of the conversation. A
guard on file names alone is easy to get around (`cat .env`, a script, `env`). The dependable place
to stop a secret is the output, after the tool has run and before the model sees it, and a required
hook makes that hold even if the hook fails.

ctx, the session archive, ships such a guard, `ctx guard`, which works with
pic and with Claude Code. In `~/.pic/hooks.json`:

```json
{
  "hooks": {
    "PreToolUse": [
      { "matcher": "Read|Grep|Bash",
        "hooks": [ { "type": "command", "command": "ctx guard", "timeout": 3 } ] }
    ],
    "PostToolUse": [
      { "matcher": "*",
        "hooks": [ { "type": "command", "command": "ctx guard post", "timeout": 5,
                     "required": true } ] }
    ]
  }
}
```

- **After a tool runs** (`ctx guard post`): it replaces every secret in the output before the model
  sees it, however the secret was read. Because the hook is required, output it could not check
  (it timed out, failed, or the output was over 16 MB) is withheld, not sent. It covers `!COMMAND`
  and `pic bash` too.
- **Before a tool runs** (`ctx guard`): it refuses a direct read of a secret file, such as `.env`
  but not `.env.example`, keys, `~/.aws/credentials` or `.netrc`, and says why. This is a courtesy,
  not the protection: an agent that wants the file can reach it another way, and the PostToolUse
  hook is what stops it there.
- In Claude Code only the PreToolUse half applies, since Claude Code cannot replace the output of
  its own tools.

What you watch while a call runs can still show the raw output; see
[what you can still see](#what-you-can-still-see).

## 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 required one counts as not finished. 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 (a required one says `required` where others say `sync` or
`async`), and `pic dump` lists every run.
