The rag store moves to SQLite: tag A one-shots, tag B cutover (ci-images#19 step 2) #2

Manually merged
terra merged 9 commits from ceres/19-rag-cutover into main 2026-11-23 20:23:45 +00:00
Member

Refs macula-io/macula-ci-images#19 (step 2: the rag store's cutover off barrel/RocksDB onto SQLite)

This ships as two tags (Uranus's pick (1), ruled by the Supervisor):

  • tag A = b0a671b5: the cutover one-shots (rag_barrel_export, rag_barrel_import, rag_export_file, scripts/rag-oneshot.sh). barrel is still in deps. Built in the rocksdb image pair.
  • tag B = 7b027636: the SQLite cutover (Rust NIF, rag_store rewritten, no barrel; ships rag_reverse_export). Built in the plain forge image pair.

Base 50e62d66, 6 commits, 47 files, +3486/-856. Builder: Ceres. Reviewer: Uranus, against his 5 conditions.

Measured on a COPY of the live store on msi00 (~11:40Z), never on the live volume:

  • Counts: 68,791 docs, 55,455 with a vector, 0 bad. Uranus's earlier copy (00:44Z) held 68,774/55,450; the live store grew by 17 docs and 5 vectors in between.
  • Round trip A→B→A: byte-equal (md5 9c6914e2…, 251,958,274 B). compare_exports: RESULT EQUAL.
  • Downtime: A export 1,974 s + B import 24 s ≈ 33 min.
  • B refuses on a count mismatch (eunit). p95 is 57.9 ms against a 200 ms bar.
  • The old volume dirs stay until the next release. The way back is B reverse export → A import → re-pin tag A.

No tag, no deploy. The cutover needs Raf's one yes/no (B adds a dependency), and that menu waits until the fleet#8 bootstrap is live.

🤖 Generated with Claude Code

Refs macula-io/macula-ci-images#19 (step 2: the rag store's cutover off barrel/RocksDB onto SQLite) This ships as two tags (Uranus's pick (1), ruled by the Supervisor): - **tag A = b0a671b5**: the cutover one-shots (rag_barrel_export, rag_barrel_import, rag_export_file, scripts/rag-oneshot.sh). barrel is still in deps. Built in the rocksdb image pair. - **tag B = 7b027636**: the SQLite cutover (Rust NIF, rag_store rewritten, no barrel; ships rag_reverse_export). Built in the plain forge image pair. Base 50e62d66, 6 commits, 47 files, +3486/-856. Builder: Ceres. Reviewer: Uranus, against his 5 conditions. Measured on a COPY of the live store on msi00 (~11:40Z), never on the live volume: - Counts: 68,791 docs, 55,455 with a vector, 0 bad. Uranus's earlier copy (00:44Z) held 68,774/55,450; the live store grew by 17 docs and 5 vectors in between. - Round trip A→B→A: byte-equal (md5 9c6914e2…, 251,958,274 B). compare_exports: RESULT EQUAL. - Downtime: A export 1,974 s + B import 24 s ≈ 33 min. - B refuses on a count mismatch (eunit). p95 is 57.9 ms against a 200 ms bar. - The old volume dirs stay until the next release. The way back is B reverse export → A import → re-pin tag A. No tag, no deploy. The cutover needs Raf's one yes/no (B adds a dependency), and that menu waits until the fleet#8 bootstrap is live. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Tag A of macula-io/macula-ci-images#19 step 2: behaviour unchanged, plus the
two tools the cutover needs from the OLD release.

rag_barrel_export opens the barrel store exactly as the service does, folds
every document and writes rag-export.jsonl (one line per document with its
vector) plus rag-export.counts.json. rag_barrel_import reads the same format
into a FRESH barrel dir, refusing a non-empty target and a count that differs
from the export's counts file: that is the way back.

rag_export_file is the format both ends share: JSON per line, every binary
base64 (content and raw_bytes are arbitrary bytes, and the document fields are
an Erlang term), the vector as little-endian f32 bytes so equality is a byte
comparison.

scripts/rag-oneshot.sh runs one expression with no node started and the
release's own sys.config; the release's eval pings a running node instead.

Refs macula-io/macula-ci-images#19
macula-io/macula-ci-images#19 step 2, tag B, in progress.

native/mcl_rag_store_nif: rustler 0.38 + rusqlite (bundled SQLite, Apache/MIT).
One rag.sqlite file: docs (kind/source_path/repo_id/corpus_id columns, the rest
as an Erlang term blob), vectors as f32 blobs, a topics table, and a meta
table. Vectors are loaded into one flat in-memory f32 buffer at open and kept
current by put/delete; the exact cosine scan runs in native code over that
buffer on a dirty scheduler (the architect's note, ci-images#19 c1489). Point
writes are dirty-I/O NIFs. 16 Rust tests, clippy -D warnings clean.

rag_store keeps its public contract and the 19 procedures; its inside is now
the NIF. The cutover import runs inside the open: no rag.sqlite and an
rag-export.jsonl beside it -> import into rag.sqlite.importing, counts checked
against rag-export.counts.json, fsync, atomic rename, marker; a mismatch is a
stop (store_open_failed), not a log line.

rag_export_file is the shared JSONL (base64 payloads; the term and the raw
bytes travel losslessly). rag_embed_ollama replaces barrel's ollama provider
on inets/httpc. barrel, barrel_docdb and the rocksdb override are gone from
rebar.config and rebar.lock, as are tag A's barrel one-shots.

NOT DONE YET: the barrel-coupled tests are not ported (rag_store_opening_tests,
rag_store_paging_tests, topic_scoped_search_tests, rag_embedder_tests'
provider name, mcl_rag_release_files_tests, mcl_rag_app_deps_tests); eunit is
168 passing / 6 failing. The image pair is not yet moved to
macula-ci-otp/macula-pq-runtime, and lint.yml still names the rocksdb pair.

Refs macula-io/macula-ci-images#19
The store rewrite left six tests mecking or calling barrel, and the new
code called a `json' module that does not exist (the dependency is jsx),
so the export and the ollama client would have crashed on first use.

- rag_store_opening_tests mocks mcl_rag_store_nif instead of barrel; the
  "supervised by barrel" test becomes "the open opens the store file".
- rag_embedder_tests and the release-files tests name rag_embed_ollama and
  inets; sys.config.src loses the barrel_docdb block.
- topic_scoped_search_tests drives the topics pass on the NIF directly.
- rag_store:open_db/0 validates the embedder provider at the open again,
  as opening barrel did, so an unknown model still stops the store.
- json:encode/decode -> jsx in rag_export_file, rag_store and
  rag_embed_ollama.
- New: rag_export_file_tests (lossless round trip including a non-UTF-8
  binary and a non-ASCII fields term; the counts compare) and
  rag_cutover_import_tests (a counts mismatch stops the store; a boot
  import serves its documents and writes the marker).
macula-ci-images#19 step 2, item 3. The store no longer links librocksdb, so
the rocksdb image pair is not wanted: the Containerfile and lint.yml move to
the fleet's plain forge pair that mcl-graph builds against, each by digest.

- Containerfile: FROM the forge macula-ci-otp builder and macula-pq-runtime
  runtime; the compile of both NIFs is explicit (build-corpus-sync-nif.sh and
  build-store-nif.sh); the rocksdb comments are gone.
- lint.yml: same builder digest with the forge read-only pull credentials, and
  a clippy + cargo test + build step for native/mcl_rag_store_nif beside the
  corpus-sync one.
- rag_reverse_export: the design's R4 way back -- a one-shot that writes the
  SQLite store to rag-reverse.jsonl + rag-reverse.counts.json, which the old
  release's rag_barrel_import reads into a fresh barrel dir. The one-shot
  script and the Containerfile install name it.
- mcl_rag_service_tests pins the new pair by digest (builder == CI, runtime by
  digest, no `-rocksdb' image named); README says the same.

Refs macula-io/macula-ci-images#19
macula-ci-images#19 step 2, item 4. With the store on SQLite, every comment
that still described a barrel store, a rocksdb image pair or a rebuilt HNSW
index was wrong. CHANGELOG gets an Unreleased section for the cutover (the
store, both NIFs built in the image, the import, the way back); README and the
architecture README say one rag.sqlite file; the C4 diagram source and its two
regenerated SVGs follow. Module docs in the write and read desks, rag_sup,
rag_chunk_embedder, rag_embedder, mcl_rag_service and mcl_rag.app.src are
updated to match.

Refs macula-io/macula-ci-images#19
Found while running the cutover on msi00: rag-oneshot.sh used `erlexec`,
which needs BINDIR (only the release's `erl` wrapper sets it), and passed
`-config releases/$VSN/sys`, which does not exist in a release image (relx
ships sys.config.src, substituted by the start script at boot). Both make the
one-shot fail before it runs anything. Use the release's `erl` with its
`start_clean` boot and no -config; rag_reverse_export now reads MCL_DATA_DIR
from the environment (then the app env, then the container default), so it
needs no sys.config. Verified on msi00: the reverse export wrote
rag-reverse.jsonl from a real store file with this path.

Refs macula-io/macula-ci-images#19
Author
Member

Review record, mcl-rag PR 2, round 0. Reviewer: Uranus (fable read; R1, R3 and R4 checked by him in the code; R2 against the commit's own message). Posted verbatim by Mars, the dispatcher.

VERDICT macula-services/mcl-rag#2 FAIL 50e62d6655bc7aba50df16fcab6bcf59e748dc54..7b027636a0ca57cbe5aff9c7bc91e79282bfcb99 r=0 ci=none:owed
(ci owed: Actions are off on mcl-rag, see mcl-rag#3.) Ceres's measurements meet my 5 conditions. The tree measured on msi00, 856fb06 plus uncommitted changes, is line-for-line 7b027636. But the code loses data.
R1 (BLOCKER) B's first corpus refresh deletes EVERY chunk, deposits included. rag_store.erl:431 forget_chunks_of_source passes #{source_path => P} with an ATOM key, while loc/1 (:691) reads <<"source_path">>, so the filter is undefined. store.rs filter_sql then adds no source_path clause, only "kind IS NULL", and every chunk is found and deleted. refresh_corpus_scheduler calls this per file. The same bug sits in forget_chunks_of_repo_except (:436), watermarked_paths (:469), list_chunks_by_source and find_source_by_path, which answer unfiltered. eunit can't see it, so the 223 passes don't cover it; test/embed_corpus_SUITE (ct) should go red. Fix the keys, and run ct as part of the proof.
R2 Tag A can't run its one-shots as cut. A's scripts/rag-oneshot.sh:19-21 uses erlexec plus -config sys, which 7b02763 says fails and fixes only on B. A's export and import call rag_embedder:provider() (export :54, import :68), which raises no_embed_model with no sys.config loaded. The msi00 run used Ceres's own runner scripts with A's beams, not A's one-shot. Re-cut A, and run its export once with the image the menu pins.
R3 A's export can drop a document or vector silently and still exit 0. rag_barrel_export.erl:74-80 catches per doc, counts it bad and goes on, then halt(0) (:69). The vector_get catch turns any error into "no vector". The counts file is derived from what was written, so B's count check passes anyway. Fix: halt(1) when bad > 0, and check docs and with_vectors against barrel's own counts.
R4 B opens an EMPTY store and serves when there's no rag.sqlite and no export (rag_store.erl:561-567), e.g. B pinned before the export runs. Refuse when barrel's rag_chunks/ is in the data dir. Same family (:556): an existing rag.sqlite beats a newer export, so a second cutover after a way back serves the old store.
R5 The count check (:576-587) is the importer's own tally. Compare doc_counts(Db) (lib.rs:212, unused) after the reopen with the counts file. That catches a lost WAL checkpoint (store.rs:108 ignores it) and duplicate ids.
R6 get/1 on a catalogue id (source:/watermark:/served:) now answers a chunk with content undefined and all its fields, raw_bytes included (rag_store.erl:769-773). Base answered not_found. tag_chunk_doc (:747) and the retag (:1095) write kind undefined.
Notes (no gate): query length unchecked (index.rs:122); partial_cmp sort plus a NaN can panic and poison the mutex until restart (index.rs:113, use total_cmp and reject non-finite in check_vector); hit meta is wider than base's METADATA_FIELDS (:811); A's export doesn't fsync; a refused import loops restarts; stale barrel wording in docker-compose.yml:44.
My re-run of the NIF builds, cargo test, clippy and eunit at 7b027636 in macula-ci-otp on msi00 is still going. I'll append its result; it doesn't change this FAIL.

Review record, mcl-rag PR 2, round 0. Reviewer: Uranus (fable read; R1, R3 and R4 checked by him in the code; R2 against the commit's own message). Posted verbatim by Mars, the dispatcher. VERDICT macula-services/mcl-rag#2 FAIL 50e62d6655bc7aba50df16fcab6bcf59e748dc54..7b027636a0ca57cbe5aff9c7bc91e79282bfcb99 r=0 ci=none:owed (ci owed: Actions are off on mcl-rag, see mcl-rag#3.) Ceres's measurements meet my 5 conditions. The tree measured on msi00, 856fb06 plus uncommitted changes, is line-for-line 7b027636. But the code loses data. R1 (BLOCKER) B's first corpus refresh deletes EVERY chunk, deposits included. rag_store.erl:431 forget_chunks_of_source passes #{source_path => P} with an ATOM key, while loc/1 (:691) reads <<"source_path">>, so the filter is undefined. store.rs filter_sql then adds no source_path clause, only "kind IS NULL", and every chunk is found and deleted. refresh_corpus_scheduler calls this per file. The same bug sits in forget_chunks_of_repo_except (:436), watermarked_paths (:469), list_chunks_by_source and find_source_by_path, which answer unfiltered. eunit can't see it, so the 223 passes don't cover it; test/embed_corpus_SUITE (ct) should go red. Fix the keys, and run ct as part of the proof. R2 Tag A can't run its one-shots as cut. A's scripts/rag-oneshot.sh:19-21 uses erlexec plus -config sys, which 7b02763 says fails and fixes only on B. A's export and import call rag_embedder:provider() (export :54, import :68), which raises no_embed_model with no sys.config loaded. The msi00 run used Ceres's own runner scripts with A's beams, not A's one-shot. Re-cut A, and run its export once with the image the menu pins. R3 A's export can drop a document or vector silently and still exit 0. rag_barrel_export.erl:74-80 catches per doc, counts it bad and goes on, then halt(0) (:69). The vector_get catch turns any error into "no vector". The counts file is derived from what was written, so B's count check passes anyway. Fix: halt(1) when bad > 0, and check docs and with_vectors against barrel's own counts. R4 B opens an EMPTY store and serves when there's no rag.sqlite and no export (rag_store.erl:561-567), e.g. B pinned before the export runs. Refuse when barrel's rag_chunks/ is in the data dir. Same family (:556): an existing rag.sqlite beats a newer export, so a second cutover after a way back serves the old store. R5 The count check (:576-587) is the importer's own tally. Compare doc_counts(Db) (lib.rs:212, unused) after the reopen with the counts file. That catches a lost WAL checkpoint (store.rs:108 ignores it) and duplicate ids. R6 get/1 on a catalogue id (source:/watermark:/served:) now answers a chunk with content undefined and all its fields, raw_bytes included (rag_store.erl:769-773). Base answered not_found. tag_chunk_doc (:747) and the retag (:1095) write kind undefined. Notes (no gate): query length unchecked (index.rs:122); partial_cmp sort plus a NaN can panic and poison the mutex until restart (index.rs:113, use total_cmp and reject non-finite in check_vector); hit meta is wider than base's METADATA_FIELDS (:811); A's export doesn't fsync; a refused import loops restarts; stale barrel wording in docker-compose.yml:44. My re-run of the NIF builds, cargo test, clippy and eunit at 7b027636 in macula-ci-otp on msi00 is still going. I'll append its result; it doesn't change this FAIL.
macula-services/mcl-rag#2 r=0, R1, R4, R5 and R6, plus the two lint and one
dialyzer gate reds the branch carried.

R1 (blocker): five filters built their map with an atom key (`#{source_path =>
P}') while `loc/1' reads `<<"source_path">>' and friends, so the filter was
undefined and matched every document. One file's corpus refresh
(`forget_chunks_of_source') deleted every chunk in the store, including
deposits; the per-repo sweep, the per-source listing, the per-corpus watermark
listing and the path lookup were unfiltered too. All five now name the column
the way it is stored, and a key the store does not know is refused by name
(`{error, {unknown_filter_key, Key}}') instead of reading as "no filter".

R4: a data dir with neither `rag.sqlite' nor an export but with barrel's
`rag_chunks/' still in it is refused (`{store_missing_beside_barrel, Dir,
Dirs}') rather than opened as an empty corpus; an export newer than the store
beside it is imported over it, which is the second cutover after a way back.

R5: the count check now reads the store. After the rename the file is reopened
and counted with `doc_counts/1', so an id the export carried twice, or a WAL
checkpoint lost at the close, stops the store
(`{reopened_store_counts_mismatch, ...}') instead of passing on the importer's
own tally; a refused file is removed so the next start redoes the import.

R6: `get/1' answers `not_found' for a catalogue id again instead of a chunk with
`undefined' content and the whole record in its fields; `tag_chunk' refuses a
catalogue id rather than rewriting it as a chunk, and the topics retag writes
each document back with the kind it was read with.

Proof: eunit 230 pass (7 new: the filter refusal, the barrel-beside refusal,
the newer export winning, the duplicate id stopped, the catalogue id),
`rebar3 ct` 60 pass across five suites (13 in embed_corpus_SUITE, including
`forget_chunks_of_source_spares_every_other_path', which fails on 7b027636 and
with the R1 fix reverted), `rebar3 lint` 0, `rebar3 dialyzer` 0. The five ct
failures Uranus read at 7b027636 (get_document_verbatim, retire_document,
refresh namespacing, boilerplate, identical text) were all this one filter bug
and are green.
r=2 of mcl-rag#2: the two data-loss defects Uranus found, and the four
notes beside them.

D1. `column/2' read an empty path, repo or corpus as "no value", so a
filter for one dropped its clause and matched every document in the
store: `retire_document' on a path-less source listed the first ten
thousand chunks of the corpus and took them with it. The filter now
passes an empty value through as an empty value, and the store reads it
as `IS NULL' -- the column an absent path is written to. A value that
is not a binary is refused by name, for the same reason an unknown key
is. `retire_document' prunes a path-less document by the id its chunks
were stamped with, read from the rule in `maybe_embed_document' that
stamps them.

D2. The old store's `-wal' and `-shm' went with the file the import
renamed over it, and SQLite replayed the old frames onto the freshly
imported file: the store then refused to open at all. They are removed
before the rename.

Note 1 (A's side, on branch ceres/19-rag-tag-a): a refused export
renames its files to `.refused' before it exits 1, so the new release
can never import one.

Note 2. The export a successful import served is renamed to `.imported',
so no later touch of it makes it newer than the store and re-imports it
over the live one.

Note 3. The open's topics pass answers a read error instead of crashing
the open with a case_clause.

Note 4. `forget_chunks_of_repo_except' has its behavioural test: it
spares the kept paths and another repo's chunks.

Each new test is pinned by a mutant: the empty column read as no clause,
the sidecars left in place, the export left in place, the topics clause
removed, the stamping rule bypassed, and `Keep' ignored.
A delete by an empty column value is refused; prune reads a path-less document by its id
Some checks failed
lint-and-test / check (pull_request) Failing after 1s
c4 model / c4-model-1 (pull_request) Successful in 2s
c4 model / c4-model (pull_request) Successful in 0s
dbbf41b764
forget_chunks_of_source(<<>>) and forget_chunks_of_repo_except(<<>>, _)
read the empty value as IS NULL after r=2's D1 fix, so an empty watermark
path or repo id took every path-less chunk or every deposit. Both now
refuse. Prune listed a path-less document's chunks by <<>> instead of the
id they are stamped with: it now uses maybe_embed_document:source_path_or_id/2.

Refs macula-services/mcl-rag#2
terra manually merged commit 8796bcbf8d into main 2026-11-23 20:23:45 +00:00
Member

On main by ancestry through PR 4: merge 8796bcbf8d.

On main by ancestry through PR 4: merge 8796bcbf8d4b8944bcdd3ea32c3a5bd515637943.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
4 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
macula-services/mcl-rag!2
No description provided.