Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
agent_servers: Treat replayed ACP v2 subagent state as history (#66061)
Ben Brandt02a04e06e239
diff --git a/crates/acp_thread/src/acp_thread.rs b/crates/acp_thread/src/acp_thread.rsindex 18b42f839d3..88de3a7ed76 100644--- a/crates/acp_thread/src/acp_thread.rs+++ b/crates/acp_thread/src/acp_thread.rs@@ -3979,6 +3979,26 @@ impl AcpThread {Ok(())}+ /// Ends a history replay into this session, after a successful load or resume. The+ /// subagents RFD treats what the replay reported about subagents as history: it may+ /// stay visible, but must not allow cancelling or confirm current activity until the+ /// agent reports it again. A replayed outcome, such as a completed or failed+ /// subagent, stays, since it confirms no activity.+ pub fn invalidate_replayed_subagent_state(&mut self, cx: &mut Context<Self>) {+ for subagent in self.subagents.values() {+ subagent.update(cx, |subagent, cx| {+ if let Some(association) = subagent.subagent_association.as_mut() {+ association.can_cancel = false;+ cx.notify();+ }+ if subagent.foreground_activity().is_active() {+ subagent.clear_session_state(cx).log_err();+ }+ subagent.invalidate_replayed_subagent_state(cx);+ });+ }+ }+pub fn parent_session_id(&self) -> Option<&acp_v2::SessionId> {self.parent_session_id.as_ref()}diff --git a/crates/agent_servers/src/acp/v2.rs b/crates/agent_servers/src/acp/v2.rsindex bb5a5e6be82..a6417f32560 100644--- a/crates/agent_servers/src/acp/v2.rs+++ b/crates/agent_servers/src/acp/v2.rs@@ -1024,6 +1024,13 @@ impl AcpV2Connection {let this = this.upgrade().context("Connection was released")?;let thread = thread.upgrade().context("Session was released")?;cx.update(|cx| {+ // Ordered responses apply after the replay and before any update sent+ // after the response, so this is the RFD's freshness boundary.+ if replay_history {+ thread.update(cx, |thread, cx| {+ thread.invalidate_replayed_subagent_state(cx)+ });+ }if !thread.read(cx).has_received_session_state() {log::warn!("Agent resumed the session without reporting its current foreground state; activity remains unconfirmed"@@ -2462,6 +2469,106 @@ mod tests {});}+ #[gpui::test]+ async fn v2_replayed_subagent_state_is_history(cx: &mut TestAppContext) {+ let harness = SessionHarness::new(session_capabilities(), vec![], vec![], cx).await;+ let announce_running_child = || {+ harness.send_update(+ "saved",+ acp::SessionUpdate::SubagentUpdate(+ acp::SubagentUpdate::new("child")+ .title("Investigator")+ .capabilities(+ acp::SubagentSessionCapabilities::new()+ .cancel(acp::SessionCancelCapabilities::new()),+ ),+ ),+ );+ harness.send_update(+ "child",+ acp::SessionUpdate::StateUpdate(acp::StateUpdate::Running(+ acp::RunningStateUpdate::new(),+ )),+ );+ };+ let cancelled = || {+ acp::StateUpdate::Idle(+ acp::IdleStateUpdate::new().stop_reason(acp::StopReason::Cancelled),+ )+ };+ for replay_history in [true, false] {+ let loading = harness.reopen(replay_history, cx);+ let (_, responder) = bounded(harness.resume_requests.recv(), cx)+ .await+ .expect("resume");+ harness.resume_state(acp::StateUpdate::Idle(acp::IdleStateUpdate::new()));+ announce_running_child();+ // A subagent whose replayed history ends, as claude-agent-acp replays each one.+ harness.send_update(+ "saved",+ acp::SessionUpdate::SubagentUpdate(acp::SubagentUpdate::new("finished")),+ );+ for state in [+ acp::StateUpdate::Running(acp::RunningStateUpdate::new()),+ cancelled(),+ ] {+ harness.send_update("finished", acp::SessionUpdate::StateUpdate(state));+ }+ responder+ .respond(acp::ResumeSessionResponse::new())+ .expect("resumed");+ let parent = bounded(loading, cx).await.expect("resumed parent");+ let (child, finished) = parent.read_with(cx, |parent, _| {+ (+ parent.subagent(&"child".into()).cloned(),+ parent.subagent(&"finished".into()).cloned(),+ )+ });+ let child = child.expect("the subagent was announced before the response");+ let finished = finished.expect("the subagent was announced before the response");+ finished.read_with(cx, |finished, _| {+ assert_eq!(+ finished.foreground_state(),+ Some(&cancelled()),+ "a replayed outcome confirms no activity, so it stays"+ );+ });+ child.read_with(cx, |child, _| {+ let association = child.subagent_association().expect("subagent");+ assert_eq!(association.title.as_deref(), Some("Investigator"));+ if replay_history {+ assert!(!association.can_cancel, "a replayed grant is history");+ assert_eq!(+ child.foreground_activity(),+ acp_thread::ForegroundActivity::Unconfirmed,+ "replayed activity is history"+ );+ } else {+ // Without replay, updates sent before the response are current.+ assert!(association.can_cancel);+ assert_eq!(+ child.foreground_activity(),+ acp_thread::ForegroundActivity::Running+ );+ }+ });++ if replay_history {+ announce_running_child();+ harness.drain_updates(cx).await;+ child.read_with(cx, |child, _| {+ assert!(child.subagent_association().expect("subagent").can_cancel);+ assert_eq!(+ child.foreground_activity(),+ acp_thread::ForegroundActivity::Running+ );+ });+ }+ drop((parent, child, finished));+ cx.run_until_parked();+ }+ }+#[gpui::test]async fn v2_subagent_updates_route_and_patch_child_sessions(cx: &mut TestAppContext) {use acp_thread::{ThreadStatus, subagents::SubagentAssociation};