Repository navigation
fix(curl): emit valid JSON envelope when truncating large responses - #1587
Closed
truffle-dev wants to merge 1 commit into
Closed
truffle-dev wants to merge 1 commit into
truffle-dev wants to merge 1 commit into
Conversation
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
Collaborator
📊 Automated PR Analysis
SummaryFixes 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
Linked issues: #1536 Analyzed automatically by wshm · This is an automated analysis, not a human review. |
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 |
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.
Closes #1536.
Today, on any curl response above 500 bytes the truncation produces
output like:
That is no longer valid JSON. Anything piping rtk through
jq, parsingstructured 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→jqfails).Approach
I considered three options and went with the smallest one that fits the
existing design:
regresses the token-savings intent of the simplified filter that landed
in Simplified
rtk curlFilter #1310, and makes large responses expensive again for the LLM caller.above the threshold, replace the body with a small envelope:
{"_rtk":{"truncated":true,"total_bytes":1234,"tee_log":"~/path"}}_rtk.truncated, and readthe full body from the tee log. Keeps the token-saving guarantee.
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 andnot 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 producesthat aren't actually JSON but happen to start with those bytes are
extremely rare and will surface as a parse error on
_rtklookup, whichis 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):
With the fix:
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.