@@ -4,6 +4,7 @@ import type { Anthropic } from "@anthropic-ai/sdk"
44import { describe , it , expect , beforeEach , vi , type Mock } from "vitest"
55import { presentAssistantMessage } from "../presentAssistantMessage"
66import { validateToolUse } from "../../tools/validateToolUse"
7+ import { ReadFileTool } from "../../tools/file-reading/ReadFileTool"
78import { getModeBySlug } from "../../../shared/modes"
89import 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 {
0 commit comments