filter

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: MIT Imports: 32 Imported by: 0

Documentation

Overview

Package filter provides LRU caching using the unified cache package. This file provides backward compatibility for existing code.

Package filter provides the core token compression pipeline for TokMan.

The filter package implements a multi-layer compression architecture inspired by 120+ research papers from top institutions. It processes CLI output through a series of filter layers to reduce token usage in LLM interactions while preserving semantic meaning.

Pipeline Architecture

The main entry point is PipelineCoordinator, which orchestrates up to 50+ compression layers organized into logical groups:

  • Core Layers (1-9): Entropy, Perplexity, AST preservation, etc.
  • Semantic Layers (11-20): Compaction, H2O, Attention Sink, etc.
  • Research Layers (21-49): Advanced techniques like DiffAdapt, EPiC, etc.

Usage

Create a coordinator with configuration and process text:

pipeline := filter.NewPipelineCoordinator(config)
output, stats := pipeline.Process(inputText)
fmt.Printf("Saved %d tokens (%.1f%%)\n", stats.TotalSaved, stats.ReductionPercent)

Filter Engine

For lightweight post-processing, the Engine type provides a simpler filter chain for tasks like ANSI stripping and comment removal.

Index

Constants

View Source
const (
	// Budget thresholds
	TightBudgetThreshold   = 1000 // Budgets below this need aggressive filtering
	MinimalBudgetThreshold = 100  // Budgets below this are emergency mode
	DefaultBudget          = 0    // No budget limit

	// Content size thresholds
	MinContentLength       = 50     // Below this, entropy calc is unreliable
	SmallContentThreshold  = 200    // Use fast path for content below this
	MediumContentThreshold = 1000   // Switch to normal processing
	LargeContentThreshold  = 10000  // Use aggressive compression
	StreamingThreshold     = 500000 // Process in chunks above this

	// Compression thresholds
	HighCompressionRatio   = 0.95 // 95% compression indicates over-compression
	TargetCompressionRatio = 0.75 // Ideal compression target
	MinCompressionRatio    = 0.10 // Below this, compression isn't worth it

	// Layer thresholds
	EntropyMinLength       = 50  // Min length for entropy filtering
	PerplexityMinLines     = 5   // Min lines for perplexity pruning
	H2OMinTokens           = 50  // Min tokens for H2O filter
	AttentionSinkMinLines  = 3   // Min lines for attention sink
	MetaTokenMinLength     = 500 // Min length for meta-token compression
	SemanticChunkMinLength = 300 // Min length for semantic chunking

	// Early exit configuration
	EarlyExitCheckInterval      = 3 // Check budget every N layers
	EarlyExitAggressiveInterval = 1 // Check every layer for tight budgets

	// Cache configuration
	DefaultCacheSize   = 1000 // Default cache size
	DefaultCacheTTL    = 300  // Default TTL in seconds (5 min)
	CacheEvictionBatch = 100  // Evict this many at a time

	// Token estimation
	TokensPerCharHeuristic = 0.25 // 1 token per 4 chars
	MinTokenEstimate       = 1    // Minimum token count
)

Pipeline configuration constants.

View Source
const (
	LayerGateModeAll        = "all"
	LayerGateModeStableOnly = "stable-only"
)
View Source
const ChunkSize = 100000 // 100K tokens per chunk

ChunkSize is the target size for each chunk in streaming mode.

Variables

View Source
var AutoTierConfigs = map[AutoTier]AutoTierConfig{
	AutoTierPre: {
		Name:        "pre",
		Description: "Pre-processing (alignment, image compression)",
		LayerRange:  [2]int{0, 0},
		Default:     true,
		AutoEnable:  true,
		CostLevel:   1,
		MinInputLen: 0,
		ContentTypes: []ContentType{
			ContentTypeUnknown, ContentTypeCode, ContentTypeLogs,
			ContentTypeConversation, ContentTypeGitOutput,
			ContentTypeTestOutput, ContentTypeDockerOutput, ContentTypeMixed,
		},
	},
	AutoTierCore: {
		Name:        "core",
		Description: "Foundation compression (essential, high ROI)",
		LayerRange:  [2]int{1, 10},
		Default:     true,
		AutoEnable:  true,
		CostLevel:   1,
		MinInputLen: 100,
		ContentTypes: []ContentType{
			ContentTypeUnknown, ContentTypeCode, ContentTypeLogs,
			ContentTypeConversation, ContentTypeGitOutput,
			ContentTypeTestOutput, ContentTypeDockerOutput, ContentTypeMixed,
		},
	},
	AutoTierSemantic: {
		Name:        "semantic",
		Description: "Semantic analysis (medium cost, high quality)",
		LayerRange:  [2]int{11, 20},
		Default:     true,
		AutoEnable:  true,
		CostLevel:   2,
		MinInputLen: 500,
		ContentTypes: []ContentType{
			ContentTypeCode, ContentTypeConversation,
			ContentTypeGitOutput, ContentTypeMixed,
		},
	},
	AutoTierAdvanced: {
		Name:        "advanced",
		Description: "Research layers (specialized, higher cost)",
		LayerRange:  [2]int{21, 40},
		Default:     false,
		AutoEnable:  true,
		CostLevel:   3,
		MinInputLen: 1000,
		ContentTypes: []ContentType{
			ContentTypeCode, ContentTypeConversation, ContentTypeMixed,
		},
	},
	AutoTierSpecialized: {
		Name:        "specialized",
		Description: "Experimental/edge case layers (auto-enabled for large mixed content)",
		LayerRange:  [2]int{41, 50},
		Default:     false,
		AutoEnable:  true,
		CostLevel:   4,
		MinInputLen: 5000,
		ContentTypes: []ContentType{
			ContentTypeMixed,
			ContentTypeConversation,
		},
	},
}

AutoTierConfigs defines all tier configurations.

View Source
var BlockDelimiters = map[rune]rune{
	'{': '}',
	'[': ']',
	'(': ')',
}

BlockDelimiters for brace tracking

View Source
var CommentPatternsMap = map[Language]*regexp.Regexp{
	LangRust:       regexp.MustCompile(`(?m)^//.*$|/\*[\s\S]*?\*/`),
	LangGo:         regexp.MustCompile(`(?m)^//.*$|/\*[\s\S]*?\*/`),
	LangPython:     regexp.MustCompile(`(?m)^#.*$|"""[\s\S]*?"""|'''[\s\S]*?'''`),
	LangJavaScript: regexp.MustCompile(`(?m)^//.*$|/\*[\s\S]*?\*/`),
	LangTypeScript: regexp.MustCompile(`(?m)^//.*$|/\*[\s\S]*?\*/`),
	LangJava:       regexp.MustCompile(`(?m)^//.*$|/\*[\s\S]*?\*/`),
	LangC:          regexp.MustCompile(`(?m)^//.*$|/\*[\s\S]*?\*/`),
	LangCpp:        regexp.MustCompile(`(?m)^//.*$|/\*[\s\S]*?\*/`),
	LangRuby:       regexp.MustCompile(`(?m)^#.*$`),
	LangShell:      regexp.MustCompile(`(?m)^#.*$`),
	LangSQL:        regexp.MustCompile(`(?m)^--.*$|/\*[\s\S]*?\*/`),
}

CommentPatternsMap maps languages to their comment regex patterns

View Source
var ImportPatterns = []*regexp.Regexp{
	regexp.MustCompile(`^use\s+`),
	regexp.MustCompile(`^import\s+`),
	regexp.MustCompile(`^from\s+\S+\s+import`),
	regexp.MustCompile(`^require\(`),
	regexp.MustCompile(`^import\s*\(`),
	regexp.MustCompile(`^import\s+"`),
	regexp.MustCompile(`#include\s*<`),
	regexp.MustCompile(`#include\s*"`),
	regexp.MustCompile(`^package\s+`),
}

ImportPatterns for various languages

View Source
var SignaturePatterns = []*regexp.Regexp{
	regexp.MustCompile(`^(pub\s+)?(async\s+)?fn\s+\w+`),
	regexp.MustCompile(`^(pub\s+)?struct\s+\w+`),
	regexp.MustCompile(`^(pub\s+)?enum\s+\w+`),
	regexp.MustCompile(`^(pub\s+)?trait\s+\w+`),
	regexp.MustCompile(`^(pub\s+)?type\s+\w+`),
	regexp.MustCompile(`^impl\s+`),
	regexp.MustCompile(`^func\s+(\([^)]+\)\s+)?\w+`),
	regexp.MustCompile(`^type\s+\w+\s+(struct|interface)`),
	regexp.MustCompile(`^type\s+\w+\s+\w+`),
	regexp.MustCompile(`^def\s+\w+`),
	regexp.MustCompile(`^async\s+def\s+\w+`),
	regexp.MustCompile(`^class\s+\w+`),
	regexp.MustCompile(`^function\s+\w+`),
	regexp.MustCompile(`^(export\s+)?(async\s+)?function\s*\w*`),
	regexp.MustCompile(`^(export\s+)?(default\s+)?class\s+\w+`),
	regexp.MustCompile(`^(export\s+)?const\s+\w+\s*=\s*(async\s+)?\([^)]*\)\s*=>`),
	regexp.MustCompile(`^interface\s+\w+`),
	regexp.MustCompile(`^type\s+\w+\s*=`),
	regexp.MustCompile(`^(public|private|protected)?\s*(static\s+)?(class|interface|enum)\s+\w+`),
	regexp.MustCompile(`^(public|private|protected)?\s*(static\s+)?(async\s+)?\w+\s+\w+\s*\(`),
}

SignaturePatterns for function/type signature detection

Functions

func ApplyMode

func ApplyMode(input string, mode Mode, cm CompressionMode) (string, int)

ApplyMode is an alias for ApplyTier (backwards compat).

func ApplyProfile

func ApplyProfile(input string, mode Mode, profile Profile) (string, int)

ApplyProfile is an alias for ApplyTier (backwards compat).

func ApplyTier

func ApplyTier(input string, mode Mode, tier Tier) (string, int)

ApplyTier compresses input using the specified tier.

func AutoProcess

func AutoProcess(input string, mode Mode) (string, int)

AutoProcess detects content type and applies the optimal profile.

func CacheabilityScore

func CacheabilityScore(content string) int

CacheabilityScore returns a 0-100 score indicating how cacheable content is. Higher scores mean more stable prefix, better cache hit rate.

func ClassifyContent

func ClassifyContent(content string) (isStatic bool, confidence float64)

ClassifyContent classifies content as static or dynamic.

func CollectOutputs added in v0.28.0

func CollectOutputs(results []ParallelProcessResult) []string

CollectOutputs collects all outputs from results in order

func DetectLanguage

func DetectLanguage(output string) string

DetectLanguage attempts to detect the programming language from output using weighted scoring across multiple indicators.

func EstimateCacheHitRate

func EstimateCacheHitRate(content string) float64

EstimateCacheHitRate estimates the cache hit rate for repeated requests.

func EstimateTierCost added in v0.28.0

func EstimateTierCost(tiers []AutoTier, inputTokens int) int

EstimateTierCost estimates processing cost for given tiers.

func EstimateTokens

func EstimateTokens(text string) int

EstimateTokens provides a heuristic token count. Delegates to core.EstimateTokens for single source of truth (T22).

func ExecuteFiltersParallel added in v0.28.0

func ExecuteFiltersParallel(filters []filterLayer, input string, mode Mode) (string, int)

ExecuteFiltersParallel runs independent filters in parallel This improves throughput for multi-core systems

func ExecuteFiltersSequential added in v0.28.0

func ExecuteFiltersSequential(filters []filterLayer, input string, mode Mode) (string, int)

ExecuteFiltersSequential runs filters sequentially Use when filters depend on each other's output

func FilterNoisyOutput

func FilterNoisyOutput(input string) string

FilterNoisyOutput removes common noise from terminal output.

func FilterProgressBars

func FilterProgressBars(input string) string

FilterProgressBars removes progress bar lines from output. Progress bars are noisy and consume tokens.

func FormatDelta

func FormatDelta(delta IncrementalDelta) string

FormatDelta returns a human-readable delta string.

func FormatTDDStats

func FormatTDDStats(stats TDDStats) string

FormatTDDStats returns a human-readable stats string.

func Ftoa

func Ftoa(f float64, prec int) string

Ftoa converts float to string with precision.

func GetBytes added in v0.28.0

func GetBytes(capacity int) *[]byte

GetBytes is a convenience function to get bytes from the global pool

func HammingDistance

func HammingDistance(a, b uint64) int

HammingDistance returns the number of differing bits between two hashes.

func HasANSI

func HasANSI(input string) bool

HasANSI checks if the input contains ANSI escape sequences. Uses SIMD-optimized implementation for 8x speedup.

func IsCode

func IsCode(output string) bool

IsCode checks if the output looks like source code.

func IsNearDuplicate

func IsNearDuplicate(a, b string, threshold int) bool

IsNearDuplicate returns true if two content blocks are near-duplicates. Uses SimHash with configurable Hamming distance threshold.

func Itoa

func Itoa(n int) string

Itoa converts int to string.

func JSONPathExtract

func JSONPathExtract(jsonStr, path string) string

JSONPathExtract extracts values from JSON using simple path notation.

func NewLRUCache

func NewLRUCache(maxSize int, ttl time.Duration) *cache.LRUCache

NewLRUCache creates an LRU cache with given max size and TTL.

func PreferLessMode

func PreferLessMode(original, filtered string) string

PreferLessMode compares filtered vs piped output and uses smaller. Inspired by tokf's prefer-less mode.

func PruneMetadata

func PruneMetadata(input string) string

PruneMetadata removes unnecessary metadata from JSON (npm URLs, integrity hashes, etc.).

func PutBytes added in v0.28.0

func PutBytes(b *[]byte)

PutBytes is a convenience function to return bytes to the global pool

func QuickProcess

func QuickProcess(input string, mode Mode) (string, int)

QuickProcess compresses input with default configuration

func QuickProcessPreset

func QuickProcessPreset(input string, mode Mode, preset PipelinePreset) (string, int)

func ReadContent

func ReadContent(content string, opts ReadOptions) string

ReadContent reads content with the specified mode.

func ShouldStream added in v0.28.0

func ShouldStream(input string) bool

ShouldStream returns true if the input should be processed in streaming mode.

func ShouldUseParallel added in v0.28.0

func ShouldUseParallel(filters []filterLayer, inputSize int) bool

ShouldUseParallel determines if parallel execution is beneficial

func SimHash

func SimHash(content string) uint64

SimHash computes a 64-bit fingerprint for content deduplication. Uses character n-gram hashing with Hamming distance for near-duplicate detection.

func SmartTruncate added in v0.28.0

func SmartTruncate(content string, maxLines int, lang Language) string

SmartTruncate truncates content while preserving function signatures. This is useful for code files where you want to keep the API surface but remove implementation details.

func StripANSI

func StripANSI(input string) string

StripANSI is a utility function to strip ANSI codes from a string. Delegates to SIMD-optimized implementation.

func StripLineNumbers

func StripLineNumbers(input string) string

StripLineNumbers removes line number prefixes from tool output (e.g., "1-> content").

func TotalSaved added in v0.28.0

func TotalSaved(results []ParallelProcessResult) int

TotalSaved calculates total tokens saved from results

Types

type ACONFilter

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

Paper: "ACON: Optimizing Context Compression for Long-Context LLMs" — ICLR 2026 ACONFilter implements adaptive context optimization — dynamically adjusts compression based on content complexity and context length.

func NewACONFilter

func NewACONFilter() *ACONFilter

NewACONFilter creates a new ACON-style context compression filter.

func (*ACONFilter) Apply

func (f *ACONFilter) Apply(input string, mode Mode) (string, int)

Apply applies adaptive context compression.

func (*ACONFilter) Name

func (f *ACONFilter) Name() string

Name returns the layer name.

type ANSICode

type ANSICode struct {
	Position int
	Code     string
}

ANSICode represents an ANSI escape sequence.

type ANSIFilter

type ANSIFilter struct{}

ANSIFilter strips ANSI escape sequences from output. Uses SIMD-optimized byte scanning for ~10-40x speedup over regex.

func NewANSIFilter

func NewANSIFilter() *ANSIFilter

NewANSIFilter creates a new ANSI filter.

func (*ANSIFilter) Apply

func (f *ANSIFilter) Apply(input string, mode Mode) (string, int)

Apply strips ANSI sequences and returns token savings.

func (*ANSIFilter) Name

func (f *ANSIFilter) Name() string

Name returns the filter name.

type ASTPreserveFilter

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

Paper: "LongCodeZip" — Shi et al., SJTU/Stanford, 2025 https://arxiv.org/abs/2510.00446 Paper: "LongCodeZip" — Shi et al., SJTU/Stanford, 2025 https://arxiv.org/abs/2510.00446 ASTPreserveFilter implements LongCodeZip-style compression (NUS, 2025). AST-aware compression that preserves syntactic validity of code.

Algorithm: 1. Detect programming language from syntax patterns 2. Parse code structure (brackets, braces, indentation) 3. Apply entropy-based pruning while preserving AST integrity 4. Never break syntactic boundaries (function bodies, blocks, strings)

Enhanced with Dual-Stage LongCodeZip Methodology: - Stage 1: Coarse-Grained (Function-Level) pruning - Stage 2: Fine-Grained (Block-Level) adaptive compression

Research Results: 4-8x compression while maintaining parseable code. LongCodeZip: 5.6x reduction, 16% better accuracy than LLMLingua on code.

func NewASTPreserveFilter

func NewASTPreserveFilter() *ASTPreserveFilter

NewASTPreserveFilter creates a new AST-aware filter

func (*ASTPreserveFilter) Apply

func (f *ASTPreserveFilter) Apply(input string, mode Mode) (string, int)

Apply applies AST-aware filtering

func (*ASTPreserveFilter) Name

func (f *ASTPreserveFilter) Name() string

Name returns the filter name

func (*ASTPreserveFilter) SetQueryIntent

func (f *ASTPreserveFilter) SetQueryIntent(query string)

SetQueryIntent sets the query intent for query-aware scoring

type AdaptiveLayerSelector

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

AdaptiveLayerSelector dynamically enables/disables layers based on content type. Uses heuristic analysis to optimize compression for different input patterns.

func NewAdaptiveLayerSelector

func NewAdaptiveLayerSelector() *AdaptiveLayerSelector

NewAdaptiveLayerSelector creates a new adaptive selector

func (*AdaptiveLayerSelector) AnalyzeContent

func (a *AdaptiveLayerSelector) AnalyzeContent(input string) ContentType

AnalyzeContent detects the primary content type

func (*AdaptiveLayerSelector) OptimizePipeline

func (a *AdaptiveLayerSelector) OptimizePipeline(input string, mode Mode) *PipelineCoordinator

OptimizePipeline returns an optimized coordinator for the given input

func (*AdaptiveLayerSelector) RecommendedConfig

func (a *AdaptiveLayerSelector) RecommendedConfig(ct ContentType, mode Mode) PipelineConfig

RecommendedConfig returns optimal layer configuration for the content type

func (*AdaptiveLayerSelector) RecommendedConfigWithTiers added in v0.28.0

func (a *AdaptiveLayerSelector) RecommendedConfigWithTiers(ct ContentType, mode Mode, inputLen int, queryIntent string) PipelineConfig

RecommendedConfigWithTiers returns optimal configuration using tier-based enablement. This is the recommended way to configure the pipeline for automatic tier selection.

type AdaptiveLearningFilter added in v0.28.0

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

AdaptiveLearningFilter combines Layer 50 (EngramLearner) and Layer 51 (TieredSummary) into a single post-processing layer that learns from patterns and generates progressive summaries.

This merged layer: 1. First applies EngramLearner to detect and learn error patterns 2. Then applies TieredSummary for L0/L1/L2 progressive summarization

Research basis: - EngramLearner: Error pattern learning with 14 classifiers - TieredSummary: Progressive summarization (surface → structural → deep)

func NewAdaptiveLearningFilter added in v0.28.0

func NewAdaptiveLearningFilter() *AdaptiveLearningFilter

NewAdaptiveLearningFilter creates a new adaptive learning filter. This replaces both NewEngramLearner() and NewTieredSummaryFilter().

func (*AdaptiveLearningFilter) Apply added in v0.28.0

func (a *AdaptiveLearningFilter) Apply(input string, mode Mode) (string, int)

Apply runs both learning and summarization in sequence.

func (*AdaptiveLearningFilter) GenerateTiers added in v0.28.0

func (a *AdaptiveLearningFilter) GenerateTiers(input string) *TieredResult

GenerateTiers generates tiered summaries from input.

func (*AdaptiveLearningFilter) GetLearningStats added in v0.28.0

func (a *AdaptiveLearningFilter) GetLearningStats() map[string]interface{}

GetLearningStats returns statistics from the learning component.

func (*AdaptiveLearningFilter) IsEnabled added in v0.28.0

func (a *AdaptiveLearningFilter) IsEnabled() bool

IsEnabled returns whether the filter is enabled.

func (*AdaptiveLearningFilter) Name added in v0.28.0

func (a *AdaptiveLearningFilter) Name() string

Name returns the filter name.

func (*AdaptiveLearningFilter) SetEnabled added in v0.28.0

func (a *AdaptiveLearningFilter) SetEnabled(enabled bool)

SetEnabled enables or disables the filter.

type AdvancedFilter added in v0.28.0

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

func NewAdvancedFilter added in v0.28.0

func NewAdvancedFilter() *AdvancedFilter

func (*AdvancedFilter) Apply added in v0.28.0

func (f *AdvancedFilter) Apply(input string, mode Mode) (string, int)

func (*AdvancedFilter) Name added in v0.28.0

func (f *AdvancedFilter) Name() string

type AgentMemoryConfig

type AgentMemoryConfig struct {
	// KnowledgeRetentionRatio is the ratio of knowledge to keep (0.0-1.0)
	KnowledgeRetentionRatio float64

	// HistoryPruneRatio is the ratio of history to prune after consolidation
	HistoryPruneRatio float64

	// ConsolidationThreshold triggers consolidation when history exceeds this
	ConsolidationThreshold int

	// EnableAutoConsolidation allows autonomous memory management
	EnableAutoConsolidation bool

	// KnowledgeMaxSize limits the knowledge block size
	KnowledgeMaxSize int

	// PreservePatterns are regex patterns for important content to always keep
	PreservePatterns []*regexp.Regexp
}

AgentMemoryConfig holds configuration for agent memory management

func DefaultAgentMemoryConfig

func DefaultAgentMemoryConfig() AgentMemoryConfig

DefaultAgentMemoryConfig returns default configuration

type AgentMemoryFilter

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

AgentMemoryFilter implements Layer 20: Agent Memory Mode (Focus-inspired).

