CAT-K8: the search index is in the module's memory (bleve), built from the ledger and kept as it writes

ask and holds on the ledger answer from a full-text index of the heads and the current versions' text and clauses; it is
built at start (index not built is the answer meanwhile), a new version replaces its record's documents, a reserved,
withdrawn or retracted record has none. A new pure Go dependency, bleve.

Issue 1966.

Claude-Session: https://claude.ai/code/session_01JG73KJNrH5EgFXMKKYSrig
This commit is contained in:
2026-11-04 12:12:44 +01:00
parent 7bf0e991d8
commit a4e3a9cbe0
5 changed files with 389 additions and 9 deletions
@@ -93,6 +93,10 @@ type ledgerStore struct {
// drafted are the sections written for a draft and named by no version yet: a version that carries one reuses its
// write-section and writes none again.
drafted map[Key][]sectionRef
// index is the search index of the module's memory, nil until built.
index *searchIndex
indexAsync bool
logf func(format string, a ...any)
}
// newLedgerStore is the store over a pipe and the module's states.
@@ -350,13 +354,13 @@ func (s *ledgerStore) Create(ctx context.Context, v Version) error {
if err != nil {
return err
}
return s.commit(ctx, k, operationFor(nil, v, true), &entryHead{CurrentVersion: 1, Versions: 1, Current: ptr(stripped(v.Record, refs))}, v, refs)
return s.commit(ctx, k, operationFor(nil, v, true), &entryHead{CurrentVersion: 1, Versions: 1, Current: ptr(stripped(v.Record, refs))}, v, refs, true)
}
func ptr[T any](v T) *T { return &v }
// commit appends the operation that carries the new head and the version, and holds the head it derived.
func (s *ledgerStore) commit(ctx context.Context, k Key, op string, e *entryHead, v Version, refs []sectionRef) error {
func (s *ledgerStore) commit(ctx context.Context, k Key, op string, e *entryHead, v Version, refs []sectionRef, current bool) error {
body := map[string]any{"kind": k.Kind, "number": k.Number, "entry": e,
"version": versionHead{N: v.N, By: v.By, At: v.At, Why: v.Why, Insight: v.Insight, Record: stripped(v.Record, refs)},
"pending-sections": map[string]any{}}
@@ -366,6 +370,7 @@ func (s *ledgerStore) commit(ctx context.Context, k Key, op string, e *entryHead
}
s.heads[k] = &headState{Kind: k.Kind, Number: k.Number, Entry: e, Sequence: seq}
delete(s.drafted, k)
s.indexed(v.Record, current)
return nil
}
@@ -455,7 +460,7 @@ func (s *ledgerStore) Save(ctx context.Context, v Version, current, clearPending
if clearPending {
e.Pending, e.PendingVersion = nil, 0
}
return s.commit(ctx, k, operationFor(old, v, current), &e, v, refs)
return s.commit(ctx, k, operationFor(old, v, current), &e, v, refs, current)
}
func (s *ledgerStore) Versions(_ context.Context, k Key) ([]Version, error) {
@@ -493,14 +498,24 @@ func (s *ledgerStore) Versions(_ context.Context, k Key) ([]Version, error) {
return out, nil
}
// Search ranks the served versions as the in-memory store does, over every record; the index of the module's memory
// replaces it (the search index of to-be 55 §1).
// Search ranks the served versions from the index of the module's memory; it is built on the first question when no start
// built it, and refuses with "index not built" while a start builds it.
func (s *ledgerStore) Search(ctx context.Context, question string, limit int) ([]Hit, error) {
all, err := s.All(ctx)
if err != nil {
return nil, err
s.mu.Lock()
idx, async := s.index, s.indexAsync
s.mu.Unlock()
if idx == nil {
if async {
return nil, ErrIndexNotBuilt
}
if err := s.BuildIndex(ctx); err != nil {
return nil, err
}
s.mu.Lock()
idx = s.index
s.mu.Unlock()
}
return searchEntries(all, question, limit), nil
return idx.search(question, limit)
}
func (s *ledgerStore) Inbound(_ context.Context, k Key) ([]Inbound, error) {