Files
gnoma/internal/context/tracker.go
T
vikingowl b0b393517e feat: M6 context intelligence — token tracker + truncation compaction
internal/context/:
- Tracker: monitors token usage with OK/Warning/Critical states
  (thresholds from CC: 20K warning buffer, 13K autocompact buffer)
- TruncateStrategy: drops oldest messages, preserves system prompt +
  recent N turns, adds compaction boundary marker
- Window: manages message history with auto-compaction trigger,
  circuit breaker after 3 consecutive failures

Engine integration:
- Context window tracks usage per turn
- Auto-compacts when critical threshold reached
- History syncs with context window after compaction

TUI status bar:
- Token count with percentage (tokens: 1234 (5%))
- Color-coded: green=ok, yellow=warning, red=critical

Session Status extended: TokensMax, TokenPercent, TokenState.
7 context tests.
2026-04-03 18:46:03 +02:00

121 lines
2.6 KiB
Go

package context
import (
"somegit.dev/Owlibou/gnoma/internal/message"
)
// TokenState indicates how close to the context limit we are.
type TokenState int
const (
TokensOK TokenState = iota // well within budget
TokensWarning // approaching limit
TokensCritical // at or near limit, compaction needed
)
func (s TokenState) String() string {
switch s {
case TokensOK:
return "ok"
case TokensWarning:
return "warning"
case TokensCritical:
return "critical"
default:
return "unknown"
}
}
// Thresholds for compaction triggers (from CC autoCompact.ts).
const (
DefaultAutocompactBuffer = 13_000 // tokens below context window to trigger
DefaultWarningBuffer = 20_000 // tokens below context window for warning
)
// Tracker monitors cumulative token usage against a context window budget.
type Tracker struct {
maxTokens int64 // context window size
current int64 // cumulative tokens used
// Configurable buffers
autocompactBuffer int64
warningBuffer int64
}
func NewTracker(maxTokens int64) *Tracker {
return &Tracker{
maxTokens: maxTokens,
autocompactBuffer: DefaultAutocompactBuffer,
warningBuffer: DefaultWarningBuffer,
}
}
// Add records token usage from a turn.
func (t *Tracker) Add(usage message.Usage) {
t.current += usage.InputTokens + usage.OutputTokens
}
// Set overrides the current token count (e.g., after compaction).
func (t *Tracker) Set(tokens int64) {
t.current = tokens
}
// Reset clears the tracked usage.
func (t *Tracker) Reset() {
t.current = 0
}
// Used returns the current token count.
func (t *Tracker) Used() int64 {
return t.current
}
// MaxTokens returns the context window size.
func (t *Tracker) MaxTokens() int64 {
return t.maxTokens
}
// Remaining returns tokens left before the context window limit.
func (t *Tracker) Remaining() int64 {
rem := t.maxTokens - t.current
if rem < 0 {
return 0
}
return rem
}
// PercentUsed returns 0-100 indicating usage level.
func (t *Tracker) PercentUsed() int {
if t.maxTokens <= 0 {
return 0
}
pct := int((t.current * 100) / t.maxTokens)
if pct > 100 {
return 100
}
return pct
}
// State returns the current token warning state.
func (t *Tracker) State() TokenState {
if t.maxTokens <= 0 {
return TokensOK
}
threshold := t.maxTokens - t.autocompactBuffer
warningThreshold := t.maxTokens - t.warningBuffer
if t.current >= threshold {
return TokensCritical
}
if t.current >= warningThreshold {
return TokensWarning
}
return TokensOK
}
// ShouldCompact returns true if auto-compaction should trigger.
func (t *Tracker) ShouldCompact() bool {
return t.State() == TokensCritical
}