filter

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

BlockDelimiters for brace tracking

View Source
var CommentPatternsMap = map[Language]*regexp.Regexp{
	LangGo:         regexp.MustCompile(`(?m)^//.*$|/\*[\s\S]*?\*/`),
	LangRust:       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]*?\*/`),
	LangShell:      regexp.MustCompile(`(?m)^#.*$`),
	LangRuby:       regexp.MustCompile(`(?m)^#.*$|=begin[\s\S]*?=end`),
	LangSQL:        regexp.MustCompile(`(?m)^--.*$`),
}

CommentPatternsMap maps languages to their comment regex patterns

View Source
var DiffHunkPattern = regexp.MustCompile(`^@@\s+-\d+(?:,\d+)?\s+\+\d+(?:,\d+)?\s+@@`)

DiffHunkPattern

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 LogTimestampPatterns = []*regexp.Regexp{
	regexp.MustCompile(`^\d{4}-\d{2}-\d{2}[T\s]\d{2}:\d{2}:\d{2}`),
	regexp.MustCompile(`^\[\d{4}-\d{2}-\d{2}`),
	regexp.MustCompile(`^\d{2}:\d{2}:\d{2}`),
}

LogTimestampPatterns

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 aggressive filtering

View Source
var TestResultPatterns = []*regexp.Regexp{
	regexp.MustCompile(`test result: (ok|FAILED|ignored)\.`),
	regexp.MustCompile(`(\d+) passed`),
	regexp.MustCompile(`(\d+) failed`),
	regexp.MustCompile(`(\d+) ignored`),
	regexp.MustCompile(`PASS`),
	regexp.MustCompile(`FAIL`),
	regexp.MustCompile(`ok\s+\S+\s+[\d.]+s`),
	regexp.MustCompile(`(\d+) passed`),
	regexp.MustCompile(`(\d+) failed`),
	regexp.MustCompile(`(\d+) skipped`),
}

TestResultPatterns

Functions

func Aggregate added in v1.2.0

func Aggregate(input string) string

Aggregate is a utility function to aggregate log output.

func CalculateTokensSaved added in v1.2.0

func CalculateTokensSaved(original, filtered string) int

CalculateTokensSaved computes token savings between original and filtered.

func DetectLanguage

func DetectLanguage(output string) string

DetectLanguage attempts to detect the programming language from output.

func EstimateTokens

func EstimateTokens(text string) int

EstimateTokens provides a heuristic token count. Uses the formula: ceil(text.length / 4.0)

func EstimateTokensForBudget added in v1.2.0

func EstimateTokensForBudget(text string) int

EstimateTokens provides a heuristic token count. Uses the formula: ceil(text.length / 4.0)

func GroupLines added in v1.2.0

func GroupLines(input string) string

GroupLines groups lines by common prefix or pattern.

func HasANSI

func HasANSI(input string) bool

HasANSI checks if the input contains ANSI escape sequences.

func IsCode

func IsCode(output string) bool

IsCode checks if the output looks like source code.

func QuickProcess

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

QuickProcess is a convenience function for simple compression

func QuickProcessFull added in v1.2.0

func QuickProcessFull(input string, mode Mode, query string, budget int, llmEnabled bool) (string, int)

QuickProcessFull is a convenience function with all options

func QuickProcessWithBudget added in v1.2.0

func QuickProcessWithBudget(input string, mode Mode, budget int) (string, int)

QuickProcessWithBudget is a convenience function for budgeted compression

func QuickProcessWithQuery added in v1.2.0

func QuickProcessWithQuery(input string, mode Mode, query string) (string, int)

QuickProcessWithQuery is a convenience function for query-aware compression

func SaveExamplePlugins added in v1.2.0

func SaveExamplePlugins(dir string) error

SaveExamplePlugins saves example plugin configs to the specified directory.

func StripANSI

func StripANSI(input string) string

StripANSI is a utility function to strip ANSI codes from a string.

func StripBodies added in v1.2.0

func StripBodies(code string, lang string, mode Mode) string

StripBodies is a utility function to strip function bodies.

func StripComments added in v1.2.0

func StripComments(code string, lang Language) string

StripComments is a utility function to strip comments from code.

Types

type ANSIFilter

type ANSIFilter struct{}

ANSIFilter strips ANSI escape sequences from output.

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
}

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)

Research Results: 4-8x compression while maintaining parseable 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

type AdaptiveAttentionFilter added in v1.2.0

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

AdaptiveAttentionFilter implements ADSC-style compression (Berkeley/Clemson, 2025). Attention-driven self-compression that mimics LLM attention patterns.

