fix(counts): counts.byType() returns inflated values due to accumulation bug

Two critical issues fixed:

1. MetadataIndex.lazyLoadCounts() - Added counts to existing Map instead of
   replacing. Each app restart caused counts to double, leading to 100x
   inflation after ~100 restarts.

2. MetadataIndex.rebuild() and GraphAdjacencyIndex.rebuild() - Did not clear
   count Maps before rebuilding, causing accumulation.

Changes:
- Clear totalEntitiesByType, entityCountsByTypeFixed, verbCountsByTypeFixed
  at start of lazyLoadCounts()
- Clear totalEntitiesByType, entityCountsByTypeFixed, verbCountsByTypeFixed,
  typeFieldAffinity in MetadataIndex.rebuild()
- Clear relationshipCountsByType in GraphAdjacencyIndex.rebuild()

Reported by: Soulcraft Workshop Team

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
David Snelling 2025-12-02 11:45:17 -08:00
parent ea53c11fea
commit 9456c2c741
2 changed files with 15 additions and 0 deletions

View file

@ -568,6 +568,8 @@ export class GraphAdjacencyIndex {
// Clear current index
this.verbIdSet.clear()
this.totalRelationshipsIndexed = 0
// v6.2.4: CRITICAL FIX - Clear relationship counts to prevent accumulation
this.relationshipCountsByType.clear()
// Note: LSM-trees will be recreated from storage via their own initialization
// Verb data will be loaded on-demand via UnifiedCache

View file

@ -381,6 +381,12 @@ export class MetadataIndexManager {
*/
private async lazyLoadCounts(): Promise<void> {
try {
// v6.2.4: CRITICAL FIX - Clear counts before loading to prevent accumulation
// Previously, counts accumulated across restarts causing 100x inflation
this.totalEntitiesByType.clear()
this.entityCountsByTypeFixed.fill(0)
this.verbCountsByTypeFixed.fill(0)
// v6.2.2: Load counts from sparse index (correct source)
const nounSparseIndex = await this.loadSparseIndex('noun')
if (!nounSparseIndex) {
@ -2501,6 +2507,13 @@ export class MetadataIndexManager {
this.fieldIndexes.clear()
this.dirtyFields.clear()
// v6.2.4: CRITICAL FIX - Clear type counts to prevent accumulation
// Previously, counts accumulated across rebuilds causing incorrect values
this.totalEntitiesByType.clear()
this.entityCountsByTypeFixed.fill(0)
this.verbCountsByTypeFixed.fill(0)
this.typeFieldAffinity.clear()
// Clear all cached sparse indices in UnifiedCache
// This ensures rebuild starts fresh (v3.44.1)
this.unifiedCache.clear('metadata')