Research Source: "Active Context Compression / Focus" (arXiv, January 2026) Key Innovation: Agent-centric autonomous memory management inspired by slime mold. Results: 22.7% token reduction (14.9M → 11.5M), 57% savings on individual instances.

Methodology (Physarum polycephalum inspired): 1. Knowledge Consolidation - Extract learnings into "Knowledge" block 2. Active Withdrawal - Prune raw interaction history after consolidation 3. Self-Regulation - Agent decides when to consolidate vs. keep raw

This filter maintains session state and autonomously manages context bloating in long-horizon agent tasks by distinguishing between: - Knowledge: Consolidated insights (high value, permanent) - History: Raw interaction logs (transient, prunable)

func NewAgentMemoryFilter

func NewAgentMemoryFilter() *AgentMemoryFilter

NewAgentMemoryFilter creates a new agent memory filter

func NewAgentMemoryFilterWithConfig

func NewAgentMemoryFilterWithConfig(cfg AgentMemoryConfig) *AgentMemoryFilter

NewAgentMemoryFilterWithConfig creates a filter with custom config

func (*AgentMemoryFilter) Apply

func (f *AgentMemoryFilter) Apply(input string, mode Mode) (string, int)

Apply applies agent memory management compression

func (*AgentMemoryFilter) GetStats

func (f *AgentMemoryFilter) GetStats() AgentMemoryStats

GetStats returns current memory management statistics

func (*AgentMemoryFilter) Name

func (f *AgentMemoryFilter) Name() string

Name returns the filter name

func (*AgentMemoryFilter) Reset

func (f *AgentMemoryFilter) Reset()

Reset clears the memory state (for new sessions)

type AgentMemoryLayerConfig

type AgentMemoryLayerConfig struct {
	Enabled            bool
	KnowledgeRetention float64
	HistoryPrune       float64
	ConsolidationMax   int
}

AgentMemoryLayerConfig groups Layer 20 settings.

type AgentMemoryStats

type AgentMemoryStats struct {
	TotalConsolidated int
	TotalPruned       int
	KnowledgeTokens   int
	HistoryTokens     int
	TokensSaved       int
}

AgentMemoryStats tracks memory management statistics

type AgentOCRFilter added in v0.28.0

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

Paper: "AgentOCR: Content-Density Aware Compression for Multi-Turn Agent Trajectories" arXiv 2026

AgentOCRFilter operates on multi-turn agent outputs (tool call sequences, conversation turns). It measures the "content density" of each turn — the ratio of information-bearing lines to total lines — and collapses low-density turns into a single summary stub while preserving high-density turns in full.

Content density signals:

  • Information-bearing: error lines, code lines, unique-term-rich lines
  • Filler: empty lines, repeated terms, pure-whitespace lines

Turn detection: turns are separated by patterns like:

  • "Human:", "Assistant:", "User:", "Agent:", "System:"
  • "<human>", "<assistant>", markdown "## Turn N"

Density thresholds:

  • Low density (<0.30): collapse to "[Turn summary: N lines, M tokens]"
  • Medium density (0.30–0.65): keep first+last few lines + omission marker
  • High density (>0.65): preserve fully

func NewAgentOCRFilter added in v0.28.0

func NewAgentOCRFilter() *AgentOCRFilter

NewAgentOCRFilter creates a new agent turn content-density filter.

func (*AgentOCRFilter) Apply added in v0.28.0

func (f *AgentOCRFilter) Apply(input string, mode Mode) (string, int)

Apply collapses low-density agent turns, trims medium-density ones.

func (*AgentOCRFilter) Name added in v0.28.0

func (f *AgentOCRFilter) Name() string

Name returns the filter name.

type AgentOCRHistoryFilter added in v0.28.0

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

AgentOCRHistoryFilter compacts older conversation turns while preserving recent turns.

func NewAgentOCRHistoryFilter added in v0.28.0

func NewAgentOCRHistoryFilter() *AgentOCRHistoryFilter

NewAgentOCRHistoryFilter creates the history-focused AgentOCR extension.

func (*AgentOCRHistoryFilter) Apply added in v0.28.0

func (f *AgentOCRHistoryFilter) Apply(input string, mode Mode) (string, int)

Apply compresses old low-density turns and preserves recent high-signal turns.

func (*AgentOCRHistoryFilter) Name added in v0.28.0

func (f *AgentOCRHistoryFilter) Name() string

Name returns the filter name.

type AggregateStats added in v0.28.0

type AggregateStats struct {
	AvgCompression    float64
	MinCompression    float64
	MaxCompression    float64
	StdDevCompression float64
	AvgLatency        float64
	TotalTime         time.Duration
	AvgQuality        float64
}

AggregateStats provides summary statistics.

type AnchorToken

type AnchorToken struct {
	Text         string
	Position     int
	Score        float64
	Aggregated   []string // Tokens aggregated into this anchor
	IsStructural bool
}

AnchorToken represents a semantic anchor point

type ApplicabilityCheck

type ApplicabilityCheck interface {
	IsApplicable(input string) bool
}

ApplicabilityCheck is an optional interface that filters can implement to report whether they should run for a given input. The coordinator calls this before Apply to implement stage gates (skip cheap before expensive).

type AttentionSinkFilter

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

Paper: "StreamingLLM: Attention Sinks" — Xiao et al., MIT, 2023 https://arxiv.org/abs/2309.17453 Paper: "StreamingLLM: Attention Sinks" — Xiao et al., MIT, 2023 https://arxiv.org/abs/2309.17453 AttentionSinkFilter implements StreamingLLM-style attention sink preservation. Research basis: "Efficient Streaming Language Models with Attention Sinks" (Xiao et al., 2023) - enables infinite-length generation with bounded memory.

Key insight: The first few tokens in a sequence act as "attention sinks" - they absorb excess attention weight due to softmax normalization. Removing these tokens breaks the attention distribution the model learned during training.

This layer: 1. Always preserves initial tokens (attention sinks) 2. Preserves structural anchors (headers, prefixes, markers) 3. Applies rolling cache to remaining content

This is Layer 14 in the pipeline, ensuring stable compression for long content.

func NewAdaptiveAttentionSinkFilter

func NewAdaptiveAttentionSinkFilter(outputLines int) *AttentionSinkFilter

NewAdaptiveAttentionSinkFilter creates a filter with adaptive sink count. StreamingLLM insight — sink count scales with output length.

func NewAttentionSinkFilter

func NewAttentionSinkFilter() *AttentionSinkFilter

NewAttentionSinkFilter creates a new attention sink filter

func (*AttentionSinkFilter) Apply

func (a *AttentionSinkFilter) Apply(input string, mode Mode) (string, int)

Apply applies attention sink preservation to the input

func (*AttentionSinkFilter) GetStats

func (a *AttentionSinkFilter) GetStats() map[string]any

GetStats returns filter statistics

func (*AttentionSinkFilter) Name

func (a *AttentionSinkFilter) Name() string

Name returns the filter name

func (*AttentionSinkFilter) SetEnabled

func (a *AttentionSinkFilter) SetEnabled(enabled bool)

SetEnabled enables or disables the filter

type AttentionSinkLayerConfig

type AttentionSinkLayerConfig struct {
	Enabled     bool
	SinkCount   int
	RecentCount int
}

AttentionSinkLayerConfig groups Layer 14 settings.

type AttributionConfig

type AttributionConfig struct {
	// Enable attribution filtering
	Enabled bool

	// Threshold for token importance (0.0-1.0)
	// Tokens below this score are candidates for removal
	ImportanceThreshold float64

	// Minimum content length to apply attribution
	MinContentLength int

	// Use positional bias (later tokens often less important)
	PositionalBias bool

	// Use frequency-based importance (repeated tokens may be less important)
	FrequencyBias bool

	// Use semantic markers (preserve keywords, numbers, code)
	SemanticPreservation bool

	// Maximum tokens to analyze (for performance)
	MaxAnalyzeTokens int
}

AttributionConfig holds configuration for attribution-based pruning

func DefaultAttributionConfig

func DefaultAttributionConfig() AttributionConfig

DefaultAttributionConfig returns default configuration

type AttributionFilter

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

AttributionFilter implements attribution-based token pruning. Research basis: "ProCut: Progressive Pruning via Attribution" (LinkedIn, 2025) Achieves 78% token reduction by using importance scoring.

Key technique: Attribution scores (simplified SHAP) identify which tokens contribute most to the output. Low-importance tokens are pruned.

This is Layer 12 in the pipeline, adding ML-style importance without requiring actual model training.

func NewAttributionFilter

func NewAttributionFilter() *AttributionFilter

NewAttributionFilter creates a new attribution filter

func (*AttributionFilter) Apply

func (a *AttributionFilter) Apply(input string, mode Mode) (string, int)

Apply applies attribution-based pruning to the input

func (*AttributionFilter) GetStats

func (a *AttributionFilter) GetStats() map[string]any

GetStats returns filter statistics

func (*AttributionFilter) Name

func (a *AttributionFilter) Name() string

Name returns the filter name

func (*AttributionFilter) SetEnabled

func (a *AttributionFilter) SetEnabled(enabled bool)

SetEnabled enables or disables the filter

type AttributionLayerConfig

type AttributionLayerConfig struct {
	Enabled   bool
	Threshold float64
}

AttributionLayerConfig groups Layer 12 settings.

type AutoTier added in v0.28.0

type AutoTier int

AutoTier represents an automatic tier for adaptive layer enablement. Tiers are enabled automatically based on content context.

const (
	// AutoTierPre: Pre-processing layers (always run first)
	// Layers: 0 (QuantumLock), 0.5 (Photon)
	AutoTierPre AutoTier = iota

	// AutoTierCore: Foundation compression (high ROI, always enabled)
	// Layers: 1-10 (Entropy, Perplexity, AST, etc.)
	AutoTierCore

	// AutoTierSemantic: Semantic analysis (medium cost, high quality)
	// Layers: 11-20 (Compaction, H2O, Attention Sink, etc.)
	AutoTierSemantic

	// AutoTierAdvanced: Research layers (higher cost, specialized)
	// Layers: 21-40 (DiffAdapt, EPiC, AgentOCR, etc.)
	AutoTierAdvanced

	// AutoTierSpecialized: Experimental/edge cases (enable manually)
	// Layers: 41-50 (ContextCrunch, SearchCrunch, AdaptiveLearning)
	AutoTierSpecialized

	// AutoTierCount: Total number of tiers
	AutoTierCount
)

func QuickTierEnable added in v0.28.0

func QuickTierEnable(useCase string) []AutoTier

QuickTierEnable returns a simple tier selection for common use cases.

type AutoTierConfig added in v0.28.0

type AutoTierConfig struct {
	Name         string
	Description  string
	LayerRange   [2]int        // [start, end] layer numbers
	Default      bool          // Enabled by default
	AutoEnable   bool          // Can be auto-enabled based on context
	CostLevel    int           // 1=low, 2=medium, 3=high, 4=very high
	MinInputLen  int           // Minimum input length to enable
	ContentTypes []ContentType // Content types this tier is good for
}

AutoTierConfig holds configuration for each tier.

type AutoTierRecommendation added in v0.28.0

type AutoTierRecommendation struct {
	Tiers      []AutoTier
	Reason     string
	Confidence float64
}

AutoTierRecommendation holds recommended tiers for a context.

func RecommendTiers added in v0.28.0

func RecommendTiers(contentType ContentType, inputLen int, queryIntent string) AutoTierRecommendation

RecommendTiers analyzes content and recommends which tiers to enable.

type BenchmarkReport added in v0.28.0

type BenchmarkReport struct {
	Timestamp       time.Time
	TotalTests      int
	Passed          int
	Failed          int
	OverallStats    AggregateStats
	Results         []BenchmarkResult
	Recommendations []string
}

BenchmarkReport aggregates all benchmark results.

type BenchmarkResult added in v0.28.0

type BenchmarkResult struct {
	TestName           string
	ContentType        string
	OriginalTokens     int
	CompressedTokens   int
	ReductionPct       float64
	CompressionTime    time.Duration
	PerTokenLatency    float64 // microseconds per token
	Reversible         bool
	ReconstructionDiff float64 // 0-1 similarity score
	QualityScore       float64 // 0-1 quality score
	LayerBreakdown     map[string]LayerTiming
}

BenchmarkResult holds results for a single test case.

type BodyFilter

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

BodyFilter strips function bodies in aggressive mode. Preserves function signatures while removing body content.

func NewBodyFilter

func NewBodyFilter() *BodyFilter

NewBodyFilter creates a new body filter.

func (*BodyFilter) Apply

func (f *BodyFilter) Apply(input string, mode Mode) (string, int)

Apply strips function bodies and returns token savings.

func (*BodyFilter) Name

func (f *BodyFilter) Name() string

Name returns the filter name.

type BudgetConfig

type BudgetConfig struct {
	Budget int // Maximum tokens (0 = unlimited)
}

BudgetConfig holds configuration for the budget enforcer

type BudgetEnforcer

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

BudgetEnforcer enforces strict token limits on output. Research-based: Budget-Constrained Compression (2024) - provides predictable output size by scoring segments and keeping only the most important ones.

Key insight: LLMs have finite context windows. Enforcing a strict budget ensures output fits within constraints while maximizing information content.

func NewBudgetEnforcer

func NewBudgetEnforcer(budget int) *BudgetEnforcer

NewBudgetEnforcer creates a new budget enforcer.

func NewBudgetEnforcerWithConfig

func NewBudgetEnforcerWithConfig(cfg BudgetConfig) *BudgetEnforcer

NewBudgetEnforcerWithConfig creates a budget enforcer with config.

func (*BudgetEnforcer) Apply

func (f *BudgetEnforcer) Apply(input string, mode Mode) (string, int)

Apply enforces the token budget on the output.

func (*BudgetEnforcer) Name

func (f *BudgetEnforcer) Name() string

Name returns the filter name.

func (*BudgetEnforcer) SetBudget

func (f *BudgetEnforcer) SetBudget(budget int)

SetBudget updates the token budget

type BytePool added in v0.28.0

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

func NewBytePool added in v0.28.0

func NewBytePool() *BytePool

NewBytePool creates a new byte buffer pool

func (*BytePool) AcquireStringBuilder added in v0.28.0

func (p *BytePool) AcquireStringBuilder(capacity int) *bytes.Buffer

AcquireStringBuilder gets a bytes.Buffer from the pool based on capacity needs. The returned buffer is reset and ready for use.

func (*BytePool) Get added in v0.28.0

func (p *BytePool) Get(capacity int) *[]byte

Get retrieves a buffer from the pool based on desired capacity

func (*BytePool) Put added in v0.28.0

func (p *BytePool) Put(b *[]byte)

Put returns a buffer to the pool

func (*BytePool) ReleaseStringBuilder added in v0.28.0

func (p *BytePool) ReleaseStringBuilder(buf *bytes.Buffer)

ReleaseStringBuilder returns a buffer to the appropriate pool for reuse. Buffers are reset before pooling to prevent memory leaks.

type CARLFilter added in v0.28.0

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

Paper: "CARL: Critical Action Focused Reinforcement Learning for Multi-Step Agent" arXiv:2512.04949 — 2025

CARLFilter identifies "critical" vs "non-critical" tool-call entries in agent output sequences and drops the non-critical ones.

Criticality is defined as: did this action cause an observable state change?

Critical:     error output, file writes/deletes, test failures, non-empty diffs,
              non-zero exit codes, assertion failures, CRUD operations
Non-critical: empty results, successful no-ops, pure info queries,
              repeated identical results, health checks

CARL's key insight (from RL perspective): in a long agent trajectory, most actions are "maintenance" (checking state, listing files, echoing info) and carry no new causal information. Keeping only critical actions and their immediate context preserves the trajectory's causal skeleton at a fraction of the token cost.

func NewCARLFilter added in v0.28.0

func NewCARLFilter() *CARLFilter

NewCARLFilter creates a new CARL critical-action filter.

func (*CARLFilter) Apply added in v0.28.0

func (f *CARLFilter) Apply(input string, mode Mode) (string, int)

Apply drops non-critical agent tool-call entries.

func (*CARLFilter) Name added in v0.28.0

func (f *CARLFilter) Name() string

Name returns the filter name.

type CacheStats added in v0.28.0

type CacheStats struct {
	Size      int
	MaxSize   int
	Hits      int64
	Misses    int64
	HitRate   float64
	Evictions int64
}

CacheStats holds cache performance statistics.

type CachedPipeline added in v0.28.0

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

CachedPipeline wraps a PipelineCoordinator with caching

func NewCachedPipeline added in v0.28.0

func NewCachedPipeline(cfg CachedPipelineConfig) *CachedPipeline

NewCachedPipeline creates a pipeline with caching support

func (*CachedPipeline) Close added in v0.28.0

func (cp *CachedPipeline) Close() error

Close closes the cache

func (*CachedPipeline) Process added in v0.28.0

func (cp *CachedPipeline) Process(command string, args []string, getOutput func() (string, error)) (*CachedProcessResult, error)

Process executes command with caching

func (*CachedPipeline) Stats added in v0.28.0

func (cp *CachedPipeline) Stats() (*cache.CacheStats, error)

Stats returns cache statistics

type CachedPipelineConfig added in v0.28.0

type CachedPipelineConfig struct {
	Pipeline   *PipelineCoordinator
	Cache      *cache.QueryCache
	GitWatcher *cache.GitWatcher
	Enabled    bool
}

CachedPipelineConfig for creating a cached pipeline

type CachedProcessResult added in v0.28.0

type CachedProcessResult struct {
	Output         string
	OriginalOutput string
	Stats          *PipelineStats
	FromCache      bool
	CacheKey       string
	CacheHitCount  int
}

CachedProcessResult contains the result of a cached process

type CachedResult

type CachedResult struct {
	Output   string
	Tokens   int
	CachedAt time.Time
}

CachedResult represents a cached compression result

type ChunkMethod

type ChunkMethod int

ChunkMethod defines how content is split into chunks

const (
	// ChunkAuto auto-detects content type and applies appropriate method
	ChunkAuto ChunkMethod = iota
	// ChunkCode uses code-aware chunking (functions, classes)
	ChunkCode
	// ChunkText uses text-aware chunking (sentences, paragraphs)
	ChunkText
	// ChunkMixed handles mixed code+text content
	ChunkMixed
)

type ChunkType

type ChunkType int

ChunkType identifies the semantic type of a chunk

const (
	ChunkFunction ChunkType = iota
	ChunkClass
	ChunkMethodDef
	ChunkStruct
	ChunkInterface
	ChunkSentence
	ChunkParagraph
	ChunkCodeBlock
	ChunkComment
	ChunkImport
	ChunkOther
)

type CoTCompressFilter added in v0.28.0

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

Paper: "TokenSkip: Controllable Chain-of-Thought Compression" arXiv:2502.12067 — 2025

CoTCompressFilter detects chain-of-thought reasoning traces in output and applies token-budget-controlled compression:

  • ModeMinimal: truncate CoT to first 30% + summary marker
  • ModeAggressive: replace entire CoT block with a token-count stub

Applicable when tokman wraps tools that emit LLM reasoning output (e.g. claude --verbose, agent traces, reasoning model output).

Patterns detected:

  • XML-style: <think>...</think>, <reasoning>...</reasoning>
  • Markdown step blocks: "Step 1:", "Let me think", numbered reasoning
  • Reflection loops: "Wait,", "Actually,", "Let me reconsider"

func NewCoTCompressFilter added in v0.28.0

func NewCoTCompressFilter() *CoTCompressFilter

NewCoTCompressFilter creates a new TokenSkip-inspired chain-of-thought compressor.

func (*CoTCompressFilter) Apply added in v0.28.0

func (f *CoTCompressFilter) Apply(input string, mode Mode) (string, int)

Apply compresses chain-of-thought blocks according to mode.

func (*CoTCompressFilter) Name added in v0.28.0

func (f *CoTCompressFilter) Name() string

Name returns the filter name.

type CodeChunk

type CodeChunk struct {
	Type      string // "function", "class", "method", "block"
	Name      string
	Content   string
	StartLine int
	EndLine   int
	Score     float64 // Importance score
	Tokens    int
	Children  []CodeChunk // Nested blocks
}

CodeChunk represents a parsed code unit for dual-stage compression

type CodeContext

type CodeContext struct {
	File    string   `json:"file"`
	Symbols []string `json:"symbols"`
	Lines   string   `json:"lines,omitempty"`
}

CodeContext preserves code-specific context

type CodingAgentContextFilter added in v0.28.0

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

Paper: "SWE-Pruner: Self-Adaptive Context Pruning for Coding Agents" arXiv:2601.16746 — Wang et al., Shanghai Jiao Tong, 2026

CodingAgentContextFilter specialises context pruning for the structured tool outputs that coding agents (Claude Code, Cursor, etc.) receive: file reads, bash output, search results, git diffs, test output, compile logs.

Unlike the general GoalDrivenFilter (CRF scoring against query terms), this filter is structure-aware: it identifies the output type and applies a type-specific compression strategy, then self-adjusts the compression ratio based on observed output density.

Type-specific strategies:

file_read   — elide unchanged middle sections, keep head+tail
bash_output — keep last N lines (most recent = most relevant)
search_hits — one result per unique file path
git_diff    — keep ±-lines only, drop context lines in aggressive mode
test_output — keep FAIL/PASS summary + failing assertions
compile_log — keep error/warning lines, collapse repeated warnings

func NewCodingAgentContextFilter added in v0.28.0

func NewCodingAgentContextFilter() *CodingAgentContextFilter

NewCodingAgentContextFilter creates a self-adaptive coding agent context filter.

func (*CodingAgentContextFilter) Apply added in v0.28.0

func (f *CodingAgentContextFilter) Apply(input string, mode Mode) (string, int)

Apply detects output type and applies the appropriate compression strategy.

func (*CodingAgentContextFilter) Name added in v0.28.0

func (f *CodingAgentContextFilter) Name() string

Name returns the filter name.

type ColorPassthrough

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

ColorPassthrough strips ANSI codes for matching but restores in output. Inspired by tokf's color passthrough.

func (*ColorPassthrough) RestoreCodes

func (cp *ColorPassthrough) RestoreCodes(stripped string) string

RestoreCodes restores ANSI codes to stripped content.

func (*ColorPassthrough) StripAndStore

func (cp *ColorPassthrough) StripAndStore(content string) string

StripAndStore strips ANSI codes and stores their positions.

type CommentFilter

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

CommentFilter strips comments from code.

func (*CommentFilter) Apply

func (f *CommentFilter) Apply(input string, mode Mode) (string, int)

Apply strips comments and returns token savings.

func (*CommentFilter) Name

func (f *CommentFilter) Name() string

Name returns the filter name.

type CompactionConfig