Algorithm: 1. Simulate attention scoring for each token/line 2. Apply progressive downsampling based on attention scores 3. Use uniform downsampling for low-attention regions 4. Preserve high-attention regions in full detail

Research Results: 66.7% - 88.9% compression with 98.2% quality preservation. Key insight: Attention patterns reveal token importance.

func NewAdaptiveAttentionFilter added in v1.2.0

func NewAdaptiveAttentionFilter() *AdaptiveAttentionFilter

NewAdaptiveAttentionFilter creates a new adaptive attention filter

func (*AdaptiveAttentionFilter) Apply added in v1.2.0

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

Apply applies adaptive attention compression

func (*AdaptiveAttentionFilter) Name added in v1.2.0

func (f *AdaptiveAttentionFilter) Name() string

Name returns the filter name

func (*AdaptiveAttentionFilter) SetDownsampleRatio added in v1.2.0

func (f *AdaptiveAttentionFilter) SetDownsampleRatio(ratio float64)

SetDownsampleRatio sets the downsampling ratio

func (*AdaptiveAttentionFilter) SetThresholds added in v1.2.0

func (f *AdaptiveAttentionFilter) SetThresholds(high, low float64)

SetThresholds sets the attention thresholds

type AttentionSinkFilter

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

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 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]interface{}

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 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

	// Enable caching of importance scores
	CacheEnabled bool

	// 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 ProCut-style 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]interface{}

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 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 CachedResult

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

CachedResult represents a cached compression result

type CodeContext

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

CodeContext preserves code-specific context

type CommandContext

type CommandContext struct {
	Command    string // "git", "npm", "cargo", etc.
	Subcommand string // "status", "test", "build"
	ExitCode   int    // Non-zero = likely has errors
	Intent     string // "debug", "review", "deploy", "search"
	IsTest     bool   // Test output detection
	IsBuild    bool   // Build output detection
	IsError    bool   // Error output detection
}

CommandContext provides metadata about the command being executed. Used for intelligent filtering decisions.

type CommentFilter

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

CommentFilter strips comments from source code.

func NewCommentFilter added in v1.2.0

func NewCommentFilter() *CommentFilter

NewCommentFilter creates a new comment filter.

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 CommentPatterns

type CommentPatterns struct {
	Line       string
	BlockStart string
	BlockEnd   string
	DocLine    string
	DocBlock   string
}

CommentPatterns represents comment structure for a language

func CommentPatternsForLang added in v1.2.0

func CommentPatternsForLang(lang Language) CommentPatterns

CommentPatternsForLang returns comment patterns for a language

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
}

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]interface{}

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 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

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

func (*CompressionCache) Size

func (c *CompressionCache) Size() int

Size returns the number of cached entries

type ContrastiveFilter

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

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 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 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 Engine

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

Engine combines multiple filters.

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 NewEngineWithLLM added in v1.2.0

func NewEngineWithLLM(mode Mode, queryIntent string, llmEnabled bool) *Engine

NewEngineWithLLM creates a filter engine with LLM-aware compression enabled. Falls back to heuristic-based filters if LLM is unavailable.

func NewEngineWithLLMAndConfig added in v1.2.0

func NewEngineWithLLMAndConfig(cfg EngineConfig) *Engine

NewEngineWithLLMAndConfig creates a fully configured engine with LLM support.

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) ProcessWithLang

func (e *Engine) ProcessWithLang(input string, lang string) (string, int)

ModeNone = raw passthrough

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 EntropyFilter

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

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.

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

func NewEntropyFilter

func NewEntropyFilter() *EntropyFilter

NewEntropyFilter creates a new entropy-based filter

func (*EntropyFilter) Apply

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

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

func (*EntropyFilter) Name

func (f *EntropyFilter) Name() string

Name returns the filter name

func (*EntropyFilter) SetThreshold

func (f *EntropyFilter) SetThreshold(threshold float64)

SetThreshold allows customizing the entropy threshold

type ErrorTraceFilter added in v1.2.0

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

ErrorTraceFilter compresses error stack traces to essential information. Research-based: Error Message Compression (2024) - achieves 4x compression by showing only: error type, first user file, line number, and message.

Key insight: Stack traces are often 50+ lines, but only 2-3 lines are actionable. Framework internals, standard library paths, and repetitive frames can be collapsed.

func NewErrorTraceFilter added in v1.2.0

func NewErrorTraceFilter() *ErrorTraceFilter

NewErrorTraceFilter creates a new error trace compressor.

func (*ErrorTraceFilter) Apply added in v1.2.0

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

Apply compresses error traces in the output.

