Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

agent_ui: Don't load tool-call-referenced sessions where the subagents RFD applies (#66095)

Ben Brandtc8995a085a99
diff --git a/crates/acp_thread/src/connection.rs b/crates/acp_thread/src/connection.rs
index 9ad1519c28f..d7f1bb3acf4 100644
--- a/crates/acp_thread/src/connection.rs
+++ b/crates/acp_thread/src/connection.rs
@@ -135,6 +135,12 @@ pub trait AgentConnection {
false
}
+ /// Whether subagents follow the ACP subagents RFD on this connection: they are discovered
+ /// only through their parent's announcements, and must never be loaded or resumed.
+ fn announces_subagents(&self) -> bool {
+ false
+ }
+
/// Whether conversation teardown should invoke `close_session`.
/// The default preserves existing UI cleanup; backend-owned release hooks are separate.
/// Autonomous backends can opt out without disabling explicit close support.
@@ -988,6 +994,7 @@ mod test_support {
supports_retry: bool,
supports_load_session: bool,
supports_close_session: bool,
+ announces_subagents: bool,
close_sessions_on_release: bool,
supports_session_additional_directories: bool,
supports_set_title: bool,
@@ -1037,6 +1044,7 @@ mod test_support {
supports_retry: false,
supports_load_session: false,
supports_close_session: false,
+ announces_subagents: false,
close_sessions_on_release: true,
supports_session_additional_directories: false,
supports_set_title: true,
@@ -1175,6 +1183,11 @@ mod test_support {
self
}
+ pub fn with_announced_subagents(mut self, announces_subagents: bool) -> Self {
+ self.announces_subagents = announces_subagents;
+ self
+ }
+
pub fn with_close_sessions_on_release(mut self, close_sessions_on_release: bool) -> Self {
self.close_sessions_on_release = close_sessions_on_release;
self
@@ -1331,6 +1344,10 @@ mod test_support {
self.supports_close_session
}
+ fn announces_subagents(&self) -> bool {
+ self.announces_subagents
+ }
+
fn should_close_sessions_on_release(&self) -> bool {
self.supports_close_session && self.close_sessions_on_release
}
diff --git a/crates/agent_servers/src/acp/v1.rs b/crates/agent_servers/src/acp/v1.rs
index b064ad2893f..de7f7ac2eb3 100644
--- a/crates/agent_servers/src/acp/v1.rs
+++ b/crates/agent_servers/src/acp/v1.rs
@@ -79,6 +79,8 @@ pub struct AcpConnection {
auth_methods: Vec<acp_v2::AuthMethod>,
agent_server_store: WeakEntity<AgentServerStore>,
agent_capabilities: acp::AgentCapabilities,
+ /// Whether Zed advertised the subagents RFD, so the agent reports subagents with it.
+ accepts_subagent_updates: bool,
request_elicitations: Entity<ElicitationStore>,
defaults: AcpConnectionDefaults,
_child: Option<transport::OwnedChild>,
@@ -561,13 +563,14 @@ impl AcpConnection {
let request_elicitations = cx.new(|_| ElicitationStore::default());
let client_capabilities = client_capabilities_for_agent(&agent_id);
+ let accepts_subagent_updates = client_capabilities.subagents.is_some();
let dispatch_context = ClientContext {
sessions: sessions.clone(),
session_list: client_session_list.clone(),
request_elicitations: request_elicitations.clone(),
active_request_scopes: None,
pending_session_updates: None,
- accepts_subagent_updates: client_capabilities.subagents.is_some(),
+ accepts_subagent_updates,
};
let (mut runtime, shutdown) = runtime::ClientRuntime::new(dispatch_context, cx);
let dispatch_tx = runtime.sender();
@@ -718,6 +721,7 @@ impl AcpConnection {
sessions,
pending_sessions: RefCell::new(HashMap::default()),
agent_capabilities: response.agent_capabilities,
+ accepts_subagent_updates,
request_elicitations,
defaults,
session_list,
@@ -742,6 +746,7 @@ impl AcpConnection {
connection: ConnectionTo<Agent>,
sessions: Rc<RefCell<HashMap<acp_v2::SessionId, AcpSession>>>,
agent_capabilities: acp::AgentCapabilities,
+ accepts_subagent_updates: bool,
request_elicitations: Entity<ElicitationStore>,
agent_server_store: WeakEntity<AgentServerStore>,
io_task: Task<()>,
@@ -763,6 +768,7 @@ impl AcpConnection {
auth_methods: vec![],
agent_server_store,
agent_capabilities,
+ accepts_subagent_updates,
request_elicitations,
defaults,
_child: None,
@@ -1461,6 +1467,10 @@ impl AgentConnection for AcpConnection {
self.agent_capabilities.load_session
}
+ fn announces_subagents(&self) -> bool {
+ self.accepts_subagent_updates
+ }
+
fn supports_resume_session(&self) -> bool {
self.agent_capabilities
.session_capabilities
@@ -2273,6 +2283,7 @@ pub mod test_support {
client_conn,
sessions,
agent_capabilities,
+ false,
request_elicitations,
agent_server_store,
client_io_task,
@@ -6969,6 +6980,7 @@ mod tests {
client_conn,
sessions,
acp::AgentCapabilities::default(),
+ false,
request_elicitations,
WeakEntity::new_invalid(),
client_io_task,
@@ -7594,6 +7606,7 @@ exit 7
client_conn,
sessions,
agent_capabilities,
+ accepts_subagent_updates,
request_elicitations,
agent_server_store,
client_io_task,
@@ -8935,6 +8948,11 @@ exit 7
_keep_agent_alive,
) = connect_fake_agent(None, cx).await;
+ assert!(
+ !connection.announces_subagents(),
+ "a v1 connection that did not advertise subagents does not follow the RFD"
+ );
+
// Queue up some history updates that the fake agent will stream to
// the client during the `load_session` call, before responding.
*load_session_updates
@@ -9015,6 +9033,10 @@ exit 7
cx,
)
.await;
+ assert!(
+ connection.announces_subagents(),
+ "a v1 connection that advertised subagents follows the RFD"
+ );
*load_session_updates
.lock()
.expect("load_session_updates mutex poisoned") = vec![
diff --git a/crates/agent_servers/src/acp/v2.rs b/crates/agent_servers/src/acp/v2.rs
index b4752d9fb97..95ee27ae061 100644
--- a/crates/agent_servers/src/acp/v2.rs
+++ b/crates/agent_servers/src/acp/v2.rs
@@ -1452,6 +1452,11 @@ impl AgentConnection for AcpV2Connection {
self.initialize_response.capabilities.session.is_some()
}
+ fn announces_subagents(&self) -> bool {
+ // Subagents are part of v2's baseline session model.
+ true
+ }
+
fn close_session(
self: Rc<Self>,
session_id: &acp::SessionId,
@@ -2679,6 +2684,7 @@ mod tests {
use agent_client_protocol::schema::MaybeUndefined;
let harness = SessionHarness::new(session_capabilities(), vec![], vec![], cx).await;
+ assert!(harness.client.announces_subagents());
let parent = harness.thread(cx).await;
harness.send_update(
"session",
diff --git a/crates/agent_ui/src/conversation_view.rs b/crates/agent_ui/src/conversation_view.rs
index 95234fc3c60..6096e43ba1c 100644
--- a/crates/agent_ui/src/conversation_view.rs
+++ b/crates/agent_ui/src/conversation_view.rs
@@ -2587,6 +2587,8 @@ impl ConversationView {
/// Restores a child session that a tool call references but its parent does not
/// own, such as a native subagent from a thread loaded from the database. Children
/// the parent already owns are never loaded: ACP forbids loading subagent sessions.
+ /// Neither is any session where the subagents RFD applies, since a child referenced
+ /// there may still be announced, and loading it first would reject the announcement.
fn load_subagent_session(
&mut self,
subagent_id: acp_v2::SessionId,
@@ -2598,6 +2600,7 @@ impl ConversationView {
};
if connected.threads.contains_key(&subagent_id)
|| !connected.connection.supports_load_session()
+ || connected.connection.announces_subagents()
{
return;
}
@@ -6896,6 +6899,32 @@ pub(crate) mod tests {
}
}
+ #[gpui::test]
+ async fn test_referenced_subagents_load_only_without_the_subagents_rfd(
+ cx: &mut TestAppContext,
+ ) {
+ init_test(cx);
+ for announces_subagents in [false, true] {
+ let connection = StubAgentConnection::new()
+ .with_supports_load_session(true)
+ .with_announced_subagents(announces_subagents);
+ let (conversation_view, cx) =
+ setup_conversation_view(StubAgentServer::new(connection), cx).await;
+ let root_session_id = active_thread(&conversation_view, cx)
+ .read_with(cx, |view, _| view.session_id.clone());
+ let referenced = acp_v2::SessionId::new("referenced");
+ conversation_view.update(cx, |view, cx| {
+ view.load_subagent_session(referenced.clone(), root_session_id, cx);
+ });
+ cx.run_until_parked();
+ assert_eq!(
+ conversation_view.read_with(cx, |view, _| view.thread_view(&referenced).is_some()),
+ !announces_subagents,
+ "a session a tool call references is loaded only where the RFD does not apply"
+ );
+ }
+ }
+
#[gpui::test]
async fn test_concurrent_subagent_loads_create_one_thread_view(cx: &mut TestAppContext) {
init_test(cx);