kb

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package kb provides a knowledge base system for storing and searching documents.

Documents are chunked and embedded for hybrid search combining vector similarity and full-text search using Reciprocal Rank Fusion (RRF).

Index

Constants

View Source
const (
	KindDocument = "document"
	KindMemory   = "memory"
	StatusActive = "active"
)

Document kinds and statuses the library itself knows about. Callers may use any other values: the library does not impose a vocabulary.

View Source
const AnyStatus = "*"

AnyStatus in Query.Statuses disables the status filter.

Variables

View Source
var ErrNotFound = errors.New("kb: document not found")

ErrNotFound is returned by Get when no document has the given path.

View Source
var ErrSyncEmbeddingBackendUnavailable = errors.New("kb: sync aborted: embedding backend unavailable")

ErrSyncEmbeddingBackendUnavailable is returned (wrapped) when a sync stops early because the embedding backend kept failing.

Functions

func ContentRev

func ContentRev(content string) string

ContentRev is the default Rev of a document: the hex sha256 of its content.

func DocumentSlugs

func DocumentSlugs(filePath string, aliases []string) []string

DocumentSlugs returns every slug a document can be addressed by: its full path, its basename, and any declared aliases. Link resolution matches a target slug against this set.

func ExtractAliasesFromMetadata

func ExtractAliasesFromMetadata(meta map[string]interface{}) []string

ExtractAliasesFromMetadata reads the "aliases" key from a metadata map. Aliases are the extra slugs a document can be reached by from a [[wiki link]].

func ExtractTagsFromMetadata

func ExtractTagsFromMetadata(meta map[string]interface{}) []string

ExtractTagsFromMetadata reads the "tags" key from a metadata map and returns them as a string slice. Returns nil if no tags are present.

func InjectAliasesIntoMetadata

func InjectAliasesIntoMetadata(meta map[string]interface{}, aliases []string) map[string]interface{}

InjectAliasesIntoMetadata stores aliases in the metadata map under "aliases" so link resolution can read them without re-parsing the document body.

func InjectTagsIntoMetadata

func InjectTagsIntoMetadata(meta map[string]interface{}, tags []string) map[string]interface{}

InjectTagsIntoMetadata ensures tags are stored in the metadata map under "tags". If tags is nil/empty and no existing tags, the key is left absent.

func MergeUnknownFrontMatterKeys

func MergeUnknownFrontMatterKeys(meta map[string]interface{}, rawFM map[string]interface{}) map[string]interface{}

MergeUnknownFrontMatterKeys copies every key from rawFM into meta that is not part of reservedFrontMatterKeys, so host-defined front-matter fields (e.g. "status", "project", "milestone") survive into a document's stored metadata instead of being silently dropped by the typed FrontMatter struct. Values that aren't JSON-representable scalars, lists or maps are stringified, since metadata is marshalled to JSON when persisted.

func NormalizeSlug

func NormalizeSlug(raw string) string

NormalizeSlug converts a link target or document path into its canonical slug: lower-cased, markdown extension stripped, spaces/underscores collapsed into single dashes, path separators preserved.

"pando/features/Foo Bar.md" -> "pando/features/foo-bar"
"Feature_Foo"               -> "feature-foo"

func SerializeFrontMatter

func SerializeFrontMatter(fm FrontMatter, body string) string

SerializeFrontMatter produces a document string with YAML front matter prepended to the body content.

func StripFrontMatter

func StripFrontMatter(raw string) string

StripFrontMatter removes any YAML front matter from raw content and returns only the body. This is a convenience wrapper around ParseFrontMatter.

func WithWriteOrigin

func WithWriteOrigin(ctx context.Context, origin string) context.Context

WithWriteOrigin tags ctx with the subsystem responsible for the writes made under it ("tool", "api", "sync", "watcher", "gc", "remote"). Unset means "tool", which is where writes come from unless something says otherwise.

func WithoutWriteObserver

func WithoutWriteObserver(ctx context.Context) context.Context

WithoutWriteObserver suppresses write publication for everything done under ctx. The IPC dispatcher uses it when applying a write forwarded by another instance: that instance already published the event, and republishing it here would ship the same memory twice.

func WriteOriginOf

func WriteOriginOf(ctx context.Context) string

WriteOriginOf returns the origin tagged on ctx, or "tool".

Types

type BackfillStats

type BackfillStats struct {
	// Candidates is how many documents were selected for (re)scanning.
	Candidates int
	// Scanned is how many of them were actually processed (may be lower than
	// Candidates when the context is cancelled mid-pass).
	Scanned int
	// Documents is how many produced at least one link.
	Documents int
	// Links is the number of link rows written.
	Links int
}

BackfillStats reports what a link backfill pass did.

type Backlink struct {
	// FilePath of the document that holds the link.
	FilePath string
	// Slug is the target the source wrote, which may be an alias or a bare
	// basename rather than this document's full path.
	Slug string
	// Label is the [[target|label]] text, empty when the link had none.
	Label string
}

Backlink is an incoming link: another document pointing at this one.

type Change

type Change struct {
	Op ChangeOp
	// Path is the file's path after the change (for a delete, the path
	// removed).
	Path string
	// OldPath is the path before a rename.
	OldPath string
	// Blob is the git blob id of the new content, when known. A change whose
	// Blob equals the indexed one is not re-read nor re-embedded.
	Blob string
	// Content is the new content; nil reads Path from the fsys passed to
	// ApplyChanges.
	Content []byte
}

