Skip to content

perf(grep): speed up files_with_matches via ripgrep mtime sort and early-stop#968

Draft
7Sageer wants to merge 1 commit into
MoonshotAI:mainfrom
7Sageer:perf/grep-rg-sort-modified
Draft

perf(grep): speed up files_with_matches via ripgrep mtime sort and early-stop#968
7Sageer wants to merge 1 commit into
MoonshotAI:mainfrom
7Sageer:perf/grep-rg-sort-modified

Conversation

@7Sageer

@7Sageer 7Sageer commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No related issue.

Problem

The Grep tool's default files_with_matches mode stat-ed every matching file from JS to sort by mtime, then discarded everything beyond head_limit. That is one (cross-channel, on remote Kaos / SSH backends) round-trip per matched file, so a broad pattern in a large repository could make a single grep call slower than an LLM inference. It also always scanned the whole tree even when only the first page of results was needed.

What changed

  • files_with_matches now runs ripgrep with --sortr modified, so ripgrep emits matches in most-recently-modified-first order.
  • Records are read incrementally and the ripgrep subprocess is killed as soon as head_limit non-sensitive matches are collected, so broad patterns no longer force a full scan or per-file stat calls from JS.
  • content and count_matches keep the existing buffered path (and their exact totals).
  • Because the search stops early, the files_with_matches truncation notice reports more available instead of an exact total.

Added tests for the early-stop behavior and removed the now-obsolete JS stat-sort tests.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

Delegate most-recently-modified-first ordering to ripgrep via --sortr
modified, then read records incrementally and kill the subprocess as soon
as head_limit non-sensitive matches are collected. This removes the
per-file stat pass from JS and avoids a full tree scan for broad patterns.

content and count_matches keep the buffered path. The files_with_matches
truncation notice no longer reports an exact total, since the search stops
early.
@changeset-bot

changeset-bot Bot commented Jun 22, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 16742d2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

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

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.

1 participant