fix: update() field asymmetry causing index corruption

CRITICAL: Fixed metadata index corruption on update() operations where
removalMetadata only contained custom metadata + type, while entityForIndexing
contained ALL indexed fields. This caused 7 fields to accumulate on every
update, eventually making queries return 0 results.

- Fix removalMetadata to include all indexed fields (src/brainy.ts)
- Add validateIndexConsistency() and getIndexStats() public APIs
- Add auto-corruption detection and repair on startup
- Add getOrAssignSync() for EntityIdMapper persistence
- Add comprehensive regression tests
This commit is contained in:
David Snelling 2026-01-26 12:12:11 -08:00
parent 478c6e6342
commit a94219e720
5 changed files with 470 additions and 4 deletions

View file

@ -86,6 +86,26 @@ export class EntityIdMapper {
return newId
}
/**
* v7.5.0: Get integer ID for UUID with immediate persistence guarantee
* Unlike getOrAssign(), this method flushes to storage immediately after assigning
* a new ID. This prevents UUIDint mapping divergence if the process crashes
* before a normal flush() occurs.
*
* Use this for critical operations where data integrity is paramount.
* Normal operations can use getOrAssign() with batched flushing for better performance.
*/
async getOrAssignSync(uuid: string): Promise<number> {
const id = this.getOrAssign(uuid)
// If a new ID was assigned, immediately persist to storage
if (this.dirty) {
await this.flush()
}
return id
}
/**
* Get UUID for integer ID
*/