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

@@ -107,6 +107,10 @@
//! navigation slash commands remain available so users can leave or manage the view. Transcript
//! exports also remain available, including an explicit destination filename.
//!
//! During reconnection, `handle_disconnected_key` edits the draft directly without
//! popup dispatch or submission. Enter and Tab leave the draft intact until reconnection succeeds.
//! Collapsed pastes expand into editable text so the full draft can be copied before quitting.
//!
//! # Reasoning Effort Animations
//!
//! The composer observes the effective reasoning tier whenever model-dependent surfaces refresh.
@@ -292,6 +296,7 @@ mod draft_state;
mod footer_state;
mod history_search;
mod popup_state;
mod reconnect;
mod slash_input;
mod vim_search;
@@ -0,0 +1,37 @@
//! Offline editing bypasses command dispatch and popup actions, retaining the draft in place.
use super::*;
impl ChatComposer {
pub(crate) fn handle_disconnected_key(&mut self, key: KeyEvent) {
self.cancel_history_search();
self.attachments.clear_remote_image_selection();
self.popups.active = ActivePopup::None;
self.set_disable_paste_burst(/*disabled*/ true);
// Expand in reverse order so remaining ranges stay valid. Textarea replacement
// preserves the cursor and other elements, including images and mentions.
let pending_pastes = std::mem::take(&mut self.draft.pending_pastes);
if !pending_pastes.is_empty() {
for element in self.draft.textarea.text_elements().into_iter().rev() {
if let Some((_, text)) = pending_pastes.iter().find(|(placeholder, _)| {
element.placeholder(self.draft.textarea.text()) == Some(placeholder.as_str())
}) {
self.draft
.textarea
.replace_range(element.byte_range.start..element.byte_range.end, text);
}
}
}
// Enter/Tab and configured submit bindings must never consume the draft offline.
// The basic editor reconciles attachments without invoking composer-level shortcuts.
if !matches!(key.code, KeyCode::Enter | KeyCode::Tab)
&& matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat)
{
self.handle_input_basic(key);
}
}
}
#[cfg(test)]
#[path = "reconnect_tests.rs"]
mod tests;
@@ -0,0 +1,60 @@
//! Offline edits retain the same attachment bookkeeping as ordinary text edits.
use super::super::tests::new_test_composer;
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn reconnect_expands_pastes_preserving_images_and_cursor() {
let (mut composer, _) = new_test_composer();
let paste = "x".repeat(LARGE_PASTE_CHAR_THRESHOLD + 1);
composer.handle_paste(paste.clone());
composer.attach_image(PathBuf::from("local.png"));
composer.handle_paste(paste);
let expanded = composer.current_text_with_pending();
let images = composer.draft_snapshot().local_images;
composer.handle_disconnected_key(KeyEvent::new(KeyCode::Char('!'), KeyModifiers::NONE));
let draft = composer.draft_snapshot();
assert_eq!(
(
draft.text,
draft.cursor,
draft.local_images,
draft.pending_pastes
),
(
format!("{expanded}!"),
expanded.len() + 1,
images,
Vec::new()
)
);
assert_eq!(draft.text_elements.len(), 1);
}
#[test]
fn reconnect_edit_removes_deleted_attachment_and_paste_metadata() {
let (mut composer, _) = new_test_composer();
composer.attach_image(PathBuf::from("local.png"));
composer.handle_paste("x".repeat(LARGE_PASTE_CHAR_THRESHOLD + 1));
composer.handle_disconnected_key(KeyEvent::new(KeyCode::Char('u'), KeyModifiers::CONTROL));
let draft = composer.draft_snapshot();
assert_eq!(
(draft.text, draft.local_images, draft.pending_pastes),
(String::new(), Vec::new(), Vec::new())
);
}
#[test]
fn reconnect_edit_cancels_history_preview_without_losing_original_draft() {
let (mut composer, _) = new_test_composer();
composer.set_text_content("original draft".into(), Vec::new(), Vec::new());
composer.draft.textarea.set_cursor(/*pos*/ 14);
composer.begin_history_search();
composer.apply_history_search_result(HistorySearchResult::Found(HistoryEntry::new(
"history preview".into(),
)));
composer.handle_disconnected_key(KeyEvent::new(KeyCode::Char('!'), KeyModifiers::NONE));
assert_eq!(composer.current_text(), "original draft!");
assert!(!composer.history_search_active());
}
+15
View File
@@ -650,6 +650,16 @@ impl BottomPane {
self.push_view(Box::new(modal));
}
/// Edit the draft without invoking popups, submissions, or remote actions.
pub(crate) fn handle_disconnected_key(&mut self, key: KeyEvent) {
self.view_stack.clear();
self.delayed_approval_requests.clear();
self.composer
.set_input_enabled(/*enabled*/ true, /*placeholder*/ None);
self.composer.handle_disconnected_key(key);
self.request_redraw();
}
/// Forward a key event to the active view or the composer.
pub fn handle_key_event(&mut self, key_event: KeyEvent) -> InputResult {
// If a modal/view is active, handle it here; otherwise forward to composer.
@@ -2374,6 +2384,11 @@ mod tests {
assert_eq!(pane.composer_text(), "ya");
assert!(pane.view_stack.is_empty());
assert_eq!(pane.delayed_approval_requests.len(), 1);
pane.handle_disconnected_key(KeyEvent::new(KeyCode::Null, KeyModifiers::NONE));
pane.pre_draw_tick_at(Instant::now() + APPROVAL_PROMPT_TYPING_IDLE_DELAY);
pane.handle_paste(" kept".into());
assert_eq!(pane.composer_text(), "ya kept");
assert!(pane.view_stack.is_empty());
while let Ok(event) = rx.try_recv() {
assert!(
!matches!(event, AppEvent::SubmitThreadOp { .. }),