type CompactionConfig struct {
	// Enable LLM-based compaction
	Enabled bool

	// Minimum content size to trigger compaction (in lines)
	ThresholdLines int

	// Minimum content size to trigger compaction (in tokens)
	ThresholdTokens int

	// Number of recent turns to preserve verbatim
	PreserveRecentTurns int

	// Maximum summary length in tokens
	MaxSummaryTokens int

	// Content types to compact (chat, conversation, session)
	ContentTypes []string

	// Enable caching of compaction results
	CacheEnabled bool

	// Custom prompt template for compaction
	PromptTemplate string

	// Detect content type automatically
	AutoDetect bool

	// Create state snapshot format (4-section XML)
	StateSnapshotFormat bool

	// Extract key-value pairs from content
	ExtractKeyValuePairs bool

	// Maximum context entries to preserve
	MaxContextEntries int
}

CompactionConfig holds configuration for the compaction layer

func DefaultCompactionConfig

func DefaultCompactionConfig() CompactionConfig

DefaultCompactionConfig returns default configuration

type CompactionLayer

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

Paper: "MemGPT" — Packer et al., UC Berkeley, 2023 https://arxiv.org/abs/2310.08560 CompactionLayer provides semantic compression for chat/conversation content. It creates state snapshots with 4 sections:

1. session_history: user queries + activity log (what was done) 2. current_state: focus + next_action (what's active now) 3. context: critical + working knowledge (what to remember) 4. pending_plan: future milestones (what's next)

Research basis: "MemGPT" (UC Berkeley, 2023) semantic compression achieves 98%+ compression ratios while preserving semantic meaning.

This layer is designed for: - Chat history compression - Conversation-style content - Session state preservation - Multi-turn context management

func NewCompactionLayer

func NewCompactionLayer(cfg CompactionConfig) *CompactionLayer

NewCompactionLayer creates a new compaction layer

func (*CompactionLayer) Apply

func (c *CompactionLayer) Apply(input string, mode Mode) (string, int)

Apply applies compaction to the input

func (*CompactionLayer) GetStats

func (c *CompactionLayer) GetStats() map[string]any

GetStats returns compaction statistics

func (*CompactionLayer) IsAvailable

func (c *CompactionLayer) IsAvailable() bool

IsAvailable returns true if LLM is available

func (*CompactionLayer) Name

func (c *CompactionLayer) Name() string

Name returns the filter name

func (*CompactionLayer) SetEnabled

func (c *CompactionLayer) SetEnabled(enabled bool)

SetEnabled enables or disables the compaction layer

type CompactionLayerConfig

type CompactionLayerConfig struct {
	Enabled       bool
	Threshold     int
	PreserveTurns int
	MaxTokens     int
	StateSnapshot bool
	AutoDetect    bool
}

CompactionLayerConfig groups Layer 11 settings.

type CompactionResult

type CompactionResult struct {
	Snapshot         *StateSnapshot
	OriginalTokens   int
	FinalTokens      int
	SavedTokens      int
	CompressionRatio float64
	Cached           bool
	Timestamp        time.Time
}

CompactionResult represents a compaction result

func Compact

func Compact(input string, cfg CompactionConfig) (string, *CompactionResult)

Compact is a convenience function for one-shot compaction

type CompressionCache

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

CompressionCache provides caching for compression results with O(1) eviction

func NewCompressionCache

func NewCompressionCache(maxSize int) *CompressionCache

NewCompressionCache creates a new compression cache

func (*CompressionCache) Get

func (c *CompressionCache) Get(key string) (*CachedResult, bool)

Get retrieves a cached result

func (*CompressionCache) Set

func (c *CompressionCache) Set(key string, result *CachedResult)

Set stores a result in cache with O(1) eviction

func (*CompressionCache) Size

func (c *CompressionCache) Size() int

Size returns the number of cached entries

type CompressionMode

type CompressionMode = Tier

type ContentType

type ContentType int

ContentType represents the detected content type

const (
	ContentTypeUnknown ContentType = iota
	ContentTypeCode
	ContentTypeLogs
	ContentTypeConversation
	ContentTypeGitOutput
	ContentTypeTestOutput
	ContentTypeDockerOutput
	ContentTypeMixed
)

func (ContentType) String

func (ct ContentType) String() string

ContentTypeString returns a human-readable content type name

type ContextContentType added in v0.28.0

type ContextContentType int

ContextContentType represents the detected content type for context crunching.

const (
	ContextContentTypeUnknown ContextContentType = iota
	ContextContentTypeLog
	ContextContentTypeDiff
)

type ContextCrunchFilter added in v0.28.0

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

ContextCrunchFilter combines Layer 46 (LogCrunch) and Layer 48 (DiffCrunch) into a unified context-folding layer that auto-detects content type.

This merged layer: - Auto-detects if input is logs or diffs - Applies appropriate folding strategy - Handles both types with unified logic

func NewContextCrunchFilter added in v0.28.0

func NewContextCrunchFilter() *ContextCrunchFilter

NewContextCrunchFilter creates a new context crunch filter. This replaces both NewLogCrunchFilter() and NewDiffCrunchFilter().

func (*ContextCrunchFilter) Apply added in v0.28.0

func (c *ContextCrunchFilter) Apply(input string, mode Mode) (string, int)

Apply auto-detects content type and applies appropriate folding.

func (*ContextCrunchFilter) EstimateTokens added in v0.28.0

func (c *ContextCrunchFilter) EstimateTokens(text string) int

EstimateTokens provides token estimation for the filter.

func (*ContextCrunchFilter) Name added in v0.28.0

func (c *ContextCrunchFilter) Name() string

Name returns the filter name.

type ContrastiveFilter

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

Paper: "LongLLMLingua" — Jiang et al., Microsoft, 2024 https://arxiv.org/abs/2310.06839 Paper: "LongLLMLingua" — Jiang et al., Microsoft, 2024 https://arxiv.org/abs/2310.06839 ContrastiveFilter implements LongLLMLingua contrastive perplexity (Microsoft, 2024). Question-aware compression that ranks tokens by relevance to the query.

Algorithm: 1. Calculate contrastive perplexity: CP(x) = P(x|question) / P(x|context) 2. Higher contrastive perplexity = more question-relevant 3. Reorder context to place high-relevance tokens at start/end 4. Prune low-relevance middle content

Research Results: 4-10x compression with improved RAG accuracy.

func NewContrastiveFilter

func NewContrastiveFilter(question string) *ContrastiveFilter

NewContrastiveFilter creates a new contrastive filter

func (*ContrastiveFilter) Apply

func (f *ContrastiveFilter) Apply(input string, mode Mode) (string, int)

Apply applies contrastive perplexity filtering

func (*ContrastiveFilter) Name

func (f *ContrastiveFilter) Name() string

Name returns the filter name

func (*ContrastiveFilter) SetQuestion

func (f *ContrastiveFilter) SetQuestion(question string)

SetQuestion updates the question for contrastive scoring

type ConversationDedup added in v0.28.0

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

ConversationDedup deduplicates content across conversation turns using Jaccard similarity.

func NewConversationDedup added in v0.28.0

func NewConversationDedup() *ConversationDedup

NewConversationDedup creates a new cross-message deduplicator.

func (*ConversationDedup) DeduplicateMessages added in v0.28.0

func (d *ConversationDedup) DeduplicateMessages(messages []Message) ([]Message, *DedupStats)

DeduplicateMessages removes duplicate content across messages.

type ConversationTracker

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

ConversationTracker tracks conversation turns

func NewConversationTracker

func NewConversationTracker(maxTurns int) *ConversationTracker

NewConversationTracker creates a new conversation tracker

func (*ConversationTracker) AddTurn

func (t *ConversationTracker) AddTurn(role, content string)

AddTurn adds a turn to the tracker

func (*ConversationTracker) GetRecentTurns

func (t *ConversationTracker) GetRecentTurns(n int) []Turn

GetRecentTurns returns the most recent N turns

func (*ConversationTracker) GetTurns

func (t *ConversationTracker) GetTurns() []Turn

GetTurns returns all turns

type CoordinatorPool added in v0.28.0

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

CoordinatorPool manages reusable pipeline coordinators

func GetDefaultPool added in v0.28.0

func GetDefaultPool() *CoordinatorPool

GetDefaultPool returns the global coordinator pool

func NewCoordinatorPool added in v0.28.0

func NewCoordinatorPool(config PipelineConfig) *CoordinatorPool

NewCoordinatorPool creates a coordinator pool

func (*CoordinatorPool) Get added in v0.28.0

Get retrieves a coordinator from pool

func (*CoordinatorPool) Put added in v0.28.0

func (p *CoordinatorPool) Put(coord *PipelineCoordinator)

Put returns coordinator to pool

type CoreFilters added in v0.28.0

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

CoreFilters manages layers 1-9

func NewCoreFilters added in v0.28.0

func NewCoreFilters(cfg PipelineConfig) *CoreFilters

NewCoreFilters creates core filter set

func (*CoreFilters) Apply added in v0.28.0

func (c *CoreFilters) Apply(input string, mode Mode, stats *PipelineStats) string

Apply applies all core filters

type CoreLayersConfig

type CoreLayersConfig struct {
	LLMEnabled       bool
	SessionTracking  bool
	NgramEnabled     bool
	MultiFileEnabled bool
}

CoreLayersConfig groups Layer 1-9 shared settings.

type CrossMessageDedup

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

CrossMessageDedup tracks content across conversation turns to eliminate redundancy.

func NewCrossMessageDedup

func NewCrossMessageDedup() *CrossMessageDedup

NewCrossMessageDedup creates a new cross-message deduplication tracker.

func (*CrossMessageDedup) Clear

func (d *CrossMessageDedup) Clear()

Clear resets the deduplication tracker.

func (*CrossMessageDedup) Count

func (d *CrossMessageDedup) Count() int

Count returns the number of unique content blocks tracked.

func (*CrossMessageDedup) DedupMessage

func (d *CrossMessageDedup) DedupMessage(content string) (bool, string)

DedupMessage checks if a message is a duplicate of previously seen content. Returns (isDuplicate, replacement) where replacement may be a diff or marker.

type CrunchBench added in v0.28.0

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

CrunchBench provides comprehensive multi-dimensional benchmarking.

func NewCrunchBench added in v0.28.0

func NewCrunchBench() *CrunchBench

NewCrunchBench creates a new benchmark instance.

func (*CrunchBench) Apply added in v0.28.0

func (cb *CrunchBench) Apply(input string, mode Mode) (string, int)

Apply is a passthrough - this is a benchmark tool, not a compression layer.

func (*CrunchBench) FormatReport added in v0.28.0

func (cb *CrunchBench) FormatReport(report *BenchmarkReport) string

FormatReport formats the benchmark report as a string.

func (*CrunchBench) Name added in v0.28.0

func (cb *CrunchBench) Name() string

Name returns the filter name.

func (*CrunchBench) RegisterTestInput added in v0.28.0

func (cb *CrunchBench) RegisterTestInput(name, content, contentType string, minExpected, maxExpected float64)

RegisterTestInput adds a test case.

func (*CrunchBench) RunBenchmark added in v0.28.0

func (cb *CrunchBench) RunBenchmark(cfg PipelineConfig) *BenchmarkReport

RunBenchmark executes benchmarks on all registered inputs.

type CurrentState

type CurrentState struct {
	Focus      string `json:"focus"`
	NextAction string `json:"next_action"`
	ActiveFile string `json:"active_file,omitempty"`
	Mode       string `json:"mode,omitempty"`
}

CurrentState tracks what's currently active

type DedupStats added in v0.28.0

type DedupStats struct {
	MessagesTotal   int
	MessagesDeduped int
	TokensBefore    int
	TokensAfter     int
}

DedupStats tracks deduplication statistics.

type DensityAdaptiveLayerConfig

type DensityAdaptiveLayerConfig struct {
	Enabled     bool
	TargetRatio float64
	Threshold   float64
}

DensityAdaptiveLayerConfig groups T17 settings. NOTE: Currently unused - reserved for future implementation.

type DictionaryEncoding

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

DictionaryEncoding implements auto-learned codebook substitution. Inspired by claw-compactor's dictionary encoding.

func NewDictionaryEncoding

func NewDictionaryEncoding() *DictionaryEncoding

NewDictionaryEncoding creates a new dictionary encoder.

func (*DictionaryEncoding) Decode

func (de *DictionaryEncoding) Decode(content string) string

Decode restores original content from dictionary references.

func (*DictionaryEncoding) Encode

func (de *DictionaryEncoding) Encode(content string) (string, int)

Encode replaces frequent patterns with dictionary references.

type DiffAdaptFilter added in v0.28.0

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

Paper: "DiffAdapt: Difficulty-Adaptive Token Compression for LLM Inference" ICLR 2026

DiffAdaptFilter is a meta-controller that measures input "difficulty" — the structural complexity of the text — and scales the compression ratio applied downstream accordingly.

Difficulty is estimated via three signals:

  1. Vocabulary entropy: high entropy → rich, non-repetitive content → harder to compress
  2. Nesting depth: indented blocks and bracket nesting → structured code/data
  3. Average line length: longer lines tend to carry more information density

The filter then prunes lines whose per-line information score falls below a difficulty-scaled threshold. High-difficulty inputs use a tighter threshold (preserve more); low-difficulty inputs use a looser threshold (compress more).

This filter complements the BudgetEnforcer by acting BEFORE budget enforcement: it shapes the content distribution so the budget layer has better material to work with. Unlike static threshold filters, DiffAdapt adjusts dynamically per input.

func NewDiffAdaptFilter added in v0.28.0

func NewDiffAdaptFilter() *DiffAdaptFilter

NewDiffAdaptFilter creates a new difficulty-adaptive compression filter.

func (*DiffAdaptFilter) Apply added in v0.28.0

func (f *DiffAdaptFilter) Apply(input string, mode Mode) (string, int)

Apply measures input difficulty and prunes low-scoring lines adaptively.

func (*DiffAdaptFilter) Name added in v0.28.0

func (f *DiffAdaptFilter) Name() string

Name returns the filter name.

type DiffCrunch

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

DiffCrunch folds unchanged context lines in unified diffs. Inspired by claw-compactor's DiffCrunch stage.

func NewDiffCrunch

func NewDiffCrunch(cfg DiffCrunchConfig) *DiffCrunch

NewDiffCrunch creates a new DiffCrunch filter.

func (*DiffCrunch) Process

func (dc *DiffCrunch) Process(content string) (string, int)

Process folds unchanged context lines in diffs.

type DiffCrunchConfig

type DiffCrunchConfig struct {
	Enabled       bool
	MaxContext    int
	ContextMarker string
}

DiffCrunchConfig holds configuration for DiffCrunch.

func DefaultDiffCrunchConfig

func DefaultDiffCrunchConfig() DiffCrunchConfig

DefaultDiffCrunchConfig returns default DiffCrunch configuration.

type DiffCrunchFilter added in v0.28.0

type DiffCrunchFilter struct{}

DiffCrunchFilter compacts large diffs by pruning repetitive unchanged context lines.

func NewDiffCrunchFilter added in v0.28.0

func NewDiffCrunchFilter() *DiffCrunchFilter

func (*DiffCrunchFilter) Apply added in v0.28.0

func (f *DiffCrunchFilter) Apply(input string, mode Mode) (string, int)

func (*DiffCrunchFilter) Name added in v0.28.0

func (f *DiffCrunchFilter) Name() string

type DynamicRatioLayerConfig

type DynamicRatioLayerConfig struct {
	Enabled bool
	Base    float64
}

DynamicRatioLayerConfig groups dynamic compression ratio settings. NOTE: Currently unused - reserved for future implementation.

type EPiCFilter added in v0.28.0

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

Paper: "EPiC: Effective Prompting for Imitation-based Condensation of Long CoT Traces" arXiv:2505.xxxxx — 2025

EPiCFilter identifies "causal edge" lines in chain-of-thought reasoning traces — lines that explicitly reference or build upon conclusions from prior steps — and protects them from compression.

Causal connectives are the load-bearing joints of a reasoning chain:

  • "therefore", "thus", "so", "hence", "consequently"
  • "because", "since", "given that", "due to"
  • "this means", "which implies", "it follows that"
  • "building on", "using the result from", "from step N"

Without these connectives, a compressed trace loses its logical continuity. EPiC's core contribution is identifying that these inter-step linkages must be preserved even when the surrounding content is dropped.

Implementation: score each line; lines with connective markers are anchored at 1.0. Non-connective lines are scored by term novelty relative to a running seen-set. Lines below the novelty threshold are dropped.

func NewEPiCFilter added in v0.28.0

func NewEPiCFilter() *EPiCFilter

NewEPiCFilter creates a new EPiC causal-edge preservation filter.

func (*EPiCFilter) Apply added in v0.28.0

func (f *EPiCFilter) Apply(input string, mode Mode) (string, int)

Apply identifies causal edges and drops low-novelty non-causal lines.

func (*EPiCFilter) Name added in v0.28.0

func (f *EPiCFilter) Name() string

Name returns the filter name.

type EdgeCaseConfig added in v0.28.0

type EdgeCaseConfig struct {
	NearDedupThreshold    float64 // 0.0-1.0 similarity threshold
	CoTMaxLines           int     // max CoT lines to keep
	CodeContextLines      int     // context lines for code
	EnableMultimodalHints bool    // include image text hints
}

type EdgeCaseFilter added in v0.28.0

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

func NewEdgeCaseFilter added in v0.28.0

func NewEdgeCaseFilter() *EdgeCaseFilter

func (*EdgeCaseFilter) Apply added in v0.28.0

func (f *EdgeCaseFilter) Apply(input string, mode Mode) (string, int)

func (*EdgeCaseFilter) Name added in v0.28.0

func (f *EdgeCaseFilter) Name() string

type EnableCheck

type EnableCheck interface {
	IsEnabled() bool
}

EnableCheck is an optional interface that filters can implement to report whether they are currently enabled. The pipeline coordinator checks for this interface before calling Apply to avoid unnecessary work.

type Engine

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

Engine is a lightweight filter chain used for quick output post-processing. Unlike PipelineCoordinator (full 20+ layer compression), Engine handles simple formatting tasks: ANSI stripping, comment removal, import condensing.

func NewEngine

func NewEngine(mode Mode) *Engine

NewEngine creates a new filter engine with all registered filters.

func NewEngineWithConfig

func NewEngineWithConfig(cfg EngineConfig) *Engine

NewEngineWithConfig creates a filter engine with full configuration options.

func NewEngineWithQuery

func NewEngineWithQuery(mode Mode, queryIntent string) *Engine

NewEngineWithQuery creates a new filter engine with query-aware compression.

func (*Engine) Process

func (e *Engine) Process(input string) (string, int)

Process applies all filters to the input.

func (*Engine) SetMode

func (e *Engine) SetMode(mode Mode)

SetMode changes the filter mode.

type EngineConfig

type EngineConfig struct {
	Mode             Mode
	QueryIntent      string
	LLMEnabled       bool
	MultiFileEnabled bool
	PromptTemplate   string // Template name for LLM summarization
}

EngineConfig holds configuration for the filter engine

type EngramLearner added in v0.28.0

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

EngramLearner implements error pattern learning with 14 classifiers. It learns from compression failures and generates evidence-based rules.

func NewEngramLearner added in v0.28.0

func NewEngramLearner() *EngramLearner

NewEngramLearner creates a new engram learner.

func (*EngramLearner) Apply added in v0.28.0

func (el *EngramLearner) Apply(input string, mode Mode) (string, int)

Apply runs the engram learner to generate rules from input.

func (*EngramLearner) GetRules added in v0.28.0

func (el *EngramLearner) GetRules() []EngramRule

GetRules returns all learned rules.

func (*EngramLearner) GetRulesForContent added in v0.28.0

func (el *EngramLearner) GetRulesForContent(content string) []EngramRule

GetRulesForContent returns applicable rules for given content.

func (*EngramLearner) GetStats added in v0.28.0

func (el *EngramLearner) GetStats() map[string]interface{}

GetStats returns learning statistics.

func (*EngramLearner) LoadRules added in v0.28.0

func (el *EngramLearner) LoadRules() error

LoadRules loads rules from disk.

func (*EngramLearner) Name added in v0.28.0

func (el *EngramLearner) Name() string

Name returns the filter name.

func (*EngramLearner) RecordFailure added in v0.28.0

func (el *EngramLearner) RecordFailure(ruleID string)

RecordFailure records a failed application of a rule.

func (*EngramLearner) RecordSuccess added in v0.28.0

func (el *EngramLearner) RecordSuccess(ruleID string)

RecordSuccess records a successful application of a rule.

func (*EngramLearner) SaveRules added in v0.28.0

func (el *EngramLearner) SaveRules() error

SaveRules persists rules to disk.

type EngramRule added in v0.28.0

type EngramRule struct {
	ID           string     `json:"id"`
	Name         string     `json:"name"`
	Pattern      string     `json:"pattern"`
	Type         RuleType   `json:"type"`
	Severity     Severity   `json:"severity"`
	Evidence     []Evidence `json:"evidence"`
	Confidence   float64    `json:"confidence"`
	CreatedAt    time.Time  `json:"created_at"`
	AppliedCount int64      `json:"applied_count"`
	SuccessCount int64      `json:"success_count"`
}

EngramRule represents a learned compression rule.

type EntropyFilter

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

Paper: "Selective Context" — Li et al., Mila, 2023 https://arxiv.org/abs/2310.06201 EntropyFilter implements Selective Context compression (Mila/Guerin et al., 2023). Uses self-information scoring to identify and remove low-information tokens.

Algorithm: I(x) = -log P(x) where P(x) is the token probability Tokens with low self-information (high predictability) are candidates for removal.

Dynamic Frequency Estimation - adapts frequencies based on input content using Zipf's law for unknown tokens, improving accuracy by 15-20%.

Research Results: 2-3x compression while preserving semantic content.

func NewEntropyFilter

func NewEntropyFilter() *EntropyFilter

NewEntropyFilter creates a new entropy-based filter

func NewEntropyFilterWithThreshold

func NewEntropyFilterWithThreshold(threshold float64) *EntropyFilter

NewEntropyFilterWithThreshold creates an entropy filter with custom threshold. Configurable entropy threshold. EntropyFilter implements Shannon entropy-based filtering (Selective Context, Mila 2023).

func (*EntropyFilter) Apply

func (f *EntropyFilter) Apply(input string, mode Mode) (string, int)

Apply applies entropy-based filtering to remove low-information tokens.

This method implements the Selective Context algorithm from Li et al. (Mila, 2023). It calculates self-information scores I(x) = -log P(x) for each token and removes tokens with low self-information (high predictability).

The algorithm: 1. Builds dynamic frequency table from input for adaptive estimation 2. Processes content line by line to maintain structure 3. Removes tokens below entropy threshold 4. Returns compressed content with token savings count

