Repository navigation
fix(core): never-worse output guard - #2554
Merged
Merged
Conversation
RTK could emit more tokens than the underlying command on small inputs:
filters that add headers, summaries, re-indentation, or a tee hint, plus
synthetic no-result messages ("0 matches", "No stashes", "[docker] 0
containers") printed where the raw command emitted nothing. Both break the
Transparency principle and inflate tokens instead of saving them.
- core::guard::never_worse(raw, filtered) returns raw when the filtered form
has more tokens (reuses tracking::estimate_tokens), so RTK output is never
larger than the real command.
- runner::emit_guarded(filtered, hint, raw) composes body + tee hint, guards
the whole, prints, and returns what was shown so printed == tracked.
- run_captured_filter guards the run_filtered* family centrally; per-site
guards cover the remaining single-string filters.
- On empty raw, emit empty and preserve the exit code instead of a synthetic
no-result message (the messages were cosmetic with no dependents; #2461
reports the grep one as actively harmful).
- git stash show now propagates its exit code instead of masking a real
failure as Ok(0).
Resolves #2551.
Masking env secrets is not what core RTK (OSS) is for -- it compresses command output, it does not redact what the command itself already reveals. It also broke raw-vs-filtered consistency, which the never-worse guard relies on.
pipe filters piped stdin but bypassed the guard; route its output through never_worse like every other compressor.
Compare filtered output against the raw the agent's command would emit (numbered for -n), not against unnumbered content, so the guard keeps requested line numbers instead of dropping to plain.
…eline
docker ps/ps -a/images ran a plain command for the raw baseline and a
separate --format command for parsing, capturing the plain run with
unwrap_or_default(). A failed plain run while --format succeeded left
raw empty, and never_worse("", summary) dropped real output.
Run the agent's actual command first as the source of truth: on failure
show its output/exit faithfully; on success use its stdout as raw and
treat --format as a best-effort parse aid that falls back to raw. The
guard stays pure and always sees a real baseline. Parsers unchanged.
aeppling
marked this pull request as ready for review
June 24, 2026 17:36
…orse-guard # Conflicts: # src/cmds/system/grep_cmd.rs
pszymkowiak
approved these changes
Jun 24, 2026
pszymkowiak
left a comment
Collaborator
There was a problem hiding this comment.
Approving. The core never_worse guard (guard.rs + runner integration) is clean and well-tested, and the docker authoritative-command restructure is a solid fix for the empty-raw drop (#2551). Tracking now records the actually-emitted output — the right call.
Non-blocking nits for a possible follow-up: wget's ok | size / FAILED summaries are "adds-info" (size comes from a separate stat), so guarding them can suppress the summary when raw is tiny (e.g. wget -q) — could be treated like the deferred git add/dotnet cases. oc support and the grep-fallback rewrite are welcome but somewhat orthogonal to the guard.
Merged
sBucka
added a commit
to sBucka/rtk
that referenced
this pull request
Jul 3, 2026
Two gaps found on re-review against the same bug classes rtk has already fixed elsewhere: - Exit code was read via .code().unwrap_or(1), which silently reports 1 for a signal-killed curl (e.g. a timeout SIGTERM) instead of the conventional 128+signal. This is the exact bug fixed for `rtk run` in rtk-ai#2681; switched to the existing exit_code_from_output() helper. (curl_cmd still has the old pattern — left untouched, out of scope for this PR.) - The Markdown conversion was emitted directly, bypassing the never-worse guard (core::guard::never_worse / runner::emit_guarded) that curl_cmd already goes through. Without it, pathological/ adversarial HTML that happens to convert to something larger than the raw response would silently cost more tokens than plain curl — the same regression class rtk-ai#2551/rtk-ai#2554 generalized the guard to prevent. Now both the HTML and passthrough paths converge through emit_guarded before tracking, matching curl_cmd's structure. No behavior change for the common case (real pages still convert and shrink as before); this only changes the signal-exit-code edge case and adds a safety net for the pathological-HTML edge case. Re-verified against example.com, api.github.com/zen, and jsonplaceholder — HTML still converts, JSON/plain-text still passes through untouched.
6 tasks done
This was referenced Jul 6, 2026
This was referenced Sep 12, 2026
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
Never-worse guard so RTK never emits more tokens than the command the agent ran.
core::guard::never_worse(raw, filtered)returnsrawwhenfilteredis larger — and is applied only wherefilteredis a structural compression of a faithfully capturedraw(the agent's exact command + args, nothing added, no extra process).never_worsestays pure (emptyraw→ empty). Guard concerns that involve execution (a baseline run failing) are fixed in the execution layer, not by weakening the guard.What's guarded (faithful
raw, compress-only)runner::emit_guarded/print_with_hintcompose body + tee hint and guard the whole;run_captured_filtercovers therun_filtered*family..rtk/ global / built-in).rawmade faithful tocat/cat -n(line numbers are faithful to the command, so they're compared like-for-like, not dropped).s3 ls,s3 sync/cp) now route throughemit_guardedtoo (Generalize never-worse guard to all filters (curl emits more tokens than raw on tiny responses) #2551 class).ps/ps -a/images) — the agent's command is now authoritative: it ran a plain command forrawand a separate--formatcommand for parsing, capturing the plain run withunwrap_or_default(); a failed plain run while--formatsucceeded leftrawempty andnever_worse("", summary)dropped real output. Now the real command is the source of truth (faithful failure on error),--formatis a best-effort parse aid that falls back toraw, and the guard always sees a real baseline. Parsers unchanged.grep), never a0 matchesline that exceeds emptyraw.Deferred — separate "adds-info" follow-up (out of scope: these emit data the command never produced / run an extra process)
git addis silent; RTK runs an extragit diff --cached --statto synthesize anok N files…summary (guarding it would suppress the summary).-bl/--loggerand reads a binlog the agent's command never produced.Documented exceptions (unguardable by design)
tsc,gradlew,cargo) — no full buffer exists mid-stream; safe by construction (subset emit + bounded flush).output == rawby definition.Related
Test plan
fmt+clippy+cargo test --all— green (2259 passed).ps/ps -a/images); aws s3 + pipe + read + env covered.