/* 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_HOOK=1 in its environment, in * its own process group, killed at its timeout (seconds, default 10, at most * 60). A hook that fails in any other way than its event's blocking exit 2 * is ignored for the run and recorded (pic_hook_run; pic dump lists them). */ #ifndef PIC_HOOKS_H #define PIC_HOOKS_H #include #include struct pic_store; typedef struct pic_hooks pic_hooks; /* The configured hooks, or NULL when no file configures any. JOURNAL is * the transcript_path hooks are given. 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 *journal, 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; /* NULL in a marker (no hook matched) */ 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) */ } 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 blocked; /* a deny (PreToolUse) or block (UserPromptSubmit, PostToolUse, Stop) */ char *reason; /* the reasons, one per line */ } pic_hook_fire; /* Runs EVENT's hooks whose matcher takes NAME or ALT (either may be NULL; * events without a matcher take every hook) with the common fields and the * members of EXTRA (a JSON object) on stdin. Nothing is journaled. 0, or -1 * when out of memory (s->err). */ int pic_hook_fire_run(struct pic_store *s, pic_hook_fire *f, const char *name, const char *alt, const char *extra); /* Inside a transaction: journals F's runs, or a marker row when MARKER and * none ran; ENTRY (0: none) is the hook entry their output became. */ int pic_hook_fire_record(struct pic_store *s, const pic_hook_fire *f, int marker, int64_t entry); void pic_hook_fire_free(pic_hook_fire *f); /* 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). */ int pic_hooks_prompt(struct pic_store *s, const char *session, const char *prompt); /* 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