Change is one file change of a commit, as a git diff reports it.

type ChangeOp

type ChangeOp string

ChangeOp is the kind of a Change.

const (
	OpAdd    ChangeOp = "add"
	OpModify ChangeOp = "modify"
	OpDelete ChangeOp = "delete"
	OpRename ChangeOp = "rename"
)

type Doc

type Doc struct {
	Path    string
	Content string
	Meta    map[string]interface{}
	Tags    []string
	Scope   string
	Author  string
	Kind    string
	Status  string
	// Rev is an opaque revision given by the caller (e.g. a content hash of
	// its own). A Put whose Rev equals the stored one does not re-embed.
	Rev string
	// GitBlob is the git blob id the content was read from, when known.
	GitBlob   string
	CreatedAt time.Time
	UpdatedAt time.Time
}

Doc is a document as stored by Put and returned by Get. Path is its id.

Fields left empty are taken from the content's YAML front matter when it has them (kind, else type; status, author, scope; tags, else labels), and otherwise default to KindDocument, StatusActive and no author/scope. Rev defaults to the sha256 of Content. Meta is stored as JSON: the front-matter keys, with Meta's own entries on top.

type Document

type Document struct {
	ID       int64
	FilePath string
	// Content is the full document body. It is populated by GetDocument and by
	// direct row-level queries such as the pinned-memory path in
	// GetMemoriesForInjection, but it is NOT populated when a Document arrives
	// embedded in a SearchResult from SearchDocuments/SearchDocumentsWithOptions
	// (searchVector/searchFTS never select it — PANDO-US-0027, kb.go). Read
	// SearchResult.ChunkContent for the matched excerpt instead; a caller that
	// genuinely needs the full body for its top-k hits backfills it with one
	// keyed query (see documentContentByID in kb.go), never by re-adding the
	// column to the scan.
	Content   string
	Metadata  map[string]interface{}
	Tags      []string
	CreatedAt time.Time
	UpdatedAt time.Time
	// Memory system fields (populated only when reading memory documents)
	MemoryKey   string
	MemoryScope string
	Importance  float64
	Hits        int
	Source      string
	Outdated    bool
	ExpiresAt   *time.Time
}

Document represents a stored document in the knowledge base.

type DocumentConverter

type DocumentConverter interface {
	// ConvertFile converts the file at path to Markdown text.
	ConvertFile(path string) (string, error)
	// IsConvertibleDocument reports whether path is a document format that
	// should be auto-converted during ingestion (curated subset, excludes
	// plain markdown/text which is indexed verbatim).
	IsConvertibleDocument(path string) bool
}

DocumentConverter converts rich document formats (docx, pdf, xlsx, …) to Markdown for on-the-fly KB ingestion. It is implemented by internal/convert.Converter and injected via SetDocumentConverter to avoid a hard dependency from the kb package on the conversion library.

type DocumentRef

type DocumentRef struct {
	ID       int64
	FilePath string
}

DocumentRef is a minimal reference to a KB document (ID + path).

type FrontMatter

type FrontMatter struct {
	CreatedAt time.Time `yaml:"created_at,omitempty"`
	UpdatedAt time.Time `yaml:"updated_at,omitempty"`
	Tags      []string  `yaml:"tags,omitempty"`
	// Aliases are extra names a document can be addressed by from a [[wiki link]],
	// on top of its path and basename.
	Aliases []string `yaml:"aliases,omitempty"`
	// Memory fields — only serialized when non-zero/non-empty
	Key        string     `yaml:"key,omitempty"`
	Scope      string     `yaml:"scope,omitempty"`
	Outdated   bool       `yaml:"outdated,omitempty"`
	ExpiresAt  *time.Time `yaml:"expires_at,omitempty"`
	Hits       int        `yaml:"hits,omitempty"`
	Importance float64    `yaml:"importance,omitempty"`
	Source     string     `yaml:"source,omitempty"`
}

FrontMatter holds the YAML front matter fields for KB documents.

func MergeFrontMatter

func MergeFrontMatter(existing, incoming FrontMatter) FrontMatter

MergeFrontMatter combines existing and incoming front matter. It preserves created_at from existing, sets updated_at to now, and replaces tags with new if provided (otherwise keeps existing).

func NewFrontMatter

func NewFrontMatter(tags []string, opts *MemoryOptions) FrontMatter

NewFrontMatter creates a fresh FrontMatter with created_at/updated_at set to now. When opts is non-nil, memory fields are populated from it.

func ParseFrontMatter

func ParseFrontMatter(raw string) (FrontMatter, string, error)

ParseFrontMatter splits YAML front matter from the body of a document. If no valid front matter is found, it returns a zero FrontMatter and the original content as body.

func ParseFrontMatterWithRaw

func ParseFrontMatterWithRaw(raw string) (FrontMatter, map[string]interface{}, string, error)

ParseFrontMatterWithRaw splits YAML front matter from the body of a document, like ParseFrontMatter, but also unmarshals the same YAML block a second time into a generic map so callers can recover front-matter keys the typed FrontMatter struct does not name (e.g. host-defined fields such as "status" or "project"). If no valid front matter is found, it returns a zero FrontMatter, a nil map, and the original content as body.

