Repository navigation
fix(tool): bound code_search result bytes, not just match count - #1984
jgautheron wants to merge 2 commits into
Conversation
Cap each rendered match line at 2 KiB and the rendered match lines at 128 KiB in total, reusing the file_read line cutter, so one minified line can no longer push the next request past the provider input limit.
|
✅ OpenCodeReview: Review complete: 0 finding(s) across 2 selected item(s). |
abhisheksharma2411
left a comment
There was a problem hiding this comment.
Nice catch on the 1.8 MB single-line case — sharing capLineBytes with file_read rather than writing a second cutter is the right call.
One thing I noticed reading gitGrep, which may or may not bother you:
matchCount only increments for lines that are actually rendered, so once the byte budget binds it stops growing and matchCount >= gitGrepMaxCount is never reached. That means truncated stays false and the count note never prints:
Note: Showing the first 100 matches across N matching files. ...
Your own TestGitGrep_BoundsTotalResultBytes is in exactly that state — 100 matches in the file, roughly 62 rendered before the budget binds, and the only note is the byte one. The byte note does say output was cut, so nothing is misleading. But the count note is the one carrying len(matchedFiles), and "your term matched 400 files" is probably the most useful thing the model could be told at that moment, since the advice in both notes is to narrow the search.
Might be as simple as letting the byte path report the matched-file count too, or counting all matches separately from rendered ones. Your call on whether it's worth the extra state.
Small nit while I'm here: entryBytes := len(fmt.Sprintf("%d|%s\n", ln, content)) builds the string just to measure it. len(strconv.Itoa(ln)) + len(content) + 2 is exact and allocation-free — tiny, but this is in the per-match loop and there's other perf work in flight nearby.
I read this rather than ran it, so take the above as a reading, not a reproduction.
mehdikarimi55
left a comment
There was a problem hiding this comment.
Reviewed and verified locally on 8c621ba:
make test— all 26 packages pass (this also covers the two checklist boxes left unchecked; you can tick them)make checkpasses- The refactor is behavior-preserving for
file_read:capLineBytes(line, fileReadMaxLineBytes)keeps the old hardcoded limit as a parameter
Correctness details I checked:
matchedFiles[fname] = truestays before the budgetcontinue, so the truncation note still reports the true matched-file count- The
renderedBytes > 0grace means a single 2 KiB line always renders even though it exceeds the budget alone — bounded bycodeSearchMaxLineBytes, so the invariant holds - Headers ("File:/Match lines:") sit outside the byte budget; worst case is ~100 headers × ~60 bytes ≈ 6 KB over, bounded by
gitGrepMaxCount, so the result stays predictably small - Skipping past-budget matches before the
seen/fileOrderappend means a file whose first matches are all beyond the budget gets no header at all — consistent with "render no more"
One non-blocking nit: entryBytes := len(fmt.Sprintf("%d|%s\n", ln, content)) allocates a formatted string per matching line just to measure it. At gitGrepMaxCount = 100 it's negligible, but len(content) + len(strconv.Itoa(ln)) + 2 avoids the allocation and reads the same intent. Fine to leave as is.
AI-assisted review (model: GLM), verified by running the tests locally; posted by mehdikarimi55.
…search matchCount only rises for a line that renders, so once the byte budget bound it never reached the cap and the note naming the matched-file count never printed. Count every match separately and report rendered-of-total.
32bb666 to
bfc78d1
Compare
Description
code_searchcaps results by match count (100) but returns each matching line whole. A committed minified or generated file whose single line is about 1.8 MB therefore makes onecode_searchcall return several megabytes. The next model request then exceeds the provider's input limit, and the whole review fails.Repro: commit a file containing one 2 MB line that includes a search term, then run a review that calls
code_searchfor that term. The tool result is larger than the line itself, and the following request is rejected.Fix:
code_searchnow applies the same byte bounds thatfile_readgained in #1878.…[line truncated, N more bytes omitted], and a note tells the model the line was cut.Match linesheader counts only the lines shown.file_read.gonow takes the limit as a parameter (capLineBytes), so both tools share one implementation.file_readbehaviour is unchanged.Type of Change
How Has This Been Tested?
Two new tests in
internal/tool/code_search_test.gogenerate their fixtures:TestGitGrep_BoundsLongMatchLines: one file with a single 2 MB matching line plus a normal file. The result stays under the byte budget, the long line carries the marker with the exact omitted byte count, and the normal match is unchanged.TestGitGrep_BoundsTotalResultBytes: 100 full-length (2 KiB) matches. The result stops near the 128 KiB budget, the header count matches the rendered lines, and the truncation note is present.make testpasses locallyManual testing:
go test -count=1 ./internal/tool/passes locally (without-race). Raising the total cap to 1 MiB makesTestGitGrep_BoundsTotalResultBytesfail, so the test exercises the bound.Checklist
go fmt,go vet)AI disclosure: this change was prepared with AI assistance (Claude, model claude-opus-5-5) and reviewed by me before submission.
Related Issues
Closes #1827. Follows #1875, which fixed the same gap in
file_read(#1878).