Skip to content

fix(curl): JSON passthrough + IsTerminal gate to prevent invalid JSON output - #1602

Merged
KuSh merged 2 commits into
developfrom
fix/curl-json-passthrough-1536
May 2, 2026
Merged

KuSh merged 2 commits into
developfrom
fix/curl-json-passthrough-1536

Conversation

@pszymkowiak

Copy link
Copy Markdown
Collaborator

Summary

Fixes #1536 — rtk curl was 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 piped rtk curl to jq / 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 > file and curl | tool were truncated even though curl -o file worked, because the rewrite layer captured curl's stdout regardless of isatty(stdout).

Changes

src/cmds/cloud/curl_cmd.rs

  • Added use std::io::IsTerminal; and let is_tty = std::io::stdout().is_terminal(); in run()
  • filter_curl_output(raw, is_tty) now skips truncation when:
    • body looks like a JSON document (starts/ends with {} or []) — matters for downstream parsers
    • stdout is not a terminal — matters for pipes / shell redirects
  • Tee file is still written in those cases (preserves observability), but the hint line is suppressed so it never leaks into pipes
  • The legacy 500-byte truncation + [full output: …] hint is preserved for human TTY mode on non-JSON bodies

Tests

  • Existing 5 unit tests updated to pass is_tty=true (preserve their TTY-mode assertions)
  • 4 new unit tests:
    • test_filter_curl_large_json_object_passthrough — TTY + JSON object > 500 B → no truncation
    • test_filter_curl_large_json_array_passthrough — TTY + JSON array > 500 B → no truncation
    • test_filter_curl_pipe_no_truncation_for_non_json — pipe + plain text 1000 B → full body
    • test_filter_curl_pipe_no_truncation_for_json — pipe + JSON > 500 B → full body, no hint

cargo fmt --all && cargo clippy --all-targets && cargo test --all → 1691 passed, 6 ignored.

Verification

End-to-end repro from the issue, before vs after:

# Before (master 0.37.2)
$ rtk curl -s 'https://jsonplaceholder.typicode.com/posts' | python3 -c 'import sys,json; json.load(sys.stdin)'
json.decoder.JSONDecodeError: Invalid control character at: line 12 column 153 (char 523)

# After (this PR)
$ rtk curl -s 'https://jsonplaceholder.typicode.com/posts' | python3 -c 'import sys,json; print(len(json.load(sys.stdin)))'
100

Notes on related PRs

Test plan

  • cargo fmt --all && cargo clippy --all-targets && cargo test --all green
  • End-to-end smoke test against https://jsonplaceholder.typicode.com/posts returns parseable JSON
  • CI matrix (Ubuntu / macOS / Windows) green
  • Manual check: rtk curl to a non-JSON endpoint in a TTY still shows the truncated form + tee hint

🤖 Generated with Claude Code

… 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]>
@CLAassistant

CLAassistant commented Apr 29, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@pszymkowiak pszymkowiak added bug Something isn't working effort-small Quelques heures, 1 fichier filter-quality Filter produces incorrect/truncated signal labels Apr 29, 2026
@pszymkowiak

Copy link
Copy Markdown
Collaborator Author

[w] wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Fixes JSON output truncation in rtk curl where responses >= 500 bytes were being cut mid-stream, producing invalid JSON that broke downstream parsers like jq and python -m json.tool. Adds an IsTerminal gate so pipes/redirects always receive full output, and skips truncation for JSON-shaped bodies even on TTY. Legacy truncation behavior is preserved only for non-JSON content displayed in a terminal.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #1536, #1282


Analyzed automatically by wshm · This is an automated analysis, not a human review.

…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]>

@KuSh KuSh left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM!

Comment thread src/cmds/cloud/curl_cmd.rs Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working effort-small Quelques heures, 1 fichier filter-quality Filter produces incorrect/truncated signal

Projects

None yet

Development

Successfully merging this pull request may close these issues.

P0 bug(rtk curl): large JSON responses truncated mid-stream producing invalid JSON, breaks all API agent pipelines

3 participants