This document provides a deep dive into the core functions of the Valori system across its three layers: Kernel (Rust), Node (Service), and Client (Python).
Location: valori-kernel/src/state/kernel.rs
The Kernel is the deterministic heart of the system. It is single-threaded, no_std compatible, and operates on fixed-point arithmetic.
- Purpose: The only way to mutate state. Transitions the system from
State_NtoState_{N+1}deterministically. - Complexity:
InsertRecord: O(1) for storage + O(log N) or O(1) for Index update (depends on Index implementation).DeleteRecord: O(1).CreateNode/CreateEdge: O(1) (amortized).
- Determinism: Guaranteed. Bit-identical results for the same sequence of commands.
- Side Effects: Updates
version,records,graph, and theindex.
- Purpose: Performs a k-nearest neighbor search using L2 squared distance.
- Behavior: Delegates to the configured
VectorIndex. - Default Implementation (
BruteForceIndex):- Complexity: O(N * D), where N is active records, D is dimensions.
- Determinism: Uses stable sorting by Score (primary) and RecordID (secondary) to break ties deterministically.
- Purpose: Serializes the entire state (Records + Graph + Index + Version) into a binary buffer.
- Format: Custom compact binary format (not standard serialization like JSON/Protobuf) for maximum density and speed.
- Complexity: O(State Size).
Location: valori-kernel/src/index/mod.rs & src/quant/mod.rs
Defines how vectors are indexed for search.
- Functions:
on_insert(id, vec): Hook called after storage insert.on_delete(id): Hook called after storage delete.search(pool, query, results): Execute retrieval.
Defines how vectors are compressed (lossy compression) to save memory/bandwidth.
- Functions:
encode(vec) -> Code: Compress a high-precision FXP vector.decode(code) -> FxpVector: Reconstruct approximation.
Location: valori-node/src/engine.rs & server.rs
Wraps the Kernel in a tokio async runtime. Use Arc<Mutex<Engine>> for sharing.
- Purpose: Initializes a new Kernel with specified
IndexKind(e.g.,Hnsw) andQuantizationKind. - Analysis: This is where dependency injection happens. The specific
VectorIndeximplementation (e.g., BruteForce, HNSW) is selected here at compile time or runtime startup.
- Purpose: Manages the lifecycle of the entire system state.
- Behavior (Checkpointing):
- Format: Multipart binary (
[Header][Meta][Kernel][Metadata][Index][CRC]). - HNSW: Uses deterministic serialization (sorting internal HashMaps) to ensure bit-identical snapshots.
- Safety: Validates bounds and checksums before loading.
- Fallback: If the Index blob is missing or incompatible, the Engine rebuilds the index from the Kernel records.
- Format: Multipart binary (
POST /v1/memory/upsert_vector:- Logic:
- Insert Vector -> Get
RecordId. - Create
DocumentNode (if not reusing). - Create
ChunkNode linked to Record. - Create Edge
ParentOf(Doc -> Chunk).
- Insert Vector -> Get
- Atomicity: Not fully atomic over HTTP (multiple kernel commands). Future work: Batched Commands.
- Logic:
POST /v1/memory/search_vector:- Logic: Calls
search_l2and formats results withmemory_id(rec:{id}).
- Logic: Calls
POST /v1/memory/meta/set: Key-Value metadata storage separate from the graph.GET /v1/memory/meta/get: Retrieve metadata by ID.
POST /v1/snapshot/save: Trigger a manual snapshot to the configured path. Supports rotation (keeps.prev).POST /v1/snapshot/restore: Load state from a specified file path. (Warning: Overwrites current state).
Location: python/valori/protocol.py & memory.py
- Purpose: The main entry point for developers. Handles the choice between Local and Remote execution.
- Initialization:
ProtocolClient(remote="...")- If
remoteis None: InstantiatesMemoryClient(FFI). - If
remoteis URL: InstantiatesProtocolRemoteClient(HTTP).
- If
- Logic:
- Chunking: Splits text into chunks locally (client-side).
- Embedding: Runs
EmbedFnlocally. - Transport:
- Local: Direct memory write.
- Remote: Sends
POST /v1/memory/upsert_vector.
- Metadata: Calls
set_metadataautomatically if metadata is provided.
- Local: Calls Rust
KernelState::snapshotdirectly. - Remote:
snapshot(): Downloads the full binary DB fromPOST /snapshot.restore(): Uploads a binary blob toPOST /restore.
- Use Case: Migrating state from a local development environment to a production server, or for backups.
Note on Performance vs. Correctness: Valori prioritizes Correctness (Determinism) > Performance.
- Fixed-Point: All floating-point inputs are converted to Fixed-Point (Q16.16).
- Deterministic Indexing: Even complex structures like HNSW are implemented to be bit-exact reproducible, sacrificing some parallelism for consistency if necessary (though current implementation is single-threaded).