diff --git a/crates/maestro-acp/src/lib.rs b/crates/maestro-acp/src/lib.rs index f5316a5..1bfb439 100644 --- a/crates/maestro-acp/src/lib.rs +++ b/crates/maestro-acp/src/lib.rs @@ -58,6 +58,24 @@ use tokio_util::compat::{TokioAsyncReadCompatExt, TokioAsyncWriteCompatExt}; pub use agent_client_protocol::schema::v1 as schema; +/// Item on the updates channel. Notifications and turn-done markers share +/// the same FIFO so the event pump processes every notification for a turn +/// before seeing its done signal. +#[derive(Debug)] +pub enum AcpUpdate { + /// A raw `session/update` notification from the adapter. + Notification(SessionNotification), + /// The prompt response for `turn` arrived. All notifications for this + /// turn precede this marker on the wire, and therefore in this channel. + /// The event pump must fire `done` after persisting every preceding + /// notification. + TurnDone { + turn: u64, + result: Result<(), String>, + done: oneshot::Sender>, + }, +} + /// ACP adapter selected from the structured session's resolved command. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum AcpAdapter { @@ -354,7 +372,7 @@ pub struct AcpSession { pid: u32, commands: mpsc::UnboundedSender, child_commands: mpsc::UnboundedSender, - updates: Mutex>>, + updates: Mutex>>, permission_requests: Mutex>>, exit: watch::Receiver>, } @@ -393,9 +411,9 @@ impl AcpSession { wait_exit(&self.exit).await } - /// The stream of raw `session/update` notifications, handed out once. - /// Notifications that arrived before the take are still queued on it. - pub fn take_updates(&self) -> Option> { + /// The stream of updates (notifications + turn-done markers), handed out + /// once. Items that arrived before the take are still queued. + pub fn take_updates(&self) -> Option> { self.updates.lock().expect("updates mutex").take() } @@ -772,7 +790,7 @@ fn config_notification(session_id: &str, options: Vec) -> S async fn run_connection( transport: T, config: ConnectionConfig, - updates_tx: mpsc::UnboundedSender, + updates_tx: mpsc::UnboundedSender, permission_requests: mpsc::UnboundedSender, mut commands_rx: mpsc::UnboundedReceiver, ready_tx: oneshot::Sender>, @@ -800,7 +818,7 @@ async fn run_connection( return Ok(()); } // A dropped receiver only means nobody wants updates any more. - let _ = received_updates.send(notification); + let _ = received_updates.send(AcpUpdate::Notification(notification)); Ok(()) }, agent_client_protocol::on_receive_notification!(), @@ -916,18 +934,18 @@ async fn run_connection( models.as_ref(), ))); if let Some(modes) = &modes { - let _ = updates_tx.send(SessionNotification::new( + let _ = updates_tx.send(AcpUpdate::Notification(SessionNotification::new( SessionId::new(session_id.as_str()), SessionUpdate::CurrentModeUpdate(CurrentModeUpdate::new( modes.current_mode_id.clone(), )), - )); + ))); } if !config_options.lock().expect("config options").is_empty() { - let _ = updates_tx.send(config_notification( + let _ = updates_tx.send(AcpUpdate::Notification(config_notification( &session_id, config_options.lock().expect("config options").clone(), - )); + ))); } let _ = ready_tx.send(Ok(session_id.clone())); @@ -955,16 +973,23 @@ async fn run_connection( let done = Arc::new(Mutex::new(Some(done))); let callback_done = done.clone(); let callback_turn = permission_turn.clone(); + let callback_updates = updates_tx.clone(); if let Err(error) = cx.send_request(request).on_receiving_result(move |result| { let callback_done = callback_done.clone(); let callback_turn = callback_turn.clone(); + let callback_updates = callback_updates.clone(); async move { callback_turn .lock() .expect("permission turn state") .finish(turn); + // Route through the same channel as notifications so the + // event pump sees this after every chunk for the turn. + let result = result.map(|_| ()).map_err(|e| e.to_string()); if let Some(done) = callback_done.lock().expect("prompt sender").take() { - let _ = done.send(result.map(|_| ()).map_err(|e| e.to_string())); + if callback_updates.send(AcpUpdate::TurnDone { turn, result, done }).is_err() { + // Channel closed — nowhere left to signal. + } } Ok(()) } @@ -998,7 +1023,7 @@ async fn run_connection( }).on_receiving_result(move |result| async move { if result.is_ok() { let options = replace_config_value(&callback_options, &callback_config_id, &callback_value); - let _ = callback_updates.send(config_notification(&callback_session, options)); + let _ = callback_updates.send(AcpUpdate::Notification(config_notification(&callback_session, options))); } if let Some(done) = callback_done.lock().expect("config sender").take() { let _ = done.send(result.map(|_| ()).map_err(|error| error.to_string())); @@ -1017,13 +1042,13 @@ async fn run_connection( )).on_receiving_result(move |result| async move { if result.is_ok() { let options = replace_config_value(&callback_options, &callback_config_id, &callback_value); - let _ = callback_updates.send(config_notification(&callback_session, options)); - let _ = callback_updates.send(SessionNotification::new( + let _ = callback_updates.send(AcpUpdate::Notification(config_notification(&callback_session, options))); + let _ = callback_updates.send(AcpUpdate::Notification(SessionNotification::new( SessionId::new(callback_session.as_str()), SessionUpdate::CurrentModeUpdate(CurrentModeUpdate::new( callback_value.as_value_id().expect("checked mode value").0.clone(), )), - )); + ))); } if let Some(done) = callback_done.lock().expect("config sender").take() { let _ = done.send(result.map(|_| ()).map_err(|error| error.to_string())); @@ -1041,7 +1066,7 @@ async fn run_connection( let mut current = callback_options.lock().expect("config options"); current.retain(|option| option.id.0.as_ref().starts_with("__maestro_legacy_")); current.extend(response.config_options.clone()); - let _ = callback_updates.send(config_notification(&callback_session, current.clone())); + let _ = callback_updates.send(AcpUpdate::Notification(config_notification(&callback_session, current.clone()))); } if let Some(done) = callback_done.lock().expect("config sender").take() { let _ = done.send(result.map(|_| ()).map_err(|error| error.to_string()));