type Hit

type Hit struct {
	// Project is never set by Search. A caller fusing hits from several
	// stores sets it before Fuse so equal paths in different projects stay
	// distinct.
	Project   string
	Path      string
	Chunk     string
	StartLine int
	EndLine   int
	// Score is the fused RRF score; higher is better.
	Score float64
	// Rank is the 1-based position in the result list.
	Rank    int
	Kind    string
	Status  string
	Scope   string
	Author  string
	Rev     string
	GitBlob string
	Meta    map[string]interface{}
}

Hit is one ranked chunk.

func Fuse

func Fuse(limit int, lists ...[]Hit) []Hit

Fuse merges hit lists ranked independently (typically one per project, whose scores are not comparable and whose embedding dimensions may differ) with Reciprocal Rank Fusion on each hit's rank in its own list, and keeps the top limit. Hits are the same when Project, Path and line range match; set Project before fusing lists from different stores.

type KBStore

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

KBStore manages knowledge base documents with chunking, embeddings, and hybrid search. It uses SQLite for storage with FTS5 for full-text search and in-memory vector search.

func NewKBStore deprecated

func NewKBStore(db *sql.DB, embedder embeddings.Embedder, chunkSize, chunkOverlap int) *KBStore

NewKBStore creates a new KBStore instance.

Parameters:

  • db: SQLite database connection
  • embedder: Embedder for generating chunk embeddings
  • chunkSize: Maximum chunk size in characters (0 = use default)
  • chunkOverlap: Overlap between consecutive chunks (0 = use default)

Deprecated: use Open, which takes every setting (logger, retry classifier, workers, wiki links, converter) in one Options value.

func Open

func Open(db *sql.DB, opts Options) (*KBStore, error)

Open returns a store over db, which must already have the rag schema (github.com/digiogithub/pando/rag/migrate). The store keeps no resources of its own besides db, which stays owned by the caller.

func (*KBStore) AddDocument

func (s *KBStore) AddDocument(ctx context.Context, filePath, content string, metadata map[string]interface{}) error

AddDocument adds a new document to the knowledge base. It chunks the content, generates embeddings, and updates the FTS index.

The write is published to the observer (observer.go) only after it commits.

func (*KBStore) AddDocumentWithEmbeddings

func (s *KBStore) AddDocumentWithEmbeddings(ctx context.Context, filePath, content string, metadata map[string]interface{}, chunks []string, embeddings [][]float32, embeddingModel string) error

AddDocumentWithEmbeddings inserts a document using pre-computed chunks and embeddings. No embedding generation is performed; the provided values are stored directly. embeddingModel is recorded on every inserted chunk that has an embedding (PANDO-US-0029); embedding_dims is derived from each vector's own length.

func (*KBStore) ApplyChanges

func (s *KBStore) ApplyChanges(ctx context.Context, fsys fs.FS, changes []Change, watermark string) (SyncStats, error)

ApplyChanges indexes one commit's changes and records watermark, all in a single transaction: either every change and the new watermark are stored, or none is. Calling it again with the watermark already recorded does nothing, so a job can be retried safely.

Only Markdown files are indexed; other paths are ignored (a rename of one into a Markdown path is an add). Content is read from Change.Content or from fsys. Everything that needs the embedder runs before the transaction. An add or modify whose Blob or content rev matches the indexed document is not re-embedded, and a rename with an unchanged blob only moves the document.

func (s *KBStore) BackfillLinks(ctx context.Context) (BackfillStats, error)

BackfillLinks indexes the [[wiki links]] of documents that were stored before the link graph existed. It is the compatibility path for databases upgraded from a version without kb_links: those documents keep their content but have no link rows, and the filesystem sync will not re-add them because it skips files whose mtime has not changed.

No chunking or embedding happens here: the links are extracted from the content already stored, so the pass costs local CPU and a few writes, never a call to the embedding provider.

Candidates are documents whose content contains "[[" and that have no link rows yet — the content itself acts as the marker, so no schema column is needed and the pass is idempotent: once a document is indexed it stops being a candidate. A document whose only "[[" occurrences sit inside code fences yields no links and is re-scanned by later passes, which is harmless.

func (s *KBStore) Backlinks(ctx context.Context, filePath string) ([]Backlink, error)

Backlinks returns the documents that link to this one, through its path, its basename or any of its aliases.

A link only counts when it actually resolves here: two documents may share a basename, and the shorter form belongs to whichever one the resolver awards it to, so a link to "[[foo]]" is not a backlink of every document named foo.md.

func (*KBStore) Close

func (s *KBStore) Close() error

Close releases the store. The *sql.DB passed to Open stays open: it is owned by the caller.

func (*KBStore) ConfigureFilesystemMirror

func (s *KBStore) ConfigureFilesystemMirror(dirPath string) error

ConfigureFilesystemMirror sets a base directory where KB documents are mirrored as markdown files on add/update/delete operations.

func (*KBStore) CountDocuments

func (s *KBStore) CountDocuments(ctx context.Context) (int64, error)

CountDocuments returns the total number of documents.

func (*KBStore) CountStaleEmbeddings

func (s *KBStore) CountStaleEmbeddings(ctx context.Context, configuredDims int) (StaleEmbeddingStats, error)

