Artifact f0911e6be65a96d03529e2533a12ff4987b2fe8b37fdaae5c71d8f4f3d5b729f:
- File
src/compact.h
— part of check-in
[5453014491]
at
2026-11-19 02:30:01
on branch compaction
— Compact long conversations with Pi's algorithm, for any model
Before each reply pic estimates the context as Pi does (the last reply's usage plus chars/4 for what followed) and past the model's window minus 16384 tokens it summarizes the history beyond the newest 20000 tokens, cutting only where a call is not separated from its result and handling a split turn as Pi does. The summary requests use Pi's prompts and serialization, are journaled as generations of their own and retried like replies; the summary, read and modified files, the first kept entry and the tokens before commit together. Requests then send the summary, the kept entries (rebuilt without thinking when older than the compaction) and what followed. Instruction updates that were summarized go out again. A context overflow compacts once and retries; pic compact asks for one before the next request; --context-window overrides the window. Schema 6. (user: dmestas, size: 1378) [annotate] [blame] [check-ins using] [more...]
/* Compaction planning, Pi's algorithm over the journal (compact.c). */ #ifndef PIC_COMPACT_H #define PIC_COMPACT_H #include "pic.h" #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 */ 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. */ int pic_compaction_plan(sqlite3 *db, int64_t submission, const char *instructions, 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); #endif