Skip to content

fix(curl): emit valid JSON envelope when truncating large responses - #1587

Closed
truffle-dev wants to merge 1 commit into
rtk-ai:developfrom
truffle-dev:fix/curl-json-envelope-on-truncation
Closed

truffle-dev wants to merge 1 commit into
rtk-ai:developfrom
truffle-dev:fix/curl-json-envelope-on-truncation

Conversation

@truffle-dev

Copy link
Copy Markdown

Closes #1536.

Today, on any curl response above 500 bytes the truncation produces
output like:

{"items":[{"id":1,"name":"alpha"},{"id":2,"name"... (1234 bytes total)
[full output: ~/.local/share/rtk/curl-...log]

That is no longer valid JSON. Anything piping rtk through jq, parsing
structured tool output, or feeding the response back to an LLM hits a
parse error on every response above the threshold. Reproducer in #1536
(rtk curl https://jsonplaceholder.typicode.com/users → jq fails).

Approach

I considered three options and went with the smallest one that fits the
existing design:

  • A — passthrough (PR fix(curl): preserve valid JSON output for piping (#1015) #1016): drop truncation entirely for JSON. This
    regresses the token-savings intent of the simplified filter that landed
    in Simplified rtk curl Filter #1310, and makes large responses expensive again for the LLM caller.
  • B — JSON envelope (this PR): when the response is JSON-shaped and
    above the threshold, replace the body with a small envelope:
    {"_rtk":{"truncated":true,"total_bytes":1234,"tee_log":"~/path"}}
    Downstream agents can parse it, branch on _rtk.truncated, and read
    the full body from the tee log. Keeps the token-saving guarantee.
  • C — config flag: adds surface area for a behavior that should
    always be correct.

The envelope path also folds the tee log into the JSON object and sets
tee_hint = None, so callers print exactly one valid JSON document and
not a separate [full output: ...] trailer line.

Non-JSON responses keep the existing prefix-with-byte-count format. The
shape detection is a one-byte check ({ or [); responses curl produces
that aren't actually JSON but happen to start with those bytes are
extremely rare and will surface as a parse error on _rtk lookup, which
is at worst the same outcome as today.

Verification

Stash-bisect against this branch's two new tests:

Without the fix (existing implementation, new tests applied):

test test_filter_curl_large_json_object_emits_valid_envelope ... FAILED
test test_filter_curl_large_json_array_emits_valid_envelope  ... FAILED
  panicked: 'envelope must be valid JSON for downstream agents to parse:
  Error("EOF while parsing a string", line: 1, column: 521)'

With the fix:

test result: ok. 7 passed; 0 failed; 0 ignored

The four pre-existing tests (test_filter_curl_json_small_no_tee_hint,
test_filter_curl_non_json, test_filter_curl_long_output_truncated,
test_filter_curl_multibyte_boundary, test_filter_curl_exact_500_bytes)
all still pass — non-JSON truncation behavior is unchanged.

The previous truncation produced output like `{"items":[{"id":1,"name":"...`
followed by `... (N bytes total)` which is not parseable as JSON. Agents
running rtk in JSON pipelines (jq, structured tool output, schema
validation) hit a parse error on every response above 500 bytes.

When the response is JSON-shaped and exceeds the threshold, replace the
output with a small envelope that downstream consumers can still parse:

    {"_rtk":{"truncated":true,"total_bytes":1234,"tee_log":"~/path"}}

Non-JSON responses keep the existing prefix-with-byte-count format so
shell pipelines that depend on the textual hint are not affected. The
tee log path is folded into the envelope and the separate trailer line
is suppressed in the JSON branch so callers print exactly one valid
JSON document.

Closes rtk-ai#1536
@CLAassistant

CLAassistant commented Apr 28, 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 28, 2026
@pszymkowiak

Copy link
Copy Markdown
Collaborator

[w] wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟢 Risk low

Summary

Fixes invalid JSON output when curl responses exceed the 500-byte truncation threshold. Instead of mid-stream truncation that breaks JSON parsing, large JSON responses now emit a valid JSON envelope containing truncation metadata and a path to the full output log.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #1536


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

@KuSh

KuSh commented May 2, 2026 •

Copy link
Copy Markdown
Collaborator

rtk aims is to reduce LLM's tokens usage, passing content to other tools doesn't consume tokens and shouldn't be impacted by rtk. it should passthrough for such cases. I'm closing this PR in favor of #1602

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.

4 participants