Skip to content

Latest commit

 

History

History
270 lines (185 loc) · 15.6 KB

architecture.md

File metadata and controls

270 lines (185 loc) · 15.6 KB

Architecture

A technical deep dive into how TotalReclaw provides semantic search + knowledge-graph memory over fully encrypted storage — without ever seeing the data.

This document covers the full system design. For a high-level overview, see the README.

v1 additions (April 2026): the encrypted blob now contains a v1 taxonomy claim (6 speech-act types + source / scope / volatility axes). The reranker applies Tier 1 source-weighted scoring. Outer protobuf bumped to v4 — subgraph schema unchanged.

TotalReclaw Architecture


System Overview

TotalReclaw splits trust across three layers. Encryption and search intelligence run entirely on the client. The relay is a blind intermediary that stores and retrieves opaque blobs. The open network provides permanent, verifiable storage and serverless recovery.

  Client (device)               Relay Service                 Open Network
 ┌──────────────────┐        ┌──────────────────┐        ┌──────────────────┐
 │ Key derivation    │        │ Stores encrypted  │        │ Gnosis Chain     │
 │ XChaCha20-Poly1305│──────▸ │ blobs + blind     │──────▸ │ anchors data     │
 │ Embeddings + LSH  │ cipher │ indices           │ anchor │ permanently      │
 │ Re-ranking        │ text   │ GIN index search  │        │                  │
 │ Fact extraction   │◂────── │ Gas sponsorship   │◂────── │ The Graph        │
 └──────────────────┘ results └──────────────────┘ index  │ indexes data     │
                                                          └──────────────────┘

Every operation that touches plaintext — encryption, embedding generation, search re-ranking — happens on the device. The relay only sees SHA-256 hashed tokens and XChaCha20-Poly1305 ciphertext. Even if the relay is fully compromised, memories remain unreadable.


End-to-End Encryption

Key Derivation

A 12-word BIP-39 recovery phrase is the single root of trust. Two independent branches are derived from it: the vault keys (HKDF), which protect and index memory content, and the owner key (BIP-32), which controls the user's on-chain Smart Account.

                 recovery phrase (12 or 24 words)
                               │  PBKDF2-HMAC-SHA512, 2048 rounds (BIP-39)
                               ▼
                         512-bit seed
              ┌────────────────┴────────────────┐
   HKDF-SHA256, salt = seed[0..32]     BIP-32, path m/44'/60'/0'/0/0
              │                                 │
   ├─ authKey                              owner key (EOA)
   ├─ encryptionKey                             │  SimpleAccountFactory (CREATE2)
   ├─ dedupKey                                  ▼
   └─ lshSeed                             Smart Account address

Step 1 — Seed Generation

The 12 or 24 words pass through PBKDF2-HMAC-SHA512 (2048 rounds, standard BIP-39) to produce a 512-bit seed.

Step 2 — Vault Keys

The seed is expanded into four 256-bit keys using HKDF-SHA256, with the first 32 bytes of the seed as the salt and a distinct info string per key:

salt = seed[0..32]
HKDF(seed, salt, "totalreclaw-auth-key-v1")        → authKey
HKDF(seed, salt, "totalreclaw-encryption-key-v1")  → encryptionKey
HKDF(seed, salt, "openmemory-dedup-v1")            → dedupKey
HKDF(seed, salt, "openmemory-lsh-seed-v1")         → lshSeed

The openmemory- info strings are a holdover from the project's earlier name. They can't be renamed: a different info string derives a different key, which would break every existing vault.

Step 3 — Owner Key and Smart Account

The same seed also derives an Ethereum key at the standard path m/44'/60'/0'/0/0, the same key viem's mnemonicToAccount() or MetaMask would produce from these words. This owner key owns the user's ERC-4337 Smart Account (eth-infinitism SimpleAccount, created by the canonical factory). The account's address is a CREATE2 function of the owner key, so every client computes the same address before the account exists; it is deployed by the user's first write. The owner key signs every UserOp, and on-chain facts are indexed under the Smart Account address.

HKDF and BIP-32 are different functions over the seed, so neither branch can be derived from the other: a leaked vault key does not reveal the owner key, or vice versa. Only a leak of the phrase or seed compromises both.

Step 4 — Key Usage

Key Purpose What the relay sees
authKey Authenticates with the relay. Sent as a bearer token (Authorization: Bearer <authKey>) on every request The key itself, on each request (over TLS). Stores only SHA-256(authKey). Grants relay access only (reads, quota, billing) — it cannot decrypt memories or sign on-chain writes
encryptionKey Encrypts all memory content via XChaCha20-Poly1305 Nothing — never transmitted
dedupKey Keys the HMAC-SHA256 content fingerprints used for exact-duplicate detection Fingerprints only
lshSeed Seeds the per-user random hyperplanes for LSH bucket hashes Bucket hashes only
Owner key Signs UserOps for the user's Smart Account Signatures and the public address only

