open-code-review icon indicating copy to clipboard operation
open-code-review copied to clipboard

Crash with "unexpected end of JSON input" when try to resume a scan

Open onaralili opened this issue 2 months ago • 3 comments

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

  1. Start a scan
  2. Abort with control +c
  3. 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

onaralili avatar Sep 16 '26 08:09 onaralili

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.

lizhengfeng101 avatar Sep 16 '26 09:09 lizhengfeng101

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.

hyy321 avatar Sep 16 '26 09:09 hyy321

I’d like to take this.

jimpablo avatar Sep 16 '26 10:09 jimpablo