feat(cli): add /bug-memory command and auto-capture heap snapshot in /bug#25639
feat(cli): add /bug-memory command and auto-capture heap snapshot in /bug#25639Anjaligarhwal wants to merge 2 commits intogoogle-gemini:mainfrom
Conversation
…/bug Adds a /bug-memory slash command that captures a V8 heap snapshot via the in-process node:inspector/promises Session and streams it to the project temp directory. The /bug command now triggers the same capture automatically when process RSS reaches 2 GB, after the bug URL has been displayed so the user is not blocked by the 20+ second snapshot. Chunks are streamed straight to disk to keep peak memory overhead constant under memory pressure. Fixes google-gemini#25337
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 enhances the CLI's diagnostic capabilities by adding functionality to capture V8 heap snapshots. By providing both a manual command and an automated trigger based on memory usage thresholds, it significantly reduces the friction for users to provide actionable data when reporting memory-related bugs, without requiring external debugging tools or port exposure. Highlights
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. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces V8 heap snapshotting to the CLI, adding a manual /bug-memory command and an automatic trigger in the /bug command for high memory usage. Feedback highlights a potential out-of-memory risk due to lack of stream backpressure in the current implementation, recommending the v8 module instead. Additionally, a correction was suggested for a UI message that incorrectly claims the CLI remains responsive during the blocking snapshot operation.
Switch memorySnapshot from node:inspector Session to v8.getHeapSnapshot() piped through node:stream/promises so V8 slows its producer when disk I/O lags, avoiding write-buffer OOM on 2GB+ snapshots. Also correct the /bug and /bug-memory notices: V8 snapshotting is stop-the-world, so the CLI is temporarily unresponsive during capture rather than responsive.
|
/gemini review |
|
Hello @Anjaligarhwal, I'm ready to perform the code review for this pull request. I will analyze the changes and provide feedback based on the review criteria. Please give me a moment to process the code. |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a new bug-memory command and enhances the existing bug command to automatically capture V8 heap snapshots when high memory usage (RSS > 2 GB) is detected. It also includes a new utility captureHeapSnapshot for efficient streaming of heap snapshots to disk and comprehensive unit tests for the new functionality. I have no feedback to provide.
Summary
Adds a new
/bug-memoryslash command that captures a V8 heap snapshot and saves it to disk, and teaches the existing/bugcommand to do the same automatically when process RSS exceeds 2 GB. Closes the friction gap calledout in #25337 — users no longer need to attach Chrome DevTools just to share a memory snapshot with a bug report.
The URL shown by
/bugalways renders before capture begins, so the 20+ second snapshot never makes the CLI look frozen.Details
How the capture works
captureHeapSnapshot(filePath)pipesv8.getHeapSnapshot()straight intofs.createWriteStream(filePath)vianode:stream/promisespipeline. This is entirely in-process — nothing is exposed over a debugger port, which addresses the "don't require port 9229" constraint in Add a /bug-memory command that takes a memory snapshot and saves it to disk #25337 by not requiring any port.pipelinepropagates backpressure end-to-end, so V8 slows its snapshot producer whenever the downstream write stream cannot keep up. A 2 GB+ heap is written with constant memory overhead; chunks never accumulate in the JavaScript heap.pipelinealso handles error propagation and stream cleanup, so there is no manualtry/finally, no listener bookkeeping, and no separate "disconnect" step./bugintegrationopen()is invoked,/bugchecks process.memoryUsage().rssagainstMEMORY_SNAPSHOT_AUTO_THRESHOLD_BYTES(2 GB). If over the threshold and a project temp dir is available, it captures a snapshot to/bug-memory-<Date.now()>.heapsnapshot` and shows the resulting path + size in the UI history.Shared utility
captureHeapSnapshot(filePath)lives inpackages/cli/src/ui/utils/memorySnapshot.tsso/bugand/bug-memorycan never drift.
Related Issues
Fixes #25337
How to Validate
Automated
Manual —
/bug-memoryhappy pathnpm run start/bug-memory*.heapsnapshotfile in Chrome DevTools → Memory → Load to confirm it is a valid snapshot.Manual —
/bugauto-captureNODE_OPTIONS='--max-old-space-size=4096' npm run start, then run some memory-heavy prompts untilprocess.memoryUsage().rssexceeds 2 GB (you can confirm via/bugwhich reports current RSS in the report info)./bug some memory leak.Edge cases covered by tests
/bug-memorywhen the project temp directory is unavailable → clean error, no capture attempted./bug-memorywhenHeapProfiler.takeHeapSnapshotrejects → the session is still disconnected, the user sees an error./bugbelow the threshold → no capture at all (existing behavior unchanged)./bugabove the threshold where capture fails → bug URL still opened, capture error surfaced, command resolves normally.Validation Evidence
End-to-end run on Windows 11, Node 20, npm run start, current RSS 211.6 MB
(below the 2 GB auto-capture threshold, so
/bugbehaves unchanged while/bug-memorycaptures on demand).1.
/bugruns unchanged below the 2 GB threshold, and/bug-memoryappears alongside it in autocomplete.2.
/bug-memoryemits the destination message before capture begins and the "snapshot saved" message after.3. The snapshot file is physically written to the project temp directory with the expected size.
4. The snapshot loads in Chrome DevTools → Memory and parses cleanly; the expanded
(compiled code)node shows real source paths from the running process, confirming the captured data is genuine.5. The Constructors summary shows the expected Node.js + gemini-cli heap composition — live classes include
Module,ModuleLoader,ResolveCache,ZodObject, plus Node internals (ArrayBuffer,system / Context) — exactly what a healthy CLI session should retain.Pre-Merge Checklist
/bugcommand internals; slash-command description is self-documenting in/help.memorySnapshot, 4 inbugMemoryCommand, 3 inbugCommandfor the auto-capture branches) plus the existingBuiltinCommandLoaderregistration test covers the new command./bugbehavior below 2 GB is byte-identical.Chrome DevTools parses the snapshot (see Validation Evidence above).