[BUG] /insights: session-meta JSON files corrupted by non-atomic writes on macOS

Resolved 💬 4 comments Opened Mar 16, 2026 by baekenough Closed Apr 13, 2026

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report (please file separate reports for different bugs)
  • [x] I am using the latest version of Claude Code

What's Wrong?

/insights generates session-meta JSON files in ~/.claude/usage-data/session-meta/ that become corrupted when sessions terminate abnormally (Ctrl+C, process kill, crash). Out of ~2,431 session-meta files, 11 were found corrupted in two distinct patterns:

Pattern A — Truncated JSON (6 files): JSON is cut off mid-stream (e.g., mid-key like "mess or mid-value like "2026-), missing closing brackets. This happens when the write is interrupted before completion.

Pattern B — Trailing NUL bytes (5 files): Valid JSON followed by NUL (\x00) padding bytes, causing "Extra data" parse errors. This suggests the file was pre-allocated but not fully written or truncated.

Both patterns indicate the session-meta files are written directly (non-atomic) — if the process exits during the write, the file is left in a corrupt state.

Error Examples

Warning: Failed to parse 74b35b34-...: Invalid control character at: line 35 column 8 (char 1001)
Warning: Failed to parse 36a8a8f9-...: Expecting ',' delimiter: line 42 column 3 (char 1127)
Warning: Failed to parse 3594e1ac-...: Extra data: line 39 column 2 (char 1055)

What Should Happen?

Session-meta files should be written atomically (write to temp file → fsync → rename) to prevent corruption. This is the same root cause as #29250 (non-atomic writes to .claude.json) but affecting a different file.

Additionally, /insights should gracefully handle corrupted session-meta files instead of showing warnings — skip them silently or attempt repair.

Environment

  • Platform: macOS (Darwin 25.3.0)
  • Version: Latest Claude Code
  • Affected path: ~/.claude/usage-data/session-meta/*.json
  • Scale: 11/2,431 files corrupted (~0.45%)

Related Issues

  • #29250 — Non-atomic writes to .claude.json (same root cause, different file)
  • #26358 — Session-meta cache not invalidated (different symptom, same subsystem)

View original on GitHub ↗

This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