contextwindow

package
v0.5.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	TransformStrategyIntegrator = "integrator"
	TransformStrategyJSONAware  = "json_aware"
	TransformStrategyByteCut    = "byte_cut"
	TransformStrategyNone       = "none"
)

Variables

This section is empty.

Functions

func CountMessages

func CountMessages(messages []Message) int

func EstimateTokens

func EstimateTokens(text string) int

func IsMaskedObservation added in v0.4.2

func IsMaskedObservation(msg Message) bool

IsMaskedObservation reports whether a message content was replaced by MaskObservations.

Types

type CompressionPolicy

type CompressionPolicy struct {
	Enabled      bool    `json:"enabled" yaml:"enabled"`
	TriggerRatio float64 `json:"trigger_ratio,omitempty" yaml:"trigger_ratio,omitempty"`
}

type InsertPosition added in v0.3.0

type InsertPosition string

InsertPosition controls where reinjected context sits after compaction.

const (
	// InsertBeforeLastUserMessage places content above the last user message
	// (and below any trailing summary/system blocks that precede it). Matches
	// Codex InitialContextInjection::BeforeLastUserMessage so the model sees
	// reinjected world/plan context before the latest user turn.
	InsertBeforeLastUserMessage InsertPosition = "before_last_user_message"
	// InsertAppend appends at the end (legacy compact_reminder behavior).
	InsertAppend InsertPosition = "append"
)

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

func New

func New(policy Policy) *Manager

func NewWithSummarizer added in v0.1.4

func NewWithSummarizer(policy Policy, summarizer Summarizer) *Manager

func (*Manager) Prepare

func (m *Manager) Prepare(messages []Message) Result

type Message

type Message struct {
	Role       Role   `json:"role"`
	Content    string `json:"content,omitempty"`
	Name       string `json:"name,omitempty"`
	ToolCallID string `json:"tool_call_id,omitempty"`
	// ToolCallIDs lists tool_call ids issued by an assistant turn. Used for
	// tool-pair-safe trimming (assistant + matching tool results stay atomic).
	ToolCallIDs []string          `json:"tool_call_ids,omitempty"`
	Metadata    map[string]string `json:"metadata,omitempty"`
}

func CompactContext added in v0.4.2

func CompactContext(messages []Message) []Message

CompactContext drops tool messages whose content was masked by MaskObservations, preserving all other messages in order.

func InsertMessage added in v0.3.0

func InsertMessage(messages []Message, msg Message, position InsertPosition) []Message

InsertMessage inserts msg into messages according to position. For BeforeLastUserMessage: finds the last RoleUser and inserts immediately before it; if none, appends.

func MaskObservations added in v0.4.2

func MaskObservations(messages []Message, afterTurns int) []Message

MaskObservations replaces tool-role messages older than afterTurns assistant turns (counting from the latest message backward) with a short placeholder.

type NormalizeResult added in v0.3.0

type NormalizeResult struct {
	Policy       Policy
	PolicySource string // profile | context_policy | default_8192
	Fallback8192 bool
}

NormalizeResult carries a normalized policy plus how MaxInputTokens was derived.

type Policy

