Fix issues where escape codes could end up on startup in the input prompt#7267
Fix issues where escape codes could end up on startup in the input prompt#7267
Conversation
There was a problem hiding this comment.
Summary of Changes
Hello @jacob314, 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 issues where malformed control characters (escape codes) could appear in the Gemini CLI's output and input prompt, particularly when the application starts slowly or the system is under load. The core solution involves activating raw terminal mode earlier in the application lifecycle and improving the robustness of Kitty keyboard protocol detection.
Highlights
- Proactive Raw Mode Activation: The application now enters raw terminal mode immediately upon startup in interactive sessions. This prevents spurious escape codes from interfering with the console output and input prompt, especially during slow initialization.
- Optimized Kitty Protocol Detection: The detection of the Kitty keyboard protocol has been moved to an earlier stage in the application's main execution flow. This ensures that the protocol is properly identified and enabled before the interactive UI is rendered, preventing potential race conditions.
- Extended Kitty Detection Timeout: The timeout for detecting the Kitty protocol has been increased from 50ms to 200ms. Furthermore, if a partial response indicating Kitty support is received, an additional 1000ms delay is introduced, accommodating terminals like iTerm2 that may respond slowly, especially when not in focus.
- Robust Raw Mode Management: Logic has been added to conditionally set and unset raw mode only if it's not already in the desired state. This prevents redundant terminal state changes and includes proper cleanup for raw mode on process exit signals (SIGTERM, SIGINT).
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| 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 issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
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 .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 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
-
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. ↩
|
I'll follow up with some tests for this. |
There was a problem hiding this comment.
Code Review
This pull request addresses an issue where terminal escape codes could leak into the input prompt, especially on slower systems. The main fix is to enable raw mode on stdin as early as possible during startup for interactive sessions. This is a solid approach to prevent unwanted characters from being echoed.
The changes also include moving the Kitty keyboard protocol detection earlier in the startup sequence and making the detection logic more robust with improved timeouts. This is a great enhancement that should improve reliability on a wider range of terminal emulators and system loads.
I have one critical piece of feedback regarding resource cleanup. While you've added handlers to restore the terminal state on SIGTERM and SIGINT, the case of a normal process exit is not handled. This could leave the user's terminal in a broken state. Please see my detailed comment.
43a675b to
cc9ab95
Compare
|
Fyi @deepankarsharma would be great to have you review this. |
Code Coverage Summary
CLI Package - Full Text ReportCore Package - Full Text ReportFor detailed HTML reports, please see the 'coverage-reports-22.x-ubuntu-latest' artifact from the main CI run. |
|
|
||
| if (progressiveEnhancementReceived) { | ||
| // Enable the protocol | ||
| process.stdout.write('\x1b[>1u'); |
There was a problem hiding this comment.
This could be a constant in platformConstants.ts
2f77bd7 to
3ee7f17
Compare
This commit addresses several build failures in the gemini.test.tsx file. - Imports the 'main' function to resolve a 'Cannot find name' error. - Corrects the mock for 'parseArguments' to provide a complete 'CliArgs' object, fixing a type incompatibility. - Updates the type of the 'setRawModeSpy' to use the correct 'MockInstance' type from vitest, resolving a type error.
3ee7f17 to
8d8b5cc
Compare
|
Size Change: +11.4 kB (+0.09%) Total Size: 12.5 MB
ℹ️ View Unchanged
|
TLDR
Particularly in the presence of slow to startup MCP servers, it was possible to get Gemini CLI too a insert malformed control character sequence both in stdout and even worse in the input prompt itself. To repro the key was to tab out of the terminal and then type keys or do something else to put the computer under high load.
Dive Deeper
To solve this we enter raw mode immediately and never leave it to avoid terminal control codes escaping where they shouldn't. This resolves both bugs.
We also needed to move kitty protocol detection earlier to reduce the time before we started kitty protocol detection
As part of testing also identified that the timeout to detect kitty mode was too aggressive (50ms). Switched to a a longer 200ms with an additonal 1000ms delay if there was a partial response indicating a terminal probably supported the kitty protocol. Measured that it can take iTerm 91ms to indicate it is in kitty protocol mode on a modern laptop.
Reviewer Test Plan
Open iterm2 or another kitty capable terminal that can reproduce the issue.
Run with an MCP server that takes a few seconds to startup.
Launch Gemini CLI and tab away typing furiously in the alternate terminal or window you tabbed to.
Go back to Gemini CLI after it has loaded and verify that no spurious characters are in the console or input prompt.
Testing Matrix
fixes #6906