diff --git a/src/utils/fileStateCache.ts b/src/utils/fileStateCache.ts index c7bb2f1dd..165b3fa1d 100644 --- a/src/utils/fileStateCache.ts +++ b/src/utils/fileStateCache.ts @@ -34,7 +34,18 @@ export class FileStateCache { this.cache = new LRUCache({ max: maxEntries, maxSize: maxSizeBytes, - sizeCalculation: value => Math.max(1, Buffer.byteLength(value.content)), + sizeCalculation: value => { + const c = value.content + const s = + typeof c === 'string' + ? c + : c === null || c === undefined + ? '' + : typeof c === 'object' + ? JSON.stringify(c) + : String(c) + return Math.max(1, Buffer.byteLength(s, 'utf8')) + }, }) } diff --git a/src/utils/queryHelpers.ts b/src/utils/queryHelpers.ts index 520588b54..c1c82bb6e 100644 --- a/src/utils/queryHelpers.ts +++ b/src/utils/queryHelpers.ts @@ -45,6 +45,14 @@ export type PermissionPromptTool = Tool< // during permission prompts or limited tool operations const ASK_READ_FILE_STATE_CACHE_SIZE = 10 +/** Transcript JSON may deserialize Write tool `content` as a nested object — LRU needs strings. */ +function coerceToolContentToString(value: unknown): string { + if (typeof value === 'string') return value + if (value === null || value === undefined) return '' + if (typeof value === 'object') return JSON.stringify(value) + return String(value) +} + /** * Checks if the result should be considered successful based on the last message. * Returns true if: @@ -402,14 +410,18 @@ export function extractReadFilesFromMessages( ) { // Extract file_path and content from the Write tool use input const input = content.input as - | { file_path?: string; content?: string } + | { file_path?: string; content?: unknown } | undefined - if (input?.file_path && input?.content) { + if ( + input?.file_path && + input.content !== undefined && + input.content !== null + ) { // Normalize to absolute path for consistent cache lookups const absolutePath = expandPath(input.file_path, cwd) fileWriteToolUseIds.set(content.id, { filePath: absolutePath, - content: input.content, + content: coerceToolContentToString(input.content), }) } } else if (