Skip to content

read reports a phantom last line and an inflated total for any file ending in a newline #764

Closed

Description

What happened?

read finished the last line unconditionally at end of file:

if (chunk.done) {
  state.stopped = consume(decoder.decode()) || finishLine()
  break
}

finishLine is the handler for a partial last line — one with no trailing
newline. For a file that ends in a newline, consume has already flushed every
line and left state.line empty, so finishLine still runs: it increments the
line counter and pushes an empty string.

So a two-line file is reported as three lines, and the tool prints a numbered
empty line for it. On 3e94875c:

<file>
00001| a
00002| b
00003| 

(End of file - total 3 lines)
</file>

Three consequences, all visible to the model:

  • (End of file - total N lines) is one too high.
  • A phantom 00003| line is printed.
  • lastReadLine = offset + raw.length feeds the "Use 'offset' parameter to read
    beyond line N" hint, so paging is off by one too: reading a three-line file
    with offset: 2 returns 00003| three and a phantom 00004|.

Almost every source file ends in a newline, so this fires on essentially every
read. A file with no trailing newline is unaffected, which is why the existing
cases did not catch it.

Steps to reproduce

  1. Write a file that ends in a newline, e.g. printf 'a\nb\n' > two.txt.
  2. Ask the agent to read it.
  3. Read the tool output: a third, empty line is printed and the total says 3.

The two focused regressions in test/tool/read.test.ts fail on 3e94875c and
pass with the fix.

How did you install OpenScience?

From source (bun dev).

OpenScience version

Source commit 3e94875c.

Provider and model

Not applicable; the defect is in how the file is rendered, before any model call.

Operating system

Not OS-specific; reproduced in the Bun test harness.

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