Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
acp_thread: Follow the subagents RFD when ACP subagents are cancelled (#66057)
Ben Brandtc07b87624c97
diff --git a/crates/acp_thread/src/acp_thread.rs b/crates/acp_thread/src/acp_thread.rsindex 88de3a7ed76..d1b6cfae98a 100644--- a/crates/acp_thread/src/acp_thread.rs+++ b/crates/acp_thread/src/acp_thread.rs@@ -4625,9 +4625,16 @@ impl AcpThread {cx.emit(AcpThreadEvent::Stopped {activity_generation: self.activity.generation(),activity_duration: self.activity.duration(),- stop_reason,+ stop_reason: stop_reason.clone(),});}+ // A subagent's work can stop because Zed cancelled an ancestor. Its requests stay+ // answerable until it reports that, and then go as if Zed had cancelled it.+ if self.subagent_association.is_some()+ && matches!(stop_reason, Some(acp_v2::StopReason::Cancelled))+ {+ self.cancel_local_actions(RequestPermissionOutcome::Cancelled, cx);+ }}if previous != self.foreground_activity() {cx.emit(AcpThreadEvent::StatusChanged);@@ -7379,6 +7386,12 @@ impl AcpThread {permission_outcome: RequestPermissionOutcome,cx: &mut Context<Self>,) -> Result<Task<Result<()>>> {+ anyhow::ensure!(+ self.subagent_association+ .as_ref()+ .is_none_or(|association| association.can_cancel),+ "This subagent's parent does not allow cancelling it"+ );self.flush_streaming_text(cx);if self.uses_reported_activity() {self.activity.ensure_observable()?;@@ -7387,7 +7400,16 @@ impl AcpThread {{self.connection.cancel(self.session_id(), cx)?;}- self.cancel_generic_permission_requests(cx);+ if self.subagent_association.is_some() {+ // The subagents RFD has a cancelled subagent answer all of its permission+ // requests, including the tool-call ones a v1 subagent makes.+ let request_ids = self.permission_requests.keys().copied().collect::<Vec<_>>();+ for id in request_ids {+ self.cancel_permission_request(id, cx);+ }+ } else {+ self.cancel_generic_permission_requests(cx);+ }let receiver = self.activity.wait_for_idle()?;let Some(receiver) = receiver else {return Ok(Task::ready(Ok(())));@@ -11683,17 +11705,19 @@ mod tests {(thread, connection)}- async fn new_agent_owned_test_thread(cx: &mut TestAppContext) -> Entity<AcpThread> {+ async fn new_agent_owned_test_thread(+ cx: &mut TestAppContext,+ ) -> (Entity<AcpThread>, Rc<StubAgentConnection>) {let project = Project::test(FakeFs::new(cx.executor()), [], cx).await;let connection = Rc::new(StubAgentConnection::new());- cx.update(|cx| {+ let thread = cx.update(|cx| {let action_log = cx.new(|_| ActionLog::new(project.clone()));cx.new(|cx| {AcpThread::new(Some(acp_v2::SessionId::new("parent")),None,None,- connection,+ connection.clone(),project,action_log,acp_v2::SessionId::new("child"),@@ -11702,7 +11726,151 @@ mod tests {).agent_owned()})- })+ });+ cx.update(|cx| connection.track_session(&thread, cx));+ (thread, connection)+ }++ fn idle_with(stop_reason: acp_v2::StopReason) -> acp_v2::StateUpdate {+ acp_v2::StateUpdate::Idle(acp_v2::IdleStateUpdate::new().stop_reason(stop_reason))+ }++ fn legacy_permission_request(+ thread: &mut AcpThread,+ tool_call_id: &str,+ cx: &mut Context<AcpThread>,+ ) -> Task<RequestPermissionOutcome> {+ thread+ .request_tool_call_authorization(+ acp_v1::ToolCall::new(acp_v1::ToolCallId::new(tool_call_id), "Edit").into(),+ PermissionOptions::Flat(vec![acp_v2::PermissionOption::new(+ "allow",+ "Allow",+ acp_v2::PermissionOptionKind::AllowOnce,+ )]),+ AuthorizationKind::PermissionGrant,+ cx,+ )+ .expect("permission request")+ }++ #[gpui::test]+ async fn test_cancelling_a_subagent_needs_its_parents_permission(cx: &mut TestAppContext) {+ init_test(cx);+ let (thread, connection) = new_agent_owned_test_thread(cx).await;+ thread.update(cx, |thread, cx| {+ thread+ .update_session_state(+ acp_v2::StateUpdate::Running(acp_v2::RunningStateUpdate::new()),+ cx,+ )+ .expect("running");+ });+ let refused = thread.update(cx, |thread, cx| thread.cancel(cx));+ assert!(refused.await.is_err());+ assert!(connection.take_cancel_calls().is_empty());++ thread.update(cx, |thread, cx| {+ thread+ .update_subagent_association(+ &acp_v2::SubagentUpdate::new("child").capabilities(+ acp_v2::SubagentSessionCapabilities::new()+ .cancel(acp_v2::SessionCancelCapabilities::new()),+ ),+ cx,+ )+ .expect("subagent")+ });+ // A v1 subagent's permission requests are anchored to its tool calls.+ let permission = thread.update(cx, |thread, cx| {+ legacy_permission_request(thread, "edit", cx)+ });+ let cancellation = thread.update(cx, |thread, cx| thread.cancel(cx));+ assert_eq!(+ connection.take_cancel_calls(),+ vec![acp_v2::SessionId::new("child")]+ );+ thread.read_with(cx, |thread, _| {+ assert_eq!(thread.pending_permission_requests().count(), 0);+ });+ assert!(matches!(+ permission.await,+ RequestPermissionOutcome::Cancelled+ ));+ thread.update(cx, |thread, cx| {+ thread+ .update_session_state(idle_with(acp_v2::StopReason::Cancelled), cx)+ .expect("idle")+ });+ cancellation+ .await+ .expect("the subagent confirmed cancellation");+ }++ #[gpui::test]+ async fn test_a_cancelled_subagent_releases_its_pending_actions(cx: &mut TestAppContext) {+ init_test(cx);+ let (thread, _connection) = new_agent_owned_test_thread(cx).await;+ let running = || acp_v2::StateUpdate::Running(acp_v2::RunningStateUpdate::new());+ thread.update(cx, |thread, cx| {+ thread.update_session_state(running(), cx).expect("running");+ });+ let (generic, legacy, elicitation) = thread.update(cx, |thread, cx| {+ let request = test_generic_permission_request(thread, "allow");+ let (_, generic) = thread.request_permission(request, cx).expect("request");+ let legacy = legacy_permission_request(thread, "edit", cx);+ let session_id = thread.session_id().clone();+ let elicitation = thread+ .request_elicitation(+ acp_v2::CreateElicitationRequest::new(+ acp_v2::ElicitationFormMode::new(+ acp_v2::ElicitationSessionScope::new(session_id),+ acp_v2::ElicitationSchema::new().string("name", true),+ ),+ "Provide a name",+ ),+ cx,+ )+ .expect("elicitation");+ (generic, legacy, elicitation)+ });++ // Work that ends normally may leave background requests that still apply.+ thread.update(cx, |thread, cx| {+ thread+ .update_session_state(idle_with(acp_v2::StopReason::EndTurn), cx)+ .expect("idle");+ assert_eq!(thread.pending_permission_requests().count(), 2);+ thread.update_session_state(running(), cx).expect("running");+ });++ // Zed did not cancel this subagent: its work stopped with an ancestor's.+ thread.update(cx, |thread, cx| {+ thread+ .update_session_state(idle_with(acp_v2::StopReason::Cancelled), cx)+ .expect("idle");+ assert_eq!(thread.pending_permission_requests().count(), 0);+ let (_, tool_call) = thread+ .tool_call(&acp_v2::ToolCallId::new("edit"))+ .expect("tool call");+ assert_eq!(tool_call.status(), ToolCallStatus::Canceled);+ let elicitation_id = thread+ .entries()+ .iter()+ .find_map(|entry| match entry {+ AgentThreadEntry::Elicitation(id) => Some(id.clone()),+ _ => None,+ })+ .expect("elicitation entry");+ let (_, elicitation) = thread.elicitation(&elicitation_id).expect("elicitation");+ assert!(matches!(elicitation.status, ElicitationStatus::Canceled));+ });+ assert_eq!(generic.await, acp_v2::RequestPermissionOutcome::Cancelled);+ assert!(matches!(legacy.await, RequestPermissionOutcome::Cancelled));+ assert!(matches!(+ elicitation.await.action,+ acp_v2::ElicitationAction::Cancel+ ));}#[gpui::test]@@ -11710,7 +11878,7 @@ mod tests {cx: &mut TestAppContext,) {init_test(cx);- let thread = new_agent_owned_test_thread(cx).await;+ let (thread, _connection) = new_agent_owned_test_thread(cx).await;thread.update(cx, |thread, cx| {assert!(thread.uses_reported_activity());assert_eq!(diff --git a/crates/acp_thread/src/connection.rs b/crates/acp_thread/src/connection.rsindex 2c1359060ae..2ac49e3bbe6 100644--- a/crates/acp_thread/src/connection.rs+++ b/crates/acp_thread/src/connection.rs@@ -910,7 +910,7 @@ mod test_support {use action_log::ActionLog;use collections::{HashMap, VecDeque};use futures::{channel::oneshot, future::try_join_all};- use gpui::{AppContext as _, WeakEntity};+ use gpui::{AppContext as _, Entity, WeakEntity};use parking_lot::Mutex;use crate::AuthorizationKind;@@ -1126,6 +1126,18 @@ mod test_support {sender}+ /// Makes a session this connection did not create, such as an ACP subagent, known to+ /// it, as a real connection knows the subagents it announced.+ pub fn track_session(&self, thread: &Entity<AcpThread>, cx: &App) {+ self.sessions.lock().insert(+ thread.read(cx).session_id().clone(),+ Session {+ thread: thread.downgrade(),+ response_tx: None,+ },+ );+ }+pub fn take_cancel_calls(&self) -> Vec<acp_v2::SessionId> {std::mem::take(&mut *self.cancel_calls.lock())}