retrieve

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package retrieve is the wedge: it fuses the FTS5 and graph-BM25 signals, expands one hop along the graph, boosts the institutional-memory tier, and packs the result to a token budget. The agent calls this, not raw search.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EstimateTokens

func EstimateTokens(s string) int

EstimateTokens is the exported counter so the measurement harness counts both arms with the exact same function the packer uses.

func TotalTokens

func TotalTokens(cards []Card) int

TotalTokens sums the estimated cost of a set of cards.

Types

type Card

type Card struct {
	NodeID  string
	NoteID  string
	Title   string
	Path    string
	Type    string
	Scope   string // access-control scope(s), comma-joined (for the scope read filter)
	Snippet string
	Score   float64
	Tier0   bool
	Reason  string
}

Card is one retrieval result: a note, why it surfaced, and its fused score.

type Options

type Options struct {
	Limit       int     // candidates pulled per signal (default 20)
	Budget      int     // token budget for packing; 0 = return all ranked
	WeightFTS   float64 // fusion weight; 0 across all three => resolved defaults
	WeightGraph float64
	WeightVec   float64
	NoRerank    bool // skip the cross-encoder stage even when configured (for tuning the fusion itself)
	// AllowedScopes, when non-nil, restricts results to notes whose scope intersects
	// the set (access control). nil = unrestricted (solo / no-ACL fast path). This is
	// THE read boundary: it is applied at the card loop below so it covers FTS, graph,
	// vector, and 1-hop expansion in one place, and before the reranker reads any doc.
	AllowedScopes map[string]bool
}

Options tunes a retrieval. Zero values get sensible defaults.

type Retriever

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

func New

func New(store *index.Store, g *graph.Graph) *Retriever

func NewFromEnv

func NewFromEnv(store *index.Store, g *graph.Graph) *Retriever

NewFromEnv builds a retriever and enables the optional BYOAI stages from the environment. The semantic (vector) and rerank stages are independent: either, both, or neither can be on. Falls back silently to lexical-only when nothing is configured.

func (*Retriever) EnableRerank

func (r *Retriever) EnableRerank(rr rerank.Reranker) bool

EnableRerank turns on the cross-encoder rerank stage. The reranker reorders the top-K fused candidates; it never gates retrieval, so a failing endpoint degrades silently to the fused order. Returns false for a nil reranker.

func (*Retriever) EnableVectors

func (r *Retriever) EnableVectors(e embed.Embedder, model string, storedDim int, vecs map[string][][]float32) bool

EnableVectors turns on the semantic signal. It is a no-op unless the query embedder's model matches the vault's stored model AND its vector width matches the stored width (homogeneity guard: vectors from a different model, or even the same model name at a different dimension, are not comparable. A length mismatch makes every cosine return 0, which min-max then normalizes to a uniform 1 - a silent garbage signal that boosts every note equally. We fail safe to lexical-only rather than emit it). storedDim is the vault's recorded width; if it is 0 (old vault, pre-vector_dim) we derive it from the loaded vectors.

func (*Retriever) HNSWActive

func (r *Retriever) HNSWActive() bool

HNSWActive reports whether the ANN index is built and serving the vector signal (vs the brute-force scan). Only true past the configured MESH_HNSW_THRESHOLD, and only in the pro build (the open core has no ANN implementation wired).

func (*Retriever) RerankActive

func (r *Retriever) RerankActive() bool

RerankActive reports whether a cross-encoder rerank stage is configured.

func (*Retriever) RerankModel

func (r *Retriever) RerankModel() string

RerankModel returns the configured rerank model id (empty when inactive).

func (*Retriever) Retrieve

func (r *Retriever) Retrieve(ctx context.Context, query string, opt Options) ([]Card, error)

Retrieve runs the full pipeline and returns ranked (and optionally budget-packed) cards.

func (*Retriever) SetWeights

func (r *Retriever) SetWeights(fts, graph, vec float64)

SetWeights sets the fusion-weight defaults used when a retrieval does not pass explicit Options weights (e.g. learned weights from `mesh tune`). Any value may be 0; if all three are 0 the built-in defaults apply.

func (*Retriever) VectorModel

func (r *Retriever) VectorModel() string

VectorModel returns the active embedding model id (empty when inactive).

func (*Retriever) VectorsActive

func (r *Retriever) VectorsActive() bool

VectorsActive reports whether the semantic signal will fire (an embedder is configured and the vault has stored vectors that match its model).

func (*Retriever) Weights

func (r *Retriever) Weights() (fts, graph, vec float64)

Weights reports the active fusion-weight defaults (0,0,0 => built-in defaults).

Jump to

Keyboard shortcuts

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