fix(core): add in-memory cache to ChatRecordingService to prevent OOM#21502
fix(core): add in-memory cache to ChatRecordingService to prevent OOM#21502SandyTao520 merged 5 commits intomainfrom
Conversation
The updateConversation hot path performed disk read + JSON.parse + double JSON.stringify on every call, including during streaming chunk processing via recordMessageTokens. In long sessions (~53 min) with large conversation histories, this caused heap exhaustion at ~4GB. Changes: - Cache ConversationRecord in memory, eliminating redundant disk reads - Remove comparison JSON.stringify in writeConversation (single serialize) - Skip all disk I/O in recordMessageTokens when just queuing tokens
|
Hi @SandyTao520, thank you so much for your contribution to Gemini CLI! We really appreciate the time and effort you've put into this. We're making some updates to our contribution process to improve how we track and review changes. Please take a moment to review our recent discussion post: Improving Our Contribution Process & Introducing New Guidelines. Key Update: Starting January 26, 2026, the Gemini CLI project will require all pull requests to be associated with an existing issue. Any pull requests not linked to an issue by that date will be automatically closed. Thank you for your understanding and for being a part of our community! |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses an Out-Of-Memory (OOM) crash occurring in long-running chat sessions due to inefficient disk I/O and excessive JSON serialization within the Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively addresses the Out-of-Memory issue in ChatRecordingService by introducing an in-memory cache for the conversation record. The changes correctly eliminate redundant disk reads and expensive JSON serializations, especially during token streaming, which was the primary cause of the problem. The new tests you've added provide good coverage for the caching logic and performance improvements.
I've found one critical edge case where a corrupted session file could lead to a runtime crash. My review includes a specific comment with a suggestion to handle this gracefully.
|
Size Change: +1.1 kB (0%) Total Size: 26 MB
ℹ️ View Unchanged
|
| this.cachedConversation = conversation; | ||
| conversation.lastUpdated = new Date().toISOString(); | ||
| const newContent = JSON.stringify(conversation, null, 2); | ||
| this.cachedLastConvData = newContent; |
There was a problem hiding this comment.
wouldn't we still want to compare if they are equal so we could skip writing if they are same?
There was a problem hiding this comment.
writeConversation is now only called by code paths that have already mutated the conversation object (e.g. recordMessage, recordToolCalls, recordMessageTokens when it actually sets tokens on the last message). The queuing-only path in recordMessageTokens no longer calls writeConversation at all. So the comparison would always find a diff and never skip.
Add null check after JSON.parse to gracefully handle corrupted session files (e.g. containing "null"), falling back to an empty conversation instead of crashing via non-null assertion.
Restore the comparison check to skip disk writes when the conversation was not actually mutated (e.g. updateMessagesFromHistory with no matching tool calls). Compare the serialized content against the cached string before updating lastUpdated to prevent false diffs from timestamp changes.
- Add doc comment on readConversation noting the returned object is the live cache reference and mutations affect future reads. - Move cachedConversation assignment after the comparison check in writeConversation to avoid unnecessary reference swap on no-op path.
…-oom-memory-cache
Summary
Fix OOM crash in long-running sessions (~53 min) caused by
ChatRecordingServiceperforming expensive disk I/O and doubleJSON.stringifyon every streaming chunk viarecordMessageTokens.Details
The
updateConversationhot path executed on every call (including per-chunk during streaming):fs.readFileSync— full conversation JSON from diskJSON.parse— parse entire string into objectsJSON.stringifydocs: Add setup instructions for API key to README #1 — serialize for comparison checkJSON.stringifyImprove readability issues #2 — serialize again for writingfs.writeFileSync— write to diskIn long sessions with large tool outputs (file reads, grep results, command outputs), the conversation object grows to hundreds of MBs. The repeated full serialization exhausts the 4GB V8 heap.
Changes
cachedConversation): Eliminates redundantfs.readFileSync+JSON.parseon every update. After the first read, all subsequent operations use the cached object.JSON.stringifyinwriteConversation. Previously it serialized once for comparison, then again for writing. Now only serializes once.recordMessageTokensqueuing path: When the last message already has tokens (the common case during streaming), tokens are queued in memory with zero serialization and zero disk I/O.Related Issues
Related to #18007
How to Validate
npm test -w @google/gemini-cli-core -- src/services/chatRecordingService.test.tsPre-Merge Checklist