Skip to content

feat(memory): unified retrieval over memory documents - #127

Merged
venkat1701 merged 5 commits into
mainfrom
feature/memory-retrieve
Nov 25, 2026
Merged

venkat1701 merged 5 commits into
mainfrom
feature/memory-retrieve

Conversation

@venkat1701

@venkat1701 venkat1701 commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Last part of the memory work, stacked on #126. One call now answers a question from memory documents with evidence attached.

What it does

Reader.retrieve(query, meter) and RETRIEVE 'question' …:

  1. Anchors. It takes the atoms passed with ANCHORS, plus atoms of the RESOLVE IN types whose key matches one to three words of the question.
  2. Three candidate lists:
    • BM25 over sections;
    • nearest content vectors, or a ROUTE with THROUGH HIERARCHY;
    • a confidence-weighted walk from the anchors and the best early candidates, which finds sections linked by hyperedges even when they share no words with the question.
  3. Fusion. The lists are fused by reciprocal rank. A Reranker set in DatabaseOptions (for example a cross-encoder run outside the database) then reorders the best 30.
  4. Time. Each candidate moves to the same section, by anchor, in the revision in force at the requested valid and recorded time. Sections that didn't exist at that time are dropped.
  5. What a hit carries:
    • the section's Markdown and heading path;
    • evidence from the revision's sources and from admitted qualifiers;
    • a path from an anchor through a hyperedge;
    • the later revisions in which the section changed;
    • a current flag. A section whose hyperedge memberships all ended is not current, so a superseded decision never comes back marked current.
  6. Final set. The best hit is always kept. The rest are chosen by how much of the question they add, using the SELECT EVIDENCE objective, with rarer words weighted higher.

DIFFUSE also gains:

  • VALID DURING [a, b), for memberships that overlap an interval;
  • WEIGHTED BY CONFIDENCE, which scales each membership by its qualifier's confidence on both sides of the hyperedge.

Syntax

retrieve = "RETRIEVE" string { "ANCHORS" "(" ref { "," ref } ")" | "RESOLVE" "IN" "(" type_name { "," type_name } ")"
         | "IN" "(" type_name { "," type_name } ")" | "VALID" "AT" instant | "RECORDED" "AT" instant
         | "POLICY" policy | "THROUGH" "HIERARCHY" identifier | "HISTORY" | "TOP" integer } ;
diffuse  = … [ "VALID" "AT" additive | "VALID" "DURING" interval ] [ "POLICY" policy ] [ "WEIGHTED" "BY" "CONFIDENCE" ] … ;

Testing

  • Full build passes: engine 269, database 370, server 41.
  • RetrievalTest runs the first-milestone question from the design, "What did Alice and I decide about ClaimsBay, and what changed afterward?":
    • It returns the May decision, not current and carrying its meeting evidence, along with the current storage section.
    • The superseded storage revision is resolved away.
    • No hit that says Postgres is marked current.
    • At valid time 150, it returns the May text as current, with the later change listed.
  • RetrievalTest also covers the lexical, semantic and structural lists, type filters, coverage across documents and a reranker hook.
  • DiffusionTest covers interval overlap and confidence on each side of a hyperedge.
  • RetrieveStatementTest covers the HQL. SecurityTest#retrievalStaysInsideTheTenant includes a cross-tenant membership planted directly in storage.
  • Mutations: 10 deliberately broken rules were each caught by a test. Two weak tests turned up along the way and were strengthened.
  • Live over the wire, two tenants:
    • The same ClaimsBay scenario passes 8 of 8 checks in each tenant, before and after SIGKILL.
    • All earlier memory checks still pass: section splitting, valid-time resolution, BM25 against Python, vector search against an exhaustive scan, and ROUTE exact at full mass.
    • 18 of 18 cross-tenant probes are refused.
    • 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.

Reader.retrieve and RETRIEVE answer a question in one call. They resolve
anchor atoms named in the question, run BM25 over sections, a vector
search or a route through a hierarchy, and a confidence-weighted walk
from the anchors and the best early candidates, then fuse the three
rankings by reciprocal rank. A configured Reranker reorders the best 30.
Each candidate is moved to the same section in the revision in force,
by the requested valid and recorded time, and dropped if it did not
exist then. Hits carry their Markdown, heading path, evidence from the
revision's sources and admitted qualifiers, a path from an anchor, the
later revisions where the section changed, and whether they are current:
a section whose hyperedge memberships all ended is not. The final set
keeps the best hit and picks the rest by coverage of the question's
rarer words and anchors with the SELECT EVIDENCE objective.

DIFFUSE gains VALID DURING [a, b) for memberships overlapping an
interval and WEIGHTED BY CONFIDENCE, which scales memberships by their
qualifier's confidence on both sides of each hyperedge.
- Each stage gets a share of the statement budget and the time left; a
  stage that runs out keeps what it found and adds a note, so a tight
  budget gives a smaller answer instead of an error.
- In force means valid at the requested time, or now, as REVISION OF
  would answer; HISTORY no longer marks superseded sections current, and
  timed queries search every revision so text in force then is found by
  its own words.
- Hyperedge links, paths and qualifier evidence count over the section
  and its earlier versions by anchor, so an unrelated edit to a node no
  longer turns an ended decision current.
- THROUGH HIERARCHY keeps the IN type filter. With anchors the walk
  starts from the anchors only; without them its seed sections are left
  out of its list.
- The reranker runs on the text that will be returned and a failing
  reranker is skipped with a note.
- Payloads, sections and revisions are cached per call, memberships are
  read by incidence instead of copying member lists, and anchor lookups,
  incident edges and lineage count against the budget.
- `later` follows valid time, coverage weights ignore what the best hit
  already covers, repeated RETRIEVE options are refused, possessives
  resolve, short words do not, and EXPLAIN DIFFUSE names the interval
  and confidence weighting.
@venkat1701

Copy link
Copy Markdown
Collaborator Author

Pushed fixes from a review pass. Each stage now has its own share of the budget, so a tight budget gives a partial answer with a note instead of an error. In force means valid at the requested time, or now; HISTORY no longer marks superseded sections current; timed queries search every revision. Hyperedge links count over a section's earlier versions, so an unrelated edit no longer makes an ended decision current. THROUGH HIERARCHY keeps the type filter, the walk starts from anchors only when there are anchors, the reranker sees the text it ranks and failures are skipped, and per-hit work is cached and charged. Full build: engine 269, database 374, server 41; each new test fails against the previous Retrieval.java. Live run green before and after SIGKILL, ClaimsBay question 8/8 in both tenants.

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