Repository navigation
fix(tool): truncate oversize code_search match lines - #1833
RichardoMrMu wants to merge 3 commits into
Conversation
`git grep` caps matches at 100 but passes matched line content through unbounded, so a committed minified bundle (single-line, megabytes long) can balloon a code_search result to multiple MB and blow the model context window (observed: 7.2 MB result, then a 400 on the 1M-token limit). Truncate each rendered match line to 4096 runes with a marker carrying the true rune count, and emit a note when any line was truncated. Oversize lines beyond the match cap were already not rendered. Fixes alibaba#1827. Assisted-by: Doubao
|
🔍 OpenCodeReview found 1 issue(s) in this PR.
|
| func truncateMatchLine(s string, runeLen int) string { | ||
| return string([]rune(s)[:gitGrepMaxLineLen]) + fmt.Sprintf(" ...(truncated, total %d chars)", runeLen) | ||
| } |
There was a problem hiding this comment.
The string s is converted to []rune here a second time — the caller at line 232 already performed []rune(content) and passed the length. For the exact use case this guards against (megabyte-scale minified lines), this redundant conversion allocates another large slice and re-decodes the entire UTF-8 string. Consider accepting the already-computed rune slice instead, or restructuring so the conversion happens only once.
Suggestion:
| func truncateMatchLine(s string, runeLen int) string { | |
| return string([]rune(s)[:gitGrepMaxLineLen]) + fmt.Sprintf(" ...(truncated, total %d chars)", runeLen) | |
| } | |
| func truncateMatchLine(runes []rune, runeLen int) string { | |
| return string(runes[:gitGrepMaxLineLen]) + fmt.Sprintf(" ...(truncated, total %d chars)", runeLen) | |
| } |
abhisheksharma2411
left a comment
There was a problem hiding this comment.
Heads up that #1832 is fixing the same issue in the same file — internal/tool/code_search.go plus its test, also for #1827. Neither of you could easily have known; they went up within a day of each other. Flagging it so a maintainer does not review both from scratch, and so neither of you keeps polishing a branch that is going to be closed.
Since they overlap, the useful thing I can add is the measurable difference rather than a preference.
Both truncate per line. This one caps each rendered line at 4096 runes; #1832 caps at 2 KB and backs off to a UTF-8 boundary. The real difference is that #1832 also adds a total output cap (256 KB) and this one does not.
That matters for the symptom in #1827, because gitGrepMaxCount is 100:
| per line | worst case total | |
|---|---|---|
main |
unbounded | the 3–8 MB in the issue |
| this PR | 4096 runes | ~400 KB for ASCII, up to ~1.6 MB if the matches are 4-byte runes |
| #1832 | 2 KB | 200 KB, and hard-capped at 256 KB |
So this PR makes the reported case much better, but the bound is still a product of two numbers rather than a ceiling, and the worst case stays in the range that provoked "Context compression exceeded threshold" in the first place. A total cap is the part that actually guarantees the property.
The reverse is true of one thing here: counting in runes and reporting the true rune count reads more naturally than a byte budget, and your note names how many lines were cut, which #1832's marker does per line but not in aggregate. If a maintainer takes #1832, the rune-count reporting from this one is worth carrying over.
My read is that #1832 is the more complete fix and this one has the nicer reporting, but that is a maintainer's call, not mine — I am just another contributor. Happy to be wrong about the numbers; they are from gitGrepMaxCount = 100 on main and the limits in each description.
Claude assisted with this review.
Adds a 256K-rune hard ceiling on the whole gitGrep result on top of the per-line cap, addressing review alibaba#1833. Reuses the strings.Builder render loop with rune counting; adds TestGitGrep_TotalOutputCap.
|
Thanks for the heads-up @abhisheksharma2411. I've read through #1832 and the overlap is real — both touch internal/tool/code_search.go for the same root cause: a minified bundle can put megabytes on a single line, and the match-count cap counts lines, not bytes. Current state of this PR (#1833):
#1832 takes a similar approach with byte-based caps (2048 bytes/line, 256K total) and UTF-8 boundary backoff in runcateMatchLine. Both solve the same problem; the threshold units differ (runes vs bytes) but the intent is identical. Happy to close #1833 in favor of #1832 if the maintainer prefers the byte-based approach, or to align thresholds if this one is chosen. |
NanaseInori
left a comment
There was a problem hiding this comment.
Reviewed both changed files, #1827, the earlier #1832 implementation, and the existing discussions. This head now includes gitGrepMaxOutputRunes and prospective checks before file headers and match lines. The older comparison saying this PR has no aggregate cap is therefore outdated.
The current design limits line content to 4,096 runes and the guarded body to 262,144 runes. Those units are not bytes: four-byte UTF-8 characters can make the guarded body approach 1 MiB, and introductory/final notes are written outside that counter. The description's approximately 410 KB figure does not describe a hard complete-result byte bound. The per-line truncation itself preserves valid UTF-8, and the added aggregate test does exercise accumulation across files.
The maintainer's November 10 direction on #1832 selects 2 KiB per-line content and 256 KiB for the complete rendered result, including notices. Given the direct overlap in the same renderer, I would carry the pre-write-check approach and aggregate fixture into that byte-based implementation rather than merge two competing limit policies. That is a scope/convergence comment, not a claim that this head still lacks aggregate protection.
The existing double rune-conversion observation remains an optimization opportunity; I am not elevating it into a new blocker without a measured impact. This review is source/test inspection only; I did not run this PR's repository tests or verify its provider-context behavior.
This review was conducted by NanaseInori's review bot, using the model GPT-6 Astra Pro. If you need a human review, please manually @NanaseInori.
Summary
Fixes #1827.
code_searchcaps the number of matches at 100 (gitGrepMaxCount) but passes each matched line through unbounded. In repositories that commit minified bundles (single-line JS/CSS, megabytes long), one tool result can be several MB and blow the model context window....(truncated, total N chars)with the true rune count.Note: N match line(s) exceeded 4096 characters and were truncated to protect the context window.when any rendered line was cut.Test plan
TestGitGrep_LongLineTruncation: 6,000-char ASCII line truncated with marker + note; short lines untouched; rendered line size bounded.TestGitGrep_LongLineTruncation_UTF8: 7,000-char CJK line truncated rune-safely (valid UTF-8 output).Assisted-by: Doubao