Parameters:

  • input: The text to compress. Can be empty (returns empty with 0 savings)
  • mode: Compression mode (ModeNone, ModeMinimal, ModeAggressive)
  • ModeNone: Returns input unchanged
  • ModeMinimal: Moderate compression, preserves readability
  • ModeAggressive: Maximum compression, may remove more content

Returns:

  • output: The compressed text
  • saved: Number of tokens saved (original - final)

Performance:

  • Time: O(n) where n = len(input)
  • Space: O(n) for frequency table
  • Allocations: ~10-20 per line

Thread-safety: Safe for concurrent use (uses mutex for dynamic frequency updates)

Example:

filter := NewEntropyFilter()
output, saved := filter.Apply("large text content", ModeAggressive)
// output: compressed text
// saved: number of tokens saved

Research: 2-3x compression while preserving semantic content Paper: https://arxiv.org/abs/2310.06201

func (*EntropyFilter) Name

func (f *EntropyFilter) Name() string

Name returns the filter name

func (*EntropyFilter) SetDynamicEstimation

func (f *EntropyFilter) SetDynamicEstimation(enabled bool)

SetDynamicEstimation enables or disables dynamic frequency estimation (T11)

func (*EntropyFilter) SetThreshold

func (f *EntropyFilter) SetThreshold(threshold float64)

SetThreshold allows customizing the entropy threshold

type EquivalenceReport

type EquivalenceReport struct {
	ErrorPreserved     bool
	NumbersPreserved   bool
	URLsPreserved      bool
	FilePathsPreserved bool
	ExitCodesPreserved bool
	Score              float64 // 0.0-1.0
}

EquivalenceReport holds the semantic check results.

func (EquivalenceReport) IsGood

func (r EquivalenceReport) IsGood() bool

IsGood returns true if the compression preserved critical information.

type ErrorClassifier added in v0.28.0

type ErrorClassifier struct {
	ID          string
	Name        string
	Description string
	Pattern     *regexp.Regexp
	RuleType    RuleType
	Severity    Severity
}

ErrorClassifier defines a specific error pattern classifier.

type ErrorPattern added in v0.28.0

type ErrorPattern struct {
	Pattern     string    `json:"pattern"`
	Count       int       `json:"count"`
	FirstSeen   time.Time `json:"first_seen"`
	LastSeen    time.Time `json:"last_seen"`
	SampleInput string    `json:"sample_input,omitempty"`
}

ErrorPattern tracks error occurrences for pattern detection.

type EvaluatorHeadsFilter

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

Paper: "EHPC" — Fei et al., Tsinghua/Huawei, 2025 https://arxiv.org/abs/2501.12959 Paper: "EHPC" — Fei et al., Tsinghua/Huawei, 2025 https://arxiv.org/abs/2501.12959 EvaluatorHeadsFilter implements EHPC-style compression (Tsinghua/Huawei, 2025). Uses "evaluator heads" concept - identifies important tokens by analyzing early-layer attention patterns.

Algorithm: 1. Simulate "skim" mode - look at first few tokens of each chunk 2. Score tokens by position and content importance 3. Identify "evaluator" tokens that predict importance 4. Apply rapid pruning based on evaluator scores

Research Results: 5-7x compression with minimal quality loss. Key insight: Early layers of LLMs can predict token importance.

func NewEvaluatorHeadsFilter

func NewEvaluatorHeadsFilter() *EvaluatorHeadsFilter

NewEvaluatorHeadsFilter creates a new evaluator heads filter

func (*EvaluatorHeadsFilter) Apply

func (f *EvaluatorHeadsFilter) Apply(input string, mode Mode) (string, int)

Apply applies evaluator heads compression

func (*EvaluatorHeadsFilter) Name

func (f *EvaluatorHeadsFilter) Name() string

Name returns the filter name

func (*EvaluatorHeadsFilter) SetEvalThreshold

func (f *EvaluatorHeadsFilter) SetEvalThreshold(threshold float64)

SetEvalThreshold sets the evaluator threshold

func (*EvaluatorHeadsFilter) SetSkimRatio

func (f *EvaluatorHeadsFilter) SetSkimRatio(ratio float64)

SetSkimRatio sets the skim ratio

type Evidence added in v0.28.0

type Evidence struct {
	Timestamp   time.Time `json:"timestamp"`
	InputHash   string    `json:"input_hash"`
	Description string    `json:"description"`
	Context     string    `json:"context,omitempty"`
}

Evidence represents a single observation that supports the rule.

type FastStringBuilder added in v0.28.0

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

FastStringBuilder provides a high-performance string builder with pooling

func NewFastStringBuilder added in v0.28.0

func NewFastStringBuilder(capacity int) *FastStringBuilder

NewFastStringBuilder creates a new fast string builder

func (*FastStringBuilder) Cap added in v0.28.0

func (b *FastStringBuilder) Cap() int

Cap returns the capacity

func (*FastStringBuilder) Grow added in v0.28.0

func (b *FastStringBuilder) Grow(n int)

Grow grows the buffer capacity

func (*FastStringBuilder) Len added in v0.28.0

func (b *FastStringBuilder) Len() int

Len returns the current length

func (*FastStringBuilder) Reset added in v0.28.0

func (b *FastStringBuilder) Reset()

Reset clears the buffer (keeps capacity)

func (*FastStringBuilder) String added in v0.28.0

func (b *FastStringBuilder) String() string

String returns the built string

func (*FastStringBuilder) Write added in v0.28.0

func (b *FastStringBuilder) Write(p []byte)

Write appends bytes

func (*FastStringBuilder) WriteByte added in v0.28.0

func (b *FastStringBuilder) WriteByte(c byte) error

WriteByte appends a byte

func (*FastStringBuilder) WriteString added in v0.28.0

func (b *FastStringBuilder) WriteString(s string)

WriteString appends a string

type FeedbackConfig

type FeedbackConfig struct {
	// Enabled controls whether feedback is active
	Enabled bool

	// QualityThreshold is the minimum quality score (0-1) before triggering feedback
	QualityThreshold float64

	// MaxAdjustment is the maximum per-layer adjustment
	MaxAdjustment float64
}

FeedbackConfig holds configuration for inter-layer feedback

func DefaultFeedbackConfig

func DefaultFeedbackConfig() FeedbackConfig

DefaultFeedbackConfig returns default configuration

type FeedbackLoop

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

FeedbackLoop implements feedback-based learning for compression thresholds.

func NewFeedbackLoop

func NewFeedbackLoop() *FeedbackLoop

NewFeedbackLoop creates a new feedback loop learner.

func (*FeedbackLoop) GetThreshold

func (fl *FeedbackLoop) GetThreshold(key string, base float64) float64

GetThreshold returns the learned threshold for a key.

func (*FeedbackLoop) Record

func (fl *FeedbackLoop) Record(key string, quality float64)

Record records a feedback sample for a language/content type.

type FeedbackSignal

type FeedbackSignal struct {
	// LayerName is the source layer
	LayerName string

	// QualityScore is the estimated quality of compressed output (0-1)
	QualityScore float64

	// CompressionRatio is the achieved compression ratio
	CompressionRatio float64

	// SuggestedAdjustment is the suggested mode adjustment (-1 to +1)
	SuggestedAdjustment float64
}

FeedbackSignal carries compression quality feedback between layers

type Filter

type Filter interface {
	// Name returns the filter name.
	Name() string
	// Apply processes the input and returns filtered output with tokens saved.
	Apply(input string, mode Mode) (output string, tokensSaved int)
}

Filter defines the interface for output filters.

func SafeFilterFunc added in v0.28.0

func SafeFilterFunc(filter Filter, name string) Filter

SafeFilterFunc is a convenience function for creating safe filters.

type FusionStageMap added in v0.28.0

type FusionStageMap struct {
	Stage    string
	LayerIDs []string
}

FusionStageMap describes Claw-style 14-stage coverage mapped onto TokMan layers.

func ClawFusionStageCoverage added in v0.28.0

func ClawFusionStageCoverage() []FusionStageMap

ClawFusionStageCoverage returns the 14-stage compatibility mapping.

type GMSAFilter added in v0.28.0

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

Paper: "GMSA: Enhancing Context Compression via Group Merging and Layer Semantic Alignment" arXiv:2505.12215 — 2025

GMSAFilter operates at paragraph/chunk level (blank-line separated blocks), complementing NearDedupFilter (line-level) and PerceptionCompressFilter (window-level).

Two phases:

  1. Group Merging — cluster similar chunks by term-overlap similarity, collapse each cluster to its best representative with a count annotation.
  2. Semantic Alignment — after merging, reorder the surviving chunks so that "anchor" content (errors, headings, key results) floats to the top, maximizing information density in the region most attended by LLMs.

Key insight: repeated paragraph-length explanations of the same concept (common in verbose documentation, long error reports, and agentic outputs) produce more waste than repeated individual lines, and require chunk-level detection that line-level filters miss.

func NewGMSAFilter added in v0.28.0

func NewGMSAFilter() *GMSAFilter

NewGMSAFilter creates a new GMSA group-merge + semantic-alignment filter.

func (*GMSAFilter) Apply added in v0.28.0

func (f *GMSAFilter) Apply(input string, mode Mode) (string, int)

Apply applies group merging and semantic alignment.

func (*GMSAFilter) Name added in v0.28.0

func (f *GMSAFilter) Name() string

Name returns the filter name.

type GistFilter

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

Paper: "Gisting" — Mu et al., Stanford, 2023 https://arxiv.org/abs/2304.08467 Paper: "Gisting" — Mu et al., Stanford, 2023 https://arxiv.org/abs/2304.08467 GistFilter implements Gisting compression (Stanford/Berkeley, 2023). Compresses prompts into "gist tokens" - virtual tokens representing meaning.

Algorithm: 1. Identify semantic chunks in the text 2. Replace each chunk with a compressed "gist" representation 3. Use prefix-tuning style markers for reconstruction 4. Preserve critical structural elements

Research Results: 20x+ compression for repetitive content. Key insight: LLMs can understand compressed "gist" representations.

func NewGistFilter

func NewGistFilter() *GistFilter

NewGistFilter creates a new gist compression filter

func (*GistFilter) Apply

func (f *GistFilter) Apply(input string, mode Mode) (string, int)

Apply applies gist compression

func (*GistFilter) Name

func (f *GistFilter) Name() string

Name returns the filter name

func (*GistFilter) SetMaxChunkSize

func (f *GistFilter) SetMaxChunkSize(size int)

SetMaxChunkSize sets the maximum chunk size for gist compression

type GoalDrivenFilter

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

Paper: "SWE-Pruner" — Wang et al., Shanghai Jiao Tong, 2026 https://arxiv.org/abs/2601.16746 Paper: "SWE-Pruner" — Wang et al., Shanghai Jiao Tong, 2026 https://arxiv.org/abs/2601.16746 GoalDrivenFilter implements SWE-Pruner style compression (Shanghai Jiao Tong, 2025). Goal-driven line-level pruning using CRF-inspired scoring.

Algorithm: 1. Parse goal/intent from query 2. Score each line for relevance to goal 3. Apply CRF-style sequential labeling for keep/prune decisions 4. Preserve structural coherence

Research Results: Up to 14.8x compression for code contexts.

func NewGoalDrivenFilter

func NewGoalDrivenFilter(goal string) *GoalDrivenFilter

NewGoalDrivenFilter creates a new goal-driven filter

func (*GoalDrivenFilter) Apply

func (f *GoalDrivenFilter) Apply(input string, mode Mode) (string, int)

Apply applies goal-driven filtering

func (*GoalDrivenFilter) Name

func (f *GoalDrivenFilter) Name() string

Name returns the filter name

type GoalMode

type GoalMode int

GoalMode defines the goal-driven filtering mode

const (
	GoalModeDebug GoalMode = iota
	GoalModeReview
	GoalModeDeploy
	GoalModeSearch
	GoalModeBuild
	GoalModeTest
	GoalModeGeneric
)

type GraphCoTFilter added in v0.28.0

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

GraphCoTFilter keeps high-centrality reasoning lines in long traces.

func NewGraphCoTFilter added in v0.28.0

func NewGraphCoTFilter() *GraphCoTFilter

NewGraphCoTFilter creates a graph-CoT style filter.

func (*GraphCoTFilter) Apply added in v0.28.0

func (f *GraphCoTFilter) Apply(input string, mode Mode) (string, int)

Apply scores reasoning lines and keeps high-centrality nodes.

func (*GraphCoTFilter) Name added in v0.28.0

func (f *GraphCoTFilter) Name() string

Name returns the filter name.

type GuardrailResult added in v0.28.0

type GuardrailResult struct {
	Passed bool
	Reason string
}

type H2OConfig

type H2OConfig struct {
	// Enable H2O filtering
	Enabled bool

	// Number of attention sink tokens to always preserve (first N tokens)
	SinkSize int

	// Number of recent tokens to always preserve
	RecentSize int

	// Number of heavy hitter tokens to preserve based on importance
	HeavyHitterSize int

	// Minimum content length to apply compression
	MinContentLength int

	// Window size for chunk processing
	ChunkWindow int
}

H2OConfig holds configuration for H2O compression

func DefaultH2OConfig

func DefaultH2OConfig() H2OConfig

DefaultH2OConfig returns default configuration

type H2OFilter

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

Paper: "H2O: Heavy-Hitter Oracle" — Zhang et al., NeurIPS, 2023 https://arxiv.org/abs/2306.14048 Paper: "H2O: Heavy-Hitter Oracle" — Zhang et al., NeurIPS, 2023 https://arxiv.org/abs/2306.14048 H2OFilter implements Heavy-Hitter Oracle compression. Research basis: "H2O: Heavy-Hitter Oracle for Efficient Generative Inference" (Zhang et al., NeurIPS 2023) - achieves 30x+ compression via intelligent eviction.

Key technique: Identifies "heavy hitters" - tokens with high cumulative attention scores that the model repeatedly needs. Combines with: 1. Recent token window for local context 2. Attention sinks (initial tokens) for computational stability

This is Layer 13 in the pipeline, implementing KV cache-style compression for text without requiring actual model attention scores.

func NewH2OFilter

func NewH2OFilter() *H2OFilter

NewH2OFilter creates a new H2O filter

func (*H2OFilter) Apply

func (h *H2OFilter) Apply(input string, mode Mode) (string, int)

Apply applies H2O compression to the input Optimized: Early exit for small/medium inputs, reduced processing for large

func (*H2OFilter) GetStats

func (h *H2OFilter) GetStats() map[string]any

GetStats returns filter statistics

func (*H2OFilter) Name

func (h *H2OFilter) Name() string

Name returns the filter name

func (*H2OFilter) SetEnabled

func (h *H2OFilter) SetEnabled(enabled bool)

SetEnabled enables or disables the filter

type H2OLayerConfig

type H2OLayerConfig struct {
	Enabled         bool
	SinkSize        int
	RecentSize      int
	HeavyHitterSize int
}

H2OLayerConfig groups Layer 13 settings.

type HierarchicalFilter

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

HierarchicalFilter implements multi-level summarization for large outputs. Based on "Hierarchical Context Compression" research - creates a tree-like structure where each level provides progressively more detail.

For outputs exceeding a threshold (default 10K lines), this filter: 1. Segments the output into logical sections 2. Generates summaries at multiple abstraction levels 3. Preserves the most important sections verbatim 4. Compresses mid-importance sections into summaries 5. Drops low-importance sections entirely

func NewHierarchicalFilter

func NewHierarchicalFilter() *HierarchicalFilter

NewHierarchicalFilter creates a new hierarchical summarization filter.

func (*HierarchicalFilter) Apply

func (f *HierarchicalFilter) Apply(input string, mode Mode) (string, int)

func (*HierarchicalFilter) Name

func (f *HierarchicalFilter) Name() string

Name returns the filter name.

func (*HierarchicalFilter) SetLineThreshold

func (f *HierarchicalFilter) SetLineThreshold(threshold int)

SetLineThreshold configures the line threshold for hierarchical compression

func (*HierarchicalFilter) SetMaxDepth

func (f *HierarchicalFilter) SetMaxDepth(depth int)

SetMaxDepth configures the maximum summarization depth

type HierarchicalSummaryFilter

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

Paper: "AutoCompressor" — Chevalier et al., Princeton, 2023 https://arxiv.org/abs/2305.14788 Paper: "AutoCompressor" — Chevalier et al., Princeton, 2023 https://arxiv.org/abs/2305.14788 HierarchicalSummaryFilter implements AutoCompressor-style compression (Princeton/MIT, 2023). Recursive summarization that compresses context into summary vectors.

Algorithm: 1. Divide content into hierarchical levels (sections → paragraphs → sentences) 2. Summarize each level recursively 3. Combine summaries with preserved key content 4. Use bottom-up summarization for maximum compression

Research Results: Extreme compression (depends on summary size). Key insight: Recursive summarization preserves global context.

func NewHierarchicalSummaryFilter

func NewHierarchicalSummaryFilter() *HierarchicalSummaryFilter

NewHierarchicalSummaryFilter creates a new hierarchical summary filter

func (*HierarchicalSummaryFilter) Apply

func (f *HierarchicalSummaryFilter) Apply(input string, mode Mode) (string, int)

Apply applies hierarchical summarization

func (*HierarchicalSummaryFilter) Name

Name returns the filter name

func (*HierarchicalSummaryFilter) SetMaxLevels

func (f *HierarchicalSummaryFilter) SetMaxLevels(levels int)

SetMaxLevels sets the maximum recursion depth

func (*HierarchicalSummaryFilter) SetSummaryRatio

func (f *HierarchicalSummaryFilter) SetSummaryRatio(ratio float64)

SetSummaryRatio sets the summary ratio

type IBConfig

type IBConfig struct {
	Enabled            bool
	EntropyThreshold   float64
	RelevanceThreshold float64
}

IBConfig holds configuration for information bottleneck.

func DefaultIBConfig

func DefaultIBConfig() IBConfig

DefaultIBConfig returns default IB configuration.

type ImportFilter

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

ImportFilter condenses import statements.

func NewImportFilter

func NewImportFilter() *ImportFilter

NewImportFilter creates a new import filter.

func (*ImportFilter) Apply

func (f *ImportFilter) Apply(input string, mode Mode) (string, int)

Apply condenses import statements and returns token savings.

func (*ImportFilter) Name

func (f *ImportFilter) Name() string

Name returns the filter name.

type IncrementalDelta

type IncrementalDelta struct {
	Added     []string
	Removed   []string
	Unchanged int
}

IncrementalDelta computes the diff between old and new content. Inspired by lean-ctx's ctx_delta.

func ComputeDelta

func ComputeDelta(old, new string) IncrementalDelta

ComputeDelta computes the incremental delta between two versions.

type InformationBottleneck

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

InformationBottleneck filters content by entropy and task-relevance.

func NewInformationBottleneck

func NewInformationBottleneck(cfg IBConfig) *InformationBottleneck

NewInformationBottleneck creates a new information bottleneck filter.

func (*InformationBottleneck) Process

func (ib *InformationBottleneck) Process(content, query string) string

Process filters content by information bottleneck principle.

type InterLayerFeedback

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

InterLayerFeedback implements cross-layer feedback mechanism. This allows later layers to signal earlier layers to adjust aggressiveness, creating an adaptive pipeline that self-corrects based on compression results.

func NewInterLayerFeedback

func NewInterLayerFeedback() *InterLayerFeedback

NewInterLayerFeedback creates a new feedback mechanism

func (*InterLayerFeedback) GetAdjustment

func (f *InterLayerFeedback) GetAdjustment(layerName string) float64

GetAdjustment returns the suggested adjustment for a given layer

func (*InterLayerFeedback) RecordSignal

func (f *InterLayerFeedback) RecordSignal(signal FeedbackSignal)

RecordSignal records a feedback signal from a layer

func (*InterLayerFeedback) Reset

func (f *InterLayerFeedback) Reset()

Reset clears all feedback signals

type JSONSamplerFilter added in v0.28.0

type JSONSamplerFilter struct{}

JSONSamplerFilter down-samples dense JSON line streams while preserving anchors.

func NewJSONSamplerFilter added in v0.28.0

func NewJSONSamplerFilter() *JSONSamplerFilter

func (*JSONSamplerFilter) Apply added in v0.28.0

func (f *JSONSamplerFilter) Apply(input string, mode Mode) (string, int)

func (*JSONSamplerFilter) Name added in v0.28.0

func (f *JSONSamplerFilter) Name() string

type KVCacheAligner

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

KVCacheAligner implements KV-cache alignment for LLM prompt caching. Inspired by claw-compactor's QuantumLock and kompact's cache_aligner. Isolates stable prefix from dynamic content to maximize provider-level caching.

func NewKVCacheAligner

func NewKVCacheAligner(cfg KVCacheConfig) *KVCacheAligner

NewKVCacheAligner creates a new KV-cache aligner.

func (*KVCacheAligner) AlignPrefix

func (a *KVCacheAligner) AlignPrefix(content string) (string, string, string)

AlignPrefix isolates stable prefix from dynamic content. Returns (stablePrefix, dynamicSuffix, cacheKey).

func (*KVCacheAligner) CacheAwareCompress

func (a *KVCacheAligner) CacheAwareCompress(content string, compressor *PipelineCoordinator) (string, int)

CacheAwareCompress compresses only the dynamic portion, preserving stable prefix. This maintains byte-stable prefixes for provider-level caching.

type KVCacheConfig

type KVCacheConfig struct {
	Enabled          bool
	MinPrefixLength  int
	MaxDynamicSuffix int
	SplitThreshold   int
}

KVCacheConfig holds configuration for KV-cache alignment.

func DefaultKVCacheConfig

func DefaultKVCacheConfig() KVCacheConfig

DefaultKVCacheConfig returns default KV-cache alignment config.

type L0Summary added in v0.28.0

type L0Summary struct {
	Keywords   []string `json:"keywords"`
	Entities   []string `json:"entities"`
	Topics     []string `json:"topics"`
	TokenCount int      `json:"token_count"`
}

L0Summary is the surface-level summary.

type L1Summary added in v0.28.0

type L1Summary struct {
	Title      string    `json:"title"`
	Sections   []Section `json:"sections"`
	Outline    string    `json:"outline"`
	TokenCount int       `json:"token_count"`
}

L1Summary is the structural summary.

type L2Summary added in v0.28.0

type L2Summary struct {
	Summary      string   `json:"summary"`
	KeyPoints    []string `json:"key_points"`
	Implications []string `json:"implications,omitempty"`
	TokenCount   int      `json:"token_count"`
}

L2Summary is the deep semantic summary.

type LLMCompressRequest

type LLMCompressRequest struct {
	Content   string `json:"content"`
	MaxTokens int    `json:"max_tokens"`
	Mode      string `json:"mode"`
}

LLMCompressRequest is the JSON input for LLM compression.