Where the keys live. A client configured with a recovery phrase derives every key on the device. It sends authKey with each request, and at registration also sends the HKDF salt (seed[0..32]) and SHA-256(authKey). No other key or seed material leaves the device. An agent host configured with a derived-bundle-v1 bundle holds the four vault keys and the owner key directly, but never the phrase or seed (see client consistency).

Legacy password path (self-hosted only). The managed service always requires a BIP-39 phrase. Self-hosted deployments can still be configured with an arbitrary password instead: it is stretched with Argon2id (t=3, m=64 MB, p=4) over a random 32-byte salt into a master key. Step 2 then runs with the master key as the HKDF input and that same random salt as the HKDF salt. This path has no owner key, since self-hosted mode doesn't use the chain.

XChaCha20-Poly1305 Wire Format

Every memory is encrypted as a single blob:

[Nonce: 24 bytes] [Auth Tag: 16 bytes] [Ciphertext: variable]

The 128-bit authentication tag ensures both confidentiality and integrity — any tampering is detected on decryption.

What the relay never sees

  • Recovery phrase
  • Encryption key or dedup key
  • Plaintext memories
  • Raw embeddings
  • Search query content

Fact Extraction

Memories aren't stored as raw conversations. The agent's LLM extracts atomic facts — concise, self-contained statements scored by importance and categorized by type.

// Example: "I've been vegan for 3 years and I work at Anthropic."
[
  { "text": "User is vegan", "importance": 8, "type": "preference" },
  { "text": "User works at Anthropic", "importance": 7, "type": "fact" }
]

Each fact is tagged with an importance score (1–10) that feeds into decay scoring, and a type (fact, preference, decision, goal, episodic) that aids retrieval context. The extraction prompt filters out credentials, ephemeral context, and third-party data.

Before storage, the client computes a content fingerprint using HMAC-SHA256(dedupKey, normalized_text). The relay checks this fingerprint against existing records — duplicates are rejected without ever comparing plaintext.


Deduplication

Agents tend to store the same information repeatedly. Over weeks, a vault accumulates multiple copies of facts like "User prefers dark mode" — diluting search results and creating contradictions. TotalReclaw uses a 5-layer dedup pipeline that catches duplicates at every stage while preserving the zero-knowledge guarantee.

Layer Method Threshold Where Description
1 HMAC-SHA256 fingerprint Exact match Server Catches verbatim duplicates via hash comparison. Cheapest gate — no decryption needed.
2 Within-batch cosine >= 0.9 Client Removes paraphrases from the same LLM extraction call. Strict threshold avoids false positives.
3 Store-time cosine >= 0.85 Client Queries 200 nearest candidates via blind indices, decrypts client-side, compares. Higher importance supersedes; lower is skipped.
4 Bulk consolidation >= 0.88 Client On-demand greedy clustering across the entire vault. Best representative per cluster survives; rest are tombstoned.
5 LLM judge Semantic Client Evaluates new facts against existing memories. Returns ADD, UPDATE, DELETE, or NOOP. Catches contradictions cosine alone misses.

Design decisions:

  • Fail-open: if any dedup layer fails, the memory is stored anyway. A duplicate is recoverable; lost data is not.
  • Multi-threshold: stricter within a single extraction batch (0.9), balanced across sessions (0.85), with slight tolerance for bulk cleanup (0.88).
  • All client-side: every dedup decision involving plaintext, embeddings, or similarity scores happens on the client.

Semantic Search over Encrypted Data

The core challenge: how do you search data the server can't read?

TotalReclaw splits search into a server-side candidate retrieval step (using blind indices) and a client-side re-ranking step (using decrypted plaintext).

Step 1 — Embedding and LSH (client-side)

Each fact is embedded using Harrier-OSS-v1-270M — a 640-dimensional embedding model. The model runs locally via ONNX Runtime (quantized, ~164MB).

For search queries, a task instruction is prepended to improve retrieval accuracy:

Instruct: Given a user query about personal preferences, facts, or history,
retrieve the most relevant stored memories
Query: <user's search query>

Facts are embedded without instructions. This asymmetry follows the model's training design and yields a 1–5% accuracy improvement.

The embedding is then projected through 20 independent LSH hash tables, each with 32-bit signatures. Similar embeddings land in the same LSH buckets with high probability. The bucket IDs are SHA-256 hashed before being sent to the relay as blind indices.

