← Knowledge base
Claude CLI Session Resume Gotchas
claude-cli-session-resume-gotchasv1updated 6h ago
Claude CLI Session Resume Gotchas
Learned while fixing ModForge's "No conversation found with session ID: …" error on launch (2026-06-10).
The trap
- In stream-json mode, the claude CLI tags every stdout line with a
session_id, including the very firstsystem/initevent of a brand-new session. - But the CLI only writes the conversation to disk once a turn actually happens. A session that was opened and never chatted in has a session ID that resolves to nothing.
- Passing that phantom ID to
claude --resume <id>fails withNo conversation found with session ID: <id>on stderr and the process exits.
Rules for harnesses that auto-resume
- Persist a session ID for later
--resumeonly from turn events (type∈assistant/user/result), never fromsystem/initorrate_limit_eventlines. - Treat
--resumefailure as recoverable: detect the stderr message, clear the stored ID, and respawn without the flag. IDs can also go stale when the CLI's history is pruned externally, so the recovery path is needed even with rule 1.
Where this lives in ModForge
src-tauri/src/claude_stream.rs— stdout thread persistslast_session_idonly on turn events; stderr thread detects the resume failure, NULLsmods.last_session_id, and emitsclaude:resume_failed:{mod_id}.src/components/StreamView.tsx— listens forclaude:resume_failed, callsclaudeStreamReset+claudeStreamOpen(no resume id) to recover seamlessly.
Related
- [[Claude AskUserQuestion in Headless Mode]] — another
--printstream-json quirk (the CLI auto-fails interactive tools) handled in the same files.
Usage windows
Learned while fixing ModForge's Claude usage banner (2026-07-08).
rate_limit_eventstream-json lines may only includestatus,resetsAt, andrateLimitType; mapping unknown non-allowed statuses to a fixed warning percent can make ModForge stale or wrong.- The exact same usage text shown by Claude's
/usagecommand is available without spending tokens via:claude --print --output-format stream-json --verbose "/usage" - Parse the assistant/result text lines (
Current session,Current week (all models), and current-model weekly such asCurrent week (Fable)) for exactN% usedand reset labels. Keeprate_limit_eventas a fallback and for live reset/status events.