Artifact b37c737cbd5e71b304bbab5bac8402817083409823c2b845a9c73490a7b3acbd:
- File
src/policy.h
— part of check-in
[8f2d30d39e]
at
2026-11-22 20:18:02
on branch keel-rpc
— Cut the RPC command registry seam
Components register RPC commands (name, help, flags, answer) and main.c wires them; rpc.c dispatches by name without including catalog.h, compact.h, prompts.h or skills.h. get_available_models and get_commands move to src/listings.c; rewind uses the new call shape. Whether compaction is automatic and the token estimate come through the context policy (automatic, tokens). Allowance 29 -> 25, stubs 29 -> 20. (user: pi-c-agent, size: 3808) [annotate] [blame] [check-ins using] [more...]
/* 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; #endif