CountStaleEmbeddings counts chunks whose recorded embedding dimension does not match configuredDims. Used by the startup staleness check (internal/app/remembrances.go) and by the enrichment status REST route.

func (*KBStore) Delete

func (s *KBStore) Delete(ctx context.Context, path string) error

Delete removes the document at path. Deleting a missing path is not an error.

func (*KBStore) DeleteDocument

func (s *KBStore) DeleteDocument(ctx context.Context, filePath string) error

DeleteDocument removes a document and all its chunks from the knowledge base.

func (*KBStore) DeleteDocumentFromFilesystem

func (s *KBStore) DeleteDocumentFromFilesystem(filePath string) error

DeleteDocumentFromFilesystem removes a mirrored KB document from disk. If no mirror path is configured, this is a no-op.

func (*KBStore) DeletePrefix

func (s *KBStore) DeletePrefix(ctx context.Context, prefix string) (int, error)

DeletePrefix removes every document whose path starts with prefix, in one transaction, and returns how many it removed. An empty prefix is refused rather than taken to mean "everything".

func (*KBStore) EmbeddingModel

func (s *KBStore) EmbeddingModel() string

EmbeddingModel returns the configured document embedder's model id, as set by SetEmbeddingModel. Empty when never set.

func (*KBStore) FilesystemMirrorPath

func (s *KBStore) FilesystemMirrorPath() string

FilesystemMirrorPath returns the configured filesystem mirror path.

func (*KBStore) Get

func (s *KBStore) Get(ctx context.Context, path string) (*Doc, error)

Get returns the document stored at path, or ErrNotFound.

func (*KBStore) GetDocument

func (s *KBStore) GetDocument(ctx context.Context, filePath string) (*Document, error)

GetDocument retrieves a document by file path.

func (*KBStore) GetExpiredDocuments

func (s *KBStore) GetExpiredDocuments(ctx context.Context) ([]DocumentRef, error)

GetExpiredDocuments returns all non-outdated documents whose expires_at is in the past.

func (*KBStore) GetMemoriesForInjection

func (s *KBStore) GetMemoriesForInjection(ctx context.Context, query string, limit int, maxChars int, pinnedScopes []string) ([]MemoryResult, error)

GetMemoriesForInjection retrieves memory documents ranked for context injection. It combines semantic search results with pinned-scope documents, deduplicates, scores, and trims to fit within the maxChars budget.

func (*KBStore) GetMemoryByKey

func (s *KBStore) GetMemoryByKey(ctx context.Context, key string) (*Document, error)

GetMemoryByKey retrieves a document by its memory_key. Returns nil, nil when no document with the key exists.

func (s *KBStore) HasLinks(ctx context.Context) (bool, error)

HasLinks reports whether the knowledge base holds any wiki link at all.

It is the guard the tools use before doing any graph work: a knowledge base written before wiki links existed (or one that simply never uses the syntax) answers false, so the tools can skip the resolver entirely and present exactly the output they presented before the graph was added.

func (*KBStore) IncrementMemoryHits

func (s *KBStore) IncrementMemoryHits(ctx context.Context, docID int64, defaultTTLDays int) error

IncrementMemoryHits increments the hit counter for a document and extends its TTL.

func (*KBStore) LinkCountsFor

func (s *KBStore) LinkCountsFor(ctx context.Context, filePaths []string) (map[string]LinkCounts, error)

LinkCountsFor returns the link counts of the given documents in a single pass over the graph, for callers (search results) that want to hint at connections without fetching them. Documents with no links in either direction are absent from the map, and a link-less knowledge base returns an empty map.

func (*KBStore) LinksFrom

func (s *KBStore) LinksFrom(ctx context.Context, filePath string) ([]WikiLink, error)

LinksFrom returns the wiki links stored for a document, ordered by position. Targets are returned unresolved; resolution against real documents is done by the graph queries.

func (*KBStore) ListDocuments

func (s *KBStore) ListDocuments(ctx context.Context, limit, offset int) ([]Document, error)

ListDocuments returns paginated documents.

func (*KBStore) MarkDocumentOutdated

func (s *KBStore) MarkDocumentOutdated(ctx context.Context, filePath string) error

MarkDocumentOutdated marks a document as outdated and updates its filesystem mirror.

func (s *KBStore) OutgoingLinks(ctx context.Context, filePath string) ([]LinkTarget, error)

OutgoingLinks returns the wiki links a document declares, each resolved against the current documents. A document with no links yields an empty slice and no error, which is the normal state of a knowledge base that does not use the syntax.

func (*KBStore) Put

func (s *KBStore) Put(ctx context.Context, d Doc) (PutResult, error)

Put stores d, replacing any document with the same path. Content is chunked without its front matter and embedded before the write; when the stored document has the same Rev (or the same non-empty GitBlob) only its fields are updated and nothing is re-embedded.

func (*KBStore) RebuildFTS

func (s *KBStore) RebuildFTS(ctx context.Context) error

RebuildFTS rebuilds the FTS5 index from kb_chunks.

func (*KBStore) RelatedDocuments

func (s *KBStore) RelatedDocuments(ctx context.Context, filePath string, limit int) ([]RelatedDocument, error)

RelatedDocuments returns the neighbours of a document in the graph, ordered by how strongly they are connected: documents it links to, documents that link to it, and — weakly — documents sharing tags with it. limit <= 0 uses a default.

