Skip to content

Warn or recover explicitly when config.json cannot be parsed #25

Closed

Description

@0disoft

Summary

loadConfig() currently returns the default config when ~/.opencodex/config.json cannot be parsed, without warning the user.

Current code:

try {
  const raw = readFileSync(CONFIG_PATH, "utf-8");
  return JSON.parse(raw) as OcxConfig;
} catch {
  return getDefaultConfig();
}

Why this matters

If the config file is truncated or manually edited into invalid JSON, opencodex silently starts with defaults. Later code paths can save the in-memory default config, making the user's provider setup appear to have disappeared.

This is especially confusing because provider credentials/configuration are user-managed state, and a parse failure likely indicates a recoverable local file problem rather than a real first-run state.

Possible directions

  • Print a warning that includes the config path and parse/read error.
  • Preserve the invalid file as config.json.invalid.<timestamp> before writing a replacement.
  • Refuse to overwrite the config until the user fixes or confirms recovery.
  • Add a small test around invalid JSON handling so this does not regress silently.

I am filing this separately because the desired recovery behavior is partly UX policy, not only implementation.

Activity

  1. lidge-jun commented on Jun 25, 2026

    @lidge-jun
    Owner

    Implemented on dev in 4d9d9f9 (fix: preserve invalid config before fallback), rebuilt from PR #26 with 0disoft / ZeroDi credited as co-author.

    Behavior now:

    • invalid config.json is copied to config.json.invalid-<timestamp> before fallback;
    • loadConfig() warns once per config path with the parse/schema reason;
    • malformed JSON and structurally invalid config fall back to defaults without silently overwriting the invalid original;
    • defaultProvider is validated when the provider map is non-empty;
    • existing current-dev contracts such as port: 0 and providers: {} remain loadable.

    Verification:

    • local bun run typecheck: pass
    • local bun run privacy:scan: pass
    • local bun test tests: 356 pass / 0 fail
    • GitHub Cross-platform CI run 28138885594: ubuntu-latest and windows-latest success

    Leaving the issue open until this dev fix is merged/released from the mainline.

  2. lidge-jun commented on Jun 25, 2026

    @lidge-jun
    Owner

    Resolved. The fix landed in 4d9d9f9 (fix: preserve invalid config before fallback), merged to main via PR #28, and shipped in v2.1.11+.

    Behavior now:

    • Invalid config.json is backed up to config.json.invalid-<timestamp> before fallback
    • loadConfig() warns once per config path with the backup location
    • Schema validation (configSchema) catches structural issues
    • defaultProvider is validated against configured providers

    README and docs-site configuration reference have been updated to describe this behavior.

  3. added a commit that references this issue on Sep 17, 2026
    9d95eaa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions