Artifact d5fd4acab0cd40c70f74b29c7e6b0200373c1f35bfa9914c4218503ea8117fab:
- File
src/pic.h
— part of check-in
[f41f8c8e00]
at
2026-11-19 11:17:41
on branch events
— Add an interactive line mode: plain pic in a terminal, or pic chat
A prompt that continues one conversation: each message streams its reply, Ctrl-C cancels just that reply, a line ending in a backslash continues the message, and Ctrl-D or /quit leaves. Slash commands start, resume, list, log, name, fork and compact conversations and change the model. The journal is opened only while an input is handled, so other pic commands can use it while the prompt waits. (user: dmestas, size: 8718) [annotate] [blame] [check-ins using] [more...]
/* pic: durable coding-agent harness. Internal interfaces. */ #ifndef PIC_H #define PIC_H #include <stddef.h> #include <stdint.h> #include <stdio.h> #include "events.h" #include "models.h" #include "sqlite3.h" #define PIC_SCHEMA_VERSION "6" #define PIC_GENERATION_ATTEMPTS 3 /* provider requests per turn before the submission fails */ #define PIC_ERR_CAP 512 #define PIC_OUTPUT_CAP 65536 /* tool output kept in the journal, bytes */ typedef struct pic_store { sqlite3 *db; int lock_fd; struct pic_events *events; /* live events while running (events.h), or NULL */ char err[PIC_ERR_CAP]; } pic_store; /* Store: owns the connection, the single-writer lock, schema, and invariants. */ int pic_open(pic_store *s, const char *db_path); void pic_close(pic_store *s); int pic_exec(pic_store *s, const char *sql); int pic_begin(pic_store *s); int pic_commit(pic_store *s); void pic_rollback(pic_store *s); int pic_check(pic_store *s); /* hand-enforced integrity: foreign_keys is OFF */ /* One assistant message from a provider, in two forms. native is what the * provider sent, journaled verbatim and replayed byte for byte to the same * provider, API and model (signatures and reasoning stay valid). neutral is * Pi's provider-neutral content array, which tool calls are taken from and * which other providers are sent: * {"type":"text","text"} {"type":"thinking","thinking","signature"?,"redacted"?} * {"type":"toolCall","id","name","arguments":{...}} * stop_reason is normalised to Pi's: stop, length, toolUse, refusal, pause. */ typedef struct { char *native; /* provider's content, JSON */ char *neutral; /* JSON array */ char *stop_reason; /* stop, length, toolUse, refusal, pause */ char *usage; /* JSON object or NULL */ char *request_id; /* provider request id or NULL */ char *provider, *api, *model; /* origin of the reply */ } pic_reply; void pic_reply_free(pic_reply *r); /* A provider produces the next assistant message for a submission from its * journal. On failure it sets err and *retryable: a retryable failure (network, * overload, cut stream) counts one attempt; any other fails the submission. */ typedef struct pic_provider { int (*reply)(struct pic_provider *p, pic_store *s, int64_t submission, int64_t turn, pic_reply *out, int *retryable, char *err, size_t errcap); /* A plain request for compaction: SYSTEM and one USER message, no tools; * the reply's text in *text, usage in *usage (both malloc'd). */ int (*summarize)(struct pic_provider *p, pic_store *s, const char *system, const char *user, char **text, char **usage, int *retryable, char *err, size_t errcap); void (*free)(struct pic_provider *p); void *state; long context_window; /* tokens; 0 means never compact automatically */ } pic_provider; /* Scripted provider: turn N of a submission is line N+1 of the script. */ int pic_provider_script(pic_provider *p, const char *script); /* A provider for a resolved model (models.h), over pic's own HTTPS stack; * url, when not NULL, replaces the model's endpoint (tests). */ int pic_provider_for_model(pic_provider *p, const pic_model *model, const char *url, const char *workspace, char *err, size_t errcap); /* Cross-provider conversion (transform.c), on a connection where * pic_transform_register has run. */ int pic_transform_register(sqlite3 *db); /* Pi's neutral content from an Anthropic content array (malloc'd, or NULL). */ char *pic_neutral_from_anthropic(sqlite3 *db, const char *native); /* The system prompt for a workspace, with its project instructions (malloc'd). */ char *pic_system_prompt(const char *workspace); /* Only the project instructions block (malloc'd; empty when there are none). */ char *pic_project_instructions(const char *workspace); /* Tools run outside any transaction and return a malloc'd result string. * started() durably records that the effect has begun; tools call it exactly * once, before any external effect. If it fails the tool returns NULL and * nothing ran. NULL after a successful started() means the outcome is lost. */ typedef struct { int exec_seconds; /* bash's default timeout */ /* plan: bytes the tool will write, journaled with "started" so a replay * after a crash writes the same bytes (edit); NULL when there is none. */ int (*started)(void *ctx, const char *plan, size_t plan_len); void *ctx; const char *plan; /* a plan an earlier attempt journaled, or NULL */ size_t plan_len; } pic_tool_hooks; /* Tool arguments from the call's JSON input. Strings are NULL when absent or * not strings; bad_input, when set, is reported instead of running. */ typedef struct { const char *path, *content, *command; long offset, limit, timeout; int has_offset, has_limit, has_timeout; size_t nedits; const char *const *old_text, *const *new_text; const char *bad_input; } pic_tool_args; int pic_tool_replay_safe(const char *tool); char *pic_tool_run(const char *workspace, const char *tool, const pic_tool_args *args, const pic_tool_hooks *hooks, int *is_error); /* The tool definitions sent to providers, as a JSON array. */ extern const char pic_tool_schemas[]; /* Loop. */ /* SESSION: NULL starts a new session, "latest" continues the most recent, else * an id or unique prefix. The session joined is copied to session_out. */ int pic_submit(pic_store *s, const char *request_key, const char *content, const char *session, int *duplicate, char *session_out, size_t session_cap); /* A new session continuing from entry AT (0: the head) of session FROM. */ int pic_fork(pic_store *s, const char *from, sqlite3_int64 at, char *out, size_t cap); int pic_sessions(pic_store *s, FILE *out); int pic_session_name(pic_store *s, const char *session, const char *name); /* A conversation id from an id, a unique prefix or "latest". */ int pic_session_resolve(pic_store *s, const char *session, char *out, size_t cap); int pic_session_log(pic_store *s, const char *session, FILE *out); /* Asks for SESSION to be compacted before its next request (the id joined in out). */ int pic_compact_request(pic_store *s, const char *session, const char *instructions, char *out, size_t cap); /* Finished entries after SINCE as JSON lines (the cursor is the entry number). */ int pic_export(pic_store *s, const char *workspace, sqlite3_int64 since, FILE *out); /* The frozen system prompt or tools ("system_prompt" or "tools") of a * submission's session, as its first generation set them (malloc'd). */ char *pic_session_text(pic_store *s, int64_t submission, const char *column); /* The context a submission's next request sends, for adapters: prefix a query * with PIC_CONTEXT_CTE, select FROM pic_entry e JOIN v ON v.seq = e.seq CROSS * JOIN kk, ORDER BY e.kind != 'compaction', e.seq; ?1 is the submission. v is * the latest compaction on the conversation's path (its summary goes first), * the entries it kept and everything after; v.fresh is 0 for kept entries from * before it, which go out rebuilt without thinking (thinking is bound to the * whole history before it). kk.cseq is that compaction (0: none). */ #define PIC_CONTEXT_CTE \ "WITH RECURSIVE p(seq) AS (SELECT head_seq FROM pic_session WHERE id = (SELECT session_id FROM pic_submission" \ " WHERE id = ?1) UNION ALL SELECT x.parent_seq FROM pic_entry x JOIN p ON x.seq = p.seq WHERE x.parent_seq IS NOT NULL)," \ " k(cseq) AS (SELECT coalesce(max(x.seq), 0) FROM pic_entry x JOIN p ON p.seq = x.seq WHERE x.kind = 'compaction')," \ " kk(cseq, kept) AS (SELECT cseq, coalesce((SELECT first_kept_seq FROM pic_entry WHERE seq = cseq), 0) FROM k)," \ " v(seq, fresh) AS (SELECT x.seq, x.seq > kk.cseq FROM pic_entry x JOIN p ON p.seq = x.seq, kk" \ " WHERE x.seq = kk.cseq OR (x.kind != 'compaction' AND x.seq >= kk.kept)) " /* Pi's wording for a compaction summary sent as a user message (SQL text). */ #define PIC_SUMMARY_SQL(col) \ "('The conversation history before this point was compacted into the following summary:' || char(10) ||" \ " char(10) || '<summary>' || char(10) || " col " || char(10) || '</summary>')" int pic_recover(pic_store *s); int pic_run(pic_store *s, const char *workspace, pic_provider *provider, int exec_seconds); int pic_cancel(pic_store *s, const char *request_key); char *pic_answer_text(pic_store *s, const char *request_key); /* malloc'd, or NULL */ int pic_dump(pic_store *s); /* Crash-injection hooks exist only in development builds. */ #ifdef PIC_FAULTS void pic_fault(const char *point); #else #define pic_fault(point) ((void)0) #endif #endif