Skip to content

feat(memory): BM25 section search, vector views and RRF - #125

Merged
venkat1701 merged 6 commits into
mainfrom
feature/memory-retrieval
Nov 25, 2026
Merged

venkat1701 merged 6 commits into
mainfrom
feature/memory-retrieval

Conversation

@venkat1701

@venkat1701 venkat1701 commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Second part of the memory work, stacked on #124.

What it does

  • SEARCH TEXT ranks sections by BM25. By default it searches each node's latest revision; with HISTORY it searches every revision.
    • The index is a derivation written in the same transaction as the sections. The search therefore always matches committed data, including with AT GENERATION and on branches.
    • N and the average length are kept per tenant, so one tenant never changes another tenant's scores.
    • Sections the reader can't see are dropped after scoring.
    • Terms are accent-folded and lower-cased. Reader.retrieve, PR-123 and snake_case are indexed whole as well as in parts, so exact technical terms rank first.
    • Postings are keyed by a 64-bit tenant-and-term hash, plus a 32-bit fingerprint that is checked on every posting.
  • Vector records give one atom several vectors, each under a named view and model (EMBED … VIEW, VECTORS OF, NEAREST VECTORS TO).
    • Each model gets its own HNSW index of vector records. It is persisted and caught up from the change feed the same way as the per-atom index.
    • SIMILAR TO and EMBED … TEXT behave as before.
  • REVISE … EMBED stores a content vector for every section.
    • DELETE drops an atom's vectors.
    • PURGE erases the vectors of the node, its revisions and its sections.
    • Replacing a vector on main erases the old one.
  • Rrf.combine does reciprocal rank fusion with k = 60. FUSE now uses the same terms.

Slots 60 to 63 are new, still under storage format 10.

Syntax

search_text  = "SEARCH" "TEXT" string [ "IN" type_name { "," type_name } ] [ "HISTORY" ] [ "TOP" integer ] ;
embed_view   = "EMBED" ref "VIEW" string ( "TEXT" string | "VECTOR" "[" number { "," number } "]" "MODEL" ( identifier | string ) ) ;
vectors_of   = "VECTORS" "OF" ref ;
nearest      = "NEAREST" "VECTORS" "TO" ( string | "[" number { "," number } "]" "MODEL" ( identifier | string ) )
               [ "VIEW" string ] [ "TOP" integer ] ;
revise_option = … | "EMBED" ;

Testing

  • Full build passes: engine 269, database 334, server 41.
  • TextSearchTest:
    • On random corpora, scores and order match a naive BM25 for both the latest revisions and the full history.
    • Covers exact technical terms, the latest-revision filter, type filters, purge, and a restart.
  • VectorTest:
    • On 150 random vectors, nearest vectors match an exhaustive scan, before and after a restart.
    • Covers views, replacement, embedding on REVISE, and purge.
  • SecurityTest#textSearchAndVectorsStayInsideTheTenant shows that scores in one tenant don't move when another tenant writes. PermissionsTest checks type grants on search.
  • Mutations: 7 of 8 deliberately broken rules were caught by a test. The survivor is the tenant comparison in nearestVectors: removing it alone is still blocked by the owner-visibility check behind it.
  • Live over the wire, two tenants:
    • SEARCH TEXT agrees with a Python BM25 on 8 of 8 queries per tenant, before and after a purge.
    • NEAREST VECTORS agrees with an exhaustive cosine scan on 12 of 12 queries per tenant.
    • 18 of 18 cross-tenant probes are refused.
    • Results are identical after SIGKILL, with 314 commits replayed.
    • A deliberately wrong reference fails.

Merge order for the memory stack: #128 (CI fix), #124, #125, #126, #127, #129 (online text index rebuild), #130 (starter kit). Retarget each to main once the one below it merges. A private preview image built from #129 merged with #128 is at ghcr.io/metacoglabs/hstore-memory:0.2.0-dec80f2.

…usion

SEARCH TEXT ranks the sections of each node's latest revision by BM25,
or every revision with HISTORY. The index is a derivation written in the
same transaction as the sections, so it matches the committed data at
every generation and branch. Statistics are kept per tenant. Terms are
accent-folded and lower-cased; dotted, dashed and underscored names are
indexed whole and in parts so exact technical terms rank first.

Vector records let one atom hold several vectors under named views and
models (EMBED ... VIEW, VECTORS OF, NEAREST VECTORS TO). Each model has
its own HNSW index of vector records beside the per-atom index, which
keeps SIMILAR TO unchanged. REVISE ... EMBED stores a content vector for
every section, and purge erases the vectors of a node, its revisions and
its sections.

Rrf.combine fuses ranked lists by reciprocal rank and FUSE now uses it.
- Text search without HISTORY reads a separate posting list for the
  sections of latest revisions, moved when a node gets a new revision,
  so it no longer scans every old revision. A derived revision-to-
  sections index makes that move possible inside the transaction.
