Skip to content

Commit 8741c78

Browse files
committed
fix: keep streamed read path empty until available (Zoo-Code-Org#1948)
1 parent 2baac5e commit 8741c78

3 files changed

Lines changed: 78 additions & 12 deletions

File tree

‎src/core/tools/ReadFileTool.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ export class ReadFileTool extends BaseTool<"read_file"> {
650650
const fullPath = filePath ? path.resolve(task.cwd, filePath) : ""
651651
const sharedMessageProps: ClineSayTool = {
652652
tool: "readFile",
653-
path: getReadablePath(task.cwd, filePath),
653+
path: filePath ? getReadablePath(task.cwd, filePath) : "",
654654
isOutsideWorkspace: filePath ? isPathOutsideWorkspace(fullPath) : false,
655655
}
656656
const partialMessage = JSON.stringify({

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

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
import path from "path"
1616

17+
import type { ToolUse } from "../../../shared/tools"
18+
import type { Task } from "../../task/Task"
19+
1720
import { isBinaryFile } from "isbinaryfile"
1821

1922
import { readFileTool, ReadFileTool } from "../ReadFileTool"
@@ -987,19 +990,82 @@ describe("ReadFileTool", () => {
987990
})
988991

989992
describe("handlePartial", () => {
990-
it("should handle partial display for new format", async () => {
993+
it.each([{}, { path: "" }, undefined])(
994+
"renders streamed native arguments before their path arrives: %j",
995+
async (nativeArgs) => {
996+
const mockTask = createMockTask()
997+
mockTask.cwd = path.resolve(mockTask.cwd)
998+
const streamedBlock = {
999+
type: "tool_use",
1000+
name: "read_file",
1001+
params: {},
1002+
nativeArgs,
1003+
partial: true,
1004+
}
1005+
1006+
// Streaming JSON is incomplete even though finalized native arguments require a path.
1007+
await Reflect.apply(readFileTool.handlePartial, readFileTool, [mockTask, streamedBlock])
1008+
1009+
expect(mockTask.ask).toHaveBeenCalledExactlyOnceWith(
1010+
"tool",
1011+
JSON.stringify({ tool: "readFile", path: "", isOutsideWorkspace: false }),
1012+
true,
1013+
)
1014+
expect(mockedFsReadFile).not.toHaveBeenCalled()
1015+
expect(mockedFsStat).not.toHaveBeenCalled()
1016+
expect(mockedExtractTextFromFile).not.toHaveBeenCalled()
1017+
},
1018+
)
1019+
1020+
it("renders incomplete legacy arguments without a file entry or outside-workspace warning", async () => {
9911021
const mockTask = createMockTask()
992-
mockTask.ask.mockResolvedValue({ response: "yesButtonClicked", text: undefined, images: undefined })
993-
994-
const block = {
995-
nativeArgs: { path: "src/app.ts" },
1022+
mockTask.cwd = path.resolve(mockTask.cwd)
1023+
const block: ToolUse<"read_file"> = {
1024+
type: "tool_use",
1025+
name: "read_file",
1026+
params: {},
1027+
nativeArgs: { files: [], _legacyFormat: true },
9961028
partial: true,
9971029
}
9981030

999-
await readFileTool.handlePartial(mockTask as any, block as any)
1031+
// This reader-boundary double omits Task fields unrelated to file reading.
1032+
await readFileTool.handlePartial(mockTask as unknown as Task, block)
10001033

1001-
expect(mockTask.ask).toHaveBeenCalledWith("tool", expect.stringContaining("readFile"), true)
1002-
})
1034+
expect(mockTask.ask).toHaveBeenCalledExactlyOnceWith(
1035+
"tool",
1036+
JSON.stringify({ tool: "readFile", path: "", isOutsideWorkspace: false }),
1037+
true,
1038+
)
1039+
expect(mockedFsReadFile).not.toHaveBeenCalled()
1040+
expect(mockedFsStat).not.toHaveBeenCalled()
1041+
expect(mockedExtractTextFromFile).not.toHaveBeenCalled()
1042+
})
1043+
1044+
it.each(["src/app.ts", "src/файл.ts", "src/e\u0301-📚.ts"])(
1045+
"should preserve partial display for %s",
1046+
async (filePath) => {
1047+
const mockTask = createMockTask()
1048+
mockTask.cwd = path.resolve(mockTask.cwd)
1049+
mockTask.ask.mockResolvedValue({ response: "yesButtonClicked", text: undefined, images: undefined })
1050+
1051+
const block: ToolUse<"read_file"> = {
1052+
type: "tool_use",
1053+
name: "read_file",
1054+
params: {},
1055+
nativeArgs: { path: filePath },
1056+
partial: true,
1057+
}
1058+
1059+
// This reader-boundary double omits Task fields unrelated to file reading.
1060+
await readFileTool.handlePartial(mockTask as unknown as Task, block)
1061+
1062+
expect(mockTask.ask).toHaveBeenCalledExactlyOnceWith(
1063+
"tool",
1064+
JSON.stringify({ tool: "readFile", path: filePath, isOutsideWorkspace: true }),
1065+
true,
1066+
)
1067+
},
1068+
)
10031069

10041070
it("should handle partial display for legacy format", async () => {
10051071
const mockTask = createMockTask()
@@ -1043,9 +1109,9 @@ describe("ReadFileTool", () => {
10431109
expect(mockTask.ask).toHaveBeenCalled()
10441110
})
10451111

1046-
it("should gracefully handle ask rejection in partial", async () => {
1112+
it.each(["Cancelled", "superseded"])("should gracefully handle %s ask rejection in partial", async (reason) => {
10471113
const mockTask = createMockTask()
1048-
mockTask.ask.mockRejectedValue(new Error("Cancelled"))
1114+
mockTask.ask.mockRejectedValue(new Error(reason))
10491115

10501116
const block = {
10511117
nativeArgs: { path: "test.ts" },

‎src/eslint-suppressions.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@
976976
},
977977
"core/tools/__tests__/readFileTool.spec.ts": {
978978
"@typescript-eslint/no-explicit-any": {
979-
"count": 98
979+
"count": 96
980980
}
981981
},
982982
"core/tools/__tests__/runSlashCommandTool.spec.ts": {

0 commit comments

Comments
 (0)