Memory stores and row expiry
memory, expiry, §M14.24 R1
Ez az oldal a nucleus saját dokumentációjából készül, ezért angol nyelvű.
Memory stores. CREATE MEMORY STORE [IF NOT EXISTS] notes USING m [WITH (merge = 0.95, analyzer = 'simple')] makes an ordinary table of
known columns — id uuid (primary key, uuidv7()), subject text
(whose memory: a user, an agent, a project), kind text ('fact'),
content text, embedding vector GENERATED BY EMBEDDING (content) USING m (§8.24), confidence real (1), seen integer (1), valid_from timestamptz (now), valid_to timestamptz, expires_at timestamptz,
source text, replaces uuid, created and last_seen timestamptz —
with indexes notes_subject, notes_embedding (HNSW, cosine) and
notes_content (text, the analyzer given), its expiry set to
expires_at and its subject columns to subject (§8.29). WITH (sealed) (or sealed = true; a bare option is true, as PostgreSQL’s)
makes a sealed store (§8.40, K4c): the table stored sealed, the
subject index alone — no HNSW or text index, which would hold the texts
and vectors in the clear — and the analyzer kept in its MemorySpec.
The table record (version 17) keeps a store’s
MemorySpec (model, merge; from version 21 the analyzer of a store
without a text index) and any table’s expiry column after the
chunk spec. merge is a cosine similarity from 0 to 1 (22023
otherwise, as for an unknown option); CREATE on the schema is needed,
and the model must exist (42704). The store is made by SQL the module
writes (memory::create), so it is a table like any other: its rows are
queried, granted, policed by row-level security, dumped and dropped as
any.
REMEMBER. REMEMBER text FOR subject IN notes [AS kind] [CONFIDENCE c] [VALID FROM t] [EXPIRES t] [SOURCE s] [REPLACING id] (the
clauses in any order, each once — 42601; every value an expression or
a parameter) returns one row, id and action:
- With
REPLACING id, the subject’s current memoryidis closed (valid_to = now(), kept as history; none:42704) and the new one added withreplaces = id—replaced. Whether a fact contradicts another is the model’s judgement: the caller says which it replaces. - Else a current memory (
valid_to IS NULL, not expired) of the subject with the same text, or else the nearest by the store’s model whose cosine similarity is at leastmerge— through the HNSW index, and among the subject’s memories whose vectors the queue has not made yet, their texts embedded here (the model’s cache has them: theirREMEMBERembedded them), so a merge does not depend on the queue’s pace — is merged:seen + 1,last_seen = now(), the greater confidence (none given keeps it) —merged. - Else a memory is added —
inserted.
A NULL text or subject is 23502; a table that is not a store, 42809.
Every step is an ordinary statement run in the caller’s transaction
(session::execute_statement) under its privileges and row-level
security: a merge never reaches a row the caller may not see (it adds a
memory of its own), a replace finds none to close, and WITH CHECK
applies to what is added. The statements are parsed once per store
(memory::plans). Two concurrent REMEMBERs of the same new text may
both add it (no constraint says a text is one memory); seen then
counts on whichever a later one finds. Its clauses are bound as one
query of casts (memory::clauses), which also gives a prepared
REMEMBER’s parameter types.
recall. recall('notes', subject, query [, k]) in FROM (the
store a text literal, 42804; 3–4 arguments, 42883) is bound as the
query memory::recall_query — $1… of it replaced by the call’s
arguments while it is bound (Binder::expansions), an argument itself
bound outside the expansion, so the caller’s parameters pass through:
SELECT id, kind, content, confidence, seen, valid_from, source, created, last_seen,
rrf(score(content @@ query), embedding <=> embed(query)) AS score,
snapshot_commit() AS as_of
FROM notes WHERE subject = $subject AND <current> ORDER BY score DESC LIMIT k -- k: 10
— the subject’s current, unexpired memories ranked by reciprocal rank
fusion of BM25 and the vectors’ nearness (§8.20), each side within the
subject: analyzed, the text and HNSW indexes rank among the subject
index’s rows; not analyzed, the subject’s rows are read by its index
(§8.17) and the words checked on them. A store without its text
index (a sealed one) is ranked exactly over the subject’s current
memories alone: score(content @@ query) becomes bm25(content, query, (SELECT bm25_stats(content, query, analyzer) FROM notes WHERE subject = $subject AND <current>), analyzer) — BM25 by those rows’ statistics,
not the store’s — and the vectors’ nearness is computed for each row.
It is a query: the
caller’s privileges, row-level security and plan apply. Deleting a
memory is DELETE; the drivers’ forget and the MCP tool are that.
Citations and read records (§M14.24 R5). recall’s as_of is
the commit its statement read (snapshot_commit()): a memory is cited
by its id and as_of, and SELECT … FROM notes AS OF COMMIT as_of WHERE id = … reads what was cited — however it was replaced, merged
into or deleted since — while the history kept by time reaches it
(§8.7, the server’s --history); BEGIN READ ONLY AS OF COMMIT as_of
then recall again gives the whole answer as it was. SET request_id = 'gen-42' (at most 200 bytes on one line, 22023) records each query
the session runs while it is set — not other statements — in the audit
log (§9) as a record of kind read, apart from the query’s
transaction (a rolled-back block keeps it): the request id, the commit
read, the tables its privilege checks looked at, the ids it returned
(its id column’s values, up to 1 000), its parameters as literals and
its text. nc_catalog.reads shows them (request_id, at,
user_name, as_of, tables, ids, parameters, statement,
erasure): an administrator’s all, a role’s own. FORGET SUBJECT
reaches both: its purge removes the subject’s versions however recent,
so a time within the retention no longer reads them (the past is
rewritten for an erasure, as PLAN’s “overriding retention” asks), and a
read record holding the subject’s value (a parameter, the statement’s
text) is blanked like any audit record’s detail, its erasure named.
A read record is a commit of its own: +0.5–0.7 ms to a recall
statement, +0.05 ms a question in batches of 16
(tests/recall_bench.rs with REQUEST_ID). Tests:
tests/citations.rs.
Batched recall and its latency (§M14.24 R4). A model’s several questions are one statement — one parse, one snapshot:
SELECT n, r.id, r.content, r.score
FROM unnest($2::text[]) WITH ORDINALITY AS b(q, n),
LATERAL recall('notes', $1, q, 10) r
ORDER BY n, r.score DESC
— each question’s rows those of its own recall (BM25’s statistics
subquery reads the enclosing row’s question: text::score::bind lifts
the query into it and marks it correlated when it reads one). In the
process (release, real files, the hashing model, 100 000 memories of
1 000 subjects; tests/recall_bench.rs, docs/benchmarks.md “Memory
stores”): recall(…, 10) 0.81 ms at the median a question, a statement
parsed once and run with parameters; 0.63 ms a question in batches of
16. embedding.pending = wait costs nothing extra once the queue is
empty: a round vacuums the queue’s range (§8.24).
Row expiry. ALTER TABLE t SET EXPIRY (column) — a timestamptz,
timestamp or date column (42804; unknown, 42703; the owner’s;
on a partitioned table, not a partition, 0A000) — makes rows whose
value has passed be deleted in the background; DROP EXPIRY stops it.
expiry::expire(kv, registry, table, now) runs DELETE FROM t WHERE column <= now for each table with an expiry, each in a transaction of
its own as the system (row-level security does not hide a row from its
expiry), tried again on a conflict (40001), a table dropped meanwhile
skipped: ordinary deletes, so foreign keys’ actions, indexes and derived
rows follow. The server’s maintenance thread runs it every minute after
the storage policies (nucleus_server::maintain_with, which takes the
edition’s registry — so a module’s types and models are known;
maintain_until uses the built-in one). A row expired but not yet
deleted is still seen by queries; recall leaves it out itself. Tests:
tests/memory.rs, slt/63_memory.slt.