Skip to content

fix(tool): bound file_read result bytes, not just line count - #1878

Merged
lizhengfeng101 merged 4 commits into
alibaba:mainfrom
RerankerGuo:fix/file-read-byte-cap
Nov 17, 2026
Merged

lizhengfeng101 merged 4 commits into
alibaba:mainfrom
RerankerGuo:fix/file-read-byte-cap

Conversation

@RerankerGuo

@RerankerGuo RerankerGuo commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

file_read caps 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 single file_read call can push megabytes into the conversation — and report IS_TRUNCATED: false, because the line-count limit was never reached. This is the file_read sibling of #1827 (code_search) and #1171 (body-size versus diff-size), filed as #1875.

Concretely, on main today:

  • a 170 KB single-line JSON is returned in full with IS_TRUNCATED: false;
  • 400 lines x 2 KB are returned in full (~820 KB) with 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:

  • Two byte bounds for the rendered result, applied while rendering so the metadata stays truthful:
    • 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 explicit Note: Output truncated at 262144 bytes. … line telling the model to continue with a narrower start_line/end_line range.
  • IS_TRUNCATED: true now also reflects either byte bound, not just the 500-line range cut.
  • LINE_RANGE is 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 pre-existing 500-line note and its condition are unchanged.

The values mirror the per-line/total split the code_search truncation work uses (#1832/#1833), kept local to file_read.go since that change is still under review.

Testing

  • New regression tests in 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, with IS_TRUNCATED: true and LINE_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, and LINE_RANGE only 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 rendered N|text lines 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.go clean (the CJK encoding fixture carries the script's allow-non-english marker with its reason).

Docs

The file_read Limits section in pages/src/content/docs/{en,zh,ja,ko,ru}/tools.md documents 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.

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
@github-actions

github-actions Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

🔍 OpenCodeReview found 1 issue(s) in this PR.

  • ✅ Successfully posted inline: 1 comment(s)

Comment thread internal/tool/file_read.go Outdated
Comment on lines +89 to +95
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)

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.

bug · medium
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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@/tmp/pr1878_reply.md

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.

@/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 NanaseInori 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.

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.

@RerankerGuo

Copy link
Copy Markdown
Contributor Author

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.

@chaojixinren

Copy link
Copy Markdown
Contributor

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

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

LGTM

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

LGTM

@lizhengfeng101
lizhengfeng101 merged commit 4192238 into alibaba:main Nov 17, 2026
15 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.

file_read's 500-line limit does not bound single-line tool-result bytes

4 participants