fix(storage): resolve count synchronization race condition across all storage adapters

Fixed critical bug where entity and relationship counts were not being tracked correctly
during add(), relate(), and import() operations. The root cause was a race condition where
count increment code tried to read metadata before it was saved to storage.

Core Fixes:
- Modified baseStorage.saveNounMetadata_internal to increment counts AFTER metadata is saved
- Modified baseStorage.saveVerbMetadata_internal to increment verb counts AFTER metadata is saved
- Added verb type to VerbMetadata to avoid circular dependency during count tracking
- Refactored verb count methods to prevent mutex deadlocks (synchronous base + async Safe wrapper)

Storage Adapter Cleanup:
- Removed broken count increment code from FileSystemStorage, GcsStorage, R2Storage, AzureBlobStorage
- Updated MemoryStorage comments to reflect centralized fix
- All count tracking now centralized in baseStorage (fixes ALL adapters automatically)

New Utilities:
- Added rebuildCounts utility to repair corrupted counts.json from actual storage data
- Added comprehensive integration tests for count synchronization across all operations

Verification:
- All 8 storage adapters verified (FileSystem, GCS, Memory, S3Compatible, R2, Azure, OPFS, TypeAware)
- All code paths verified (add, relate, import, batch, update, delete)
- 599 tests passing (no regressions)
- No deadlocks (tests complete in 6s vs 150s+)

Fixes #1 and #2 reported by Workshop team
This commit is contained in:
David Snelling 2025-10-21 10:58:44 -07:00
parent e5c56ed285
commit 798a6946d6
10 changed files with 598 additions and 76 deletions

View file

@ -1015,47 +1015,63 @@ export abstract class BaseStorageAdapter implements StorageAdapter {
}
/**
* Increment verb count - O(1) operation with mutex protection
* Increment verb count - O(1) operation (v4.1.2: now synchronous)
* Protected by storage-specific mechanisms (mutex, distributed consensus, etc.)
* @param type The verb type
*/
protected async incrementVerbCount(type: string): Promise<void> {
protected incrementVerbCount(type: string): void {
this.verbCounts.set(type, (this.verbCounts.get(type) || 0) + 1)
this.totalVerbCount++
// Update cache
this.countCache.set('verbs_count', {
count: this.totalVerbCount,
timestamp: Date.now()
})
}
/**
* Thread-safe increment for verb counts (v4.1.2)
* Uses mutex for single-node, distributed consensus for multi-node
* @param type The verb type
*/
protected async incrementVerbCountSafe(type: string): Promise<void> {
const mutex = getGlobalMutex()
await mutex.runExclusive(`count-verb-${type}`, async () => {
this.verbCounts.set(type, (this.verbCounts.get(type) || 0) + 1)
this.totalVerbCount++
// Update cache
this.countCache.set('verbs_count', {
count: this.totalVerbCount,
timestamp: Date.now()
})
this.incrementVerbCount(type)
// Smart batching (v3.32.3+): Adapts to storage type
await this.scheduleCountPersist()
})
}
/**
* Decrement verb count - O(1) operation with mutex protection
* Decrement verb count - O(1) operation (v4.1.2: now synchronous)
* @param type The verb type
*/
protected async decrementVerbCount(type: string): Promise<void> {
protected decrementVerbCount(type: string): void {
const current = this.verbCounts.get(type) || 0
if (current > 1) {
this.verbCounts.set(type, current - 1)
} else {
this.verbCounts.delete(type)
}
if (this.totalVerbCount > 0) {
this.totalVerbCount--
}
// Update cache
this.countCache.set('verbs_count', {
count: this.totalVerbCount,
timestamp: Date.now()
})
}
/**
* Thread-safe decrement for verb counts (v4.1.2)
* @param type The verb type
*/
protected async decrementVerbCountSafe(type: string): Promise<void> {
const mutex = getGlobalMutex()
await mutex.runExclusive(`count-verb-${type}`, async () => {
const current = this.verbCounts.get(type) || 0
if (current > 1) {
this.verbCounts.set(type, current - 1)
} else {
this.verbCounts.delete(type)
}
if (this.totalVerbCount > 0) {
this.totalVerbCount--
}
// Update cache
this.countCache.set('verbs_count', {
count: this.totalVerbCount,
timestamp: Date.now()
})
this.decrementVerbCount(type)
// Smart batching (v3.32.3+): Adapts to storage type
await this.scheduleCountPersist()
})