compaction

package
v0.3.34 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyAll

ApplyAll runs compaction services in order with compaction telemetry.

func BoundOversizedIndexedMessages added in v0.3.32

func BoundOversizedIndexedMessages(indexed []capscompaction.IndexedMessage, maxChars int) ([]agentkit.ModelMessage, bool)

BoundOversizedIndexedMessages bounds each indexed message's model-visible size for compaction output (retained tail): oversized text is truncated, and hydratable attachment parts in messages whose logical size exceeds maxChars are replaced with a text hint, so the post-compaction view cannot re-inflate during hydration. Durable session events keep the original content.

func EstimateMessagesTokens

func EstimateMessagesTokens(messages []agentkit.ModelMessage) int

EstimateMessagesTokens sums token estimates for a message slice.

func EstimateTokens

func EstimateTokens(msg agentkit.ModelMessage) int

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

func FindCutPoint

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

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

func FitIndexedMessagesToBudget added in v0.3.32

func FitIndexedMessagesToBudget(indexed []capscompaction.IndexedMessage, maxChars int) ([]agentkit.ModelMessage, bool)

FitIndexedMessagesToBudget is the force-compaction send budget: neutralize every hydratable part (stored attachment_ref can be tiny but hydrate into a huge data: URL), cap each message, then drop oldest messages until the total fits maxChars. Durable session events keep the original content.

func NewChain added in v0.3.33

func NewChain(_ struct{}) (capscompaction.Chain, error)

NewChain registers compaction/chain: ordered ApplyAll over deps.services (telemetry in runtime ApplyAll).

func NewPrune added in v0.3.33

func NewPrune(cfg PruneConfig) (compaction.Service, error)

NewPrune registers compaction/prune-tool-results: Trim verbose tool results without calling a model.

Best practices:

  • Cheap and lossless enough to run before compaction/summary in the same chain.

func NewSummary added in v0.3.33

func NewSummary(cfg SummaryConfig, deps SummaryDeps) (capcompaction.Service, error)

NewSummary registers compaction/summary: Pi-style summary + retained tail compaction.

func Prepare

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

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

func PruneToolResults

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

PruneToolResults truncates oversized tool result text deterministically.

func PruneToolResultsReport added in v0.3.31

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

PruneToolResultsReport is PruneToolResults plus whether any content was actually truncated, so callers can report Applied honestly.

func ResolveRetrySettings

func ResolveRetrySettings(cfg *capscompaction.RetryConfig) capscompaction.RetryPolicy

ResolveRetrySettings applies defaults aligned with agent-level auto retry.

func RetryCall

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

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

func SerializeConversation

func SerializeConversation(messages []agentkit.ModelMessage) string

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

func SerializeConversationWithBudget added in v0.3.32

func SerializeConversationWithBudget(messages []agentkit.ModelMessage, maxTotalChars int) string

SerializeConversationWithBudget is SerializeConversation bounded for the summary model's window: each message is capped at summaryMaxMessageChars and when the total exceeds maxTotalChars the oldest messages are dropped with an omission marker (recent context matters more for continuation). A maxTotalChars <= 0 disables the total budget.

func TruncateOversizedMessageTexts added in v0.3.31

func TruncateOversizedMessageTexts(messages []agentkit.ModelMessage, maxChars int) ([]agentkit.ModelMessage, bool)

TruncateOversizedMessageTexts truncates text content of messages that alone exceed maxChars, keeping the head plus a marker. It is the last-resort bound for retained history so post-compaction context stays under budget; durable session events keep the full text, only the model-visible view is cut.

func TruncateToolResult

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

TruncateToolResult truncates oversized tool result content after execution.

Types

type PruneConfig added in v0.3.33

type PruneConfig struct {
	// MaxToolResultBytes is per-result truncation limit.
	MaxToolResultBytes int `json:"maxToolResultBytes"`
}

type SummaryConfig added in v0.3.33

type SummaryConfig struct {
	// MinMessages is an optional pre-gate when Force is false. Zero disables it.
	MinMessages int `json:"minMessages"`
	// KeepRecentTokens is the recent context budget kept verbatim (Pi-compatible).
	KeepRecentTokens int `json:"keepRecentTokens"`
	// ReserveTokens caps summarization output size.
	ReserveTokens int `json:"reserveTokens"`
	// KeepRecent is deprecated; use keepRecentTokens.
	KeepRecent int `json:"keepRecent"`
	// SummaryModel is model used for the summary; defaults to the agent's model.
	SummaryModel string `json:"summaryModel"`
	// MaxInputTokens bounds the summarization request input (oldest messages
	// are dropped first, single messages truncated). Zero uses the default.
	MaxInputTokens int `json:"maxInputTokens"`
	// SummaryPrompt overrides the built-in summarisation instruction.
	SummaryPrompt string                     `json:"summaryPrompt"`
	Retry         *capcompaction.RetryConfig `json:"retry,omitempty"`
}

type SummaryDeps added in v0.3.33

type SummaryDeps struct {
	LLM           agentkit.LLMProvider  `json:"llm"`
	SessionEvents capsession.Compaction `json:"sessionEvents"`
}

Jump to

Keyboard shortcuts

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