fix: add WORLD_CACHE_SCHEMA_VERSION to cache key for automatic invalidation

Cache keys now include a schema version prefix so that world-schema
changes automatically invalidate stale cached extraction results.
Previously two incompatible schema versions would share the same
cache key if URL/selectors/env matched, silently returning stale data.

WORLD_CACHE_SCHEMA_VERSION exported publicly so consumers can
increment it when making schema-incompatible changes to extraction.

658 tests pass.
This commit is contained in:
John Dvorak
2026-05-22 13:35:12 -07:00
parent 1bc0c8e6df
commit a49d6008a4
2 changed files with 9 additions and 1 deletions
+8 -1
View File
@@ -193,10 +193,17 @@ function djb2Hash(str: string): string {
return hash.toString(16).padStart(8, '0')
}
/**
* Schema version for cache invalidation. Increment when the world schema
* changes in a way that makes previously cached extraction results incompatible.
*/
export const WORLD_CACHE_SCHEMA_VERSION = 2
/**
* Compute a stable cache key for a geometry extraction.
*
* The key incorporates:
* - schema version (automatic invalidation on schema changes)
* - page URL
* - sorted selectors (so order doesn't matter)
* - environment (viewport dimensions, color scheme, etc.)
@@ -221,7 +228,7 @@ export function computeGeometryCacheKey(
}),
)
const urlHash = djb2Hash(pageUrl)
return `${urlHash}-${selectorsHash}-${envHash}`
return `${WORLD_CACHE_SCHEMA_VERSION}-${urlHash}-${selectorsHash}-${envHash}`
}
// ---------------------------------------------------------------------------
+1
View File
@@ -60,6 +60,7 @@ export {
getDefaultCacheDir,
readCachedExtractionResult,
writeCachedExtractionResult,
WORLD_CACHE_SCHEMA_VERSION,
} from './geometry-cache.js'
// Public API type aliases for high-level evaluation results