← Back to PRs

#11893: feat(doctor): add session JSON validation to detect corruption

by isorabins open 2026-02-08 13:55 View on GitHub →
commands stale
## Problem A corrupt `sessions.json` file can cause cascading failures: 1. Gateway tries to load invalid JSON 2. API calls fail with errors 3. Provider cooldowns trigger 4. Agent goes down for hours until manual intervention This happened to me (running OpenClaw via Alex agent) and took hours to diagnose. ## Solution Add JSON validation to `openclaw doctor`: 1. **`validateSessionsJson()`** - Checks `sessions.json` is valid JSON before loading 2. **`countCorruptJsonlLines()`** - Detects malformed lines in session JSONL files 3. **Repair option** - If sessions.json is corrupt, offer to reset it to `{}` 4. **Warning** - Alert if main session transcript has corrupt JSON lines ## Testing Tested by intentionally corrupting `sessions.json` and running `openclaw doctor`. ## Related This check could also be added to the default `AGENTS.md` heartbeat recommendations so agents self-monitor, but that's a separate PR. <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR extends `openclaw doctor` state-integrity checks by: - Validating `sessions.json` parses as JSON and offering an interactive repair (reset to `{}`) when it’s corrupt. - Scanning the main session transcript JSONL file and warning when some lines aren’t valid JSON. These checks run as part of `noteStateIntegrity()` and aim to detect/mitigate corrupted session state before it causes broader gateway/API failures. <h3>Confidence Score: 3/5</h3> - This PR is close to safe, but needs a guard to avoid doctor crashing on still-corrupt sessions.json. - The added validation/repair logic is straightforward, but `loadSessionStore()` is still called even when validation fails and the user declines or repair fails, which can reintroduce the original failure mode and prevent the doctor command from completing. - src/commands/doctor-state-integrity.ts <!-- greptile_other_comments_section --> <sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub> <!-- /greptile_comment -->

Most Similar PRs