Fix JSON stream parsing in subprocess transport #22
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix: Robust JSON Stream Parsing for Large CLI Responses
Problem
The current subprocess transport implementation fails when the Claude CLI outputs large JSON responses that exceed stdout buffer boundaries. This results in
JSONDecodeError: Unterminated string
errors when processing substantial file contents or complex operations.Root Cause
The issue occurs due to stdout buffering at the OS level, not file content or special characters:
{"type":"user","content":"truncated...
json.loads()
fails on malformed partial JSONThis affects any operation that produces JSON responses larger than the stdout buffer size, regardless of file type or content.
Solution
This PR implements a robust
JSONStreamParser
class that:✅ Handles incomplete JSON streams with intelligent buffering
✅ Addresses PR #5 issue (newline-separated JSON objects)
✅ Addresses PR #16 issue ("Extra data" from concatenated JSON)
✅ Uses brace counting to detect complete JSON objects
✅ Provides fallback parsing for edge cases
✅ Maintains backward compatibility with existing functionality
Key Features
Reproduction Case
The following script reliably reproduces the issue by creating a large file that triggers stdout buffer splitting:
Error Output