This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Fractal-Mind is an evolutionary memory AI system that mimics human cognition through a dual-phase architecture:
- Vigilia (Wakefulness): Fast, real-time query responses using fractal graph navigation
- REM Phase (Sleep): Asynchronous learning, memory consolidation, and knowledge integration from external sources
The system combines graph precision (SurrealDB) with vector flexibility (embeddings) to create a self-learning knowledge engine that operates on disk-based storage for scalability.
Backend (Rust):
- Axum: HTTP API framework
- SurrealDB: Multi-model database (graph + vector storage with HNSW indexing)
- Tokio: Async runtime for non-blocking I/O
- LRU Cache: In-memory caching for frequently accessed fractal nodes
Core Algorithms:
- RAPTOR: Recursive clustering for fractal graph construction
- SSSP (Single-Source Shortest Path): Graph navigation with O(m log^(2/3) n) optimization using hopsets
- HNSW: Hierarchical Navigable Small World for vector similarity search
External Integrations:
- Web search engines (Tavily/Serper) for REM phase knowledge acquisition
- OCR for image/PDF ingestion
- Embedding models (text/vision) for semantic encoding
The system organizes knowledge as a fractal tree:
- Documents are split into leaf nodes
- REM phase clusters semantically similar leaves → generates parent summaries
- Process repeats recursively → self-similar hierarchical structure
- Navigation uses shortest-path algorithms with relevance as edge weight (1/similarity)
NAMESPACE global_knowledge # Shared public memory
NAMESPACE user_alice # Private user scope
NAMESPACE user_bob # Private user scope
Each user has a SCOPE with exclusive access to their personal graph. The system creates links between personal and global memory during REM consolidation.
Nodes are tagged with status:
complete: High-confidence knowledgeincomplete: Identified gaps requiring REM phase research
POST /v1/ingest # Ingest files (PDF, images with OCR, text)
POST /v1/remember # Store episodic/conversational memory
POST /v1/ask # Query fractal graph for LLM context retrieval
POST /v1/sync_rem # Trigger REM phase: consolidation + web learning
PATCH /v1/memory # Manual knowledge updates/corrections
SurrealDB must use persistent storage (not in-memory):
surreal start --log trace --user root --pass root file://fractalmind.dbEnable HNSW indexing for vector search directly on disk to avoid RAM overflow.
cargo build --release # Production build
cargo build # Development build (faster linking)
cargo test # Run test suite
cargo clippy # Lint checks
cargo fmt # Format code- Use
tokio::spawnfor async tasks, never block the runtime - Implement
LruCachefor top-level fractal nodes (most frequently queried) - HNSW index parameters should balance recall vs. disk I/O
async fn consolidate_memory() {
// 1. Detect incomplete nodes from Vigilia phase
let incomplete = query_nodes_by_status("incomplete").await;
// 2. Web search for external knowledge
let external_data = search_web(incomplete).await;
// 3. Synthesize into global namespace
let new_nodes = create_nodes(external_data, "global_knowledge").await;
// 4. Cluster leaves by semantic similarity
let clusters = cluster_by_embedding_similarity(new_nodes).await;
// 5. Generate parent summaries recursively
let parent_nodes = generate_parent_summaries(clusters).await;
// 6. Link personal ↔ global memory
create_cross_namespace_edges(user_scope, global_scope).await;
}async fn answer_query(user_query: &str, user_scope: &str) {
// 1. Embed query as vector
let query_vector = embed_text(user_query).await;
// 2. HNSW search in user scope
let personal_results = hnsw_search(query_vector, user_scope).await;
// 3. Shortest path to global knowledge if needed
let global_results = sssp_search(query_vector, "global_knowledge", personal_results).await;
// 4. Return context to LLM
let context = merge_and_rank(personal_results, global_results);
return context;
}The system supports pluggable embedding models without database rebuilds:
- Store model identifier with each vector:
{"vector": [...], "model": "nomic-embed-text-v1.5"} - Query filters vectors by model version before similarity search
- Allows gradual migration to newer/multimodal embeddings
- Disk-based operation: All vector indices must use HNSW on disk (not in-memory)
- Non-blocking I/O: All database/network calls use Tokio async
- Namespace isolation: User data must never leak across scopes
- Fractal depth limit: Implement max recursion depth to prevent infinite clustering
- REM phase scheduling: Run asynchronously (background task), not blocking API responses