v3/implementation/adrs/ADR-049-self-learning-memory-gnn.md
Status: Implemented Date: 2026-02-08 Authors: RuvNet, Claude Flow Team Supersedes: None Related: ADR-048 (Auto Memory Integration), ADR-006 (Unified Memory), ADR-009 (Hybrid Memory Backend)
ADR-048 established the AutoMemoryBridge for bidirectional sync between Claude Code auto memory files and AgentDB. While this successfully bridges the two systems, it operates as a passive store — insights are recorded but the system does not learn from them. Three gaps exist:
No learning pipeline: The @claude-flow/neural package has a fully implemented NeuralLearningSystem with SONA, ReasoningBank (4-step RETRIEVE/JUDGE/DISTILL/CONSOLIDATE pipeline), and PatternLearner — but these are completely disconnected from the memory bridge.
No knowledge graph: MemoryEntry.references supports graph relationships between entries, but nothing constructs or queries a graph from them. Insights are flat lists without structural understanding.
No agent scoping: Claude Code supports 3-scope agent memory directories (project, local, user) for per-agent knowledge isolation, but the bridge only handles the project-level auto memory directory.
Extend the AutoMemoryBridge with three new modules:
learning-bridge.ts)Connects insights to the neural learning pipeline:
Insight Recorded → Begin Trajectory → Record Steps
Insight Accessed → Boost Confidence → Record Step
Consolidate → Complete Trajectories → JUDGE/DISTILL/CONSOLIDATE
Time Passes → Decay Confidences
@claude-flow/neural is loaded dynamically; when unavailable, all learning operations degrade to no-ops (confidence remains static).memory-graph.ts)Builds an in-memory knowledge graph from entry references and similarity:
Build from Backend → Add Nodes → Add Reference Edges
→ Add Similarity Edges (HNSW)
Compute PageRank → Power Iteration (d=0.85, convergence=1e-6)
Detect Communities → Label Propagation
Rank with Graph → alpha * vectorScore + (1-alpha) * normalizedPageRank
curateIndex() can prioritize high-PageRank entries for MEMORY.md, ensuring the most connected/influential insights appear first.reference (from MemoryEntry.references), similar (from HNSW search), temporal, co-accessed, causal.agent-memory-scope.ts)Maps Claude Code's 3-scope agent memory system:
project: <gitRoot>/.claude/agent-memory/<agentName>/
local: <gitRoot>/.claude/agent-memory-local/<agentName>/
user: ~/.claude/agent-memory/<agentName>/
┌─────────────────────────────────────────────────────────────┐
│ Claude Code Session │
│ ┌──────────────────┐ ┌──────────────────┐ │
│ │ Auto Memory │ │ Agent Memory │ │
│ │ (MEMORY.md) │ │ (3-scope dirs) │ │
│ └────────┬─────────┘ └────────┬─────────┘ │
│ │ │ │
│ ┌────────▼──────────────────────▼─────────┐ │
│ │ AutoMemoryBridge │ │
│ │ ┌──────────────┐ ┌─────────────────┐ │ │
│ │ │ LearningBridge│ │ MemoryGraph │ │ │
│ │ │ (optional │ │ (PageRank + │ │ │
│ │ │ neural) │ │ communities) │ │ │
│ │ └──────┬────────┘ └──────┬──────────┘ │ │
│ │ │ │ │ │
│ │ ┌──────▼──────────────────▼──────────┐ │ │
│ │ │ AgentDB (HNSW) │ │ │
│ │ │ 150x-12,500x faster search │ │ │
│ │ └────────────────────────────────────┘ │ │
│ └──────────────────────────────────────────┘ │
│ │ │
│ ┌────────▼─────────────────┐ │
│ │ @claude-flow/neural │ (optional peer dependency) │
│ │ - NeuralLearningSystem │ │
│ │ - SONA + ReasoningBank │ │
│ │ - PatternLearner │ │
│ └──────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
config.learning?: LearningBridgeConfig — enables learningconfig.graph?: MemoryGraphConfig — enables graphrecordInsight() → calls learningBridge.onInsightRecorded()syncToAutoMemory() → calls learningBridge.consolidate() firstcurateIndex() → uses graph.getTopNodes() for section orderingimportFromAutoMemory() → builds graph from imported entriesdestroy() → calls learningBridge.destroy()session-start → importFromAutoMemory(), build graphsession-end → consolidate(), syncToAutoMemory(), curateIndex()post-task → recordInsight() for task learnings| Operation | Target | Rationale |
|---|---|---|
| Graph build (1k nodes) | <200ms | Startup cost, amortized over session |
| PageRank (1k nodes) | <100ms | Power iteration converges fast |
| Consolidation | <500ms | Batch trajectory completion |
| Confidence decay (1k entries) | <50ms | Simple arithmetic per entry |
| Knowledge transfer (20 entries) | <100ms | Query + store operations |
@claude-flow/neural unavailable| Action | File | Lines | Tests |
|---|---|---|---|
| CREATE | memory/src/learning-bridge.ts | 453 | — |
| CREATE | memory/src/learning-bridge.test.ts | 723 | 56 |
| CREATE | memory/src/memory-graph.ts | 392 | — |
| CREATE | memory/src/memory-graph.test.ts | 732 | 60 |
| CREATE | memory/src/agent-memory-scope.ts | 300 | — |
| CREATE | memory/src/agent-memory-scope.test.ts | 613 | 30 |
| MODIFY | memory/src/auto-memory-bridge.ts | 953 (+70) | 73 |
| MODIFY | memory/src/types.ts | +35 | — |
| MODIFY | memory/src/index.ts | +15 | — |
Total: 219 tests passing in 385ms across 4 test suites
LearningBridge class with neuralLoader injection (453 lines)MemoryGraph class with PageRank + label propagation (392 lines)createAgentBridge() factory for scoped bridgestransferKnowledge() with confidence filtering + content-hash deduplistAgentScopes() for scope discoverylearning and graph options@claude-flow/neural becomes an optional peer dependency