Description
Describe the bug
The Harness read_file tool paginates by line count through its offset and
limit parameters, but it does not impose a character or byte limit on the
returned content.
For a file containing one very large line, such as a multi-megabyte JSON file,
read_file(path, offset=0, limit=1) returns the entire line. Therefore, even a
small line limit can produce an unbounded tool result and exceed the model
context window.
This is particularly problematic because read_file is included in
ToolResultEvictionConfig.DEFAULT_EXCLUDED_TOOLS. The oversized result is
therefore not handled by ToolResultEvictionMiddleware and is passed directly
to the next model call.
Removing read_file from the exclusion set is not a complete workaround:
when the agent reads an evicted artifact and that read is itself oversized,
the result can be evicted into another artifact. The agent then receives
another placeholder instead of a reliably bounded chunk.
The ranged-read optimization merged in #2402 reduces memory usage by reading
only the requested lines, but a single requested line can still be arbitrarily
large.
To Reproduce
-
Create a workspace file containing a multi-megabyte single line:
{ printf '{"payload":"' head -c 5000000 /dev/zero | tr '\0' 'a' printf '"}' } > large-single-line.json -
Build a
HarnessAgentwith its default filesystem tools and default
ToolResultEvictionConfig. -
Have the agent execute:
read_file( path="large-single-line.json", offset=0, limit=1 ) -
Observe that the tool result contains the complete multi-megabyte line,
despitelimit=1. -
The result bypasses tool-result eviction because
read_fileis excluded,
and the following model request can fail with a context-length error.
The same behavior can be reproduced directly through
FilesystemTool.readFile(...) and an AbstractFilesystem implementation.
Expected behavior
read_file should have a hard output-size boundary independent of line count.
A possible backward-compatible design would be:
- add an optional
max_charsormax_bytesparameter; - enforce a conservative default maximum;
- stop reading when either the line limit or size limit is reached;
- return explicit truncation metadata or guidance;
- provide a byte/character continuation offset for reading the remainder;
- preserve UTF-8 character boundaries;
- handle a single line larger than the limit without returning the whole line.
For example, the result could indicate:
[Content truncated: returned bytes 0-65535.
Continue with byte_offset=65536.]
This would allow read_file to remain excluded from tool-result eviction while
still guaranteeing bounded model input.
grep_files, glob_files, and similar discovery tools already received
bounded-result handling on main; read_file needs an equivalent boundary
that also works for files without newline characters.
Error messages
There is no error at the tool layer. The oversized result is returned
successfully.
The subsequent model call may fail with an error similar to:
context length exceeded
or emergency compaction may fail because the oversized tool result has already
entered the reasoning context.
Environment (please complete the following information):
- AgentScope-Java Version: 2.0.1; current
mainalso appears affected - Java Version: 21
- OS: Linux
Additional context
Related upstream work:
- fix: preserve skill isolation and tool result history #2319 fixed tool-result eviction for the immediate reasoning input and
persisted history. - perf(tool): stream ranged file reads #2402 changed ranged file reads to stream requested lines, but did not add a
character/byte output limit. - [Feature]: Add stable session entry identity and exact history recall #2134 notes that recall tools may need exclusion from tool-result eviction to
avoid immediately evicting recalled content again.
This issue is specifically about making filesystem reads intrinsically bounded,
rather than relying on tool-result eviction to handle an already oversized
read_file result.
Activity
tianchangxin commentedon Sep 3, 2026
Hi, I'd like to take this on — happy to implement the fix. Two options:
Option A — minimal, tool-layer only. Add an optional max_chars/max_bytes to FilesystemTool.readFile
; truncate oversized content at a UTF-8-safe boundary and append a notice (reusing the grep_files/glob_files truncation pattern). No interface change; read_file stays in DEFAULT_EXCLUDED_TOOLS. Limitation: the whole file is still read into memory, and there's no clean continuation within a single oversized line.
Option B — intrinsic, filesystem-level. Extend AbstractFilesystem.read
with a size budget + byte continuation offset; stream only up to the budget, return truncation metadata so the agent can resume mid-line. Fully addresses the issue, but touches all 7 read implementations + tests.
Questions:
(1) accept A first, or go straight to B?
(2) max_chars vs max_bytes, and a default/max value?
Once we agree, I'll open a PR. Thanks!
oss-maintainer commentedon Sep 3, 2026
Issue Evaluation
Category:
bug| Status: ConfirmedVerified against current
main(8a78d6c).Root Cause:
LocalFilesystem.read()(~lines 272–319) paginates by line count only (offset/limit) and imposes no character/byte cap —read_file(path, offset=0, limit=1)on a multi-megabyte single-line file returns the entire line. Becauseread_fileis listed inToolResultEvictionConfig.DEFAULT_EXCLUDED_TOOLS(~lines 66–74),ToolResultEvictionMiddleware.maybeEvict()skips it, so the oversized result is passed directly to the next model call. The ranged-read optimization from #2402 reduces memory usage but does not bound per-line size.Impact: HarnessAgent default filesystem tools; any artifact with very long lines (minified JSON, base64 blobs) can overflow the model context window.
Severity: medium
Note: there is already an active discussion below (Options A/B), and the related report #2942 proposes char-level pagination (
char_offset/char_limit). We confirm the underlying defect is real; choosing between a tool-layer cap and a filesystem-level byte budget is a design decision for maintainers. We will not open a competing PR while that discussion is ongoing.Automated evaluation by github-manager-bot
wzq-xzwj commentedon Oct 28, 2026
Confirmed on current main (4100c86). Two local regression tests fail against the unchanged implementation:
read_file(offset=0, limit=1)returns a 200,000-character single line, and omitting line limits returns an entire 280,000-character snapshot artifact. Both are deterministic local-file tests, without a live model.I am preparing a focused PR to cap returned content and add Unicode code-point pagination inside the selected line range. It preserves the existing four-argument Java entry point and does not change filesystem backend allocation/I/O. This also addresses the readback route where an already-evicted browser result enters context again through
read_file, which is excluded from default tool-result eviction. Separate 2.0.x streaming recovery issues are tracked in #3171; the newer 2.1 recovery implementation does not remove this unbounded tool-output path.fix(harness): bound read_file output with Unicode-safe pagination (ag…