From e072ffc91f92926691762accc25bbbcae3124d92 Mon Sep 17 00:00:00 2001 From: Hannes Rudolph Date: Sun, 21 Dec 2025 19:22:57 -0700 Subject: [PATCH] fix(read_file): empty files should not report hasMoreBefore: true in indentation mode When totalLinesInFile is 0, there are no lines before the anchor point, so hasMoreBefore must be false regardless of the offset/anchorLine value. --- .../misc/__tests__/read-file-content.spec.ts | 27 ++++++++++++++++++- src/integrations/misc/read-file-content.ts | 2 +- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/integrations/misc/__tests__/read-file-content.spec.ts b/src/integrations/misc/__tests__/read-file-content.spec.ts index 2526908ff6..74b4256be7 100644 --- a/src/integrations/misc/__tests__/read-file-content.spec.ts +++ b/src/integrations/misc/__tests__/read-file-content.spec.ts @@ -214,7 +214,7 @@ describe("read-file-content", () => { const codeWithComment = `# This is a comment header # describing the function def my_function(): - return 42` + return 42` await withTempFile("indent-header-test.py", codeWithComment, async (filepath) => { const result = await readIndentationBlock(filepath, 3, 100, { anchorLine: 3, @@ -224,6 +224,31 @@ def my_function(): expect(result.content).toContain("# This is a comment header") }) }) + + it("should report hasMoreBefore as false for empty files", async () => { + await withTempFile("indent-empty-test.txt", "", async (filepath) => { + const result = await readIndentationBlock(filepath, 1, 100) + expect(result.content).toBe("") + expect(result.lineCount).toBe(0) + expect(result.totalLines).toBe(0) + expect(result.metadata.totalLinesInFile).toBe(0) + expect(result.metadata.hasMoreBefore).toBe(false) + expect(result.metadata.hasMoreAfter).toBe(false) + }) + }) + + it("should report hasMoreBefore as false for empty files even with offset > 1", async () => { + await withTempFile("indent-empty-offset-test.txt", "", async (filepath) => { + const result = await readIndentationBlock(filepath, 5, 100) + expect(result.content).toBe("") + expect(result.lineCount).toBe(0) + expect(result.totalLines).toBe(0) + expect(result.metadata.totalLinesInFile).toBe(0) + // Even though offset > 1, there are no lines in an empty file + expect(result.metadata.hasMoreBefore).toBe(false) + expect(result.metadata.hasMoreAfter).toBe(false) + }) + }) }) describe("readFileContent", () => { diff --git a/src/integrations/misc/read-file-content.ts b/src/integrations/misc/read-file-content.ts index 38301a83b5..91fc8e22b4 100644 --- a/src/integrations/misc/read-file-content.ts +++ b/src/integrations/misc/read-file-content.ts @@ -382,7 +382,7 @@ export async function readIndentationBlock( linesReturned: 0, startLine: anchorLine, endLine: anchorLine, - hasMoreBefore: anchorLine > 1, + hasMoreBefore: false, // Empty file has no lines before hasMoreAfter: false, linesBeforeStart: 0, linesAfterEnd: 0,