/* Compaction planning, Pi's algorithm over the journal (compact.c). */ #ifndef PIC_COMPACT_H #define PIC_COMPACT_H #include "pic.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); /* 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); #endif