{"record":{"id":"2299042bacaffefe","repo":"BigPizzaV3/CodexPlusPlus","slug":"must-be-a-json-object","errorCode":null,"errorMessage":"{} must be a JSON object","messagePattern":"(.+?) must be a JSON object","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codex-plus-core/src/codex_app_state.rs","lineNumber":182,"sourceCode":"            );\n        }\n    }\n}\n\nfn load_global_state(home: &Path) -> anyhow::Result<Option<Map<String, Value>>> {\n    let path = state_path(home);\n    if !path.exists() {\n        return Ok(None);\n    }\n    let text =\n        fs::read_to_string(&path).with_context(|| format!(\"failed to read {}\", path.display()))?;\n    let value: Value = serde_json::from_str(&text)\n        .with_context(|| format!(\"failed to parse {}\", path.display()))?;\n    value\n        .as_object()\n        .cloned()\n        .map(Some)\n        .ok_or_else(|| anyhow::anyhow!(\"{} must be a JSON object\", path.display()))\n}\n\nfn load_snapshot(home: &Path) -> anyhow::Result<Option<Map<String, Value>>> {\n    let path = snapshot_path(home);\n    if !path.exists() {\n        return Ok(None);\n    }\n    let text =\n        fs::read_to_string(&path).with_context(|| format!(\"failed to read {}\", path.display()))?;\n    let value: Value = serde_json::from_str(&text)\n        .with_context(|| format!(\"failed to parse {}\", path.display()))?;\n    let state = value\n        .get(\"state\")\n        .and_then(Value::as_object)\n        .or_else(|| value.as_object())\n        .cloned()\n        .unwrap_or_default();\n    Ok(Some(state))","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/BigPizzaV3/CodexPlusPlus/blob/b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6/crates/codex-plus-core/src/codex_app_state.rs#L164-L200","documentation":"`load_global_state` reads the codex global state JSON file, parses it with serde_json, and requires the top-level value to be a JSON object (`Map<String, Value>`). If the file parses as valid JSON but is an array, string, number, or boolean, this error is raised with the file path. It guards downstream code that indexes the state by key.","triggerScenarios":"Calling `load_global_state` (via `capture_app_state_snapshot` or `sync_app_state_after_provider_switch`) when the state file contains e.g. `[]`, `\"text\"`, `123`, or `null` instead of `{...}`.","commonSituations":"The file was hand-edited or overwritten by another tool writing a non-object JSON value, a truncation/partial write left a scalar, or a version change changed the state file schema.","solutions":["Inspect the file named in the message and restore it to a JSON object (e.g. `{}`)","Delete/regenerate the state file so codex rewrites a fresh object","Check for concurrent writers (two processes) clobbering the file and serialize access","Treat non-object content as empty state instead of erroring, if recovery is preferred"],"exampleFix":"// before\nvalue.as_object().cloned().map(Some)\n    .ok_or_else(|| anyhow::anyhow!(\"{} must be a JSON object\", path.display()))\n// after\nOk(value.as_object().cloned().or_else(|| {\n    tracing::warn!(\"{} was not a JSON object; resetting to empty state\", path.display());\n    Some(Map::new())\n}))","handlingStrategy":"validation","validationCode":"let text = std::fs::read_to_string(&path)?;\nlet v: serde_json::Value = serde_json::from_str(&text)?;\nif !v.is_object() {\n    eprintln!(\"{} is not a JSON object; resetting\", path.display());\n}","typeGuard":"// Rust narrowing\nfn as_state_object(v: &serde_json::Value) -> Option<&serde_json::Map<String, serde_json::Value>> {\n    v.as_object()\n}","tryCatchPattern":"match load_global_state(home) {\n    Err(e) if e.to_string().contains(\"must be a JSON object\") => {\n        // quarantine bad file and continue with empty state\n        std::fs::rename(&state_path, state_path.with_extension(\"bad\"))?;\n        Map::new()\n    }\n    Err(e) => return Err(e),\n    Ok(state) => state.unwrap_or_default(),\n}","preventionTips":["Never hand-edit the codex state file; write whole objects atomically","Use temp-file + rename for state writes to avoid partial/scalar content","Validate the JSON top-level type after any external tool touches the file"],"tags":["json","state-file","schema-validation","codex"],"backgroundTag":"schema-validation-failed","analyzedSha":"b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6","analyzedAt":"2026-09-19T23:35:21.129Z","contentChangedAt":"2026-09-19T23:35:21.129Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}