retrieve

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package retrieve implements the token-minimal retrieval funnel: gate → scope filter + shadowing → trigger pinning → (BM25 ‖ embed+cosine) → RRF fuse × utility → floor. The embedding arm is optional at every level: nil Embedder, open breaker, or a dead Ollama all collapse the fusion to BM25-only.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtendLexicon

func ExtendLexicon(acks, stops []string)

ExtendLexicon adds user-configured ack phrases and stopwords (from config.yaml) to the built-in packs. Called once at startup before any Gate call; not safe to call concurrently with retrieval.

func FTSExpr

func FTSExpr(prompt string) string

FTSExpr builds a safe FTS5 MATCH expression from raw prompt text. Prompt text is hostile input to FTS5 (quotes and operators crash MATCH — 14.42): we extract bare folded terms and OR-join them, each double-quoted. Returns "" when nothing searchable remains.

func Fold

func Fold(s string) string

Fold, Terms and Jaccard export the text-normalization primitives for non-hot-path consumers (the learning pipelines) so Go-side matching agrees everywhere. The hot path keeps calling the unexported forms directly.

func Jaccard

func Jaccard(a, b []string) float64

func Terms

func Terms(s string, max int) []string

Types

type Candidate

type Candidate struct {
	Card   store.StoredCard
	Score  float64
	Pinned bool
	Vec    []float32
}

Candidate is one card that survived the funnel, best first. Vec is the card's embedding when the cosine arm ran (nil otherwise) — the packer uses it for MMR dedup.

type GateDecision

type GateDecision struct {
	Skip   bool
	Reason string // ack | short | slash | paste | novelty | "" (retrieve)
	Query  string // prompt possibly truncated for huge pastes
}

GateDecision says whether retrieval should run at all. The gate is the biggest token saver: 40-60% of real prompts are acks, follow-ups, or slash commands that need zero new context.

func Gate

func Gate(prompt, lastPrompt string) GateDecision

Gate decides skip-vs-retrieve for one prompt. lastPrompt is the previous prompt in this session ("" if none) for the novelty check.

type Retriever

type Retriever struct {
	Store    *store.Store
	Embedder embed.Embedder
	Model    string // embedding model tag; vectors from other models are stale
}

Retriever runs the funnel against a store. Embedder nil ⇒ BM25-only.

func (*Retriever) Baseline

func (r *Retriever) Baseline(ctx context.Context, sc Scope) ([]Candidate, error)

Baseline returns SessionStart cards for a scope: baseline-flagged cards first (narrowest scope first), for the packer to budget.

func (*Retriever) Retrieve

func (r *Retriever) Retrieve(ctx context.Context, query string, sc Scope) ([]Candidate, error)

Retrieve returns ranked candidates for a gated query. May return an empty slice — the score floor decides that injecting nothing is correct.

type Scope

type Scope struct {
	Repo    string // "" outside git
	Branch  string
	Langs   []string
	Allowed map[string]bool
}

Scope is the deterministic routing context for one prompt: which card scopes are candidates and their precedence boosts. Costs a few stats and small file reads — no exec, no LLM.

func DetectScope

func DetectScope(cwd string) Scope

DetectScope resolves cwd → scope chain. Never fails: worst case is global + dir:<hash>.

func (Scope) Boost

func (Scope) Boost(narrowestRank int) float64

Boost returns the additive precedence bonus for a card's narrowest in-scope rank (see knowledge.NarrowestScopeRank).

Jump to

Keyboard shortcuts

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