fix: resolve clear() not deleting COW data and counters

Fixes critical bug where brain.clear() did not fully clear storage:

Root causes:
1. _cow/ directory contents deleted but directory not removed
2. In-memory counters (totalNounCount, totalVerbCount) not reset
3. COW could auto-reinitialize on next operation

Fixes applied:
- FileSystemStorage: Delete entire _cow/ directory with fs.rm()
- OPFSStorage: Delete _cow/ with removeEntry({recursive: true})
- S3CompatibleStorage: Reset counters after clear
- BaseStorage: Guard initializeCOW() against reinit when cowEnabled=false
- All adapters: Reset totalNounCount and totalVerbCount to 0

Impact: Resolves Workshop bug report - storage now properly clears from
103MB to 0 bytes, entity counts correctly return to 0.

GCSStorage, R2Storage, AzureBlobStorage already had correct implementations.
This commit is contained in:
David Snelling 2025-11-11 09:04:56 -08:00
parent ef7bf1b04c
commit e6cc12b64e
8 changed files with 141 additions and 4 deletions

View file

@ -2123,10 +2123,28 @@ export class S3CompatibleStorage extends BaseStorage {
// Delete all objects in the index directory
await deleteObjectsWithPrefix(this.indexPrefix)
// v5.6.1: Delete COW (copy-on-write) version control data
// This includes all git-like versioning data (commits, trees, blobs, refs)
// Must be deleted to fully clear all data including version history
await deleteObjectsWithPrefix('_cow/')
// CRITICAL: Reset COW state to prevent automatic reinitialization
// When COW data is cleared, we must also clear the COW managers
// Otherwise initializeCOW() will auto-recreate initial commit on next operation
this.refManager = undefined
this.blobStorage = undefined
this.commitLog = undefined
this.cowEnabled = false
// Clear the statistics cache
this.statisticsCache = null
this.statisticsModified = false
// v5.6.1: Reset entity counters (inherited from BaseStorageAdapter)
// These in-memory counters must be reset to 0 after clearing all data
;(this as any).totalNounCount = 0
;(this as any).totalVerbCount = 0
} catch (error) {
prodLog.error('Failed to clear storage:', error)
throw new Error(`Failed to clear storage: ${error}`)