Skip to content

[Bug]: read_file line pagination can return unbounded output for a single-line file #2951

Closed

Description

@joylae

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

  1. 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
  2. Build a HarnessAgent with its default filesystem tools and default
    ToolResultEvictionConfig.

  3. Have the agent execute:

    read_file(
      path="large-single-line.json",
      offset=0,
      limit=1
    )
    
  4. Observe that the tool result contains the complete multi-megabyte line,
    despite limit=1.

  5. The result bypasses tool-result eviction because read_file is 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_chars or max_bytes parameter;
  • 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 main also appears affected
  • Java Version: 21
  • OS: Linux

Additional context

Related upstream work:

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

  1. added
    bug
    on Sep 3, 2026
  2. tianchangxin commented on Sep 3, 2026

    @tianchangxin

    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!

  3. oss-maintainer commented on Sep 3, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: bug | Status: Confirmed

    Verified 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. Because read_file is listed in ToolResultEvictionConfig.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

  4. added
    area/harness
    on Sep 3, 2026
  5. wzq-xzwj commented on Oct 28, 2026

    @wzq-xzwj
    Contributor

    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.

  6. added a commit that references this issue on Oct 29, 2026
    0ee7812
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/harnessbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions