Skip to content

fix(acp): skip a stdout line that is not a JSON object - #18462

Merged
iamwhatever merged 1 commit into
kirodotdev:mainfrom
javenciu:fix/acp-reader-non-object-frames
Oct 9, 2026
Merged

iamwhatever merged 1 commit into
kirodotdev:mainfrom
javenciu:fix/acp-reader-non-object-frames

Conversation

@javenciu

@javenciu javenciu commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

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_loop parses each stdout line with json.loads under an except json.JSONDecodeError arm. Three ordinary lines raise something else: nesting past the decoder's ceiling raises RecursionError, an integer past the int-string digit limit raises a plain ValueError, and bytes that do not decode raise UnicodeDecodeError. Each reaches the loop's except Exception arm, which marks the runtime dead.

AcpClient._read_message has the same arm and then calls data.get(...) on whatever json.loads returned. A bare scalar, an array, null or a JSON-RPC batch array raises AttributeError out of _wait_for_response, so out of initialize, 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 AttributeError instead of being skipped like a non-JSON line.

Not a goal

  • Field-level parses inside an already accepted frame (acp/_dispatch.py tool-result and gate-envelope parses, acp/kas_wire.py, acp/types.py tool input). They catch ValueError and 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.
  • The oversize-frame drain and its budget (_drain_oversize_line); unchanged.
  • The two other ACP stdout parsers already use the shared helper (acp/harness/pi.py command readback, the client's tool-results line parser) and are unchanged.

What changed (motivation → approach → change)

  • Motivation: kiro_crew.json_line.parse_json_object_line already returns None for 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: the json.loads arm and the separate non-object check become one parse_json_object_line(line, errors="replace") call and one skip arm with the reader's existing debug log, gated by recording_allowed as before. Decoding with replacement matches what AcpClient._read_message already does, so an invalid byte inside a string costs that character, not the frame.
  • src/kiro_crew/acp/client.py, AcpClient._read_message: the json.loads arm becomes parse_json_object_line(text) with the existing debug log, gated by memory_mode as before. The two isinstance(data, dict) checks after the parse are removed because data is always an object there. The parsed frame is bound as data: 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.
  • Rejected: widening each except to (ValueError, RecursionError). It duplicates the helper's contract in two more places and still needs the non-object check.
  • Rejected: continuing the runtime loop from its except Exception arm. 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 returns None.
  • test/test_acp_client.py::TestAcpClientReadMessage::test_wait_for_response_reads_past_a_stray_scalar_line: a stray 123 before the initialize reply is skipped and the reply is returned.

Red on unchanged main (-n 0): 14 failed. Runtime cases fail on assert not rt._dead ("one stray stdout line marked the shared runtime dead"); the log shows Reader 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 with AttributeError: 'int' object has no attribute 'get' (and list, str, bool, NoneType), RecursionError and ValueError: Exceeds the limit (4300 digits).

Green on the branch (-n 0): test/test_acp_runtime.py test/test_acp_client.py 1227 passed.

Manual verification

None beyond the tests: both readers are exercised through their real parse paths with synthetic lines (a real asyncio.StreamReader for 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 bare json.loads under except json.JSONDecodeError, because RecursionError, the digit-limit ValueError and UnicodeDecodeError escape that arm.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

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.
@javenciu
javenciu requested review from a team and KiroCrewCaptain as code owners October 9, 2026 11:42
@javenciu
javenciu requested a review from dodgeblaster October 9, 2026 11:42
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Oct 9, 2026
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5, fork) — ⏭️ skipped

Revision e9dd2de9e30818a8e27842eff758a8cd33a2b500 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Advisory — does not block merge.

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — ✅ PASS

Premise-level review of e9dd2de9e30818a8e27842eff758a8cd33a2b500 via the fork AI-review pipeline — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

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 ships

Inventory (4 items) — 4 justified

Intent: 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.

  1. On the shared runtime, a line nested past the decoder's limit, an integer past the digit limit, or an undecodable line is now skipped. It no longer marks the runtime dead. — justified
  2. The per-process client now skips a line that is a bare scalar, an array, null, a batch array, deep nesting or a too-long integer. Before, it raised AttributeError out of initialize and out of turns. — justified
  3. On the runtime, a frame with one invalid UTF-8 byte inside a string is now delivered with U+FFFD in place of that byte, instead of being lost. — justified
  4. The acp-client spec paragraph now says such a line is skipped, not that it crashes the reader. — justified

[FIRST-PRINCIPLES-REVIEWED] e9dd2de

Description read

[DESCRIPTION-READ] 1a872d913996bb681e84382290f8366e5d35e0feaa09c2ba56607530946a26c6

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — ✅ PASS

Design-level review of e9dd2de9e30818a8e27842eff758a8cd33a2b500 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

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 .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — ✅ no blocking findings

Reviewed e9dd2de9e30818a8e27842eff758a8cd33a2b500 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] e9dd2de

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — ✅ no blocking findings

Reviewed e9dd2de9e30818a8e27842eff758a8cd33a2b500 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] e9dd2de

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Oct 9, 2026

@iamwhatever iamwhatever left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@iamwhatever
iamwhatever enabled auto-merge (squash) October 9, 2026 19:34
@iamwhatever
iamwhatever merged commit 5e22792 into kirodotdev:main Oct 9, 2026
103 checks passed
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 9, 2026
@javenciu
javenciu deleted the fix/acp-reader-non-object-frames branch October 11, 2026 04:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants