Skip to content

Commit 0067f0a

Browse files
committed
refactor(read-file): isolate reusable reader foundation (Zoo-Code-Org#1948)
Build on the extracted Unicode clipping, shared image MIME guard, and streamed path fixes from PRs Zoo-Code-Org#1960, Zoo-Code-Org#1961, and Zoo-Code-Org#1962. Preserve their latest regression coverage while composing reader strategies, descriptor-bound access, approval, cancellation, and validated text/document results.
1 parent cde93bb commit 0067f0a

43 files changed

Lines changed: 6659 additions & 2456 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/api/providers/__tests__/base-provider.spec.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ModelInfo } from "@roo-code/types"
55
import { BaseProvider } from "../base-provider"
66
import type { ApiStream } from "../../transform/stream"
77
import { expectNoLoneSurrogates } from "../../../test-utils/surrogates"
8+
import { createReadFileTool } from "../../../core/prompts/tools/native-tools/read_file"
89

910
// Create a concrete implementation for testing
1011
class TestProvider extends BaseProvider {
@@ -270,6 +271,37 @@ describe("BaseProvider", () => {
270271
expect(result.properties.only.type).toBe("null")
271272
})
272273
describe("convertToolsForOpenAI", () => {
274+
it("preserves read_file integer minima while requiring properties for strict mode", () => {
275+
const result = provider.testConvertToolsForOpenAI([createReadFileTool()])
276+
277+
expect(result?.[0]).toMatchObject({
278+
function: {
279+
strict: true,
280+
parameters: {
281+
required: ["path", "mode", "offset", "limit", "indentation"],
282+
properties: {
283+
offset: { type: "integer", minimum: 1 },
284+
limit: { type: "integer", minimum: 1 },
285+
indentation: {
286+
required: [
287+
"anchor_line",
288+
"max_levels",
289+
"include_siblings",
290+
"include_header",
291+
"max_lines",
292+
],
293+
properties: {
294+
anchor_line: { type: "integer", minimum: 1 },
295+
max_levels: { type: "integer", minimum: 0 },
296+
max_lines: { type: "integer", minimum: 1 },
297+
},
298+
},
299+
},
300+
},
301+
},
302+
})
303+
})
304+
273305
it("should return undefined for undefined input", () => {
274306
const result = provider.testConvertToolsForOpenAI(undefined)
275307
expect(result).toBeUndefined()

‎src/core/assistant-message/NativeToolCallParser.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
toolParamNames,
1212
} from "../../shared/tools"
1313
import { resolveToolAlias } from "../prompts/tools/filter-tools-for-mode"
14+
import { validateReadFileNumbers } from "../tools/file-reading/readFileValidation"
1415
import type {
1516
ApiStreamToolCallStartChunk,
1617
ApiStreamToolCallDeltaChunk,
@@ -779,6 +780,11 @@ export class NativeToolCallParser {
779780
}
780781
// New format: { path: "...", mode: "..." }
781782
if (!nativeArgs && args.path !== undefined) {
783+
const validationError = validateReadFileNumbers(args)
784+
if (validationError) {
785+
console.error(`Invalid read_file parameters: ${validationError}`)
786+
return null
787+
}
782788
nativeArgs = {
783789
path: args.path,
784790
mode: args.mode,

‎src/core/assistant-message/__tests__/NativeToolCallParser.spec.ts‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,82 @@ import { NativeToolCallParser, type ToolCallStreamEvent } from "../NativeToolCal
33
describe("NativeToolCallParser", () => {
44
describe("parseToolCall", () => {
55
describe("read_file tool", () => {
6+
it("rejects a zero limit instead of constructing successful read arguments", () => {
7+
const result = NativeToolCallParser.parseToolCall({
8+
id: "invalid_limit",
9+
name: "read_file",
10+
arguments: JSON.stringify({ path: "test.ts", limit: 0 }),
11+
})
12+
13+
expect(result).toBeNull()
14+
})
15+
16+
it.each(
17+
["offset", "limit", "anchor_line", "max_levels", "max_lines"].flatMap((name) =>
18+
[-1, 1.5, "1", "NaN", true, [], {}].map((value) => ({ name, value })),
19+
),
20+
)("rejects malformed supplied $name=$value before coercion", ({ name, value }) => {
21+
const args =
22+
name === "offset" || name === "limit"
23+
? { path: "test.ts", [name]: value }
24+
: { path: "test.ts", mode: "slice", indentation: { [name]: value } }
25+
26+
expect(
27+
NativeToolCallParser.parseToolCall({
28+
id: "invalid_number",
29+
name: "read_file",
30+
arguments: JSON.stringify(args),
31+
}),
32+
).toBeNull()
33+
})
34+
35+
it.each(["offset", "limit", "anchor_line", "max_lines"])("rejects zero for positive integer %s", (name) => {
36+
const args =
37+
name === "offset" || name === "limit"
38+
? { path: "test.ts", [name]: 0 }
39+
: { path: "test.ts", indentation: { [name]: 0 } }
40+
41+
expect(
42+
NativeToolCallParser.parseToolCall({
43+
id: "invalid_zero",
44+
name: "read_file",
45+
arguments: JSON.stringify(args),
46+
}),
47+
).toBeNull()
48+
})
49+
50+
it("rejects a nonfinite number produced by JSON exponent overflow", () => {
51+
expect(
52+
NativeToolCallParser.parseToolCall({
53+
id: "nonfinite",
54+
name: "read_file",
55+
arguments: '{"path":"test.ts","limit":1e309}',
56+
}),
57+
).toBeNull()
58+
})
59+
60+
it.each([
61+
{ offset: 1, limit: 1, indentation: { anchor_line: 1, max_levels: 0, max_lines: 1 } },
62+
{ offset: null, limit: null, indentation: { anchor_line: null, max_levels: null, max_lines: null } },
63+
{ indentation: null },
64+
])("keeps valid boundary values and null-as-unset numbers: %j", (numbers) => {
65+
const result = NativeToolCallParser.parseToolCall({
66+
id: "valid_numbers",
67+
name: "read_file",
68+
arguments: JSON.stringify({ path: "test.ts", ...numbers }),
69+
})
70+
71+
expect(result).toMatchObject({ type: "tool_use", nativeArgs: { path: "test.ts" } })
72+
if (result?.type !== "tool_use" || !result.nativeArgs || "files" in result.nativeArgs) {
73+
throw new Error("Expected modern read_file arguments")
74+
}
75+
expect(result.nativeArgs.offset).toBe(numbers.offset ?? undefined)
76+
expect(result.nativeArgs.limit).toBe(numbers.limit ?? undefined)
77+
expect(result.nativeArgs.indentation?.anchor_line).toBe(numbers.indentation?.anchor_line ?? undefined)
78+
expect(result.nativeArgs.indentation?.max_levels).toBe(numbers.indentation?.max_levels ?? undefined)
79+
expect(result.nativeArgs.indentation?.max_lines).toBe(numbers.indentation?.max_lines ?? undefined)
80+
})
81+
682
it("should parse minimal single-file read_file args", () => {
783
const toolCall = {
884
id: "toolu_123",

‎src/core/assistant-message/__tests__/presentAssistantMessage-tool-usage-attribution.spec.ts‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Anthropic } from "@anthropic-ai/sdk"
44
import { describe, it, expect, beforeEach, vi, type Mock } from "vitest"
55
import { presentAssistantMessage } from "../presentAssistantMessage"
66
import { validateToolUse } from "../../tools/validateToolUse"
7+
import { ReadFileTool } from "../../tools/file-reading/ReadFileTool"
78
import { getModeBySlug } from "../../../shared/modes"
89
import type { Task } from "../../task/Task"
910

@@ -155,6 +156,127 @@ describe("presentAssistantMessage - tool usage attribution", () => {
155156
expect(TelemetryService.instance.captureToolUsage).toHaveBeenCalledWith(mockTask.taskId, "read_file")
156157
})
157158

159+
describe("shared read_file tool", () => {
160+
it("reuses one instance for partial and complete calls across tasks", async () => {
161+
const handle = vi.spyOn(ReadFileTool.prototype, "handle").mockResolvedValue(undefined)
162+
const otherTask = { ...mockTask, taskId: "other-task-id", instanceId: "other-instance" }
163+
164+
try {
165+
for (const task of [mockTask, otherTask]) {
166+
for (const partial of [true, false]) {
167+
task.assistantMessageContent = [
168+
{
169+
type: "tool_use",
170+
id: `call_${task.taskId}`,
171+
name: "read_file",
172+
params: { path: "test.txt" },
173+
nativeArgs: { path: "test.txt" },
174+
partial,
175+
},
176+
]
177+
178+
// This dispatch double omits Task fields unrelated to tool presentation.
179+
await presentAssistantMessage(task as unknown as Task)
180+
}
181+
}
182+
183+
expect(handle).toHaveBeenCalledTimes(4)
184+
const sharedInstance = handle.mock.contexts[0]
185+
expect(sharedInstance).toBeInstanceOf(ReadFileTool)
186+
for (const instance of handle.mock.contexts) {
187+
expect(instance).toBe(sharedInstance)
188+
}
189+
} finally {
190+
handle.mockRestore()
191+
}
192+
})
193+
194+
it("preserves the injected reader when recursively processing tool calls", async () => {
195+
const reader = {
196+
handle: vi.fn<ReadFileTool["handle"]>().mockResolvedValue(undefined),
197+
getReadFileToolDescription: vi.fn(() => ""),
198+
}
199+
mockTask.assistantMessageContent = ["first.txt", "second.txt"].map((path, index) => ({
200+
type: "tool_use",
201+
id: `call_read_${index}`,
202+
name: "read_file",
203+
params: { path },
204+
nativeArgs: { path },
205+
partial: false,
206+
}))
207+
208+
// This dispatch double omits Task fields unrelated to tool presentation.
209+
await presentAssistantMessage(mockTask as unknown as Task, reader)
210+
211+
expect(reader.handle).toHaveBeenCalledTimes(2)
212+
for (const instance of reader.handle.mock.contexts) {
213+
expect(instance).toBe(reader)
214+
}
215+
})
216+
217+
it("preserves the injected reader when processing pending streaming updates", async () => {
218+
const reader = {
219+
handle: vi
220+
.fn<ReadFileTool["handle"]>()
221+
.mockResolvedValue(undefined)
222+
.mockImplementationOnce(async (task) => {
223+
task.presentAssistantMessageHasPendingUpdates = true
224+
}),
225+
getReadFileToolDescription: vi.fn(() => ""),
226+
}
227+
mockTask.assistantMessageContent = [
228+
{
229+
type: "tool_use",
230+
id: "call_streaming_read",
231+
name: "read_file",
232+
params: { path: "test.txt" },
233+
nativeArgs: { path: "test.txt" },
234+
partial: true,
235+
},
236+
]
237+
238+
// This dispatch double omits Task fields unrelated to tool presentation.
239+
await presentAssistantMessage(mockTask as unknown as Task, reader)
240+
241+
expect(reader.handle).toHaveBeenCalledTimes(2)
242+
for (const instance of reader.handle.mock.contexts) {
243+
expect(instance).toBe(reader)
244+
}
245+
})
246+
247+
it.each([
248+
{ nativeArgs: { path: "native.txt" }, expectedPath: "native.txt" },
249+
{ nativeArgs: undefined, expectedPath: "legacy.txt" },
250+
])("uses the injected reader to describe $expectedPath", async ({ nativeArgs, expectedPath }) => {
251+
const reader = new ReadFileTool()
252+
const describeTool = vi.spyOn(reader, "getReadFileToolDescription")
253+
mockTask.didRejectTool = true
254+
mockTask.assistantMessageContent = [
255+
{
256+
type: "tool_use",
257+
id: "call_rejected_read",
258+
name: "read_file",
259+
params: { path: "legacy.txt" },
260+
nativeArgs,
261+
partial: false,
262+
},
263+
]
264+
265+
try {
266+
// This dispatch double omits Task fields unrelated to tool presentation.
267+
await presentAssistantMessage(mockTask as unknown as Task, reader)
268+
269+
expect(describeTool).toHaveBeenCalledExactlyOnceWith("read_file", { path: expectedPath })
270+
expect(describeTool.mock.contexts[0]).toBe(reader)
271+
expect(mockTask.pushToolResultToUserContent).toHaveBeenCalledWith(
272+
expect.objectContaining({ content: expect.stringContaining(expectedPath) }),
273+
)
274+
} finally {
275+
describeTool.mockRestore()
276+
}
277+
})
278+
})
279+
158280
it("records a valid dynamic mcp_ tool name as use_mcp_tool", async () => {
159281
mockTask.assistantMessageContent = [
160282
{

‎src/core/assistant-message/presentAssistantMessage.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { AskIgnoredError } from "../task/AskIgnoredError"
1717
import { Task } from "../task/Task"
1818

1919
import { listFilesTool } from "../tools/ListFilesTool"
20-
import { readFileTool } from "../tools/ReadFileTool"
20+
import { ReadFileTool } from "../tools/file-reading/ReadFileTool"
2121
import { readCommandOutputTool } from "../tools/ReadCommandOutputTool"
2222
import { writeToFileTool } from "../tools/WriteToFileTool"
2323
import { editTool } from "../tools/EditTool"
@@ -44,6 +44,8 @@ import { codebaseSearchTool } from "../tools/CodebaseSearchTool"
4444
import { formatResponse } from "../prompts/responses"
4545
import { sanitizeToolUseId } from "../../utils/tool-id"
4646

47+
const defaultReadFileTool = new ReadFileTool()
48+
4749
/**
4850
* Maps a raw, potentially model-controlled tool name to a safe analytics key.
4951
* Never returns the raw name unless it is a known static tool, so an
@@ -86,7 +88,10 @@ export function toTelemetryToolName(
8688
* as it becomes available.
8789
*/
8890

89-
export async function presentAssistantMessage(cline: Task) {
91+
export async function presentAssistantMessage(
92+
cline: Task,
93+
readFileTool: Pick<ReadFileTool, "handle" | "getReadFileToolDescription"> = defaultReadFileTool,
94+
) {
9095
if (cline.abort) {
9196
return
9297
}
@@ -1115,7 +1120,7 @@ export async function presentAssistantMessage(cline: Task) {
11151120
if (cline.currentStreamingContentIndex < cline.assistantMessageContent.length) {
11161121
// There are already more content blocks to stream, so we'll call
11171122
// this function ourselves.
1118-
return presentAssistantMessage(cline)
1123+
return presentAssistantMessage(cline, readFileTool)
11191124
} else {
11201125
// CRITICAL FIX: If we're out of bounds and the stream is complete, set userMessageContentReady
11211126
// This handles the case where assistantMessageContent is empty or becomes empty after processing
@@ -1127,7 +1132,7 @@ export async function presentAssistantMessage(cline: Task) {
11271132

11281133
// Block is partial, but the read stream may have finished.
11291134
if (cline.presentAssistantMessageHasPendingUpdates) {
1130-
return presentAssistantMessage(cline)
1135+
return presentAssistantMessage(cline, readFileTool)
11311136
}
11321137
}
11331138

‎src/core/mentions/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { getCommitInfo, getWorkingState } from "../../utils/git"
1111
import { openFile } from "../../integrations/misc/open-file"
1212
import { extractTextFromFileWithMetadata, type ExtractTextResult } from "../../integrations/misc/extract-text"
1313
import { diagnosticsToProblemsString } from "../../integrations/diagnostics"
14-
import { DEFAULT_LINE_LIMIT } from "../prompts/tools/native-tools/read_file"
14+
import { DEFAULT_LINE_LIMIT } from "../tools/file-reading/readFileConstants"
1515

1616
import { FileContextTracker } from "../context-tracking/FileContextTracker"
1717

0 commit comments

Comments
 (0)