From 6b9250ea8cdf95d30d9a9f660e136e48d45b951d Mon Sep 17 00:00:00 2001 From: rUv Date: Thu, 14 May 2026 19:44:08 -0400 Subject: [PATCH] fix(memory): memory stats HNSW count reads persistent DB (#1987) (#1993) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `memory stats` always reported `HNSW Index: active (0 entries)` even when `memory store` had just stored a vector and `memory search` successfully retrieved it. Root cause: the rendered count was `hnsw.entryCount`, which is the live in-process JS state (`hnswIndex?.entries.size`). A fresh `memory stats` invocation has never initialized HNSW in this Node process, so the in-process counter is always 0 — even when the persistent .swarm/memory.db has many entries with embeddings. Fix: prefer the persistent count from the `memory_stats` MCP tool, which already computes `entriesWithEmbeddings` by listing the DB's real rows. Fall back to the live counter when persistent count isn't available. When both are non-zero, take the max (covers the edge case where the live process has indexed more entries than the persisted store has flushed). Behavior matrix: hnsw.available persisted live → rendered false any any → "not active" (unchanged) true 0 0 → "available but not initialized" true N>0 any → "active (max(N,live) entries)" true 0 M>0 → "active (M entries)" (unchanged) Adds `entriesWithEmbeddings?: number` to the stats result type so TypeScript is happy. The MCP tool already returns this field; this just teaches the consumer to read it. Co-authored-by: Reuven --- v3/@claude-flow/cli/src/commands/memory.ts | 23 +++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/v3/@claude-flow/cli/src/commands/memory.ts b/v3/@claude-flow/cli/src/commands/memory.ts index 2982d24e4f..8a5db27d8f 100644 --- a/v3/@claude-flow/cli/src/commands/memory.ts +++ b/v3/@claude-flow/cli/src/commands/memory.ts @@ -660,6 +660,7 @@ const statsCommand: Command = { try { const statsResult = await callMCPTool('memory_stats', {}) as { totalEntries: number; + entriesWithEmbeddings?: number; totalSize: string; version: string; backend: string; @@ -768,11 +769,23 @@ const statsCommand: Command = { }, { metric: 'HNSW Index', - value: hnsw.available && hnsw.initialized - ? output.success(`active (${hnsw.entryCount.toLocaleString()} entries)`) - : hnsw.available - ? output.warning('available but not initialized') - : output.dim('not active'), + // ruflo#1989 / #1987: `hnsw.entryCount` is in-process JS state + // (the live HNSW index of the current Node process). A fresh + // `memory stats` invocation has never indexed anything, so it + // reports 0 even when the persistent DB has thousands of + // entries with embeddings. Use the persistent count from the + // MCP tool (`entriesWithEmbeddings`, which is the actual + // count of rows that have a vector) as the source of truth. + value: (() => { + const persisted = typeof statsResult.entriesWithEmbeddings === 'number' + ? statsResult.entriesWithEmbeddings + : null; + const live = hnsw.entryCount || 0; + const total = persisted !== null ? Math.max(persisted, live) : live; + if (!hnsw.available) return output.dim('not active'); + if (total === 0) return output.warning('available but not initialized'); + return output.success(`active (${total.toLocaleString()} entries)`); + })(), }, ] });