Memory Graph Architecture
How graph nodes and edges are extracted, stored, retrieved, and guarded in memories.sh.
This page describes the graph pipeline end-to-end: write path, storage model, hybrid retrieval, and rollout guardrails.
Why The Graph Exists
Baseline retrieval ranks memories by text relevance. The graph layer adds relationship traversal so related memories can be recalled even when exact query terms differ.
Examples:
- Query mentions
rollout alarms, but the best supporting memory is connected throughtopic:dashboard. - Query mentions
hybrid strategy, and relateddecisionorrulememories are reached through graph edges instead of keyword overlap.
Storage Model
Graph storage is maintained in the workspace Turso database:
graph_nodes: canonical entities (repo,topic,category,memory_type, etc.)graph_edges: typed, weighted directional relationships between nodesmemory_node_links: mapping between each memory and its extracted nodes
Primary code paths:
/Users/tradecraft/dev/memories/packages/web/src/lib/memory-service/graph/upsert.ts/Users/tradecraft/dev/memories/packages/web/src/lib/memory-service/graph/retrieval.ts/Users/tradecraft/dev/memories/packages/web/src/lib/memory-service/graph/status.ts
Write Path
- A memory write/update enters memory mutation handlers.
- Graph extraction produces node and edge candidates from memory content and metadata.
upsert.tsensures schema, upserts nodes, writes memory-node links, writes evidence-backed edges, and prunes stale links for edited/forgotten memories.- Cleanup removes orphan graph nodes with no links or edges.
Behavior is best-effort: memory writes stay available even if graph indexing fails.
Read Path (Hybrid Retrieval)
Hybrid retrieval runs in layered recall logic:
- Baseline candidates are gathered from rules, working memories, and long-term memories.
- If request strategy is
hybrid_graphand rollout allows it, graph expansion traverses related nodes/edges from seed memories. - Graph-expanded memories are appended with provenance metadata (
whyIncluded: graph_expansion, linked node, edge type, hop count). - If graph expansion errors or rollout disallows application, retrieval falls back safely to baseline.
Fallback reasons include:
feature_flag_disabledrollout_offshadow_modequality_gate_blockedgraph_expansion_errorrollout_guardrail
Rollout And Safety Controls
Workspace rollout mode controls whether hybrid expansion is applied:
off: baseline onlyshadow: execute graph path for metrics, return baselinecanary: apply graph expansion forstrategy: hybrid_graphrequests
Safety and observability are emitted through graph status and trace payloads:
/api/sdk/v1/graph/status/api/sdk/v1/graph/rollout/api/sdk/v1/graph/trace
Canary mode is also protected by a quality gate that evaluates fallback and relevance regressions over rolling windows before allowing canary application.
Scope And Workspace Routing
Graph data respects the same routing model as memories:
tenantId: AI SDK Project selector (security/database boundary)userId: end-user scope insidetenantIdprojectId: optional project/repo context filter (not an auth boundary)
When workspace repo-routing mode is auto, org repos route to org workspace memory and personal repos route to personal workspace memory by default.
Dashboard Explorer Mapping
The dashboard graph explorer (Memory Graph section) reads from /api/graph/explore and renders:
- Relationship graph (interactive node/edge view)
- Selected edge metadata (direction, weight, confidence, evidence, expiry)
- Linked memories and evidence highlighting
This view is designed for debugging retrieval quality and data linkage, not only for health checks.