Skip to content
pi-c Artifact [0995615f7b]
Login

Artifact [0995615f7b]

Artifact 0995615f7be58879d6b85598d00b7d0abcdb7fa3f061434ac8b08dfdccd2633f:


/* The context-policy seam: core asks, a component decides (docs/design/keel.md,
 * docs/dev/durability.md). Before each reply request core asks one question,
 * PreRequest, through the hook host (pic_hooks_ask): the facts core owns go
 * in, and the component's answer is journaled as a hook run before it takes
 * effect. A journaled answer stands: recovery and retries read it and never
 * ask again. An answer to compact names a mode; core asks the component for
 * that mode's plan, runs the plan's summary requests (if any) and commits the
 * compaction as it always has. With no policy (pic_store.policy NULL) core
 * never compacts on its own and refuses pic compact. */
#ifndef PIC_POLICY_H
#define PIC_POLICY_H

#include "hooks.h"
#include "pic.h"

/* What a compaction keeps and how its entry's text is made: the entry keeps
 * the context from FIRST_KEPT_SEQ on. With prompt[0] NULL its text is BODY
 * (no request is made); else it is join() over the replies to prompt[0] and
 * prompt[1] (NULL when one request does), each sent with SYSTEM. */
typedef struct {
    sqlite3_int64 first_kept_seq, tokens_before;
    const char *system;
    char *prompt[2];
    char *body;
    char *details; /* journaled with the entry (pic_entry.native) for the next plan */
    void *state;   /* the component's */
} pic_compact_plan;

typedef struct pic_context_policy {
    const char *name; /* the answering run's command in the journal */
    /* PreRequest for SUBMISSION's TURN. FACTS is a JSON object of what core
     * knows: model, contextWindow, cacheSeconds, idleSeconds (since the
     * conversation's last reply; null before one), firstRequest (true before
     * the submission's first reply request, false on its tool-result and
     * later turns). An answer to compact sets
     * a->context to a JSON object with "compact" (the mode) and a->reason;
     * an answer with no "compact" is kept as the component's record and does
     * nothing; nothing at all (every field NULL) is not journaled. 0, or -1. */
    int (*answer)(sqlite3 *db, int64_t submission, int64_t turn, const char *facts, pic_hook_answer *a);
    /* The plan to compact SUBMISSION's context in MODE ("summary", "trim")
     * for MODEL, with ANSWER (the journaled answer's context that named the
     * mode; NULL when none did, as for pic compact) and INSTRUCTIONS (NULL:
     * none) for a summary. 0 with a plan, 1 when there is nothing to
     * compact, -1 on error. */
    int (*plan)(sqlite3 *db, int64_t submission, const char *model, const char *mode, const char *answer,
                const char *instructions, pic_compact_plan *p);
    /* The plan for a branch summary made when /tree leaves the conversation
     * ending at FROM for entry AT, for a model with WINDOW tokens (0: not
     * known). 0 with a plan (with no prompt when nothing fit), 1 when FROM's
     * path leaves nothing behind, -1 on error. */
    int (*branch_plan)(sqlite3 *db, sqlite3_int64 from, sqlite3_int64 at, long window, const char *instructions,
                       pic_compact_plan *p);
    /* The entry's text from the replies to p->prompt (malloc'd, NULL when out of memory). */
    char *(*join)(const pic_compact_plan *p, char *const reply[2]);
    void (*plan_free)(pic_compact_plan *p);
    /* Whether a request MODEL found too long is compacted and tried again. */
    int (*overflow)(const char *model);
    /* Whether the component compacts on its own for MODEL (Pi's
     * autoCompactionEnabled, as get_state reports it). */
    int (*automatic)(const char *model);
    /* The component's estimate of the context SUBMISSION's next request
     * sends (Pi's, for get_session_stats and compact's result). 0, or -1. */
    int (*tokens)(sqlite3 *db, int64_t submission, long *tokens);
} pic_context_policy;

/* The stop-policy seam (docs/design/wakes.md): when a request is about to end answered in a
 * conversation with an active goal, core runs the goal's check command, if it has one, journaled
 * as a hook run (event GoalCheck: started, then done; a started one found after a crash is
 * interrupted, never run again, and its outcome unknown), then asks this one question through the
 * hook host (event StopPolicy, pic_hooks_ask), and acts on the journaled answer in the transaction
 * that makes the reply the answer. The first answer for a request stands. With no policy
 * (pic_store.stop_policy NULL) requests end as they always have and goals never continue. */
typedef struct pic_stop_policy {
    const char *name; /* the answering run's command in the journal */
    /* FACTS: {"goal":TEXT,"round":N,"maxRounds":N,"stalls":N,"maxStalls":N,"worked":BOOL (a file
     * changed or a command ran in this request),"check":null or {"command","exitCode" (null:
     * unknown),"output","same" (as the last round's)},"declared":null or {"state":"done"|
     * "blocked","reason"}}. The answer's context is {"goal":"done"|"continue"|"needs","stall":BOOL}
     * and its reason the words for done and needs. 0, or -1. */
    int (*answer)(sqlite3 *db, int64_t submission, int64_t ref, const char *facts, pic_hook_answer *a);
} pic_stop_policy;

#endif