File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -34,7 +34,18 @@ export class FileStateCache {
3434 this . cache = new LRUCache < string , FileState > ( {
3535 max : maxEntries ,
3636 maxSize : maxSizeBytes ,
37- sizeCalculation : value => Math . max ( 1 , Buffer . byteLength ( value . content ) ) ,
37+ sizeCalculation : value => {
38+ const c = value . content
39+ const s =
40+ typeof c === 'string'
41+ ? c
42+ : c === null || c === undefined
43+ ? ''
44+ : typeof c === 'object'
45+ ? JSON . stringify ( c )
46+ : String ( c )
47+ return Math . max ( 1 , Buffer . byteLength ( s , 'utf8' ) )
48+ } ,
3849 } )
3950 }
4051
Original file line number Diff line number Diff line change @@ -45,6 +45,14 @@ export type PermissionPromptTool = Tool<
4545// during permission prompts or limited tool operations
4646const ASK_READ_FILE_STATE_CACHE_SIZE = 10
4747
48+ /** Transcript JSON may deserialize Write tool `content` as a nested object — LRU needs strings. */
49+ function coerceToolContentToString ( value : unknown ) : string {
50+ if ( typeof value === 'string' ) return value
51+ if ( value === null || value === undefined ) return ''
52+ if ( typeof value === 'object' ) return JSON . stringify ( value )
53+ return String ( value )
54+ }
55+
4856/**
4957 * Checks if the result should be considered successful based on the last message.
5058 * Returns true if:
@@ -402,14 +410,18 @@ export function extractReadFilesFromMessages(
402410 ) {
403411 // Extract file_path and content from the Write tool use input
404412 const input = content . input as
405- | { file_path ?: string ; content ?: string }
413+ | { file_path ?: string ; content ?: unknown }
406414 | undefined
407- if ( input ?. file_path && input ?. content ) {
415+ if (
416+ input ?. file_path &&
417+ input . content !== undefined &&
418+ input . content !== null
419+ ) {
408420 // Normalize to absolute path for consistent cache lookups
409421 const absolutePath = expandPath ( input . file_path , cwd )
410422 fileWriteToolUseIds . set ( content . id , {
411423 filePath : absolutePath ,
412- content : input . content ,
424+ content : coerceToolContentToString ( input . content ) ,
413425 } )
414426 }
415427 } else if (
You can’t perform that action at this time.
0 commit comments