fix(8.0): stats() per-type counts no longer inflate with HNSW re-saves

nounCountsByType (read by stats().entitiesByType / counts.byTypeEnum) was
incremented in saveNoun_internal(), which runs on every noun write — including
the HNSW index re-saving a node whenever its neighbor links change. So per-type
counts grew with graph connectivity instead of entity count (an internal report
saw 8 documents read as 44). Moved the increment into saveNounMetadata_internal(),
gated on isNew + visibility, exactly parallel to the authoritative total; the
visibility-flip path adjusts it in lockstep too.

Tests: tests/unit/storage/stats-count-accuracy.test.ts (30 dense-type entities ->
exactly 30 across stats/counts/getNounCount) + de-theatricalized the >=2 assertion
in brainy-core.unit.test.ts to exact equality. Build green; 1455 unit green.

Part of the 8.0 readiness audit Phase 1; cold-reopen count rehydration is still WIP.
This commit is contained in:
David Snelling 2026-06-17 17:07:58 -07:00
parent b2005ff22a
commit 21d02d3bae
3 changed files with 79 additions and 12 deletions

View file

@ -202,8 +202,10 @@ describe('Brainy 3.0 Core (Unit Tests)', () => {
const stats = await brain.stats()
expect(stats.mode).toBe('writer')
expect(stats.entityCount).toBeGreaterThanOrEqual(2)
expect(stats.entitiesByType[NounType.Concept]).toBeGreaterThanOrEqual(2)
// Exact, not >= : the per-type counter must equal the entity count even
// after HNSW re-saves neighbor links (see stats-count-accuracy.test.ts).
expect(stats.entityCount).toBe(2)
expect(stats.entitiesByType[NounType.Concept]).toBe(2)
})
})