v3/implementation/adrs/ADR-050-intelligence-loop.md
Status: Accepted Date: 2026-02-09 Authors: Claude Flow Team
The memory system has three powerful modules — AutoMemoryBridge (storage), MemoryGraph (PageRank + community detection), LearningBridge (confidence tracking) — all shipped and tested (219 tests, published as @claude-flow/[email protected]). But they are not wired into the hook system that runs during Claude Code sessions.
The result is a gap:
Record insight -> Store in AgentDB -> [GAP] -> Retrieve at right moment -> Better output
No active process builds graph edges, ranks knowledge by importance, injects ranked context into prompts, or feeds success/failure signals back. Hooks fire at exactly the right moments (session-restore, route, post-edit, session-end) but only do simple routing and metric counting.
Add a CJS intelligence layer (intelligence.js) to the hook system with file-based graph persistence. This module is loaded by hook-handler.cjs via safeRequire() and provides five functions wired to existing hook events:
| Function | Hook | Budget | Purpose |
|---|---|---|---|
init() | session-restore | <200ms | Build graph, compute PageRank, write caches |
getContext(prompt) | route | <15ms | Match prompt to ranked entries, return top-5 |
recordEdit(file) | post-edit | <2ms | Append to pending-insights.jsonl |
feedback(success) | post-task | <10ms | Boost/decay confidence for matched patterns |
consolidate() | session-end | <500ms | Process insights, rebuild edges, recompute PageRank |
All under .claude-flow/data/:
auto-memory-store.json (existing) — written by auto-memory-hook.mjsgraph-state.json (new) — serialized graph: nodes + edges + pageRanksranked-context.json (new) — pre-computed ranked entries for fast lookuppending-insights.jsonl (new) — append-only edit/task logSelf-contained CJS implementations (~60 lines each):
Hooks are short-lived Node.js processes invoked by Claude Code. hook-handler.cjs uses require(). ESM dynamic import() is async and adds ~50ms overhead per invocation. The memory package (@claude-flow/memory) is ESM-only. The intelligence layer must be CJS for synchronous, fast loading.
Each hook invocation is a separate process. There is no long-running daemon to hold state in memory. JSON files provide simple, atomic persistence between invocations. The graph state (~50KB for 100 entries) loads in <5ms.
auto-memory-hook.mjs is a separate ESM process called by SessionStart/SessionEnd hooks. It handles import/sync of the full memory package. The intelligence layer runs inside hook-handler.cjs for every hook event (route, post-edit, etc.) and must be CJS.
A daemon would require process management, health checking, and IPC complexity. The file-based approach is simpler, more reliable, and stays within the performance budget.
[INTELLIGENCE] output| Hook | Baseline | + Intelligence | Total |
|---|---|---|---|
| session-restore | 28ms | +150ms (one-time) | 178ms |
| route | 28ms | +10ms (cached read) | 38ms |
| post-edit | 28ms | +1ms (append) | 29ms |
| session-end | 28ms | +300ms (recompute) | 328ms |
All within existing hook timeouts (10-15s).
| Action | File | Lines |
|---|---|---|
| CREATE | cli/.claude/helpers/intelligence.cjs | ~560 |
| CREATE | v3/implementation/adrs/ADR-050-intelligence-loop.md | ~150 |
| MODIFY | cli/.claude/helpers/hook-handler.cjs | +30 |
| MODIFY | cli/.claude/helpers/session.js | +6 |
| MODIFY | cli/src/init/executor.ts | +1 |