Show read-only conversations when resume encounters an active writer (#43253)

## Why

Resuming a conversation that is open in another app fails with an active-writer error. Allow users to inspect its transcript and retry after closing it in the other app.

## What changed

- Fall back to a read-only history snapshot on active-writer conflicts during startup and session selection.
- Replace the composer with an explanatory notice, `R` to retry, exit shortcuts, and the configured transcript shortcut.
- Block input and freeze active command and MCP animations while viewing the snapshot. Preserve initial prompts and existing drafts for a successful retry.
- Keep the subscription when retrying the same thread and preserve read-only state across thread switches and reconnects.

## Testing

Add regression tests for conflict detection, history viewing, draft preservation, same-thread retry without unsubscribing, exit shortcuts, and notice and frozen-tool rendering.

GitOrigin-RevId: fa864cc73143cad9139c22e33b7958569eec20cf
This commit is contained in:
Felipe Coury authored and copyberry committed 2026-09-06 17:27:44 +00:00
1 parent a9896da3fe
commit 4aec23384e
29 files changed
+869 -109

No files matched your search

@@ -1,6 +1,95 @@
use super::*;
use pretty_assertions::assert_eq;
#[tokio::test]
async fn external_writer_view_quits_with_escape_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.chat_widget.config_ref()).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::Esc, KeyModifiers::NONE),
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 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.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.chat_widget.config_ref()).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());
Ok(())
}
#[tokio::test]
async fn daemon_disconnect_exit_summary_includes_reconnect_and_stop_instructions() -> Result<()> {
let (mut app, _, _) = make_test_app_with_channels().await;
@@ -41,6 +41,63 @@ use tokio_tungstenite::tungstenite::Message;
pub(super) type RecordedRequests = Arc<Mutex<Vec<JSONRPCRequest>>>;
pub(super) type RecordingAppServer = (AppServerSession, RecordedRequests, JoinHandle<Result<()>>);
#[tokio::test]
async fn same_thread_retry_keeps_subscription_and_restores_draft() -> Result<()> {
let (mut app, codex_home) = make_history_test_app().await?;
let thread_id = ThreadId::from_string(
&create_fake_rollout(
codex_home.path(),
"2026-01-01T00-00-00",
"2026-01-01T00:00:00Z",
"Saved user message",
Some(app.config.model_provider_id.as_str()),
/*git_info*/ None,
)
.expect("create rollout"),
)?;
let path = Some(rollout_path(
codex_home.path(),
"2026-01-01T00-00-00",
&thread_id.to_string(),
));
let (mut app_server, requests, proxy) = start_recording_app_server(
&app.config,
/*blocked_thread_list*/ None,
/*failed_thread_name*/ None,
)
.await?;
app.enqueue_primary_thread_session(
test_thread_session(thread_id, test_path_buf("/tmp/project")),
Vec::new(),
)
.await?;
app.ensure_thread_channel(thread_id).mark_external_writer();
app.chat_widget.insert_str("Retained draft");
app.chat_widget.show_external_writer_thread();
app.harness_overrides.cwd = Some(app.config.cwd.to_path_buf());
requests.lock().expect("request recorder lock").clear();
let mut tui = crate::tui::test_support::make_test_tui()?;
app.resume_target_session(
&mut tui,
&mut app_server,
crate::resume_picker::SessionTarget {
path,
thread_id,
history_mode: None,
},
)
.await?;
assert_eq!(recorded_params(&requests, "thread/resume").len(), 1);
assert!(recorded_params(&requests, "thread/unsubscribe").is_empty());
assert!(!app.chat_widget.is_external_writer_view());
assert_eq!(
app.chat_widget.composer_text_with_pending(),
"Retained draft"
);
proxy.abort();
Ok(())
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum HistoryCapabilities {
Current,
+68
View File
@@ -1366,3 +1366,71 @@ async fn ignore_same_thread_resume_allows_reattaching_displayed_inactive_thread(
assert!(!ignored);
assert!(app.transcript_cells.is_empty());
}
#[tokio::test]
async fn ignore_same_thread_resume_allows_retrying_read_only_view() -> Result<()> {
let mut app = make_test_app().await;
let thread_id = ThreadId::new();
let session = test_thread_session(thread_id, test_path_buf("/tmp/project"));
app.chat_widget.handle_thread_session(session.clone());
let mut channel = ThreadEventChannel::new_with_session(
THREAD_EVENT_CHANNEL_CAPACITY,
session.clone(),
Vec::new(),
);
channel.mark_external_writer();
app.thread_event_channels.insert(thread_id, channel);
app.activate_thread_channel(thread_id).await;
assert!(
!app.ignore_same_thread_resume(&crate::resume_picker::SessionTarget {
path: Some(test_path_buf("/tmp/project")),
thread_id,
history_mode: None,
})
);
let app_server =
crate::start_embedded_app_server_for_picker(app.chat_widget.config_ref()).await?;
let mut tui = crate::tui::test_support::make_test_tui()?;
app.render_thread_snapshot(
&mut tui,
&app_server,
thread_id,
ThreadEventSnapshot {
session: Some(session),
turns: vec![test_turn("running", TurnStatus::InProgress, Vec::new())],
events: Vec::new(),
input_state: None,
},
/*resume_restored_queue*/ false,
)?;
assert!(app.chat_widget.is_external_writer_view());
assert!(!app.chat_widget.is_task_running_for_test());
Ok(())
}
#[tokio::test]
async fn external_writer_startup_keeps_initial_prompt_as_draft() -> Result<()> {
let (mut app, mut events, _operations) = make_test_app_with_channels().await;
let thread_id = ThreadId::new();
let prompt = "Continue after the other app closes";
set_test_initial_prompt(&mut app, prompt.to_string());
app.chat_widget.show_external_writer_thread();
app.enqueue_primary_thread_session(
test_thread_session(thread_id, test_path_buf("/tmp/project")),
Vec::new(),
)
.await?;
assert_eq!(app.chat_widget.composer_text_with_pending(), prompt);
assert!(!std::iter::from_fn(|| events.try_recv().ok()).any(|event| {
matches!(
event,
AppEvent::SubmitThreadOp {
op: Op::UserTurn { .. },
..
} | AppEvent::CodexOp(Op::UserTurn { .. })
)
}));
Ok(())
}