Skip to content
pi-c Documentation
Login

Documentation

/* Command hooks in Claude Code's shape (hooks.c), so tools written for its
 * hooks (ctx) work unchanged. The configuration is the "hooks" object of
 * ~/.pic/hooks.json, then of WORKSPACE/.pic/hooks.json when the workspace is
 * trusted; pic never writes either file:
 *   {"hooks": {"PreToolUse": [{"matcher": "Bash|Read",
 *     "hooks": [{"type": "command", "command": "ctx gate", "timeout": 5}]}]}}
 * Events: SessionStart, UserPromptSubmit, PreToolUse, PostToolUse,
 * PreCompact, Stop, SessionEnd. A command gets the event's JSON on stdin and
 * runs with /bin/sh -c in the workspace, pic's environment plus PIC_HOOK=1,
 * in its own process group, which a watchdog kills at its timeout (seconds,
 * default 10, at most 60), on Ctrl-C, or when pic dies. Each run is
 * journaled as started before the command runs and done after (pic_hook_run;
 * pic dump lists them); one a crash interrupted is never run again, and its
 * event's safe default applies (a PreToolUse call does not run). A hook that
 * fails in any other way than its event's blocking exit 2 is ignored for the
 * run and recorded, unless it is "required": true, which fails closed: a
 * required PreToolUse hook that does not finish cleanly denies the call, and
 * a required PostToolUse hook that does not withholds the output. */
#ifndef PIC_HOOKS_H
#define PIC_HOOKS_H

#include <stdint.h>
#include <stdio.h>

struct pic_store;
struct sqlite3;
typedef struct pic_hooks pic_hooks;

/* The configured hooks, or NULL when no file configures any. A hook's
 * transcript_path is ~/.pic/pic.sqlite (pic_userdb_path), whose
 * picu_pi_session_line rows hold the conversation as Pi lines, written
 * through its latest entry before every firing; the journal itself is
 * locked while pic runs. OWNER, when not NULL, is the token of
 * a parent process that owns the conversation's lifecycle (the terminal
 * interface, through PIC_HOOK_OWNER): this process fires SessionStart only
 * once per conversation for it and never SessionEnd. A file that does not
 * parse is skipped with a warning on WARN. */
pic_hooks *pic_hooks_load(const char *workspace, const char *owner, FILE *warn);
void pic_hooks_free(pic_hooks *h);
/* pic hooks: one line per configured command (event, matcher, timeout,
 * command, file), tab-separated. */
int pic_hooks_list(const char *workspace, FILE *out);
/* Whether any hook is configured for EVENT. */
int pic_hooks_has(const pic_hooks *h, const char *event);
/* Whether a parent owns the lifecycle (pic_hooks_load's OWNER was given). */
int pic_hooks_owned(const pic_hooks *h);

/* One command's run: how it ended and what it said. */
typedef struct {
    char *command;
    int exited, code;   /* exited: the command exited with code */
    long ms;
    char *error;        /* why it was ignored, or NULL */
    const char *outcome; /* "allow", "ask", "deny", "block" or NULL */
    char *reason;       /* a deny's or block's reason */
    char *context;      /* additionalContext (or plain stdout where it counts) */
    char *updated_input;  /* PreToolUse: the input to run instead, a JSON object, or NULL */
    char *updated_output; /* PostToolUse: the result to show instead, JSON, or NULL */
    int interrupted;    /* cut off by Ctrl-C: journaled as interrupted */
    int clean;          /* it ran to the end and its answer was read */
} pic_hook_run;

/* A firing: every matching command of one event, run in order. */
typedef struct {
    const char *event;
    char session[64];
    int64_t submission, ref;
    pic_hook_run *runs;
    size_t n;
    int marker;   /* journal a row even when no hook matched */
    int blocked;  /* a deny (PreToolUse) or block (UserPromptSubmit, PostToolUse, Stop) */
    char *reason; /* the reasons, one per line */
    int64_t first; /* the firing's first pic_hook_run row (0: none) */
    char *unfinished; /* the first required hook that did not finish cleanly, or NULL */
    int ordinary; /* run only the hooks that are not required */
} pic_hook_fire;

/* Runs EVENT's hooks whose matcher takes one of NAMES (NULL-terminated; NULL
 * for events without a matcher, which take every hook) with the common
 * fields and the members of EXTRA (a JSON object) on stdin, each journaled
 * in transactions of its own: never call it inside one. 0, or -1 on a store
 * error (s->err). */
int pic_hook_fire_run(struct pic_store *s, pic_hook_fire *f, const char *const *names, const char *extra);
void pic_hook_fire_free(pic_hook_fire *f);
/* The names a matcher may give TOOL by: pic's, then Claude Code's (Edit
 * also as MultiEdit), NULL-terminated. */
void pic_hooks_tool_names(const char *tool, const char *names[4]);

/* The firings pic makes, each journaled in its own transaction. 0, or -1 on
 * a store error (s->err). */
/* SessionStart for SESSION (source "startup" or "resume"), once per
 * conversation and owner. */
int pic_hooks_session_start(struct pic_store *s, const char *session, const char *source);
/* UserPromptSubmit: 1 when a hook rejects the prompt (s->err says why). ORIGIN (NULL: "user") is
 * who sent it: an inbox sender such as session:JOURNAL/CONVERSATION, in the input as origin.
 * *first is the firing's first row: the admission that takes the prompt
 * claims its context (pic_hooks_claim); unclaimed, it is never sent. */
