Skip to content

fix(git): don't count --max-lines truncation as savings (#1561) - #1573

Closed
SAY-5 wants to merge 10 commits into
rtk-ai:developfrom
SAY-5:fix/git-diff-show-max-lines-tracking-1561
Closed

SAY-5 wants to merge 10 commits into
rtk-ai:developfrom
SAY-5:fix/git-diff-show-max-lines-tracking-1561

Conversation

@SAY-5

@SAY-5 SAY-5 commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

Closes #1561.

Background

Sibling of #1045 (already addressed for `rtk read` in #1076). `rtk git diff` and `rtk git show` previously called:

compact_diff(stdout, max_lines.unwrap_or(500))

and then tracked savings against the resulting (possibly truncated) output. `compact_diff` performs both real RTK compression and the early-break truncation at `result.len() >= max_lines`. When the user passed `--max-lines N`, the truncated tail was counted as RTK savings — the same inflation pattern as #1045.

Fix

Apply the same shape #1076 used for read:

  1. Promote the implicit RTK budget to a named constant `RTK_DIFF_BUDGET = 500`. `compact_diff` is now always called with this budget, so its early break represents real RTK compression and nothing else.
  2. Add `truncate_to_lines(&str, Option) -> Cow<'_, str>` as a separate post-step that honors the user's `--max-lines`. `Cow::Borrowed` on the no-truncation fast path keeps the common case zero-alloc.
  3. In `run_diff` and `run_show`, display the user-truncated slice but track against the RTK-compressed (pre-user-truncation) string. When `max_lines` is `None`, the tracking string equals `final_output` exactly as before.

Behavior

Invocation Displayed Tracked
`rtk git diff` (no flag) compact_diff with 500-line cap (unchanged) same as displayed (unchanged)
`rtk git diff --max-lines 5` compact_diff(500) → first 5 lines + "more changes truncated" compact_diff(500) — full ~500-line view
Same for `rtk git show` … …

Test plan

Four regression tests under `cmds::git::git::tests`:

  • `test_truncate_to_lines_no_truncation_when_none` — pins the `Cow::Borrowed` fast path.
  • `test_truncate_to_lines_under_limit_returns_owned_unchanged_body` — body preserved, no breadcrumb when under the limit.
  • `test_truncate_to_lines_over_limit_emits_breadcrumb` — over-limit truncates and emits the breadcrumb.
  • `test_max_lines_does_not_inflate_tracking` — end-to-end shape: 200-line compacted diff + `--max-lines 5` → displayed ≤ 6 lines with breadcrumb, tracking has all 200 and no breadcrumb.

Results:

  • `cargo test --bin rtk -- cmds::git::git` → 62 passed; 0 failed (4 new + 58 existing).
  • `cargo build --bin rtk` clean (pre-existing libc::sighandler_t warning is unrelated).
  • `cargo fmt` clean.

Notes

  • Independent of fix(read): don't count --max-lines truncation as token savings (#1045) #1076 (which is still in review): this PR introduces its own `truncate_to_lines` helper local to `git.rs` rather than depending on a future shared `pick_tracking_output` in `core/utils.rs`. If maintainers prefer that helper to land first, this can be rebased to use it.
  • Polluted historical data is out of scope (per the issue) — this fix only makes future tracking accurate.

aeppling and others added 10 commits April 17, 2026 09:44
…master--components--rtk

chore(master): release 0.37.0
…master--components--rtk

chore(master): release 0.37.1
…master--components--rtk

chore(master): release 0.37.2
Closes rtk-ai#1561.

`rtk git diff` and `rtk git show` previously called

    compact_diff(stdout, max_lines.unwrap_or(500))

and tracked savings against the resulting (possibly truncated)
output. `compact_diff` does both real RTK compression *and* the
early-break truncation at `result.len() >= max_lines`. When the
user passed `--max-lines N`, the truncated tail counted as RTK
savings — exactly the inflation pattern fixed for `rtk read` in
rtk-ai#1045 / rtk-ai#1076.

Apply the same shape here:

* Add a fixed `RTK_DIFF_BUDGET = 500` constant. compact_diff is
  always called with this budget so its early-break is a real
  RTK compression decision.
* Add a `truncate_to_lines(&str, Option<usize>)` post-step that
  honors the user's `--max-lines`. Returns `Cow::Borrowed` when
  no truncation is requested, so the no-flag path is zero-alloc.
* In `run_diff` and `run_show`, display the user-truncated
  slice but track against the RTK-compressed (pre-user-truncation)
  string. When `max_lines` is None, behavior is unchanged.

Tests:
* truncate_to_lines: no-op fast path, under-limit body, over-limit
  breadcrumb.
* End-to-end shape: when user passes --max-lines 5 against a
  200-line compacted diff, displayed has <= 6 lines + breadcrumb
  and tracking sees the full 200-line text without breadcrumb.
@CLAassistant

CLAassistant commented Apr 28, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@pszymkowiak pszymkowiak added bug Something isn't working effort-small Quelques heures, 1 fichier filter-quality Filter produces incorrect/truncated signal labels Apr 28, 2026
@pszymkowiak

Copy link
Copy Markdown
Collaborator

[w] wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟢 Risk low

Summary

Fixes inflated RTK token savings tracking when users pass --max-lines to rtk git diff or rtk git show. The truncation from --max-lines was being counted as RTK compression savings. The fix separates RTK compression (compact_diff with a fixed 500-line budget) from user-requested truncation (new truncate_to_lines helper), tracking against the pre-truncation content.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #1561


Analyzed automatically by wshm · This is an automated analysis, not a human review.

@SAY-5

SAY-5 commented May 6, 2026

Copy link
Copy Markdown
Contributor Author

Closing per the triage feedback — thanks!

@SAY-5 SAY-5 closed this May 6, 2026
maxmilian added a commit to maxmilian/rtk that referenced this pull request May 26, 2026
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 added a commit to maxmilian/rtk that referenced this pull request Jun 6, 2026
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working effort-small Quelques heures, 1 fichier filter-quality Filter produces incorrect/truncated signal

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rtk git diff/show --max-lines may inflate savings same way as #1045

4 participants