Crash with "unexpected end of JSON input" when try to resume a scan
OpenCodeReview Version
open-code-review v1.12.3 (71ed3a288d) darwin/arm64 built at: 2026-09-16T00:20:30Z
Operating System
macOS (Apple Silicon)
Installation Method
npm (global)
LLM Provider
Other OpenAI-compatible endpoint
Bug Description
When try to resume a scan, it returns an error. I set it up with the DeepSeek API.
Steps to Reproduce
- Start a scan
- Abort with control +c
- Resume with ocr scan --resume
Expected Behavior
Continue scanning where from the last valid point.
Logs / Error Output
Error: load resume session: parse resume session "4493c9f6-7eee-4b0f-8e0e-9b278ecabb53": unexpected end of JSON input (run 'ocr session list' to see available sessions)
AI/LLM Disclosure
- [x] I didn't use AI/LLM to assist or generate the content above.
Additional Context
No response
Thanks for the report — this is a real bug, and the reproduction is spot on. Confirmed after tracing the code.
Root cause: session records are written through a bufio.Writer (internal/session/persist.go), which flushes on 4 KB byte boundaries, not line boundaries. The checkpoint records (review_item_done etc.) flush after every write, but the verbose llm_request / llm_response / tool_call records do not. So when you Ctrl+C, the file's final line is often a half-written record with no trailing newline (a torn write). Because writing only ever appends, only the last line can be truncated — every earlier line is intact.
On resume, scan uses the strict loader (LoadResumeState), which json.Unmarshals every line and treats any parse failure as fatal — hence unexpected end of JSON input. The irony is that the truncated line is almost always a transcript record (llm_* / tool_call) that resume ignores anyway; scan only replays review_item_* checkpoints. So one torn logging line throws away all the completed work.
Fix direction: in loadResumeState, tolerate a single unterminated final line — a record not ended by \n was never fully written, so it carries no committed state and is safe to drop for both scan and review. (Optionally, also flush the writer on interrupt to make torn writes less likely, but tolerating the torn final line is the actual fix.)
This is a well-scoped fix and a good one to pick up if anyone in the community would like to. A regression test should build a JSONL whose final line is truncated and assert that scan resume skips it and recovers the completed items.
If you take it on, please remember the project's AI-assistance disclosure guidelines in AGENTS.md.
Hi @lizhengfeng101, I’d like to take this. I’ll keep the fix scoped to loadResumeState: tolerate only an unterminated final JSONL record while preserving strict parsing for completed lines, and add a regression test for resuming with a truncated final record.
I’d like to take this.