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. */
#ifndef PIC_HOOKS_H
#define PIC_HOOKS_H

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

struct pic_store;
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) */
    int interrupted;    /* cut off by Ctrl-C: journaled as interrupted */
} 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) */
} 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).
 * *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, 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 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. */
int pic_hooks_post_tools(struct pic_store *s, int64_t submission);
/* 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);

#endif