From 01db6459cb7abcd6cda3a4009b1f9007d9a96bf1 Mon Sep 17 00:00:00 2001 From: Miguel Palhas Date: Sun, 13 Sep 2026 12:22:59 +0100 Subject: [PATCH] fix(maestro): block event pump until turn is settled The adapter returns the session/prompt response before the agent finishes working. Late-arriving notifications raced past the turn marker and leaked into the next turn, making each response appear one prompt behind. The event pump now sends a settled oneshot through the done payload and awaits it before processing further messages. finish_queued_acp_turn fires it after persisting the turn marker and dispatching any queued prompt. Co-Authored-By: Claude Opus 4.6 --- crates/maestro-acp/src/lib.rs | 27 +++++++++++++++++++---- crates/maestro-daemon/src/session.rs | 32 +++++++++++++++++++++++----- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/crates/maestro-acp/src/lib.rs b/crates/maestro-acp/src/lib.rs index 455bafe..fc63a1b 100644 --- a/crates/maestro-acp/src/lib.rs +++ b/crates/maestro-acp/src/lib.rs @@ -69,13 +69,28 @@ pub enum AcpUpdate { /// turn precede this marker on the wire, and therefore in this channel. /// The event pump must fire `done` after persisting every preceding /// notification. + /// + /// The `done` oneshot carries both the prompt result and a `settled_tx` + /// that the receiver fires after the turn marker is persisted. The + /// event pump awaits the corresponding `settled_rx` before processing + /// further messages so late-arriving notifications cannot leak into the + /// next turn. TurnDone { turn: u64, result: Result<(), String>, - done: oneshot::Sender>, + done: oneshot::Sender, }, } +/// Payload sent through the `done` oneshot inside [`AcpUpdate::TurnDone`]. +#[derive(Debug)] +pub struct TurnDonePayload { + pub result: Result<(), String>, + /// The receiver fires this after persisting the turn marker and + /// dispatching any queued prompt. + pub settled: oneshot::Sender<()>, +} + /// ACP adapter selected from the structured session's resolved command. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum AcpAdapter { @@ -301,7 +316,7 @@ enum Command { Prompt { text: String, turn: u64, - done: oneshot::Sender>, + done: oneshot::Sender, }, SetConfig { config_id: String, @@ -435,7 +450,7 @@ impl AcpSession { &self, text: String, turn: u64, - ) -> Result>, String> { + ) -> Result, String> { let (done_tx, done_rx) = oneshot::channel(); self.commands .send(Command::Prompt { @@ -999,7 +1014,11 @@ async fn run_connection( .expect("permission turn state") .finish(turn); if let Some(done) = done.lock().expect("prompt sender").take() { - let _ = done.send(Err(error.to_string())); + let (settled_tx, _) = oneshot::channel(); + let _ = done.send(TurnDonePayload { + result: Err(error.to_string()), + settled: settled_tx, + }); } } } diff --git a/crates/maestro-daemon/src/session.rs b/crates/maestro-daemon/src/session.rs index 1dae7a1..c29f0aa 100644 --- a/crates/maestro-daemon/src/session.rs +++ b/crates/maestro-daemon/src/session.rs @@ -869,11 +869,24 @@ impl SessionManager { stream.dispatch.queued_turn = Some(turn); let manager = self.clone(); tokio::spawn(async move { - let lifecycle = match done.await { - Ok(Ok(())) => TurnLifecycle::Finish, - Ok(Err(_)) | Err(_) => TurnLifecycle::Failure, + let (lifecycle, settled) = match done.await { + Ok(payload) => { + let lifecycle = if payload.result.is_ok() { + TurnLifecycle::Finish + } else { + TurnLifecycle::Failure + }; + (lifecycle, Some(payload.settled)) + } + Err(_) => (TurnLifecycle::Failure, None), }; manager.finish_queued_acp_turn(id, turn, lifecycle).await; + // Tell the event pump it may resume processing + // notifications — the turn marker is persisted + // and any queued prompt has been dispatched. + if let Some(settled) = settled { + let _ = settled.send(()); + } }); drop(stream); sync_structured_activity(&self.inner, id).await; @@ -2443,8 +2456,17 @@ fn start_acp_event_pump( } => { // Every notification for this turn has been persisted and // broadcast above. Signal the done oneshot so - // finish_queued_acp_turn runs with the transcript complete. - let _ = done.send(result); + // finish_queued_acp_turn runs with the transcript complete, + // then wait for it to persist the turn marker before we + // resume — otherwise late-arriving notifications race into + // the next turn. + let (settled_tx, settled_rx) = tokio::sync::oneshot::channel(); + let _ = done.send(maestro_acp::TurnDonePayload { + result, + settled: settled_tx, + }); + // Block the pump until the turn is fully settled. + let _ = settled_rx.await; } } }