compaction

package
v0.34.1 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package compaction summarizes old conversation turns to reduce context size.

The core loop calls Compact when context approaches the model's input limit. Old turns are serialized, sent to the LLM for summarization, and replaced with a single compaction_summary message. Recent turns are kept verbatim.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendCheckpoint

func AppendCheckpoint(result *Result, compacted []core.AgentMessage, checkpoint string)

AppendCheckpoint mechanically appends a checkpoint to a summary and rewrites the summary message in place. Both the manual and automatic compaction paths must call this: previously only the manual path did, so an automatic compaction silently discarded a checkpoint the user had already paid for.

func FindCutPoint

func FindCutPoint(msgs []core.AgentMessage, contextTokens, contextWindow int, settings core.CompactionSettings) int

FindCutPoint returns the index of the first message to KEEP (everything before it gets summarized). Returns 0 if nothing needs cutting.

The cut targets contextWindow - reserveTokens - summary overhead, ensuring the result actually fits. Snaps to a valid boundary (user, assistant, or compaction_summary — never mid-tool-result).

func GenerateSummary

func GenerateSummary(ctx context.Context, provider core.Provider, model core.Model, opts core.StreamOptions, msgs []core.AgentMessage, previousSummary, focus string) (string, *core.Usage, error)

GenerateSummary makes an LLM call to summarize conversation messages. Returns the summary text, provider-reported usage (may be nil), or an error. focus, when non-empty, is a caller instruction (from `/compact <focus>`) that tells the summarizer what to keep in the foreground; it is advisory and never replaces the structured format.

func SerializeForSummary

func SerializeForSummary(msgs []core.AgentMessage, maxInput int) string

SerializeForSummary converts messages to a human-readable transcript for the summarization prompt. Truncates at a limit derived from the model's context window (maxInput tokens). Pass 0 for the default (400k chars).

When the transcript exceeds the limit, the OLDEST messages are dropped rather than the newest. The previous implementation appended forward and broke on overflow, discarding the most recent turns — precisely the ones closest to the kept tail and most likely to describe live work.

Types

type FileOps

type FileOps struct {
	Read    map[string]bool
	Written map[string]bool
	Edited  map[string]bool
}

FileOps tracks file operations found in tool calls.

func ExtractFileOps

func ExtractFileOps(msgs []core.AgentMessage) FileOps

ExtractFileOps scans messages for tool calls that reference files.

func (FileOps) Modified

func (f FileOps) Modified() []string

Modified returns files that were written or edited, sorted.

func (FileOps) ReadOnly

func (f FileOps) ReadOnly() []string

ReadOnly returns files that were read but not modified, sorted.

type Result

type Result struct {
	Summary       string
	TokensBefore  int
	TokensAfter   int
	ReadFiles     []string
	ModifiedFiles []string
	Usage         *core.Usage // LLM usage for the summarization call
}

Result holds the outcome of a compaction.

func Compact

func Compact(ctx context.Context, provider core.Provider, model core.Model, opts core.StreamOptions, msgs []core.AgentMessage, contextTokens, contextWindow int, settings core.CompactionSettings, focus string) (*Result, []core.AgentMessage, error)

Compact orchestrates context compaction. Returns nil Result if nothing needs compacting. On LLM failure, returns the error with the original messages unchanged (non-fatal). focus is an optional caller instruction (from `/compact <focus>`) telling the summarizer what to keep in the foreground; empty for automatic compaction.

Jump to

Keyboard shortcuts

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