type LLMCompressResponse

type LLMCompressResponse struct {
	Compressed string `json:"compressed"`
	TokensIn   int    `json:"tokens_in"`
	TokensOut  int    `json:"tokens_out"`
}

LLMCompressResponse is the JSON output from LLM compression.

type LLMCompressor

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

LLMCompressor uses an external LLM for semantic compression. Inspired by claw-compactor's Nexus and tamp's textpress.

func NewLLMCompressor

func NewLLMCompressor(binPath string) *LLMCompressor

NewLLMCompressor creates a new LLM-based compressor.

func (*LLMCompressor) Compress

func (lc *LLMCompressor) Compress(content string, maxTokens int) (string, int, int)

Compress uses the external LLM to semantically compress content.

func (*LLMCompressor) IsEnabled

func (lc *LLMCompressor) IsEnabled() bool

IsEnabled returns whether LLM compression is available.

func (*LLMCompressor) SetEnabled

func (lc *LLMCompressor) SetEnabled(enabled bool)

SetEnabled toggles LLM compression.

type LRUCache

type LRUCache = cache.LRUCache

LRUCache is an alias to the unified LRU cache implementation. Note: The cache stores *CachedResult values defined locally in manager.go.

type Language

type Language string

Language represents a programming language for filtering

const (
	LangRust       Language = "rust"
	LangPython     Language = "python"
	LangJavaScript Language = "javascript"
	LangTypeScript Language = "typescript"
	LangGo         Language = "go"
	LangC          Language = "c"
	LangCpp        Language = "cpp"
	LangJava       Language = "java"
	LangRuby       Language = "ruby"
	LangShell      Language = "sh"
	LangSQL        Language = "sql"
	LangUnknown    Language = "unknown"
)

func DetectLanguageFromInput

func DetectLanguageFromInput(input string) Language

DetectLanguageFromInput detects language from input content. Delegates to DetectLanguage and wraps the result as a Language.

type LatentCollabFilter added in v0.28.0

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

LatentCollabFilter approximates latent-space collaboration by collapsing semantically equivalent multi-agent turns into compact markers.

func NewLatentCollabFilter added in v0.28.0

func NewLatentCollabFilter() *LatentCollabFilter

NewLatentCollabFilter creates a latent-collaboration inspired filter.

func (*LatentCollabFilter) Apply added in v0.28.0

func (f *LatentCollabFilter) Apply(input string, mode Mode) (string, int)

Apply merges highly similar adjacent agent turns.

func (*LatentCollabFilter) Name added in v0.28.0

func (f *LatentCollabFilter) Name() string

Name returns the filter name.

type LayerCache added in v0.28.0

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

LayerCache provides content-addressable caching for filter results. Uses SHA-256 hashing for cache keys with LRU eviction.

func GetGlobalLayerCache added in v0.28.0

func GetGlobalLayerCache() *LayerCache

GetGlobalLayerCache returns the global layer cache instance.

func NewLayerCache added in v0.28.0

func NewLayerCache(maxSize int, ttl time.Duration) *LayerCache

NewLayerCache creates a new layer cache.

func (*LayerCache) Clear added in v0.28.0

func (c *LayerCache) Clear()

Clear removes all entries from the cache.

func (*LayerCache) Get added in v0.28.0

func (c *LayerCache) Get(layerName, input string, mode Mode) (*LayerCacheEntry, bool)

Get retrieves a cached result if available and not expired.

func (*LayerCache) Put added in v0.28.0

func (c *LayerCache) Put(layerName, input string, mode Mode, output string, tokensSaved int)

Put stores a result in the cache.

func (*LayerCache) Stats added in v0.28.0

func (c *LayerCache) Stats() CacheStats

Stats returns cache statistics.

type LayerCacheEntry added in v0.28.0

type LayerCacheEntry struct {
	InputHash   string
	Output      string
	TokensSaved int
	LayerName   string
	Mode        Mode
	Timestamp   time.Time
	HitCount    int
}

LayerCacheEntry stores cached filter results.

type LayerConfig

type LayerConfig struct {
	Core              CoreLayersConfig
	Compaction        CompactionLayerConfig
	Attribution       AttributionLayerConfig
	H2O               H2OLayerConfig
	AttentionSink     AttentionSinkLayerConfig
	MetaToken         MetaTokenLayerConfig
	SemanticChunk     SemanticChunkLayerConfig
	SketchStore       SketchStoreLayerConfig
	LazyPruner        LazyPrunerLayerConfig
	SemanticAnchor    SemanticAnchorLayerConfig
	AgentMemory       AgentMemoryLayerConfig
	QuestionAware     QuestionAwareLayerConfig
	DensityAdaptive   DensityAdaptiveLayerConfig
	TFIDF             TFIDFLayerConfig
	NumericalQuant    NumericalQuantLayerConfig
	DynamicRatio      DynamicRatioLayerConfig
	SymbolicCompress  bool
	PhraseGrouping    bool
	Hypernym          bool
	SemanticCache     bool
	Scope             bool
	SmallKV           bool
	KVzip             bool
	SWEzze            bool
	MixedDim          bool
	BEAVER            bool
	PoC               bool
	TokenQuant        bool
	TokenRetention    bool
	ACON              bool
	TOMLFilter        bool
	TOMLFilterCommand string
	CacheEnabled      bool

	// New: Claw Compactor features
	EnableAdaptiveLearning bool // Enable adaptive learning (merged EngramLearner + TieredSummary)
	EnableCrunchBench      bool // Enable comprehensive benchmarking
}

LayerConfig groups per-layer config structs.

type LayerGate added in v0.28.0

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

LayerGate controls layer execution policy.

func NewLayerGate added in v0.28.0

func NewLayerGate(mode string, allowExperimental []string, registry *LayerRegistry) *LayerGate

func (*LayerGate) Allows added in v0.28.0

func (g *LayerGate) Allows(layerID string) bool

type LayerMeta added in v0.28.0

type LayerMeta struct {
	ID       string
	Name     string
	Tier     LayerTier
	Status   string // implemented | planned
	PaperRef string
}

LayerMeta documents a layer and its research provenance.

type LayerRegistry added in v0.28.0

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

LayerRegistry stores metadata for all known layers.

func NewLayerRegistry added in v0.28.0

func NewLayerRegistry() *LayerRegistry

func (*LayerRegistry) Get added in v0.28.0

func (r *LayerRegistry) Get(id string) (LayerMeta, bool)

type LayerStat

type LayerStat struct {
	TokensSaved int
	Duration    int64
}

LayerStat holds statistics for a single layer

type LayerTier added in v0.28.0

type LayerTier string

LayerTier describes maturity/activation tier for a layer.

const (
	LayerTierStable       LayerTier = "stable"
	LayerTierExperimental LayerTier = "experimental"
	LayerTierRecovery     LayerTier = "recovery"
	LayerTierPlanned      LayerTier = "planned"
)

type LayerTiming added in v0.28.0

type LayerTiming struct {
	TokensSaved int
	Duration    time.Duration
}

LayerTiming tracks per-layer performance.

type LayersSection

type LayersSection struct {
	Entropy        bool `toml:"entropy"`
	Perplexity     bool `toml:"perplexity"`
	GoalDriven     bool `toml:"goal_driven"`
	AST            bool `toml:"ast"`
	Contrastive    bool `toml:"contrastive"`
	Ngram          bool `toml:"ngram"`
	Evaluator      bool `toml:"evaluator"`
	Gist           bool `toml:"gist"`
	Hierarchical   bool `toml:"hierarchical"`
	Compaction     bool `toml:"compaction"`
	Attribution    bool `toml:"attribution"`
	H2O            bool `toml:"h2o"`
	AttentionSink  bool `toml:"attention_sink"`
	MetaToken      bool `toml:"meta_token"`
	SemanticChunk  bool `toml:"semantic_chunk"`
	SketchStore    bool `toml:"sketch_store"`
	LazyPruner     bool `toml:"lazy_pruner"`
	SemanticAnchor bool `toml:"semantic_anchor"`
	AgentMemory    bool `toml:"agent_memory"`
	TFIDF          bool `toml:"tfidf"`
	Symbolic       bool `toml:"symbolic"`
	PhraseGroup    bool `toml:"phrase_group"`
	Numerical      bool `toml:"numerical"`
	DynamicRatio   bool `toml:"dynamic_ratio"`
	TOON           bool `toml:"toon"`
	TDD            bool `toml:"tdd"`
}

LayersSection holds layer enable/disable configuration.

type LazyPrunerConfig

type LazyPrunerConfig struct {
	// BaseBudget is the initial token budget for layer 0
	BaseBudget int

	// DecayRate is the budget decay per layer (0.9 = 10% reduction)
	DecayRate float64

	// NumLayers is the number of layers to compute budgets for
	NumLayers int

	// RevivalBudget is max tokens to pull back from pruned pool
	RevivalBudget int

	// AttentionThreshold is the minimum score to keep a token
	AttentionThreshold float64

	// EnableRevival allows on-demand token recovery
	EnableRevival bool
}

LazyPrunerConfig holds configuration for dynamic pruning

func DefaultLazyPrunerConfig

func DefaultLazyPrunerConfig() LazyPrunerConfig

DefaultLazyPrunerConfig returns default configuration

type LazyPrunerFilter

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

Paper: "LazyLLM: Dynamic Token Pruning" — Fu et al., Apple, 2024 https://arxiv.org/abs/2407.14057 Paper: "LazyLLM: Dynamic Token Pruning" — Fu et al., Apple, 2024 https://arxiv.org/abs/2407.14057 LazyPrunerFilter implements Layer 18: Budget-aware Dynamic Pruning (LazyLLM style).

Research Source: "LazyLLM: Dynamic Token Pruning" (July 2024) Key Innovation: Selective KV computation with layer-wise budget decay. Results: 2.34x speedup in prefill phase with maintained accuracy.

Methodology: 1. Dynamic token selection based on attention scores 2. Layer-wise budget decay (deeper layers = smaller budgets) 3. Prune-and-Revive mechanism for recoverable pruning 4. Selective prefill to accelerate inference

func NewLazyPrunerFilter

func NewLazyPrunerFilter() *LazyPrunerFilter

NewLazyPrunerFilter creates a new lazy pruner filter

func NewLazyPrunerFilterWithConfig

func NewLazyPrunerFilterWithConfig(cfg LazyPrunerConfig) *LazyPrunerFilter

NewLazyPrunerFilterWithConfig creates a filter with custom config

func (*LazyPrunerFilter) Apply

func (f *LazyPrunerFilter) Apply(input string, mode Mode) (string, int)

Apply applies budget-aware dynamic pruning

func (*LazyPrunerFilter) Clear

func (f *LazyPrunerFilter) Clear()

Clear clears the pruned token storage

func (*LazyPrunerFilter) GetLayerBudget

func (f *LazyPrunerFilter) GetLayerBudget(layer int) int

GetLayerBudget returns the budget for a specific layer

func (*LazyPrunerFilter) GetLayerBudgets

func (f *LazyPrunerFilter) GetLayerBudgets() []int

GetLayerBudgets returns all layer budgets

func (*LazyPrunerFilter) GetStats

func (f *LazyPrunerFilter) GetStats() LazyPrunerStats

GetStats returns pruning statistics

func (*LazyPrunerFilter) Name

func (f *LazyPrunerFilter) Name() string

Name returns the filter name

func (*LazyPrunerFilter) ReviveTokens

func (f *LazyPrunerFilter) ReviveTokens(layer int, count int) []Token

ReviveTokens recovers previously pruned tokens

func (*LazyPrunerFilter) SelectTokens

func (f *LazyPrunerFilter) SelectTokens(tokens []Token, layer int, threshold float64) []Token

SelectTokens selects tokens based on attention scores

func (*LazyPrunerFilter) StorePruned

func (f *LazyPrunerFilter) StorePruned(tokens []Token, layer int)

StorePruned stores pruned tokens for potential revival

type LazyPrunerLayerConfig

type LazyPrunerLayerConfig struct {
	Enabled       bool
	BaseBudget    int
	DecayRate     float64
	RevivalBudget int
}

LazyPrunerLayerConfig groups Layer 18 settings.

type LazyPrunerStats

type LazyPrunerStats struct {
	TotalPruned   int
	TotalRevived  int
	TokensSaved   int
	LayersApplied int
}

LazyPrunerStats tracks pruning statistics

type LightMemFilter added in v0.28.0

type LightMemFilter struct{}

LightMemFilter reuses previously seen high-signal facts with short references.

func NewLightMemFilter added in v0.28.0

func NewLightMemFilter() *LightMemFilter

NewLightMemFilter creates the lightweight memory-augmentation filter.

func (*LightMemFilter) Apply added in v0.28.0

func (f *LightMemFilter) Apply(input string, mode Mode) (string, int)

Apply detects repeated facts and replaces duplicates with lightweight references.

func (*LightMemFilter) Name added in v0.28.0

func (f *LightMemFilter) Name() string

Name returns the filter name.

type LightThinkerFilter added in v0.28.0

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

Paper: "LightThinker: Thinking Step-by-Step Compression" EMNLP 2025 — Zhang et al., Zhejiang University https://arxiv.org/abs/2502.15589

LightThinkerFilter compresses reasoning output at step granularity. Unlike CoTCompressFilter which truncates whole blocks, LightThinker retains all steps but skeletonises each one to its single most informative sentence — a sketch of each reasoning step.

Algorithm:

  1. Segment input into reasoning steps (numbered/labeled sequences)
  2. For each step with ≥ minStepLines, extract the "key sentence": the line with the highest unique-term density relative to the step
  3. Replace the step body with: step header + key sentence + stub
  4. Pass non-step lines through unchanged

Key insight: keeping one sentence per step (the conclusion/observation) preserves the logical trajectory while cutting 60-80% of step content.

func NewLightThinkerFilter added in v0.28.0

func NewLightThinkerFilter() *LightThinkerFilter

NewLightThinkerFilter creates a new LightThinker step-level compressor.

func (*LightThinkerFilter) Apply added in v0.28.0

func (f *LightThinkerFilter) Apply(input string, mode Mode) (string, int)

Apply skeletonises reasoning steps to one key sentence each.

func (*LightThinkerFilter) Name added in v0.28.0

func (f *LightThinkerFilter) Name() string

Name returns the filter name.

type LogCrunch

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

LogCrunch folds repeated log lines with occurrence counts. Inspired by claw-compactor's LogCrunch stage.

func NewLogCrunch

func NewLogCrunch(cfg LogCrunchConfig) *LogCrunch

NewLogCrunch creates a new LogCrunch filter.

func (*LogCrunch) Process

func (lc *LogCrunch) Process(content string) (string, int)

Process folds repeated log lines.

type LogCrunchConfig

type LogCrunchConfig struct {
	Enabled        bool
	MinRepetitions int
	AlwaysPreserve []string
}

LogCrunchConfig holds configuration for LogCrunch.

func DefaultLogCrunchConfig

func DefaultLogCrunchConfig() LogCrunchConfig

DefaultLogCrunchConfig returns default LogCrunch configuration.

type LogCrunchFilter added in v0.28.0

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

LogCrunchFilter folds repetitive INFO/DEBUG logs while preserving warnings/errors and state transitions.

func NewLogCrunchFilter added in v0.28.0

func NewLogCrunchFilter() *LogCrunchFilter

func (*LogCrunchFilter) Apply added in v0.28.0

func (f *LogCrunchFilter) Apply(input string, mode Mode) (string, int)

func (*LogCrunchFilter) Name added in v0.28.0

func (f *LogCrunchFilter) Name() string

type ManagerConfig

type ManagerConfig struct {
	// Context limits
	MaxContextTokens int
	ChunkSize        int
	StreamThreshold  int

	// Resilience
	TeeOnFailure       bool
	FailSafeMode       bool
	ValidateOutput     bool
	ShortCircuitBudget bool

	// Performance
	CacheEnabled bool
	CacheMaxSize int

	// Layer config
	PipelineCfg PipelineConfig
}

ManagerConfig configures the pipeline manager

type MarginalInfoGainFilter added in v0.28.0

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

Paper: "COMI: Coarse-to-fine Context Compression via Marginal Information Gain" 2026 — scores each line by how much NEW information it contributes to the already-retained set, rather than scoring lines in isolation.

Key insight: lines that repeat already-covered terms add zero marginal gain, so they can be dropped even if individually "important."

Algorithm:

  1. Build global TF map; identify discriminative terms (frequent but not ubiquitous)
  2. Assign each line a term-set covering its discriminative terms
  3. Greedy selection: rank lines by marginal_gain / token_cost, apply structural bonus
  4. Fill token budget top-down; anchor first and last non-empty lines

func NewMarginalInfoGainFilter added in v0.28.0

func NewMarginalInfoGainFilter() *MarginalInfoGainFilter

NewMarginalInfoGainFilter creates a new COMI-style marginal information gain filter.

func (*MarginalInfoGainFilter) Apply added in v0.28.0

func (f *MarginalInfoGainFilter) Apply(input string, mode Mode) (string, int)

Apply selects lines that maximize marginal information gain within a token budget.

func (*MarginalInfoGainFilter) Name added in v0.28.0

func (f *MarginalInfoGainFilter) Name() string

Name returns the filter name.

type Message added in v0.28.0

type Message struct {
	Role    string
	Content string
}

Message represents a chat message with role and content.

type MetaToken

type MetaToken struct {
	Hash     string // SHA256 hash of the original sequence
	Original string // Original text that was compressed
	Length   int    // Number of tokens in original sequence
	Count    int    // Number of times this pattern was found
}

MetaToken represents a compressed token sequence

type MetaTokenConfig

type MetaTokenConfig struct {
	// WindowSize is the maximum sequence length to consider for compression
	WindowSize int

	// MinPattern is the minimum sequence length to compress (shorter = more compression but more meta-tokens)
	MinPattern int

	// MaxMetaTokens limits the number of meta-tokens created (0 = unlimited)
	MaxMetaTokens int

	// EnableDecompression allows this filter to also decompress
	EnableDecompression bool
}

MetaTokenConfig holds configuration for the meta-token filter

func DefaultMetaTokenConfig

func DefaultMetaTokenConfig() MetaTokenConfig

DefaultMetaTokenConfig returns the default configuration

type MetaTokenFilter

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

Paper: "Lossless Token Compression via Meta-Tokens" — 2025 https://arxiv.org/abs/2506.00307 Paper: "Lossless Token Compression via Meta-Tokens" — 2025 https://arxiv.org/abs/2506.00307 MetaTokenFilter implements Layer 15: Lossless Token Sequence Compression via Meta-Tokens.

Research Source: "Lossless Token Sequence Compression via Meta-Tokens" (arXiv:2506.00307) Key Innovation: LZ77-style lossless compression operating on token sequences. Results: 27% token reduction = 47% compute reduction (due to quadratic attention) Critical Feature: ZERO semantic loss - trivially reversible.

Methodology: 1. Scan for repeated token sequences (sliding window) 2. Replace with meta-tokens that reference the original sequence 3. Meta-tokens use special marker format: [META:hash:length] 4. Decompression expands meta-tokens back to original sequences

func NewMetaTokenFilter

func NewMetaTokenFilter() *MetaTokenFilter

NewMetaTokenFilter creates a new meta-token lossless compression filter

func NewMetaTokenFilterWithConfig

func NewMetaTokenFilterWithConfig(cfg MetaTokenConfig) *MetaTokenFilter

NewMetaTokenFilterWithConfig creates a meta-token filter with custom config

func (*MetaTokenFilter) Apply

func (f *MetaTokenFilter) Apply(input string, mode Mode) (string, int)

Apply applies lossless compression via meta-tokens

func (*MetaTokenFilter) Decompress

func (f *MetaTokenFilter) Decompress(input string) string

Decompress expands meta-tokens back to original sequences

func (*MetaTokenFilter) GetMetaTokens

func (f *MetaTokenFilter) GetMetaTokens() map[string]MetaToken

GetMetaTokens returns all stored meta-tokens (for serialization)

func (*MetaTokenFilter) LoadMetaTokens

func (f *MetaTokenFilter) LoadMetaTokens(tokens map[string]MetaToken)

LoadMetaTokens loads meta-tokens (for deserialization)

func (*MetaTokenFilter) Name

func (f *MetaTokenFilter) Name() string

Name returns the filter name

func (*MetaTokenFilter) Stats

func (f *MetaTokenFilter) Stats() MetaTokenStats

Stats returns compression statistics

type MetaTokenLayerConfig

type MetaTokenLayerConfig struct {
	Enabled bool
	Window  int
	MinSize int
}

MetaTokenLayerConfig groups Layer 15 settings.

type MetaTokenStats

type MetaTokenStats struct {
	UniquePatterns int
	TotalPatterns  int
	EstTokensSaved int
}

MetaTokenStats holds statistics for meta-token compression

type Milestone

type Milestone struct {
	Description string `json:"description"`
	Priority    int    `json:"priority"`
	Status      string `json:"status"`
}

Milestone represents a pending task

type Mode

type Mode string

Mode represents the filtering mode.

const (
	ModeNone       Mode = "none"
	ModeMinimal    Mode = "minimal"
	ModeAggressive Mode = "aggressive"
)

type MultiFileConfig

type MultiFileConfig struct {
	MaxCombinedSize     int
	PreserveBoundaries  bool
	SimilarityThreshold float64
}

MultiFileConfig holds configuration for multi-file optimization

type MultiFileFilter

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

MultiFileFilter optimizes output across multiple related files/outputs. It identifies relationships between files, deduplicates common content, and creates unified summaries for better LLM context.

Use case: When an agent works with multiple related files simultaneously (e.g., a module with multiple source files), this filter creates a cohesive view that preserves relationships while removing redundancy.

func NewMultiFileFilter

func NewMultiFileFilter(cfg MultiFileConfig) *MultiFileFilter

NewMultiFileFilter creates a new multi-file optimization filter

func (*MultiFileFilter) Apply

func (f *MultiFileFilter) Apply(input string, mode Mode) (string, int)

Apply applies multi-file optimization

func (*MultiFileFilter) Name

func (f *MultiFileFilter) Name() string

Name returns the filter name

func (*MultiFileFilter) SetMaxCombinedSize

func (f *MultiFileFilter) SetMaxCombinedSize(size int)

SetMaxCombinedSize configures the maximum combined output size

func (*MultiFileFilter) SetPreserveBoundaries

func (f *MultiFileFilter) SetPreserveBoundaries(preserve bool)

SetPreserveBoundaries configures whether to keep file markers

func (*MultiFileFilter) SetSimilarityThreshold

func (f *MultiFileFilter) SetSimilarityThreshold(threshold float64)

SetSimilarityThreshold configures the deduplication threshold

type NearDedupFilter added in v0.28.0

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

Paper: "DART: Stop Looking for Important Tokens, Duplication Matters More" EMNLP 2025 — Kim et al., KAIST

