fix(harness): bound CLI output capture (16MB/stream, env-tunable) - #903
Merged
Conversation
run_cli buffered a child's entire stdout/stderr unbounded — chunk list, joined str, and (in providers) the parsed JSONL event list all live at once, so a runaway stream is held in memory several times over, and N concurrent harness calls multiply that. This was one contributing layer in pr-af's OOM crash (Agent-Field/pr-af#65): 16 concurrent opencode calls, each buffering everything, on one container. Capture is now bounded per stream via AGENTFIELD_HARNESS_MAX_OUTPUT_BYTES (default 16MB, <=0 disables). Real provider streams are completion-boundary events (hundreds of KB), so normal runs stay byte-identical. On overflow the head (session/model info, first error) and the tail (final result + cumulative usage events) are kept around a truncation marker line; the marker and any partial seam line parse as invalid JSON, which parse_jsonl already skips, so extract_final_text and token extraction keep working on a truncated stream. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
Performance
✓ No regressions detected |
Contributor
📊 Coverage gateThresholds from
✅ Gate passedNo surface regressed past the allowed threshold and the aggregate stayed above the floor. |
Contributor
📐 Patch coverage gateThreshold: 80% on lines this PR touches vs
✅ Patch gate passedEvery surface whose lines were touched by this PR has patch coverage at or above the threshold. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
run_clibuffered a CLI child's entire stdout/stderr with no bound. By the time a provider is done with one call, the stream exists ~three times over (raw chunk list, joinedstr, parsed JSONL event list onRawResult.messages) — and concurrent harness calls multiply that. This was one contributing layer in the pr-af node OOM (Agent-Field/pr-af#65, fixed on the pr-af side by Agent-Field/pr-af#68): ~16 concurrent opencode subprocesses, each buffered in full, on one container.Change
run_cli, env-tunable viaAGENTFIELD_HARNESS_MAX_OUTPUT_BYTES(default 16 MB,<= 0disables). Real provider streams are completion-boundary JSONL events (hundreds of KB), so normal runs are byte-identical — the cap only bites pathological streams._BoundedChunksaccumulator keeps the head (25% — session/model info, first error) and the tail (75% — the final result and cumulative-usage events), splicing a truncation marker line at the seam. The marker and any partial seam line are invalid JSON, whichparse_jsonlalready skips, soextract_final_text/ token extraction keep working on truncated output. The newest chunk is always retained even if it alone exceeds the tail budget.Applies to every CLI-based provider that goes through
run_cli(opencode, codex, gemini, claude), not just opencode.Validation contract → tests (
tests/test_harness_cli.py)test_bounded_chunks_is_byte_identical_below_cap,test_run_cli_no_truncation_below_captest_bounded_chunks_keeps_head_and_tail_above_captest_run_cli_truncation_preserves_final_jsonl_eventstest_bounded_chunks_retains_newest_chunk_even_when_oversized<= 0disables the cap →test_bounded_chunks_cap_disabled_when_nonpositiveRan locally on Python 3.11:
ruff check .(pinned 0.15.22) clean, full./scripts/run_pytest.shgreen.🤖 Generated with Claude Code