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_servers: Keep ACP v2 subagent traffic sent before a session/new response (#66096)

Ben Brandt035e6ac2f358
diff --git a/crates/agent_servers/src/acp/session_updates.rs b/crates/agent_servers/src/acp/session_updates.rs
index 4113e177041..f39fedf6d27 100644
--- a/crates/agent_servers/src/acp/session_updates.rs
+++ b/crates/agent_servers/src/acp/session_updates.rs
@@ -93,10 +93,18 @@ impl PendingSessionUpdates {
let mut notifications = Vec::new();
// Session IDs correlate concurrent creations. The wire carries no request ID
// on these notifications, so provisional ID reuse by the agent remains ambiguous.
+ // The new session's subagents, and theirs, are announced before their own traffic,
+ // so walking in order also collects that traffic, in the order it arrived.
+ let mut sessions = vec![session_id.clone()];
for update in std::mem::take(&mut self.updates) {
- if &update.notification.session_id == session_id
- && update.generation >= creation.generation
+ if update.generation >= creation.generation
+ && sessions.contains(&update.notification.session_id)
{
+ if let InboundSessionUpdate::Subagent(subagent) = &update.notification.update
+ && !sessions.contains(&subagent.session_id)
+ {
+ sessions.push(subagent.session_id.clone());
+ }
notifications.push(update.notification);
} else {
self.updates.push_back(update);
diff --git a/crates/agent_servers/src/acp/v2.rs b/crates/agent_servers/src/acp/v2.rs
index d3901e8b620..1d2ae2ccfa5 100644
--- a/crates/agent_servers/src/acp/v2.rs
+++ b/crates/agent_servers/src/acp/v2.rs
@@ -1345,9 +1345,22 @@ impl AgentConnection for AcpV2Connection {
})?;
let result = (|| {
for notification in notifications {
+ // Traffic for the new session's subagents goes to their own threads,
+ // which the announcements before it created.
+ let target = this
+ .sessions
+ .borrow()
+ .get(&notification.session_id)
+ .map(|session| session.thread.clone())
+ .with_context(|| {
+ format!(
+ "Session update for unannounced session {}",
+ notification.session_id
+ )
+ })?;
apply_v2_session_notification(
notification,
- &thread.downgrade(),
+ &target,
&this.sessions,
cx,
)?;
@@ -3773,6 +3786,69 @@ done
assert!(harness.closes.is_empty());
}
+ #[gpui::test]
+ async fn v2_new_session_keeps_subagent_traffic_sent_before_its_response(
+ cx: &mut TestAppContext,
+ ) {
+ let harness = SessionHarness::new(session_capabilities(), vec![], vec![], cx).await;
+ *harness.hold_new_responses.lock().expect("new mode") = true;
+ let creation = harness.create(cx);
+ let (_, response) = bounded(harness.new_replies.recv(), cx)
+ .await
+ .expect("new request");
+ let message = |id: &str, text: &str| {
+ acp::SessionUpdate::AgentMessage(acp::AgentMessage::new(id).content(vec![text.into()]))
+ };
+ harness.send_update(
+ "session",
+ acp::SessionUpdate::SubagentUpdate(acp::SubagentUpdate::new("child")),
+ );
+ harness.send_update("child", message("child-message", "child works"));
+ harness.send_update(
+ "child",
+ acp::SessionUpdate::SubagentUpdate(acp::SubagentUpdate::new("grandchild")),
+ );
+ harness.send_update(
+ "grandchild",
+ message("grandchild-message", "grandchild works"),
+ );
+ // Traffic for a session nobody announced is still not delivered.
+ harness.send_update("stranger", message("stranger-message", "unrelated"));
+ cx.run_until_parked();
+ response
+ .respond(initial_response())
+ .expect("new session response");
+ let parent = bounded(creation, cx).await.expect("created session");
+ cx.run_until_parked();
+
+ let child = parent
+ .read_with(cx, |parent, _| parent.subagent(&"child".into()).cloned())
+ .expect("announced before the response");
+ let grandchild = child
+ .read_with(cx, |child, _| child.subagent(&"grandchild".into()).cloned())
+ .expect("announced before the response");
+ for (thread, text) in [(&child, "child works"), (&grandchild, "grandchild works")] {
+ thread.read_with(cx, |thread, cx| {
+ assert!(
+ thread.to_markdown(cx).contains(text),
+ "a subagent's traffic sent before the response reaches it"
+ );
+ });
+ }
+ parent.read_with(cx, |parent, cx| {
+ let transcript = parent.to_markdown(cx);
+ assert!(!transcript.contains("child works"));
+ assert!(!transcript.contains("unrelated"));
+ });
+ assert!(
+ !harness
+ .client
+ .sessions
+ .borrow()
+ .contains_key(&acp::SessionId::new("stranger"))
+ );
+ }
+
#[gpui::test]
async fn v2_new_sdk_correlates_concurrent_prefixes_by_response_id(cx: &mut TestAppContext) {
use acp_thread::ForegroundActivity;