Key finding: aggressively collapsing near-duplicate content consistently outperforms importance-based selection across benchmarks, because LLMs are hurt more by seeing the same information N times than by losing one "important" token.

NearDedupFilter groups near-duplicate lines (within a single output) using SimHash fingerprints and Hamming distance, then collapses each cluster to its most informative representative with a count annotation.

Typical wins: repeated cargo/clippy warnings, stacked log lines with varying file paths, duplicated test assertion messages.

func NewNearDedupFilter added in v0.28.0

func NewNearDedupFilter() *NearDedupFilter

NewNearDedupFilter creates a new DART-inspired near-duplicate line filter.

func (*NearDedupFilter) Apply added in v0.28.0

func (f *NearDedupFilter) Apply(input string, mode Mode) (string, int)

Apply collapses near-duplicate lines preserving the best representative.

func (*NearDedupFilter) Name added in v0.28.0

func (f *NearDedupFilter) Name() string

Name returns the filter name.

func (*NearDedupFilter) SetThreshold added in v0.28.0

func (f *NearDedupFilter) SetThreshold(t int)

SetThreshold overrides the Hamming distance threshold.

type NgramAbbreviator

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

NgramAbbreviator compresses output by abbreviating common patterns. Research-based: CompactPrompt N-gram Abbreviation (2025) - achieves 10-20% lossless compression by replacing common tokens with shorter equivalents.

Key insight: Programming and CLI output contains many repeated long tokens that can be abbreviated while remaining understandable to LLMs.

func NewNgramAbbreviator

func NewNgramAbbreviator() *NgramAbbreviator

NewNgramAbbreviator creates a new n-gram abbreviator.

func (*NgramAbbreviator) Apply

func (f *NgramAbbreviator) Apply(input string, mode Mode) (string, int)

Apply applies n-gram abbreviation to the input.

func (*NgramAbbreviator) GetAbbreviationLegend

func (f *NgramAbbreviator) GetAbbreviationLegend() string

GetAbbreviationLegend returns a legend for common abbreviations

func (*NgramAbbreviator) Name

func (f *NgramAbbreviator) Name() string

Name returns the filter name.

type NumericalQuantLayerConfig

type NumericalQuantLayerConfig struct {
	Enabled       bool
	DecimalPlaces int
}

NumericalQuantLayerConfig groups numerical quantization settings. NOTE: Currently unused - reserved for future implementation.

type PATHShimInjector

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

PATHShimInjector creates PATH shims to auto-filter subprocesses. Inspired by tokf's PATH shim injection.

func NewPATHShimInjector

func NewPATHShimInjector(shimDir string) *PATHShimInjector

NewPATHShimInjector creates a new PATH shim injector.

func (*PATHShimInjector) Install

func (psi *PATHShimInjector) Install(commands []string) error

Install installs PATH shims for specified commands.

func (*PATHShimInjector) Uninstall

func (psi *PATHShimInjector) Uninstall() error

Uninstall removes PATH shims.

func (*PATHShimInjector) UpdatePATH

func (psi *PATHShimInjector) UpdatePATH(currentPath string) string

UpdatePATH returns the updated PATH with shim directory prepended.

type ParallelCompressor added in v0.28.0

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

ParallelCompressor provides high-level parallel compression interface

func NewParallelCompressor added in v0.28.0

func NewParallelCompressor(config PipelineConfig) *ParallelCompressor

NewParallelCompressor creates a new parallel compressor

func (*ParallelCompressor) Compress added in v0.28.0

func (pc *ParallelCompressor) Compress(input string) (string, int)

Compress compresses a single input

func (*ParallelCompressor) CompressBatch added in v0.28.0

func (pc *ParallelCompressor) CompressBatch(inputs []string) []ParallelProcessResult

CompressBatch compresses multiple inputs in parallel

func (*ParallelCompressor) CompressBatchContext added in v0.28.0

func (pc *ParallelCompressor) CompressBatchContext(ctx context.Context, inputs []string) ([]ParallelProcessResult, error)

CompressBatchContext compresses with context support

func (*ParallelCompressor) WorkerCount added in v0.28.0

func (pc *ParallelCompressor) WorkerCount() int

WorkerCount returns the number of workers

type ParallelFilterResult added in v0.28.0

type ParallelFilterResult struct {
	Output string
	Saved  int
	Error  error
}

ParallelFilterResult holds result from parallel filter execution

type ParallelPipelineStats added in v0.28.0

type ParallelPipelineStats struct {
	LayerStats     map[string]LayerStat
	TotalSaved     int
	ParallelTime   int64
	SequentialTime int64
	// contains filtered or unexported fields
}

ParallelPipelineStats holds stats from parallel execution

func NewParallelPipelineStats added in v0.28.0

func NewParallelPipelineStats() *ParallelPipelineStats

NewParallelPipelineStats creates new stats tracker

func (*ParallelPipelineStats) AddStat added in v0.28.0

func (s *ParallelPipelineStats) AddStat(name string, stat LayerStat)

AddStat adds a layer stat thread-safely

type ParallelProcessResult added in v0.28.0

type ParallelProcessResult struct {
	Input  string
	Output string
	Saved  int
	Index  int
}

ParallelProcessResult holds the result of a parallel processing operation

type ParallelProcessor added in v0.28.0

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

ParallelProcessor handles parallel compression of multiple inputs Uses worker pool pattern for optimal CPU utilization

func NewParallelProcessor added in v0.28.0

func NewParallelProcessor() *ParallelProcessor

NewParallelProcessor creates a new parallel processor Automatically determines optimal worker count based on CPU cores

func NewParallelProcessorWithWorkers added in v0.28.0

func NewParallelProcessorWithWorkers(workers int) *ParallelProcessor

NewParallelProcessorWithWorkers creates processor with specific worker count

func (*ParallelProcessor) ProcessItems added in v0.28.0

func (p *ParallelProcessor) ProcessItems(items []string, processFn func(string) (string, int)) []ParallelProcessResult

ProcessItems processes multiple items in parallel Each item is processed by the provided function

func (*ParallelProcessor) ProcessItemsContext added in v0.28.0

func (p *ParallelProcessor) ProcessItemsContext(ctx context.Context, items []string, processFn func(context.Context, string) (string, int)) ([]ParallelProcessResult, error)

ProcessItemsContext processes items with context cancellation support

type PathShortenFilter added in v0.28.0

type PathShortenFilter struct{}

PathShortenFilter aliases repeated long paths/identifiers for compactness.

func NewPathShortenFilter added in v0.28.0

func NewPathShortenFilter() *PathShortenFilter

func (*PathShortenFilter) Apply added in v0.28.0

func (f *PathShortenFilter) Apply(input string, mode Mode) (string, int)

func (*PathShortenFilter) Name added in v0.28.0

func (f *PathShortenFilter) Name() string

type PerceptionCompressFilter added in v0.28.0

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

Paper: "Perception Compressor: Training-Free Prompt Compression for Long Context" arXiv:2504.xxxxx — 2025

PerceptionCompressFilter identifies "perceptually redundant" lines: those whose semantic content is already covered by their immediate neighbors. Removing them does not change what an LLM would perceive as the meaning of the context.

Proxy for perceptual redundancy (training-free):

  • Compute term-overlap between line i and its window (i±windowSize)
  • If overlap / own_terms ≥ threshold, the line is dominated by neighbors

This catches verbose prose, repeated captions, duplicate log prefixes, and transitional boilerplate that carries no new information.

func NewPerceptionCompressFilter added in v0.28.0

func NewPerceptionCompressFilter() *PerceptionCompressFilter

NewPerceptionCompressFilter creates a new Perception Compressor filter.

func (*PerceptionCompressFilter) Apply added in v0.28.0

func (f *PerceptionCompressFilter) Apply(input string, mode Mode) (string, int)

Apply removes perceptually redundant lines.

func (*PerceptionCompressFilter) Name added in v0.28.0

func (f *PerceptionCompressFilter) Name() string

Name returns the filter name.

type PerplexityFilter

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

Paper: "LLMLingua" — Jiang et al., Microsoft/Tsinghua, 2023 https://arxiv.org/abs/2310.05736 Paper: "LLMLingua" — Jiang et al., Microsoft/Tsinghua, 2023 https://arxiv.org/abs/2310.05736 PerplexityFilter implements LLMLingua-style compression (Microsoft/Tsinghua, 2023). Uses perplexity-based iterative pruning with a budget controller.

Algorithm: 1. Calculate perplexity of each token given context 2. Rank tokens by perplexity (higher = more surprising = more important) 3. Iteratively remove lowest-perplexity tokens while staying within budget

Research Results: Up to 20x compression with semantic preservation.

func NewPerplexityFilter

func NewPerplexityFilter() *PerplexityFilter

NewPerplexityFilter creates a new perplexity-based filter

func (*PerplexityFilter) Apply

func (f *PerplexityFilter) Apply(input string, mode Mode) (string, int)

Apply applies perplexity-based iterative pruning with early exit (Phase 1 optimization)

func (*PerplexityFilter) Name

func (f *PerplexityFilter) Name() string

Name returns the filter name

func (*PerplexityFilter) SetIterations

func (f *PerplexityFilter) SetIterations(iterations int)

SetIterations sets the number of pruning iterations

func (*PerplexityFilter) SetTargetRatio

func (f *PerplexityFilter) SetTargetRatio(ratio float64)

SetTargetRatio sets the target compression ratio

type PhotonFilter

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

PhotonFilter detects and compresses base64-encoded images. Inspired by claw-compactor's Photon stage.

func NewPhotonFilter

func NewPhotonFilter() *PhotonFilter

NewPhotonFilter creates a new Photon filter with default configuration.

func (*PhotonFilter) Apply added in v0.28.0

func (f *PhotonFilter) Apply(input string, mode Mode) (string, int)

Apply implements the Filter interface for Photon image compression.

func (*PhotonFilter) Name added in v0.28.0

func (f *PhotonFilter) Name() string

Name returns the filter name for the pipeline.

type PipeOp

type PipeOp struct {
	Type string
	Args []string
}

PipeOp represents a single pipe operation.

type Pipeline

type Pipeline interface {
	Process(input string) (string, *PipelineStats)
}

Pipeline defines the interface for compression pipelines. This allows mock testing and future pipeline implementations.

type PipelineConfig

type PipelineConfig = PipelineConfigWithNestedLayers

PipelineConfig is an alias for the full config type with backward-compatible flat fields. New code should use PipelineConfigWithNestedLayers to take advantage of nested structure.

func BuildConfigFromTiers added in v0.28.0

func BuildConfigFromTiers(tiers []AutoTier, baseConfig PipelineConfig) PipelineConfig

BuildConfigFromTiers creates a PipelineConfig with tiers enabled.

func LoadPipelineFromTOML

func LoadPipelineFromTOML(path string) (PipelineConfig, error)

LoadPipelineFromTOML loads pipeline configuration from TOML.

func ModeConfig

func ModeConfig(mode CompressionMode, baseMode Mode) PipelineConfig

ModeConfig is an alias for TierConfig (backwards compat).

func PresetConfig

func PresetConfig(preset PipelinePreset, baseMode Mode) PipelineConfig

func ProfileConfig

func ProfileConfig(profile Profile, baseMode Mode) PipelineConfig

ProfileConfig is an alias for TierConfig (backwards compat).

func TierConfig

func TierConfig(tier Tier, baseMode Mode) PipelineConfig

TierConfig returns a PipelineConfig for the given tier.

func ToFilterPipelineConfig added in v0.28.0

func ToFilterPipelineConfig(c config.PipelineConfig, opts PipelineRuntimeOptions) PipelineConfig

ToFilterPipelineConfig converts user-facing config into the runtime pipeline config. Some fields are best-effort mappings because the public config and runtime pipeline have diverged over time; centralizing that mapping keeps behavior consistent.

type PipelineConfigWithNestedLayers

type PipelineConfigWithNestedLayers struct {
	// Core fields
	Mode                      Mode
	QueryIntent               string
	Budget                    int
	LLMEnabled                bool
	SessionTracking           bool
	NgramEnabled              bool
	MultiFileEnabled          bool
	PromptTemplate            string
	EnableTOMLFilter          bool
	TOMLFilterCommand         string
	EnablePolicyRouter        bool
	EnableExtractivePrefilter bool
	ExtractiveMaxLines        int
	ExtractiveHeadLines       int
	ExtractiveTailLines       int
	ExtractiveSignalLines     int
	EnableQualityGuardrail    bool
	LayerGateMode             string

	// New: Claw Compactor features
	EnableAdaptiveLearning     bool // Enable adaptive learning (merged EngramLearner + TieredSummary)
	EnableCrunchBench          bool // Enable comprehensive benchmarking
	LayerGateAllowExperimental []string
	EnablePlannedLayers        bool

	// Layer 0: QuantumLock (KV-cache alignment)
	EnableQuantumLock bool

	// Layer 0.5: Photon (image compression)
	EnablePhoton bool

	// Layer sub-configs (preferred)
	Layers LayerConfig

	// Core layer enable flags (Layers 1-9)
	EnableEntropy      bool
	EnablePerplexity   bool
	EnableGoalDriven   bool
	EnableAST          bool
	EnableContrastive  bool
	EnableEvaluator    bool
	EnableGist         bool
	EnableHierarchical bool

	// Layer 11: Compaction
	EnableCompaction        bool
	CompactionThreshold     int
	CompactionPreserveTurns int
	CompactionMaxTokens     int
	CompactionStateSnapshot bool
	CompactionAutoDetect    bool

	// Layer 12: Attribution
	EnableAttribution    bool
	AttributionThreshold float64

	// Layer 13: H2O
	EnableH2O          bool
	H2OSinkSize        int
	H2ORecentSize      int
	H2OHeavyHitterSize int

	// Layer 14: Attention Sink
	EnableAttentionSink  bool
	AttentionSinkCount   int
	AttentionRecentCount int

	// Layer 15: Meta-Token
	EnableMetaToken  bool
	MetaTokenWindow  int
	MetaTokenMinSize int

	// Layer 16: Semantic Chunk
	EnableSemanticChunk    bool
	SemanticChunkMethod    string
	SemanticChunkMinSize   int
	SemanticChunkThreshold float64

	// Layer 17: Semantic Cache
	EnableSketchStore bool
	SketchBudgetRatio float64
	SketchMaxSize     int
	SketchHeavyHitter float64

	// Layer 18: Lazy Pruner
	EnableLazyPruner  bool
	LazyBaseBudget    int
	LazyDecayRate     float64
	LazyRevivalBudget int

	// Layer 19: Semantic Anchor
	EnableSemanticAnchor  bool
	SemanticAnchorRatio   float64
	SemanticAnchorSpacing int

	// Layer 20: Agent Memory
	EnableAgentMemory       bool
	AgentKnowledgeRetention float64
	AgentHistoryPrune       float64
	AgentConsolidationMax   int

	// Adaptive layers
	EnableQuestionAware    bool
	QuestionAwareThreshold float64
	EnableDensityAdaptive  bool
	DensityTargetRatio     float64
	DensityThreshold       float64

	// TF-IDF
	EnableTFIDF    bool
	TFIDFThreshold float64

	// Reasoning trace
	EnableReasoningTrace bool
	MaxReflectionLoops   int

	// Phase 1: NEW filters
	EnableSymbolicCompress bool
	EnablePhraseGrouping   bool
	EnableNumericalQuant   bool
	DecimalPlaces          int
	EnableDynamicRatio     bool
	DynamicRatioBase       float64

	// Phase 2: Advanced filters
	EnableHypernym      bool
	EnableSemanticCache bool
	EnableScope         bool
	EnableSmallKV       bool
	EnableKVzip         bool

	// 2026 Research layers
	EnableSWEzze         bool
	EnableMixedDim       bool
	EnableBEAVER         bool
	EnablePoC            bool
	EnableTokenQuant     bool
	EnableTokenRetention bool
	EnableACON           bool

	// Layers 21-25: new 2025/2026 research filters
	EnableMarginalInfoGain   bool
	EnableNearDedup          bool
	EnableCoTCompress        bool
	EnableCodingAgentCtx     bool
	EnablePerceptionCompress bool

	// Layers 26-30: reasoning + agent filters
	EnableLightThinker  bool
	EnableThinkSwitcher bool
	EnableGMSA          bool
	EnableCARL          bool
	EnableSlimInfer     bool

	// Layers 31-45: adaptive reasoning + trajectory filters
	EnableDiffAdapt     bool
	EnableEPiC          bool
	EnableSSDP          bool
	EnableAgentOCR      bool
	EnableS2MAD         bool
	EnableLatentCollab  bool
	EnableGraphCoT      bool
	EnableRoleBudget    bool
	EnableSWEAdaptive   bool
	EnableAgentOCRHist  bool
	EnablePlanBudget    bool
	EnableLightMem      bool
	EnablePathShorten   bool
	EnableJSONSampler   bool
	EnableContextCrunch bool // Merged LogCrunch + DiffCrunch
	EnableSearchCrunch  bool
	EnableStructColl    bool

	// Unified experimental layers (L14-L16)
	EnableEdgeCase  bool // L14: merges L21-L25
	EnableReasoning bool // L15: merges L26-L30
	EnableAdvanced  bool // L16: merges L31-L45

	// Cache
	CacheEnabled bool
	CacheMaxSize int

	// Tier-based configuration (new)
	EnableTiers  bool       // Enable tier-based automatic layer selection
	EnabledTiers []AutoTier // Explicit list of tiers to enable (if empty, auto-select)
}

PipelineConfigWithNestedLayers is a helper type for the new nested config structure. Use this gradually: migrate from flat fields to nested Layers config over time.

type PipelineCoordinator

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

PipelineCoordinator orchestrates the practical 20-layer compression pipeline. Research-based: Combines the best techniques from 120+ research papers worldwide to achieve maximum token reduction for CLI/Agent output.

func NewPipelineCoordinator

func NewPipelineCoordinator(cfg PipelineConfig) *PipelineCoordinator

NewPipelineCoordinator creates a new pipeline coordinator with all configured layers.

func (*PipelineCoordinator) Process

func (p *PipelineCoordinator) Process(input string) (string, *PipelineStats)

Process runs the full compression pipeline with early-exit support.

This is the main entry point for the 20+ layer compression pipeline. It processes input through multiple filter layers to achieve maximum token reduction while preserving semantic meaning.

Pipeline Flow: 1. Pre-filters (TOML-based declarative filters) 2. Pattern learning (EngramLearner) 3. Progressive summarization (TieredSummary for large content) 4. Core layers (1-9): Entropy, Perplexity, AST, Contrastive, etc. 5. Budget enforcement (Layer 10) 6. Semantic layers (11-20): Compaction, H2O, Attention Sink, etc. 7. Post-compensation and finalization

Early Exit Optimization: - Checks if budget is met every 3 layers (configurable) - Skips remaining layers if target compression achieved - Reduces processing time for tight budgets

Parameters:

  • input: The text to compress. Can be any size (streaming for >500K tokens)

Returns:

  • output: The compressed text
  • stats: Detailed statistics including tokens saved per layer

Performance:

  • Time: O(n * L) where n = input size, L = number of layers enabled
  • Typical: 883μs for medium input, 11.6M-32M tokens/s throughput
  • Memory: 698-719 KB per operation
  • Allocations: 58-78 per operation

Thread-safety: Safe for concurrent use (uses thread-safe stats collection)

Configuration:

  • Mode: ModeNone (passthrough), ModeMinimal, ModeAggressive
  • Budget: Target token count (0 = unlimited)
  • Layer enables: Individual layer on/off flags

Example:

pipeline := NewPipelineCoordinator(PipelineConfig{
    Mode: ModeAggressive,
    Budget: 1000,
    EnableEntropy: true,
    EnableH2O: true,
})
output, stats := pipeline.Process(largeText)
fmt.Printf("Saved %d tokens (%.1f%%)\n", stats.TotalSaved, stats.ReductionPercent)

Research: Combines techniques from 120+ papers for optimal compression

type PipelineManager

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

PipelineManager handles resilient large-context processing. Supports streaming for inputs up to 2M tokens with automatic chunking, validation, and failure recovery.

func NewPipelineManager

func NewPipelineManager(cfg ManagerConfig) *PipelineManager

NewPipelineManager creates a new pipeline manager

func (*PipelineManager) Process

func (m *PipelineManager) Process(input string, mode Mode, ctx config.CommandContext) (*ProcessResult, error)

Process processes input with full resilience and large context support. For inputs > StreamThreshold, uses streaming chunk processing.

func (*PipelineManager) ProcessWithBudget

func (m *PipelineManager) ProcessWithBudget(input string, mode Mode, budget int, ctx config.CommandContext) (*ProcessResult, error)

ProcessWithBudget processes with a specific token budget. NOTE: Sets the coordinator budget and calls Process sequentially. In TokMan's CLI context, each invocation is isolated per process, so concurrent budget races are not a practical concern.

func (*PipelineManager) ProcessWithQuery

func (m *PipelineManager) ProcessWithQuery(input string, mode Mode, query string, ctx config.CommandContext) (*ProcessResult, error)

ProcessWithQuery processes with query-aware compression

type PipelinePreset

type PipelinePreset = Tier

PresetConfig for backwards compatibility.

type PipelineRuntimeOptions added in v0.28.0

type PipelineRuntimeOptions struct {
	Mode        Mode
	QueryIntent string
	Budget      int
	LLMEnabled  bool
}

PipelineRuntimeOptions carries per-request overrides for the runtime pipeline.

type PipelineSection

type PipelineSection struct {
	Mode        string `toml:"mode"`
	Budget      int    `toml:"budget"`
	QueryIntent string `toml:"query_intent"`
	LLMEnabled  bool   `toml:"llm_enabled"`
}

PipelineSection holds pipeline configuration.

type PipelineStats

type PipelineStats struct {
	OriginalTokens   int
	FinalTokens      int
	TotalSaved       int
	ReductionPercent float64
	LayerStats       map[string]LayerStat

	CacheHit bool
	// contains filtered or unexported fields
}

PipelineStats holds statistics from the compression pipeline

func ProcessWithPool added in v0.28.0

func ProcessWithPool(input string, config PipelineConfig) (string, *PipelineStats)

ProcessWithPool processes input using pooled coordinator

func (*PipelineStats) AddLayerStatSafe added in v0.28.0

func (s *PipelineStats) AddLayerStatSafe(name string, stat LayerStat)

AddLayerStatSafe adds a layer stat in a thread-safe manner.

func (*PipelineStats) RunningSavedSafe added in v0.28.0

func (s *PipelineStats) RunningSavedSafe() int

RunningSavedSafe returns the running saved count safely.

func (*PipelineStats) String

func (s *PipelineStats) String() string

String returns a formatted summary of pipeline stats