type Policy struct {
	Strategy             Strategy `json:"strategy,omitempty" yaml:"strategy,omitempty"`
	ContextWindowTokens  int      `json:"context_window_tokens,omitempty" yaml:"context_window_tokens,omitempty"`
	MaxInputTokens       int      `json:"max_input_tokens,omitempty" yaml:"max_input_tokens,omitempty"`
	ReservedOutputTokens int      `json:"reserved_output_tokens,omitempty" yaml:"reserved_output_tokens,omitempty"`
	SummaryTokens        int      `json:"summary_tokens,omitempty" yaml:"summary_tokens,omitempty"`
	ToolResultMaxTokens  int      `json:"tool_result_max_tokens,omitempty" yaml:"tool_result_max_tokens,omitempty"`
	// ToolOutputMaxBytes caps tool message bytes written to session memory.
	// Zero disables the write-side byte cap (LLM-side ToolResultMaxTokens still applies).
	ToolOutputMaxBytes     int               `json:"tool_output_max_bytes,omitempty" yaml:"tool_output_max_bytes,omitempty"`
	MemoryRecallLimit      int               `json:"memory_recall_limit,omitempty" yaml:"memory_recall_limit,omitempty"`
	SystemPromptProtection bool              `json:"system_prompt_protection,omitempty" yaml:"system_prompt_protection,omitempty"`
	Compression            CompressionPolicy `json:"compression,omitempty" yaml:"compression,omitempty"`
	RoleBudgets            RoleBudgets       `json:"role_budgets,omitempty" yaml:"role_budgets,omitempty"`
	SummaryMode            string            `json:"summary_mode,omitempty" yaml:"summary_mode,omitempty"`
	ToolSchemaPruning      bool              `json:"tool_schema_pruning,omitempty" yaml:"tool_schema_pruning,omitempty"`
	StaleToolTurns         int               `json:"stale_tool_turns,omitempty" yaml:"stale_tool_turns,omitempty"`
	// ExcludeFromStaleWindow lists tool result classes that do not consume a
	// StaleToolTurns slot. When nil, denied and empty are excluded by default.
	ExcludeFromStaleWindow []ToolResultClass `json:"exclude_from_stale_window,omitempty" yaml:"exclude_from_stale_window,omitempty"`
	PinUserMessages        *bool             `json:"pin_user_messages,omitempty" yaml:"pin_user_messages,omitempty"`
	// MemoryStoreLimit caps how many messages are retained in the persistent
	// flat memory store. When exceeded, the oldest messages are dropped on
	// write (keeping the most recent MemoryStoreLimit). Zero (the default)
	// disables the write-side cap, preserving unbounded append behavior.
	MemoryStoreLimit int `json:"memory_store_limit,omitempty" yaml:"memory_store_limit,omitempty"`
	// StripAssistantPatterns is an optional list of substrings; when non-empty,
	// matching assistant messages may be stripped on memory write by integrators
	// that opt in. The framework does not strip by default.
	StripAssistantPatterns []string `json:"strip_assistant_patterns,omitempty" yaml:"strip_assistant_patterns,omitempty"`
	// InjectCompactReminder asks the runtime to reinject a system reminder with
	// active plan/TODO state after compaction drops or summarizes history.
	// Placement is before the last user message (see InsertBeforeLastUserMessage).
	InjectCompactReminder bool `json:"inject_compact_reminder,omitempty" yaml:"inject_compact_reminder,omitempty"`
	// ObservationMaskAfterTurns replaces tool-role messages older than this many
	// assistant turns with a short placeholder before summarization/truncation.
	ObservationMaskAfterTurns int `json:"observation_mask_after_turns,omitempty" yaml:"observation_mask_after_turns,omitempty"`
}

func (Policy) ExcludeFromStaleWindowOrDefault added in v0.3.0

func (p Policy) ExcludeFromStaleWindowOrDefault() []ToolResultClass

ExcludeFromStaleWindowOrDefault returns configured exclusions, or denied+empty.

func (Policy) Normalize

func (p Policy) Normalize() Policy

func (Policy) NormalizeDetailed added in v0.3.0

func (p Policy) NormalizeDetailed() NormalizeResult

func (Policy) PinUserMessagesEnabled added in v0.3.0

func (p Policy) PinUserMessagesEnabled() bool

PinUserMessagesEnabled reports whether user messages should be retained during context-window truncation. When nil, user pinning defaults to true.

type Result

type Result struct {
	Messages []Message `json:"messages"`
	Stats    Stats     `json:"stats"`
}

type Role

type Role string
const (
	RoleSystem    Role = "system"
	RoleUser      Role = "user"
	RoleAssistant Role = "assistant"
	RoleTool      Role = "tool"
)

type RoleBudgets added in v0.1.4

type RoleBudgets struct {
	System    int `json:"system,omitempty" yaml:"system,omitempty"`
	User      int `json:"user,omitempty" yaml:"user,omitempty"`
	Assistant int `json:"assistant,omitempty" yaml:"assistant,omitempty"`
	Tool      int `json:"tool,omitempty" yaml:"tool,omitempty"`
}

