/* Permission modes (docs/dev/permissions.md): a conversation's mode, kept in pic_permission, says * which tool calls wait for a person's yes. allow asks nothing; ask-edits asks before write and * edit; ask-all before every call of a tool that is not read-only; read-only refuses those. A call * that asks is an ask (ask.h) keyed perm:EFFECT, a select of Allow, Allow for this conversation and * Deny, and the call waits on it; a replay finds the same ask, answered or still open. A * PreToolUse hook's ask asks in every mode. */ #ifndef PIC_PERM_H #define PIC_PERM_H #include #include "pic.h" #include "rpc.h" #define PIC_PERM_MODES "allow, ask-edits, ask-all or read-only" #define PIC_PERM_ALWAYS "Allow for this conversation" /* The table, made IF NOT EXISTS by every full open (store.c) and by a set. */ extern const char pic_perm_sql[]; /* 1 when MODE is one of the four. */ int pic_perm_valid(const char *mode); /* SESSION's mode in MODE ("allow" when it has none, or the journal keeps none). 0 or -1. */ int pic_perm_get(pic_store *s, const char *session, char *mode, size_t cap); /* SESSION's mode set to MODE, in a transaction of its own (a savepoint inside the caller's); the * tools allowed for the conversation are kept. 0; 1 refused (s->err: not a mode); -1. */ int pic_perm_set(pic_store *s, const char *session, const char *mode); /* Inside the transaction that admits a request into SESSION: s->permission_set becomes its mode; * else, when SESSION has had no request, s->permission_default does (allow keeps no row). */ int pic_perm_admit(pic_store *s, const char *session); /* Inside a transaction: conversation TO (a fork of FROM) takes FROM's mode and allowed tools. */ int pic_perm_copy(pic_store *s, const char *from, const char *to); /* SESSION's mode set to MODE under KEY (a phone's rc:DEVICE:KEY), once: the first call with KEY * sets it and keeps its answer, a later one changes nothing and gives the kept answer. 0 with * *DATA ({"permissionMode","allowed"}, malloc'd); 1 refused (s->err); -1. */ int pic_perm_set_keyed(pic_store *s, const char *session, const char *mode, const char *key, char **data); /* SESSION's mode and its allowed tools as {"permissionMode","allowed":[names]} (malloc'd), or NULL. */ char *pic_perm_json(pic_store *s, const char *session); /* The mode's name in Claude Code's terms, for a hook's permission_mode. */ const char *pic_perm_claude_name(const char *mode); /* A call to gate, after its PreToolUse hooks have decided. */ typedef struct { const char *session; long long submission; const char *key; /* the ask's key: perm:EFFECT, perm:n:ROW for a call a tool made */ const char *tool, *input; /* the tool and its JSON input */ int hook_ask; /* a hook asked */ const char *hook_reason; /* what the asking hooks said, or NULL */ int hook_allow; /* a hook allowed it and none asked */ } pic_perm_call; /* Whether C may run, asking the person when the mode or a hook says so and waiting for the answer. * 0: run it; 1: not run, *REASON (malloc'd) for the model; 2: Ctrl-C cut the wait short (the ask * withdrawn); -1. Fault point perm_ask: arrival 1 once the ask is open, 2 once its answer is read. */ int pic_perm_gate(pic_store *s, const pic_perm_call *c, char **reason); /* get_permission_mode and set_permission_mode {mode}: the conversation's mode and allowed tools. */ extern const pic_rpc_command pic_get_permission_mode_rpc, pic_set_permission_mode_rpc; #endif