Parameter Value
Embedding model Harrier-OSS-v1-270M
Dimensions 640
Size ~164MB
Hash tables 20
Bits per table 32
Validated recall 98.1% Recall@8 on real data
Query latency <10ms (trapdoor generation + GIN lookup)

Step 2 — Candidate retrieval (server-side)

The relay's PostgreSQL database uses a GIN (Generalized Inverted Index) over the blind indices array column. The search query becomes an array overlap:

SELECT * FROM memories
WHERE blind_indices && $trapdoors
  AND decay_score > $threshold
ORDER BY decay_score DESC
LIMIT $candidate_pool;

This returns 400–3,000 encrypted candidates in under 15ms. The relay has no idea what concepts it just matched — it only compared opaque SHA-256 hashes.

Step 3 — Decrypt and re-rank (client-side)

Candidates are decrypted with XChaCha20-Poly1305, then scored across four signals:

Signal What it measures Method
BM25 Text keyword relevance Okapi BM25 (k1=1.2, b=0.75), Porter stemming
Cosine Semantic similarity Dot product of L2-normalized 1024-dim embeddings
Decay Recency Exponential decay with 7-day half-life: importance x e^(-lambda*t)
Importance User relevance LLM-assigned score (1–10) from extraction

Scores are fused via Reciprocal Rank Fusion (RRF) with k=60:

RRF_score = sum of 1/(k + rank_i) across all signals

RRF is chosen over learned-weight fusion because it's robust without training data and stable across different corpus sizes. The top 8 results are returned to the agent. Optional MMR (Maximal Marginal Relevance) diversification ensures results don't cluster around a single topic.

End-to-end search latency: <150ms.


Locality-Sensitive Hashing — How It Works

LSH is the bridge that makes blind search possible. Rather than storing raw vectors on the server (which would leak semantic information), the client projects each embedding into hashed buckets.

Each of the 20 hash tables uses random hyperplanes generated deterministically from the user's HKDF seed. For a given embedding vector, each hyperplane produces a single bit: which side of the plane does the vector fall on? 32 hyperplanes produce a 32-bit signature — a bucket ID. Two similar embeddings will share bucket IDs across multiple tables with high probability.

The bucket IDs are then SHA-256 hashed. The server stores these hashed IDs and can match them (via GIN array overlap) without knowing what semantic concept they represent.

Why not just download everything? At 50,000 memories, downloading and decrypting the entire vault for every search is impractical. LSH lets the server do the cheap, fast filtering (narrowing to a few hundred candidates) while the client does the expensive, accurate re-ranking — and neither side has the complete picture.


Open Network Layer

For users who want persistence beyond any single server, TotalReclaw anchors encrypted memories to a decentralized network.

Gnosis Chain

A minimal smart contract (EventfulDataEdge) with a single fallback() function that emits Log(msg.data). No storage, no access control, no state — just a permissionless event emitter. Cost: approximately 5,300 gas per 256-byte fact.

ERC-4337 Smart Accounts

Each user gets a counterfactual smart account derived from their BIP-39 mnemonic (m/44'/60'/0'/0/0). Writes are sponsored by a Paymaster (gasless for users). The same mnemonic recovers the same account and decryption keys on any device.

The Graph Subgraph

An AssemblyScript subgraph indexes all Log events by smart account address. Recovery is straightforward: query the subgraph for the account address, download all events, decrypt with the mnemonic-derived key. No server required.

Why this matters

The relay handles fast day-to-day reads and writes. The network layer is what turns portability and data ownership from promises into structural guarantees. If TotalReclaw shuts down, if the relay goes offline, if the company gets acquired — the data still exists on a global, open network that no single entity controls. A recovery phrase and the subgraph are all that's needed.


Key Parameters Reference

Component Detail
Encryption XChaCha20-Poly1305 — 256-bit key, 192-bit nonce, 128-bit auth tag
KDF (password) Argon2id — t=3, m=64 MB, p=4
KDF (mnemonic) BIP-39 PBKDF2 — 2048 rounds → HKDF-SHA256 expansion
Embeddings Harrier-OSS-v1-270M — 640 dimensions, ONNX quantized (~164MB)
LSH 20 hash tables, 32 bits per table
Blind indices SHA-256 of word tokens + LSH bucket IDs
Content dedup HMAC-SHA256 fingerprint
Search latency <150ms end-to-end
BM25 k1=1.2, b=0.75, Porter stemming, English stop words
RRF constant k=60
Decay half-life 7 days (configurable)
On-chain contract EventfulDataEdge on Gnosis Chain
Account abstraction ERC-4337 with Paymaster gas sponsorship
Subgraph The Graph (AssemblyScript indexer)