Agent memory: the recall index on disk, format 1 (draft)¶
Agent memory keeps its records (entities, aliases, claims, episodes, ...) in two LSM trees,
durable and session (storage-format.md), under order-preserving tuple keys whose
first component is the tenant. Until this format, the structures RECALL searches (term
postings, vectors, the mention, thread and day lists, per-record metadata) were built in
process memory from the records at every open and held whole; their size grew with every
record, without a bound. Format 1 stores them as entries of the same trees, next to the
records they describe, and holds in memory only what a configured budget allows.
This document says what the entries are, when they are written, what a reader may keep in memory, how an existing store gets them, and what erasure leaves behind.
Requirements this format meets¶
- Bounded memory. The process memory the index uses (outside the trees' block cache and
memtables, which have their own bounds) is at most the configured index budget, whatever
the number of records. The budget has a documented default (256 MiB) and is a server
setting (
recall_index_mb); the server reports the budget and the bytes held (GET /v1/admin/stats, sectionmemory.index). - Same answers. RECALL, and every call that reads the index, returns what it returned with the whole index in memory: the same candidates, scores, order and context. The budget changes speed only.
- One commit. A record and its index entries are written in one commit (one write-ahead log record) of the record's tree. A crash leaves both or neither; nothing is rebuilt at open.
- Erasure. Erasing a subject leaves no index entry of the erased records, and no readable content of them in any file, log or backup (section "Erasure").
- Explicit upgrade. A store written before this format is not indexed silently at open: opening it fails with an error that names the build tool (section "Upgrade").
Keys¶
Components are encoded as in the memory record keys: each component's bytes with 00
escaped as 00 FF, closed by 00 01, so that byte order is component-wise order. T is
the tenant, id a record id as 8 bytes big-endian, tok a term token (below), eid an
entity id, th a thread name. Every index key's second component starts with Q; record
keys never do.
| entry | key | tree | value |
|---|---|---|---|
| document | T / QD / id |
the record's | document entry (below) |
| posting | T / QP / tok / id |
the record's | tf:varint dl:varint |
| masked posting | T / QN / tok / id |
the record's | tf:varint dl:varint |
| vector | T / QV / id |
the record's | sealed vector entry (below) |
| mention | T / QM / eid / id |
the record's | empty |
| active claim | T / QA / eid / attr / id |
durable | empty |
| thread | T / QT / th / inv_at:u64 BE / id |
the record's | empty |
| day | T / QY / day:u32 BE / id |
the record's | empty |
| statistics delta | T / QS / ts:u64 BE |
the record's | statistics (below) |
| format marker | FF FF 51 (no tenant) |
each tree | format marker (below) |
- A document is a claim (durable tree) or an episode (durable or session tree, as the episode). Its document entry, postings, vector, mentions and days are in the same tree.
tfis how often the term occurs in the document's indexed text,dlhow many terms that text has (BM25 needs both for each posting; keepingdlbeside the posting saves a read per posting).inv_atisu64::MAX - at_ms, so a thread lists its episodes newest first.dayis the day number (days since 1970-01-01, UTC) plus 2^31. An episode has a day entry for the day it was written, and for each day its text speaks of (at most 41 days from the start of each span named in the text, the rule of the memory's time parser).- Thread and day entries exist for episodes only; claims have a mention entry for their entity, episodes one per declared and per detected entity.
- An active-claim entry exists exactly while a claim's status is active (
attras in the claim's record key): a write decides against the active claims of its attribute without reading the attribute's history.
Term tokens¶
The indexed text of a claim is <entity name> <attribute with "_" as " ">: <value>; of an
episode its text (its summary once cooled). Its terms are its words (maximal runs of
Unicode letters and digits, lower-cased; apostrophes are dropped, any other character ends
a word), without the stop words of the analyzer oliviadb-terms-v1, each with one suffix
removed (the first of ing, ed, es, s that ends the word, when at least three
characters remain). The stop word list is part of the analyzer and is given in
appendix A.
A term's token is the first 16 bytes of HMAC-SHA-256 under the tenant's key (the key that
also makes alias pseudonyms), of the bytes "Q" 00 followed by the term's UTF-8 bytes. The
"Q" 00 prefix keeps tokens apart from alias pseudonyms of the same words. No term appears
in plain text in any key or value.
The masked class (policies that mask personal data) indexes the text after the configured
masker has replaced the personal data it finds; its postings are the QN entries, its
vector the second vector of the vector entry.
Document entry¶
doc = version:u8 (1) kind:u8 (0 claim, 1 episode) lp(record key)
lp(author) visibility:u8 (0 private, 1 team, 2 tenant) lp(team)
lp(thread) at_ms:u64 recorded_ts:u64
superseded_ts:opt_u64 valid_from:u64 valid_to:opt_u64
status:u8 (0 active, 1 superseded, 2 disputed, 3 retracted) lp(retraction reason)
trust:f32 conf:f32 lp(attribute) cold:u8 risk:f32
n:varint lp(eid)*n ; declared entities
m:varint lp(eid)*m ; detected entities (mentions)
dl:varint t:varint tok*t ; indexed length, distinct term tokens
mdl:varint mt:varint tok*mt ; the same for the masked text (mt = 0 without a masker)
opt_u64 = 0:u8 | 1:u8 u64
Integers are little-endian unless the key says big-endian; lp is a LEB128 length and
the bytes. The fields are the record's own unsealed fields (the record key holds the same
author, thread and times); the entry holds no text. The token lists let a document's
postings be deleted when its text can no longer be read (after its subject key is
destroyed).
Vector entry¶
Only for an embedder whose vectors are stored. The built-in embedder oliviadb-hash-v1
(feature hashing of terms and trigrams, microseconds per text) is not: a store indexed with
it has no vector entries, and a reader that needs a document's vector embeds the
document's indexed text, opened from its record, again. Every other embedder's vectors are
stored, because computing them again would cost a model run per document.
The value is sealed exactly as the record's text: under a fresh data key, wrapped so that every key the record's text needs (each subject's key and, for an episode, the thread key) is needed to open it, with the record id as associated data (the memory's sealing: XChaCha20-Poly1305, XOR shares). The plain bytes are
vec = version:u8 (1) lp(model id) dims:u32
x:f32*dims ; the text's vector
has_masked:u8 [x:f32*dims] ; the masked text's
x is the embedder's unit vector of the indexed text. A reader derives its 8-bit copy
(scale = max|x_i| / 127, q_i = round(x_i / scale) clamped to ±127; scale = 1 for a
zero vector, and qnorm = scale · |q|); the 8-bit copy is what a search scans first; the
exact score of a candidate is computed from x, so the search's result is that of an exact
scan (the bound that proves it is the one documented with the memory's vector index).
Statistics delta¶
BM25 needs, per tenant, the number of documents and their total length. Rewriting one counter per commit would leave a version per commit in the tree's history, so each commit that adds or removes documents writes a new key instead:
stats = version:u8 (1) docs:svarint len:svarint mdocs:svarint mlen:svarint
max_id:u64 max_time:u64
docs and len are the commit's change of the document count and total indexed length
(signed, zigzag LEB128), mdocs, mlen the same for the masked text, max_id the largest
record id and max_time the latest at_ms or valid_from the commit wrote (0 when none).
A reader sums all deltas of a tenant in both trees. A writer may fold a tenant's deltas
into one (in one commit: delete the folded keys, write their sum under the newest of their
timestamps); the sum is unchanged by folding.
Format marker¶
marker = magic:"OLVQ" format:u16 (1) state:u8 (0 building, 1 complete)
lp(embedder model id) dims:u32 lp(masker id) lp(analyzer "oliviadb-terms-v1")
cursor:lp ; building: the last record key indexed in this tree
A tree whose marker is complete holds the index entries of every record in it. The
embedder, masker and analyzer recorded in the marker are the ones every entry was written
with; opening the store with another embedder, masker or analyzer is refused, with an error
naming both and the build tool (vectors and tokens of different models do not compare).
Writes¶
Every call that writes a record writes, in the same commit, the index entries that follow from it:
| record change | index change (same commit) |
|---|---|
| a claim or episode is written | document entry, postings, masked postings, vector entry, mentions; for an episode thread and day entries; a statistics delta |
| a claim is superseded, disputed or retracted, an episode retracted or its risk changed | the document entry rewritten (postings unchanged); a claim's active-claim entry deleted or written with its status |
| an episode is cooled (its text replaced by its summary) | document entry, postings, masked postings, vector entry and day entries replaced (old ones deleted by the old entry's token list and days); a statistics delta |
| a record is deleted (erasure, consolidation) | every entry of the document deleted (the token lists, mentions, thread and days found from its document entry); a statistics delta |
No index entry is written in any other commit, except by the build tool (below).
What a reader keeps in memory¶
Anything it likes within the budget: decoded document entries, posting lists of tokens, the 8-bit copies and scales of vectors, the per-tenant statistics. A cached item is updated or dropped by the commit that changes its entries, before that commit's writer is answered, so a reader never sees a cached state older than the trees. What is not cached is read from the trees. The result of every call is independent of what is cached (requirement 2); the reported resident bytes count every cached item.
Entity tables (names, aliases, operational attributes, restrictions) are not part of this index: they stay in memory as before, one entry per entity and alias.
Reads and snapshots¶
A call that reads the index (RECALL and the navigation calls) reads the latest state: it
takes one read version r of each tree when it starts (the newest commit applied there)
and reads every index entry and every record it uses at r. Index entries and records
of a tree are written in the same commits, so at r they agree. "As of" questions
(valid_at, known_at) are answered from the times stored in the records and document
entries (claims keep every version as its own record), never by reading the trees at an
older version; there is no call that reads the index at a version older than its start.
Index keys keep a short history (section "Erasure"); the short watermark never passes a read version in use: the writer moves it to the oldest read version of the calls still running (the newest commit when none runs), so no running call loses a version it reads. A transaction over memory records (agent state's optimistic transactions do not read the index) gets the same rule if it ever reads index entries: its read version holds the watermark back while it runs.
A test runs RECALL calls that hold their read version while other writers add, supersede and erase records and compactions run with the short watermark advancing, and checks that each call's answer is the one an index at its read version gives.
Erasure¶
Subject erasure destroys the subject's key first, then deletes the subject's records and every index entry of those records (by their document entries), in the same commits as the records.
- Vector entries are sealed under the erased key: from the moment it is destroyed, no copy of them in any table, log or backup can be opened, as for the records' text.
- Document entries and postings hold no text. A posting shows that record
idcontained a term with tokentok; a token can be tested against a guessed term only with the tenant's key, the same protection alias pseudonyms (T / A / HMAC(name) / eid) have. Destroying the tenant's key ends both. How long a deleted entry can still be found: - Write-ahead log: until the memtable holding the entry or its deletion is flushed; a flush removes the log files it covers.
- Live table files: index entries keep no history. The tree's history window (the
versions kept for "as of" reads of records) does not apply to keys whose second
component starts with
Q: a compaction drops every overwritten version of such a key, and a deleted one with its deletion marker when it writes the last level. A deleted index entry is therefore gone from the live files once each level that holds a version of it has been compacted once (at most one compaction per level of the tree, 7 by default), independent of the history window. - On demand:
oliviadb-server memory-index purge --dir <dir>(and the admin callPOST /v1/admin/memory/index/purge) flushes both trees and compacts them to the last level, after which no live file and no log of the store holds a deleted index entry.POST /v1/admin/subjects/erasewith"purge": trueruns it after the erasure. - Backups taken before the erasure keep the entries until the backup's own retention ends; they are never rewritten.
- Memory: cached items of the erased records are dropped before the erasure returns.
A model test checks the requirement: after random sequences of writes, deletions and
erasures, the trees hold no index entry of an erased or deleted record, no file holds a
readable vector of an erased record, and RECALL answers as the in-memory index answers.
A second test erases a subject, purges, and finds no QD, QP, QN, QV, QM, QA, QT
or QY key of an erased record in the bytes of any file of the store.
Upgrade¶
A tree without a marker that holds memory records was written before this format. Opening memory over it fails with
the recall index of <dir> is not on disk yet (store written before index format 1):
run `oliviadb-server memory-index build --dir <dir>` (resumable) and open again
The build tool indexes each tree in record-key order, in commits of at most 1024 records,
each commit also writing the marker with state = building and the cursor; run again
after an interruption it continues after the cursor. When a tree is done it writes the
marker complete. An empty tree (a new store) gets a complete marker at its first open.
Opening memory while any marker says building fails with the same error.
Appendix A: stop words of oliviadb-terms-v1¶
a, an, the, and, or, of, to, in, on, at, for, with, is, are, was, were, be, been, it, that, this, as, by, from, do, did, does, what, when, where, who, how, which, i, you, he, she, they, we, my, your, his, her, their, our, me, him, them, have, has, had, not, but, so, if, about, would, could, should, will, can, there, any, all, some