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

@ -1021,10 +1021,32 @@ export class FileSystemStorage extends BaseStorage {
if (await this.directoryExists(this.indexDir)) {
await removeDirectoryContents(this.indexDir)
}
// v5.6.1: Remove COW (copy-on-write) version control data
// This directory stores all git-like versioning data (commits, trees, blobs, refs)
// Must be deleted to fully clear all data including version history
const cowDir = path.join(this.rootDir, '_cow')
if (await this.directoryExists(cowDir)) {
// Delete the entire _cow/ directory (not just contents)
await fs.promises.rm(cowDir, { recursive: true, force: true })
// 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
}
/**