Fixes two critical production bugs in GCS storage adapter:
1. brain.find({ where: {...} }) returned empty array after restart
- Root cause: Counts only persisted every 10 operations
- If <10 entities added before restart, counts were lost
- After restart: totalNounCount = 0, causing empty results
2. brain.init() returned 0 entities after container restart
- Same root cause as bug #1
- Counts file never written for small datasets
- getStats() returned 0 despite data in GCS bucket
Changes:
- baseStorageAdapter.ts: Persist counts on EVERY operation (not every 10)
- incrementEntityCountSafe(): Now persists immediately
- decrementEntityCountSafe(): Now persists immediately
- incrementVerbCount(): Now persists immediately
- decrementVerbCount(): Now persists immediately
- gcsStorage.ts: Better error handling for count initialization
- initializeCounts(): Fail loudly on network/permission errors
- initializeCountsFromScan(): Throw on scan failures instead of silent fail
- Added recovery logic with bucket scan fallback
Impact: Critical for serverless/containerized deployments (Cloud Run, Fargate, Lambda)
where containers restart frequently. The basic write→restart→read scenario now works.
Fixed bug where getMetadataBatch() was reading from wrong directory:
- FileSystemStorage: Changed to use getNounMetadata() instead of getMetadata()
- OPFSStorage: Changed to use getNounMetadata() instead of getMetadata()
- MetadataIndex fallback: Fixed to use getNounMetadata()
- Added getNounMetadata() to StorageAdapter interface
This resolves 0% success rate during metadata index rebuild.
Also added comprehensive API documentation for return values and data field behavior.
- Removed countNouns() and countVerbs() from BaseStorageAdapter
- These methods would be dangerous with millions of entries
- We already have incremental statistics tracking via incrementStatistic()
- Statistics are maintained in cache and updated as items are added/removed
- Much more scalable than iterating through all items
The existing statistics system is the proper way to get counts:
- Uses incrementStatistic('noun'/'verb', service) on add
- Uses decrementStatistic() on delete
- Access via getStatistics() which returns cached counts
- No iteration through millions of items needed
- Add missing getVerbsWithPagination() method to FileSystemStorage
- Fixes verb retrieval returning empty arrays
- Add pagination method declarations to BaseStorageAdapter interface
- Support filtering by sourceId, targetId, verbType, and service
- Include metadata retrieval for each verb
Resolves issue where brain.getVerbs() returned empty array even after
successfully adding verbs with FileSystemStorage adapter.