Help us shape the future of Go! Take the 2026 Go Developer Survey

relevanceprune

package
v0.0.0-...-a92561a Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: MIT Imports: 3 Imported by: 0

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

View Source
const DefaultCompactTailTurns = 3

DefaultCompactTailTurns is the number of recent messages preserved verbatim.

Variables

This section is empty.

Functions

func CalculateRelevance

func CalculateRelevance(m Message, o Options) float64

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

func NormalizeCompactTailTurns(value int) int

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

func RoughTokenCount(text string) int

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

func PruneByRelevance(messages []Message, o Options) []Message

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.

type Options

type Options struct {
	TargetTokens   int
	TaskContext    string
	PreserveRecent int
	PreserveTools  bool
	PreserveErrors bool
}

Options configures a pruning pass.

Jump to

Keyboard shortcuts

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