Skip to content

Add --max-results to end a search after the file with the N-th matching line - #226

Merged
Shengyu Fu (shengyfu) merged 5 commits into
microsoft:mainfrom
dpelksnitis:max-results
Nov 25, 2026
Merged

Shengyu Fu (shengyfu) merged 5 commits into
microsoft:mainfrom
dpelksnitis:max-results

Conversation

@dpelksnitis

@dpelksnitis Dan Elksnitis (dpelksnitis) commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

A pattern that matches most of a large tree can only be cut short by killing the search or piping it into head, and a server searches every candidate either way.

  • --max-results <N> stops after the file in which the N-th matching line is found. Files are reported whole, so a few more lines than N can be printed. The lines counted are those of the source, so a multiline match counts every line it covers, however it is printed.
  • A scan and the on-disk index stop at that file.
  • A plain server gets it as the max_results search option, stops after the chunk of candidates that reaches it, and marks the final reply "truncated": true.
  • A shared daemon and a search with --sort search every candidate, and the limit is applied to the result.
  • It cannot be combined with -l, --files-without-match, -c, --count-matches, -q or --files.

On a 154k-file private tree (Windows), a query with 37k candidate files that takes 6.5 s in full ends after 0.6 s with --max-results 10000.

Tested by tests/max_results.rs (scan, on-disk index, server, several search paths, multiline matches, and the rejected flag combinations), a shared daemon test, and a unit test for counting the rows of an older server. README and AGENTS.md describe the flag.

Copilot AI balanced review requested due to automatic review settings November 19, 2026 14:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The result limit can be exceeded because file identities collide across search roots and multiline or replacement output is counted instead of original matched source lines.

2 open issues
What changed in this PR

Adds --max-results <N> to limit broad tgrep searches while preserving whole-file output.

Changes:

  • Adds the CLI flag, invocation-wide counting, and validation of incompatible output modes.
  • Introduces early stopping for scans, on-disk indexes, and plain servers; shared-daemon and sorted searches apply the limit after searching.
  • Documents the behavior and adds backend, streaming, and flag-conflict tests.
File Description
tgrep-cli/​tests/​streamed_search.rs Checks limited streamed replies and truncation metadata.
tgrep-cli/​tests/​max_results.rs Tests limits across backends, paths, sorting, and incompatible flags.
tgrep-cli/​src/​serve.rs Accepts max_results, limits results, and reports truncation.
tgrep-cli/​src/​search.rs Applies backend stopping and server-result rendering limits.
tgrep-cli/​src/​output.rs Tracks matching lines across the invocation.
tgrep-cli/​src/​main.rs Defines the flag and stops processing further search targets.
README.md Documents CLI behavior, restrictions, and server options.
AGENTS.md Adds guidance on using the limit for broad searches.

Review effort: Balanced

Comment thread tgrep-cli/src/output.rs Resolved
Comment thread tgrep-cli/src/output.rs Resolved
…ng line

A pattern that matches most of a large tree is only cut short by killing the
search or piping it into head, and a server searches every candidate either
way.

--max-results <N> stops after the file in which the N-th matching line is
found, so the output is whole files. A scan and the on-disk index stop there.
The plain server gets it as the max_results search option, stops after the
chunk of candidates that reaches it, and marks the final reply "truncated".
A shared daemon and a search with --sort search every candidate, and the
limit is applied to the result. It cannot be combined with -l,
--files-without-match, -c, --count-matches, -q or --files.

On a 154k-file private tree (Windows), with the candidate reads batched (a
separate change), a query with 37k candidate files that takes 6.5 s in full
ends after 0.6 s with --max-results 10000.
The limit was counted from the rows written, keyed by the file's path under
its search root and the row's line number. Files of one name under separate
search paths were taken for one file, and a match of several lines printed
as one row (-U with --vimgrep or --replace) counted as one line.

