[Routine - QP] fix: guard VersionManager against malformed history JSON shape#48
Draft
winterdrive wants to merge 1 commit into
Draft
[Routine - QP] fix: guard VersionManager against malformed history JSON shape#48winterdrive wants to merge 1 commit into
winterdrive wants to merge 1 commit into
Conversation
JSON.parse succeeding on unexpected shapes (e.g. an array, or an object missing `versions`/`currentVersionId`) previously slipped past the generic catch block and would only surface later as a TypeError when callers accessed `history.versions.find(...)`. Add the same shape validation already used in VersionHistoryService.loadHistory so a corrupted or malformed *.history.json resets to an empty history instead of crashing downstream callers.
This was referenced Jul 7, 2026
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.
Pre-flight Check
Open PRs / active branches checked before selecting this target:
routine/qp-fix-mcp-clipboard-non-array-260706— touchesmcp-server/src/tools/clipboardTools.tsroutine/qp-fix-versionhistory-substr-260703— touchessrc/services/VersionHistoryService.tschore/ignore-claude-worktrees-jest— touchesjest.config.jsroutine/qp-fix-path-traversal-prefix-260702— touchessrc/core/PathUtils.ts,src/test/unit/pathUtils.test.tsfix/quick-create-workspace-ux— touchessrc/commands.ts,src/promptProvider.ts,src/test/unit/promptProviderMultiroot.test.tsThis PR only touches
src/core/VersionManager.tsandsrc/test/unit/versionManager.test.ts, which none of the above PRs modify, so there is no overlap.Changes
VersionManager.loadHistory(the pure Node.js core version manager, distinct fromVersionHistoryService.tswhich #46 is fixing) parses*.history.jsonwith a bareJSON.parseinside a try/catch. The catch only handlesJSON.parsethrowing (e.g. corrupted JSON) — it does not catch the case where parsing succeeds but produces the wrong shape (e.g. the file contains[1,2,3]or{}instead of{ promptId, versions, currentVersionId }). In that casehistory.versionsisundefined, and the very next line calling.find(...)elsewhere (getCurrentVersion,applyVersion,createVersion, etc.) throws a rawTypeErrorinstead of falling back gracefully.This mirrors the fix already merged for the sibling file
VersionHistoryService.ts(commit20b6a09, "guard VersionHistoryService against corrupted/malformed history JSON") — same bug pattern, same fallback strategy (reset to empty history), applied to the not-yet-patchedVersionManager.ts.VersionManager.isValidHistorytype guard (same shape check asVersionHistoryService.isValidHistory).loadHistorynow throws internally (caught by the existing catch block) when the parsed JSON doesn't match the expected shape, resetting to an empty history instead of letting a malformed shape propagate.VersionHistoryServicecorrupted-JSON tests: corrupted JSON text, and valid-but-wrong-shape JSON (an array).Confirms this PR does not modify any MCP tool schema/names/parameters in
mcp-server/src/tools/, and does not add/remove/update any dependencies.Safety Verification
Commands run locally, all passed:
npx tsc -p ./— no errorsnpm run test:coverage(project's Jest command) — 7 test suites, 109 tests, all passed (includes the 2 new tests plus all pre-existing suites)CI / Release Gate Note
This is a daily routine Draft PR. Package version bump (
package.json/package-lock.json) andCHANGELOG.mdupdates are intentionally deferred to the weekend release/integration PR. If GitHub Actions fails only due to the repository's version-bump gate, that is expected release-readiness behavior for a routine PR, not a code validation failure.