Skip to content

fix(core): never-worse output guard - #2554

Merged
aeppling merged 9 commits into
developfrom
fix/global-never-worse-guard
Jun 25, 2026
Merged

aeppling merged 9 commits into
developfrom
fix/global-never-worse-guard

Conversation

@aeppling

@aeppling aeppling commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Never-worse guard so RTK never emits more tokens than the command the agent ran. core::guard::never_worse(raw, filtered) returns raw when filtered is larger — and is applied only where filtered is a structural compression of a faithfully captured raw (the agent's exact command + args, nothing added, no extra process).

never_worse stays pure (empty raw → 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)

  • Shared sink: runner::emit_guarded / print_with_hint compose body + tee hint and guard the whole; run_captured_filter covers the run_filtered* family.
  • TOML filters one central guard site (project / .rtk / global / built-in).
  • read raw made faithful to cat / cat -n (line numbers are faithful to the command, so they're compared like-for-like, not dropped).
  • pipe stdin filter mode now guarded.
  • aws JSON subcommands already guarded; the two s3 text paths (s3 ls, s3 sync/cp) now route through emit_guarded too (Generalize never-worse guard to all filters (curl emits more tokens than raw on tiny responses) #2551 class).
  • docker (ps / ps -a / images) — the agent's command is now authoritative: it ran a plain command for raw 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. Now the real command is the source of truth (faithful failure on error), --format is a best-effort parse aid that falls back to raw, and the guard always sees a real baseline. Parsers unchanged.
  • grep — rebased onto develop's grep rewrite (fix(grep): correctly handle all flag shapes and never exceed raw output #2550), re-applying our no-result rule: a genuine no-match emits empty (like real grep), never a 0 matches line that exceeds empty raw.

Deferred — separate "adds-info" follow-up (out of scope: these emit data the command never produced / run an extra process)

  • git add — git add is silent; RTK runs an extra git diff --cached --stat to synthesize an ok N files… summary (guarding it would suppress the summary).
  • dotnet — injects -bl/--logger and reads a binlog the agent's command never produced.
  • git status / git show — same authoritative-command restructure as docker (two-run today); higher risk because porcelain drops the rebase/merge state header, so done carefully and separately.

Documented exceptions (unguardable by design)

  • streaming (tsc, gradlew, cargo) — no full buffer exists mid-stream; safe by construction (subset emit + bounded flush).
  • proxy / passthrough — output == raw by definition.

Related

Test plan

  • fmt + clippy + cargo test --all — green (2259 passed).
  • guard unit + integration tests; empty-raw / no-match regressions.
  • docker authoritative-command verified live (ps / ps -a / images); aws s3 + pipe + read + env covered.
  • develop merged; grep conflict resolved (develop's rewrite + our no-match→empty).

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.
@aeppling aeppling added the invalid This doesn't seem right label Jun 23, 2026
@aeppling
aeppling marked this pull request as draft June 23, 2026 20:50
aeppling added 5 commits June 24, 2026 11:18
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
aeppling marked this pull request as ready for review June 24, 2026 17:36

@pszymkowiak pszymkowiak 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.

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.

@aeppling
aeppling merged commit af81b08 into develop Jun 25, 2026
17 checks passed
@aeppling aeppling mentioned this pull request Jun 25, 2026
@aeppling
aeppling deleted the fix/global-never-worse-guard branch June 30, 2026 20:07
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

invalid This doesn't seem right

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generalize never-worse guard to all filters (curl emits more tokens than raw on tiny responses)

2 participants