feat(cli): Safeguard embedded shell exit flow with Ctrl-C confirmation#12637
feat(cli): Safeguard embedded shell exit flow with Ctrl-C confirmation#126370xlakshan wants to merge 4 commits intogoogle-gemini:mainfrom
Conversation
Summary of ChangesHello @0xlakshan, 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 Gemini CLI's interactive shell by adding a confirmation prompt when a user attempts to exit using Ctrl-C. This prevents unintended session terminations and the associated loss of conversation history, making the interactive experience more forgiving and user-friendly. 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. 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 introduces a confirmation step to prevent accidental exits from the embedded shell via Ctrl-C, which is a great usability improvement. The implementation uses a new custom hook to manage the confirmation dialog state. The logic seems well-thought-out. I've found one potential issue regarding a stale closure in the new hook that could lead to unpredictable behavior, which I've detailed in a specific comment. Addressing this will make the implementation more robust.
| useEffect(() => { | ||
| if (triggerEmbeddedShellExit) { | ||
| setTriggerEmbeddedShellExit(false); | ||
| setEmbeddedShellExitConfirmationRequest({ | ||
| prompt: createElement( | ||
| Text, | ||
| null, | ||
| 'Are you sure you want to quit? Any unsaved conversation history will be lost.', | ||
| ), | ||
| onConfirm: (answer) => { | ||
| setEmbeddedShellExitConfirmationRequest(null); | ||
| if (answer) quit(); | ||
| }, | ||
| }); | ||
| } | ||
| }, [triggerEmbeddedShellExit, quit]); |
There was a problem hiding this comment.
The onConfirm callback defined inside this useEffect creates a stale closure over the quit function. The quit function depends on the handleSlashCommand prop, which can change during the component's lifecycle. If handleSlashCommand changes while the confirmation dialog is displayed, onConfirm will call an outdated quit function. This could lead to unpredictable behavior.
To ensure the latest quit function is always called, you can store it in a ref. This is a common pattern in React to handle callbacks that depend on props or state that may change.
Here is a suggested implementation:
import { useState, useEffect, createElement, useCallback, useRef } from 'react'; // ... const quit = useCallback(() => { handleSlashCommand('/quit'); }, [handleSlashCommand]); const quitRef = useRef(quit); useEffect(() => { quitRef.current = quit; }); useEffect(() => { if (triggerEmbeddedShellExit) { setTriggerEmbeddedShellExit(false); setEmbeddedShellExitConfirmationRequest({ prompt: createElement( Text, null, 'Are you sure you want to quit? Any unsaved conversation history will be lost.', ), onConfirm: (answer) => { setEmbeddedShellExitConfirmationRequest(null); if (answer) quitRef.current(); }, }); } }, [triggerEmbeddedShellExit]);Note that you will need to add useRef to your imports from react.
| Hi @0xlakshan, 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! |
| Thank you for submission to the Gemini CLI project. At this time, we are closing this pull request in order to allow us to better triage and support more recent pull requests against the latest code changes. If you feel like this pull request is a critical contribution to the Gemini CLI project, please associate the pull request with an existing GitHub issue (instructions here: https://docs.github.com/en/issues/tracking-your-work-with-issues/using-issues/linking-a-pull-request-to-an-issue) before reopening. After Monday January 26 2026, any pull requests submitted by contributors without an associated issue will be automatically closed (more information here: #16706). If you do choose to reopen and submit this pull request, please ensure you rebase your changes onto the current main branch before resubmitting. This will help avoid merge conflicts and ensure your contribution is compatible with the latest codebase. |
Summary
#11291 - Add a confirmation step before quitting gemini-cli when pressing Ctrl-C inside the embedded shell.
Details
Add a confirmation step before quitting gemini-cli when pressing
Ctrl-Cinside the embedded shell. Right now, doubleCtrl-Dand doubleCtrl-Cboth exit gemini-cli as expected, but in the new 0.9.0 interactive shell flow, users can easily get frustrated and spamCtrl-Cto cancel the embedding shell. That often ends up quitting the entire CLI and wiping out the conversation history.This PR makes that flow safer by detecting
Ctrl-Cpresses inside the embedded shell and showing a confirmation prompt before actually exiting, so quick or repeatedCtrl-Chits won’t accidentally terminate the whole session.Related Issues
#11291
How to Validate
Pre-Merge Checklist