Description
invalidateCache in src/cache/invalidation.ts detects stale reads using JSON.stringify(staleCheck) !== JSON.stringify(freshData). This is fragile: key ordering, undefined vs missing keys, and BigInt/Date serialization produce false positive or false negative stale detections, and the default verifyFn therefore mis-reports recordStaleCacheRead. We should compare via a stable, type-aware hash (reuse src/utils/hash.ts) so stale-read metrics are trustworthy.
Requirements and context
- Replace
JSON.stringify comparison in src/cache/invalidation.ts with a canonical hash from src/utils/hash.ts.
- Handle
BigInt, Date, and key-order normalization in the canonicalizer.
- Keep the custom
verifyFn override path working.
- Ensure
recordStaleCacheRead only fires on genuine divergence.
Suggested execution
Create branch feature/stable-cache-stale-detection.
- Add a canonical serializer to
src/utils/hash.ts.
- Modify
src/cache/invalidation.ts.
- Add
src/cache/invalidation.test.ts for ordering/BigInt/Date cases.
- Update
docs/CACHE_INVALIDATION.md.
Test and commit
Run npm run test. Edge cases: reordered keys equal, BigInt fields, nested objects, null vs undefined. Security: no sensitive data logged in stale warnings.
Example commit message
feat: use stable canonical hash for cache stale-read detection
Guidelines
- Minimum 95% test coverage
- Clear documentation in
docs/CACHE_INVALIDATION.md
- Timeframe: 96 hours
Description
invalidateCacheinsrc/cache/invalidation.tsdetects stale reads usingJSON.stringify(staleCheck) !== JSON.stringify(freshData). This is fragile: key ordering,undefinedvs missing keys, andBigInt/Dateserialization produce false positive or false negative stale detections, and the defaultverifyFntherefore mis-reportsrecordStaleCacheRead. We should compare via a stable, type-aware hash (reusesrc/utils/hash.ts) so stale-read metrics are trustworthy.Requirements and context
JSON.stringifycomparison insrc/cache/invalidation.tswith a canonical hash fromsrc/utils/hash.ts.BigInt,Date, and key-order normalization in the canonicalizer.verifyFnoverride path working.recordStaleCacheReadonly fires on genuine divergence.Suggested execution
Create branch
feature/stable-cache-stale-detection.src/utils/hash.ts.src/cache/invalidation.ts.src/cache/invalidation.test.tsfor ordering/BigInt/Date cases.docs/CACHE_INVALIDATION.md.Test and commit
Run
npm run test. Edge cases: reordered keys equal, BigInt fields, nested objects, null vs undefined. Security: no sensitive data logged in stale warnings.Example commit message
feat: use stable canonical hash for cache stale-read detectionGuidelines
docs/CACHE_INVALIDATION.md