Repository navigation
fix(tool): bound file_read result bytes, not just line count - #1878
Conversation
The 500-line cap counts lines, not bytes, so a committed minified or generated file can push megabytes into the conversation in one call with IS_TRUNCATED: false. Cap a rendered line at 2,048 bytes (UTF-8 rune-boundary cut, explicit omission marker) and the rendered body at 256 KiB, set IS_TRUNCATED when either fires, and keep LINE_RANGE describing only the lines actually shown. Closes alibaba#1875
|
🔍 OpenCodeReview found 1 issue(s) in this PR.
|
| if renderedBytes > 0 && renderedBytes+len(text) > fileReadMaxResultBytes { | ||
| outputTruncated = true | ||
| break | ||
| } | ||
| renderedBytes += len(text) | ||
| shown = i + 1 | ||
| fmt.Fprintf(&body, "%d|%s\n", int(startLine)+i, text) |
There was a problem hiding this comment.
The renderedBytes budget only accounts for len(text) (the line content after per-line capping), but the actual bytes written to body on line 95 also include the line-number prefix (%d|) and the trailing newline (\n). Each rendered line therefore contributes ~3–8 extra bytes that are not counted toward fileReadMaxResultBytes. With up to 500 lines, the actual body can exceed the stated 256 KiB cap by several kilobytes.
To keep the cap accurate, include the full formatted line length in the budget check, e.g.:
entry := fmt.Sprintf("%d|%s\n", int(startLine)+i, text)
if renderedBytes > 0 && renderedBytes+len(entry) > fileReadMaxResultBytes {
...
}
renderedBytes += len(entry)
body.WriteString(entry)There was a problem hiding this comment.
@/tmp/pr1878_reply.md
There was a problem hiding this comment.
@/tmp/pr1878_reply.md
??另外请在pr描述中说明使用的模型名称而不只是agent工具
The renderedBytes budget counted only len(text), the line content after the per-line cap, but the body also carries the line-number prefix and the trailing newline for every rendered line. That uncounted per-line overhead let the rendered body drift past fileReadMaxResultBytes: 500 lines overshoot the stated 256 KiB cap by up to a few kilobytes. Budget the fully formatted line instead, so the rendered body is now guaranteed to stay within the cap. Line truncation markers and the IS_TRUNCATED / LINE_RANGE metadata semantics are unchanged. Add a regression test that measures the rendered body exactly: 499 rendered lines of 525 bytes produce 264,362 body bytes under the old content-only accounting, against the 262,144-byte budget.
NanaseInori
left a comment
There was a problem hiding this comment.
The file_read result-size problem in #1875 is supported by the original rendering path: a line-count limit does not bound a minified line's bytes. The issue-number search returned only this PR; the described code_search work concerns a different tool.
Reviewed Execute(), capFileReadLine(), the added regression tests, and the existing review discussion. The earlier uncounted-prefix/newline finding is fixed at this head: the limit is checked against the entire formatted entry. The displayed end line uses the number actually emitted, and either byte limit sets IS_TRUNCATED. For valid UTF-8 input, the per-line cut backs off without splitting a character. The total-body limit includes the omission markers; headers and explanatory notes are separate from that body limit. I found no remaining blocking defect in this implementation.
One scope clarification: #1875 asks for configurable bounds, while this patch provides fixed constants (2,048 content bytes per line and 256 KiB for the formatted body). That is a product-scope decision to acknowledge, not a reason to reject the demonstrated bounded-output fix. Narrower line ranges recover later lines after the total cap; they do not recover the omitted tail of a capped individual line, which the new omission message correctly warns about.
Approval is based on source/test inspection. I did not independently run the repository tests or reproduce the author's full-suite results.
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.
|
Thanks for the thorough review and for confirming the bounded-output behavior at this head. Noted on the scope clarification: #1875 asks for configurable bounds while this patch ships fixed constants (2,048 content bytes per line, 256 KiB for the formatted body). That was a deliberate minimal choice; if maintainers prefer configurability, a small follow-up can lift the two constants into the scan template without touching the truncation logic. |
|
Resolve conflicts |
# Conflicts: # pages/src/content/docs/en/tools.md # pages/src/content/docs/ja/tools.md # pages/src/content/docs/ko/tools.md # pages/src/content/docs/ru/tools.md # pages/src/content/docs/zh/tools.md
Summary
file_readcaps results at 500 lines, but nothing bounds bytes. A committed minified or generated file (a dist bundle, a lockfile, a one-line JSON export) packs arbitrarily many bytes into one line, so a singlefile_readcall can push megabytes into the conversation — and reportIS_TRUNCATED: false, because the line-count limit was never reached. This is thefile_readsibling of #1827 (code_search) and #1171 (body-size versus diff-size), filed as #1875.Concretely, on main today:
IS_TRUNCATED: false;IS_TRUNCATED: false, since 400 < 500.Both results then enter the LLM conversation as-is and accumulate across rounds until compression.
Changes
internal/tool/file_read.go:fileReadMaxLineBytes = 2048: a longer line is cut at a UTF-8 rune boundary (a multibyte character is never split mid-sequence) and marked with an explicit[line truncated, N more bytes omitted]suffix.fileReadMaxResultBytes = 256 KiB: rendering stops before the rendered body would exceed the budget — counted over the fully formatted lines (line-number prefix + content + trailing newline), so the cap is not undershot by uncounted per-line overhead — with an explicitNote: Output truncated at 262144 bytes. …line telling the model to continue with a narrowerstart_line/end_linerange.IS_TRUNCATED: truenow also reflects either byte bound, not just the 500-line range cut.LINE_RANGEis computed from the lines actually rendered, so it no longer overstates when the byte budget stops rendering early (line numbers in the body were already truthful; the header now agrees with them).The values mirror the per-line/total split the code_search truncation work uses (#1832/#1833), kept local to
file_read.gosince that change is still under review.Testing
internal/tool/file_read_test.go, all failing on main before the fix and passing after:TestExecute_SingleOversizeLineIsCapped— a 170,000-byte single line comes back bounded, marked, withIS_TRUNCATED: trueandLINE_RANGE: 1-1(on main: full 170 KB returned,IS_TRUNCATED: false).TestExecute_TotalResultBytesCapped— 400 x 2,048-byte lines stop at the total budget with the output-truncation note, andLINE_RANGEonly claims rendered lines (on main: ~821 KB returned).TestExecute_LineCapIsUtf8Safe— a line of multibyte characters cut mid-line stays valid UTF-8 (rune boundary, no split sequence).TestExecute_ResultBudgetCountsFormattedLine(added in 7a07888, from review) — the budget counts the fully formatted line, not just its content: 499 rendered 525-byte lines measured a 264,362-byte body against the 262,144-byte budget under content-only accounting; the test extracts the renderedN|textlines exactly and asserts the body stays within the cap.go test ./internal/... ./cmd/... -count=1— all green, including the existing truncation test (IS_TRUNCATED: true,LINE_RANGE: 1-500, "Results truncated to 500 lines" note unchanged).go build ./...,go vet ./internal/tool/,gofmt -s,scripts/verify-license.sh,scripts/verify-english-only.goclean (the CJK encoding fixture carries the script'sallow-non-englishmarker with its reason).Docs
The
file_readLimits section inpages/src/content/docs/{en,zh,ja,ko,ru}/tools.mddocuments the new byte bounds alongside the 500-line cap.Closes #1875
AI-Assisted Development disclosure
Per CONTRIBUTING ("AI-Assisted Development"): this change was developed with the help of an AI coding assistant (ZCode CLI, GLM model by Z.ai) — issue triage, a first draft of the patch and tests, and doc wording drafts. I reviewed and edited every line, re-derived the failure modes (verified both oversize shapes on main before the fix), ran the full test suite and the repo's check scripts locally, and take responsibility for the content. No AI attribution is included in the commit, per the contribution rules.