Skip to content
pi-c Documentation
Login

Documentation

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

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

#include "sqlite3.h"

#define PIC_SCHEMA_VERSION "1"
#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;
    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 */

/* Scripted provider: turn N of a submission is line N+1 of the script. */
typedef enum { PIC_REPLY_TEXT, PIC_REPLY_TOOL } pic_reply_kind;
typedef struct {
    pic_reply_kind kind;
    char *tool; /* "write" or "exec" when kind is PIC_REPLY_TOOL */
    char *args; /* tool arguments, or the answer text */
} pic_reply;
int pic_provider_reply(const char *script, int64_t turn, pic_reply *out, char *err, size_t errcap);
void pic_reply_free(pic_reply *r);

/* 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;
    int (*started)(void *ctx);
    void *ctx;
} pic_tool_hooks;
int pic_tool_replay_safe(const char *tool);
char *pic_tool_run(const char *workspace, const char *tool, const char *args, const pic_tool_hooks *hooks);

/* 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, const char *script, int exec_seconds);
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