int pic_hooks_prompt(struct pic_store *s, const char *session, const char *prompt, const char *origin, int64_t *first);
/* Inside a transaction: the unclaimed UserPromptSubmit runs from FIRST on
 * become SUBMISSION's, or (SUBMISSION 0) queued message QUEUED's, whose
 * delivery hands them on (pic_hooks_claim_queued). */
int pic_hooks_claim(struct pic_store *s, int64_t first, int64_t submission, int64_t queued);
/* Inside the transaction that queues message QUEUED: when prompt hooks are
 * configured, they are journaled as pending for it. */
int pic_hooks_mark_pending(struct pic_store *s, int64_t queued);
/* Runs the prompt hooks (SessionStart, then UserPromptSubmit) of every
 * message pending them, oldest first, outside any transaction. A block
 * takes a still-queued message out of the queue, unrun (queue_returned with
 * the reason, then queue_update); a message an answer already admitted gets
 * their context but can no longer be blocked. Driven by the journal, so a
 * message queued before a crash is hooked before it is delivered. */
int pic_hooks_run_pending(struct pic_store *s);
/* Inside a transaction: queued message QUEUED, delivered into SUBMISSION,
 * brings its UserPromptSubmit context. */
int pic_hooks_claim_queued(struct pic_store *s, int64_t queued, int64_t submission);
/* SessionEnd for SESSION, when this owner started it and has not ended it. */
int pic_hooks_session_end(struct pic_store *s, const char *session, const char *reason);
/* SessionEnd for every conversation this owner started and has not ended. */
int pic_hooks_end_owned(struct pic_store *s, const char *reason);
/* PostToolUse for SUBMISSION's finished calls of its latest turn that have
 * not had it yet. Where a required hook matches a call, the call's result is
 * withheld in the journal before any hook runs, and given back (or the
 * hook's replacement written) only when every required hook finished
 * cleanly: a crash or a failure leaves the notice, never the output. */
int pic_hooks_post_tools(struct pic_store *s, int64_t submission);
/* PostToolUse for a call another call made (pic_tool_hooks.call): its ordinary hooks only (a
 * required one runs on the calling tool's own result), journaled under REF (minus the inner
 * call's row), with the caller's PARENT_CALL id in the input as parent_tool_use_id. *RESULT
 * (malloc'd) becomes a hook's replacement. 0, or -1 on a store error. */
int pic_hooks_post_nested(struct pic_store *s, int64_t submission, int64_t ref, const char *tool, const char *input,
                          const char *call_id, const char *parent_call, char **result, int is_error);
/* PostToolUse, as tool Bash, for the output of a command the user ran (pic
 * bash, the terminal's !) before it is journaled: *OUTPUT (malloc'd) becomes
 * a hook's replacement, or the withheld notice when a required hook did not
 * finish cleanly, and *REPLACED is then 1. 0, or -1 on a store error. */
int pic_hooks_post_bash(struct pic_store *s, const char *command, char **output, int is_error, int *replaced);
/* SessionStart with source "compact" after a compaction of SUBMISSION's
 * current turn, once. */
int pic_hooks_compact_start(struct pic_store *s, int64_t submission);
/* PreCompact before compacting for SUBMISSION's TURN: the context hooks
 * add to the summary instructions in *extra (malloc'd, or NULL). A retry of
 * the same compaction reuses what the first attempt journaled. */
int pic_hooks_pre_compact(struct pic_store *s, int64_t submission, int64_t turn, const char *trigger,
                          const char *instructions, char **extra);
/* Inside a transaction: the conversation's pending hook context becomes
 * hook entries of SUBMISSION, oldest first. Driven by the journal alone, so
 * a resumed run delivers what an earlier process journaled. */
int pic_hooks_deliver(struct pic_store *s, int64_t submission);

/* The text a Stop block's reason goes to the model as (malloc'd). */
char *pic_hooks_stop_feedback(const char *reason);
/* PreToolUse/PostToolUse fields for a call: Claude Code's tool name (pic's
 * when none maps) and the input with read/edit/write's path also as
 * file_path; as a JSON object of tool_name, tool_input and tool_use_id
 * (malloc'd), plus tool_response when RESULT is not NULL. */
char *pic_hooks_tool_extra(struct pic_store *s, const char *tool, const char *input, const char *call_id,
                           const char *result, int is_error);
/* Claude Code's name for a pic tool, or NULL when none maps. */
const char *pic_hooks_cc_name(const char *tool);

/* A built-in component's answer to a question core asks (policy.h): what it
 * said, as a hook run's reason and context (malloc'd), and its row (0: not
 * journaled). */
typedef struct {
    int64_t id;
    char *reason, *context;
} pic_hook_answer;
void pic_hook_answer_free(pic_hook_answer *a);
/* EVENT for SUBMISSION's REF, answered in process by NAME's ANSWER: the
 * answer journaled for them before, if any, which stands; else ANSWER's,
 * called with FACTS outside any transaction and journaled, when it says
 * anything, as a done run of NAME in a transaction of its own before it is
 * returned. No command hook runs for such an event. A crash before the row
 * commits leaves nothing, and the next asker asks again. 0, or -1 on a store
 * error (s->err). */
typedef int (*pic_hook_answerer)(struct sqlite3 *db, int64_t submission, int64_t ref, const char *facts,
                                 pic_hook_answer *a);
int pic_hooks_ask(struct pic_store *s, const char *event, int64_t submission, int64_t ref, const char *name,
                  pic_hook_answerer answer, const char *facts, pic_hook_answer *out);

#endif