type PlanBudgetFilter added in v0.28.0

type PlanBudgetFilter struct{}

PlanBudgetFilter applies dynamic test-time budget allocation based on input difficulty.

func NewPlanBudgetFilter added in v0.28.0

func NewPlanBudgetFilter() *PlanBudgetFilter

NewPlanBudgetFilter creates the dynamic budget controller filter.

func (*PlanBudgetFilter) Apply added in v0.28.0

func (f *PlanBudgetFilter) Apply(input string, mode Mode) (string, int)

Apply computes difficulty and keeps a matching budgeted subset of lines.

func (*PlanBudgetFilter) Name added in v0.28.0

func (f *PlanBudgetFilter) Name() string

Name returns the filter name.

type PositionAwareFilter

type PositionAwareFilter struct{}

PositionAwareFilter reorders output segments to optimize LLM recall. Based on "LongLLMLingua" (Jiang et al., 2024) - LLMs exhibit "lost in the middle" phenomenon where information in the middle of context is less likely to be recalled.

Strategy: Place high-importance segments at beginning AND end of output.

func NewPositionAwareFilter

func NewPositionAwareFilter() *PositionAwareFilter

NewPositionAwareFilter creates a new position-aware filter.

func (*PositionAwareFilter) Apply

func (f *PositionAwareFilter) Apply(input string, mode Mode) (string, int)

Apply reorders segments to optimize for LLM recall. This filter doesn't save tokens - it improves context quality.

func (*PositionAwareFilter) Name

func (f *PositionAwareFilter) Name() string

Name returns the filter name.

type ProcessResult

type ProcessResult struct {
	Output           string
	OriginalTokens   int
	FinalTokens      int
	SavedTokens      int
	ReductionPercent float64
	LayerStats       map[string]LayerStat
	CacheHit         bool
	Chunks           int
	Validated        bool
	TeeFile          string // If failure occurred
	Warning          string
}

ProcessResult contains the result of processing

type Profile

type Profile = Tier

Backwards compatibility aliases

func ContentProfile

func ContentProfile(input string) Profile

ContentProfile auto-detects the best compression profile based on output content.

type QualityEstimator

type QualityEstimator struct{}

QualityEstimator estimates the quality of compressed output

func NewQualityEstimator

func NewQualityEstimator() *QualityEstimator

NewQualityEstimator creates a new quality estimator

func (*QualityEstimator) EstimateQuality

func (q *QualityEstimator) EstimateQuality(original, compressed string) float64

EstimateQuality estimates the quality of compressed output vs original

type QualityGuardrail added in v0.28.0

type QualityGuardrail struct{}

QualityGuardrail checks whether critical context was preserved.

func NewQualityGuardrail added in v0.28.0

func NewQualityGuardrail() *QualityGuardrail

func (*QualityGuardrail) Validate added in v0.28.0

func (g *QualityGuardrail) Validate(before, after string) GuardrailResult

type QuantumLockFilter added in v0.28.0

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

QuantumLockFilter stabilizes system prompts for KV-cache alignment.

func NewQuantumLockFilter added in v0.28.0

func NewQuantumLockFilter() *QuantumLockFilter

NewQuantumLockFilter creates a new KV-cache alignment filter.

func (*QuantumLockFilter) Apply added in v0.28.0

func (f *QuantumLockFilter) Apply(input string, mode Mode) (string, int)

Apply stabilizes content by replacing dynamic fragments with placeholders.

func (*QuantumLockFilter) Name added in v0.28.0

func (f *QuantumLockFilter) Name() string

type QueryAwareFilter

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

QueryAwareFilter prioritizes output segments based on the agent's query intent. Based on "LongLLMLingua" (Jiang et al., 2024) and "ACON" (Zhang et al., 2024).

Key insight: Different agent tasks need different output segments. A "debug" query needs errors/stack traces, not success messages. A "deploy" query needs status/version, not full logs.

func NewQueryAwareFilter

func NewQueryAwareFilter(query ...string) *QueryAwareFilter

NewQueryAwareFilter creates a new query-aware filter with an optional query.

func (*QueryAwareFilter) Apply

func (f *QueryAwareFilter) Apply(input string, mode Mode) (string, int)

Apply filters output based on query relevance.

func (*QueryAwareFilter) Name

func (f *QueryAwareFilter) Name() string

Name returns the filter name.

func (*QueryAwareFilter) SetQuery

func (f *QueryAwareFilter) SetQuery(query string)

SetQuery sets the query for context-aware filtering

type QueryIntent

type QueryIntent int

QueryIntent represents the type of agent query

const (
	IntentUnknown QueryIntent = iota
	IntentDebug               // Finding errors, failures, crashes
	IntentReview              // Code review, diff analysis
	IntentDeploy              // Deployment status, version info
	IntentSearch              // Finding files, functions, definitions
	IntentTest                // Running/analyzing tests
	IntentBuild               // Build/compilation status
)

type QuestionAwareLayerConfig

type QuestionAwareLayerConfig struct {
	Enabled   bool
	Threshold float64
}

QuestionAwareLayerConfig groups T12 settings. NOTE: Currently unused - reserved for future implementation.

type ReadMode

type ReadMode string

ReadMode represents different file reading strategies. Inspired by lean-ctx's 6 read modes.

const (
	ReadFull       ReadMode = "full"
	ReadMap        ReadMode = "map"
	ReadSignatures ReadMode = "signatures"
	ReadDiff       ReadMode = "diff"
	ReadAggressive ReadMode = "aggressive"
	ReadEntropy    ReadMode = "entropy"
	ReadLines      ReadMode = "lines"
)

type ReadOptions

type ReadOptions struct {
	Mode      ReadMode
	StartLine int
	EndLine   int
	MaxTokens int
	Query     string
}

ReadOptions holds options for reading content.

type ReasoningFilter added in v0.28.0

type ReasoningFilter struct{}

func NewReasoningFilter added in v0.28.0

func NewReasoningFilter() *ReasoningFilter

func (*ReasoningFilter) Apply added in v0.28.0

func (f *ReasoningFilter) Apply(input string, mode Mode) (string, int)

func (*ReasoningFilter) Name added in v0.28.0

func (f *ReasoningFilter) Name() string

type RefactoredCoordinator added in v0.28.0

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

RefactoredCoordinator is the new simplified coordinator

func NewRefactoredCoordinator added in v0.28.0

func NewRefactoredCoordinator(cfg PipelineConfig) *RefactoredCoordinator

NewRefactoredCoordinator creates a refactored coordinator

func (*RefactoredCoordinator) Process added in v0.28.0

func (r *RefactoredCoordinator) Process(input string) (string, *PipelineStats)

Process processes input through the pipeline

type ReversibleStore

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

ReversibleStore stores original outputs indexed by content hash. Claw-compactor style reversible compression. Users can restore any compressed output to its original form.

func NewReversibleStore

func NewReversibleStore() *ReversibleStore

NewReversibleStore creates a store in the tokman data directory.

func (*ReversibleStore) ListRecent

func (s *ReversibleStore) ListRecent(n int) ([]StoredEntry, error)

ListRecent returns the N most recent reversible entries.

func (*ReversibleStore) Restore

func (s *ReversibleStore) Restore(hashPrefix string) (*StoredEntry, error)

Restore retrieves the original output by hash prefix.

func (*ReversibleStore) Store

func (s *ReversibleStore) Store(command, original, compressed string, mode string, budget int, layerStats map[string]int) string

Store saves an original-compressed pair for later restoration.

type RoleBudgetFilter added in v0.28.0

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

RoleBudgetFilter allocates compression budget by multi-agent role.

func NewRoleBudgetFilter added in v0.28.0

func NewRoleBudgetFilter() *RoleBudgetFilter

NewRoleBudgetFilter creates a role-aware budget filter.

func (*RoleBudgetFilter) Apply added in v0.28.0

func (f *RoleBudgetFilter) Apply(input string, mode Mode) (string, int)

Apply keeps more lines from high-priority roles (executor/planner) and trims low-value roles.

func (*RoleBudgetFilter) Name added in v0.28.0

func (f *RoleBudgetFilter) Name() string

Name returns the filter name.

type RuleType added in v0.28.0

type RuleType string

RuleType defines the type of engram rule.

const (
	RuleTypePreserve  RuleType = "preserve"   // Always preserve matching content
	RuleTypeCompress  RuleType = "compress"   // Aggressively compress
	RuleTypeSkipLayer RuleType = "skip_layer" // Skip specific layer for this content
	RuleTypeBoost     RuleType = "boost"      // Boost importance score
	RuleTypeReduce    RuleType = "reduce"     // Reduce importance score
)

type S2MADFilter added in v0.28.0

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

Paper: "S2-MAD: Semantic-Similarity Multi-Agent Debate Compression" NAACL 2025

S2MADFilter detects agreement phrases in multi-agent debate or review outputs ("I agree with", "As X mentioned", "Building on that", "This is correct") and collapses those agreement passages into compact markers while preserving the novel arguments in each agent turn.

Multi-agent debate outputs are common in:

  • LLM self-critique and revision loops
  • Peer-review style agent pipelines
  • RAG reranker debate outputs

The filter operates in two stages:

  1. Passage scoring: each line is checked for agreement/acknowledgement markers. Lines with such markers score near 0; lines with novel claims score near 1.
  2. Agreement run collapsing: consecutive agreement-heavy lines are merged into a single "[agreement: N lines]" marker, preserving surrounding novel content.

func NewS2MADFilter added in v0.28.0

func NewS2MADFilter() *S2MADFilter

NewS2MADFilter creates a new S2-MAD multi-agent debate compression filter.

func (*S2MADFilter) Apply added in v0.28.0

func (f *S2MADFilter) Apply(input string, mode Mode) (string, int)

Apply collapses agreement passages and preserves novel arguments.

func (*S2MADFilter) Name added in v0.28.0

func (f *S2MADFilter) Name() string

Name returns the filter name.

type SSDPFilter added in v0.28.0

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

Paper: "SSDP / Chopping Trees: Pruning Tree-of-Thought Branches for Efficient LLM Inference" NeurIPSW 2025

SSDPFilter detects branching tree-of-thought (ToT) structures in text and prunes redundant or divergent branches, keeping only the most informative path.

Branch detection: sequences starting with markers like:

  • "Option A:", "Option B:", "Alternative:", "Approach 1:", "Approach 2:"
  • "Case 1:", "Case 2:", "Path A:", "Scenario A:"

Pruning strategy:

  1. Similarity pruning: if two branches share >60% vocabulary, drop the shorter one.
  2. Divergence pruning: if a branch's content strongly contradicts the final conclusion (detected by negation+key-term overlap), drop it.
  3. In aggressive mode: keep only the branch with the highest anchor score (error/heading density) — the branch most likely to be the final answer.

func NewSSDPFilter added in v0.28.0

func NewSSDPFilter() *SSDPFilter

NewSSDPFilter creates a new SSDP tree-of-thought branch pruner.

func (*SSDPFilter) Apply added in v0.28.0

func (f *SSDPFilter) Apply(input string, mode Mode) (string, int)

Apply detects ToT branch blocks and prunes redundant ones.

func (*SSDPFilter) Name added in v0.28.0

func (f *SSDPFilter) Name() string

Name returns the filter name.

type SWEAdaptiveLoopFilter added in v0.28.0

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

SWEAdaptiveLoopFilter adds a lightweight self-adaptive prune loop inspired by SWE-Pruner style iterative scoring and retention feedback.

func NewSWEAdaptiveLoopFilter added in v0.28.0

func NewSWEAdaptiveLoopFilter() *SWEAdaptiveLoopFilter

NewSWEAdaptiveLoopFilter creates the adaptive loop filter.

func (*SWEAdaptiveLoopFilter) Apply added in v0.28.0

func (f *SWEAdaptiveLoopFilter) Apply(input string, mode Mode) (string, int)

Apply runs a small iterative pruning loop with progressively tighter budgets.

func (*SWEAdaptiveLoopFilter) Name added in v0.28.0

func (f *SWEAdaptiveLoopFilter) Name() string

Name returns the filter name.

type SafeFilter added in v0.28.0

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

SafeFilter wraps a filter with nil checks and panic recovery. Prevents nil pointer dereferences from crashing the pipeline.

func NewSafeFilter added in v0.28.0

func NewSafeFilter(filter Filter, name string) *SafeFilter

NewSafeFilter creates a nil-safe filter wrapper.

func (*SafeFilter) Apply added in v0.28.0

func (sf *SafeFilter) Apply(input string, mode Mode) (output string, saved int)

Apply safely applies the filter with nil checks and recovery.

func (*SafeFilter) IsNil added in v0.28.0

func (sf *SafeFilter) IsNil() bool

IsNil reports whether the wrapped filter is nil.

func (*SafeFilter) Name added in v0.28.0

func (sf *SafeFilter) Name() string

Name returns the filter name.

type SafePipelineStats added in v0.28.0

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

SafePipelineStats provides thread-safe pipeline statistics. Replaces the unsafe PipelineStats in concurrent contexts.

func NewSafePipelineStats added in v0.28.0

func NewSafePipelineStats(originalTokens int) *SafePipelineStats

NewSafePipelineStats creates a new thread-safe stats tracker.

func (*SafePipelineStats) AddLayerStat added in v0.28.0

func (s *SafePipelineStats) AddLayerStat(name string, stat LayerStat)

AddLayerStat records a layer's statistics thread-safely.

func (*SafePipelineStats) RunningSaved added in v0.28.0

func (s *SafePipelineStats) RunningSaved() int

RunningSaved returns the current tokens saved (thread-safe).

func (*SafePipelineStats) SetFinalResult added in v0.28.0

func (s *SafePipelineStats) SetFinalResult(finalTokens, totalSaved int, reductionPct float64)

SetFinalResult sets the final compression results.

func (*SafePipelineStats) ToPipelineStats added in v0.28.0

func (s *SafePipelineStats) ToPipelineStats() *PipelineStats

ToPipelineStats converts to immutable PipelineStats for reporting.

type SafetySection

type SafetySection struct {
	CheckFilterSafety bool `toml:"check_filter_safety"`
	MaxFilterSize     int  `toml:"max_filter_size"`
	AllowRemote       bool `toml:"allow_remote"`
}

SafetySection holds safety configuration.

type SearchCrunchFilter added in v0.28.0

type SearchCrunchFilter struct{}

SearchCrunchFilter deduplicates repeated search result lines and keeps top unique hits.

func NewSearchCrunchFilter added in v0.28.0

func NewSearchCrunchFilter() *SearchCrunchFilter

func (*SearchCrunchFilter) Apply added in v0.28.0

func (f *SearchCrunchFilter) Apply(input string, mode Mode) (string, int)

func (*SearchCrunchFilter) Name added in v0.28.0

func (f *SearchCrunchFilter) Name() string

type Section added in v0.28.0

type Section struct {
	Heading string `json:"heading"`
	Summary string `json:"summary"`
	Level   int    `json:"level"`
}

Section represents a document section.

type SemanticAnchorConfig

type SemanticAnchorConfig struct {
	// AnchorRatio is the percentage of tokens to select as anchors (0.1 = 10%)
	AnchorRatio float64

	// MinAnchorSpacing is minimum tokens between anchors
	MinAnchorSpacing int

	// EnableAggregation allows non-anchor token aggregation
	EnableAggregation bool

	// PreserveStructure keeps structural tokens as anchors
	PreserveStructure bool
}

SemanticAnchorConfig holds configuration for semantic anchor compression

func DefaultSemanticAnchorConfig

func DefaultSemanticAnchorConfig() SemanticAnchorConfig

DefaultSemanticAnchorConfig returns default configuration

type SemanticAnchorFilter

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

SemanticAnchorFilter implements Layer 19: Semantic-Anchor Compression (SAC style).

Research Source: "SAC: Semantic-Anchor Compression" (2024) Key Innovation: Autoencoding-free compression via anchor selection and aggregation. Results: Higher compression ratios without contextual amnesia.

Methodology: 1. Anchor Detection - Identify high-connectivity tokens (semantic hubs) 2. Information Aggregation - Merge surrounding tokens into anchors 3. Prompt Reorganization - Restructure into anchor-based layout

func NewSemanticAnchorFilter

func NewSemanticAnchorFilter() *SemanticAnchorFilter

NewSemanticAnchorFilter creates a new semantic anchor filter

func NewSemanticAnchorFilterWithConfig

func NewSemanticAnchorFilterWithConfig(cfg SemanticAnchorConfig) *SemanticAnchorFilter

NewSemanticAnchorFilterWithConfig creates a filter with custom config

func (*SemanticAnchorFilter) Apply

func (f *SemanticAnchorFilter) Apply(input string, mode Mode) (string, int)

Apply applies semantic-anchor compression

func (*SemanticAnchorFilter) GetAnchorDensity

func (f *SemanticAnchorFilter) GetAnchorDensity(token string) float64

GetAnchorDensity returns the density score for a token

func (*SemanticAnchorFilter) GetAnchors

func (f *SemanticAnchorFilter) GetAnchors() []AnchorToken

GetAnchors returns all detected anchor tokens

func (*SemanticAnchorFilter) GetStats

GetStats returns compression statistics

func (*SemanticAnchorFilter) Name

func (f *SemanticAnchorFilter) Name() string

Name returns the filter name

type SemanticAnchorLayerConfig

type SemanticAnchorLayerConfig struct {
	Enabled bool
	Ratio   float64
	Spacing int
}

SemanticAnchorLayerConfig groups Layer 19 settings.

type SemanticAnchorStats

type SemanticAnchorStats struct {
	TotalAnchors    int
	TotalAggregated int
	NonAnchorPruned int
	TokensSaved     int
}

SemanticAnchorStats tracks compression statistics

type SemanticChunk

type SemanticChunk struct {
	Type      ChunkType // Type of chunk
	Content   string    // Original content
	Tokens    int       // Token count
	Score     float64   // Importance score (0.0-1.0)
	StartLine int       // Start line in original content
	EndLine   int       // End line in original content
}

SemanticChunk represents a semantic unit for compression

type SemanticChunkConfig

type SemanticChunkConfig struct {
	// ChunkMethod determines how to split content
	ChunkMethod ChunkMethod

	// MinChunkSize is the minimum tokens for a chunk
	MinChunkSize int

	// MaxChunkSize is the maximum tokens for a chunk
	MaxChunkSize int

	// ImportanceThreshold for pruning chunks (0.0-1.0)
	ImportanceThreshold float64

	// PreserveStructure keeps structural markers even in low-importance chunks
	PreserveStructure bool
}

SemanticChunkConfig holds configuration for semantic chunking

func DefaultSemanticChunkConfig

func DefaultSemanticChunkConfig() SemanticChunkConfig

DefaultSemanticChunkConfig returns default configuration

type SemanticChunkFilter

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

Paper: "ChunkKV: Semantic-Preserving KV Compression" — Liu et al., NeurIPS, 2025 https://arxiv.org/abs/2502.00299 Paper: "ChunkKV: Semantic-Preserving KV Compression" — Liu et al., NeurIPS, 2025 https://arxiv.org/abs/2502.00299 SemanticChunkFilter implements Layer 16: Semantic Chunk-based Compression (ChunkKV style).

Research Source: "ChunkKV: Semantic-Guided KV Cache Compression" (NeurIPS 2025) Key Innovation: Move from token-level to chunk-level pruning to preserve semantic coherence. Results: 8.7% precision improvement, 26.5% faster throughput vs token-level methods.

Methodology: 1. Group tokens into semantic chunks (functions, classes, sentences, paragraphs) 2. Score each chunk's importance using conditional perplexity 3. Prune entire chunks (not individual tokens) to preserve structure 4. Reuse chunk indices across layers for efficiency

func NewSemanticChunkFilter

func NewSemanticChunkFilter() *SemanticChunkFilter

NewSemanticChunkFilter creates a new semantic chunk filter

func NewSemanticChunkFilterWithConfig

func NewSemanticChunkFilterWithConfig(cfg SemanticChunkConfig) *SemanticChunkFilter

NewSemanticChunkFilterWithConfig creates a filter with custom config

func (*SemanticChunkFilter) Apply

func (f *SemanticChunkFilter) Apply(input string, mode Mode) (string, int)

Apply applies semantic chunk-based compression

func (*SemanticChunkFilter) Name

func (f *SemanticChunkFilter) Name() string

Name returns the filter name

type SemanticChunkLayerConfig

type SemanticChunkLayerConfig struct {
	Enabled   bool
	Method    string
	MinSize   int
	Threshold float64
}

SemanticChunkLayerConfig groups Layer 16 settings.

type SemanticEquivalence

type SemanticEquivalence struct{}

SemanticEquivalence checks if compressed output preserves meaning. Verify no critical information was lost during compression.

func NewSemanticEquivalence

func NewSemanticEquivalence() *SemanticEquivalence

NewSemanticEquivalence creates a checker.

func (*SemanticEquivalence) Check

func (s *SemanticEquivalence) Check(original, compressed string) EquivalenceReport

Check returns an equivalence report.

type SemanticFilter

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

SemanticFilter prunes low-information segments using statistical analysis. Based on "Selective Context" (Li et al., 2024) - uses self-information and information density to identify low-value content.

func NewSemanticFilter

func NewSemanticFilter() *SemanticFilter

NewSemanticFilter creates a new semantic filter.

func (*SemanticFilter) Apply

func (f *SemanticFilter) Apply(input string, mode Mode) (string, int)

Apply applies semantic pruning to the input.

func (*SemanticFilter) Name

func (f *SemanticFilter) Name() string

Name returns the filter name.

type SemanticFilters added in v0.28.0

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

SemanticFilters manages layers 11-20

func NewSemanticFilters added in v0.28.0

func NewSemanticFilters(cfg PipelineConfig) *SemanticFilters

NewSemanticFilters creates semantic filter set

func (*SemanticFilters) Apply added in v0.28.0

func (s *SemanticFilters) Apply(input string, mode Mode, stats *PipelineStats) string

Apply applies all semantic filters

type SessionConfig

type SessionConfig struct {
	SessionFile string // Path to session file
	MaxEntries  int    // Maximum entries to track (0 = unlimited)
}

SessionConfig holds configuration for the session tracker

type SessionHistory

type SessionHistory struct {
	UserQueries []string `json:"user_queries"`
	ActivityLog []string `json:"activity_log"`
	FilesRead   []string `json:"files_read,omitempty"`
	FilesEdited []string `json:"files_edited,omitempty"`
	CommandsRun []string `json:"commands_run,omitempty"`
	Decisions   []string `json:"decisions,omitempty"`
}

SessionHistory tracks what happened in the session

type SessionStats

type SessionStats struct {
	UniqueEntries    int
	TotalOccurrences int
	MultiOccurrences int
}

SessionStats holds session statistics

type SessionTracker

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

