Skip to content

feat(memory): region hierarchy with top-p routing - #126

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

venkat1701 merged 6 commits into
mainfrom
feature/memory-hierarchy

Conversation

@venkat1701

@venkat1701 venkat1701 commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Third part of the memory work, stacked on #125.

What it does

  • CREATE HIERARCHY name ON MODEL m VIEW v [OF Type] [FANOUT f] [LEAVES n] [CONTINUOUS] groups sections into nested regions.
    • It covers the sections of each node's latest revision that have a vector in that model and view.
    • The build is recursive spherical k-means: fixed seeds, at most FANOUT children per region and at most LEAVES sections per lowest region.
    • Each region stores its parent, level, section count and centroid. The centroid is a vector record of the region that is kept out of vector search.
  • REFRESH HIERARCHY follows new revisions, purges and vector changes from the change feed.
    • A new section descends to the most similar centroid.
    • An overfull region splits with 2-means; when the root splits, a new root is added above it.
    • Emptied regions go away, and a root with a single child is replaced by that child.
    • Centroids on every changed path are recomputed from their children.
    • If the feed no longer covers the hierarchy's generation, it is rebuilt.
    • A CONTINUOUS hierarchy refreshes after each relevant commit and once on open.
  • ROUTE query IN HIERARCHY h expands regions best first.
    • Child scores are calibrated per level, divided by the temperature and turned into a softmax.
    • It keeps the smallest set of children holding MASS of the probability, within MIN and MAX.
    • When the normalised entropy is above FALLBACK, it widens to MAX.
    • REGIONS and DEPTH cap the work.
    • It reports regions scored and expanded, sections examined, mean entropy, mass kept, whether the fallback fired, and, with RECALL, recall against an exact ranking.
  • Routing data trained outside the database is loaded with:
    • REFERENCE REGION r OF HIERARCHY h VECTORS (…), up to 16 vectors per region, which replace the centroid when scoring;
    • CALIBRATE HIERARCHY h [TEMPERATURE t] [LEVEL l SCALE a SHIFT b]…, which also bumps the router version.
  • Privileges:
    • A tenant admin creates, rebuilds, drops, calibrates and sets references.
    • A writer refreshes.
    • A reader routes and lists.
    • Hierarchies live on main and belong to one tenant.
  • Region and leaf records are the source of truth. The child, document and per-hierarchy indexes are derived from them by a derivation.

Slots 64 to 66 and 68 to 70 are new, still under format 10.

Syntax

create_hier  = "CREATE" "HIERARCHY" identifier "ON" "MODEL" ( identifier | string ) "VIEW" string [ "OF" type_name ]
               [ "FANOUT" integer ] [ "LEAVES" integer ] [ "CONTINUOUS" ] ;
hier_admin   = ( "REBUILD" | "DROP" | "REFRESH" ) "HIERARCHY" identifier ;
show_hier    = "SHOW" "HIERARCHIES" | "REGIONS" "OF" "HIERARCHY" identifier ;
calibrate    = "CALIBRATE" "HIERARCHY" identifier [ "TEMPERATURE" number ] { "LEVEL" integer "SCALE" number "SHIFT" number } ;
reference    = "REFERENCE" "REGION" integer "OF" "HIERARCHY" identifier "VECTORS" "(" vector { "," vector } ")" ;
route        = "ROUTE" ( string | vector ) "IN" "HIERARCHY" identifier
               { "MASS" number | "MIN" integer | "MAX" integer | "DEPTH" integer | "REGIONS" integer
               | "TEMPERATURE" number | "FALLBACK" number | "RECALL" | "TOP" integer } ;

Testing

  • Full build passes: engine 269, database 356, server 41.
  • RouterTest, on generated trees:
    • At full mass, routing matches the exact top-k and reports recall 1.
    • Reported recall equals the measured overlap.
    • As the mass grows, more leaves are examined and recall never drops, reaching 1 at full mass.
    • Covers the entropy fallback, min/max limits and a reversed calibration.
  • HierarchyTest checks the structure after build, refresh, purge, continuous upkeep, rebuild and restart:
    • every current section with a vector is exactly one leaf;
    • fanout and capacity limits hold;
    • counts add up;
    • each centroid equals the mean of its leaves.
    • It also shows that reference vectors steer the route and that drop removes every region and centroid.
  • HierarchyStatementTest covers the HQL. SecurityTest#hierarchiesStayInsideTheTenant covers tenants and roles.
  • Mutations: 7 of 7 deliberately broken rules were caught by a test. One of them exposed a gap in the build test, which now includes nodes with several revisions.
  • Live over the wire, two tenants, sections with explicit vectors:
    • ROUTE at full mass returns the exact top 5 on 10 of 10 queries.
    • The reported recall is right on 10 of 10.
    • At mass 0.5 it examines about 2 to 3 of 30 to 50 sections.
    • All of this holds after a purge plus refresh, and after SIGKILL with 486 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.

CREATE HIERARCHY groups the sections that have a vector in one model and
view into nested regions by recursive spherical k-means. REFRESH (or
CONTINUOUS upkeep after each relevant commit) follows new revisions,
purges and vector changes from the change feed: sections are inserted by
descending to the most similar centroid, overfull regions split with
2-means, emptied regions go away and centroids on changed paths are
recomputed. Region and leaf records are the truth; child, document and
hierarchy indexes are derived from them.

ROUTE expands regions best first. At each step it turns calibrated child
scores into a softmax, keeps the smallest set holding MASS of the
probability (within MIN and MAX), and widens to MAX when the entropy says
the children cannot be told apart. It reports regions scored and
expanded, sections examined, entropy, kept mass, fallback use and, with
RECALL, recall against an exact ranking. REFERENCE REGION and CALIBRATE
HIERARCHY load reference vectors and per-level calibration trained
outside the database.
- Centroids and reference vectors move from payload vector records to a
  region-vectors slot in 64-component pieces. Replaced versions are freed
  with their tree pages, so a purged section's direction no longer
  survives in an old centroid payload, the payload files stop growing
  with every refresh, and no payload quota is charged. The public
  region vector methods on Writer are gone.
- Leaves remember their vector's payload, so replacing a section's
  vector in place moves the leaf and fixes its centroids.
- Hierarchy ids derive from tenant and name: two concurrent creates of
  one name conflict.
- Continuous upkeep refreshes only hierarchies of the tenants a commit
  touched, writes nothing when nothing changed, and holds the change
  feed only while a hierarchy has relevant changes left to read. Closing
  waits for a refresh in progress.
- Calibration scale and shift are bounded, temperatures are at least
  0.001, the default depth is 64, RECALL charges its full scan up front,
  REGIONS OF HIERARCHY needs a tenant admin and SHOW HIERARCHIES hides
  counts from users limited by type grants.
@venkat1701

Copy link
Copy Markdown
Collaborator Author

Pushed fixes from a review pass: centroids and reference vectors now live in their own tree slot instead of payload files, so purged content can't survive in an old centroid and refreshes no longer grow the payload store; replaced section vectors move their leaf; hierarchy names are unique under concurrent creates; continuous upkeep only touches hierarchies of the affected tenant, writes nothing when nothing changed and no longer holds the change feed; calibration and temperature are bounded; REGIONS OF needs a tenant admin. Full build: engine 269, database 361, server 41; each new test fails with its fix reverted; live run green before and after SIGKILL.

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