Skip to content

fix(grep): preserve -- separator between non-adjacent match blocks - #2836

Merged
aeppling merged 2 commits into
rtk-ai:developfrom
guyoron1:fix/grep-context-separator
Jul 6, 2026
Merged

aeppling merged 2 commits into
rtk-ai:developfrom
guyoron1:fix/grep-context-separator

Conversation

@guyoron1

@guyoron1 guyoron1 commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Summary

  • grep -A/-B/-C context output was dropping the -- group separator between non-adjacent match blocks
  • Consumers (human or LLM) could not tell where one match ended and the next began
  • Fixed by preserving -- in both the faithful baseline and grouped output paths

Fixes #2795

Test plan

  • Added regression test (group_separator_between_non_adjacent_matches)
  • Added faithful format test (context_group_separator_matches_grep_n)
  • Verified output matches native grep -n -A1 exactly
  • cargo fmt && cargo clippy --all-targets && cargo test --all passes

@aeppling

aeppling commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Hey @guyoron1 , thanks for the contribution it solve the core problem of #2795

One blocker before merge;
This inserts a -- on any line-number gap, but it never checks that context (-A/-B/-C) was actually requested.
Native grep only prints -- in context mode. This reconstructs separators from line-number gaps unconditionally, so aplain (non-context) search invents -- between matches that grep never emits.

Repro:
grep -rn todo


rtk grep -rn todo # here lot of -- printed

Single large file behaves the same: grep -n match huge.txt, native 0 but rtk 24. The output ends up with a -- between essentially every match.

Should gate the insertion so it runs when actually requested context.

@aeppling aeppling self-assigned this Jul 5, 2026
guyoron1 added 2 commits July 6, 2026 09:21
When context flags (-A/-B/-C) are used, grep prints -- between
non-adjacent match blocks. parse_match_line() returned None for
this separator, and both output paths silently dropped it, merging
distinct matches into one contiguous block.

Fixes rtk-ai#2795
Native grep only prints -- between non-adjacent match blocks when
-A/-B/-C context is requested. The grouped output path was inserting
-- on any line-number gap unconditionally, so `rtk grep -rn todo`
invented separators between every match that grep never emits.

Gate the insertion on has_context_flag() which checks for -A/-B/-C
and their long-form equivalents.
@guyoron1
guyoron1 force-pushed the fix/grep-context-separator branch 2 times, most recently from 6c7f526 to 34a0f0e Compare July 6, 2026 06:22
@aeppling

aeppling commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Hey @guyoron1 , thanks for the fix, LGTM

Thanks for contributing to RTK !

@aeppling
aeppling merged commit 6871e55 into rtk-ai:develop Jul 6, 2026
20 of 21 checks passed
@github-actions github-actions Bot mentioned this pull request Jul 6, 2026
@guyoron1

guyoron1 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

woohoooooo yay !
I'm using RTK a ton, so why not contribute : )

@guyoron1

guyoron1 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Heyaaaa!

I've actually faced a very similar issue on #2751 — the benchmark curl json test hit the same intermittent failure there too. The root cause was identified in #2551 and fixed within RTK by the never-worse guard in #2554.

The benchmark gate has passed on the latest CI runs, but the curl json failure seen earlier is a known intermittent issue — the benchmark runs curl -s https://httpbin.org/json and rtk curl https://httpbin.org/json as separate HTTP requests, so response variations between calls cause non-deterministic token count comparisons.

The never-worse guard (#2554) works correctly within a single RTK execution, but the benchmark's cross-execution comparison is structurally flaky for live endpoints.

Filed #2842 to fix the benchmark by replacing live HTTP calls with local fixtures.

This PR only touches grep filter code (src/cmds/system/search.rs) and grep tests — no curl code was modified. : )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

grep -A/-B/-C drops the "--" separator between non-adjacent match blocks

2 participants