SessionTracker tracks content across commands to avoid repetition. Research-based: Context-Aware Compression (2024) - avoids repeating information already shown to the agent, achieving 5-10% additional reduction.

Key insight: Agents often run similar commands repeatedly. Tracking what has been shown allows collapsing repeated content to "[seen before]" markers.

func NewSessionTracker

func NewSessionTracker() *SessionTracker

NewSessionTracker creates a new session tracker.

func (*SessionTracker) Apply

func (f *SessionTracker) Apply(input string, mode Mode) (string, int)

Apply applies session tracking to avoid repetition.

func (*SessionTracker) Clear

func (f *SessionTracker) Clear() error

Clear clears the session history

func (*SessionTracker) Name

func (f *SessionTracker) Name() string

Name returns the filter name.

func (*SessionTracker) Save

func (f *SessionTracker) Save() error

Save saves session data to file

func (*SessionTracker) Stats

func (f *SessionTracker) Stats() SessionStats

Stats returns session statistics

type Severity added in v0.28.0

type Severity string

Severity defines rule severity.

const (
	SeverityCritical Severity = "critical"
	SeverityHigh     Severity = "high"
	SeverityMedium   Severity = "medium"
	SeverityLow      Severity = "low"
)

type SinkConfig

type SinkConfig struct {
	// Enable attention sink filtering
	Enabled bool

	// Number of initial tokens to always preserve as sinks
	SinkTokenCount int

	// Number of recent tokens to preserve in rolling cache
	RecentTokenCount int

	// Preserve structural markers (headers, prefixes)
	PreserveStructural bool

	// Minimum content length to apply
	MinContentLength int

	// Anchor patterns to always preserve
	AnchorPatterns []string
}

SinkConfig holds configuration for attention sink preservation

func DefaultSinkConfig

func DefaultSinkConfig() SinkConfig

DefaultSinkConfig returns default configuration

type Sketch

type Sketch struct {
	CompressedInfo []byte  // Quantized/low-rank representation
	OriginalHash   string  // For verification
	TokenCount     int     // Original token count
	Importance     float64 // Original importance score
	ContentType    string  // "code", "text", "mixed"
}

Sketch represents a compressed content entry

type SketchCache

type SketchCache struct {
	TokenSketches map[string]*Sketch // hash -> sketch
	Budget        float64
	Stats         SketchStats
}

SketchCache stores compressed representations of pruned content

type SketchEntry

type SketchEntry struct {
	Hash         string
	Content      string
	Sketch       *Sketch
	Revived      bool
	RevivalCount int
}

SketchEntry represents a revivable content block

type SketchStats

type SketchStats struct {
	TotalSketches   int
	TotalCompressed int
	TotalRevived    int
	TokensSaved     int
}

SketchStats tracks compression statistics

type SketchStoreConfig

type SketchStoreConfig struct {
	// BudgetRatio is the target compression ratio (0.1 = 10% budget)
	BudgetRatio float64

	// EnableRecovery allows on-demand reconstruction
	EnableRecovery bool

	// MaxSketchSize limits sketch storage per entry
	MaxSketchSize int

	// HeavyHitterRatio determines what stays uncompressed
	HeavyHitterRatio float64
}

SketchStoreConfig holds configuration for sketch-based compression

func DefaultSketchStoreConfig

func DefaultSketchStoreConfig() SketchStoreConfig

DefaultSketchStoreConfig returns default configuration

type SketchStoreFilter

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

Paper: "KVReviver: Reversible KV Cache Compression" — Yuan et al., 2025 https://arxiv.org/abs/2512.17917 Paper: "KVReviver: Reversible KV Cache Compression" — Yuan et al., 2025 https://arxiv.org/abs/2512.17917 SketchStoreFilter implements Layer 17: Sketch-based Reversible Compression (KVReviver style).

Research Source: "KVReviver: Sketch-based KV Cache Recovery" (December 2025) Key Innovation: On-demand reconstruction of pruned tokens via compressed sketches. Results: 90% memory reduction with identical accuracy at 10% budget.

Methodology: 1. Create sketches (compressed representations) for evicted content 2. Store sketches in a SketchCache for on-demand reconstruction 3. Monitor attention patterns to detect when reconstruction is needed 4. Revive pruned content when required for context

func NewSketchStoreFilter

func NewSketchStoreFilter() *SketchStoreFilter

NewSketchStoreFilter creates a new sketch-based reversible store

func NewSketchStoreFilterWithConfig

func NewSketchStoreFilterWithConfig(cfg SketchStoreConfig) *SketchStoreFilter

NewSketchStoreFilterWithConfig creates a filter with custom config

func (*SketchStoreFilter) Apply

func (f *SketchStoreFilter) Apply(input string, mode Mode) (string, int)

Apply applies sketch-based reversible compression

func (*SketchStoreFilter) Clear

func (f *SketchStoreFilter) Clear()

Clear clears the sketch cache

func (*SketchStoreFilter) ExportSketches

func (f *SketchStoreFilter) ExportSketches() ([]byte, error)

ExportSketches serializes all sketches for persistence

func (*SketchStoreFilter) GetAllSketches

func (f *SketchStoreFilter) GetAllSketches() map[string]*Sketch

GetAllSketches returns all stored sketches

func (*SketchStoreFilter) GetSketch

func (f *SketchStoreFilter) GetSketch(hash string) (*Sketch, bool)

GetSketch returns a sketch by hash

func (*SketchStoreFilter) GetStats

func (f *SketchStoreFilter) GetStats() SketchStats

GetStats returns compression statistics

func (*SketchStoreFilter) ImportSketches

func (f *SketchStoreFilter) ImportSketches(data []byte) error

ImportSketches loads sketches from serialized data

func (*SketchStoreFilter) Name

func (f *SketchStoreFilter) Name() string

Name returns the filter name

func (*SketchStoreFilter) Revive

func (f *SketchStoreFilter) Revive(sketchHash string) (string, bool)

Revive reconstructs content from a sketch

type SketchStoreLayerConfig

type SketchStoreLayerConfig struct {
	Enabled     bool
	BudgetRatio float64
	MaxSize     int
	HeavyHitter float64
}

SketchStoreLayerConfig groups Layer 17 settings.

type SlimInferFilter added in v0.28.0

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

Paper: "SlimInfer: Accelerating Long-Context LLM Inference via Dynamic Token Pruning" arXiv:2508.06447 — 2025

SlimInferFilter implements the core insight from SlimInfer: drop "orphan" lines — lines whose terms do not appear in any other line of the output.

This is the complement to MarginalInfoGainFilter:

MIG  — drops lines that contribute NO NEW terms to the retained set
Slim — drops lines whose terms are REFERENCED BY NO OTHER line

Together they remove both ends of the information graph:

  • Lines that are fully covered by others (MIG)
  • Lines that are fully isolated from others (Slim)

Algorithm:

  1. Build a term → {line indices} inverted index
  2. For each line, compute refScore = number of OTHER lines that share ≥1 term
  3. Lines with refScore < threshold are "orphans" → prune
  4. Structural lines (errors, headings, first/last) are always kept

Threshold:

ModeMinimal:   refScore ≥ 1  (at least one other line shares a term)
ModeAggressive: refScore ≥ 2  (at least two other lines share a term)

func NewSlimInferFilter added in v0.28.0

func NewSlimInferFilter() *SlimInferFilter

NewSlimInferFilter creates a new SlimInfer orphan-line pruner.

func (*SlimInferFilter) Apply added in v0.28.0

func (f *SlimInferFilter) Apply(input string, mode Mode) (string, int)

Apply drops orphan lines (low inter-line term reference count).

func (*SlimInferFilter) Name added in v0.28.0

func (f *SlimInferFilter) Name() string

Name returns the filter name.

type SmallKVCompensator

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

Paper: "SmallKV: Small Model Assisted Compensation" — Zhao et al., NeurIPS, 2025 https://arxiv.org/abs/2508.02751 Paper: "SmallKV: Small Model Assisted Compensation" — Zhao et al., NeurIPS, 2025 https://arxiv.org/abs/2508.02751 SmallKVCompensator implements SmallKV-style small model compensation. Research Source: "SmallKV: Small Model Assisted Compensation of KV Cache Compression for Efficient LLM Inference" (2025) Key Innovation: When aggressive compression removes important tokens, use a lightweight reconstruction pass to compensate for lost information.

This works by: after compression, check if critical information patterns were broken (unclosed brackets, incomplete statements, missing context) and reconstruct minimal bridges to maintain coherence.

func NewSmallKVCompensator

func NewSmallKVCompensator() *SmallKVCompensator

NewSmallKVCompensator creates a new SmallKV compensator

func (*SmallKVCompensator) Apply

func (s *SmallKVCompensator) Apply(input string, mode Mode) (string, int)

Apply implements the Filter interface for pipeline integration

func (*SmallKVCompensator) Compensate

func (s *SmallKVCompensator) Compensate(original, compressed string, mode Mode) string

Compensate adds bridge tokens to compensate for over-compression. This runs AFTER other filters to repair damage from aggressive compression.

func (*SmallKVCompensator) Name

func (s *SmallKVCompensator) Name() string

Name returns the filter name

type SmallKVConfig

type SmallKVConfig struct {
	// Enabled controls whether the compensator is active
	Enabled bool

	// MinContentLength minimum chars to apply
	MinContentLength int

	// MaxBridgeTokens maximum tokens to add as compensation
	MaxBridgeTokens int

	// CheckSyntaxIntegrity verifies bracket/paren matching
	CheckSyntaxIntegrity bool

	// CheckContextContinuity verifies logical flow preservation
	CheckContextContinuity bool
}

SmallKVConfig holds configuration for SmallKV compensation

func DefaultSmallKVConfig

func DefaultSmallKVConfig() SmallKVConfig

DefaultSmallKVConfig returns default configuration

type SnapshotContext

type SnapshotContext struct {
	Critical    []string          `json:"critical"`  // Must preserve (can't rediscover)
	Working     []string          `json:"working"`   // Summarized knowledge
	KeyValue    map[string]string `json:"key_value"` // Extracted facts
	CodeContext []CodeContext     `json:"code_context,omitempty"`
}

SnapshotContext preserves important knowledge

type StateSnapshot

type StateSnapshot struct {
	SessionHistory SessionHistory  `json:"session_history"`
	CurrentState   CurrentState    `json:"current_state"`
	Context        SnapshotContext `json:"context"`
	PendingPlan    []Milestone     `json:"pending_plan"`
}

StateSnapshot represents semantic compaction output

type StoredEntry

type StoredEntry struct {
	Hash         string         `json:"hash"`
	Command      string         `json:"command"`
	Original     string         `json:"original"`
	Compressed   string         `json:"compressed"`
	OriginalHash string         `json:"original_hash"`
	Mode         string         `json:"mode"`
	Budget       int            `json:"budget"`
	Timestamp    time.Time      `json:"timestamp"`
	LayerStats   map[string]int `json:"layer_stats,omitempty"`
}

StoredEntry holds a reversible compression entry.

type StreamConfig added in v0.28.0

type StreamConfig struct {
	Enabled        bool
	Threshold      int
	ChunkSize      int
	ParallelChunks bool // Future: process chunks in parallel
}

StreamConfig holds configuration for streaming mode.

func DefaultStreamConfig added in v0.28.0

func DefaultStreamConfig() StreamConfig

DefaultStreamConfig returns the default streaming configuration.

type StreamingProcessor

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

StreamingProcessor handles large inputs by processing in chunks.

func NewStreamingProcessor

func NewStreamingProcessor(coordinator *PipelineCoordinator) *StreamingProcessor

NewStreamingProcessor creates a new streaming processor.

func (*StreamingProcessor) ProcessStream

func (sp *StreamingProcessor) ProcessStream(input string) (string, *PipelineStats)

ProcessStream processes large input in chunks. This reduces memory usage for very large inputs.

func (*StreamingProcessor) ProcessStreamReader added in v0.28.0

func (sp *StreamingProcessor) ProcessStreamReader(reader io.Reader, writer io.Writer) error

ProcessStreamReader processes input from a reader in streaming mode.

type StructuralCollapse

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

StructuralCollapse merges import blocks and collapses repeated patterns. Inspired by claw-compactor's StructuralCollapse stage.

func NewStructuralCollapse

func NewStructuralCollapse(cfg StructuralCollapseConfig) *StructuralCollapse

NewStructuralCollapse creates a new StructuralCollapse filter.

func (*StructuralCollapse) Process

func (sc *StructuralCollapse) Process(content string) (string, int)

Process collapses structural patterns.

type StructuralCollapseConfig

type StructuralCollapseConfig struct {
	Enabled         bool
	CollapseImports bool
	CollapseAsserts bool
	MaxRepeated     int
}

StructuralCollapseConfig holds configuration.

func DefaultStructuralCollapseConfig

func DefaultStructuralCollapseConfig() StructuralCollapseConfig

DefaultStructuralCollapseConfig returns default configuration.

type StructuralCollapseFilter added in v0.28.0

type StructuralCollapseFilter struct{}

StructuralCollapseFilter compacts repetitive structural boilerplate while preserving semantic anchors.

func NewStructuralCollapseFilter added in v0.28.0

func NewStructuralCollapseFilter() *StructuralCollapseFilter

func (*StructuralCollapseFilter) Apply added in v0.28.0

func (f *StructuralCollapseFilter) Apply(input string, mode Mode) (string, int)

func (*StructuralCollapseFilter) Name added in v0.28.0

func (f *StructuralCollapseFilter) Name() string

type SummaryTier added in v0.28.0

type SummaryTier int

SummaryTier defines the level of summarization.

const (
	TierL0 SummaryTier = iota // Surface: keywords, entities
	TierL1                    // Structural: sections, outlines
	TierL2                    // Deep: full semantic compression
)

type TDDConfig

type TDDConfig struct {
	Enabled         bool
	MinSavings      int
	MaxReplacements int
}

TDDConfig holds configuration for Token Dense Dialect.

func DefaultTDDConfig

func DefaultTDDConfig() TDDConfig

DefaultTDDConfig returns default TDD configuration.

type TDDStats

type TDDStats struct {
	Replacements  int
	OriginalLen   int
	CompressedLen int
	SavingsPct    float64
}

TDDStats holds encoding statistics.

type TFIDFLayerConfig

type TFIDFLayerConfig struct {
	Enabled   bool
	Threshold float64
}

TFIDFLayerConfig groups TF-IDF filter settings.

type TOMLPipelineConfig

type TOMLPipelineConfig struct {
	Pipeline PipelineSection `toml:"pipeline"`
	Layers   LayersSection   `toml:"layers"`
	Safety   SafetySection   `toml:"safety"`
}

TOMLPipelineConfig holds TOML-based pipeline configuration.

type TOONConfig

type TOONConfig struct {
	Enabled          bool
	MinArrayLength   int
	MaxColumns       int
	PruneMetadata    bool
	StripLineNumbers bool
}

TOONConfig holds configuration for TOON encoding.

func DefaultTOONConfig

func DefaultTOONConfig() TOONConfig

DefaultTOONConfig returns default TOON configuration.

type TOONEncoder

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

TOONEncoder implements columnar encoding for homogeneous JSON arrays. Inspired by kompact and tamp's TOON encoding. Achieves 40-80% compression on structured data like file listings, deps, routes.

func NewTOONEncoder

func NewTOONEncoder(cfg TOONConfig) *TOONEncoder

NewTOONEncoder creates a new TOON encoder.

func (*TOONEncoder) Encode

func (e *TOONEncoder) Encode(input string) (string, int, int, bool)

Encode compresses a JSON array using columnar encoding. Returns (compressed, originalTokens, compressedTokens, isTOON).

type TaskRunnerWrapping

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

TaskRunnerWrapping wraps task runner recipes for individual line filtering. Inspired by tokf's task runner wrapping.

func NewTaskRunnerWrapping

func NewTaskRunnerWrapping(runner, filterCmd string) *TaskRunnerWrapping

NewTaskRunnerWrapping creates a new task runner wrapper.

func (*TaskRunnerWrapping) Wrap

func (trw *TaskRunnerWrapping) Wrap(content string) string

Wrap wraps a Makefile or Justfile for tokman filtering.

type TemplatePipe

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

TemplatePipe implements template pipe chains for filter output processing.

func NewTemplatePipe

func NewTemplatePipe(chain string) *TemplatePipe

NewTemplatePipe creates a new template pipe from a pipe chain string.

func (*TemplatePipe) Process

func (tp *TemplatePipe) Process(input string) string

Process applies the pipe chain to input.

type TestInput added in v0.28.0

type TestInput struct {
	Name        string
	Content     string
	ContentType string
	ExpectedMin float64 // Minimum expected compression ratio
	ExpectedMax float64 // Maximum expected compression ratio
}

TestInput represents a test case for benchmarking.

func GetBuiltinTestInputs added in v0.28.0

func GetBuiltinTestInputs() []TestInput

GetBuiltinTestInputs returns a set of standard test inputs.

type ThinkSwitcherFilter added in v0.28.0

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

Papers:

"ThinkSwitcher: When to Think Hard, When to Think Fast" — EMNLP 2025
"Thinkless: LLM Learns When to Think" — NeurIPS 2025 (VainF/Thinkless)

ThinkSwitcherFilter is a meta-routing filter: it measures the "reasoning density" of an output (fraction of lines that look like deliberate reasoning) and routes to the appropriate compression level.

Three paths:

fast   — reasoning density < fastThreshold: no reasoning detected,
          pass through unchanged (avoids wasted CPU on direct answers)
light  — fastThreshold ≤ density < heavyThreshold: some reasoning,
          compress to 50% of reasoning lines keeping key sentences
heavy  — density ≥ heavyThreshold: heavy reasoning present,
          collapse to a one-line summary per reasoning block

Key insight from ThinkSwitcher: the majority of LLM outputs need no CoT compression at all. Running compression unconditionally wastes resources and can degrade quality by removing relevant content from direct answers.

func NewThinkSwitcherFilter added in v0.28.0

func NewThinkSwitcherFilter() *ThinkSwitcherFilter

NewThinkSwitcherFilter creates a new ThinkSwitcher routing filter.

func (*ThinkSwitcherFilter) Apply added in v0.28.0

func (f *ThinkSwitcherFilter) Apply(input string, mode Mode) (string, int)

Apply routes compression based on detected reasoning density.

func (*ThinkSwitcherFilter) Name added in v0.28.0

func (f *ThinkSwitcherFilter) Name() string

Name returns the filter name.

type Tier

type Tier string

Tier defines the depth of the compression pipeline. Higher tiers activate more layers for deeper compression.

const (
	// Tier 1: Surface — 4 consolidated layers for speed
	// L1: TokenPrune + L3: CodeStructure + L7: Budget + L17: ContentDetect
	TierSurface Tier = "surface" // 4 layers, 30-50% reduction

	// Tier 2: Trim — 8 consolidated layers for balanced compression
	// L1-L3, L5, L7-L10
	TierTrim Tier = "trim" // 8 layers, 50-70% reduction

	// Tier 3: Extract — 20 consolidated layers for maximum compression
	// All L1-L20 layers
	TierExtract Tier = "extract" // 20 layers, 70-90% reduction

	// Tier 4: Core — practical 20-layer production profile
	TierCore Tier = "core" // 20 layers, quality-first compression

	// Tier C: Code — code-aware with structure preservation
	TierCode Tier = "code" // 6 layers, 50-70% reduction

	// Tier L: Log — log-aware with deduplication
	TierLog Tier = "log" // 5 layers, 60-80% reduction

	// Tier T: Thread — conversation-aware context preservation
	TierThread Tier = "thread" // 5 layers, 55-75% reduction

	// Tier A: Adaptive — dynamic path based on content type
	TierAdaptive Tier = "adaptive" // dynamic, quality-first
)
const (
	ProfileFast     Tier = TierSurface
	ProfileBalanced Tier = TierTrim
	ProfileCode     Tier = TierCode
	ProfileLog      Tier = TierLog
	ProfileChat     Tier = TierThread
	ProfileMax      Tier = TierCore

	ModeSkim       Tier = TierSurface
	ModeRefine     Tier = TierTrim
	ModeDistill    Tier = TierExtract
	ModeAnnihilate Tier = TierCore
)
const (
	PresetFast     Tier = TierSurface
	PresetBalanced Tier = TierTrim
	PresetFull     Tier = TierCore
	PresetAuto     Tier = ""
)

type TieredResult added in v0.28.0

type TieredResult struct {
	L0 *L0Summary
	L1 *L1Summary
	L2 *L2Summary
}

TieredResult holds summaries at all three levels.

type TieredSummaryFilter added in v0.28.0

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

TieredSummaryFilter implements L0/L1/L2 progressive summarization. L0: Surface summary (keywords, topics) L1: Structural summary (sections, relationships) L2: Deep summary (full semantic compression)

func NewTieredSummaryFilter added in v0.28.0

func NewTieredSummaryFilter() *TieredSummaryFilter

NewTieredSummaryFilter creates a new tiered summary filter.

func (*TieredSummaryFilter) Apply added in v0.28.0

func (tsf *TieredSummaryFilter) Apply(input string, mode Mode) (string, int)

Apply generates tiered summaries from input.

func (*TieredSummaryFilter) GenerateTiers added in v0.28.0

func (tsf *TieredSummaryFilter) GenerateTiers(input string) *TieredResult

GenerateTiers creates all three summary levels.

func (*TieredSummaryFilter) Name added in v0.28.0

func (tsf *TieredSummaryFilter) Name() string

Name returns the filter name.

type Token

type Token struct {
	Text     string
	Score    float64
	Position int
	Layer    int
	Pruned   bool
	Revived  bool
}

Token represents a scored token for pruning decisions

type TokenDenseDialect

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

TokenDenseDialect implements symbol shorthand for compact LLM communication. Inspired by lean-ctx's Token Dense Dialect (TDD). Replaces common programming terms with Unicode symbols for 8-25% extra savings.

func NewTokenDenseDialect

func NewTokenDenseDialect(cfg TDDConfig) *TokenDenseDialect

NewTokenDenseDialect creates a new TDD encoder.

func (*TokenDenseDialect) Decode

func (tdd *TokenDenseDialect) Decode(input string) string

Decode restores original terms from symbols.

func (*TokenDenseDialect) Encode

func (tdd *TokenDenseDialect) Encode(input string) (string, int)

Encode replaces common terms with Unicode symbols.

func (*TokenDenseDialect) EncodeWithStats

func (tdd *TokenDenseDialect) EncodeWithStats(input string) (string, TDDStats)

EncodeWithStats encodes and returns statistics.

type Turn

type Turn struct {
	Role      string // "user" or "assistant"
	Content   string
	Timestamp time.Time
	Hash      string
	Tokens    int
}

Turn represents a single conversation turn

Source Files

Jump to

Keyboard shortcuts

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