Repository navigation
Conversation
maxmilian
marked this pull request as ready for review
May 26, 2026 23:18
maxmilian
force-pushed
the
fix/git-max-lines-not-savings
branch
from
May 26, 2026 23:24
c6880fe to
875c67d
Compare
Follow-up to rtk-ai#1045's fix in `rtk read`. `rtk git diff` and `rtk git show` were doing the same thing: passing the user's `--max-lines N` straight into `compact_diff`, which both compresses and truncates in the same pass. The post-truncation text was then handed to `timer.track`, so any bytes the user themselves asked to hide were recorded as RTK token savings. The fix splits the two concerns: * `RTK_DIFF_BUDGET = 500` — RTK's standard compression budget, used by both `run_diff` and `run_show` regardless of user flags. This is real RTK compression and counts as savings. * `truncate_to_lines(text, n)` — applies the user's `--max-lines` cap on top of an already-compacted diff. Display-only, never tracked. * `prepare_diff_section(raw, user_max_lines) -> (displayed, tracked)` — central helper called by both `run_diff` and `run_show`. `tracked` is always `compact_diff(raw, RTK_DIFF_BUDGET)` so the routing invariant is unit-testable and an accidental refactor that re-introduces the bug fails CI. User-facing: when `--max-lines N` is passed and the RTK-budget output exceeds N lines, the trailing hint becomes `... (M lines hidden by --max-lines)` (terse, matches the existing `[full diff: rtk git diff --no-compact]` style emitted by `compact_diff` itself). Out of scope (per the issue): - Polluted historical data in the local tracking DB. - Pre-existing `verbose`-gated "--- Changes ---" header divergence between `run_diff` (unconditional) and `run_show` (gated). - CLI parser hardening for `--max-lines 0` (helper is panic-safe; defensive rejection at the parser is a separate concern). Avoids the procedural issues that closed prior attempt rtk-ai#1573: branched cleanly from `upstream/develop`, no CHANGELOG / Cargo.toml / release-please-manifest edits (release-please owns those).
maxmilian
force-pushed
the
fix/git-max-lines-not-savings
branch
from
June 6, 2026 05:28
875c67d to
792333c
Compare
Contributor
Author
|
Closing this one as a cleanup pass on my older open PRs. This change has sat without review for a while and the surrounding |
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 #1561.
rtk git diffandrtk git showwere passing the user's--max-lines Ndirectly intocompact_diff, which compresses and truncates in the same pass. The post-truncation text was then handed totimer.track, so bytes the user themselves asked to hide were being recorded as RTK token savings.Root cause
run_diff(src/cmds/git/git.rs) andrun_showboth called:compact_diffinternally stops atresult.len() >= max_lines(git.rs:454) — so--max-lines 5made it stop at 5 lines, andtimer.tracksaw a 5-line output vs an N-thousand-line input, computing a huge savings number. This is the exact pattern @pszymkowiak flagged in #1045's PR-review.Fix
Three new symbols in
src/cmds/git/git.rs:pub(crate) const RTK_DIFF_BUDGET: usize = 500— RTK's standard compression budget, used by bothrun_diffandrun_showregardless of user flags. Anything beyond this is real RTK compression and legitimately counts as savings. (Doc-comment notes the intentionally-separategh_cmd.rs::gh pr diff500 andgit stash show100 budgets.)fn truncate_to_lines(text, n)(private) — applies the user's--max-linescap on top of already-compacted text. Display-only, never tracked. Emits the terse... (N lines hidden by --max-lines)hint to matchcompact_diff's existing[full diff: rtk git diff --no-compact]style. Panic-safe atn == 0(collapses to single hint line, no leading blank).pub(crate) fn prepare_diff_section(raw, user_max) -> (displayed, tracked)— central helper called by bothrun_diffandrun_show.trackedis alwayscompact_diff(raw, RTK_DIFF_BUDGET), independent of user input. Doc explains the "tracked = output RTK would have produced absent the user's display knob" rationale.run_diffandrun_showare reduced to:User-visible changes
rtk git diff --max-lines N/rtk git show --max-lines Nwill be lower (the bug fix — they were over-counted before).... (M lines hidden by --max-lines)(terse, matchescompact_diff's existing trailer style).--max-lines Nshifts subtly: previously you saw the first N lines of acompact_diff(diff, N)invocation (which stops mid-compaction), now you see the first N lines of acompact_diff(diff, 500)invocation (which gets further through the diff before truncating). For most real-worldN < 500cases this means more files appear in the visible head and the per-file hunk caps work consistently.Tests
cargo fmt --all --check && rtk cargo clippy --all-targets -- -D warnings && rtk cargo test— 1952 passed, 6 new incmds::git::git::tests.test_truncate_to_lines_passes_through_when_under_cap/_chops_with_terse_hint/_zero_cap_keeps_only_hint— helper unit tests. The zero-cap test explicitly asserts no leading blank line and a single-line total output.test_rtk_diff_budget_value— locks the constant.test_compact_diff_uses_rtk_budget_independent_of_user_max_lines— shape-level.test_prepare_diff_section_tracks_against_rtk_budget_not_user_cap— routing lock: iterates user_cap ∈{None, 1000, 500, 50, 5}and assertstracked == compact_diff(raw, RTK_DIFF_BUDGET)for every case AND thatdisplayedstarts with the literal head oftracked(catches buggy refactors that recomputedisplayedindependently).Scope
Single file. Deliberately does not:
verbose-gated "--- Changes ---" header divergence betweenrun_diff(unconditional) andrun_show(gated onverbose > 0). Worth noting for a follow-up but unrelated to savings calculation.--max-lines 0at the CLI parser. The helper is panic-safe and returns a single hint line; defensive parser-level rejection would be a separate UX-focused PR.gh_cmd.rs::gh pr diff's hardcoded500toRTK_DIFF_BUDGET. Same concept but cross-module coupling for a one-off callsite is out of scope; the new constant's doc-comment notes this explicitly so the divergence isn't silent.Cargo.toml/CHANGELOG.md/release-please-manifest.json(release-please owns those — the previous attempt fix(git): don't count --max-lines truncation as savings (#1561) #1573 hit a CLA blocker partly because of accidental version-bump commits).Prior attempt
#1573 by @SAY-5 took a similar approach but was self-closed after 9 days without human review. The wshm bot triage was actually positive (
low risk, tests present); the blocker was procedural (CLA stuck on agithub-actions[bot]co-author signature, plus the scope creep above). This PR keeps the approach and avoids the procedural pitfalls.