Skip to content

Truncate reports one more truncated line than it removed when the output ends in a newline #788

Closed

Description

What happens

Truncate.output splits on a newline and counts lines from the result. For text that ends in a newline, split("\n") yields a trailing empty string, so a 100-line output becomes 101 elements.

Two visible effects:

  • The preview keeps that empty string as its last entry.
  • removed = lines.length - out.length reports 91 for a 100-line file truncated to maxLines: 10, where 90 lines were actually dropped.
Received: "line0\n...\nline9\n\n...91 lines truncated..."
Expected: "line0\n...\nline9\n\n...90 lines truncated..."

Why it is wrong

The count is how the model reasons about how much output it missed, and it appears alongside "The tool call succeeded but the output was truncated." A file that ends in a newline is the common case (almost every CLI and log file), so the number the model reads is routinely one too high.

It also reads a blank line into the preview, which is what a model takes as "there is an empty line here" when deciding where a log ends.

Reproduction

const lines = Array.from({ length: 100 }, (_, i) => `line${i}`).join("\n") + "\n"
const result = await Truncate.output(lines, { maxLines: 10 })
result.content  // contains "...91 lines truncated...", should be 90

The existing truncates by line count test uses the same 100 lines without the trailing newline, which is why this was not caught.

Suggested fix

Strip one trailing newline before splitting.

const lines = text.endsWith("\n") ? text.slice(0, -1).split("\n") : text.split("\n")

Byte accounting (totalBytes) is computed from text directly and is unaffected, as is the full output saved to disk.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions