Repository navigation
fix(acp): skip a stdout line that is not a JSON object - #18462
Conversation
One stdout line from an ACP backend could end far more than itself. AcpRuntime._reader_loop parsed each line with json.loads under an except json.JSONDecodeError arm. Nesting past the decoder's ceiling raises RecursionError, an integer past the int-string digit limit a plain ValueError, and undecodable bytes UnicodeDecodeError. Each one reached the loop's crash arm, which marks the runtime dead and ends every multiplexed session on it. AcpClient._read_message had the same arm and then called data.get() on whatever json.loads returned, so a bare scalar, array or null line raised AttributeError out of the initialize wait and out of turns. Both readers now parse each line with kiro_crew.json_line.parse_json_object_line and skip, with the reader's existing debug log, any line that is not a JSON object. The runtime decodes with replacement, as the client already does, so an invalid byte inside a string costs that character, not the frame. The client's two isinstance(data, dict) checks after the parse are gone because data is always an object there. The acp-client spec paragraph on the oversize drain describes the skip in the parse step.
UX Review (Opus 5.5, fork) — ⏭️ skippedRevision |
First Principles Review (Opus 5.5, fork) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Check one thing before merge: the runtime reader now decodes invalid bytes with replacement, so a frame containing one is delivered with U+FFFD instead of being dropped. What this change shipsInventory (4 items) — 4 justifiedIntent: one bad stdout line from an ACP backend should cost only that line, not the shared runtime and not the caller's handshake or turn. This is a FIX.
[FIRST-PRINCIPLES-REVIEWED] e9dd2de Description read[DESCRIPTION-READ] 1a872d913996bb681e84382290f8366e5d35e0feaa09c2ba56607530946a26c6 That sha256 names the PR description this verdict read. Recompute it with |
Design Review (Opus 5.5, fork) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing to check. [DESIGN-REVIEWED] e9dd2de Description read[DESCRIPTION-READ] fdf8b7c98556f1cc9e10a5418f27f4a91f57701becf79a3a5849e5a1fab5cdb9 That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
GPT 6.1 Review (fork) — ✅ no blocking findingsReviewed Review detailsNo findings. |
Opus 5.5 Review (fork) — ✅ no blocking findingsReviewed |
iamwhatever
left a comment
There was a problem hiding this comment.
This fixes two ACP stdout readers that let one bad line do too much damage. In AcpRuntime._reader_loop, deep nesting (RecursionError), an integer past the digit limit (ValueError) or undecodable bytes got past the except json.JSONDecodeError arm and marked the shared runtime dead. In AcpClient._read_message, a bare scalar or array raised AttributeError out of initialize and out of turns. The root cause is fixed: both readers now call the existing kiro_crew.json_line.parse_json_object_line, which already returns None for each of those cases (I read the helper at head), and the loop's terminal except Exception arm stays in place for real bugs. I checked that the client's later isinstance(data, dict) checks are only dropped where data is now always a dict, and that the runtime's switch to replacement decoding matches what the client already did. The new parametrized tests reach the real parse paths (a real StreamReader for the runtime) and would fail on main, as the body's red run shows. All checks are green, base is main, and all five AI lanes PASS with no findings. — crew-pr-reviewer
Problem / Motivation
Goal: One stdout line from an ACP backend that is not a JSON object costs that line, never the shared runtime or the caller's handshake or turn.
AcpRuntime._reader_loopparses each stdout line withjson.loadsunder anexcept json.JSONDecodeErrorarm. Three ordinary lines raise something else: nesting past the decoder's ceiling raisesRecursionError, an integer past the int-string digit limit raises a plainValueError, and bytes that do not decode raiseUnicodeDecodeError. Each reaches the loop'sexcept Exceptionarm, which marks the runtime dead.AcpClient._read_messagehas the same arm and then callsdata.get(...)on whateverjson.loadsreturned. A bare scalar, an array,nullor a JSON-RPC batch array raisesAttributeErrorout of_wait_for_response, so out ofinitialize, and out of turns.Why it matters
On the shared runtime, one such line ends every multiplexed session on it mid-turn. On the per-process client, one such line fails that session's handshake or turn with an
AttributeErrorinstead of being skipped like a non-JSON line.Not a goal
acp/_dispatch.pytool-result and gate-envelope parses,acp/kas_wire.py,acp/types.pytool input). They catchValueErrorand check for a dict, so non-object and long-integer text is already handled there; only nesting past the decoder's ceiling escapes them, and those parses are not stdout line readers._drain_oversize_line); unchanged.acp/harness/pi.pycommand readback, the client's tool-results line parser) and are unchanged.What changed (motivation → approach → change)
kiro_crew.json_line.parse_json_object_linealready returnsNonefor every unusable line (not UTF-8 under strict decoding, blank, not JSON, not an object, past the digit limit, past the nesting ceiling), and the other line readers use it. The two ACP readers did not.src/kiro_crew/acp/runtime.py,AcpRuntime._reader_loop: thejson.loadsarm and the separate non-object check become oneparse_json_object_line(line, errors="replace")call and one skip arm with the reader's existing debug log, gated byrecording_allowedas before. Decoding with replacement matches whatAcpClient._read_messagealready does, so an invalid byte inside a string costs that character, not the frame.src/kiro_crew/acp/client.py,AcpClient._read_message: thejson.loadsarm becomesparse_json_object_line(text)with the existing debug log, gated bymemory_modeas before. The twoisinstance(data, dict)checks after the parse are removed becausedatais always an object there. The parsed frame is bound asdata: dict[str, Any]so mypy keeps the dict type past the projection call.docs/system-specs/modules/acp-client.md: the paragraph on the oversize drain describes the parse step skipping a line that is not a JSON object, in place of the crash it described.exceptto(ValueError, RecursionError). It duplicates the helper's contract in two more places and still needs the non-object check.except Exceptionarm. That arm is the reader's net for real bugs and must stay terminal.Backwards compatibility
Compatible: every line that parses as a JSON object is handled exactly as before; only lines that used to crash a reader or raise out of it are now skipped.
Removes nothing: no config key, API, frame shape or public function changes.
Tests
test/test_acp_runtime.py::test_line_json_loads_rejects_without_decode_error_does_not_crash_reader(4 cases: nesting past the ceiling at about 60 KB so it reaches the parser, a 5,000-digit integer, an invalid UTF-8 byte, a UTF-16 BOM): the reader stays alive and routes the next frame.test/test_acp_runtime.py::test_frame_with_an_invalid_utf8_byte_in_a_string_is_still_routed: the frame is delivered with U+FFFD.test/test_acp_client.py::TestAcpClientReadMessage::test_read_line_that_is_not_a_json_object_skipped(8 cases: int, string, null, bool, empty array, batch array, nested, 5,000 digits): each returnsNone.test/test_acp_client.py::TestAcpClientReadMessage::test_wait_for_response_reads_past_a_stray_scalar_line: a stray123before theinitializereply is skipped and the reply is returned.Red on unchanged main (
-n 0):14 failed. Runtime cases fail onassert not rt._dead("one stray stdout line marked the shared runtime dead"); the log showsReader loop crashed: maximum recursion depth exceeded while decoding a JSON array,Exceeds the limit (4300 digits),'utf-8' codec can't decode byte 0x80. Client cases fail withAttributeError: 'int' object has no attribute 'get'(andlist,str,bool,NoneType),RecursionErrorandValueError: Exceeds the limit (4300 digits).Green on the branch (
-n 0):test/test_acp_runtime.py test/test_acp_client.py1227 passed.Manual verification
None beyond the tests: both readers are exercised through their real parse paths with synthetic lines (a real
asyncio.StreamReaderfor the runtime).Related Issues
Completes the ACP reader half that the stray-line hardening of the other JSON line readers (#16280) listed under its Not a goal.
Overlap: open PR #18285 changes
_read_message's signature and decorates_wait_for_response, but not the parse step this change edits. Open PRs #9314, #15325 and #15351 edit_reader_loop's routing, not its parse step.Pattern harvest
Rule candidate: review-prompt: a line reader of child-process output parses through
kiro_crew.json_line.parse_json_object_line, never a barejson.loadsunderexcept json.JSONDecodeError, becauseRecursionError, the digit-limitValueErrorandUnicodeDecodeErrorescape that arm.Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)