Skip to content

fix(tool): stop read from appending a phantom last line - #765

Merged
Ishaan Gangwani (ishaan1124) merged 1 commit into
synthetic-sciences:mainfrom
aniruddhaadak80:fix/read-trailing-line-total
Sep 28, 2026
Merged

Ishaan Gangwani (ishaan1124) merged 1 commit into
synthetic-sciences:mainfrom
aniruddhaadak80:fix/read-trailing-line-total

Conversation

@aniruddhaadak80

Copy link
Copy Markdown
Contributor

What does this PR do, and why?

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 final line — one with no trailing
newline. When a file 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.

A two-line file is therefore reported as three lines. On 3e94875c:

<file>
00001| a
00002| b
00003| 

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

Three things the model sees, all wrong:

  • (End of file - total N lines) is one too high.
  • A numbered 00003| empty line is printed.
  • lastReadLine = offset + raw.length feeds the "Use 'offset' parameter to read
    beyond line N"
    hint, so paging is off by one as well: a three-line file read
    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 never caught it.

The fix only finishes a line when one is actually pending:

const flushed = consume(decoder.decode())
const pending = state.line.length > 0 || state.lineWide
state.stopped = flushed || (pending && finishLine())

state.lineWide is included so a line that was cut at MAX_LINE_LENGTH still
gets its ... marker and lineNumber bump.

Linked issue

Fixes #764

How did you verify it?

Two regressions added to backend/cli/test/tool/read.test.ts, both driven
through the real ReadTool:

  • a newline-terminated file reports the lines it has and no phantom last one
    covers "a\nb\n" (2 lines, no 00003|), "only\n" (1 line, no 00002|) and
    "a\nb" — the unterminated case still keeps its partial last line, so the fix
    is not "drop the last line".
  • offset paging agrees with the reported total pins the interaction with
    paging: limit: 2 then offset: 2 ends at the real last line, and offset: 3
    on a three-line file returns nothing while still reporting total 3 lines.

On unmodified 3e94875c both fail:

Expected to contain: "(End of file - total 2 lines)"
Received: "<file>\n00001| a\n00002| b\n00003| \n\n(End of file - total 3 lines)\n</file>"

Expected to contain: "(End of file - total 3 lines)"
Received: "<file>\n00003| three\n00004| \n\n(End of file - total 4 lines)\n</file>"

Commands run:

  • bun test --timeout 30000 ./test/tool/read.test.ts → 36 pass, 1 fail
  • The single failure is asks for external_directory permission when an internal symlink resolves outside the project, which I reproduced on unmodified main
    with my change stashed, so it is not from this diff.
  • bun run --cwd backend/cli typecheck → exit 0

The whole suite matters here rather than just the new cases: this change touches
line accounting for every read, and the existing truncation, offset, limit and
long-line cases all still pass.

Pre-existing reds on this Windows checkout, not from this diff:

  • bun run format:check cannot pass here: git materializes the LF blobs as CRLF,
    so Prettier flags hundreds of untouched files, including tsconfig.json and
    turbo.json. I verified all three changed files are formatted per the repo
    config with line endings normalized.
  • bun run typecheck: backend is clean; @synsci/workspace fails because
    frontend/workspace/src/custom-elements.d.ts is a symlink (mode 120000) that
    Windows checked out as a text file containing ../../ui/src/custom-elements.d.ts,
    which TypeScript then parses as source → TS1128.

Checklist

  • bun run check is green (format, typecheck, backend + frontend/ui + SDK tests) — blocked on this Windows checkout by the CRLF and symlink artifacts described above; backend typecheck is clean and the full read suite is green apart from the pre-existing symlink case above
  • bun run --cwd frontend/workspace build succeeds if I touched frontend/workspace or frontend/ui — not touched
  • ./tooling/repo/generate.ts was run and the tooling/sdk output committed if I changed backend/cli/src/server — not touched
  • CHANGELOG.md has an Unreleased entry if the change is user-visible
  • The matching docs page under frontend/docs/src/content/openscience/ is updated if behavior changed — no doc change needed: the offset parameter is already documented as a line number, and this makes the reported numbers match it
  • Screenshots or a short video are attached for UI changes — not a UI change
  • No version bumps (package.json versions and tags are written by the release workflow)
  • install and frontend/landing/public/install are still byte-identical if I touched either — not touched

@vercel

vercel Bot commented Sep 27, 2026

Copy link
Copy Markdown

ANIRUDDHA ADAK (@aniruddhaadak80) is attempting to deploy a commit to the InkVell Team on Vercel.

A member of the Team first needs to authorize it.

`finishLine` handles a partial final line, so a file ending in a newline had an
empty one appended: every line number shifted, the total reported to the model
was one too high, and the offset hint pointed one line past the end.
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.

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

2 participants