Skip to content
pi-c Artifact [ad2a464b97]
Login

Artifact [ad2a464b97]

Artifact ad2a464b976bd3143961342ede09b8883d8bf8e6ae51e3c3ea1c2efb9a261314:


/* pic: durable coding-agent harness. Internal interfaces. */
#ifndef PIC_H
#define PIC_H

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

#include "models.h"
#include "sqlite3.h"

#define PIC_SCHEMA_VERSION "4"
#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;
    FILE *progress; /* when set, each tool call is announced here as it starts */
    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);
    void (*free)(struct pic_provider *p);
    void *state;
} 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);

/* 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. */
int pic_submit(pic_store *s, const char *request_key, const char *content, int *duplicate);
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