Skip to content

fix(tool): bound code_search result bytes, not just match count - #1984

Open
jgautheron wants to merge 2 commits into
alibaba:mainfrom
jgautheron:fix/code-search-result-bytes
Open

jgautheron wants to merge 2 commits into
alibaba:mainfrom
jgautheron:fix/code-search-result-bytes

Conversation

@jgautheron

@jgautheron jgautheron commented Nov 17, 2026 •

Copy link
Copy Markdown

Description

code_search caps 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 one code_search call 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_search for that term. The tool result is larger than the line itself, and the following request is rejected.

Fix: code_search now applies the same byte bounds that file_read gained in #1878.

  • Each rendered match line is cut at 2 KiB on a UTF-8 rune boundary. The cut line ends with …[line truncated, N more bytes omitted], and a note tells the model the line was cut.
  • Rendered match lines are capped at 128 KiB in total. Matches past the budget are not rendered, a note says the output was truncated, and each file's Match lines header counts only the lines shown.
  • The existing cutter in file_read.go now takes the limit as a parameter (capLineBytes), so both tools share one implementation. file_read behaviour is unchanged.
  • Normal-length lines render exactly as before.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

How Has This Been Tested?

Two new tests in internal/tool/code_search_test.go generate 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 test passes locally

  • Manual testing: go test -count=1 ./internal/tool/ passes locally (without -race). Raising the total cap to 1 MiB makes TestGitGrep_BoundsTotalResultBytes fail, so the test exercises the bound.

Checklist

  • My code follows the project's coding style (go fmt, go vet)
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or my feature works
  • New and existing unit tests pass locally with my changes
  • I have signed the CLA
  • I did not use AI/LLM to create this PR, or I disclosed the tool/model below and reviewed its output; I did not attribute commits to AI and will answer maintainer questions and review comments myself without AI/LLM.

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).

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.
@CLAassistant

CLAassistant commented Nov 17, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions

Copy link
Copy Markdown
Contributor

✅ OpenCodeReview: Review complete: 0 finding(s) across 2 selected item(s).

@abhisheksharma2411 abhisheksharma2411 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.

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 mehdikarimi55 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.

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 check passes
  • 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] = true stays before the budget continue, so the truncation note still reports the true matched-file count
  • The renderedBytes > 0 grace means a single 2 KiB line always renders even though it exceeds the budget alone — bounded by codeSearchMaxLineBytes, 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/fileOrder append 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.
@jgautheron
jgautheron force-pushed the fix/code-search-result-bytes branch from 32bb666 to bfc78d1 Compare November 19, 2026 22:48

This branch has not been deployed

No deployments
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.

code_search: 100-match cap is count-only — minified single-line files produce multi-MB tool results that exceed the context limit

4 participants