compactor

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

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

func ShouldCompact(tokens int, p Policy) bool

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

func Run(ctx context.Context, t *tree.Tree, p providers.Provider, policy Policy) (Result, error)

Run executes a compaction cycle on the session tree. Steps:

  1. Replay the active branch.
  2. Bail early if there aren't enough entries to cut.
  3. Ask the provider to count tokens on the serialised history.
  4. If below threshold, skip.
  5. Determine firstKeptEntryId (keep the last KeepRecentTurns + any active system.prompt entries verbatim).
  6. Ask the provider to summarise the entries being dropped.
  7. 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL