fix(cli): use tryCatch for JSON.parse in loadPreferredModel (#2782)

tryCatchIf(isFileError) only catches filesystem errors (ENOENT, EACCES),
but JSON.parse throws SyntaxError on corrupted preferences.json. This
was the same bug fixed in 16a2f180 across 4 files, but orchestrate.ts
was missed. A corrupted ~/.spawn/preferences.json would crash the CLI
instead of gracefully falling back to no preferred model.

Agent: code-health

Co-authored-by: B <6723574+louisgv@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
A 2026-03-18 20:15:17 -07:00 committed by GitHub
parent b0ecb3a139
commit 15a62a9ad0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 3 additions and 3 deletions

View file

@ -15,7 +15,7 @@ import { tryTarballInstall } from "./agent-tarball";
import { generateEnvConfig } from "./agents";
import { getOrPromptApiKey } from "./oauth";
import { getSpawnPreferencesPath } from "./paths";
import { asyncTryCatch, asyncTryCatchIf, isFileError, isOperationalError, tryCatchIf } from "./result.js";
import { asyncTryCatch, asyncTryCatchIf, isOperationalError, tryCatch } from "./result.js";
import { isWindows } from "./shell";
import { startSshTunnel } from "./ssh";
import { ensureSshKeys, getSshKeyOpts } from "./ssh-keys";
@ -94,7 +94,7 @@ const PreferencesSchema = v.object({
});
function loadPreferredModel(agentName: string): string | null {
const result = tryCatchIf(isFileError, () => {
const result = tryCatch(() => {
const raw = JSON.parse(readFileSync(getSpawnPreferencesPath(), "utf-8"));
const parsed = v.safeParse(PreferencesSchema, raw);
if (!parsed.success) {