Skip to content

fix(grep): dedupe overlapping context windows; honor max_matches=0 - #167

Merged
vedaant00 merged 3 commits into
vedaant00:mainfrom
t957095:fix/grep-context-dedupe
Sep 21, 2026
Merged

vedaant00 merged 3 commits into
vedaant00:mainfrom
t957095:fix/grep-context-dedupe

Conversation

@t957095

@t957095 t957095 commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

Both halves of #165:

  1. Overlapping context windows duplicated lines. Each match appended its full window with no tracking — a 5-line file with 3 matches and context=2 emitted 11 lines. A per-file printed set now emits each line at most once.
  2. max_matches=0 returned one match labelled (capped at 0). The cap was checked after appending; the check now runs before a match is emitted, so 0 yields no matches and max_matches=1 still returns the first hit plus the cap note.

Tests: three regression tests added (overlap dedupe emits each line once and in order; max_matches=0; max_matches=1 keeps the first hit). All pass.

Note: test_grep_context_lines_returns_neighbors and test_grep_context_zero_is_default_and_no_context_markers fail on Windows on clean main too — _rel() emits OS-native separators (src\a.py vs the asserted src/a.py). Pre-existing, unrelated to this change; happy to send a small follow-up normalizing separators if you want.

Closes #165

Two fixes in the grep tool (vedaant00#165):
- each match re-emitted its full context window with no tracking, so
  overlapping windows duplicated lines (5-line file produced 11);
  a per-file printed set now emits each line once
- the cap was checked after appending, so max_matches=0 returned one
  match labelled '(capped at 0)'; the check now runs before emitting

Tests: three regression tests in tests/test_tools.py (overlap dedupe,
max_matches=0, max_matches=1 still includes the first hit). Note: two
existing context tests fail on Windows due to path separators in
_rel() output ('src\a.py' vs 'src/a.py'); that is pre-existing on
main and unrelated to this change.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Overlapping matches can retain an incorrect context marker, and the regression test does not assert marker correctness.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Medium severity

Open (1)
What changed in this PR

Updates grep to deduplicate overlapping context output and correctly honor max_matches=0.

Changes:

  • Deduplicates overlapping context lines.
  • Checks match caps before emitting results.
  • Adds regression tests for overlap and match limits.
File Description
tests/​test_tools.py Adds regression coverage for deduplication and match caps.
src/​opendot/​tools/​local.py Implements context deduplication and cap handling.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/opendot/tools/local.py
…ches

Copilot review on the PR: when an overlapping context window reached a
line already emitted as context (j in printed) and that line was the
current match (j == i), the entry kept its dash marker, so the fixture
returned f.txt:3-aaa instead of f.txt:3:aaa — a real match hidden as
context, contradicting the docstring's path:line:text format. The entry
is now rewritten to the colon form, and the overlap regression test
asserts every marker.
@t957095

t957095 commented Sep 19, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the Copilot finding in the latest commit: when an overlapping context window reaches a line already emitted as context and that line is the current match (j in printed and j == i), the entry is upgraded from the context dash to the match colon instead of keeping the stale marker — the fixture now reports f.txt:3:aaa, not f.txt:3-aaa.

The overlap regression test asserts every marker explicitly: : / - / : / - / :. All grep tests pass locally; the two remaining context-test failures on my Windows box are the pre-existing _rel() path-separator issue noted in the commit message, and fail identically on unmodified main.

The logic changes were correct but three lines exceeded the configured
width, so CI's lint job failed while the three test jobs passed. No
behavior change: the dedupe, the pre-append cap check and the
context-to-match marker upgrade all produce identical output.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@vedaant00

Copy link
Copy Markdown
Owner

Nice work @t957095, logic is correct on all three counts, and I like that the overlap test asserts every marker rather than just counting lines. Verified locally: output now matches real grep -C2 exactly, max_matches=0 returns no matches, and cross-file line collisions are handled. Both new tests fail on pre-fix code and pass with it.

Only the lint job was red (three lines over the width limit). I pushed a ruff format commit to your branch, no behavior change. Good to merge once CI is green.

And yes please on the Windows _rel() separator follow-up, that's a real gap our Linux-only CI can't see.

@vedaant00
vedaant00 merged commit 35e599e into vedaant00:main Sep 21, 2026
4 checks passed
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 duplicates lines when match context windows overlap; max_matches=0 returns one match

3 participants