func (*ErrorTraceFilter) Name added in v1.2.0

func (f *ErrorTraceFilter) Name() string

Name returns the filter name.

type EvaluatorHeadsFilter

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

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 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.

type GistFilter

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

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
}

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 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
}

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

func (*H2OFilter) GetStats

func (h *H2OFilter) GetStats() map[string]interface{}

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 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)

Apply applies hierarchical summarization to large outputs.

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
}

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 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 LLMAwareConfig

type LLMAwareConfig struct {
	Threshold      int
	Enabled        bool
	CacheEnabled   bool
	PromptTemplate string // Template name for intent-specific summarization
}

LLMAwareConfig holds configuration for the LLM-aware filter

type LLMAwareFilter

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

LLMAwareFilter uses local LLM for high-quality summarization. This filter is optional and only activates when: 1. An LLM provider is available (Ollama, LM Studio, etc.) 2. The content exceeds the threshold for LLM-based processing 3. The user has enabled LLM mode via flag or config

Research basis: "LLM-based Context Compression" shows 40-60% better semantic preservation compared to heuristic-only approaches.

func NewLLMAwareFilter

func NewLLMAwareFilter(cfg LLMAwareConfig) *LLMAwareFilter

NewLLMAwareFilter creates a new LLM-aware filter

func (*LLMAwareFilter) Apply

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

Apply applies LLM-based summarization if available, otherwise falls back to heuristic

func (*LLMAwareFilter) GetModel

func (f *LLMAwareFilter) GetModel() string

GetModel returns the current model name

func (*LLMAwareFilter) GetProvider

func (f *LLMAwareFilter) GetProvider() string

GetProvider returns the current LLM provider name

func (*LLMAwareFilter) IsAvailable

func (f *LLMAwareFilter) IsAvailable() bool

IsAvailable returns true if LLM is available

func (*LLMAwareFilter) Name

func (f *LLMAwareFilter) Name() string

Name returns the filter name

func (*LLMAwareFilter) SetEnabled

func (f *LLMAwareFilter) SetEnabled(enabled bool)

SetEnabled enables or disables LLM mode

func (*LLMAwareFilter) SummarizeWithIntent

func (f *LLMAwareFilter) SummarizeWithIntent(content string, intent string) (string, int)

SummarizeWithIntent provides intent-aware summarization

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

type LayerStat

type LayerStat struct {
	TokensSaved int
}

LayerStat holds statistics for a single layer

type LogAggregator added in v1.2.0

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

LogAggregator deduplicates and compresses log output.

func NewLogAggregator added in v1.2.0

func NewLogAggregator() *LogAggregator

NewLogAggregator creates a new log aggregator.

func (*LogAggregator) Apply added in v1.2.0

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

Apply aggregates log output and returns token savings.

func (*LogAggregator) Name added in v1.2.0

func (f *LogAggregator) Name() string

Name returns the filter name.

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 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 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 PerplexityFilter

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

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

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 PipelineConfig

type PipelineConfig struct {
	// Mode: none, minimal, aggressive
	Mode Mode

	// Query intent for query-aware compression
	QueryIntent string

	// Token budget (0 = unlimited)
	Budget int

	// Enable LLM-based compression
	LLMEnabled bool

	// Enable session tracking
	SessionTracking bool

	// Enable N-gram abbreviation
	NgramEnabled bool

	// Enable multi-file optimization
	MultiFileEnabled bool

	// Prompt template for LLM
	PromptTemplate string

	// Enable specific layers (all enabled by default)
	EnableEntropy      bool
	EnablePerplexity   bool
	EnableGoalDriven   bool
	EnableAST          bool
	EnableContrastive  bool
	EnableEvaluator    bool
	EnableGist         bool
	EnableHierarchical bool

	// Layer 11: Compaction (Semantic compression)
	EnableCompaction        bool
	CompactionThreshold     int
	CompactionPreserveTurns int
	CompactionMaxTokens     int
	CompactionStateSnapshot bool
	CompactionAutoDetect    bool

	// Layer 12: Attribution Filter (ProCut-style pruning)
	EnableAttribution    bool
	AttributionThreshold float64

	// Layer 13: H2O Filter (Heavy-Hitter Oracle)
	EnableH2O          bool
	H2OSinkSize        int
	H2ORecentSize      int
	H2OHeavyHitterSize int

	// Layer 14: Attention Sink Filter (StreamingLLM-style)
	EnableAttentionSink  bool
	AttentionSinkCount   int
	AttentionRecentCount int
}

PipelineConfig holds configuration for the compression pipeline

type PipelineCoordinator

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

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

