Documentation
¶
Overview ¶
Package compactor implements the proactive compaction trigger per registration spec §2.7. Runtime-owned; invokes the provider to summarise older entries when the active branch grows past the window - reserve threshold, then emits a compaction entry into the session tree. Active head advances to the compaction entry so subsequent replays see the summary as the cut point.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ShouldCompact ¶
ShouldCompact implements the spec formula tokens > window - reserve. Callers pre-compute the token count via the provider.
Types ¶
type Policy ¶
type Policy struct {
// Model is the provider-scoped model id used for token counting
// and the summary call. Empty → adapter default.
Model string
// Window is the model's context window in tokens.
Window int
// Reserve is the headroom kept free above active context. Default
// 20% of window or 10_000, whichever larger.
Reserve int
// KeepRecentTurns is how many trailing turns to preserve verbatim
// across a compaction (everything older gets summarised).
// Default 6 per §2.7.
KeepRecentTurns int
// SummaryHint is passed through to the provider as additional
// context on what to prioritise. Optional.
SummaryHint string
}
Policy configures the compaction trigger (§2.7).
type Result ¶
type Result struct {
CompactionEntry tree.Entry
FirstKeptEntryID string
SummarisedCount int
TokensBefore int
TokensAfter int
SkippedReason string // set when Run decided compaction wasn't needed or possible
}
Result is what Run returns so the runtime can log / observe the cut without re-reading the tree.
func Run ¶
Run executes a compaction cycle on the session tree. Steps:
- Replay the active branch.
- Bail early if there aren't enough entries to cut.
- Ask the provider to count tokens on the serialised history.
- If below threshold, skip.
- Determine firstKeptEntryId (keep the last KeepRecentTurns + any active system.prompt entries verbatim).
- Ask the provider to summarise the entries being dropped.
- Append a KindCompaction entry whose parentId is the previous active head — this advances the head atomically (tree.Append is the active-head-advance primitive).
Returns a Result with SkippedReason set when nothing was done.