fix(blueprint): add error handling for JSON.parse in state, config, and migration loaders#638
fix(blueprint): add error handling for JSON.parse in state, config, and migration loaders#638pjt222 wants to merge 1 commit intoNVIDIA:mainfrom
Conversation
…nd migration loaders Three sites used bare JSON.parse(readFileSync(...)) that crash on corrupted files. The same codebase already has robust error handling in loadConfigDocument() (migration-state.ts:143-153) — this applies the same patterns to the remaining unprotected call sites. Fixes: - loadState() (state.ts): try-catch returning blankState() — state is ephemeral machine-written data, silent recovery matches the existing null-on-missing pattern and allows self-healing on next saveState() call. - loadOnboardConfig() (config.ts): try-catch returning null — triggers re-onboarding, matches the null-on-missing pattern. - readSnapshotManifest() (migration-state.ts): structural validation (not-null, is-object, not-array) following the loadConfigDocument() pattern — throws on corruption because migration cannot proceed safely with corrupt manifest data. Related to NVIDIA#553 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
📝 WalkthroughWalkthroughThe PR adds error-handling safeguards for JSON file operations across three modules (state, migration-state, and config). Each loader function now gracefully handles malformed or invalid JSON by returning fallback values instead of throwing exceptions, and validation logic ensures parsed data matches expected types. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
|
Thanks for submitting this proposed fix to add error handling for JSON.parse and additional tests, which may improve the overall reliability of the loaders. |
Summary
JSON.parsecalls inloadState(),loadOnboardConfig(), andreadSnapshotManifest()loadConfigDocument()inmigration-state.ts(lines 143-153)loadState()recovers silently (returns blank state) — state is machine-written ephemeral data, and the nextsaveState()call self-heals the fileloadOnboardConfig()recovers silently (returns null) — allows re-onboardingreadSnapshotManifest()throws a descriptive error with structural validation (matchingloadConfigDocumentpattern) — migration cannot proceed safely with corrupt dataRelated to #553
Test plan
cd nemoclaw && npx vitest run— 79 tests pass (4 new)npx tsc --noEmit— no type errorsstate.test.tsconfig.test.tsmigration-state.test.tsCo-Authored-By: Claude Opus 4.6 (1M context) noreply@anthropic.com