fix(memory): memory stats HNSW count reads persistent DB (#1987) (#1993)

`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 <cohen@ruv-mac-mini.local>
This commit is contained in:
rUvandReuven authored and GitHub committed 2026-05-14 19:44:08 -04:00
1 parent 4680a2c044
commit 6b9250ea8c
1 file changed
+18 -5
+18 -5
View File
@@ -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)`);
})(),
},
]
});