Layer order is critical - each layer builds on the previous:

Layer 1: Entropy Filtering (Selective Context, Mila 2023) - 2-3x Layer 2: Perplexity Pruning (LLMLingua, Microsoft/Tsinghua 2023) - 20x Layer 3: Goal-Driven Selection (SWE-Pruner, Shanghai Jiao Tong 2025) - 14.8x Layer 4: AST Preservation (LongCodeZip, NUS 2025) - 4-8x Layer 5: Contrastive Ranking (LongLLMLingua, Microsoft 2024) - 4-10x Layer 6: N-gram Abbreviation (CompactPrompt 2025) - 2.5x Layer 7: Evaluator Heads (EHPC, Tsinghua/Huawei 2025) - 5-7x Layer 8: Gist Compression (Stanford/Berkeley 2023) - 20x+ Layer 9: Hierarchical Summary (AutoCompressor, Princeton/MIT 2023) - Extreme Layer 10: Budget Enforcement (Industry standard) - Guaranteed Layer 11: Compaction Layer (Semantic compression) - Auto Layer 12: Attribution Filter (ProCut, LinkedIn 2025) - 78% Layer 13: H2O Filter (Heavy-Hitter Oracle, NeurIPS 2023) - 30x+ Layer 14: Attention Sink Filter (StreamingLLM, 2023) - Infinite context stability

func NewPipelineCoordinator

func NewPipelineCoordinator(cfg PipelineConfig) *PipelineCoordinator

NewPipelineCoordinator creates a new 10-layer pipeline coordinator.

func (*PipelineCoordinator) Process

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

Process runs the full 10-layer compression pipeline.

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) GetStats added in v1.2.0

func (m *PipelineManager) GetStats() map[string]interface{}

GetStats returns pipeline statistics

func (*PipelineManager) Process

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

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

func (*PipelineManager) ProcessFile added in v1.2.0

func (m *PipelineManager) ProcessFile(path string, mode Mode, ctx CommandContext) (*ProcessResult, error)

ProcessFile processes a file with streaming for large files

func (*PipelineManager) ProcessWithBudget

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

ProcessWithBudget processes with a specific token budget

func (*PipelineManager) ProcessWithQuery

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

ProcessWithQuery processes with query-aware compression

type PipelineStats

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

PipelineStats holds statistics from the compression pipeline

func (*PipelineStats) String

func (s *PipelineStats) String() string

String returns a formatted summary of pipeline stats

type PluginConfig added in v1.2.0

type PluginConfig struct {
	Name         string            `json:"name"`
	Description  string            `json:"description"`
	Enabled      bool              `json:"enabled"`
	Patterns     []string          `json:"patterns"`
	Replacements map[string]string `json:"replacements"`
	Mode         string            `json:"mode"` // "hide" or "replace"
}

PluginConfig defines the structure for filter plugins in config.

func ExamplePluginConfigs added in v1.2.0

func ExamplePluginConfigs() []PluginConfig

ExamplePluginConfigs returns example plugin configurations.

type PluginFilter added in v1.2.0

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

PluginFilter represents a user-defined filter loaded from config.

func NewPluginFilter added in v1.2.0

func NewPluginFilter(cfg PluginConfig) (*PluginFilter, error)

NewPluginFilter creates a filter from plugin config.

func (*PluginFilter) Apply added in v1.2.0

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

func (*PluginFilter) Description added in v1.2.0

func (f *PluginFilter) Description() string

func (*PluginFilter) Name added in v1.2.0

func (f *PluginFilter) Name() string

type PluginManager added in v1.2.0

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

PluginManager handles loading and managing filter plugins.

func NewPluginManager added in v1.2.0

func NewPluginManager(pluginsDir string) *PluginManager

NewPluginManager creates a plugin manager.

func (*PluginManager) AddFilter added in v1.2.0

func (pm *PluginManager) AddFilter(filter *PluginFilter)

AddFilter manually adds a plugin filter.

func (*PluginManager) GetFilters added in v1.2.0

func (pm *PluginManager) GetFilters() []*PluginFilter

GetFilters returns all loaded plugin filters.

func (*PluginManager) LoadPlugins added in v1.2.0

func (pm *PluginManager) LoadPlugins() error

LoadPlugins loads all plugin configurations from the plugins directory.

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 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 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 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 NewSessionTrackerWithConfig added in v1.2.0

func NewSessionTrackerWithConfig(cfg SessionConfig) *SessionTracker

NewSessionTrackerWithConfig creates a session tracker with config.

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 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 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 Turn

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

Turn represents a single conversation turn

Jump to

Keyboard shortcuts

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