Skip to content
pi-c Hooks
Login

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:

{"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)

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 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, WebFetch, WebSearch), 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 puts the call to you as a question before it runs, in every permission mode, and a PreToolUse allow runs it without the mode asking (read-only still refuses a change). permission_mode in a hook's input is the conversation's mode in Claude Code's words: bypassPermissions for allow, plan for read-only, default otherwise. 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.

Elsewhere required has no effect.

{"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:

{
  "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 } ] }
    ]
  }
}

What you watch while a call runs can still show the raw output; 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.