Reconnect TUI app-server sessions automatically (#41916)

## What changed

- Reconnect out-of-process TUI sessions after the app-server transport drops, bootstrap a fresh client, and resume the active thread.
- Preserve cached transcripts, drafts, runtime settings, task-tool capabilities, and live notification routing across the new connection.
- Keep recovered or possibly submitted input paused for manual review instead of resending it, and leave conversations that cannot be resumed available as read-only cached views.
- Show reconnect progress and actionable failure states while still allowing drafts to be edited or copied.

## Testing

- Add app-level coverage for successful recovery, failed and unavailable resumes, input quarantine, and preserved session state.
- Extend the terminal integration test to verify that a draft survives reconnection, the thread is resumed without starting a turn, and notifications arrive through the replacement connection.

GitOrigin-RevId: 5dcb867c89e226a0c545d6313a77424c233cd47a
This commit is contained in:
Eric Traut authored and copyberry committed 2026-08-31 21:36:49 +00:00
1 parent 9a4b78579a
commit 907c34e867
20 files changed
+1082 -46

No files matched your search

+47 -2
View File
@@ -13,9 +13,47 @@ impl ChatWidget {
self.bottom_pane.ensure_status_indicator();
self.bottom_pane
.set_interrupt_hint_visible(/*visible*/ false);
self.set_status_header("Connection lost — copy your draft, then relaunch".to_string());
self.set_status_header("Reconnecting to app-server…".to_string());
self.set_footer_hint_override(Some(vec![("ctrl+c".into(), "quit".into())]));
self.add_error_message("Connection lost.".into());
self.add_error_message("Connection lost. Attempting to reconnect…".into());
}
/// Restore local input only after replay, which can otherwise move interrupted queues into the draft.
pub(crate) fn restore_reconnected_input(&mut self, input: Option<ThreadInputState>) {
let running = self.turn_lifecycle.agent_turn_running;
if let Some(mut input) = input {
// Its acceptance is unknown. Keep a local copy for manual recovery without
// comparing against partial history or automatically submitting it again.
if input.user_turn_pending_start
&& let Some(prompt) = input.safety_buffering_prompt.take()
{
input.queued_user_messages.push_front(prompt.into());
input
.queued_user_message_history_records
.push_front(UserMessageHistoryRecord::UserMessageText);
input.recovered_queue = true;
}
input.current_collaboration_mode = self.current_collaboration_mode.clone();
// Resume supplies model/effort, but not the user's selected collaboration mode.
if let Some(mask) = input.active_collaboration_mask.as_mut() {
mask.model = Some(self.current_model().to_string());
mask.reasoning_effort = Some(self.effective_reasoning_effort());
}
self.restore_thread_input_state(
Some(input),
ThreadInputStateRestoreMode {
preserve_in_flight_turn: false,
},
);
}
self.turn_lifecycle.restore_running(running, Instant::now());
self.update_task_running_state();
}
pub(crate) fn pause_unavailable_thread(&mut self) {
self.turn_lifecycle
.restore_running(/*running*/ false, Instant::now());
self.update_task_running_state();
}
pub(crate) fn handle_disconnected_view_key(&mut self, key: KeyEvent) {
@@ -41,4 +79,11 @@ impl ChatWidget {
self.bottom_pane.handle_disconnected_key(key);
}
}
pub(crate) fn reconnect_failed(&mut self) {
self.set_status_header("Reconnect failed — check the endpoint, then relaunch".into());
self.add_error_message(
"Automatic reconnect could not restore this session. Your draft is still editable. Copy it before quitting with Ctrl+C, then reconnect with the same command.".into(),
);
}
}
@@ -2413,3 +2413,40 @@ async fn interrupt_prepends_queued_messages_before_existing_composer_text() {
let _ = drain_insert_history(&mut rx);
}
#[tokio::test]
async fn reconnect_holds_only_recovered_input_until_manually_edited() {
for (recovered, pending_start) in [
(None, false),
(Some("review this old input"), false),
(Some("unacknowledged prompt"), true),
] {
let (mut chat, _rx, _ops) = make_chatwidget_manual(/*model_override*/ None).await;
if let Some(text) = recovered {
if pending_start {
chat.input_queue.user_turn_pending_start = true;
chat.safety_buffering_prompt = Some(UserMessage::from(text));
} else {
chat.input_queue
.queued_user_messages
.push_back(UserMessage::from(text).into());
}
}
chat.pause_for_disconnect();
let input = chat.capture_thread_input_state();
let (mut chat, _rx, mut ops) = make_chatwidget_manual(/*model_override*/ None).await;
chat.thread_id = Some(ThreadId::new());
chat.restore_reconnected_input(input);
chat.set_queue_autosend_suppressed(/*suppressed*/ false);
if let Some(text) = recovered {
assert!(!chat.maybe_send_next_queued_input());
assert_eq!(chat.pop_latest_queued_composer_state().unwrap().text, text);
assert_no_submit_op(&mut ops);
}
chat.input_queue
.queued_user_messages
.push_back(UserMessage::from("new follow-up").into());
assert!(chat.maybe_send_next_queued_input());
assert_matches!(next_submit_op(&mut ops), Op::UserTurn { .. });
}
}