From 93fbe26adbbcf15fec0b2ddd395478e9100de41e Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Fri, 20 Mar 2026 10:10:57 -0700 Subject: [PATCH] fix(config): tighten json and json5 parsing paths (#51153) --- CHANGELOG.md | 1 + src/agents/subagent-depth.test.ts | 27 ++++++++++++++++++++++++++ src/agents/subagent-depth.ts | 10 +++++++++- src/cli/config-cli.test.ts | 11 +++++++++++ src/cli/config-cli.ts | 8 ++++---- src/config/paths.ts | 2 +- src/cron/store.test.ts | 32 +++++++++++++++++++++++++++++++ src/cron/store.ts | 10 +++++++++- ui/src/ui/views/config.ts | 4 ++-- 9 files changed, 96 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b95fe247361..8e33a2d82a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,7 @@ Docs: https://docs.openclaw.ai ### Fixes +- CLI/config: make `config set --strict-json` enforce real JSON, prefer `JSON.parse` with JSON5 fallback for machine-written cron/subagent stores, and relabel raw config surfaces as `JSON/JSON5` to match actual compatibility. Related: #48415, #43127, #14529, #21332. Thanks @adhitShet and @vincentkoc. - CLI/Ollama onboarding: keep the interactive model picker for explicit `openclaw onboard --auth-choice ollama` runs so setup still selects a default model without reintroducing pre-picker auto-pulls. (#49249) Thanks @BruceMacD. - Plugins/bundler TDZ: fix `RESERVED_COMMANDS` temporal dead zone error that prevented device-pair, phone-control, and talk-voice plugins from registering when the bundler placed the commands module after call sites in the same output chunk. Thanks @BunsDev. - Plugins/imports: fix stale googlechat runtime-api import paths and signal SDK circular re-exports broken by recent plugin-sdk refactors. Thanks @BunsDev. diff --git a/src/agents/subagent-depth.test.ts b/src/agents/subagent-depth.test.ts index 5d9427b7818..2c62432a692 100644 --- a/src/agents/subagent-depth.test.ts +++ b/src/agents/subagent-depth.test.ts @@ -76,6 +76,33 @@ describe("getSubagentDepthFromSessionStore", () => { expect(depth).toBe(2); }); + it("accepts JSON5 syntax in the on-disk depth store for backward compatibility", () => { + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-subagent-depth-json5-")); + const storeTemplate = path.join(tmpDir, "sessions-{agentId}.json"); + const storePath = storeTemplate.replaceAll("{agentId}", "main"); + fs.writeFileSync( + storePath, + `{ + // hand-edited legacy store + "agent:main:subagent:flat": { + sessionId: "subagent-flat", + spawnDepth: 2, + }, + }`, + "utf-8", + ); + + const depth = getSubagentDepthFromSessionStore("subagent:flat", { + cfg: { + session: { + store: storeTemplate, + }, + }, + }); + + expect(depth).toBe(2); + }); + it("falls back to session-key segment counting when metadata is missing", () => { const key = "agent:main:subagent:flat"; const depth = getSubagentDepthFromSessionStore(key, { diff --git a/src/agents/subagent-depth.ts b/src/agents/subagent-depth.ts index 8b62539ac45..9ad03bbbc91 100644 --- a/src/agents/subagent-depth.ts +++ b/src/agents/subagent-depth.ts @@ -11,6 +11,14 @@ type SessionDepthEntry = { spawnedBy?: unknown; }; +function parseSessionDepthStore(raw: string): unknown { + try { + return JSON.parse(raw); + } catch { + return JSON5.parse(raw); + } +} + function normalizeSpawnDepth(value: unknown): number | undefined { if (typeof value === "number") { return Number.isInteger(value) && value >= 0 ? value : undefined; @@ -37,7 +45,7 @@ function normalizeSessionKey(value: unknown): string | undefined { function readSessionStore(storePath: string): Record { try { const raw = fs.readFileSync(storePath, "utf-8"); - const parsed = JSON5.parse(raw); + const parsed = parseSessionDepthStore(raw); if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) { return parsed as Record; } diff --git a/src/cli/config-cli.test.ts b/src/cli/config-cli.test.ts index d30a476004d..6e9cc07bf7e 100644 --- a/src/cli/config-cli.test.ts +++ b/src/cli/config-cli.test.ts @@ -442,6 +442,15 @@ describe("config cli", () => { expect(mockReadConfigFileSnapshot).not.toHaveBeenCalled(); }); + it("rejects JSON5-only object syntax when strict parsing is enabled", async () => { + await expect( + runConfigCommand(["config", "set", "gateway.auth", "{mode:'token'}", "--strict-json"]), + ).rejects.toThrow("__exit__:1"); + + expect(mockWriteConfigFile).not.toHaveBeenCalled(); + expect(mockReadConfigFileSnapshot).not.toHaveBeenCalled(); + }); + it("accepts --strict-json with batch mode and applies batch payload", async () => { const resolved: OpenClawConfig = { gateway: { port: 18789 } }; setSnapshot(resolved, resolved); @@ -470,6 +479,8 @@ describe("config cli", () => { expect(helpText).toContain("--strict-json"); expect(helpText).toContain("--json"); expect(helpText).toContain("Legacy alias for --strict-json"); + expect(helpText).toContain("Value (JSON/JSON5 or raw string)"); + expect(helpText).toContain("Strict JSON parsing (error instead of"); expect(helpText).toContain("--ref-provider"); expect(helpText).toContain("--provider-source"); expect(helpText).toContain("--batch-json"); diff --git a/src/cli/config-cli.ts b/src/cli/config-cli.ts index 604e27666c9..e7a94ae99ab 100644 --- a/src/cli/config-cli.ts +++ b/src/cli/config-cli.ts @@ -159,9 +159,9 @@ function parseValue(raw: string, opts: ConfigSetParseOpts): unknown { const trimmed = raw.trim(); if (opts.strictJson) { try { - return JSON5.parse(trimmed); + return JSON.parse(trimmed); } catch (err) { - throw new Error(`Failed to parse JSON5 value: ${String(err)}`, { cause: err }); + throw new Error(`Failed to parse JSON value: ${String(err)}`, { cause: err }); } } @@ -1280,8 +1280,8 @@ export function registerConfigCli(program: Command) { .command("set") .description(CONFIG_SET_DESCRIPTION) .argument("[path]", "Config path (dot or bracket notation)") - .argument("[value]", "Value (JSON5 or raw string)") - .option("--strict-json", "Strict JSON5 parsing (error instead of raw string fallback)", false) + .argument("[value]", "Value (JSON/JSON5 or raw string)") + .option("--strict-json", "Strict JSON parsing (error instead of raw string fallback)", false) .option("--json", "Legacy alias for --strict-json", false) .option( "--dry-run", diff --git a/src/config/paths.ts b/src/config/paths.ts index 84c27749bcf..a35a1a3d03d 100644 --- a/src/config/paths.ts +++ b/src/config/paths.ts @@ -99,7 +99,7 @@ function resolveUserPath( export const STATE_DIR = resolveStateDir(); /** - * Config file path (JSON5). + * Config file path (JSON or JSON5). * Can be overridden via OPENCLAW_CONFIG_PATH. * Default: ~/.openclaw/openclaw.json (or $OPENCLAW_STATE_DIR/openclaw.json) */ diff --git a/src/cron/store.test.ts b/src/cron/store.test.ts index f511636fb85..405d04cbe60 100644 --- a/src/cron/store.test.ts +++ b/src/cron/store.test.ts @@ -56,6 +56,38 @@ describe("cron store", () => { await expect(loadCronStore(store.storePath)).rejects.toThrow(/Failed to parse cron store/i); }); + it("accepts JSON5 syntax when loading an existing cron store", async () => { + const store = await makeStorePath(); + await fs.mkdir(path.dirname(store.storePath), { recursive: true }); + await fs.writeFile( + store.storePath, + `{ + // hand-edited legacy store + version: 1, + jobs: [ + { + id: 'job-1', + name: 'Job 1', + enabled: true, + createdAtMs: 1, + updatedAtMs: 1, + schedule: { kind: 'every', everyMs: 60000 }, + sessionTarget: 'main', + wakeMode: 'next-heartbeat', + payload: { kind: 'systemEvent', text: 'tick-job-1' }, + state: {}, + }, + ], + }`, + "utf-8", + ); + + await expect(loadCronStore(store.storePath)).resolves.toMatchObject({ + version: 1, + jobs: [{ id: "job-1", enabled: true }], + }); + }); + it("does not create a backup file when saving unchanged content", async () => { const store = await makeStorePath(); const payload = makeStore("job-1", true); diff --git a/src/cron/store.ts b/src/cron/store.ts index 8e8f0440f35..551a1f3cb64 100644 --- a/src/cron/store.ts +++ b/src/cron/store.ts @@ -10,6 +10,14 @@ export const DEFAULT_CRON_DIR = path.join(CONFIG_DIR, "cron"); export const DEFAULT_CRON_STORE_PATH = path.join(DEFAULT_CRON_DIR, "jobs.json"); const serializedStoreCache = new Map(); +function parseCronStoreRaw(raw: string): unknown { + try { + return JSON.parse(raw); + } catch { + return JSON5.parse(raw); + } +} + export function resolveCronStorePath(storePath?: string) { if (storePath?.trim()) { const raw = storePath.trim(); @@ -26,7 +34,7 @@ export async function loadCronStore(storePath: string): Promise { const raw = await fs.promises.readFile(storePath, "utf-8"); let parsed: unknown; try { - parsed = JSON5.parse(raw); + parsed = parseCronStoreRaw(raw); } catch (err) { throw new Error(`Failed to parse cron store at ${storePath}: ${String(err)}`, { cause: err, diff --git a/ui/src/ui/views/config.ts b/ui/src/ui/views/config.ts index 7c1121e6bb8..6e3db2c6a67 100644 --- a/ui/src/ui/views/config.ts +++ b/ui/src/ui/views/config.ts @@ -1062,7 +1062,7 @@ export function renderConfig(props: ConfigProps) { }
- Raw JSON5 + Raw config (JSON/JSON5) ${ sensitiveCount > 0 ? html` @@ -1087,7 +1087,7 @@ export function renderConfig(props: ConfigProps) {