Route TUI Windows sandbox setup through the app server (#44945)

## What changed

Use `windowsSandbox/setupStart` for elevated and unelevated setup, and handle completion notifications while retaining the pending approval preset and permission profile selection. Verify the effective sandbox mode before enabling Agent mode.

Keep input locked and setup pending when the start request times out or its response is lost. Block thread replacement during setup, ignore completion notifications for a different mode, and clear interrupted setup state with a restart message after reconnection. Preserve the fallback prompt when elevated setup fails and report unelevated setup failures.

Remove the TUI's direct sandbox setup helpers and sandbox-mode configuration writes.

## Testing

Add regression coverage for uncertain setup responses, matching completion modes, blocked thread replacement with retained input, and interrupted setup during reconnection.

GitOrigin-RevId: 5ec1101a1a69b3f96f01fc2ba6a03112705f4918
This commit is contained in:
Eric Traut authored and copyberry committed 2026-09-12 00:20:30 +00:00
1 parent 39d193d72d
commit cebdb732ea
13 files changed
+452 -348

No files matched your search

@@ -1,4 +1,5 @@
use super::*;
use pretty_assertions::assert_eq;
#[tokio::test]
async fn agents_navigation_requires_local_daemon() -> Result<()> {
@@ -48,3 +49,53 @@ async fn agents_navigation_requires_local_daemon() -> Result<()> {
app_server.shutdown().await?;
Ok(())
}
#[tokio::test]
async fn pending_windows_sandbox_setup_blocks_thread_replacement() -> Result<()> {
use crate::app_event::WindowsSandboxEnableMode;
let (mut app, mut events, _ops) = make_test_app_with_channels().await;
while events.try_recv().is_ok() {}
let mut app_server = start_config_write_test_app_server(&app).await?;
let mut tui = crate::tui::test_support::make_test_tui()?;
let current = ThreadId::new();
let other = ThreadId::new();
app.active_thread_id = Some(current);
app.primary_thread_id = Some(current);
let preset = codex_utils_approval_presets::builtin_approval_presets()
.into_iter()
.find(|preset| preset.id == "auto")
.expect("auto preset");
app.windows_sandbox.pending_setup = Some((WindowsSandboxEnableMode::Elevated, preset, None));
let prompt = crate::chatwidget::create_initial_user_message(
Some("review this".to_string()),
Vec::new(),
Vec::new(),
);
app.chat_widget.initial_user_message = prompt.clone();
app.select_agent_thread(&mut tui, &mut app_server, other)
.await?;
assert_eq!(app.active_thread_id, Some(current));
assert_eq!(app.chat_widget.initial_user_message, prompt);
let cell = match events.try_recv() {
Ok(AppEvent::InsertHistoryCell(cell)) => cell,
other => panic!("expected setup navigation message, got {other:?}"),
};
let rendered = lines_to_single_string(&cell.display_lines(/*width*/ 100));
insta::assert_snapshot!(rendered, @"• Finish Windows sandbox setup before switching threads.");
app.chat_widget.initial_user_message = None;
app.run_agents_overview_action(
&mut tui,
&mut app_server,
current,
crate::app_event::AgentsOverviewAction::Archive,
)
.await?;
assert_eq!(app.active_thread_id, Some(current));
assert!(events.try_recv().is_err());
app_server.shutdown().await?;
Ok(())
}
@@ -6,6 +6,7 @@ use super::*;
use crate::app::reconnect::ReconnectPresentation;
use crate::app::reconnect::reconnect;
use crate::app_event::AgentsOverviewThreadRefresh;
use crate::app_event::WindowsSandboxEnableMode;
use crate::app_server_session::ThreadParamsMode;
use codex_app_server_client::AppServerEvent;
use pretty_assertions::assert_eq;
@@ -170,7 +171,16 @@ async fn reconnect_daemon_command_center_after_socket_replacement_without_a_conv
app.app_server_target = AppServerTarget::LocalDaemon {
endpoint: endpoint.clone(),
};
if previous_thread.is_none() {
let interrupted_setup = previous_thread.is_none() && !overview_initialized;
if interrupted_setup {
let preset = codex_utils_approval_presets::builtin_approval_presets()
.into_iter()
.find(|preset| preset.id == "auto")
.expect("auto preset");
app.windows_sandbox.pending_setup =
Some((WindowsSandboxEnableMode::Elevated, preset, None));
app.windows_sandbox.setup_started_at = Some(Instant::now());
} else if previous_thread.is_none() {
app.chat_widget.windows_sandbox_elevated_setup_complete = true;
}
let available = Arc::new(std::sync::atomic::AtomicBool::new(false));
@@ -317,7 +327,27 @@ async fn reconnect_daemon_command_center_after_socket_replacement_without_a_conv
CODEX_CLI_VERSION,
)
.await?;
if previous_thread.is_none() {
if interrupted_setup {
assert!(app.windows_sandbox.pending_setup.is_none());
assert!(app.windows_sandbox.setup_started_at.is_none());
let mut retained = Vec::new();
let mut saw_warning = false;
while let Ok(event) = events.try_recv() {
if let AppEvent::InsertHistoryCell(cell) = &event {
let rendered = lines_to_single_string(&cell.display_lines(/*width*/ 100));
if rendered.contains("Windows sandbox setup was interrupted") {
insta::assert_snapshot!(rendered, @"■ Windows sandbox setup was interrupted. Restart Codex before using Agent mode.");
saw_warning = true;
continue;
}
}
retained.push(event);
}
assert!(saw_warning);
for event in retained {
app.app_event_tx.send(event);
}
} else if previous_thread.is_none() {
assert!(app.chat_widget.windows_sandbox_elevated_setup_complete);
}
assert!(!app.reconnect.offline);
+103
View File
@@ -63,6 +63,109 @@ async fn windows_sandbox_setup_skips_remote_default_executor() -> Result<()> {
Ok(())
}
#[tokio::test]
async fn uncertain_windows_sandbox_setup_keeps_intent_and_input_locked() {
use crate::app_event::WindowsSandboxEnableMode;
use codex_app_server_protocol::WindowsSandboxSetupStartResponse;
let preset = builtin_approval_presets()
.into_iter()
.find(|preset| preset.id == "auto")
.expect("auto preset");
for response in [
Ok(Err(TypedRequestError::Transport {
method: "windowsSandbox/setupStart".to_string(),
source: std::io::Error::new(std::io::ErrorKind::BrokenPipe, "connection closed"),
})),
tokio::time::timeout(
Duration::ZERO,
std::future::pending::<
std::result::Result<WindowsSandboxSetupStartResponse, TypedRequestError>,
>(),
)
.await,
] {
let (mut app, mut events, _ops) = make_test_app_with_channels().await;
while events.try_recv().is_ok() {}
app.windows_sandbox.pending_setup =
Some((WindowsSandboxEnableMode::Elevated, preset.clone(), None));
app.windows_sandbox.setup_started_at = Some(Instant::now());
app.chat_widget.show_windows_sandbox_setup_status();
let transport_error = matches!(&response, Ok(Err(TypedRequestError::Transport { .. })));
app.finish_windows_sandbox_setup_start(response);
assert!(app.windows_sandbox.pending_setup.is_some());
app.chat_widget
.handle_key_event(KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE));
assert!(app.chat_widget.composer_text_with_pending().is_empty());
let cell = match events.try_recv() {
Ok(AppEvent::InsertHistoryCell(cell)) => cell,
other => panic!("expected setup error message, got {other:?}"),
};
let rendered = lines_to_single_string(&cell.display_lines(/*width*/ 160));
if transport_error {
insta::assert_snapshot!(rendered, @"■ Windows sandbox setup response was lost. Waiting for completion or reconnection.");
} else {
insta::assert_snapshot!(rendered, @"■ Windows sandbox setup request timed out. Waiting for completion; restart Codex if it does not finish.");
}
}
}
#[tokio::test]
async fn windows_sandbox_setup_completion_requires_matching_pending_mode() -> Result<()> {
use crate::app_event::WindowsSandboxEnableMode;
use codex_app_server_protocol::WindowsSandboxSetupCompletedNotification;
use codex_app_server_protocol::WindowsSandboxSetupMode;
let (mut app, mut events, _ops) = make_test_app_with_channels().await;
while events.try_recv().is_ok() {}
let app_server =
crate::start_embedded_app_server_for_picker(app.chat_widget.config_ref()).await?;
let preset = builtin_approval_presets()
.into_iter()
.find(|preset| preset.id == "auto")
.expect("auto preset");
app.windows_sandbox.pending_setup = Some((WindowsSandboxEnableMode::Elevated, preset, None));
app.windows_sandbox.setup_started_at = Some(Instant::now());
for mode in [
WindowsSandboxSetupMode::Unelevated,
WindowsSandboxSetupMode::Elevated,
] {
app.handle_app_server_event(
&app_server,
codex_app_server_client::AppServerEvent::ServerNotification(Box::new(
ServerNotification::WindowsSandboxSetupCompleted(
WindowsSandboxSetupCompletedNotification {
mode,
success: true,
error: None,
},
),
)),
)
.await;
assert_eq!(
app.windows_sandbox.pending_setup.is_some(),
mode == WindowsSandboxSetupMode::Unelevated
);
}
assert!(!app.chat_widget.windows_sandbox_elevated_setup_complete);
assert!(app.windows_sandbox_blocks_thread_switch());
assert!(matches!(
events.try_recv(),
Ok(AppEvent::EnableWindowsSandboxForAgentMode {
mode: WindowsSandboxEnableMode::Elevated,
..
})
));
assert!(events.try_recv().is_err());
app_server.shutdown().await?;
Ok(())
}
fn startup_bottom_pane() -> (BottomPane, UnboundedReceiver<AppEvent>) {
let (app_event_tx, app_event_rx) = unbounded_channel();
(