From 30d40234755de6174b6e27cebdf0dc730931c91e Mon Sep 17 00:00:00 2001 From: Hare Date: Thu, 3 Sep 2026 08:57:28 +0900 Subject: [PATCH] fix: preserve blocked TUI submissions --- crates/tui/src/app.rs | 14 ++++ crates/tui/src/console/mod.rs | 130 ++++++++++++++++++++++++---------- 2 files changed, 107 insertions(+), 37 deletions(-) diff --git a/crates/tui/src/app.rs b/crates/tui/src/app.rs index 2b963b4d..b1f4fd04 100644 --- a/crates/tui/src/app.rs +++ b/crates/tui/src/app.rs @@ -778,6 +778,20 @@ impl App { Some(self.method_for_run(segments)) } + pub fn restore_unsent_run(&mut self, method: &Method) { + let Method::Run { input } = method else { + return; + }; + self.pending_submit_rollback = None; + if self.input.is_empty() { + self.input.replace_with_segments(input); + self.completion = None; + } else { + self.queued_inputs + .push_front(QueuedInput::new(input.clone())); + } + } + fn method_for_run(&mut self, segments: Vec) -> Method { // TurnHeader / UserMessage blocks are pushed only after the Worker // emits `Event::UserMessage` from a committed `LogEntry::AnnotatedUserInput`. diff --git a/crates/tui/src/console/mod.rs b/crates/tui/src/console/mod.rs index a5b41f27..0106b403 100644 --- a/crates/tui/src/console/mod.rs +++ b/crates/tui/src/console/mod.rs @@ -137,7 +137,6 @@ fn reconcile_attachment_submission( ) -> bool { match event { Event::UserMessage { segments, .. } => { - let had_awaiting = !awaiting_acceptance.is_empty(); let accepted_ids = segments .iter() .filter_map(|segment| match segment { @@ -145,11 +144,11 @@ fn reconcile_attachment_submission( _ => None, }) .collect::>(); - let accepts_exact_submission = had_awaiting - && accepted_ids.len() == awaiting_acceptance.len() - && awaiting_acceptance - .iter() - .all(|file| accepted_ids.contains(&file.artifact_id.as_str())); + let awaited_ids = awaiting_acceptance + .iter() + .map(|file| file.artifact_id.as_str()) + .collect::>(); + let accepts_exact_submission = !awaited_ids.is_empty() && accepted_ids == awaited_ids; if accepts_exact_submission { awaiting_acceptance.clear(); } @@ -818,7 +817,7 @@ async fn drain_worker_events( app.clear_actionbar_notice(); } if let Some(method) = app.handle_worker_event(ev) { - client.send(&method).await?; + send_console_method(app, client, &method).await?; } } None => break, @@ -898,7 +897,7 @@ async fn run_loop( app.clear_actionbar_notice(); } if let Some(method) = app.handle_worker_event(ev) { - client.send(&method).await?; + send_console_method(app, client, &method).await?; } } None => { @@ -943,6 +942,7 @@ async fn send_console_method( method: &Method, ) -> Result<(), Box> { if matches!(method, Method::Run { .. }) && client.has_active_uploads() { + app.restore_unsent_run(method); app.flash_actionbar_notice( "Attachment upload is still in progress; wait or use /clear-attachments.", ActionbarNoticeLevel::Info, @@ -956,6 +956,7 @@ async fn send_console_method( matches!(method, Method::Run { .. }) && !client.pending_attachments.is_empty(); if let Err(error) = client.send(method).await { if sends_attachments { + app.restore_unsent_run(method); app.push_error(format!( "Attachment submission failed: {error}. Pending attachments were retained; retry Send." )); @@ -1475,7 +1476,8 @@ mod tests { use crate::text_selection::{HistoryViewport, SelectionRow}; use async_trait::async_trait; use protocol::{ - Event, RewindTarget, RewindTargetId, Segment, UploadedFileAvailability, UploadedFileRef, + Event, RewindTarget, RewindTargetId, RunResult, Segment, UploadedFileAvailability, + UploadedFileRef, WorkerStatus, }; #[test] @@ -1563,27 +1565,32 @@ mod tests { }; let mut app = App::new("worker".into()); - let method = Method::Run { - input: vec![Segment::text("inspect")], - }; + app.input.insert_str("inspect"); connection.active_uploads = 1; - send_console_method(&mut app, &mut connection, &method) + let blocked = app.submit_input().unwrap(); + assert!(app.input.is_empty()); + send_console_method(&mut app, &mut connection, &blocked) .await .unwrap(); + assert_eq!(app.input.plain_text(), "inspect"); assert_eq!(connection.pending_attachments, vec![file.clone()]); assert!(connection.awaiting_attachment_acceptance.is_empty()); connection.active_uploads = 0; - send_console_method(&mut app, &mut connection, &method) + let failed = app.submit_input().unwrap(); + send_console_method(&mut app, &mut connection, &failed) .await .unwrap(); + assert_eq!(app.input.plain_text(), "inspect"); assert_eq!(connection.pending_attachments, vec![file.clone()]); assert!(connection.awaiting_attachment_acceptance.is_empty()); - send_console_method(&mut app, &mut connection, &method) + let retry = app.submit_input().unwrap(); + send_console_method(&mut app, &mut connection, &retry) .await .unwrap(); + assert!(app.input.is_empty()); assert!(connection.pending_attachments.is_empty()); assert_eq!( connection.awaiting_attachment_acceptance, @@ -1597,6 +1604,49 @@ mod tests { assert!(connection.awaiting_attachment_acceptance.is_empty()); } + #[tokio::test] + async fn queued_attachment_send_failure_restores_draft_without_exiting_console() { + let file = UploadedFileRef { + artifact_id: "artifact-queued".into(), + file_name: "queued.txt".into(), + media_type: "text/plain".into(), + created_at_ms: 1, + availability: UploadedFileAvailability::Available, + byte_len: 1, + sha256: "a".repeat(64), + source_entry_id: None, + }; + let mut connection = ConsoleConnection { + client: Client::new(FailOnceSocket { + fail_next_send: true, + }), + standalone_host: None, + backend_target: None, + pending_attachments: vec![file.clone()], + awaiting_attachment_acceptance: Vec::new(), + upload_tasks: Vec::new(), + active_uploads: 0, + upload_ids: HashMap::new(), + }; + let mut app = App::new("worker".into()); + app.set_worker_status(WorkerStatus::Running); + app.input.insert_str("queued inspect"); + assert!(app.submit_input().is_none()); + + let method = app + .handle_worker_event(Event::RunEnd { + result: RunResult::Finished, + }) + .expect("queued run must be released"); + send_console_method(&mut app, &mut connection, &method) + .await + .unwrap(); + + assert_eq!(app.input.plain_text(), "queued inspect"); + assert_eq!(connection.pending_attachments, vec![file]); + assert!(connection.awaiting_attachment_acceptance.is_empty()); + } + #[test] fn partial_attachment_echo_does_not_acknowledge_an_atomic_submission() { let first = UploadedFileRef { @@ -1616,31 +1666,37 @@ mod tests { }; let mut pending = Vec::new(); let mut awaiting = vec![first.clone(), second.clone()]; + let expected = awaiting.clone(); - assert!(!reconcile_attachment_submission( - &mut pending, - &mut awaiting, - &Event::UserMessage { - segments: vec![Segment::UploadedFile { file: first }], - }, - )); - assert_eq!( - awaiting, + for segments in [ + vec![Segment::UploadedFile { + file: first.clone(), + }], vec![ - UploadedFileRef { - artifact_id: "artifact-1".into(), - file_name: "one.txt".into(), - media_type: "text/plain".into(), - created_at_ms: 1, - availability: UploadedFileAvailability::Available, - byte_len: 1, - sha256: "a".repeat(64), - source_entry_id: None, + Segment::UploadedFile { + file: second.clone(), }, - second, - ] - ); - assert!(pending.is_empty()); + Segment::UploadedFile { + file: first.clone(), + }, + ], + vec![ + Segment::UploadedFile { + file: first.clone(), + }, + Segment::UploadedFile { + file: first.clone(), + }, + ], + ] { + assert!(!reconcile_attachment_submission( + &mut pending, + &mut awaiting, + &Event::UserMessage { segments }, + )); + assert_eq!(awaiting, expected); + assert!(pending.is_empty()); + } } #[test]