diff --git a/src/core/tools/__tests__/readFileTool.spec.ts b/src/core/tools/__tests__/readFileTool.spec.ts index 44be1d3b92..ce91ef3d2a 100644 --- a/src/core/tools/__tests__/readFileTool.spec.ts +++ b/src/core/tools/__tests__/readFileTool.spec.ts @@ -481,6 +481,21 @@ describe("read_file tool XML output structure", () => { `\n${testFilePath}\nFile is empty\n\n`, ) }) + + it("should treat files with only whitespace as empty", async () => { + // Setup - file has lines but only whitespace content + mockedCountFileLines.mockResolvedValue(3) // File has 3 lines + mockedExtractTextFromFile.mockResolvedValue(" \n\t\n ") // Only whitespace + mockProvider.getState.mockResolvedValue({ maxReadFileLine: -1 }) + + // Execute + const result = await executeReadFileTool({}, { totalLines: 3 }) + + // Verify - should show empty file notice even though totalLines > 0 + expect(result).toBe( + `\n${testFilePath}\nFile is empty\n\n`, + ) + }) }) describe("Error Handling Tests", () => { diff --git a/src/core/tools/readFileTool.ts b/src/core/tools/readFileTool.ts index 6de8dd5642..5c83d375ca 100644 --- a/src/core/tools/readFileTool.ts +++ b/src/core/tools/readFileTool.ts @@ -519,9 +519,13 @@ export async function readFileTool( // Handle normal file read const content = await extractTextFromFile(fullPath) const lineRangeAttr = ` lines="1-${totalLines}"` - let xmlInfo = totalLines > 0 ? `\n${content}\n` : `` - if (totalLines === 0) { + // Check if file is effectively empty (no content or only whitespace) + const isEffectivelyEmpty = totalLines === 0 || content.trim() === "" + + let xmlInfo = !isEffectivelyEmpty ? `\n${content}\n` : `` + + if (isEffectivelyEmpty) { xmlInfo += `File is empty\n` }