mirror of
https://github.com/openai/codex.git
synced 2026-11-17 22:10:37 +00:00
## Why Command-center navigation required a shared app server, leaving embedded sessions without the same way to switch between chats. Archiving or deleting an embedded session also exited the TUI. ## What changed - Enable command-center navigation and session listing for embedded sessions, including navigation from read-only conversations and the safety precaution menu. - Return to the command center after archiving or deleting a session. - Retain thread subscriptions when forking, including on embedded servers. - Confirm before quitting an embedded server would stop running sessions or side conversations. - Construct large startup and event-loop futures outside their callers' stack frames before boxing them. ## Testing Add regression coverage for embedded session switching with background approvals and draft restoration, plus confirmation before quitting with a running side conversation. Update navigation, archive/delete, fork, and UI snapshot tests for the new behavior. GitOrigin-RevId: 96bac48cccf85b685b58934d23dd1db93b663751
766 lines
28 KiB
Rust
766 lines
28 KiB
Rust
use super::*;
|
||
use pretty_assertions::assert_eq;
|
||
|
||
#[tokio::test]
|
||
async fn external_writer_view_quits_with_ctrl_c_or_q() -> Result<()> {
|
||
let (mut app, mut events, _operations) = make_test_app_with_channels().await;
|
||
app.chat_widget.show_external_writer_thread();
|
||
let mut app_server = crate::start_embedded_app_server_for_picker(&app.config).await?;
|
||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||
while events.try_recv().is_ok() {}
|
||
|
||
app.keymap.app.open_external_editor = vec![crate::key_hint::ctrl(KeyCode::Char('g'))];
|
||
app.handle_key_event(
|
||
&mut tui,
|
||
&mut app_server,
|
||
KeyEvent::new(KeyCode::Char('g'), KeyModifiers::CONTROL),
|
||
)
|
||
.await;
|
||
assert_eq!(
|
||
app.chat_widget.external_editor_state(),
|
||
ExternalEditorState::Closed
|
||
);
|
||
assert!(
|
||
!events
|
||
.try_recv()
|
||
.is_ok_and(|event| matches!(event, AppEvent::LaunchExternalEditor))
|
||
);
|
||
|
||
for key in [
|
||
KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL),
|
||
KeyEvent::new(KeyCode::Char('q'), KeyModifiers::NONE),
|
||
] {
|
||
app.handle_key_event(&mut tui, &mut app_server, key).await;
|
||
assert!(matches!(
|
||
events.try_recv(),
|
||
Ok(AppEvent::Exit(ExitMode::Immediate))
|
||
));
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn embedded_quit_confirms_stopping_a_running_side_conversation() -> Result<()> {
|
||
let (mut app, mut events, mut ops) = make_test_app_with_channels().await;
|
||
let (mut server, mut tui) = prepare_background_exit_test(&app, &mut events, &mut ops).await?;
|
||
let id = ThreadId::new();
|
||
app.side_threads
|
||
.insert(id, SideThreadState::new(ThreadId::new()));
|
||
app.thread_event_channels.insert(
|
||
id,
|
||
ThreadEventChannel::new_with_session(
|
||
THREAD_EVENT_CHANNEL_CAPACITY,
|
||
test_thread_session(id, test_path_buf("/tmp/project")),
|
||
vec![test_turn("side-turn", TurnStatus::InProgress, Vec::new())],
|
||
),
|
||
);
|
||
assert_matches!(
|
||
app.handle_event(&mut tui, &mut server, AppEvent::Exit(ExitMode::Immediate))
|
||
.await?,
|
||
AppRunControl::Continue
|
||
);
|
||
insta::assert_snapshot!(
|
||
"embedded_exit_running_sessions",
|
||
render_bottom_popup(&app.chat_widget, /*width*/ 90)
|
||
);
|
||
app.chat_widget.handle_key_event(KeyCode::Down.into());
|
||
app.chat_widget.handle_key_event(KeyCode::Enter.into());
|
||
assert_matches!(
|
||
app.handle_event(&mut tui, &mut server, events.try_recv()?)
|
||
.await?,
|
||
AppRunControl::Exit(ExitReason::UserRequested)
|
||
);
|
||
server.shutdown().await?;
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn external_writer_view_preserves_draft_from_keys_and_paste() -> Result<()> {
|
||
let (mut app, _events, _operations) = make_test_app_with_channels().await;
|
||
let thread_id = ThreadId::new();
|
||
app.enqueue_primary_thread_session(
|
||
test_thread_session(thread_id, test_path_buf("/tmp/project")),
|
||
Vec::new(),
|
||
)
|
||
.await?;
|
||
app.app_server_target = AppServerTarget::Remote {
|
||
endpoint: crate::RemoteAppServerEndpoint::WebSocket {
|
||
websocket_url: "wss://example.com/".to_string(),
|
||
auth_token: None,
|
||
},
|
||
};
|
||
app.ensure_thread_channel(thread_id).mark_external_writer();
|
||
app.transcript_cells
|
||
.push(Arc::new(history_cell::new_user_prompt(
|
||
"Existing prompt".to_string(),
|
||
Vec::new(),
|
||
Vec::new(),
|
||
Vec::new(),
|
||
)));
|
||
app.chat_widget.insert_str("Retained draft");
|
||
app.chat_widget.show_external_writer_thread();
|
||
let mut app_server = crate::start_embedded_app_server_for_picker(&app.config).await?;
|
||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||
|
||
for offline in [false, true] {
|
||
app.reconnect.offline = offline;
|
||
app.handle_tui_event(
|
||
&mut tui,
|
||
&mut app_server,
|
||
TuiEvent::Key(KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE)),
|
||
)
|
||
.await?;
|
||
app.handle_tui_event(&mut tui, &mut app_server, TuiEvent::Paste("pasted".into()))
|
||
.await?;
|
||
assert_eq!(
|
||
app.chat_widget.composer_text_with_pending(),
|
||
"Retained draft"
|
||
);
|
||
}
|
||
|
||
app.reconnect.offline = false;
|
||
app.handle_tui_event(
|
||
&mut tui,
|
||
&mut app_server,
|
||
TuiEvent::Key(KeyEvent::new(KeyCode::Char('t'), KeyModifiers::CONTROL)),
|
||
)
|
||
.await?;
|
||
assert!(app.overlay.is_some());
|
||
app.handle_tui_event(
|
||
&mut tui,
|
||
&mut app_server,
|
||
TuiEvent::Key(KeyCode::Esc.into()),
|
||
)
|
||
.await?;
|
||
assert!(app.overlay.is_none());
|
||
assert_eq!(
|
||
(
|
||
app.backtrack.primed,
|
||
app.backtrack.overlay_preview_active,
|
||
app.chat_widget.composer_text_with_pending(),
|
||
),
|
||
(false, false, "Retained draft".to_string()),
|
||
);
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn external_writer_view_opens_agents_with_left() -> Result<()> {
|
||
for offline in [false, true] {
|
||
let (mut app, _, _) = make_test_app_with_channels().await;
|
||
app.app_server_target = AppServerTarget::Remote {
|
||
endpoint: crate::resolve_remote_addr("ws://127.0.0.1:4500")?,
|
||
};
|
||
app.chat_widget.insert_str("Retained draft");
|
||
app.chat_widget.show_external_writer_thread();
|
||
app.reconnect.offline = offline;
|
||
let mut app_server = crate::start_embedded_app_server_for_picker(&app.config).await?;
|
||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||
tui.set_owned_screen(/*owned*/ true)?;
|
||
|
||
app.handle_tui_event(
|
||
&mut tui,
|
||
&mut app_server,
|
||
TuiEvent::Key(KeyCode::Left.into()),
|
||
)
|
||
.await?;
|
||
|
||
assert!(
|
||
app.chat_widget
|
||
.selected_index_for_present_view(AGENTS_OVERVIEW_VIEW_ID)
|
||
.is_some()
|
||
);
|
||
assert_eq!(
|
||
app.chat_widget.composer_text_with_pending(),
|
||
"Retained draft"
|
||
);
|
||
tui.set_owned_screen(/*owned*/ false)?;
|
||
app_server.shutdown().await?;
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn external_writer_view_respects_remapped_left() -> Result<()> {
|
||
let config: codex_config::types::TuiKeymap = toml::from_str(
|
||
"[global]\nopen_transcript = 'left'\n[editor]\nmove_left = 'ctrl-b'\n[list]\nmove_left = 'ctrl-h'\n",
|
||
)?;
|
||
let keymap = RuntimeKeymap::from_config(&config).map_err(|err| color_eyre::eyre::eyre!(err))?;
|
||
for (offline, vim) in [(false, false), (true, false), (false, true), (true, true)] {
|
||
let (mut app, _, _) = make_test_app_with_channels().await;
|
||
app.app_server_target = AppServerTarget::Remote {
|
||
endpoint: crate::resolve_remote_addr("ws://127.0.0.1:4500")?,
|
||
};
|
||
app.chat_widget.apply_keymap_update(config.clone(), &keymap);
|
||
app.keymap = keymap.clone();
|
||
if vim {
|
||
app.chat_widget.toggle_vim_mode_and_notify();
|
||
}
|
||
app.chat_widget.show_external_writer_thread();
|
||
app.reconnect.offline = offline;
|
||
let mut app_server = crate::start_embedded_app_server_for_picker(&app.config).await?;
|
||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||
|
||
if !offline && vim {
|
||
insta::assert_snapshot!(render_bottom_popup(&app.chat_widget, /*width*/ 96));
|
||
}
|
||
app.handle_tui_event(
|
||
&mut tui,
|
||
&mut app_server,
|
||
TuiEvent::Key(KeyCode::Left.into()),
|
||
)
|
||
.await?;
|
||
assert!(app.chat_widget.no_modal_or_popup_active());
|
||
assert_eq!(app.overlay.is_some(), !offline);
|
||
app_server.shutdown().await?;
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn daemon_disconnect_exit_summary_includes_reconnect_and_stop_instructions() -> Result<()> {
|
||
let (mut app, _, _) = make_test_app_with_channels().await;
|
||
let thread_id = prepare_running_local_daemon(&mut app)?;
|
||
app.keymap.agents.stop = vec![crate::key_hint::plain(KeyCode::F(10))];
|
||
let mut exit_info = app.exit_info(ExitReason::UserRequested);
|
||
exit_info.token_usage = TokenUsage {
|
||
input_tokens: 10,
|
||
output_tokens: 2,
|
||
total_tokens: 12,
|
||
..Default::default()
|
||
};
|
||
let output = exit_info
|
||
.format_exit_messages(/*color_enabled*/ false)
|
||
.join("\n")
|
||
.replace(&thread_id.to_string(), "THREAD_ID");
|
||
assert_snapshot!("daemon_disconnect_exit", output);
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn remote_disconnect_exit_summary_does_not_require_a_local_rollout_or_print_credentials() {
|
||
let (mut app, _, _) = make_test_app_with_channels().await;
|
||
app.app_server_target = AppServerTarget::Remote {
|
||
endpoint: crate::RemoteAppServerEndpoint::WebSocket {
|
||
websocket_url: "wss://user:secret@example.com:443/?token=private#secret".to_string(),
|
||
auth_token: Some("secret-token".to_string()),
|
||
},
|
||
};
|
||
let thread_id = ThreadId::from_string("123e4567-e89b-12d3-a456-426614174000").unwrap();
|
||
app.active_thread_id = Some(thread_id);
|
||
app.chat_widget.handle_thread_session(test_thread_session(
|
||
thread_id,
|
||
test_path_buf("/tmp/project"),
|
||
));
|
||
let exit_info = app.exit_info(ExitReason::Fatal("connection lost".to_string()));
|
||
let lines = exit_info.format_exit_messages(/*color_enabled*/ false);
|
||
let command = shlex::split(&lines[2]).unwrap();
|
||
assert_eq!(
|
||
crate::resolve_remote_addr(&command[2]).unwrap(),
|
||
crate::RemoteAppServerEndpoint::WebSocket {
|
||
websocket_url: "wss://example.com/".to_string(),
|
||
auth_token: None,
|
||
}
|
||
);
|
||
assert_snapshot!("remote_disconnect_exit", lines.join("\n"));
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn embedded_exit_keeps_the_session_summary() {
|
||
let (mut app, _, _) = make_test_app_with_channels().await;
|
||
let thread_id = prepare_local_daemon_thread(&mut app).unwrap();
|
||
app.app_server_target = AppServerTarget::Embedded;
|
||
let mut exit_info = app.exit_info(ExitReason::UserRequested);
|
||
exit_info.token_usage = TokenUsage {
|
||
output_tokens: 2,
|
||
total_tokens: 2,
|
||
..Default::default()
|
||
};
|
||
exit_info.resume_hint = Some(ResumableThread {
|
||
thread_id,
|
||
thread_name: None,
|
||
});
|
||
let output = exit_info
|
||
.format_exit_messages(/*color_enabled*/ false)
|
||
.join("\n")
|
||
.replace(&thread_id.to_string(), "THREAD_ID");
|
||
assert_snapshot!(output, @"
|
||
Token usage: total=2 input=0 output=2
|
||
To continue this session, run:
|
||
codex resume THREAD_ID
|
||
");
|
||
}
|
||
|
||
fn prepare_local_daemon_thread(app: &mut App) -> Result<ThreadId> {
|
||
app.app_server_target = AppServerTarget::LocalDaemon {
|
||
allow_embedded_fallback: true,
|
||
endpoint: crate::RemoteAppServerEndpoint::UnixSocket {
|
||
socket_path: AbsolutePathBuf::relative_to_current_dir("codex.sock")?,
|
||
},
|
||
};
|
||
let thread_id = ThreadId::new();
|
||
app.active_thread_id = Some(thread_id);
|
||
app.chat_widget.handle_thread_session(test_thread_session(
|
||
thread_id,
|
||
test_path_buf("/tmp/project"),
|
||
));
|
||
Ok(thread_id)
|
||
}
|
||
|
||
fn prepare_running_local_daemon(app: &mut App) -> Result<ThreadId> {
|
||
let thread_id = prepare_local_daemon_thread(app)?;
|
||
app.chat_widget.handle_server_notification(
|
||
turn_started_notification(thread_id, "turn-1"),
|
||
/*replay_kind*/ None,
|
||
);
|
||
Ok(thread_id)
|
||
}
|
||
|
||
async fn open_running_task_exit_menu(
|
||
app: &mut App,
|
||
tui: &mut tui::Tui,
|
||
app_server: &mut AppServerSession,
|
||
) {
|
||
app.handle_key_event(
|
||
tui,
|
||
app_server,
|
||
KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL),
|
||
)
|
||
.await;
|
||
}
|
||
|
||
async fn prepare_background_exit_test(
|
||
app: &App,
|
||
app_event_rx: &mut mpsc::UnboundedReceiver<AppEvent>,
|
||
op_rx: &mut mpsc::UnboundedReceiver<Op>,
|
||
) -> Result<(AppServerSession, tui::Tui)> {
|
||
while app_event_rx.try_recv().is_ok() {}
|
||
while op_rx.try_recv().is_ok() {}
|
||
let app_server = crate::start_embedded_app_server_for_picker(&app.config).await?;
|
||
Ok((app_server, crate::tui::test_support::make_test_tui()?))
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn daemon_ctrl_c_shows_background_exit_menu_and_escape_dismisses_it() -> Result<()> {
|
||
let (mut app, mut app_event_rx, mut op_rx) = make_test_app_with_channels().await;
|
||
prepare_running_local_daemon(&mut app)?;
|
||
let (mut app_server, mut tui) =
|
||
prepare_background_exit_test(&app, &mut app_event_rx, &mut op_rx).await?;
|
||
|
||
open_running_task_exit_menu(&mut app, &mut tui, &mut app_server).await;
|
||
|
||
assert!(!app.chat_widget.no_modal_or_popup_active());
|
||
assert_snapshot!(render_bottom_popup(&app.chat_widget, /*width*/ 90), @r"
|
||
Task is still running
|
||
Choose what happens to the current task.
|
||
|
||
|
||
› 1. Cancel task Stop the current task and stay in Codex
|
||
2. Run in background Exit Codex and leave the task running
|
||
3. Exit Stop the current task and exit Codex
|
||
|
||
enter select · esc back
|
||
");
|
||
|
||
app.handle_key_event(
|
||
&mut tui,
|
||
&mut app_server,
|
||
KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE),
|
||
)
|
||
.await;
|
||
|
||
assert!(app.chat_widget.no_modal_or_popup_active());
|
||
assert!(op_rx.try_recv().is_err());
|
||
assert!(app_event_rx.try_recv().is_err());
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn cancel_task_interrupts_without_exiting() -> Result<()> {
|
||
let (mut app, mut app_event_rx, mut op_rx) = make_test_app_with_channels().await;
|
||
prepare_running_local_daemon(&mut app)?;
|
||
let (mut app_server, mut tui) =
|
||
prepare_background_exit_test(&app, &mut app_event_rx, &mut op_rx).await?;
|
||
open_running_task_exit_menu(&mut app, &mut tui, &mut app_server).await;
|
||
|
||
app.chat_widget
|
||
.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
|
||
let event = app_event_rx.try_recv()?;
|
||
assert_matches!(
|
||
event,
|
||
AppEvent::RunningTaskExit {
|
||
action: RunningTaskExitAction::CancelTask,
|
||
..
|
||
}
|
||
);
|
||
let control = app.handle_event(&mut tui, &mut app_server, event).await?;
|
||
|
||
assert_matches!(control, AppRunControl::Continue);
|
||
assert_matches!(op_rx.try_recv(), Ok(Op::Interrupt));
|
||
assert!(app_event_rx.try_recv().is_err());
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn run_in_background_detaches_without_interrupting_main_or_side_threads() -> Result<()> {
|
||
let (mut app, mut app_event_rx, mut op_rx) = make_test_app_with_channels().await;
|
||
let parent_thread_id = prepare_running_local_daemon(&mut app)?;
|
||
let side_thread_id = ThreadId::new();
|
||
app.side_threads
|
||
.insert(side_thread_id, SideThreadState::new(parent_thread_id));
|
||
let (mut app_server, mut tui) =
|
||
prepare_background_exit_test(&app, &mut app_event_rx, &mut op_rx).await?;
|
||
open_running_task_exit_menu(&mut app, &mut tui, &mut app_server).await;
|
||
|
||
app.chat_widget
|
||
.handle_key_event(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE));
|
||
app.chat_widget
|
||
.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
|
||
let event = app_event_rx.try_recv()?;
|
||
assert_matches!(
|
||
event,
|
||
AppEvent::RunningTaskExit {
|
||
action: RunningTaskExitAction::RunInBackground,
|
||
thread_id,
|
||
} if thread_id == parent_thread_id
|
||
);
|
||
let control = app.handle_event(&mut tui, &mut app_server, event).await?;
|
||
|
||
assert_matches!(control, AppRunControl::Exit(ExitReason::UserRequested));
|
||
assert!(app.side_threads.contains_key(&side_thread_id));
|
||
assert!(op_rx.try_recv().is_err());
|
||
assert!(app_event_rx.try_recv().is_err());
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn exit_interrupts_before_requesting_shutdown() -> Result<()> {
|
||
let (mut app, mut app_event_rx, mut op_rx) = make_test_app_with_channels().await;
|
||
// The UI fixture's placeholder cwd does not exist on clean runners.
|
||
// This test starts a real shell, so keep its working directory alive.
|
||
let cwd = tempdir()?;
|
||
app.config.cwd = cwd.path().abs();
|
||
prepare_running_local_daemon(&mut app)?;
|
||
app.chat_widget
|
||
.set_feature_enabled(Feature::Goals, /*enabled*/ true);
|
||
let (mut app_server, mut tui) =
|
||
prepare_background_exit_test(&app, &mut app_event_rx, &mut op_rx).await?;
|
||
let started = app_server.start_thread(&app.config).await?;
|
||
let thread_id = started.session.thread_id;
|
||
app.active_thread_id = Some(thread_id);
|
||
app.chat_widget
|
||
.handle_thread_session(started.session.clone());
|
||
let goal = app_server
|
||
.thread_goal_set(
|
||
thread_id,
|
||
Some("test goal".to_string()),
|
||
Some(codex_app_server_protocol::ThreadGoalStatus::Paused),
|
||
/*token_budget*/ None,
|
||
)
|
||
.await?
|
||
.goal;
|
||
app.chat_widget.handle_server_notification(
|
||
ServerNotification::ThreadGoalUpdated(
|
||
codex_app_server_protocol::ThreadGoalUpdatedNotification {
|
||
thread_id: thread_id.to_string(),
|
||
turn_id: None,
|
||
goal: codex_app_server_protocol::ThreadGoal {
|
||
status: codex_app_server_protocol::ThreadGoalStatus::Active,
|
||
..goal
|
||
},
|
||
},
|
||
),
|
||
/*replay_kind*/ None,
|
||
);
|
||
// Keep the command alive until interruption, even on a busy runner. Dropping
|
||
// the directory also releases it if the test fails before reaching the exit.
|
||
let running = tempdir()?;
|
||
let running_path = running.path().to_string_lossy();
|
||
let command = if cfg!(windows) {
|
||
format!(
|
||
"Write-Output 'exit-test-ready'; while (Test-Path -LiteralPath '{}') {{ Start-Sleep -Milliseconds 100 }}",
|
||
running_path.replace('\'', "''"),
|
||
)
|
||
} else {
|
||
format!(
|
||
"printf 'exit-test-ready\\n'; while [ -d {} ]; do sleep 0.1; done",
|
||
shlex::try_quote(&running_path)?,
|
||
)
|
||
};
|
||
app_server.thread_shell_command(thread_id, command).await?;
|
||
let turn_id = time::timeout(Duration::from_secs(/*secs*/ 10), async {
|
||
let mut turn_id = None;
|
||
let mut output = String::new();
|
||
loop {
|
||
let event = app_server
|
||
.next_event()
|
||
.await
|
||
.expect("app-server event stream should remain open");
|
||
if let codex_app_server_client::AppServerEvent::ServerNotification(notification) =
|
||
event.event
|
||
{
|
||
match notification.as_ref() {
|
||
ServerNotification::TurnStarted(notification)
|
||
if notification.thread_id == thread_id.to_string() =>
|
||
{
|
||
turn_id = Some(notification.turn.id.clone());
|
||
}
|
||
ServerNotification::CommandExecutionOutputDelta(notification)
|
||
if notification.thread_id == thread_id.to_string() =>
|
||
{
|
||
output.push_str(¬ification.delta);
|
||
}
|
||
ServerNotification::TurnCompleted(notification)
|
||
if notification.thread_id == thread_id.to_string() =>
|
||
{
|
||
panic!("shell command ended before interruption: {notification:?}");
|
||
}
|
||
_ => {}
|
||
}
|
||
}
|
||
if output.contains("exit-test-ready")
|
||
&& let Some(turn_id) = turn_id.as_ref()
|
||
{
|
||
break turn_id.clone();
|
||
}
|
||
}
|
||
})
|
||
.await
|
||
.expect("shell command should be running before interruption");
|
||
app.thread_event_channels.insert(
|
||
thread_id,
|
||
ThreadEventChannel::new_with_session(
|
||
THREAD_EVENT_CHANNEL_CAPACITY,
|
||
started.session,
|
||
vec![test_turn(&turn_id, TurnStatus::InProgress, Vec::new())],
|
||
),
|
||
);
|
||
app.chat_widget.handle_server_notification(
|
||
turn_started_notification(thread_id, &turn_id),
|
||
/*replay_kind*/ None,
|
||
);
|
||
while app_event_rx.try_recv().is_ok() {}
|
||
while op_rx.try_recv().is_ok() {}
|
||
let control = app
|
||
.handle_event(
|
||
&mut tui,
|
||
&mut app_server,
|
||
AppEvent::RunningTaskExit {
|
||
action: RunningTaskExitAction::Exit,
|
||
thread_id,
|
||
},
|
||
)
|
||
.await?;
|
||
|
||
assert_matches!(control, AppRunControl::Continue);
|
||
assert!(op_rx.try_recv().is_err());
|
||
let exit_event = app_event_rx.try_recv()?;
|
||
assert_matches!(exit_event, AppEvent::Exit(ExitMode::ShutdownAfterInterrupt));
|
||
let AppRunControl::Exit(reason) = app
|
||
.handle_event(&mut tui, &mut app_server, exit_event)
|
||
.await?
|
||
else {
|
||
panic!("stop-and-exit must exit");
|
||
};
|
||
assert_matches!(reason, ExitReason::TurnInterrupted);
|
||
let output = app
|
||
.exit_info(reason)
|
||
.format_exit_messages(/*color_enabled*/ false)
|
||
.join("\n")
|
||
.replace(&thread_id.to_string(), "THREAD_ID");
|
||
assert_snapshot!("interrupted_disconnect_exit", output);
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn daemon_ctrl_c_closes_running_side_thread_and_returns_to_parent() -> Result<()> {
|
||
let (mut app, mut app_event_rx, mut op_rx) = make_test_app_with_channels().await;
|
||
let side_thread_id = prepare_running_local_daemon(&mut app)?;
|
||
let (mut app_server, mut tui) =
|
||
prepare_background_exit_test(&app, &mut app_event_rx, &mut op_rx).await?;
|
||
let started = app_server.start_thread(&app.config).await?;
|
||
let parent_thread_id = started.session.thread_id;
|
||
app.primary_thread_id = Some(parent_thread_id);
|
||
app.side_threads
|
||
.insert(side_thread_id, SideThreadState::new(parent_thread_id));
|
||
app.thread_event_channels.insert(
|
||
parent_thread_id,
|
||
ThreadEventChannel::new_with_session(
|
||
THREAD_EVENT_CHANNEL_CAPACITY,
|
||
started.session,
|
||
started.turns,
|
||
),
|
||
);
|
||
|
||
open_running_task_exit_menu(&mut app, &mut tui, &mut app_server).await;
|
||
|
||
assert!(app.chat_widget.no_modal_or_popup_active());
|
||
assert_eq!(app.active_thread_id, Some(parent_thread_id));
|
||
assert!(!app.side_threads.contains_key(&side_thread_id));
|
||
assert!(op_rx.try_recv().is_err());
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn daemon_ctrl_c_hides_background_exit_for_running_background_side_thread() -> Result<()> {
|
||
let (mut app, mut app_event_rx, mut op_rx) = make_test_app_with_channels().await;
|
||
let parent_thread_id = prepare_local_daemon_thread(&mut app)?;
|
||
let side_thread_id = ThreadId::new();
|
||
app.side_threads
|
||
.insert(side_thread_id, SideThreadState::new(parent_thread_id));
|
||
app.thread_event_channels.insert(
|
||
side_thread_id,
|
||
ThreadEventChannel::new_with_session(
|
||
THREAD_EVENT_CHANNEL_CAPACITY,
|
||
test_thread_session(side_thread_id, test_path_buf("/tmp/project")),
|
||
vec![test_turn("side-turn", TurnStatus::InProgress, Vec::new())],
|
||
),
|
||
);
|
||
let (mut app_server, mut tui) =
|
||
prepare_background_exit_test(&app, &mut app_event_rx, &mut op_rx).await?;
|
||
|
||
open_running_task_exit_menu(&mut app, &mut tui, &mut app_server).await;
|
||
assert_snapshot!(render_bottom_popup(&app.chat_widget, /*width*/ 90), @r"
|
||
Task is still running
|
||
Choose what happens to the current task.
|
||
|
||
|
||
› 1. Cancel task Stop the current task and stay in Codex
|
||
2. Exit Stop the current task and exit Codex
|
||
|
||
enter select · esc back
|
||
");
|
||
app.chat_widget
|
||
.handle_key_event(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE));
|
||
app.chat_widget
|
||
.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
|
||
let event = app_event_rx.try_recv()?;
|
||
|
||
assert_matches!(
|
||
event,
|
||
AppEvent::RunningTaskExit {
|
||
action: RunningTaskExitAction::Exit,
|
||
thread_id,
|
||
} if thread_id == side_thread_id
|
||
);
|
||
assert!(op_rx.try_recv().is_err());
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn daemon_ctrl_c_hides_background_exit_with_queued_follow_up() -> Result<()> {
|
||
let (mut app, mut app_event_rx, mut op_rx) = make_test_app_with_channels().await;
|
||
prepare_running_local_daemon(&mut app)?;
|
||
app.chat_widget
|
||
.apply_external_edit("queued follow-up".to_string());
|
||
app.chat_widget
|
||
.handle_key_event(KeyEvent::new(KeyCode::Tab, KeyModifiers::NONE));
|
||
assert_eq!(
|
||
app.chat_widget.queued_user_message_texts(),
|
||
vec!["queued follow-up".to_string()]
|
||
);
|
||
let (mut app_server, mut tui) =
|
||
prepare_background_exit_test(&app, &mut app_event_rx, &mut op_rx).await?;
|
||
|
||
open_running_task_exit_menu(&mut app, &mut tui, &mut app_server).await;
|
||
|
||
assert!(!render_bottom_popup(&app.chat_widget, /*width*/ 90).contains("Run in background"));
|
||
assert!(op_rx.try_recv().is_err());
|
||
assert!(app_event_rx.try_recv().is_err());
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn non_daemon_ctrl_c_keeps_interrupt_behavior() -> Result<()> {
|
||
for target in [
|
||
AppServerTarget::Embedded,
|
||
AppServerTarget::Remote {
|
||
endpoint: crate::RemoteAppServerEndpoint::WebSocket {
|
||
websocket_url: "ws://127.0.0.1:4500".to_string(),
|
||
auth_token: None,
|
||
},
|
||
},
|
||
] {
|
||
let (mut app, mut app_event_rx, mut op_rx) = make_test_app_with_channels().await;
|
||
prepare_running_local_daemon(&mut app)?;
|
||
app.app_server_target = target;
|
||
let (mut app_server, mut tui) =
|
||
prepare_background_exit_test(&app, &mut app_event_rx, &mut op_rx).await?;
|
||
|
||
open_running_task_exit_menu(&mut app, &mut tui, &mut app_server).await;
|
||
|
||
assert!(app.chat_widget.no_modal_or_popup_active());
|
||
assert_matches!(op_rx.try_recv(), Ok(Op::Interrupt));
|
||
assert!(app_event_rx.try_recv().is_err());
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn daemon_ctrl_c_with_draft_preserves_composer_cancellation() -> Result<()> {
|
||
let (mut app, mut app_event_rx, mut op_rx) = make_test_app_with_channels().await;
|
||
prepare_running_local_daemon(&mut app)?;
|
||
app.chat_widget.apply_external_edit("draft".to_string());
|
||
let (mut app_server, mut tui) =
|
||
prepare_background_exit_test(&app, &mut app_event_rx, &mut op_rx).await?;
|
||
|
||
open_running_task_exit_menu(&mut app, &mut tui, &mut app_server).await;
|
||
|
||
assert!(app.chat_widget.no_modal_or_popup_active());
|
||
assert!(app.chat_widget.composer_is_empty());
|
||
assert!(op_rx.try_recv().is_err());
|
||
assert_matches!(
|
||
app_event_rx.try_recv(),
|
||
Ok(AppEvent::AppendMessageHistoryEntry { text, .. }) if text == "draft"
|
||
);
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn daemon_ctrl_c_during_paste_burst_does_not_show_exit_menu() -> Result<()> {
|
||
let (mut app, mut app_event_rx, mut op_rx) = make_test_app_with_channels().await;
|
||
prepare_running_local_daemon(&mut app)?;
|
||
app.chat_widget
|
||
.handle_key_event(KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE));
|
||
assert!(app.chat_widget.composer_text_with_pending().is_empty());
|
||
assert!(!app.chat_widget.composer_is_empty());
|
||
let (mut app_server, mut tui) =
|
||
prepare_background_exit_test(&app, &mut app_event_rx, &mut op_rx).await?;
|
||
|
||
open_running_task_exit_menu(&mut app, &mut tui, &mut app_server).await;
|
||
|
||
assert!(app.chat_widget.no_modal_or_popup_active());
|
||
assert!(!app.chat_widget.composer_is_empty());
|
||
assert_matches!(op_rx.try_recv(), Ok(Op::Interrupt));
|
||
assert!(app_event_rx.try_recv().is_err());
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn daemon_ctrl_c_during_mcp_startup_does_not_show_background_exit_menu() -> Result<()> {
|
||
let (mut app, mut app_event_rx, mut op_rx) = make_test_app_with_channels().await;
|
||
let thread_id = prepare_local_daemon_thread(&mut app)?;
|
||
app.chat_widget
|
||
.set_mcp_startup_expected_servers(["slow".to_string()]);
|
||
app.chat_widget.handle_server_notification(
|
||
ServerNotification::McpServerStatusUpdated(McpServerStatusUpdatedNotification {
|
||
thread_id: Some(thread_id.to_string()),
|
||
name: "slow".to_string(),
|
||
status: McpServerStartupState::Starting,
|
||
error: None,
|
||
failure_reason: None,
|
||
}),
|
||
/*replay_kind*/ None,
|
||
);
|
||
assert!(app.chat_widget.is_task_running_for_test());
|
||
let (mut app_server, mut tui) =
|
||
prepare_background_exit_test(&app, &mut app_event_rx, &mut op_rx).await?;
|
||
|
||
open_running_task_exit_menu(&mut app, &mut tui, &mut app_server).await;
|
||
|
||
assert!(app.chat_widget.no_modal_or_popup_active());
|
||
assert_matches!(op_rx.try_recv(), Ok(Op::Interrupt));
|
||
assert!(app_event_rx.try_recv().is_err());
|
||
Ok(())
|
||
}
|