Skip to content

fix(core,cli): improve terminal buffer memory management and format Windows diagnostic paths - #29380

Merged
DavidAPierce merged 4 commits into
google-gemini:mainfrom
jesussamuel-byte:561554833
Sep 18, 2026
Merged

DavidAPierce merged 4 commits into
google-gemini:mainfrom
jesussamuel-byte:561554833

Conversation

@jesussamuel-byte

@jesussamuel-byte jesussamuel-byte commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Optimizes memory usage during PTY shell execution and headless terminal buffer serialization in ShellExecutionService and terminalSerializer, and improves Markdown formatting of Windows file paths in /bug and /bug-memory diagnostic messages.

Details

  • Headless Terminal Buffer Sizing: Adjusts SCROLLBACK_LIMIT to 50,000 lines per headless terminal instance and enforces a MAX_CHILD_PROCESS_BUFFER_SIZE byte cap inside getFullBufferText using sliding window truncation (truncateMiddle).
  • Color Serialization Optimization: Adds an optional includeColor parameter to serializeTerminalToObject to skip redundant RGB/palette hex string conversions when color output is disabled, and updates renderFn to clear color properties in-place without allocating duplicate arrays via .map().
  • PTY Execution Lifecycle Cleanup:
    • Removes temporary onAbortDuringDrain listeners from AbortSignal upon normal process exit to avoid listener accumulation across repeated command executions.
    • Clears intermediate output and sniffChunks closure buffers and cancels pending renderTimeout timers during process finalization.
    • Updates ShellExecutionService.resetForTest() to properly tear down active PTY processes, child processes, and background log streams.
  • Windows Path Markdown Formatting: Wraps file paths output by /bug and /bug-memory in inline code backticks so backslashes (e.g., C:\Users\ASUS\.gemini\...) are preserved accurately by Markdown renderers.
  • Unit Test Coverage: Added unit tests covering includeColor = false serialization, AbortSignal listener cleanup on normal exit, resetForTest() resource teardown, and Windows path backtick formatting.

Related Issues

GH-27557

How to Validate

  1. Run unit tests for ShellExecutionService, terminalSerializer, bugCommand, and bugMemoryCommand:
    npm test -w @google/gemini-cli-core -- src/services/shellExecutionService.test.ts src/utils/terminalSerializer.test.ts
    npm test -w @google/gemini-cli -- src/ui/commands/bugCommand.test.ts src/ui/commands/bugMemoryCommand.test.ts
  2. Run full preflight checks (format, lint, and typecheck):
    npm run format && npm run lint:ci && npm run typecheck

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

@jesussamuel-byte
jesussamuel-byte requested a review from a team as a code owner September 17, 2026 20:51
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 introduces critical memory management and stability improvements for terminal execution within the core and CLI packages. By optimizing buffer sizing, streamlining serialization processes, and ensuring rigorous cleanup of PTY and child process resources, the changes significantly reduce the risk of memory leaks and performance degradation during long-running shell operations. Additionally, it addresses minor UI formatting issues to improve the readability of diagnostic file paths on Windows.

Highlights

  • Memory Management: Reduced the headless terminal scrollback limit to 50,000 lines and implemented a sliding window truncation for terminal buffers to prevent excessive V8 heap growth.
  • Serialization Optimization: Added an optional includeColor parameter to terminal serialization to avoid unnecessary color processing and optimized color property clearing to avoid redundant array allocations.
  • Resource Lifecycle Cleanup: Improved PTY lifecycle management by ensuring proper cleanup of AbortSignal listeners, clearing closure buffers, and adding a robust resetForTest method to tear down active processes and streams.
  • Markdown Formatting: Wrapped Windows file paths in diagnostic messages with backticks to ensure backslashes are correctly rendered in Markdown.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the 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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. 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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions Bot added the size/l A large sized PR label Sep 17, 2026
@github-actions

github-actions Bot commented Sep 17, 2026

Copy link
Copy Markdown

📊 PR Size: size/L

  • Lines changed: 359
  • Additions: +306
  • Deletions: -53
  • Files changed: 8

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses memory leaks and excessive V8 heap growth during shell execution. Key changes include reducing the scrollback limit, releasing intermediate closure buffers, properly cleaning up active PTYs and event listeners on exit, and updating serializeTerminalToObject to optionally omit foreground and background colors. Additionally, file paths in bug commands are now wrapped in backticks to prevent Markdown escaping on Windows. The review feedback points out a redundant loop in shellExecutionService.ts that manually clears fg and bg colors when showColor is disabled, as this is already handled internally by serializeTerminalToObject when passing the includeColor parameter. Removing this loop will avoid unnecessary O(N*M) traversals and improve rendering efficiency.

Comment thread packages/core/src/services/shellExecutionService.ts Outdated
@gemini-cli gemini-cli Bot added the status/need-issue Pull requests that need to have an associated issue. label Sep 17, 2026
@jesussamuel-byte

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces memory optimizations, robust cleanup mechanisms, and path formatting fixes. It wraps Windows file paths in backticks within the bug and memory commands to prevent Markdown backslash escaping. In the core package, it reduces the terminal scrollback limit to prevent excessive V8 heap growth, optimizes terminal serialization by allowing color omission, ensures proper disposal of event listeners and active PTYs (including a thorough cleanup in resetForTest), and releases intermediate closure buffers to prevent heap retention. As there are no review comments, I have no additional feedback to provide.

@DavidAPierce
DavidAPierce added this pull request to the merge queue Sep 18, 2026
Merged via the queue into google-gemini:main with commit 196c772 Sep 18, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/l A large sized PR status/need-issue Pull requests that need to have an associated issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants