fix(#1657): recover malformed (non-object) ~/.gsd/defaults.json in finishInstall (#1661)

* fix(#1657): recover malformed (non-object) ~/.gsd/defaults.json in finishInstall

JSON.parse of defaults.json succeeds for valid-JSON-but-non-object values (null, [],
42, "str"), which then bypassed the parse catch: null threw a TypeError on property
access (swallowed by the outer try/catch), and array/number/string had resolve_model_ids
set on a non-object whose JSON.stringify round-trip kept the broken shape. The non-Claude
finishInstall step now resets any non-object (null, non-object, or array) parse result to
{} before reading/writing, so the file is repaired and resolve_model_ids defaults normally.
Regression folded into the owning tests/bug-410-install-defaults-test-mode-guard.test.cjs
(parameterized over null/[]/42/"str").

* chore(#1657): backfill changeset pr ref to 1661
This commit is contained in:
Tom Boucher
2026-06-24 13:48:16 -04:00
committed by GitHub
parent e1d768dd78
commit 752df8adb4
3 changed files with 50 additions and 0 deletions

View File

@@ -11322,6 +11322,14 @@ function finishInstall(settingsPath, settings, statuslineCommand, shouldInstallS
fs.mkdirSync(gsdDir, { recursive: true });
let defaults = {};
try { defaults = JSON.parse(fs.readFileSync(defaultsPath, 'utf8')); } catch { /* new file */ }
// Recover a malformed (valid-JSON-but-non-object) defaults.json to a fresh object so
// the write below succeeds and the file is no longer broken. Without this, `null` /
// `[]` / a number / a string bypass the parse catch and either throw a TypeError on
// property access (swallowed by the outer try/catch, leaving the file broken) or get
// a property set that won't round-trip through JSON.stringify. (#1657)
if (defaults === null || typeof defaults !== 'object' || Array.isArray(defaults)) {
defaults = {};
}
// Three-valued domain: false/absent → aliases; true → full IDs; "omit" → ''.
// Honor ONLY an explicit canonical `true` opt-in (full model IDs) and an existing
// "omit"; default everything else — absent, falsy, OR any non-canonical value — to