Expand description
Compaction: summarize older context so a long session fits the model.
Port of packages/coding-agent/src/core/compaction/compaction.ts and
compaction/utils.ts in pi v1.0.0. The functions here are pure apart from
the summary request. The agent session decides when to compact and records the
result.
Structs§
- Compaction
Settings - When and how much to compact.
- Context
Estimate - Estimated context size: the last valid usage plus estimates for what follows.
- File
Operations - Files read and changed by tool calls, for the summary.
- Preparation
- What compaction will summarize and keep, serialized as pi’s
CompactionPreparation. - Retry
Policy - Retry policy for summary requests, from
settings.retry. - Summarizer
- What summary requests need besides the messages.
- Summary
- A summary and the usage of the requests that made it.
Enums§
- Branch
Summary - What summarizing a branch produced.
- Summary
Retry - pi-ai’s
RetryCallbacks: what a summarizer reports while it retries.
Constants§
- SUMMARIZATION_
SYSTEM_ PROMPT - System prompt of summary requests.
Functions§
- calculate_
context_ tokens - Tokens a response’s usage says the context held.
- collect_
branch_ entries - Entries from
old_leafback to the deepest common ancestor withtarget, oldest first, and that ancestor. - estimate_
context_ tokens - Estimates context tokens from the last valid usage and the messages after it.
- estimate_
projected_ context_ tokens - Estimates projected context without trusting usage recorded before a later context edit or compaction.
- estimate_
tokens - Estimated tokens of one message: characters / 4, rounded up, where an image counts as 4800 characters.
- format_
file_ operations - The file lists as tags appended to a summary.
- prepare_
branch_ entries - Messages of the abandoned branch, newest first until
token_budget(0 for none), and the files touched across all of it. - prepare_
compaction - Plans a compaction of a branch, or
Nonewhen there is nothing to summarize. - serialize_
conversation - The conversation as plain text, so the summarizer does not continue it. Takes messages already converted for the model.
- should_
compact - Whether the context leaves less than the reserve free.