fix(registry): only rewrite last pipe segment; prevent data corruption in pipes/redirects#2274
Open
youbamj wants to merge 1 commit into
Open
fix(registry): only rewrite last pipe segment; prevent data corruption in pipes/redirects#2274youbamj wants to merge 1 commit into
youbamj wants to merge 1 commit into
Conversation
Implement Unix philosophy: when commands are piped, only the last segment outputs to terminal. All commands before the last pipe stay raw; only the portion after gets RTK processing. Changes: - rewrite_compound(): find last | globally, leave before raw, process after with normal per-segment logic - has_stdout_redirect(): only block on > >> &> (not < input redirect) - cat < file.txt: special case → rtk read - < file.txt - xargs: add as transparent prefix (xargs grep → xargs rtk grep) - Add tests for pipe chains with &&/||/; operators - Add tests for input redirect (<) behavior Fixes stdout passthrough for piped/redirected commands.
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 critical data corruption bugs where RTK incorrectly filters piped or redirected commands, breaking downstream tools and producing malformed output files.
Real Bugs Fixed
Bug 1: Large JSON truncated, breaks
jq$ rtk curl https://jsonplaceholder.typicode.com/posts | jq jq: parse error: Unfinished string at EOF at line 14, column 0Root cause: RTK filters
curloutput, truncating large JSON responses. The truncated data is piped tojq, which fails to parse incomplete JSON.Bug 2: Git diff corrupted when redirected to file
Before (RTK-filtered patch - BROKEN):
README.md | 2 ++ 1 file changed, 2 insertions(+) --- Changes --- README.md @@ -2,6 +2,8 @@ +Change + <p align="center"> <strong>High-performance CLI proxy that reduces LLM token consumption by 60-90%</strong> </p> +2 -0After this fix (real patch - CORRECT):
Result:
git apply /tmp/rtk-test.patchfails with "No valid patches in input" on the RTK version, but succeeds on the real version.Algorithm
|in entire command|→ stays raw (intermediate pipe segments)|→ normal rewrite rules:>,>>,&>) → skip entirelyrtkif yes<is fine (reading from file, output still to terminal)Examples
git log | cat && git stashgit log | rtk cat && rtk git stashcargo test 2>&1 | head > fileNonecat < file.txtrtk read - < file.txtfind . | xargs grep patternfind . | xargs rtk grep patterncurl ... | jqNoneChanges
src/discover/registry.rs(+198, -62 lines)rewrite_compound()with last-pipe detectionrewrite_segments()helperhas_stdout_redirect()(checks>,>>,&>only)<input redirect to not block rewritexargstoSHELL_PREFIX_BUILTINSTesting
Breaking Change
This changes the behavior of
rtk rewritefor piped commands. Previously all commands in a pipe gotrtkprefix; now only the last segment does. This is the correct Unix behavior but may surprise users relying on the old (broken) behavior.