Skip to content

fix: prevent profile doctor from silently wiping Claude state on bad JSON#135

Open
xzq-xu wants to merge 1 commit intoHKUDS:mainfrom
xzq-xu:fix/profile-doctor-data-loss
Open

fix: prevent profile doctor from silently wiping Claude state on bad JSON#135
xzq-xu wants to merge 1 commit intoHKUDS:mainfrom
xzq-xu:fix/profile-doctor-data-loss

Conversation

@xzq-xu
Copy link
Copy Markdown
Contributor

@xzq-xu xzq-xu commented Apr 13, 2026

Summary

  • Data loss bug: clawteam profile doctor claude silently destroyed all existing Claude state (~/.claude.json) when the file contained invalid JSON or a non-dict value. The bare except Exception: data = {} handler replaced the entire file with {"hasCompletedOnboarding": true}, discarding API keys, preferences, and other saved state.
  • Fix: Corrupted/non-dict state files are now backed up to .claude.json.bak before being replaced, and the user receives a clear warning. Valid dict state is preserved and merged as before.
  • Safety: The write itself now uses atomic_write_text from clawteam.fileutil to prevent partial writes.

Changes

File What changed
clawteam/cli/commands.py Replace bare except Exception: data = {} with explicit json.JSONDecodeError handling, backup-before-wipe, and atomic write
tests/test_profiles.py Add 2 tests: corrupted JSON backup + non-dict JSON backup

Test plan

  • uv run pytest tests/test_profiles.py -v — 10/10 passed
  • uv run pytest -q — full suite 547 passed, 0 failures
  • uv run ruff check — all checks passed

Made with Cursor

…JSON

When ~/.claude.json existed but contained invalid JSON (or a non-dict
value), the except-all handler replaced the entire file contents with
an empty dict — silently destroying all existing Claude state (API
keys, preferences, onboarding flags, etc.).

Now, corrupted state files are backed up to .claude.json.bak before
being replaced, and the user is warned. Valid dict state is preserved
and merged as before. The write itself uses atomic_write_text to
prevent partial writes.

Made-with: Cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant