Preserve TUI drafts after app-server disconnects (#41911)

## Why

Losing an external app-server connection can leave a submission's outcome uncertain. The TUI must preserve local work without retrying it or allowing stale server events to trigger more actions.

## What changed

- Enter an offline state when an external app-server transport disconnects during startup, event streaming, or submission.
- Keep conversation drafts, queued input, expanded pastes, attachments, and agent-overview input editable while blocking submissions, remote actions, and automatic queue replay.
- Show a connection-lost notice that directs users to copy their work and relaunch, while retaining `Ctrl-C` and an empty-composer `Ctrl-D` as quit shortcuts.
- Cancel pending views and asynchronous work, and ignore stale completions after the disconnect. Embedded app-server behavior is unchanged.

## Testing

Added unit, snapshot, and pseudo-terminal coverage for lost startup and mutation replies, offline draft editing, queued-input recovery, stale-event quarantine, agent-overview behavior, and end-to-end preservation without reconnection or resubmission.

GitOrigin-RevId: 7219a95fad676dda447d625db9e465345b469d58
This commit is contained in:
Eric Traut authored and copyberry committed 2026-08-31 21:00:25 +00:00
1 parent b51b07785b
commit a7913390f7
35 files changed
+987 -38

No files matched your search

@@ -304,6 +304,17 @@ async fn parent_owned_thread_blocks_settings_shortcuts() {
assert_chatwidget_snapshot!("parent_owned_thread_rejects_settings_shortcuts", rendered);
}
#[tokio::test]
async fn disconnect_restores_initial_prompt_without_submitting_it() {
let (mut chat, _events, mut ops) = make_chatwidget_manual(/*model_override*/ None).await;
chat.initial_user_message = Some("CLI prompt".into());
chat.restore_user_message_to_composer("typed draft".into());
chat.pause_for_disconnect();
chat.submit_initial_user_message_if_pending();
assert_eq!(chat.composer_text_with_pending(), "CLI prompt\ntyped draft");
assert_no_submit_op(&mut ops);
}
#[tokio::test]
async fn parent_owned_thread_restores_pending_initial_prompt() {
let (mut chat, _rx, mut op_rx) = make_chatwidget_manual(/*model_override*/ Some("gpt-5")).await;
@@ -1757,6 +1768,7 @@ async fn restore_thread_input_state_applies_running_state_policy() {
rejected_steer_history_records: VecDeque::new(),
queued_user_messages: VecDeque::from([UserMessage::from("already queued").into()]),
queued_user_message_history_records: VecDeque::from([queued_history.clone()]),
recovered_queue: false,
user_turn_pending_start: true,
submit_pending_steers_after_interrupt: true,
current_collaboration_mode: chat.current_collaboration_mode.clone(),
@@ -1790,6 +1802,21 @@ async fn restore_thread_input_state_applies_running_state_policy() {
Some(UserMessage::from("buffered prompt"))
);
chat.pause_for_disconnect();
chat.handle_disconnected_key(KeyEvent::new(KeyCode::Up, KeyModifiers::ALT));
assert!(!chat.has_queued_follow_up_messages());
// Editing the last queued draft must not release the uncertain steer for replay.
assert!(chat.capture_thread_input_state().unwrap().recovered_queue);
chat.handle_disconnected_key(KeyEvent::new(KeyCode::Up, KeyModifiers::ALT));
assert_eq!(
chat.composer_text_with_pending(),
"submitted history\nqueued history"
);
assert!(chat.input_queue.pending_steers.is_empty());
assert!(!chat.capture_thread_input_state().unwrap().recovered_queue);
assert_no_submit_op(&mut op_rx);
chat.set_queue_autosend_suppressed(/*suppressed*/ false);
chat.restore_thread_input_state(
Some(input_state),
ThreadInputStateRestoreMode {
@@ -384,6 +384,7 @@ async fn restore_thread_input_state_restores_pending_steers_without_downgrading_
rejected_steer_history_records: VecDeque::new(),
queued_user_messages,
queued_user_message_history_records: VecDeque::new(),
recovered_queue: false,
user_turn_pending_start: false,
submit_pending_steers_after_interrupt: false,
current_collaboration_mode: chat.current_collaboration_mode.clone(),