- Tokenization keeps combining marks outside Latin and Greek, and splits
  Chinese, Japanese, Thai, Lao, Khmer and Burmese runs into character
  bigrams.
- The indexing cache keeps term counts per section span with byte and
  entry bounds, is cleared for purged and abandoned payloads, and only a
  purged payload reads as empty; other read errors fail the commit.
- Vector records are keyed by owner, view and model, so concurrent
  writes of one view conflict instead of leaving two. Replacing a vector
  no longer erases the old bytes, which branches and history may still
  read; replaced and dropped vectors give their quota back.
- NEAREST VECTORS skips sections of superseded revisions unless HISTORY
  is given, meters every vector it examines, scores each one once, and
  skips records whose payload cannot be read.
- REVISE ... EMBED charges all section vectors in one commit.
- MERGE BRANCH copies vectors of the revisions and sections it copies and
  drops vectors of atoms the branch deleted. PURGE erases vectors of the
  node's revisions and sections from the retained history too.
@venkat1701

Copy link
Copy Markdown
Collaborator Author

Pushed fixes from a review pass. This branch now also merges the latest #124.

  • Search without HISTORY reads a separate posting list for the sections of latest revisions. A common term no longer scans every old revision or runs out of budget. A derived revision-to-sections index moves those postings inside the transaction whenever a node gets a new revision.
  • Tokenization:
    • Combining marks are kept outside Latin and Greek, so Hindi words stay whole and で no longer matches て.
    • Chinese, Japanese, Thai, Lao, Khmer and Burmese text is indexed as character bigrams.
  • The indexing cache:
    • It now holds term counts per section span, bounded in bytes and entries.
    • It is cleared for purged and abandoned payloads.
    • Only a purged payload reads as empty. Any other read error fails the commit instead of silently indexing nothing.
  • Vector records:
    • They are keyed by owner, view and model, so two concurrent writes of one view conflict instead of leaving two.
    • Replacing a vector no longer erases the old bytes, which branches and history may still read.
    • Replaced and dropped vectors give their quota back.
  • NEAREST VECTORS:
    • It skips sections of superseded revisions unless HISTORY is given.
    • It meters every vector it looks at and scores each one only once.
    • It skips records whose payload can't be read.
  • REVISE … EMBED charges all section vectors in a single commit.
  • MERGE BRANCH copies the vectors of the revisions and sections it brings across, and drops the vectors of atoms the branch deleted.
  • PURGE also erases vectors of revisions and sections that are still in the history window.
  • One deliberate rule from before stays: payloads written by a rolled-back transaction keep counting against the quota, because the payload files never shrink. feat(memory): Markdown documents with revisions and sections (format 10) #124 had started refunding abandoned Markdown; that is reverted in bbd23b7.

Full build: engine 269, database 341, server 41. Each new test fails when its fix is reverted. The live run is green again, before and after SIGKILL.

REBUILD TEXT INDEX, or Writer.rebuildTextIndex(), drops the BM25
postings, the statistics and the revision-to-section index of the
current branch and indexes every stored section again in one
transaction. It runs through Transaction.apply like the CREATE INDEX
backfill, so it is replayed if the transaction is rebased, and its
key changes go to the WAL like any other commit. Only an
administrator of the default tenant may run it, because it rewrites
every tenant's index.
A rebuild that failed part-way, for example on a payload with a bad
checksum, left the emptied index in its transaction; inside BEGIN ...
COMMIT the next commit published it. The rebuild now puts the old
trees back before rethrowing.

Erasing purged payloads after a checkpoint did not drop them from the
text index's term cache, so a later rebuild on a branch could index
Markdown that was already erased.
@venkat1701

Copy link
Copy Markdown
Collaborator Author

Added REBUILD TEXT INDEX (Writer.rebuildTextIndex()). It drops the BM25 postings, the statistics and the revision-to-section index of the current branch, and indexes every stored section again in one transaction. Only an administrator of the default tenant may run it.

  • It runs through Transaction.apply, like the CREATE INDEX backfill, so a rebased commit redoes it.
  • If a payload can't be read, the old index is restored, so a failure inside BEGIN … COMMIT can't publish a half-built index.
  • Erasing purged payloads now also drops them from the term cache, so a rebuild on a branch can't index Markdown that was already erased.

Tests:

  • A corrupted index is regenerated and matches the original search results, including after reopening.
  • The rebuild races a new revision in both commit orders.
  • A failed rebuild in an explicit transaction leaves the index alone.
  • Erased text isn't indexed from the cache.
  • Tenant admins are refused.

Removing either of the two resets that clear the postings and the statistics makes the corruption test fail.

Live, two tenants: the other tenant's admin is refused. The rebuild re-indexed 287 sections, and BM25 then matched the Python reference on 8 of 8 queries in each tenant, before and after SIGKILL.

The docs note the limits: it builds in memory, counts against the transaction page budget, and holds up other commits if it has to be redone at commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant