Skip to main content

Module compact

Module compact 

Source
Expand description

Compaction: when a context’s token estimate crosses context.compact_at × model_window (or context.compact is called), the older messages are summarized by a structured think into the summary block, the last keep_last messages stay verbatim, the plan stays verbatim, skill bodies not referenced in the kept window are evicted (the names stay), the version bumps and the record is checkpointed.

The runtime never calls the model itself, so compaction is two halves: a pure plan (plan_compaction — which messages to fold + the prompt + the output schema for the summarizer) and a pure apply (apply_compaction — fold the summarizer’s verdict into the context). The turn worker runs the think in between.

Structs§

CompactionOutcome
CompactionRequest
A prepared compaction: what to summarize and how.

Functions§

apply_compaction
Fold the summarizer’s verdict into the context: absorb the summary, drop the folded messages, bump the version, evict unreferenced skill bodies (names stay — the caller drops the bodies from its cache), recount. Refuses when the context’s version has moved since the plan was made, or when the plan’s fold exceeds the messages actually present.
apply_fallback
A degraded compaction for when the summarizer is unavailable: fold the older messages into a plain narrative built from their rendered lines (truncated). Never loses the plan or the skill names.
plan_compaction
Decide what to fold. keep_last messages stay verbatim; a fold always ends before the kept window, never splits an assistant tool-call from its tool results, and always leaves a User message first in the kept window. Returns None when there is nothing worth folding (fewer than keep_last + 2 messages, or no user message to fold up to).
summary_schema
The summarizer’s output schema: the fields of the summary block, all required, with no additional properties so a model cannot smuggle unread keys into a record that is kept forever.