Skip to content

Commit 33b5650

Browse files
myk1ytZoo (VP)
andauthored
fix(read-file): propagate beyond-EOF error instead of reporting file as empty (#1734)
* fix(read-file): propagate beyond-EOF error instead of reporting file as empty * fix(read-file): report 1-based offset in beyond-EOF error What: readWithSlice now prints offset + 1 in its beyond-file-end error and both spec fixtures assert the 1-based value. Why: read_file takes a 1-based offset from the model, but the error text showed the 0-based value passed to readWithSlice (offset 100 read back as "offset 99"), disagreeing with indentation mode which reports 1-based lines. Impact: the model sees the same line number it asked for, so both read modes report consistent line numbers in errors. * test(read-file): add beyond-EOF integration case through real readWithSlice producer What: add one integration test to readFileTool.spec.ts that unmocks the indentation-reader, fs/promises, path and isbinaryfile modules, builds a ReadFileTool against a real temp file with 5 lines, and calls it with the 1-based offset 100. Why: the existing beyond-EOF regression test mocks readWithSlice, so it only proved that the tool forwards whatever string the stub returns. The 1-based offset in the user-facing message ("offset 100 is beyond file end") was asserted against a hand-written stub whose shape had to be kept in sync with indentation-reader.ts by hand. A change to the real producer would not have failed that test. Impact: the beyond-EOF error path is now covered end-to-end through the real producer against a real file. Verified by mutating indentation-reader.ts to emit the 0-based offset: the new test fails with "offset 99", and passes again once reverted. Test count 84 -> 85 in the core vitest config. No production code changed. --------- Co-authored-by: Zoo (VP) <[email protected]>
1 parent 070ee98 commit 33b5650

5 files changed

Lines changed: 71 additions & 5 deletions

File tree

‎src/core/tools/ReadFileTool.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ export class ReadFileTool extends BaseTool<"read_file"> {
323323
To read more: Use the read_file tool with offset=${nextOffset} and limit=${limit}.
324324
325325
${result.content}`
326-
} else if (result.returnedLines === 0) {
326+
} else if (result.returnedLines === 0 && result.totalLines === 0) {
327327
output = "Note: File is empty"
328328
}
329329

‎src/core/tools/__tests__/readFileTool.spec.ts‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* - Output structure formatting
1313
*/
1414

15+
import os from "os"
1516
import path from "path"
1617

1718
import type { ToolUse } from "../../../shared/tools"
@@ -581,7 +582,72 @@ describe("ReadFileTool", () => {
581582

582583
await readFileTool.execute({ path: "empty.ts" }, mockTask as any, callbacks)
583584

584-
expect(callbacks.pushToolResult).toHaveBeenCalledWith(expect.stringContaining("empty"))
585+
expect(callbacks.pushToolResult).toHaveBeenCalledWith(expect.stringContaining("Note: File is empty"))
586+
})
587+
588+
it("should propagate beyond-EOF error instead of reporting file as empty", async () => {
589+
const mockTask = createMockTask()
590+
const callbacks = createMockCallbacks()
591+
592+
mockedFsReadFile.mockResolvedValue(Buffer.from("line1\nline2\nline3\nline4\nline5"))
593+
mockedReadWithSlice.mockReturnValue({
594+
content: "Error: offset 100 is beyond file end (5 lines)",
595+
returnedLines: 0,
596+
totalLines: 5,
597+
wasTruncated: false,
598+
includedRanges: [],
599+
})
600+
601+
await readFileTool.execute(
602+
{ path: "test.ts", mode: "slice", offset: 100 },
603+
mockTask as unknown as Task,
604+
callbacks,
605+
)
606+
607+
expect(mockedReadWithSlice).toHaveBeenCalledWith(expect.any(String), 99, expect.any(Number))
608+
expect(callbacks.pushToolResult).toHaveBeenCalledWith(expect.stringContaining("beyond file end"))
609+
expect(callbacks.pushToolResult).not.toHaveBeenCalledWith(expect.stringContaining("Note: File is empty"))
610+
})
611+
612+
// Integration counterpart to the mocked case above: the real readWithSlice
613+
// producer runs against a real file, so the error text and its 1-based
614+
// offset are proven end-to-end instead of being asserted against a stub.
615+
it("should propagate the real beyond-EOF error from the producer for a real file", async () => {
616+
const realFs = await vi.importActual<typeof import("fs/promises")>("fs/promises")
617+
const realPath = await vi.importActual<typeof import("path")>("path")
618+
const tempDir = await realFs.mkdtemp(realPath.join(os.tmpdir(), "read-file-beyond-eof-"))
619+
const fileName = "sample.txt"
620+
await realFs.writeFile(realPath.join(tempDir, fileName), "line1\nline2\nline3\nline4\nline5", "utf8")
621+
622+
vi.doUnmock("fs/promises")
623+
vi.doUnmock("path")
624+
vi.doUnmock("isbinaryfile")
625+
vi.doUnmock("../../../integrations/misc/indentation-reader")
626+
vi.resetModules()
627+
628+
try {
629+
const { ReadFileTool } = await import("../ReadFileTool")
630+
const mockTask = { ...createMockTask(), cwd: tempDir }
631+
const callbacks = createMockCallbacks()
632+
633+
await new ReadFileTool().execute(
634+
{ path: fileName, mode: "slice", offset: 100 },
635+
mockTask as unknown as Task,
636+
callbacks,
637+
)
638+
639+
expect(mockedReadWithSlice).not.toHaveBeenCalled()
640+
expect(callbacks.pushToolResult).toHaveBeenCalledTimes(1)
641+
expect(callbacks.pushToolResult).toHaveBeenCalledWith(
642+
expect.stringContaining("Error: offset 100 is beyond file end (5 lines)"),
643+
)
644+
expect(callbacks.pushToolResult).not.toHaveBeenCalledWith(
645+
expect.stringContaining("Note: File is empty"),
646+
)
647+
} finally {
648+
vi.resetModules()
649+
await realFs.rm(tempDir, { recursive: true, force: true }).catch(() => {})
650+
}
585651
})
586652
})
587653

‎src/integrations/misc/__tests__/indentation-reader-unicode.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ describe("Unicode clipping through the existing readers", () => {
127127

128128
it("preserves reader errors without formatting or leaking the long line", () => {
129129
expect(readWithSlice(longLine, 1)).toEqual({
130-
content: "Error: offset 1 is beyond file end (1 lines)",
130+
content: "Error: offset 2 is beyond file end (1 lines)",
131131
includedRanges: [],
132132
totalLines: 1,
133133
returnedLines: 0,

‎src/integrations/misc/__tests__/indentation-reader.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ describe("readWithSlice", () => {
282282
const result = readWithSlice(SIMPLE_CODE, 1000, 10)
283283

284284
expect(result.returnedLines).toBe(0)
285-
expect(result.content).toContain("Error")
285+
expect(result.content).toContain("Error: offset 1001 is beyond file end")
286286
})
287287

288288
it("should handle negative offset", () => {

‎src/integrations/misc/indentation-reader.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ export function readWithSlice(
381381
if (offset < 0) offset = 0
382382
if (offset >= totalLines) {
383383
return {
384-
content: `Error: offset ${offset} is beyond file end (${totalLines} lines)`,
384+
content: `Error: offset ${offset + 1} is beyond file end (${totalLines} lines)`,
385385
includedRanges: [],
386386
totalLines,
387387
returnedLines: 0,

0 commit comments

Comments
 (0)