type Stats

type Stats struct {
	Strategy                 Strategy `json:"strategy"`
	BeforeTokens             int      `json:"before_tokens"`
	AfterTokens              int      `json:"after_tokens"`
	MaxInputTokens           int      `json:"max_input_tokens"`
	DroppedMessages          int      `json:"dropped_messages"`
	DroppedUserMessages      int      `json:"dropped_user_messages,omitempty"`
	DroppedAssistantMessages int      `json:"dropped_assistant_messages,omitempty"`
	DroppedToolMessages      int      `json:"dropped_tool_messages,omitempty"`
	ContextIncomplete        bool     `json:"context_incomplete,omitempty"`
	Summarized               bool     `json:"summarized"`
	SummaryTokens            int      `json:"summary_tokens,omitempty"`
	PolicySource             string   `json:"policy_source,omitempty"`
	FallbackApplied          bool     `json:"fallback_applied,omitempty"`
	StaleDroppedToolTurns    int      `json:"stale_dropped_tool_turns,omitempty"`
	DenialOccupiedSlots      int      `json:"denial_occupied_slots,omitempty"`
	StaleExcludedTurns       int      `json:"stale_excluded_turns,omitempty"`
	CompactedToolDenials     int      `json:"compacted_tool_denials,omitempty"`
	// NeedsReminder is true when compaction dropped or summarized history and
	// hosts should re-inject active plan/TODO state.
	NeedsReminder bool `json:"needs_reminder,omitempty"`
}

type Strategy

type Strategy string
const (
	StrategyNone                     Strategy = "none"
	StrategySlidingWindow            Strategy = "sliding_window"
	StrategySlidingWindowWithSummary Strategy = "sliding_window_with_summary"
	// StrategyFullReplace summarizes everything outside a recent tail into one
	// system summary, then keeps the tool-pair-safe recent tail.
	StrategyFullReplace Strategy = "full_replace"
)

type Summarizer added in v0.1.4

type Summarizer func(messages []Message, budget int) string

type ToolOutputTransform added in v0.3.0

type ToolOutputTransform func(tool string, raw []byte, limit int) (out []byte, meta TransformMeta)

ToolOutputTransform reshapes a tool's raw output to fit a byte/token budget. limit is a token budget (same units as ToolResultMaxTokens); implementations may convert via rune heuristics.

type ToolResultClass added in v0.3.0

type ToolResultClass string

ToolResultClass classifies tool messages for stale-window accounting.

const (
	ToolResultClassSuccess ToolResultClass = "success"
	ToolResultClassDenied  ToolResultClass = "denied"
	ToolResultClassEmpty   ToolResultClass = "empty"
)

type TransformMeta added in v0.3.0

type TransformMeta struct {
	Truncated      bool   `json:"truncated"`
	OriginalBytes  int    `json:"original_bytes"`
	TruncatedBytes int    `json:"truncated_bytes"`
	Strategy       string `json:"strategy"` // integrator | json_aware | byte_cut | none
}

TransformMeta describes how a tool output was reshaped before LLM/memory use.

func ApplyToolOutputTransform added in v0.3.0

func ApplyToolOutputTransform(tool string, raw []byte, limit int, transforms map[string]ToolOutputTransform) ([]byte, TransformMeta)

ApplyToolOutputTransform applies a per-tool integrator transform when present, otherwise JSON-aware truncation for JSON payloads, otherwise byte truncation. When limit <= 0 the input is returned unchanged.

func ByteTruncate added in v0.3.0

func ByteTruncate(raw []byte, limit int) ([]byte, TransformMeta)

ByteTruncate cuts raw bytes on a rune boundary and marks truncation. The cut point is chosen against EstimateTokens (not a fixed runes-per-token ratio) so CJK-heavy payloads honor the same budget as Latin text.

func JSONAwareTruncate added in v0.3.0

func JSONAwareTruncate(raw []byte, limit int) ([]byte, TransformMeta)

JSONAwareTruncate shortens JSON while keeping it parseable: top-level object keys are preserved; long strings and array elements are trimmed.

Jump to

Keyboard shortcuts

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