func (*KBStore) RelinkAll

func (s *KBStore) RelinkAll(ctx context.Context) (BackfillStats, error)

RelinkAll rebuilds the whole link graph: every stored link row is dropped and re-extracted from the documents' content. Unlike BackfillLinks it does not skip documents that already have links, so it is the way to pick up a change in the extractor (a new syntax, a different slug rule) that made the stored rows stale.

func (*KBStore) RepairFrontMatterMetadata

func (s *KBStore) RepairFrontMatterMetadata(ctx context.Context, dirPath string) (RepairStats, error)

RepairFrontMatterMetadata re-indexes documents whose stored metadata is missing front-matter-derived keys (tags, aliases, or any other host-defined key) that their source file on disk still declares. It repairs the damage the pre-fix watcher inflicted: every edit made through the watcher replaced a document's metadata with bare source_* fields, discarding tags and any other front-matter key, and recorded a fresh source_mtime_unix so the sync path never noticed the file "changed" again and so never repaired it on its own.

The pass ignores the mtime-skip semantics the regular sync uses: it re-parses every filesystem-backed document's current source file regardless of what source_mtime_unix says, because that is exactly the field the bug left in a false "unchanged" state.

The repair is bounded and one-shot per dirPath: on success it records a marker document and returns immediately without scanning on a later call for the same directory. A corpus already indexed by the fixed sync/watcher code has nothing to repair, so the pass still scans it once but writes nothing (RepairStats.Repaired stays 0).

func (*KBStore) Search

func (s *KBStore) Search(ctx context.Context, text string, q Query) ([]Hit, Stats, error)

