fix(core): merge grep context hunks#17274
Conversation
Collect grep matches before rendering context output so overlapping or adjacent context windows are emitted once. Add regression coverage for overlapping hunks, fractional context lines, and separator placement.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🦋 Changeset detectedLatest commit: 797fca4 The changes in this PR will be included in the next version bump. This PR includes changesets to release 20 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
WalkthroughThis PR fixes the grep tool to eliminate duplicate context lines when matches overlap. The implementation collects all matches during file scan, then post-processes them into merged hunks for emission with separators between distinct hunks. ChangesGrep overlap fix
🎯 3 (Moderate) | ⏱️ ~22 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
PR triageLinked issue check skipped for core contributor @mbenhamd. PR complexity score
Applied label: Changed test gateChanged Test Gate is pending. The |
Description
Fixes workspace grep context output so overlapping or adjacent context windows are merged before rendering. This prevents the same line from appearing multiple times when matches are near each other.
The grep tool now collects matches for each file, builds merged context hunks, and renders separators only between distinct hunks. It also normalizes fractional
contextLinesvalues before calculating line indexes.Related issue(s)
Fixes #17273
Type of change
Checklist
Validation
pnpm --filter ./packages/core test src/workspace/tools/__tests__/grep.test.tspnpm --filter ./packages/core checkpnpm --filter ./packages/core lintpnpm build:corepnpm prettier --check packages/core/src/workspace/tools/grep.ts packages/core/src/workspace/tools/__tests__/grep.test.ts .changeset/salty-rules-scream.mdELI5
Imagine you're searching a document for specific words. The tool shows you the words you found plus some extra lines around them for context. The bug was: if you have two search results close to each other, those extra "context" lines would get shown twice because each result was showing its own separate context. This PR fixes it by smart merging—now when context windows overlap, the tool shows those lines just once instead of duplicating them.
Overview
This PR fixes a bug in the
workspace_greptool where overlapping or adjacent context windows caused the same source lines to appear multiple times in the output. The solution collects all matches in a file first, then merges nearby matches into unified context "hunks" before rendering output, ensuring each line appears only once and separators (--) only appear between distinct hunks.Changes
Core Implementation
packages/core/src/workspace/tools/grep.ts — Refactored match output generation:
contextLinesvalues (converting fractional values to integers)contextLines > 0contextLinesis 0, emits only matched linesMAX_LINE_LENGTH) behaviorTests
packages/core/src/workspace/tools/tests/grep.test.ts — Added three new test cases:
contextLinesvalues are normalized before renderingDocumentation
.changeset/salty-rules-scream.md — Added changeset entry marking this as a patch release for
@mastra/corewith note: "Fixed grep context output so overlapping matches are shown once."