/* Compaction planning, Pi's algorithm over the journal (compact.c). */ #ifndef PIC_COMPACT_H #define PIC_COMPACT_H #include "pic.h" #include "policy.h" /* Pi's defaults (settings-defaults.ts) for the settings below. */ #define PIC_RESERVE_TOKENS 16384 /* compact when the context passes the window minus this */ #define PIC_KEEP_RECENT_TOKENS 20000 /* about this much recent context is kept verbatim */ #define PIC_BRANCH_RESERVE_TOKENS 16384 /* a branch summary sends at most the window minus this */ #define PIC_BRANCH_WINDOW 128000 /* the window a branch summary assumes for a model without one */ /* Pi's compaction settings: the "compaction" setting, a JSON object as in * Pi's settings.json ({"enabled", "reserveTokens", "keepRecentTokens", * "modelOverrides": {"provider/model": {"reserveTokens", ...}}}). enabled * off stops automatic compaction, threshold and overflow alike (Pi's * _checkCompaction); pic compact still runs. A value that is not a * non-negative whole number is the default. MODEL is "provider/model". */ typedef struct { int enabled; long reserve_tokens, keep_recent_tokens; } pic_compaction_settings; void pic_compaction_settings_get(const char *model, pic_compaction_settings *out); /* Pi's "branchSummary" setting: {"reserveTokens", "skipPrompt"}. skipPrompt: * /tree leaves a branch without asking whether to summarize it (no summary). */ typedef struct { long reserve_tokens; int skip_prompt; } pic_branch_settings; void pic_branch_settings_get(pic_branch_settings *out); extern const char pic_summary_system_prompt[]; /* A planned compaction. The summary is the history prompt's reply (or the * previous summary, or "No prior history." when only a split turn is cut), * joined to the prefix prompt's reply for a split turn as Pi joins them, then * files appended. details are the file lists as JSON, kept for the next one. */ typedef struct { sqlite3_int64 first_kept_seq, tokens_before; int split; char *history_prompt; /* NULL when only a split turn's start is summarized */ char *prefix_prompt; /* split turns only */ char *previous_summary; /* NULL when this is the first */ char *files, *details; } pic_compaction; /* 0 with a plan; 1 when there is nothing to summarize; -1 on error. About * KEEP_RECENT_TOKENS of the newest context are kept. */ int pic_compaction_plan(sqlite3 *db, int64_t submission, const char *instructions, long keep_recent_tokens, pic_compaction *c); void pic_compaction_free(pic_compaction *c); /* Pi's estimate of the context the submission's next request sends. */ int pic_context_tokens(sqlite3 *db, int64_t submission, long *tokens); /* The summary-free plan (lapseMode trim): at least KEEP_RECENT_TOKENS of the * newest context kept from the start of its turn (from the nearest reply * instead when that would keep twice as much; never from a tool result), * everything older dropped with no request. Both are real tokens: chars/4 * times RATIO, the conversation's calibration. BODY says so and lists the * files read and modified, as a summary does; DETAILS carries the lists on. * 0 with a plan; 1 when everything fits; -1 on error. */ int pic_compaction_trim(sqlite3 *db, int64_t submission, long keep_recent_tokens, double ratio, sqlite3_int64 *first_kept, sqlite3_int64 *tokens_before, char **body, char **details); /* The bounds of a calibration (real tokens per chars/4 token). chars/4 * undercounts code and JSON; it is never taken to overcount, so a trim never * keeps more than chars/4 says. */ #define PIC_CALIBRATION_MIN 1.0 #define PIC_CALIBRATION_MAX 3.0 /* The context-policy component (policy.h): PreRequest's answer, Pi's * threshold as a summary, then lapse compaction. Every cold cache (idle past * the cache's lifetime, lapse compaction on) is answered once with its arm: * small (the context at or under the floor), holdout (drawn at random, the * "lapseHoldout" setting or PIC_LAPSE_HOLDOUT of the rest, left uncompacted * so the savings can be measured) or compact, in lapseMode (trim, the * default, or summary). The composition root registers it. */ #define PIC_LAPSE_HOLDOUT 0.1 #define PIC_LAPSE_FLOOR 100000 extern const pic_context_policy pic_compact_policy; /* Its lapse switch, lifetime (-1: the provider's cache lifetime) and floor, * as main.c resolves them from flags, environment and settings. */ void pic_lapse_configure(int on, long seconds, long floor); /* "trim" or "summary": the lapseMode setting (anything else is trim). */ const char *pic_lapse_mode(void); /* pic usage --lapse [--json]: the cost from each cold cache to the next in * its session, each mode's compacted against its held out, with a 95% * interval. */ int pic_lapse_report(sqlite3 *db, int json, FILE *out); /* The same pooled over the journals DBS (SKIPPED: ones the caller could not open): 0, or -1. */ int pic_lapse_report_dbs(sqlite3 *const *dbs, size_t n, int skipped, int json, FILE *out); /* A branch summary (Pi's branch-summarization.ts), made when /tree leaves * the conversation ending at FROM for entry TARGET: the entries on FROM's * path below their common ancestor, newest first within BUDGET tokens (0: * no limit). The summary is pic_branch_summary_preamble, the reply to * prompt, then files. prompt is NULL when nothing fit (Pi's summary is then * "No content to summarize", with no request). */ typedef struct { char *prompt; char *files, *details; /* as a compaction's */ } pic_branch; extern const char pic_branch_summary_preamble[]; /* 0 with a plan; 1 when FROM's path has nothing below the ancestor; -1. */ int pic_branch_plan(sqlite3 *db, sqlite3_int64 from, sqlite3_int64 target, long budget, const char *instructions, pic_branch *b); void pic_branch_free(pic_branch *b); /* RPC command (rpc.h): get_compaction_settings. */ struct pic_rpc_command; extern const struct pic_rpc_command pic_compaction_settings_rpc; #endif