Search ranks chunks matching text, with every filter of q applied in SQL. In hybrid mode the vector list, the chunk full-text list and the front-matter fields list (title, labels, aliases; a match there ranks the document's first chunk) are fused with RRF.

func (*KBStore) SearchDocuments

func (s *KBStore) SearchDocuments(ctx context.Context, query string, limit int) ([]SearchResult, error)

SearchDocuments performs hybrid search combining vector similarity and FTS. Results are fused using Reciprocal Rank Fusion (RRF).

func (*KBStore) SearchDocumentsWithOptions

func (s *KBStore) SearchDocumentsWithOptions(ctx context.Context, query string, limit int, opts SearchOptions) ([]SearchResult, error)

SearchDocumentsWithOptions performs hybrid search with optional tag filtering and chronological ordering. When opts.Tags is non-empty, results are filtered to documents whose tags fuzzy-match any of the requested tags. When opts.SortByDate is true, results are sorted by updated_at descending.

func (*KBStore) SearchDocumentsWithOptionsAndStats

func (s *KBStore) SearchDocumentsWithOptionsAndStats(ctx context.Context, query string, limit int, opts SearchOptions) ([]SearchResult, SearchStats, error)

SearchDocumentsWithOptionsAndStats is SearchDocumentsWithOptions plus SearchStats. Callers that need to warn on a stale-embedding skip (kb_search_documents, the REST search route) use this instead of SearchDocumentsWithOptions; every other caller is unaffected. When a search extension middleware is installed (observer.go), stats are not tracked through it — the middleware only ever saw the ranked results, the same as SearchDocumentsWithOptions, so this returns a zero SearchStats in that case rather than misreporting.

func (*KBStore) SetDocumentConverter

func (s *KBStore) SetDocumentConverter(c DocumentConverter)

SetDocumentConverter installs a converter so that supported document files found in the KB source directory are converted to Markdown and indexed, referencing the original file. Passing nil disables conversion (the default), in which case only .md files are indexed.

func (*KBStore) SetEmbeddingModel

func (s *KBStore) SetEmbeddingModel(model string)

SetEmbeddingModel records the document embedder's model id so it can be written alongside every chunk this store inserts. Called once at startup from Remembrances.DocumentEmbeddingModel (internal/rag/service.go).

func (*KBStore) SetLogger

func (s *KBStore) SetLogger(l *slog.Logger)

SetLogger sets the logger the store writes its diagnostics to. Call it before the store is used; nil restores slog.Default().

func (*KBStore) SetRetryable

func (s *KBStore) SetRetryable(r sqlx.Retryable)

SetRetryable sets the test that decides whether a failed transaction lost a race against a concurrent writer and should run again. Call it before the store is used; nil restores sqlx.DefaultRetryable.

func (*KBStore) SetSearchMiddleware

func (s *KBStore) SetSearchMiddleware(fn SearchMiddleware)

SetSearchMiddleware installs the search middleware, replacing any previous one. Passing nil removes it.

func (*KBStore) SetSyncWorkers

func (s *KBStore) SetSyncWorkers(workers int)

SetSyncWorkers configures how many concurrent workers are used for KB filesystem import preprocessing (file reads and in-memory preparation). SQLite writes remain serialized through a single writer goroutine.

func (*KBStore) SetWikiLinksEnabled

func (s *KBStore) SetWikiLinksEnabled(enabled bool)

SetWikiLinksEnabled toggles the [[wiki link]] graph. When false no link rows are written and every graph query answers empty, so the KB tools present the output they presented before the graph existed.

Turning it off does not wipe the graph: the rows already indexed stay in the database, invisible, and light up again if it is turned back on — no reindex needed. Only a document rewritten while it is off loses its rows, like any update does.

func (*KBStore) SetWriteObserver

func (s *KBStore) SetWriteObserver(fn WriteObserver)

SetWriteObserver installs the write observer, replacing any previous one. Passing nil removes it.

func (*KBStore) SyncDirectory

func (s *KBStore) SyncDirectory(ctx context.Context, dirPath string) error

SyncDirectory imports or syncs all .md files from a directory. Existing documents are updated, new files are added.

func (*KBStore) SyncDirectoryWithStats

func (s *KBStore) SyncDirectoryWithStats(ctx context.Context, dirPath string, deleteMissing bool) (SyncStats, error)

SyncDirectoryWithStats imports or syncs all markdown files from a directory. It recursively scans for .md files, upserts modified documents, and optionally deletes KB documents that no longer exist on disk for that directory source.

func (*KBStore) SyncTree

func (s *KBStore) SyncTree(ctx context.Context, fsys fs.FS, opts SyncOptions) (SyncStats, error)

SyncTree indexes every file of fsys under opts.Prefix with Put (unchanged files are not re-embedded) and, with DeleteMissing, removes the stored documents under the prefix that are gone. Hidden files and directories are skipped. It stops at the first error or when ctx is cancelled; what was stored until then stays. It does not touch the ApplyChanges watermark.

func (*KBStore) UpdateDocument

func (s *KBStore) UpdateDocument(ctx context.Context, filePath, content string, metadata map[string]interface{}) error

UpdateDocument updates an existing document's content and metadata. It re-chunks and re-embeds the content.

func (*KBStore) UpsertMemory

func (s *KBStore) UpsertMemory(ctx context.Context, opts MemoryUpsertOptions) (created bool, err error)

UpsertMemory stores or updates a memory document, keyed by opts.Key when provided. Returns created=true when a new document was inserted, false when an existing one was updated.

It delegates to AddDocument/UpdateDocument on some paths, so the write observer is suppressed for the nested call: the memory event published here carries the scope and key those events cannot know, and one write must be reported once.

func (*KBStore) WantedConcepts

func (s *KBStore) WantedConcepts(ctx context.Context, limit int) ([]WantedConcept, error)

WantedConcepts returns the link targets that no document defines, most linked first: the concepts the knowledge base refers to but never explains. limit <= 0 returns them all.

func (*KBStore) WatchDirectory

func (s *KBStore) WatchDirectory(ctx context.Context, dirPath string) error

WatchDirectory monitors a KB source directory recursively and keeps KB documents in sync with .md file changes in near real-time.

func (*KBStore) Watermark

func (s *KBStore) Watermark(ctx context.Context) (string, error)

Watermark returns the watermark of the last ApplyChanges (typically a commit id), or "" when none ran yet.

func (*KBStore) WikiLinksEnabled

func (s *KBStore) WikiLinksEnabled() bool

WikiLinksEnabled reports whether the wiki link graph is active.

func (*KBStore) WriteDocumentToFilesystem

func (s *KBStore) WriteDocumentToFilesystem(filePath, content string) error

WriteDocumentToFilesystem writes a KB document to the configured mirror path. If no mirror path is configured, this is a no-op.

type LinkCounts

type LinkCounts struct {
	Outgoing  int
	Backlinks int
}

LinkCounts is how connected a document is: the links it declares and the links it receives.

type LinkTarget

type LinkTarget struct {
	WikiLink
	// FilePath of the document the link points at, empty when unresolved.
	FilePath string
	// Resolved reports whether the target slug matched a document.
	Resolved bool
}

LinkTarget is a wiki link together with the document it resolves to, if any. Unresolved targets are kept on purpose: they are concepts the author referred to but nobody has documented yet.

type MemoryGCService

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

MemoryGCService periodically marks expired memory documents as outdated.

func NewMemoryGCService

func NewMemoryGCService(store *KBStore, interval time.Duration) *MemoryGCService

NewMemoryGCService creates a new MemoryGCService.

func (*MemoryGCService) Start

func (g *MemoryGCService) Start(ctx context.Context)

Start launches the GC loop in a background goroutine.

func (*MemoryGCService) Stop

func (g *MemoryGCService) Stop()

Stop signals the background goroutine to exit.

type MemoryOptions

type MemoryOptions struct {
	Key        string
	Scope      string
	Source     string
	Importance float64
	TTLDays    int // 0 = no expiry
}

MemoryOptions carries optional memory-layer settings for NewFrontMatter.

type MemoryResult

type MemoryResult struct {
	Document     Document
	ChunkContent string
	Score        float64
}

MemoryResult wraps a Document with its chunk content and relevance score.

type MemoryUpsertOptions

type MemoryUpsertOptions struct {
	FilePath       string
	Key            string
	Scope          string
	Content        string
	Tags           []string
	Metadata       map[string]interface{}
	Importance     float64
	Source         string
	DefaultTTLDays int // 0 → 180
}

MemoryUpsertOptions configures a memory document upsert operation.

type Options

type Options struct {
	// Embedder computes chunk and query embeddings. Nil disables the vector
	// leg of search; full-text search still works.
	Embedder embeddings.Embedder
	// EmbeddingModel is the embedder's model id, recorded on every chunk so a
	// later embedder change can be detected (CountStaleEmbeddings). Empty
	// falls back to the embedder's own Model() (embeddings.ModelOf).
	EmbeddingModel string
	// ChunkSize is the maximum chunk size in characters (0 = default).
	ChunkSize int
	// ChunkOverlap is the overlap between consecutive chunks (0 = default;
	// clamped below ChunkSize).
	ChunkOverlap int
	// Workers is the number of parallel workers a directory sync uses
	// (0 = default, at most 32).
	Workers int
	// DisableWikiLinks turns off [[wiki link]] extraction and the link graph
	// queries. Links are on by default.
	DisableWikiLinks bool
	// Converter turns non-Markdown files (PDF, Office, HTML...) into Markdown
	// during directory syncs. Nil indexes Markdown and plain text only.
	Converter DocumentConverter
	// Logger receives the store's diagnostics (nil = slog.Default()).
	Logger *slog.Logger
	// Retryable decides whether a failed transaction lost a race against a
	// concurrent writer and should run again (nil = sqlx.DefaultRetryable).
	Retryable sqlx.Retryable
}

Options configures a store opened with Open. The zero value is valid: no embedder (full-text search only), default chunking, default sync workers, wiki links on, slog.Default() and sqlx.DefaultRetryable.

type PutResult

type PutResult struct {
	// Created is true when no document had this path before.
	Created bool
	// Embedded is true when the content was (re)chunked and (re)embedded;
	// false when only the document's fields changed because its Rev did not.
	Embedded bool
}

PutResult reports what Put did.

type Query

type Query struct {
	// Limit is the maximum number of hits (default 10).
	Limit int
	// PathPrefixes keeps documents under any of these path prefixes.
	PathPrefixes []string
	// ExcludePrefixes drops documents under any of these path prefixes.
	ExcludePrefixes []string
	// Scopes keeps documents whose scope is one of these or nested under one
	// ("project/acme" matches "project/acme" and "project/acme/x", not
	// "project/acme2").
	Scopes []string
	// Kinds keeps documents of these kinds.
	Kinds []string
	// Statuses keeps documents with these statuses. Empty means
	// []string{StatusActive}; include AnyStatus to search every status.
	Statuses []string
	// Tags keeps documents carrying any of these tags (case-insensitive).
	Tags []string
	// Authors keeps documents by any of these authors.
	Authors []string
	// ExcludeOutdated drops documents marked outdated.
	ExcludeOutdated bool
	// Mode selects the search legs (default ModeHybrid).
	Mode SearchMode
	// MatchAll makes the full-text legs require every query word (FTS5
	// AND). By default (false) a chunk or document matching any query word
	// is a candidate (OR) and bm25 ranks those matching more, and rarer,
	// words higher, so a multi-word query still finds partial matches.
	MatchAll bool
}

Query filters a Search. Every filter is applied in SQL, before ranking, so a filter never makes a search return fewer hits than exist.

type RelatedDocument

type RelatedDocument struct {
	FilePath string
	Score    float64
	// Reasons explains the score, e.g. "links to it", "links here",
	// "shared tags: kb, wiki".
	Reasons []string
}

RelatedDocument is a neighbour in the document graph, scored by how it is connected.

type RepairStats

type RepairStats struct {
	Scanned  int `json:"scanned"`
	Repaired int `json:"repaired"`
}

RepairStats reports the outcome of RepairFrontMatterMetadata: how many filesystem-backed documents it looked at, and how many of those had their metadata rebuilt because it was missing front-matter-derived keys their source file still declares.

type SearchMiddleware

type SearchMiddleware func(ctx context.Context, query string, limit int, opts SearchOptions, next SearchNext) ([]SearchResult, error)

SearchMiddleware decorates document search. It receives the rest of the chain and may add, drop or reorder results.

type SearchMode

type SearchMode int

SearchMode selects the legs a Search runs.

const (
	// ModeHybrid fuses vector and full-text results (RRF). Without an
	// embedder it runs full-text only.
	ModeHybrid SearchMode = iota
	// ModeFTS runs full-text search only (chunk text and front-matter fields).
	ModeFTS
	// ModeVector runs vector search only; it needs an embedder.
	ModeVector
)

type SearchNext

type SearchNext func(ctx context.Context, query string, limit int, opts SearchOptions) ([]SearchResult, error)

SearchNext is the remaining search chain, ending in the store's own search.

type SearchOptions

type SearchOptions struct {
	Tags            []string // filter results by tags (fuzzy match)
	SortByDate      bool     // sort results by updated_at descending
	ExcludeOutdated bool     // exclude documents where outdated = 1
	Scope           string   // filter by memory_scope prefix (empty = all)
	// PathPrefix restricts both search legs to documents whose file_path
	// starts with this prefix, pushed into the SQL as a bound
	// `AND d.file_path LIKE ? || '%' ESCAPE '\'` clause (PANDO-US-0028) rather
	// than filtered in Go after fusion — a prefix filtered after the fact would
	// under-return whenever the unfiltered top candidates all belong to another
	// prefix. Empty (the default) changes nothing about current behaviour.
	PathPrefix string
}

SearchOptions provides optional filtering/sorting for search queries.

type SearchResult

type SearchResult struct {
	Document     Document
	ChunkContent string
	Score        float64
	Rank         int
}

SearchResult represents a ranked search result from the knowledge base.

Document.Content is unpopulated: see the field's doc comment on Document. Use ChunkContent for the matched text.

type SearchStats

type SearchStats struct {
	// SkippedForDimensionMismatch counts chunks the vector leg skipped
	// because their recorded embedding dimension does not match the query
	// embedding's — evidence the document embedding model changed and some
	// chunks have not been re-embedded yet (PANDO-US-0029). Zero on a
	// consistent corpus.
	SkippedForDimensionMismatch int
}

SearchStats reports auxiliary counters from a search call that are not carried by the ranked results themselves.

type StaleEmbeddingStats

type StaleEmbeddingStats struct {
	// Count is the number of embedded chunks whose recorded embedding_dims
	// differs from the configured dimension.
	Count int64
	// RecordedModels lists the distinct, non-empty embedding_model values
	// found among the mismatched chunks (sorted). A chunk written before
	// PANDO-US-0029 has an empty embedding_model — treated as "unknown", not
	// listed here — even though it still counts towards Count if its
	// backfilled dimension does not match.
	RecordedModels []string
}

StaleEmbeddingStats reports chunks whose recorded embedding dimension does not match a given (normally the currently configured) embedder dimension — evidence the document embedding model changed since those chunks were written (PANDO-US-0029).

type Stats

type Stats struct {
	// VectorCandidates and FTSCandidates are the sizes of the lists fused.
	VectorCandidates int
	FTSCandidates    int
	// SkippedForDimensionMismatch counts chunks whose embedding dimension
	// differs from the query's; SkippedForModelMismatch counts chunks
	// embedded by another model than the store's. Both mean the corpus needs
	// re-embedding (a Put or ApplyChanges of the document does it).
	SkippedForDimensionMismatch int
	SkippedForModelMismatch     int
}

Stats reports counters from a Search that are not carried by the hits.

type SyncOptions

type SyncOptions struct {
	// Prefix is the directory of the fsys to walk ("" = the whole fsys).
	// Document paths are fsys paths, so they keep this prefix.
	Prefix string
	// Include keeps only files matching any of these path.Match patterns,
	// tried against the full path and the base name. Empty means Markdown
	// files.
	Include []string
	// Exclude drops files (or whole directories) matching any of these
	// patterns, tried the same way.
	Exclude []string
	// DeleteMissing removes stored documents under Prefix that the walk did
	// not see.
	DeleteMissing bool
}

SyncOptions configures SyncTree.

type SyncStats

type SyncStats struct {
	Scanned   int `json:"scanned"`
	Added     int `json:"added"`
	Updated   int `json:"updated"`
	Unchanged int `json:"unchanged"`
	Deleted   int `json:"deleted"`
	// Renamed counts documents ApplyChanges moved to a new path without
	// re-embedding them.
	Renamed int `json:"renamed,omitempty"`
	// LinksIndexed counts the [[wiki links]] found in the documents this run
	// added or updated. Unchanged documents keep the links they already had and
	// are not counted, so this is what the run wrote, not the size of the graph.
	LinksIndexed int `json:"links_indexed"`
}

SyncStats contains counters from a filesystem-to-KB synchronization run.

type WantedConcept

type WantedConcept struct {
	// Slug is the normalized target.
	Slug string
	// Raw is the target as an author first wrote it, which reads better than the
	// slug when suggesting a title.
	Raw string
	// Count is how many documents point at it.
	Count int
	// Sources are the documents that point at it.
	Sources []string
}

WantedConcept is a link target that no document defines yet: the wiki equivalent of a red link. Frequently wanted concepts are the best candidates for the next document to write.

type WikiLink struct {
	Raw      string
	Slug     string
	Label    string
	Position int
}

WikiLink is a [[target]] or [[target|label]] reference found in a document body.

Slug is the normalized form used for storage and resolution; Raw preserves the target exactly as the author wrote it. A link is not required to resolve to an existing document: unresolved targets are kept on purpose, since they record concepts that are worth documenting later.

func ExtractWikiLinks(body string) []WikiLink

ExtractWikiLinks returns the wiki links found in a document body, in order of appearance and deduplicated by slug (the first occurrence wins). Links inside fenced code blocks and inline code spans are ignored, so documentation that merely shows the syntax does not pollute the graph.

type WriteEvent

type WriteEvent struct {
	Kind      WriteKind
	Op        WriteOp
	FilePath  string
	Key       string
	Scope     string
	Content   string
	Tags      []string
	Metadata  map[string]any
	Origin    string
	Timestamp time.Time
}

WriteEvent describes one committed write. Observers see it only after the write succeeded: an observer must never be able to report something that did not happen.

type WriteKind

type WriteKind string

WriteKind distinguishes a keyed memory from a plain knowledge-base document.

const (
	WriteKindMemory   WriteKind = "memory"
	WriteKindDocument WriteKind = "document"
)

type WriteObserver

type WriteObserver func(ctx context.Context, ev WriteEvent)

WriteObserver receives committed writes. It is called synchronously on the write path and must return promptly; whether the work behind it is async is the observer's decision, not the store's.

type WriteOp

type WriteOp string

WriteOp is what happened to the record.

const (
	WriteCreated WriteOp = "created"
	WriteUpdated WriteOp = "updated"
	WriteDeleted WriteOp = "deleted"
)

Jump to

Keyboard shortcuts

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