-
Notifications
You must be signed in to change notification settings - Fork 2
feat(memory): graph-aware retrieval with BFS traversal and RRF fusion #1226
Description
Summary
Phase 3 of graph memory (#1222): augment existing recall with graph traversal, scoring, and RRF fusion.
Depends on: #1225 (extraction pipeline)
Tasks
1. Graph Retrieval (graph/retrieval.rs)
graph_recall(query: &str, store: &GraphStore, qdrant: &EmbeddingStore, provider: &AnyProvider, config: &GraphConfig) -> Result<Vec<GraphFact>>:
- Entity matching: Embed query → search
zeph_graph_entitiesQdrant collection (top-5 entities by cosine). Also run SQLite fuzzy match as fallback. - BFS expansion: For each matched entity, run
store.bfs(entity_id, config.max_hops)to collect neighbors + edges. - Scoring: For each collected edge, compute
GraphFact.composite_score()= entity_match_score * (1/(1+hop_distance)) * confidence. - Dedup: Remove duplicate edges (same source+target+relation).
- Rank and truncate: Sort by composite_score desc, take top
config.recall_limit.
2. Router Update
Add Graph variant to MemoryRoute enum (always present, not cfg-gated). Router only returns Graph when graph-memory feature is enabled. When disabled, Graph route is treated as Hybrid.
Update HeuristicRouter:
- "What does the user think about X?" → Graph route
- "How is X related to Y?" → Graph route
- Entity-centric queries (contains known entity name) → Graph route
3. Context Budget
Add graph_facts field to BudgetAllocation (always present, 0 when disabled):
- Redistribute: code_context 30% → 29%, graph_facts 0% → 4% (only when enabled)
- Existing allocations: summaries=8%, semantic_recall=8%, cross_session=4%, recent_history=50%
4. Context Formatting
Format graph facts as a system message with GRAPH_FACTS_PREFIX:
## Known Facts
- User prefers neovim for Rust development (since 2026-01) [confidence: 0.95]
- User works on project "zeph" (AI agent) [confidence: 1.0]
Architecture Reference
See .local/plan/graph-memory-architecture.md Section 5 for retrieval pseudocode, router patterns, and budget allocation.
Acceptance Criteria
- Graph recall returns relevant facts for entity-centric queries
- BFS traversal via recursive CTE respects max_hops
- RRF fusion produces better results than vector-only on relationship queries
- Router correctly identifies graph-suitable queries
- Budget allocation correct when enabled/disabled
- Context formatted with entity names, relations, and confidence
- ~20 tests (18 unit + 2 integration)