compaction

package
v0.1.14 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KindSummary = "summary"
	KindPrune   = "prune"
)

Variables

This section is empty.

Functions

func ApplyAll

func ApplyAll(ctx context.Context, services []Service, req Request) ([]agentkit.ModelMessage, int, error)

ApplyAll runs compaction services in order, returning the latest message view and how many services reported Applied.

func EstimateIndexedTokens added in v0.1.9

func EstimateIndexedTokens(indexed []IndexedMessage) int

func EstimateMessagesTokens added in v0.1.9

func EstimateMessagesTokens(messages []agentkit.ModelMessage) int

EstimateMessagesTokens sums token estimates for a message slice.

func EstimateTokens added in v0.1.9

func EstimateTokens(msg agentkit.ModelMessage) int

EstimateTokens approximates message size with a chars/4 heuristic (Pi-compatible).

func PreviousSummaryText added in v0.1.9

func PreviousSummaryText(data EventData) string

PreviousSummaryText extracts prior summary body from a compaction event.

func PruneToolResults

func PruneToolResults(messages []agentkit.ModelMessage, maxBytes int) []agentkit.ModelMessage

PruneToolResults truncates oversized tool result text deterministically.

func RetryCall

func RetryCall(
	ctx context.Context,
	policy RetryPolicy,
	isRetryable func(error) bool,
	call func() error,
	callbacks *SummarizationRetryCallbacks,
) error

RetryCall runs call with bounded retries. isRetryable classifies transient failures; when nil, only context cancellation is treated as non-retryable.

func SerializeConversation added in v0.1.9

func SerializeConversation(messages []agentkit.ModelMessage) string

SerializeConversation formats messages as plain text for summarization (Pi-style).

func TruncateToolResult

func TruncateToolResult(result agentkit.ToolResult, maxBytes int) agentkit.ToolResult

TruncateToolResult truncates oversized tool result content after execution.

Types

type CutPointResult added in v0.1.9

type CutPointResult struct {
	FirstKeptIndex int
	TurnStartIndex int
	IsSplitTurn    bool
}

CutPointResult is the compaction cut point in an indexed message list.

func FindCutPoint added in v0.1.9

func FindCutPoint(messages []IndexedMessage, startIndex, endIndex, keepRecentTokens int) CutPointResult

FindCutPoint walks backward until keepRecentTokens is reached, then cuts at the nearest valid cut point. Tool messages are never cut points (Pi-compatible).

type EventData

type EventData struct {
	// BeforeSeq is legacy: highest summarized seq when FirstKeptSeq is unset.
	BeforeSeq agentkit.EventSeq `json:"beforeSeq,omitempty"`
	// FirstKeptSeq is the first retained event after compaction (Pi firstKeptEntryId).
	FirstKeptSeq agentkit.EventSeq `json:"firstKeptSeq,omitempty"`
	// RetainedTail is verbatim recent history kept for the model after compaction.
	RetainedTail []agentkit.ModelMessage `json:"retainedTail,omitempty"`
	TokensBefore int                     `json:"tokensBefore,omitempty"`
	Summary      agentkit.ModelMessage   `json:"summary"`
	Kind         string                  `json:"kind"`
}

func (EventData) MemoryCutoffSeq added in v0.1.9

func (d EventData) MemoryCutoffSeq() agentkit.EventSeq

MemoryCutoffSeq returns the highest event seq dropped from hot memory.

type IndexedMessage added in v0.1.9

type IndexedMessage struct {
	Message     agentkit.ModelMessage
	Seq         agentkit.EventSeq
	IsTurnStart bool
}

IndexedMessage is a model-visible message with its primary source event seq.

type Preparation added in v0.1.9

type Preparation struct {
	FirstKeptSeq        agentkit.EventSeq
	FirstKeptIndex      int
	MessagesToSummarize []agentkit.ModelMessage
	TurnPrefixMessages  []agentkit.ModelMessage
	RetainedTail        []agentkit.ModelMessage
	PreviousSummary     string
	TokensBefore        int
	IsSplitTurn         bool
}

Preparation is the Pi-style compaction plan for one pass.

func Prepare added in v0.1.9

func Prepare(indexed []IndexedMessage, boundaryStart int, keepRecentTokens int, previousSummary string, tokensBefore int) *Preparation

Prepare builds a compaction plan from indexed messages. boundaryStart is the first index eligible for summarization (after any previous compaction summary).

type Request

type Request struct {
	SessionID agentkit.SessionID
	AgentID   agentkit.AgentID
	Session   agentkit.Session
	Messages  []agentkit.ModelMessage
	// Force skips automatic thresholds such as minMessages (manual /compact).
	Force bool
}

type Result

type Result struct {
	Applied  bool
	Event    agentkit.SessionEvent
	Messages []agentkit.ModelMessage
}

type RetryConfig

type RetryConfig struct {
	Enabled     *bool `json:"enabled"`
	MaxRetries  int   `json:"maxRetries"`
	BaseDelayMs int   `json:"baseDelayMs"`
}

RetryConfig controls bounded retries with exponential backoff for compaction LLM calls.

type RetryPolicy

type RetryPolicy struct {
	Enabled     bool
	MaxRetries  int
	BaseDelayMs int
}

RetryPolicy is the resolved retry configuration.

func ResolveRetrySettings

func ResolveRetrySettings(cfg *RetryConfig) RetryPolicy

ResolveRetrySettings applies defaults aligned with agent-level auto retry.

type Service

type Service interface {
	Compact(context.Context, Request) (Result, error)
}

type SummarizationRetryCallbacks

type SummarizationRetryCallbacks struct {
	OnScheduled func(attempt, maxAttempts, delayMs int, errorMessage string)
	OnFinished  func(success bool, attempt int, finalError string)
}

SummarizationRetryCallbacks are optional hooks for compaction summarization retries.

Jump to

Keyboard shortcuts

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