Documentation
¶
Overview ¶
Package relevanceprune provides deterministic, token-budgeted context pruning that keeps messages relevant to the current task while preserving recent turns, tool calls, and error messages verbatim. It is the Go port of OpenClaude's relevance-based context pruning — a cheap, predictable alternative to pure LLM summarization: score older messages by keyword overlap against the task context, keep the highest-relevance groups up to a token budget, and always retain the most recent tail.
Index ¶
Constants ¶
const DefaultCompactTailTurns = 3
DefaultCompactTailTurns is the number of recent messages preserved verbatim.
Variables ¶
This section is empty.
Functions ¶
func CalculateRelevance ¶
CalculateRelevance scores a message in [0,1]. Base 0.5, boosted by keyword overlap with the task context, preserved tool calls / errors, recency, and user role.
func GetRelevanceStats ¶
func GetRelevanceStats(messages []Message, o Options) (average float64, highCount, toolCalls, errors int)
GetRelevanceStats summarizes a set of messages.
func NormalizeCompactTailTurns ¶
NormalizeCompactTailTurns floors any finite value >= 1 to an integer and falls back to the default for everything else (0, negatives, fractions below 1, NaN). UI and runtime must share this single rule.
func RoughTokenCount ¶
RoughTokenCount estimates tokens as chars/4, matching the source's cheap heuristic.
Types ¶
type Message ¶
type Message struct {
Role string // "user", "assistant", or "system"
Content string
Timestamp time.Time
HasToolCall bool
IsError bool
}
Message is a minimal conversation message the pruner understands.
func PruneByRelevance ¶
PruneByRelevance drops low-relevance history to fit targetTokens while preserving the most recent preserveRecent messages verbatim. Older messages are grouped by API round and scored by average relevance; the highest-scoring groups are kept until the token budget is exhausted, then everything is re-sorted chronologically.