Skip to main content

Module compaction

Module compaction 

Source
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§

CompactionSettings
When and how much to compact.
ContextEstimate
Estimated context size: the last valid usage plus estimates for what follows.
FileOperations
Files read and changed by tool calls, for the summary.
Preparation
What compaction will summarize and keep, serialized as pi’s CompactionPreparation.
RetryPolicy
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§

BranchSummary
What summarizing a branch produced.
SummaryRetry
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_leaf back to the deepest common ancestor with target, 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 None when 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.