mirror of
https://github.com/openai/codex.git
synced 2026-11-24 23:22:05 +00:00
tui: double-press Ctrl+C/Ctrl+D to quit (#8936)
## Problem
Codex’s TUI quit behavior has historically been easy to trigger
accidentally and hard to reason
about.
- `Ctrl+C`/`Ctrl+D` could terminate the UI immediately, which is a
common key to press while trying
to dismiss a modal, cancel a command, or recover from a stuck state.
- “Quit” and “shutdown” were not consistently separated, so some exit
paths could bypass the
shutdown/cleanup work that should run before the process terminates.
This PR makes quitting both safer (harder to do by accident) and more
uniform across quit
gestures, while keeping the shutdown-first semantics explicit.
## Mental model
After this change, the system treats quitting as a UI request that is
coordinated by the app
layer.
- The UI requests exit via `AppEvent::Exit(ExitMode)`.
- `ExitMode::ShutdownFirst` is the normal user path: the app triggers
`Op::Shutdown`, continues
rendering while shutdown runs, and only ends the UI loop once shutdown
has completed.
- `ExitMode::Immediate` exists as an escape hatch (and as the
post-shutdown “now actually exit”
signal); it bypasses cleanup and should not be the default for
user-triggered quits.
User-facing quit gestures are intentionally “two-step” for safety:
- `Ctrl+C` and `Ctrl+D` no longer exit immediately.
- The first press arms a 1-second window and shows a footer hint (“ctrl
+ <key> again to quit”).
- Pressing the same key again within the window requests a
shutdown-first quit; otherwise the
hint expires and the next press starts a fresh window.
Key routing remains modal-first:
- A modal/popup gets first chance to consume `Ctrl+C`.
- If a modal handles `Ctrl+C`, any armed quit shortcut is cleared so
dismissing a modal cannot
prime a subsequent `Ctrl+C` to quit.
- `Ctrl+D` only participates in quitting when the composer is empty and
no modal/popup is active.
The design doc `docs/exit-confirmation-prompt-design.md` captures the
intended routing and the
invariants the UI should maintain.
## Non-goals
- This does not attempt to redesign modal UX or make modals uniformly
dismissible via `Ctrl+C`.
It only ensures modals get priority and that quit arming does not leak
across modal handling.
- This does not introduce a persistent confirmation prompt/menu for
quitting; the goal is to keep
the exit gesture lightweight and consistent.
- This does not change the semantics of core shutdown itself; it changes
how the UI requests and
sequences it.
## Tradeoffs
- Quitting via `Ctrl+C`/`Ctrl+D` now requires a deliberate second
keypress, which adds friction for
users who relied on the old “instant quit” behavior.
- The UI now maintains a small time-bounded state machine for the armed
shortcut, which increases
complexity and introduces timing-dependent behavior.
This design was chosen over alternatives (a modal confirmation prompt or
a long-lived “are you
sure” state) because it provides an explicit safety barrier while
keeping the flow fast and
keyboard-native.
## Architecture
- `ChatWidget` owns the quit-shortcut state machine and decides when a
quit gesture is allowed
(idle vs cancellable work, composer state, etc.).
- `BottomPane` owns rendering and local input routing for modals/popups.
It is responsible for
consuming cancellation keys when a view is active and for
showing/expiring the footer hint.
- `App` owns shutdown sequencing: translating
`AppEvent::Exit(ShutdownFirst)` into `Op::Shutdown`
and only terminating the UI loop when exit is safe.
This keeps “what should happen” decisions (quit vs interrupt vs ignore)
in the chat/widget layer,
while keeping “how it looks and which view gets the key” in the
bottom-pane layer.
## Observability
You can tell this is working by running the TUIs and exercising the quit
gestures:
- While idle: pressing `Ctrl+C` (or `Ctrl+D` with an empty composer and
no modal) shows a footer
hint for ~1 second; pressing again within that window exits via
shutdown-first.
- While streaming/tools/review are active: `Ctrl+C` interrupts work
rather than quitting.
- With a modal/popup open: `Ctrl+C` dismisses/handles the modal (if it
chooses to) and does not
arm a quit shortcut; a subsequent quick `Ctrl+C` should not quit unless
the user re-arms it.
Failure modes are visible as:
- Quits that happen immediately (no hint window) from `Ctrl+C`/`Ctrl+D`.
- Quits that occur while a modal is open and consuming `Ctrl+C`.
- UI termination before shutdown completes (cleanup skipped).
## Tests
- Updated/added unit and snapshot coverage in `codex-tui` and
`codex-tui2` to validate:
- The quit hint appears and expires on the expected key.
- Double-press within the window triggers a shutdown-first quit request.
- Modal-first routing prevents quit bypass and clears any armed shortcut
when a modal consumes
`Ctrl+C`.
These tests focus on the UI-level invariants and rendered output; they
do not attempt to validate
real terminal key-repeat timing or end-to-end process shutdown behavior.
---
Screenshot:
<img width="912" height="740" alt="Screenshot 2026-01-13 at 1 05 28 PM"
src="https://github.com/user-attachments/assets/18f3d22e-2557-47f2-a369-ae7a9531f29f"
/>
This commit is contained in:
1 parent
92472e7baa
commit
4283a7432b
25 files changed
+932
-199
No files matched your search
@@ -57,7 +57,8 @@
|
||||
//! edits and renders a placeholder prompt instead of the editable textarea. This is part of the
|
||||
//! overall state machine, since it affects which transitions are even possible from a given UI
|
||||
//! state.
|
||||
|
||||
use crate::key_hint;
|
||||
use crate::key_hint::KeyBinding;
|
||||
use crate::key_hint::has_ctrl_or_alt;
|
||||
use crossterm::event::KeyCode;
|
||||
use crossterm::event::KeyEvent;
|
||||
@@ -168,7 +169,8 @@ pub(crate) struct ChatComposer {
|
||||
active_popup: ActivePopup,
|
||||
app_event_tx: AppEventSender,
|
||||
history: ChatComposerHistory,
|
||||
ctrl_c_quit_hint: bool,
|
||||
quit_shortcut_expires_at: Option<Instant>,
|
||||
quit_shortcut_key: KeyBinding,
|
||||
esc_backtrack_hint: bool,
|
||||
use_shift_enter_hint: bool,
|
||||
dismissed_file_popup_token: Option<String>,
|
||||
@@ -222,7 +224,8 @@ impl ChatComposer {
|
||||
active_popup: ActivePopup::None,
|
||||
app_event_tx,
|
||||
history: ChatComposerHistory::new(),
|
||||
ctrl_c_quit_hint: false,
|
||||
quit_shortcut_expires_at: None,
|
||||
quit_shortcut_key: key_hint::ctrl(KeyCode::Char('c')),
|
||||
esc_backtrack_hint: false,
|
||||
use_shift_enter_hint,
|
||||
dismissed_file_popup_token: None,
|
||||
@@ -578,16 +581,37 @@ impl ChatComposer {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_ctrl_c_quit_hint(&mut self, show: bool, has_focus: bool) {
|
||||
self.ctrl_c_quit_hint = show;
|
||||
if show {
|
||||
self.footer_mode = FooterMode::CtrlCReminder;
|
||||
} else {
|
||||
self.footer_mode = reset_mode_after_activity(self.footer_mode);
|
||||
}
|
||||
/// Show the transient "press again to quit" hint for `key`.
|
||||
///
|
||||
/// The owner (`BottomPane`/`ChatWidget`) is responsible for scheduling a
|
||||
/// redraw after [`super::QUIT_SHORTCUT_TIMEOUT`] so the hint can disappear
|
||||
/// even when the UI is otherwise idle.
|
||||
pub fn show_quit_shortcut_hint(&mut self, key: KeyBinding, has_focus: bool) {
|
||||
self.quit_shortcut_expires_at = Instant::now()
|
||||
.checked_add(super::QUIT_SHORTCUT_TIMEOUT)
|
||||
.or_else(|| Some(Instant::now()));
|
||||
self.quit_shortcut_key = key;
|
||||
self.footer_mode = FooterMode::QuitShortcutReminder;
|
||||
self.set_has_focus(has_focus);
|
||||
}
|
||||
|
||||
/// Clear the "press again to quit" hint immediately.
|
||||
pub fn clear_quit_shortcut_hint(&mut self, has_focus: bool) {
|
||||
self.quit_shortcut_expires_at = None;
|
||||
self.footer_mode = reset_mode_after_activity(self.footer_mode);
|
||||
self.set_has_focus(has_focus);
|
||||
}
|
||||
|
||||
/// Whether the quit shortcut hint should currently be shown.
|
||||
///
|
||||
/// This is time-based rather than event-based: it may become false without
|
||||
/// any additional user input, so the UI schedules a redraw when the hint
|
||||
/// expires.
|
||||
pub(crate) fn quit_shortcut_hint_visible(&self) -> bool {
|
||||
self.quit_shortcut_expires_at
|
||||
.is_some_and(|expires_at| Instant::now() < expires_at)
|
||||
}
|
||||
|
||||
fn next_large_paste_placeholder(&mut self, char_count: usize) -> String {
|
||||
let base = format!("[Pasted Content {char_count} chars]");
|
||||
let next_suffix = self.large_paste_counters.entry(char_count).or_insert(0);
|
||||
@@ -1497,10 +1521,7 @@ impl ChatComposer {
|
||||
modifiers: crossterm::event::KeyModifiers::CONTROL,
|
||||
kind: KeyEventKind::Press,
|
||||
..
|
||||
} if self.is_empty() => {
|
||||
self.app_event_tx.send(AppEvent::ExitRequest);
|
||||
(InputResult::None, true)
|
||||
}
|
||||
} if self.is_empty() => (InputResult::None, false),
|
||||
// -------------------------------------------------------------
|
||||
// History navigation (Up / Down) – only when the composer is not
|
||||
// empty or when the cursor is at the correct position, to avoid
|
||||
@@ -1801,7 +1822,7 @@ impl ChatComposer {
|
||||
return false;
|
||||
}
|
||||
|
||||
let next = toggle_shortcut_mode(self.footer_mode, self.ctrl_c_quit_hint);
|
||||
let next = toggle_shortcut_mode(self.footer_mode, self.quit_shortcut_hint_visible());
|
||||
let changed = next != self.footer_mode;
|
||||
self.footer_mode = next;
|
||||
changed
|
||||
@@ -1813,6 +1834,7 @@ impl ChatComposer {
|
||||
esc_backtrack_hint: self.esc_backtrack_hint,
|
||||
use_shift_enter_hint: self.use_shift_enter_hint,
|
||||
is_task_running: self.is_task_running,
|
||||
quit_shortcut_key: self.quit_shortcut_key,
|
||||
steer_enabled: self.steer_enabled,
|
||||
context_window_percent: self.context_window_percent,
|
||||
context_window_used_tokens: self.context_window_used_tokens,
|
||||
@@ -1823,8 +1845,13 @@ impl ChatComposer {
|
||||
match self.footer_mode {
|
||||
FooterMode::EscHint => FooterMode::EscHint,
|
||||
FooterMode::ShortcutOverlay => FooterMode::ShortcutOverlay,
|
||||
FooterMode::CtrlCReminder => FooterMode::CtrlCReminder,
|
||||
FooterMode::ShortcutSummary if self.ctrl_c_quit_hint => FooterMode::CtrlCReminder,
|
||||
FooterMode::QuitShortcutReminder if self.quit_shortcut_hint_visible() => {
|
||||
FooterMode::QuitShortcutReminder
|
||||
}
|
||||
FooterMode::QuitShortcutReminder => FooterMode::ShortcutSummary,
|
||||
FooterMode::ShortcutSummary if self.quit_shortcut_hint_visible() => {
|
||||
FooterMode::QuitShortcutReminder
|
||||
}
|
||||
FooterMode::ShortcutSummary if !self.is_empty() => FooterMode::ContextOnly,
|
||||
other => other,
|
||||
}
|
||||
@@ -2365,16 +2392,16 @@ mod tests {
|
||||
});
|
||||
|
||||
snapshot_composer_state("footer_mode_ctrl_c_quit", true, |composer| {
|
||||
composer.set_ctrl_c_quit_hint(true, true);
|
||||
composer.show_quit_shortcut_hint(key_hint::ctrl(KeyCode::Char('c')), true);
|
||||
});
|
||||
|
||||
snapshot_composer_state("footer_mode_ctrl_c_interrupt", true, |composer| {
|
||||
composer.set_task_running(true);
|
||||
composer.set_ctrl_c_quit_hint(true, true);
|
||||
composer.show_quit_shortcut_hint(key_hint::ctrl(KeyCode::Char('c')), true);
|
||||
});
|
||||
|
||||
snapshot_composer_state("footer_mode_ctrl_c_then_esc_hint", true, |composer| {
|
||||
composer.set_ctrl_c_quit_hint(true, true);
|
||||
composer.show_quit_shortcut_hint(key_hint::ctrl(KeyCode::Char('c')), true);
|
||||
let _ = composer.handle_key_event(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user