Architecture Overview
The Encrypted Memory Vault is a local-first, vectorized storage engine built on top of Turso (libSQL) and the macOS Keychain. It provides standard CRUD operations for theAgentRuntime while guaranteeing that sensitive developer context is never written to disk in plaintext.
The memory vault lives in src-tauri/src/services/memory_vault/ and interfaces primarily with the EmbedderService and AgentMemory wrapper.
Core Components and Functions
crypto.rs
This module handles the low-level AES-256-GCM encryption mechanics.
encrypt(data: &[u8], key: &[u8]) -> Result<Vec<u8>, String>Generates a random 12-byte nonce, encrypts the plaintextdatausing the provided 32-bytekey, and returns the concatenatednonce + ciphertext.decrypt(encrypted_data: &[u8], key: &[u8]) -> Result<Vec<u8>, String>Extracts the 12-byte nonce from the beginning of theencrypted_data, and decrypts the remaining ciphertext using the AES-256-GCM algorithm and the symmetrickey.
key_provider.rs
This module defines the VaultKeyProvider trait, which abstracts the retrieval of the master encryption key.
MacOSKeychainVaultKeyProvider::get_or_create_master_key()Interfaces with thesecurity-frameworkcrate to fetch the 32-byte master key from the macOS Keychain. If the key does not exist or raises anItemNotFounderror, it securely generates a new 32-byte key usingring::rand, encodes it in Base64, and stores it in the Keychain.
service.rs (MemoryVaultService)
This is the primary repository layer for memory management. It initializes the libSQL connection pool and applies the AES-256-GCM encryption before any data touches the database.
new(app_data_dir: PathBuf) -> Result<Self, String>Initializes the native macOS Keychain provider and establishes the database connection torainy_cowork_v2.db. It runs schema migrations to ensure thememoriestable exists withFLOAT32vector columns.store(&self, input: StoreMemoryInput) -> Result<String, String>Takes aStoreMemoryInputstruct containing the plaintext content, source, vector (generated byEmbedderService), and sensitivity. It encrypts the plaintext content using the provider’s master key and inserts the Base64-encoded ciphertext and vector into the database.retrieve(&self, query_vector: &[f32], limit: usize) -> Result<Vec<MemoryEntry>, String>Performs a nativevector_distance_coscosine similarity search against the stored libSQL vectors. For the closest matches, it fetches the Base64-encoded ciphertext, decodes it, and decrypts it back into plaintext, returning fully hydratedMemoryEntryobjects.delete(&self, id: &str) -> Result<(), String>Standard deletion of a memory by its UUID.clear(&self) -> Result<(), String>Truncates thememoriestable entirely.
Extensibility
The vault is currently hardcoded to useMacOSKeychainVaultKeyProvider. For cross-platform support (Windows/Linux), new implementations of the VaultKeyProvider trait must be created utilizing keyring-rs or equivalent secure enclaves.