ruvnet/ruflo/tree/main/plugins/ruflo-rag-memory
ruflo-rag-memory
結合 HNSW 搜尋、AgentDB 與語意檢索的 RuVector 記憶。作為外掛(ADR-445),包含防止機密進入記憶的寫入守衛、/rag-mod 命令,以及主控台顯示的狀態檔。
關於這個 mod
ruflo-rag-memory
結合 HNSW 向量搜尋、AgentDB 持久化與 Claude Code 記憶橋接的檢索增強生成記憶。
概覽
在 AgentDB 上提供語意儲存、搜尋與回憶,並使用 HNSW 索引的向量搜尋(實測在 N=20k 時約為暴力搜尋的 1.9x,在 N=5k 時約為 3.2x–4.7x,recall@10 約為 0.99;超過索引大小交叉點後,ANN 更有優勢)。它會把 Claude Code 的原生自動記憶橋接到 AgentDB,並使用 384 維 ONNX 嵌入,提供跨工作階段的統一語意檢索。
快速開始
在不同工作階段之間儲存與擷取知識:
# Store a pattern you want to remember
npx ruflo memory store --key "oauth-flow" --value "OAuth2 with pkce for SPAs, use refresh tokens" --namespace patterns
# Search for it later (even across projects!)
npx ruflo recall "oauth single page app"
# Retrieve exact entry
npx ruflo memory retrieve --key "oauth-flow" --namespace patterns
搭配代理程式使用:
# In your Claude Code agent prompt:
const context = await memory_search({ query: "authentication patterns", limit: 3 });
// Returns top 3 semantic matches from all sessions
安裝
claude --plugin-dir plugins/ruflo-rag-memory
需求
ruflo-core外掛(提供 MCP 伺服器)
代理程式
| 代理程式 | 模型 | 角色 |
|-------|-------|------|
| memory-specialist | sonnet | AgentDB 管理、HNSW 最佳化、記憶橋接、整合 |
技能
| 技能 | 用法 | 說明 |
|-------|-------|------|
| memory-search | /memory-search <query> | 跨所有命名空間的語意向量搜尋 |
| memory-bridge | /memory-bridge [--all-projects] | 將 Claude Code 自動記憶匯入 AgentDB |
命令
# Store a memory entry
memory store --key "pattern-auth" --value "JWT with refresh tokens" --namespace patterns
# Semantic search (HNSW-indexed)
memory search --query "authentication patterns" --namespace patterns --limit 5
# Retrieve by key
memory retrieve --key "pattern-auth" --namespace patterns
# List entries
memory list --namespace patterns --limit 10
# Delete
memory delete --key "old-entry" --namespace patterns
# Quick semantic recall across all namespaces
recall "how did we handle rate limiting?"
架構
Claude Code Auto-Memory (~/.claude/projects/*/memory/*.md)
│
▼ (ONNX all-MiniLM-L6-v2, 384-dim)
Memory Bridge
│
▼
AgentDB (SQLite + vector_indexes)
│
├── patterns namespace
├── tasks namespace
├── solutions namespace
├── feedback namespace
├── security namespace
└── claude-memories namespace
│
▼ (HNSW ANN index)
Semantic Search (HNSW ANN — measured ~1.9x at N=20k vs brute force; see docs/reviews/intelligence-system-audit-2026-05-29.md)
靜態加密(ruflo 3.6.25+)
此外掛寫入的 AgentDB SQLite 資料塊(.swarm/memory.db)支援依照 ADR-096 選用 AES-256-GCM 靜態加密。設定 CLAUDE_FLOW_ENCRYPT_AT_REST=1 與 CLAUDE_FLOW_ENCRYPTION_KEY 後:
- 每次寫入
.swarm/memory.db都會使用新的 12 位元組 IV 加密(writeFileRestricted({encrypt:true}))。 - 讀取使用
readFileMaybeEncrypted(path, null)——透過魔數偵測(RFE1),因此遷移期間舊的明文 memory.db 檔案仍能照常運作。 - 嵌入會與 SQLite 資料塊的其餘部分一起加密,第一階段不需要個別的欄位級加密。
- 翻轉一個位元組會使 GCM 驗證失敗並產生解密錯誤,而不是靜默損壞。
使用 ruflo doctor -c encryption 驗證閘門狀態。預設關閉;開啟它不需要遷移步驟(讀取時會偵測舊的明文字節,啟用後第一次寫入會將資料庫改寫為加密形式)。
記憶命名空間
| 命名空間 | 用途 | 範例金鑰 |
|-----------|------|--------|
| patterns | 成功的程式碼/設計模式 | pattern-auth-jwt |
| tasks | 工作階段內容與結果 | task-refactor-api |
| solutions | 錯誤修正與解決方案 | fix-race-condition |
| feedback | 使用者回饋與更正 | feedback-style-test |
| security | 漏洞模式 | vuln-sql-injection |
| claude-memories | 橋接的 Claude Code 記憶 | auto-imported |
Claude 記憶橋接
在 AgentDB 工作階段開始時,會自動從 Claude Code 原生的 ~/.claude/projects/*/memory/*.md 檔案匯入記憶,並使用 ONNX 向量嵌入。
# Manual import (current project)
/memory-bridge
# Import all projects
/memory-bridge --all-projects
# Check bridge health
# Via MCP: memory_bridge_status({})
結果會包含來源歸屬:claude-code、auto-memory 或 agentdb。
SmartRetrieval(ADR-090)
用於跨工作階段提升檢索品質的 5 個階段:
- 查詢擴充 ——以範本產生變體(無 LLM)
- 多查詢扇出 + RRF ——跨變體進行倒數排名融合
- 新近度加權 ——根據中繼資料時間戳指數衰減
- MMR 多樣性 ——使用 token-Jaccard 最大邊際相關性重新排序
- 工作階段輪詢 ——交錯回傳來自不同工作階段的結果
# CLI
npx @claude-flow/cli@latest memory search --query "auth patterns" --smart --limit 10
# MCP
mcp__plugin_ruflo-core_ruflo__memory_search({ query: "auth patterns", smart: true, limit: 10 })
適合多工作階段搜尋、時間查詢(「上週我們如何處理速率限制?」)和多樣化結果。
統一搜尋
同時查詢所有命名空間,並使用 MMR 多樣性重新排序:
# Via MCP: memory_search_unified({ query: "auth security", limit: 5 })
# Via CLI:
npx @claude-flow/cli@latest memory search --query "auth security" --limit 5
HNSW 效能
實測資料來自 docs/reviews/intelligence-system-audit-2026-05-29.md 與 scripts/benchmark-intelligence.mjs:
| 操作 | 相對暴力搜尋 | 備註 | |-----------|----------------|-------| | N=5k 的向量搜尋 | 約快 3.2x–4.7x | ruvector NAPI,recall@10 約 0.99 | | N=20k 的向量搜尋 | 約快 1.9x | ANN 在交叉點以上更有優勢 | | 低於交叉點的向量搜尋 | 持平/更慢 | 小規模資料優先使用暴力搜尋 |
先前公布的「150x–12,500x」資料是暴力搜尋回退產生的結果,在稽核測試框架下無法重現。
與 ruvector 整合
同時載入 ruflo-ruvector 時,rag-memory 會將後端交給 ruvector,以取得進階功能:
- FlashAttention-3,用於 O(N) 記憶體注意力
- Graph RAG,用於多跳檢索
- 使用 RRF 融合的混合搜尋(稀疏 + 稠密)
- DiskANN,用於大型持久化索引
相容性
- CLI: 固定使用
@claude-flow/cli的 v3.6 大版本與小版本。 - 驗證:
bash plugins/ruflo-rag-memory/scripts/smoke.sh是契約。
命名空間協調——claude-memories 使用者
此外掛是 [ruflo-agentdb ADR-0001 「Namespace convention」](../ruflo-agentdb/docs/adrs/0001-agentdb-optimization.md) 定義之 claude-memories` 保留命名空間的規範使用者端消費者。自動匯入流程:
Claude Code SessionStart hook
→ memory_import_claude (MCP)
→ claude-memories namespace (reserved, ruflo-agentdb owned)
→ exposed by this plugin's memory-bridge skill + memory_search_unified
此外掛不擁有 claude-memories,而是使用它。保留命名空間(pattern、claude-memories、default)不得被遮蔽。
其他命名空間(patterns、tasks、solutions、feedback、security)透過依命名空間路由的 memory_* 存取。外掛全程使用正確路由,不會使用帶有命名空間引數的 agentdb_hierarchical-* 或 agentdb_pattern-store。
驗證
bash plugins/ruflo-rag-memory/scripts/smoke.sh
# Expected: "10 passed, 0 failed"
架構決策
相關外掛
ruflo-agentdb——完整的 AgentDB 控制器橋接(15 個agentdb_*MCP 工具);命名空間慣例擁有者;擁有claude-memories保留命名空間ruflo-ruvector——進階向量操作(FlashAttention-3、Graph RAG、混合搜尋)ruflo-rvf——跨機器匯入/匯出的可攜式 RVF 記憶格式ruflo-knowledge-graph——對記憶執行實體擷取與圖形走訪
授權條款
MIT
作為外掛
函式勾點外掛(ADR-445,採用 ruflo-agentdb 的模式)。它從 hooks/hooks.json → hooks/register.ts 載入。
記憶寫入守衛:memory_store、agentdb_hierarchical-store 和 agentdb_pattern-store 會拒絕機密,因此憑證不會落入向量儲存。
- 命令:
/rag-mod在本機回答,不呼叫模型。動詞:status、scan <text>(守衛會拒絕這段文字嗎?)、tools(哪些記憶工具已連線)。 - 狀態檔:
.claude-flow/rag-mod/status.json({version, updatedMs, ...counters}),在工作階段開始和計數器變更時寫入。 - 安全性: 不連線網路,不建立程序;只使用已連線的工具。
- 選項
guard(預設on,設為off可停用):只會收緊的tool.call守衛。若上述其中一個工具的輸入包含金鑰、權杖、私密金鑰或密碼,就拒絕呼叫。拒絕原因不會再次顯示機密。 - 測試:
claude plugin validate plugins/ruflo-rag-memory、claude plugin test plugins/ruflo-rag-memory、bash plugins/ruflo-rag-memory/scripts/smoke.sh。
安裝
請先查看作者 README,確認 marketplace 與外掛名稱;指令可能隨儲存庫結構而變動。
claude plugin marketplace add ruvnet/ruflo claude plugin install ruflo-rag-memory
原文 / README
ruflo-rag-memory
Retrieval-Augmented Generation memory with HNSW vector search, AgentDB persistence, and Claude Code memory bridge.
Overview
Provides semantic store/search/recall over AgentDB with HNSW-indexed vector search (measured ~1.9x at N=20k, ~3.2x–4.7x at N=5k vs brute force, recall@10 ~0.99; ANN wins above the index-size crossover). Bridges Claude Code's native auto-memory into AgentDB with 384-dim ONNX embeddings for unified cross-session semantic retrieval.
Quick Start
Store and retrieve knowledge across sessions:
# Store a pattern you want to remember
npx ruflo memory store --key "oauth-flow" --value "OAuth2 with pkce for SPAs, use refresh tokens" --namespace patterns
# Search for it later (even across projects!)
npx ruflo recall "oauth single page app"
# Retrieve exact entry
npx ruflo memory retrieve --key "oauth-flow" --namespace patterns
Use with agents:
# In your Claude Code agent prompt:
const context = await memory_search({ query: "authentication patterns", limit: 3 });
// Returns top 3 semantic matches from all sessions
Installation
claude --plugin-dir plugins/ruflo-rag-memory
Requires
ruflo-coreplugin (provides MCP server)
Agents
| Agent | Model | Role |
|-------|-------|------|
| memory-specialist | sonnet | AgentDB management, HNSW optimization, memory bridge, consolidation |
Skills
| Skill | Usage | Description |
|-------|-------|-------------|
| memory-search | /memory-search <query> | Semantic vector search across all namespaces |
| memory-bridge | /memory-bridge [--all-projects] | Import Claude Code auto-memory into AgentDB |
Commands
# Store a memory entry
memory store --key "pattern-auth" --value "JWT with refresh tokens" --namespace patterns
# Semantic search (HNSW-indexed)
memory search --query "authentication patterns" --namespace patterns --limit 5
# Retrieve by key
memory retrieve --key "pattern-auth" --namespace patterns
# List entries
memory list --namespace patterns --limit 10
# Delete
memory delete --key "old-entry" --namespace patterns
# Quick semantic recall across all namespaces
recall "how did we handle rate limiting?"
Architecture
Claude Code Auto-Memory (~/.claude/projects/*/memory/*.md)
│
▼ (ONNX all-MiniLM-L6-v2, 384-dim)
Memory Bridge
│
▼
AgentDB (SQLite + vector_indexes)
│
├── patterns namespace
├── tasks namespace
├── solutions namespace
├── feedback namespace
├── security namespace
└── claude-memories namespace
│
▼ (HNSW ANN index)
Semantic Search (HNSW ANN — measured ~1.9x at N=20k vs brute force; see docs/reviews/intelligence-system-audit-2026-05-29.md)
Encryption at rest (ruflo 3.6.25+)
The AgentDB SQLite blob written by this plugin (.swarm/memory.db) supports opt-in AES-256-GCM encryption at rest per ADR-096. When CLAUDE_FLOW_ENCRYPT_AT_REST=1 and CLAUDE_FLOW_ENCRYPTION_KEY is set:
- Each write of
.swarm/memory.dbis encrypted with a fresh 12-byte IV (writeFileRestricted({encrypt:true})). - Reads use
readFileMaybeEncrypted(path, null)— magic-byte sniff (RFE1) so legacy plaintext memory.db files keep working unchanged during the migration window. - Embeddings are encrypted along with the rest of the SQLite blob — no separate column-level encryption needed for Phase 1.
- A flipped byte fails GCM auth and produces a decrypt error rather than silent corruption.
Verify gate state with ruflo doctor -c encryption. Off by default; flipping it on doesn't require a migration step (legacy plaintext bytes are sniffed on read; first write after enable rewrites the DB encrypted).
Memory Namespaces
| Namespace | Purpose | Example Key |
|-----------|---------|-------------|
| patterns | Successful code/design patterns | pattern-auth-jwt |
| tasks | Task context and outcomes | task-refactor-api |
| solutions | Bug fixes and solutions | fix-race-condition |
| feedback | User feedback and corrections | feedback-test-style |
| security | Vulnerability patterns | vuln-sql-injection |
| claude-memories | Bridged Claude Code memories | auto-imported |
Claude Memory Bridge
Auto-imports Claude Code's native ~/.claude/projects/*/memory/*.md files into AgentDB on session start with ONNX vector embeddings.
# Manual import (current project)
/memory-bridge
# Import all projects
/memory-bridge --all-projects
# Check bridge health
# Via MCP: memory_bridge_status({})
Results include source attribution: claude-code, auto-memory, or agentdb.
SmartRetrieval (ADR-090)
5-phase retrieval pipeline for higher-quality recall across sessions:
- Query expansion -- template-based variant generation (no LLM)
- Multi-query fan-out + RRF -- Reciprocal Rank Fusion across variants
- Recency boost -- exponential decay from metadata timestamps
- MMR diversity -- token-Jaccard Maximal Marginal Relevance re-ranking
- Session round-robin -- interleaved results from distinct sessions
# CLI
npx @claude-flow/cli@latest memory search --query "auth patterns" --smart --limit 10
# MCP
mcp__plugin_ruflo-core_ruflo__memory_search({ query: "auth patterns", smart: true, limit: 10 })
Best for multi-session recall, temporal queries ("what did we decide last week?"), and diverse result sets.
Unified Search
Queries across all namespaces simultaneously with MMR diversity reranking:
# Via MCP: memory_search_unified({ query: "auth security", limit: 5 })
# Via CLI:
npx @claude-flow/cli@latest memory search --query "auth security" --limit 5
HNSW Performance
Measured numbers from docs/reviews/intelligence-system-audit-2026-05-29.md + scripts/benchmark-intelligence.mjs:
| Operation | vs Brute Force | Notes | |-----------|----------------|-------| | Vector search (N=5k) | ~3.2x–4.7x faster | ruvector NAPI, recall@10 ~0.99 | | Vector search (N=20k) | ~1.9x faster | ANN wins above crossover | | Vector search (below crossover) | ties/loses | brute force preferred for small N |
The previously published "150x–12,500x" figures were brute-force fallback artifacts and are not reproduced under the audit harness.
Integration with ruvector
When ruflo-ruvector is also loaded, rag-memory delegates to ruvector's backend for advanced features:
- FlashAttention-3 for O(N) memory attention
- Graph RAG for multi-hop knowledge retrieval
- Hybrid search (sparse + dense) with RRF fusion
- DiskANN for large-scale persistent indexes
Compatibility
- CLI: pinned to
@claude-flow/cliv3.6 major+minor. - Verification:
bash plugins/ruflo-rag-memory/scripts/smoke.shis the contract.
Namespace coordination — claude-memories consumer
This plugin is the canonical user-facing consumer of the claude-memories reserved namespace defined in ruflo-agentdb ADR-0001 §"Namespace convention". The auto-import flow:
Claude Code SessionStart hook
→ memory_import_claude (MCP)
→ claude-memories namespace (reserved, ruflo-agentdb owned)
→ exposed by this plugin's memory-bridge skill + memory_search_unified
This plugin does not own claude-memories — it consumes it. Reserved namespaces (pattern, claude-memories, default) MUST NOT be shadowed.
Other namespaces (patterns, tasks, solutions, feedback, security) are accessed via memory_* (namespace-routed). The plugin uses correct routing throughout — no agentdb_hierarchical-* or agentdb_pattern-store with namespace arguments.
Verification
bash plugins/ruflo-rag-memory/scripts/smoke.sh
# Expected: "10 passed, 0 failed"
Architecture Decisions
Related Plugins
ruflo-agentdb— Full AgentDB controller bridge (15agentdb_*MCP tools); namespace convention owner; owns theclaude-memoriesreserved namespaceruflo-ruvector— Advanced vector operations (FlashAttention-3, Graph RAG, hybrid search)ruflo-rvf— Portable RVF memory format for cross-machine export/importruflo-knowledge-graph— Entity extraction and graph traversal over memory
License
MIT
As a mod
Function-hook mod (ADR-445, pattern of ruflo-agentdb). It loads from hooks/hooks.json → hooks/register.ts.
A write guard for memory: memory_store, agentdb_hierarchical-store and agentdb_pattern-store refuse secrets, so credentials never land in the vector store.
- Command:
/rag-modanswers locally with no model call. Verbs:status,scan <text>(would the guard refuse this?),tools(which memory tools are connected). - Status file:
.claude-flow/rag-mod/status.json({version, updatedMs, ...counters}), written at session start and as counters change. - Safety: no network, no process spawning; it only uses tools already connected.
- Option
guard(onby default,offto disable): a tighten-onlytool.callguard. A call to one of the tools above whose input holds a key, token, private key or password is denied. The reason never repeats the secret. - Test:
claude plugin validate plugins/ruflo-rag-memory,claude plugin test plugins/ruflo-rag-memory,bash plugins/ruflo-rag-memory/scripts/smoke.sh.

