From 9456c2c74147f9de8c9290dbd88df1c7ccb2b36e Mon Sep 17 00:00:00 2001 From: David Snelling Date: Tue, 2 Dec 2025 11:45:17 -0800 Subject: [PATCH] fix(counts): counts.byType() returns inflated values due to accumulation bug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/graph/graphAdjacencyIndex.ts | 2 ++ src/utils/metadataIndex.ts | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/graph/graphAdjacencyIndex.ts b/src/graph/graphAdjacencyIndex.ts index bf0963af..af58c5cf 100644 --- a/src/graph/graphAdjacencyIndex.ts +++ b/src/graph/graphAdjacencyIndex.ts @@ -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 diff --git a/src/utils/metadataIndex.ts b/src/utils/metadataIndex.ts index 70223374..b0b92105 100644 --- a/src/utils/metadataIndex.ts +++ b/src/utils/metadataIndex.ts @@ -381,6 +381,12 @@ export class MetadataIndexManager { */ private async lazyLoadCounts(): Promise { 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')