Skip to content

Commit 918defa

Browse files
Merge pull request #127 from xutianyi1999/main
fix: improve size calculation for LRU cache and handle nested object …
2 parents 4e5a0dd + 354c11f commit 918defa

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

src/utils/fileStateCache.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff 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

src/utils/queryHelpers.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ export type PermissionPromptTool = Tool<
4545
// during permission prompts or limited tool operations
4646
const 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 (

0 commit comments

Comments
 (0)