From 2396f7b9729c1b2b5ef8bfd28cdc87717fe97bf6 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Fri, 13 Nov 2026 11:33:53 -0600 Subject: [PATCH] fix(core): preserve a single blank line in paged reads (#63809) --- packages/core/src/tool/plugin/read.ts | 3 ++- packages/core/src/tool/read-filesystem.ts | 2 ++ .../core/test/tool-read-filesystem.test.ts | 25 +++++++++++++++++-- packages/core/test/tool-read.test.ts | 2 ++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/packages/core/src/tool/plugin/read.ts b/packages/core/src/tool/plugin/read.ts index f0b04931e62..3d71d12fd65 100644 --- a/packages/core/src/tool/plugin/read.ts +++ b/packages/core/src/tool/plugin/read.ts @@ -348,7 +348,8 @@ export const toModelContent = (path: string, offset: number | undefined, output: const start = output.type === "text-page" ? output.offset : 1 // Pages already join selected lines; a trailing newline represents a selected blank line. const text = output.type === "file" ? output.content.replace(/\n$/, "") : output.content - const lines = output.content === "" ? [] : text.split("\n") + const empty = output.type === "text-page" ? output.lineCount === 0 : output.content === "" + const lines = empty ? [] : text.split("\n") // A page that starts before the requested offset is the file's tail, returned for an offset past the end. const total = start + lines.length - 1 const pastEnd = output.type === "text-page" && offset !== undefined && start < offset diff --git a/packages/core/src/tool/read-filesystem.ts b/packages/core/src/tool/read-filesystem.ts index 98962a9ee54..444379e5c71 100644 --- a/packages/core/src/tool/read-filesystem.ts +++ b/packages/core/src/tool/read-filesystem.ts @@ -82,6 +82,7 @@ export type FileContent = typeof FileContent.Type export class TextPage extends Schema.Class("ReadTool.TextPage")({ type: Schema.Literal("text-page"), content: Schema.String, + lineCount: NonNegativeInt, mime: Schema.String, offset: PositiveInt, truncated: Schema.Boolean, @@ -332,6 +333,7 @@ const textPage = Effect.fnUntraced(function* ( return new TextPage({ type: "text-page", content: entries.join("\n"), + lineCount: entries.length, mime: mimeType(input), offset: from, truncated: next !== undefined, diff --git a/packages/core/test/tool-read-filesystem.test.ts b/packages/core/test/tool-read-filesystem.test.ts index 7b32291646f..2e413f6f583 100644 --- a/packages/core/test/tool-read-filesystem.test.ts +++ b/packages/core/test/tool-read-filesystem.test.ts @@ -90,9 +90,30 @@ describe("ReadTool text serialization", () => { name: "preserves empty-file page output", content: "", page: { limit: 2 }, - output: { type: "text-page", content: "", offset: 1, truncated: false }, + output: { type: "text-page", content: "", lineCount: 0, offset: 1, truncated: false }, model: "Read file lines.txt, 0 lines", }, + { + name: "numbers the only blank line in a newline-only file", + content: "\n", + page: { limit: 1 }, + output: { type: "text-page", content: "", lineCount: 1, offset: 1, truncated: false }, + model: "Read file lines.txt, lines 1-1\n1: ", + }, + { + name: "numbers a single blank line before continuation", + content: "alpha\n\nomega\n", + page: { offset: 2, limit: 1 }, + output: { type: "text-page", content: "", lineCount: 1, offset: 2, truncated: true, next: 3 }, + model: "Read file lines.txt, lines 2-2\n2: \n[Output truncated. Continue reading with offset: 3]", + }, + { + name: "counts the last blank line when an offset is past EOF", + content: "alpha\n\n", + page: { offset: 9, limit: 1 }, + output: { type: "text-page", content: "", lineCount: 1, offset: 2, truncated: false }, + model: "Read file lines.txt, lines 2-2 (offset 9 is past the end; the file has 2 lines)\n2: ", + }, ] cases.forEach((input) => { @@ -105,7 +126,7 @@ describe("ReadTool text serialization", () => { const result = yield* ReadToolFileSystem.read(current.environment, file, "lines.txt", input.page) expect(result).toMatchObject(input.output) - expect(ReadTool.toModelContent("lines.txt", undefined, result)).toBe(input.model) + expect(ReadTool.toModelContent("lines.txt", input.page.offset, result)).toBe(input.model) }), ) }) diff --git a/packages/core/test/tool-read.test.ts b/packages/core/test/tool-read.test.ts index 48ad4f76140..e969ba5c51f 100644 --- a/packages/core/test/tool-read.test.ts +++ b/packages/core/test/tool-read.test.ts @@ -1130,6 +1130,7 @@ describe("ReadTool", () => { readResult = new ReadToolFileSystem.TextPage({ type: "text-page", content: "hello", + lineCount: 1, mime: "text/plain", offset: 2, truncated: true, @@ -1170,6 +1171,7 @@ describe("ReadTool", () => { readResult = new ReadToolFileSystem.TextPage({ type: "text-page", content: "nine\nten", + lineCount: 2, mime: "text/plain", offset: 9, truncated: true,