Show available slash commands in the disconnected TUI (#63258)

## Why

When the TUI loses its app server connection, users can still run
local commands, but the slash menu gives them no way to discover which
commands are available.

## What changed

Typing `/` while disconnected opens a navigable menu of local commands.
Searching for a server-dependent command such as `/model` shows it as
disabled and explains that it requires a server connection. The full
menu returns on reconnect.

While disconnected, the diagnostics command shows the thread ID when available and
explains that sending a report or saving a ZIP requires reconnecting.

## Testing

Add snapshots for the disconnected menu and disabled search results,
and regression coverage for selection across redraws, completion,
catalog restoration, draft preservation, offline diagnostics, and `/q`.

GitOrigin-RevId: 835e66ad7e436227f28f62708760c91185d1b3ec
This commit is contained in:
Anton Panasenko
2026-11-23 04:34:46 +00:00
committed by copyberry
parent e0a6d0532d
commit 326bac6ce0
10 changed files with 218 additions and 55 deletions
+11
View File
@@ -91,6 +91,17 @@ impl App {
if self.reconnect.offline && matches!(event, AppEvent::Exit(ExitMode::ShutdownFirst)) {
return Ok(AppRunControl::Exit(ExitReason::UserRequested));
}
if self.reconnect.offline && matches!(event, AppEvent::OpenFeedback) {
let thread = self.current_displayed_thread_id().map_or_else(
|| "Thread ID unavailable.".to_string(),
|id| format!("Thread ID {id}"),
);
self.chat_widget.add_info_message(
thread,
Some("Reconnect to send feedback or save a ZIP.".into()),
);
return Ok(AppRunControl::Continue);
}
if self.reconnect.offline
&& !matches!(
&event,
+22 -1
View File
@@ -220,7 +220,28 @@ async fn lost_mutation_reply_preserves_work_without_resubmitting() -> Result<()>
AppRunControl::Exit(ExitReason::UserRequested)
));
}
for command in ["/exit", "/quit"] {
for ch in "/feedback".chars() {
app.handle_tui_event(
&mut tui,
&mut session,
TuiEvent::Key(KeyCode::Char(ch).into()),
)
.await?;
}
app.handle_tui_event(&mut tui, &mut session, TuiEvent::Key(KeyCode::Enter.into()))
.await?;
while let Ok(event) = events.try_recv() {
app.handle_event(&mut tui, &mut session, event).await?;
}
let guidance = lines_to_single_string(
&app.transcript_cells
.last()
.unwrap()
.display_lines(/*width*/ 120),
);
assert_snapshot!(guidance.replace(&id.to_string(), "<id>"), @"• Thread ID <id> Reconnect to send feedback or save a ZIP.");
for command in ["/exit", "/quit", "/q"] {
app.chat_widget
.restore_user_message_to_composer(command.into());
app.handle_tui_event(&mut tui, &mut session, TuiEvent::Key(KeyCode::Enter.into()))
+16 -9
View File
@@ -194,10 +194,11 @@
//! navigation slash commands remain available so users can leave or manage the view.
//! Exports and local display commands (`/title` and `/statusline`) also accept arguments.
//!
//! During reconnection, `handle_restricted_key` edits the draft directly without popup dispatch,
//! composer shortcuts, or submission; `?` becomes literal input. Enter and Tab leave the draft
//! intact until reconnection succeeds, except for `/warnings`, `/config`, `/rc` (alias `/remote`,
//! including `stop`), `/quit`, and `/exit`.
//! While disconnected, `handle_restricted_key` keeps the draft editable without submission.
//! The slash popup supports navigation, Tab and submission of local commands.
//! Local commands include `/warnings`, `/config`, `/rc` (alias `/remote`, including `stop`),
//! `/feedback` (thread ID and reconnection guidance), `/quit`, and `/exit`. Searching for
//! other commands shows that they require a server connection. `?` becomes literal input.
//! When connected but the thread is unavailable, configured
//! submit keys may dispatch explicitly allowed recovery and local commands; other drafts stay put.
//! Escape dismisses command and argument popups while preserving the Vim Insert-to-Normal transition.
@@ -4192,13 +4193,17 @@ impl ChatComposer {
let cursor = self.draft.textarea.cursor();
let caret_on_first_line = cursor <= first_line_end;
let is_editing_slash_command_name = caret_on_first_line
&& self
.slash_input()
.is_editing_command_name(first_line, cursor);
let command_filter_text = caret_on_first_line
.then(|| slash_input::command_popup_filter_text(first_line, cursor))
.flatten();
let disconnected =
self.popups.restricted_input_mode == Some(RestrictedInputMode::Disconnected);
// Completing `/q` offline must not erase the rest of a draft like `/question`.
let is_editing_slash_command_name = caret_on_first_line
&& (!disconnected || command_filter_text.as_deref() == Some(text.trim_end()))
&& self
.slash_input()
.is_editing_command_name(first_line, cursor);
// If the cursor is currently positioned within an `@token`, prefer the
// file-search popup over the slash popup so users can insert a file path
@@ -4223,7 +4228,9 @@ impl ChatComposer {
if is_editing_slash_command_name
&& let Some(command_filter_text) = command_filter_text.as_deref()
{
let command_popup = self.slash_input().command_popup(command_filter_text);
let command_popup = self
.slash_input()
.command_popup(command_filter_text, disconnected);
self.popups.active = ActivePopup::Command(command_popup);
}
}
@@ -1,4 +1,4 @@
//! Restricted editing retains drafts and allows local warnings, configuration, and quit commands.
//! Restricted editing retains drafts and limits slash dispatch to local commands.
//! Unavailable threads also allow recovery and other local commands.
//! Connected unavailable threads retain the empty-prompt agents shortcut.
//! Paste Enter handling is shared with normal submission so buffered newlines survive both paths.
@@ -16,9 +16,31 @@ pub(crate) enum RestrictedInputMode {
DraftHandoff,
}
impl RestrictedInputMode {
fn allows_command(self, command: SlashCommand) -> bool {
match self {
Self::Disconnected => command.available_when_disconnected(),
Self::UnavailableThread => {
command == SlashCommand::Config || command.available_when_thread_unavailable()
}
Self::DraftHandoff => false,
}
}
}
impl ChatComposer {
pub(crate) fn set_restricted_input_mode(&mut self, mode: Option<RestrictedInputMode>) {
let was_disconnected =
self.popups.restricted_input_mode == Some(RestrictedInputMode::Disconnected);
self.popups.restricted_input_mode = mode;
// Command popups cache their catalog. Rebuild it on a connection change, keeping
// the selection across keys and redraws that use the same catalog.
if was_disconnected != (mode == Some(RestrictedInputMode::Disconnected))
&& matches!(self.popups.active, ActivePopup::Command(_))
{
self.popups.active = ActivePopup::None;
self.sync_restricted_slash_popup();
}
// Reconcile a popup opened before the routing mode changed, before Tab can accept it.
if matches!(self.popups.active, ActivePopup::Argument(_)) {
self.sync_slash_argument_popup(/*allow*/ true);
@@ -109,12 +131,10 @@ impl ChatComposer {
}
self.cancel_history_search();
self.attachments.clear_remote_image_selection();
if mode != RestrictedInputMode::UnavailableThread
|| !matches!(
self.popups.active,
ActivePopup::Command(_) | ActivePopup::Argument(_)
)
{
if !matches!(
self.popups.active,
ActivePopup::Command(_) | ActivePopup::Argument(_)
) {
self.popups.active = ActivePopup::None;
}
self.set_disable_paste_burst(/*disabled*/ true);
@@ -139,8 +159,7 @@ impl ChatComposer {
self.draft.textarea_state.get_mut().follow_cursor();
return InputResult::None;
}
if mode == RestrictedInputMode::UnavailableThread
&& self.draft.textarea.wants_vim_search_key(key)
if self.draft.textarea.wants_vim_search_key(key)
&& matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat)
{
self.popups.active = ActivePopup::None;
@@ -166,11 +185,10 @@ impl ChatComposer {
slash_input::command_popup_filter_text(first_line, /*cursor*/ 0);
self.edit_restricted_draft(key);
}
self.sync_unavailable_slash_popup();
self.sync_restricted_slash_popup();
return result;
}
if mode == RestrictedInputMode::UnavailableThread
&& pending_pastes.is_empty()
if pending_pastes.is_empty()
&& matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat)
&& let ActivePopup::Command(popup) = &self.popups.active
{
@@ -180,7 +198,7 @@ impl ChatComposer {
{
let text = self.draft.textarea.text();
if !matches!(selected, CommandItem::Builtin(command)
if (command == SlashCommand::Config || command.available_when_thread_unavailable())
if mode.allows_command(command)
&& text.trim().lines().count() == 1
&& (command.supports_inline_args()
|| text.split_once(char::is_whitespace).is_none_or(|(_, args)| args.trim().is_empty())))
@@ -191,11 +209,11 @@ impl ChatComposer {
let result = self
.handle_key_event_with_slash_popup(
popup_key,
slash_input::SlashPopupInputMode::UnavailableThread,
slash_input::SlashPopupInputMode::Restricted,
)
.0;
self.reset_vim_mode_after_successful_dispatch(&result);
self.sync_unavailable_slash_popup();
self.sync_restricted_slash_popup();
return result;
}
if !submit
@@ -213,7 +231,7 @@ impl ChatComposer {
let result = self
.handle_key_event_with_slash_popup(
key,
slash_input::SlashPopupInputMode::UnavailableThread,
slash_input::SlashPopupInputMode::Restricted,
)
.0;
if vim_escape {
@@ -232,9 +250,7 @@ impl ChatComposer {
.bare_command(text)
.or_else(|| input.inline_command(text).map(|command| command.command))
.filter(|_| text.trim().lines().count() == 1);
if matches!(command, Some(SlashCommandItem::Builtin(command))
if matches!(command, SlashCommand::Warnings | SlashCommand::Config | SlashCommand::Rc | SlashCommand::Quit | SlashCommand::Exit)
|| mode == RestrictedInputMode::UnavailableThread && command.available_when_thread_unavailable())
if matches!(command, Some(SlashCommandItem::Builtin(command)) if mode.allows_command(command))
&& let Some(result) = self
.try_dispatch_bare_slash_command()
.or_else(|| self.try_dispatch_slash_command_with_args())
@@ -251,14 +267,12 @@ impl ChatComposer {
&& matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat)
{
self.edit_restricted_draft(key);
if mode == RestrictedInputMode::UnavailableThread {
self.sync_unavailable_slash_popup();
}
self.sync_restricted_slash_popup();
}
InputResult::None
}
fn sync_unavailable_slash_popup(&mut self) {
fn sync_restricted_slash_popup(&mut self) {
let allow = self.slash_commands_enabled()
&& !self.draft.is_bash_mode
&& self.draft.textarea.vim_query().is_none();
@@ -320,6 +320,53 @@ fn unavailable_thread_vim_escape_and_search_keep_popup_closed() {
}
}
#[test]
fn disconnected_menu_keeps_its_selection_across_redraws_and_restores_the_catalog() {
let (mut composer, _) = new_test_composer();
composer.set_text_content("/".into(), Vec::new(), Vec::new());
let mode = RestrictedInputMode::Disconnected;
for key in [KeyCode::Down, KeyCode::Down, KeyCode::Up] {
composer.handle_restricted_key(key.into(), mode);
composer.set_restricted_input_mode(Some(mode));
composer.sync_popups();
}
let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 10)).unwrap();
terminal
.draw(|frame| composer.render(frame.area(), frame.buffer_mut()))
.unwrap();
insta::assert_snapshot!("disconnected_command_menu", terminal.backend());
assert_eq!(
composer.handle_restricted_key(KeyCode::Tab.into(), mode),
InputResult::None
);
assert_eq!(composer.current_text(), "/rc ");
composer.set_text_content("/model".into(), Vec::new(), Vec::new());
terminal
.draw(|frame| composer.render(frame.area(), frame.buffer_mut()))
.unwrap();
insta::assert_snapshot!("disconnected_server_command", terminal.backend());
for key in [KeyCode::Enter, KeyCode::Tab] {
assert_eq!(
composer.handle_restricted_key(key.into(), mode),
InputResult::None
);
assert_eq!(composer.current_text(), "/model");
}
composer.set_text_content("/".into(), Vec::new(), Vec::new());
composer.set_restricted_input_mode(/*mode*/ None);
composer.sync_popups();
let ActivePopup::Command(popup) = &composer.popups.active else {
panic!("expected command popup after reconnect");
};
assert!(
popup
.filtered_items()
.contains(&CommandItem::Builtin(SlashCommand::Model))
);
}
#[test]
fn disconnected_input_dispatches_local_config_command() {
let (mut composer, _) = new_test_composer();
@@ -381,6 +428,8 @@ fn restricted_input_preserves_blocked_drafts_and_attachments() {
"/compact",
"/voice",
"/unknown",
"/q extra",
"/q\nkeep my draft",
"/status extra",
"/status\nkeep my draft",
"/clear recovery\nkeep my draft",
@@ -410,6 +459,18 @@ fn restricted_input_preserves_blocked_drafts_and_attachments() {
);
assert_eq!(composer.current_text(), text);
}
let (mut composer, _) = new_test_composer();
composer.set_text_content("/question".into(), Vec::new(), Vec::new());
composer.draft.textarea.set_cursor("/q".len());
composer.sync_popups();
for key in [KeyCode::Tab, KeyCode::Enter] {
assert_eq!(
composer.handle_restricted_key(key.into(), RestrictedInputMode::Disconnected),
InputResult::None
);
assert_eq!(composer.current_text(), "/question");
}
}
#[test]
@@ -33,7 +33,7 @@ pub(super) enum SlashValidation {
#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) enum SlashPopupInputMode {
Normal,
UnavailableThread,
Restricted,
}
pub(super) enum SubmissionValidation {
@@ -172,9 +172,10 @@ impl<'a> SlashInput<'a> {
has_slash_command_prefix(name, self.command_flags, self.service_tier_commands)
}
pub(super) fn command_popup(&self, filter_text: &str) -> CommandPopup {
pub(super) fn command_popup(&self, filter_text: &str, disconnected: bool) -> CommandPopup {
let mut command_popup = CommandPopup::new(
CommandPopupFlags {
disconnected,
collaboration_modes_enabled: self.command_flags.collaboration_modes_enabled,
connectors_enabled: self.command_flags.connectors_enabled,
plugins_command_enabled: self.command_flags.plugins_command_enabled,
@@ -0,0 +1,14 @@
---
source: tui/src/bottom_pane/chat_composer/reconnect_tests.rs
expression: terminal.backend()
---
" /config configure Codex preferences "
"› /rc manage remote control for the local background server "
" /warnings view retained warnings and diagnostic details "
" /exit exit Codex "
" /feedback show the thread ID and how to send feedback "
" "
"› / "
" "
" "
" 100% context left "
@@ -0,0 +1,14 @@
---
source: tui/src/bottom_pane/chat_composer/reconnect_tests.rs
expression: terminal.backend()
---
" /model (disabled) choose what model and reasoning effort to use (disabled: "
" requires a server connection) "
" "
"› /model "
" "
" "
" "
" "
" "
" 100% context left "
+32 -20
View File
@@ -33,6 +33,7 @@ pub(crate) enum CommandItem {
}
pub(crate) struct CommandPopup {
disconnected: bool,
command_filter: String,
commands: Vec<CommandItem>,
side_conversation_active: bool,
@@ -42,6 +43,7 @@ pub(crate) struct CommandPopup {
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct CommandPopupFlags {
pub(crate) disconnected: bool,
pub(crate) collaboration_modes_enabled: bool,
pub(crate) connectors_enabled: bool,
pub(crate) plugins_command_enabled: bool,
@@ -95,6 +97,7 @@ impl CommandPopup {
})
.collect();
Self {
disconnected: flags.disconnected,
command_filter: String::new(),
commands,
side_conversation_active: flags.side_conversation_active,
@@ -164,7 +167,7 @@ impl CommandPopup {
if matches!(command, CommandItem::Builtin(cmd) if ALIAS_COMMANDS.contains(cmd)) {
continue;
}
if self.is_unavailable(command) {
if self.unavailable_reason(command).is_some() {
continue;
}
out.push((command.clone(), None));
@@ -206,8 +209,8 @@ impl CommandPopup {
out.extend(exact);
out.extend(prefix);
if self.side_conversation_active {
out.sort_by_key(|(item, _)| self.is_unavailable(item));
if self.side_conversation_active || self.disconnected {
out.sort_by_key(|(item, _)| self.unavailable_reason(item).is_some());
}
out
}
@@ -225,18 +228,21 @@ impl CommandPopup {
.enumerate()
.map(|(index, (item, indices))| {
let name = format!("/{}", item.command());
let description = if matches!(item, CommandItem::Builtin(SlashCommand::Daybreak)) {
self.daybreak_command_description
.unwrap_or(item.description())
} else {
item.description()
let description = match item {
CommandItem::Builtin(SlashCommand::Feedback) if self.disconnected => {
"show the thread ID and how to send feedback"
}
CommandItem::Builtin(SlashCommand::Daybreak) => self
.daybreak_command_description
.unwrap_or(item.description()),
_ => item.description(),
}
.to_string();
let unavailable = self.is_unavailable(&item);
let disabled_reason = self.unavailable_reason(&item);
GenericDisplayRow {
name,
name_prefix_spans: vec![if self.state.selected_idx == Some(index)
&& !unavailable
&& disabled_reason.is_none()
{
"› ".into()
} else {
@@ -245,9 +251,8 @@ impl CommandPopup {
selection_style: Some(selection_style()),
match_indices: indices.map(|v| v.into_iter().map(|i| i + 1).collect()),
description: Some(description),
is_disabled: unavailable,
disabled_reason: unavailable
.then(|| "not available in a side conversation".to_string()),
is_disabled: disabled_reason.is_some(),
disabled_reason: disabled_reason.map(str::to_owned),
..Default::default()
}
})
@@ -285,15 +290,21 @@ impl CommandPopup {
self.state
.selected_idx
.and_then(|idx| matches.get(idx).cloned())
.filter(|item| !self.is_unavailable(item))
.filter(|item| self.unavailable_reason(item).is_none())
}
fn is_unavailable(&self, item: &CommandItem) -> bool {
self.side_conversation_active
&& match item {
CommandItem::Builtin(cmd) => !cmd.available_in_side_conversation(),
CommandItem::ServiceTier(_) => true,
}
fn unavailable_reason(&self, item: &CommandItem) -> Option<&'static str> {
if self.disconnected
&& !matches!(item, CommandItem::Builtin(cmd) if cmd.available_when_disconnected())
{
return Some("requires a server connection");
}
if self.side_conversation_active
&& !matches!(item, CommandItem::Builtin(cmd) if cmd.available_in_side_conversation())
{
return Some("not available in a side conversation");
}
None
}
}
@@ -666,6 +677,7 @@ mod tests {
fn plan_command_visible_when_collaboration_modes_enabled() {
let mut popup = CommandPopup::new(
CommandPopupFlags {
disconnected: false,
collaboration_modes_enabled: true,
connectors_enabled: false,
plugins_command_enabled: false,
+8
View File
@@ -230,6 +230,14 @@ impl SlashCommand {
matches!(self, SlashCommand::Fork | SlashCommand::Review)
}
/// Commands the TUI can handle locally while disconnected from its app server.
pub(crate) fn available_when_disconnected(self) -> bool {
matches!(
self,
Self::Warnings | Self::Config | Self::Rc | Self::Feedback | Self::Quit | Self::Exit
)
}
/// Commands that do not require a writable current thread. The server must still be connected.
pub(crate) fn available_when_thread_unavailable(self) -> bool {
matches!(