Why a Local Immutable CAS Holds the Evidence Foundation
Vector stores and graph databases are rebuildable projections; the CAS stores raw data, IR, and the CPG, and a Manifest Generation references those immutable objects.
On this page · 10 sections
A vector store is good at answering "which fragments are most similar to this question?" It is poor at answering "exactly which original bytes, which parser, and which commit produced this fragment?" Once only an embedding and a truncated piece of text remain, the system cannot fully replay the evidence chain of that moment when a parser is upgraded, chunks change, or an answer turns out wrong.
So mt_rag uses a local filesystem CAS (content-addressable storage, that is, storage addressed by the hash of the content) as its foundation of fact: raw snapshots, DocumentIR, the full CPG, and other immutable artifacts are stored by content hash; Qdrant, FTS, and Neo4j are merely rebuildable, query-facing projections.
The Short Version
- The CAS is the foundation of fact: raw snapshots, DocumentIR, and the full CPG are stored as immutable objects by content hash; Qdrant, FTS, and Neo4j are only rebuildable projections.
- A CAS digest represents only the exact bytes of the stored object; it is not interchangeable with
original_content_hashor any other hash domain. - A write publishes atomically with a unique temp file,
fsync, andos.replace, then verifies; this is safe publication within one machine's filesystem, not a distributed transaction across machines. - The CAS solves content identity, integrity, and replayability. It is not a secret manager; disk encryption, access control, backups, and a retention policy are still needed.
Knowledge Map for This Article
Four concepts need to be kept apart here: content address, hash domain, atomic publication, and lineage reference.
The CAS Stores Exact Bytes, Not "Text That Looks the Same"
The current LocalFilesystemCas uses the URI:
cas://sha256/<64 hex digest>
The object actually lands at:
data/00-cas/01-objects/sha256/<first 2 chars>/<chars 3-4>/<full digest>
Two byte-for-byte identical inputs get the same address, so deduplication comes for free; changing any single bit produces a new object. CasObjectRefV1 records the digest, byte size, media type, and encoding together, so consumers do not have to guess the content type from a filename.
A CAS digest represents only the exact bytes of the stored object. It must not be confused with original_content_hash.
Text content identity may apply normalization such as Unicode NFC and CRLF → LF, while the CAS still hashes the original bytes; a CRLF file and an LF file with the same text can share one canonical content hash and still have different CAS digests. Both results are correct, because they answer different questions.
A Hash Is Not One Field but a Set of Non-Interchangeable Domains
system_contracts.py uses HashRefV1 to make the hash type and canonicalization (that is, which normalization runs before hashing) explicit. The table lists each domain's input and what is affected when it changes:
| Hash domain | Input | Effect when it changes |
|---|---|---|
blob_sha256 |
The exact bytes in the CAS | Produces a new immutable object |
original_content_hash |
The normalized original content | Indicates that the source content changed |
record_payload_hash |
Versioned metadata chosen by the producer | Triggers a payload update, not necessarily a re-embedding |
cleaned_content_hash |
The cleaning output | Parser/cleaner changes can be tracked independently |
summary_hash |
The summary text | Does not contaminate original content identity |
chunk_content_hash |
The chunk text | Decides whether the chunk/embedding is recomputed |
Freshness, observed-at, rerank score, and LLM summaries are never allowed into original_content_hash. Otherwise a page that has not changed by a single character would manufacture new content merely by being re-observed every day, trigger re-embedding, and break idempotency.
How a Single Write Avoids a Half-Written Object
The current implementation does not open the target path directly and overwrite it. Instead it:
- Computes the full byte digest and derives the target path;
- If the object already exists, re-verifies the digest and size and returns the same reference;
- Creates a unique temporary file in the same directory, named with the PID, thread ID, and a UUID;
- Writes, flushes, calls
fsync, and sets the file mode; - Publishes atomically with
os.replace; - Reads the object back after publication and verifies the SHA-256 and size;
- Cleans up the temporary file whether the write succeeded or failed.
What this provides is safe publication within the boundary of a single machine's filesystem, not a distributed transaction across machines. The manifest remains responsible for stating which objects a given run referenced, which parser version it used, and which projections it produced.
The CAS Also Has to Defend Against Path Attacks and Silent Corruption
Content addressing does not automatically mean safety. The current implementation adds several layers of constraint:
- Neither the CAS root nor any path component may be a symlink;
- A derived path must lie inside the root, and a URI may contain only a single valid SHA-256 digest;
- The default directory permission is
0700and the object permission is0600; - With
verify_on_read=true, every read recomputes the digest and checks the expected size; - An object that is not a regular file, is missing, or has a mismatched digest raises
CasIntegrityError; incomplete bytes are never returned.
These checks do not turn the CAS into a secret manager. Enterprise source content still needs disk encryption, host access control, backups, and a retention policy; what the CAS solves is content identity, integrity, and replayability.
Why the Full CPG Stays in the CAS and Neo4j Holds Only a Semantic Projection
The full Code Property Graph contains a very large number of nodes: AST, CFG, DFG, call edges, and more. Stuffing every syntax node into Neo4j would significantly increase storage and traversal cost, and would drown development queries in low-level syntactic noise.
The current store_full_cpg writes the complete graph JSON to the CAS; semantic_cpg_projection selects only retrieval-relevant nodes such as Repository, File, Class, Method, Function, and API, along with relationships such as CALLS, IMPORTS, READS, WRITES, and ROUTES_TO, to enter the query graph. That way an audit can return to the full CPG, while day-to-day queries do not have to bear the cost of the complete syntax graph.
Failure Modes and Trade-offs
Each row below is one failure mode: the middle column is the current protection, and the right column is what that protection costs.
| Failure mode | Current protection | Trade-off |
|---|---|---|
| The process exits mid-write | Unique temp file + atomic replace; the target object is never half-written | A temp file may be left behind, to be cleaned up by the finally block or a maintenance task |
| An existing object is corrupted | Re-hash on put/read and raise an integrity error immediately | Verify-on-read adds disk read cost |
| A symlink points outside the root | lstat at every level and reject symlinks |
The implementation is more complex than an ordinary path write |
| A parser is upgraded | The raw object is unchanged; the new IR/projection gets new hashes | Disk usage grows, in exchange for comparability and rollback |
| CAS objects grow without bound | Currently append-only; automatic GC is not a default action | Deletion requires analyzing manifest reachability and the retention policy first |
| Only the query stores are backed up | The original evidence cannot be restored | Backups should focus on the CAS, manifests, and configuration, not only on vector points |
Verifiable Project Evidence
src/mt_rag/core/cas.py: URI parsing, sharded paths, permissions, symlink protection, atomic write, and verify-on-read;src/mt_rag/core/content_identity.py: canonicalization and hashing for bytes/text/JSON;src/mt_rag/core/system_contracts.py:CasObjectRefV1,HashRefV1, and the individual hash domains;src/mt_rag/code/cpg.py: the boundary between the full CPG going into the CAS and the semantic CPG projection;- The
cassection ofconfig/rag-runtime.json: root, algorithm, permissions, and verify configuration; docs/02-runbooks/local-filesystem-cas.md: local operations and integrity verification instructions.
References
- Git Internals: Git Objects;
- Git Hash Function Transition documentation;
- NIST FIPS 180-4 Secure Hash Standard;
- Python
os.replacedocumentation; - W3C PROV-O.
The value of the CAS is not "keeping one more copy of a file." It is separating rebuildable query projections from irreplaceable original evidence: an index can be redone, but evidence identity cannot be made up after the fact.
Related notes
How Incremental Projection Lets Vector, Keyword, and Graph Evolve Independently
Layered hashes, a delta planner, consumer cursors, and shadow projections together handle updates, failure recovery, and rollback.
LLM Runtime: Which Layer Should Own Rate Limits, Retries, and Routing
Once the second business, the second provider, and the second tenant show up, calling the SDK directly is no longer enough. This piece covers the responsibility boundary of an LLM Runtime, multi-model routing, reliability under high concurrency, and how to choose an observability approach.
Working on something similar?
I'm always happy to compare notes on distributed systems, delivery, and applied AI.
[email protected]