Documentation
¶
Index ¶
- func AutoCompactThresholdRatio() float64
- func AutoCompactThresholdTokens(contextWindow int) int
- func EstimateTokens(text string) int
- type CompactResult
- type CompactSnapshot
- type ContextManager
- type Manager
- func (m *Manager) Add(msg provider.Message)
- func (m *Manager) AddedSinceRunStart() []provider.Message
- func (m *Manager) ApplyCompactResult(snapshot CompactSnapshot, result CompactResult) (bool, int)
- func (m *Manager) AutoCompactThreshold() int
- func (m *Manager) CheckAndSummarize(ctx context.Context, prov provider.Provider) (bool, error)
- func (m *Manager) Clear()
- func (m *Manager) CompactSnapshot() CompactSnapshot
- func (m *Manager) ContextWindow() int
- func (m *Manager) Messages() []provider.Message
- func (m *Manager) MessagesAndTokenCount() ([]provider.Message, int)
- func (m *Manager) PromptBudget() int
- func (m *Manager) ReconcileToolCalls() bool
- func (m *Manager) RecordUsage(usage provider.TokenUsage)
- func (m *Manager) RemoveLastAssistantGroup() string
- func (m *Manager) SetCheckpointBaseline(tokens int)
- func (m *Manager) SetContextWindow(n int)
- func (m *Manager) SetOutputReserve(n int)
- func (m *Manager) SetProvider(p provider.Provider)
- func (m *Manager) SetTodoFilePath(path string)
- func (m *Manager) SetUsageHandler(fn func(provider.TokenUsage))
- func (m *Manager) StartRunTracking()
- func (m *Manager) Summarize(ctx context.Context, prov provider.Provider) error
- func (m *Manager) TokenCount() int
- func (m *Manager) TruncateOldestGroupForRetry() bool
- func (m *Manager) UpdateFirstSystemMessage(msg provider.Message)
- func (m *Manager) UsageRatio() float64
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AutoCompactThresholdRatio ¶
func AutoCompactThresholdRatio() float64
func EstimateTokens ¶
EstimateTokens provides a rough token estimation. Uses ~4 chars/token for ASCII and ~1.5 chars/token for CJK, which matches common BPE tokenizer behavior more closely than a flat len/4.
Types ¶
type CompactResult ¶ added in v1.2.1
CompactResult is the output of compacting a CompactSnapshot.
type CompactSnapshot ¶ added in v1.2.1
type CompactSnapshot struct {
Messages []provider.Message
OrigLen int
ContextWindow int
OutputReserve int
TodoPath string
Version int64
}
CompactSnapshot is an immutable point-in-time view used by background compaction. It lets callers summarize a stable copy without mutating the live conversation while an LLM turn may still be running.
func (CompactSnapshot) Compact ¶ added in v1.2.1
func (s CompactSnapshot) Compact(ctx context.Context, prov provider.Provider) (CompactResult, error)
type ContextManager ¶
type ContextManager interface {
Add(msg provider.Message)
Messages() []provider.Message
TokenCount() int
// MessagesAndTokenCount returns both values under a single lock,
// guaranteeing a consistent snapshot.
MessagesAndTokenCount() ([]provider.Message, int)
ContextWindow() int
SetContextWindow(n int)
SetOutputReserve(n int)
RecordUsage(usage provider.TokenUsage)
Summarize(ctx context.Context, prov provider.Provider) error
CheckAndSummarize(ctx context.Context, prov provider.Provider) (bool, error)
TruncateOldestGroupForRetry() bool
RemoveLastAssistantGroup() string
Clear()
UsageRatio() float64
AutoCompactThreshold() int
ReconcileToolCalls() bool
}
ContextManager manages conversation history, tracking tokens and auto-summarizing.
⚠️ Consuming packages must import this as "ctxpkg" to avoid collision with the standard library "context" package.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager implements ContextManager.
func NewManager ¶
NewManager creates a ContextManager with the given context window limit.
func (*Manager) AddedSinceRunStart ¶ added in v1.3.101
AddedSinceRunStart returns messages added via Add() since the last StartRunTracking(). This includes user messages, assistant responses, tool results, synthetic nudges, etc. — everything the agent added.
func (*Manager) ApplyCompactResult ¶ added in v1.2.1
func (m *Manager) ApplyCompactResult(snapshot CompactSnapshot, result CompactResult) (bool, int)
func (*Manager) AutoCompactThreshold ¶ added in v1.1.17
func (*Manager) CheckAndSummarize ¶
CheckAndSummarize triggers summarization if usage ratio >= threshold.
func (*Manager) CompactSnapshot ¶ added in v1.2.1
func (m *Manager) CompactSnapshot() CompactSnapshot
func (*Manager) ContextWindow ¶ added in v1.3.0
func (*Manager) MessagesAndTokenCount ¶ added in v1.1.43
func (*Manager) PromptBudget ¶ added in v1.2.1
func (*Manager) ReconcileToolCalls ¶ added in v1.3.87
ReconcileToolCalls checks whether any assistant message in the conversation has unpaired tool_use blocks (i.e. tool_calls without matching tool_result blocks in subsequent messages). If so, it inserts user messages containing the actual tool results at the correct position (before the next assistant), preserving real execution output rather than dropping it.
This handles two scenarios:
- Session restoration from file: the process crashed while a tool was still pending, so the session file contains an assistant message with tool_use but no tool_result (or tool_results placed after another assistant message).
- Runtime interruption: the user interrupted (e.g. Ctrl+C) while the agent was about to execute tools, and the next user message starts a new agent run without the cancelled tool_results having been added.
Returns true if any messages were inserted or moved. Returns true if any cancelled tool_result entries were added.
func (*Manager) RecordUsage ¶ added in v1.1.17
func (m *Manager) RecordUsage(usage provider.TokenUsage)
func (*Manager) RemoveLastAssistantGroup ¶ added in v1.3.101
RemoveLastAssistantGroup removes the most recent assistant message and any trailing tool messages that follow it. This is used by /regenerate to discard the agent's last response so it can be re-generated. Returns the text of the last remaining user message, or "" if no regeneration is possible (no assistant message found or no preceding user message).
func (*Manager) SetCheckpointBaseline ¶ added in v1.3.101
SetCheckpointBaseline sets the initial token baseline from a session checkpoint. This avoids inflated token counts on session restore where the local estimator (len/4) diverges significantly from real token counts. The first real LLM call (RecordUsage) will override this with actual values.
func (*Manager) SetContextWindow ¶ added in v1.3.0
func (*Manager) SetOutputReserve ¶ added in v1.1.17
func (*Manager) SetProvider ¶
SetProvider sets the provider for provider-aware token counting.
func (*Manager) SetTodoFilePath ¶ added in v1.1.7
func (*Manager) SetUsageHandler ¶ added in v1.3.41
func (m *Manager) SetUsageHandler(fn func(provider.TokenUsage))
func (*Manager) StartRunTracking ¶ added in v1.3.101
func (m *Manager) StartRunTracking()
StartRunTracking clears the run-added message tracking. Call this at the start of each agent RunStreamWithContent. After the run, AddedSinceRunStart() returns all messages that were added via Add() during this run.
Note: ApplyCompactResult replaces m.messages directly (bypassing Add), so compaction does NOT pollute runAdded. Messages added before compaction but during the same run are still tracked — this is correct because they are real conversation events that need to be persisted.
func (*Manager) Summarize ¶
Summarize compresses all messages (except system prompt) into a single summary. This implements rolling compaction: each invocation produces [system, summary, extra...] where extra = messages that arrived during the async compaction window. On the next trigger, the summary itself is included in the compression input, producing a fresh summary.
func (*Manager) TokenCount ¶
func (*Manager) TruncateOldestGroupForRetry ¶ added in v1.1.15
func (*Manager) UpdateFirstSystemMessage ¶ added in v1.1.34
UpdateFirstSystemMessage replaces the first system message in the context. If no system message exists, it prepends one.