fix(brain-cache): normalize valid-but-partial _meta.json so consumers don't crash#1880
Open
jbetala7 wants to merge 1 commit into
Open
fix(brain-cache): normalize valid-but-partial _meta.json so consumers don't crash#1880jbetala7 wants to merge 1 commit into
jbetala7 wants to merge 1 commit into
Conversation
… don't crash
loadMeta starts fresh when _meta.json is missing or corrupt (unparseable),
but returned a valid-but-partial meta verbatim. A meta that parses yet lacks
the last_refresh map (external tooling, hand-edit, partial-but-valid state)
then crashed every consumer that dereferences it:
- isStale (via cmdGet warm-path): meta.last_refresh[entityName]
- cmdInvalidate: delete meta.last_refresh[entityName]
- refreshEntity: meta.last_refresh[entityName] = Date.now()
refreshEntity already guarded its sibling map (last_attempt = last_attempt || {}),
and the type marks last_attempt optional but last_refresh required, so the
guard was applied to only one of the two maps. Normalize both once in loadMeta
so a partial file degrades like the corrupt-file case instead of throwing.
Adds regression tests covering cmdGet and cmdInvalidate against a _meta.json
that lacks last_refresh.
Fixes garrytan#1879
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Merging to
After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
bin/gstack-brain-cachecrashes with aTypeErrorwhen its_meta.jsonis valid JSON but lacks thelast_refreshmap.loadMetaalready starts fresh for a missing file and for corrupt (unparseable) JSON, but a file that parses successfully is returned verbatim with no normalization. Three consumers then dereferencemeta.last_refreshunguarded:isStale(reached viacmdGet's warm-path check) —meta.last_refresh[entityName]cmdInvalidate—delete meta.last_refresh[entityName]refreshEntity—meta.last_refresh[entityName] = Date.now()refreshEntityalready guards the sibling map (meta.last_attempt = meta.last_attempt || {}), and theCacheMetatype markslast_attempt?optional whilelast_refreshis required — so the guard was applied to only one of the two maps.Repro (on
main)A
_meta.jsonwhoseschema_versionandendpoint_hashmatch the current pack (so the schema/endpoint rebuild path is not taken) but with nolast_refreshkey:cmdGet('product', 'helsinki')→TypeError: undefined is not an object (evaluating 'meta.last_refresh[entityName]')cmdInvalidate('product', 'helsinki')→TypeError: undefined is not an object (evaluating 'delete meta.last_refresh[entityName]')Such a meta can arise from external tooling, a hand-edit, or any valid-but-partial persisted state — the same class of input the corrupt-JSON branch already guards against.
Root cause
loadMetanormalizes the missing-file and corrupt-JSON cases but returns a valid-but-partial meta as-is, leavinglast_refresh/last_attemptpossiblyundefinedfor downstream consumers.Fix
Normalize both maps once in
loadMetaso a partial file degrades like the corrupt-file case instead of crashing. A meta withoutlast_refreshis now treated as never-refreshed:cmdGetfalls through to a normal cold-refresh/stale path andcmdInvalidateis a safe no-op.Testing
bun test test/brain-cache-roundtrip.test.ts test/brain-cache-spec.test.ts→ 32 pass / 0 failcmdGet/cmdInvalidateagainst a_meta.jsonlackinglast_refresh); both fail onmainwith theTypeErrorabove and pass with this change.Fixes #1879
🤖 Generated with Claude Code