Each reported file now adds the matching lines of its source, as its match
totals give them. A scan and the on-disk index take them from the file's
matches, and the server carries them with the file's result. For the count
it keeps across search paths, the client asks every limited search for the
per-file totals; rows are only counted for a server that predates those.
Copilot AI balanced review requested due to automatic review settings November 19, 2026 15:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Server-side limiting counts binary matches that the client later hides, allowing them to exhaust --max-results and suppress valid text matches, potentially producing empty output and exit 1.

4 open issues
1 resolved since last review

Review effort: Balanced

Comment thread tgrep-cli/src/serve.rs Resolved
Comment thread tgrep-cli/src/serve.rs Resolved
Comment thread tgrep-cli/src/serve.rs Resolved
A file can be indexed as text and still be found binary when it is searched:
indexing inspects only its first 8 KiB, and a file can change before the
index catches up. The server counted the matching lines of such a file
towards max_results, while the client leaves a binary file out of a directory
search. A limit reached on that file ended the search with nothing printed,
though text files matched.

The server no longer counts a binary file. The client counts one that it
reports, as a scan does.
Copilot AI balanced review requested due to automatic review settings November 19, 2026 16:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Multi-path searches fail to pass the remaining result budget to the server, causing unnecessary work, while sorted, limited searches overcount match statistics through servers or shared daemons.

1 open issue
2 previously missed issues

In code that hasn't changed since last review

Medium severity Limit later paths by the remaining result budget

tgrep-cli/​src/​search.rs:1156

For later command-line search paths, this sends the original invocation limit rather than the remaining budget. If the first path reports 9,999 matching lines with --max-results 10000, a server answering the second path still searches until it finds another 10,000 lines (subject to chunk boundaries), although the client only needs the next matching file. This loses the early-stop benefit on broad searches across multiple paths. Subtract writer.reported_lines() before sending the server limit.

Medium severity Exclude truncated files from reported match totals

tgrep-cli/​src/​search.rs:1823

With five one-line matching files, --sort path --max-results 1 --stats prints one match but reports five matches and five matched lines through a server or shared daemon. Track the files retained by this loop and restrict the match totals below to those files, including the older-server fallback; search-work statistics can still include all searched candidates.

3 resolved since last review

Review effort: Balanced

A search of several paths sent each path's server the whole limit, so a
later path was searched for more lines than the output still needed. It now
gets what is left.

With --stats, a limit applied to a server's or shared daemon's reply (a
sorted search, a shared daemon) printed the totals of every file in the
reply, where a scan reports the files it printed. The totals now cover the
files ahead of the one at which the limit ended the output.
@dpelksnitis

Copy link
Copy Markdown
Contributor Author

Both points from the latest review are addressed in 3ad98c7.

  • Remaining budget for later paths: the client sends a server what is left of the limit (limit - reported lines), so a later search path ends as early as the first. limited_request covers the calculation.
  • Totals of a limited search: with --stats, the totals cover the files ahead of the one at which the limit ended the output, including the older-server fallback, so a server or shared daemon reports what a scan does. stats_cover_reported_files checks a server with and without --sort.

Copilot AI balanced review requested due to automatic review settings November 19, 2026 16:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block the change introducing the --max-results search limit.

1 open issue

Review effort: Balanced

@dpelksnitis

Dan Elksnitis (dpelksnitis) commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

I think these two fails are also spurious, not caused by this change:

  • Clippy fails on two needless_bool lints in serve.rs (lines 3572 and 7669) that also fail on main's scheduled run
  • the ubuntu failure is managed::faults::operations_cancel_stops_an_in_flight_collection_without_later_delete_credit, which passes on the other platforms and on main, looks flaky?

Could you re-run these jobs?

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 01:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the change adding invocation-wide search result limits.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@shengyfu
Shengyu Fu (shengyfu) merged commit ba19304 into microsoft:main Nov 25, 2026
10 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.

3 participants