Repository navigation
fix(curl): JSON passthrough + IsTerminal gate to prevent invalid JSON output - #1602
Merged
Merged
Conversation
… output `rtk curl` was unconditionally truncating any response >=500 bytes when a tee hint was available, inserting the literal `... (N bytes total)` marker mid-stream. For JSON bodies this produces invalid, unparseable output and silently breaks every downstream `jq`, `python -m json.tool`, or agent pipeline that consumes `rtk curl`. Two changes in `filter_curl_output`: 1. JSON heuristic passthrough — if the trimmed body starts/ends with matching JSON brackets, return it unchanged. The body is already buffered in memory so the check is essentially free. 2. `is_terminal()` gate — only truncate non-JSON output when stdout is a TTY. Pipes and shell redirects (`> file`, `| jq`) now receive the full body, matching what `curl -o file` already does and closing the silent-data-loss path described in #1282. The tee file is still written for inspection, but the hint line is suppressed in the passthrough cases so it never leaks into pipes. Tests: existing 5 cases updated to pass `is_tty=true` (preserve TTY-mode assertions); 4 new cases cover JSON object/array passthrough at >500 bytes and pipe-mode (non-TTY) full-body delivery. Verified end-to-end against the public repro from the issue: `rtk curl -s https://jsonplaceholder.typicode.com/posts | python3 -c 'import sys,json; print(len(json.load(sys.stdin)))'` now returns 100. Closes #1536 Refs #1282 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Collaborator
Author
📊 Automated PR Analysis
SummaryFixes JSON output truncation in Review Checklist
Analyzed automatically by wshm · This is an automated analysis, not a human review. |
3 tasks done
…y alloc Follow-up to the initial #1536 fix after a deeper review pass. - force_tee_hint() is no longer called on the passthrough path. Previously every non-TTY pipe invocation (curl | jq, curl | python -m json.tool — the exact scenarios this PR fixes) was still writing a tee log file as a side effect, which is wasteful: the tee file's purpose is to give the LLM a recovery path when output is truncated, and we are no longer truncating in those cases. - looks_like_json now also matches bare top-level JSON strings (e.g. /api/token endpoints returning a long quoted token). Previously a >500-byte bare JSON string on a TTY would still get truncated mid-stream and produce invalid output. - FilterResult.content is now Cow<'_, str> so the passthrough path no longer copies the whole body. For multi-MB curl responses piped through rtk this eliminates a full-size allocation. - Exit code from curl is propagated via Ok(result.exit_code) instead of the previously hardcoded Ok(0). On the success path the value is 0 anyway, but the inconsistency with the early-exit branch above made the intent unclear. Tests: 9 -> 11 (added bare-JSON-string passthrough and a Cow::Borrowed assertion verifying the passthrough paths don't allocate). cargo test --all: 1693 passed, 6 ignored. Verified end-to-end against jsonplaceholder.typicode.com/posts (~27 KB, 100 entries, parseable) and a randomly-generated 100-entry / 52 KB / 1450-line JSON served from a local http.server (rtk output byte-identical to source modulo the trailing newline added by println!). Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
7 tasks done
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
Fixes #1536 —
rtk curlwas truncating any response ≥500 bytes mid-stream, inserting... (N bytes total)directly inside JSON string values and producing invalid, unparseable output. This silently broke every agent pipeline that pipedrtk curltojq/python -m json.tool/ similar, and was reproducible on the issue's public repro (https://jsonplaceholder.typicode.com/posts).Also closes the related silent-data-loss path tracked in #1282 —
curl > fileandcurl | toolwere truncated even thoughcurl -o fileworked, because the rewrite layer captured curl's stdout regardless ofisatty(stdout).Changes
src/cmds/cloud/curl_cmd.rsuse std::io::IsTerminal;andlet is_tty = std::io::stdout().is_terminal();inrun()filter_curl_output(raw, is_tty)now skips truncation when:{}or[]) — matters for downstream parsers[full output: …]hint is preserved for human TTY mode on non-JSON bodiesTests
is_tty=true(preserve their TTY-mode assertions)test_filter_curl_large_json_object_passthrough— TTY + JSON object > 500 B → no truncationtest_filter_curl_large_json_array_passthrough— TTY + JSON array > 500 B → no truncationtest_filter_curl_pipe_no_truncation_for_non_json— pipe + plain text 1000 B → full bodytest_filter_curl_pipe_no_truncation_for_json— pipe + JSON > 500 B → full body, no hintcargo fmt --all && cargo clippy --all-targets && cargo test --all→ 1691 passed, 6 ignored.Verification
End-to-end repro from the issue, before vs after:
Notes on related PRs
curl_cmd.rsshape it targeted has been refactored away). It also bundles an unrelatedextract_schemaquoting fix injson_cmd.rs. I'll comment on fix(curl): preserve valid JSON output for piping (#1015) #1016 to either rebase itsjson_cmd.rspart as a separate scoped PR or close it — happy to follow whatever you prefer.Test plan
cargo fmt --all && cargo clippy --all-targets && cargo test --allgreenhttps://jsonplaceholder.typicode.com/postsreturns parseable JSONrtk curlto a non-JSON endpoint in a TTY still shows the truncated form + tee hint🤖 Generated with Claude Code