diff --git a/Cargo.lock b/Cargo.lock index c043952a1c1..0c6a352e9e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -328,9 +328,9 @@ dependencies = [ [[package]] name = "agent-client-protocol" -version = "3.4.0" +version = "3.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72adac0ce6b0d0390df9b7a498392b24ebcf9c65c3451e5d618d936f50256289" +checksum = "9bbe69c878aef64a3d92041e256e75acdb1bf2ea8e55ef3313e69dbb2f5bd75a" dependencies = [ "agent-client-protocol-derive", "agent-client-protocol-schema", @@ -346,9 +346,9 @@ dependencies = [ [[package]] name = "agent-client-protocol-derive" -version = "3.4.0" +version = "3.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "532fa6f1870bb7cb0bfe91fbcb5166393f7046e186bc590d1aa037d306d48716" +checksum = "25b7ae92f0d1ba46ca61e7e03d4a9758e877d100b783aa1c62b140d5a9d59fb2" dependencies = [ "quote", "syn 3.0.3", @@ -18953,7 +18953,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2fb391ac70462b3097a755618fbf9c8f95ecc1eb379a414f7b46f202ed10db1f" dependencies = [ "cc", - "windows-targets 0.48.5", + "windows-targets 0.52.6", ] [[package]] @@ -20769,7 +20769,7 @@ dependencies = [ "objc2-foundation 0.3.2", "objc2-metal 0.3.2", "objc2-quartz-core 0.3.2", - "ordered-float 4.6.0", + "ordered-float 5.5.0", "profiling", "range-alloc", "raw-window-handle", diff --git a/Cargo.toml b/Cargo.toml index 7802328d71d..734798d183b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -530,7 +530,7 @@ accesskit = { version = "0.24.0", features = ["enumn"] } accesskit_macos = "0.26.0" accesskit_unix = "0.22" accesskit_windows = "0.34" -agent-client-protocol = { version = "=3.4.0", features = ["schemars", "unstable", "unstable_protocol_v2"] } +agent-client-protocol = { version = "=3.5.0", features = ["schemars", "unstable", "unstable_protocol_v2"] } aho-corasick = "1.1" alacritty_terminal = { git = "https://github.com/zed-industries/alacritty", rev = "4c129667ce56611becdc82de6e28218c80e2e88f" } any_vec = "0.14" diff --git a/crates/acp_thread/src/acp_thread.rs b/crates/acp_thread/src/acp_thread.rs index cafc803c55f..be05e4ef011 100644 --- a/crates/acp_thread/src/acp_thread.rs +++ b/crates/acp_thread/src/acp_thread.rs @@ -4427,7 +4427,10 @@ impl AcpThread { error.is::() || error .downcast_ref::() - .is_some_and(agent_client_protocol::is_incoming_transport_closed) + .is_some_and(|error| { + agent_client_protocol::is_incoming_transport_closed(error) + || agent_client_protocol::is_connection_terminated(error) + }) }) { this.handle_connection_lost(cx); diff --git a/crates/agent_servers/src/acp.rs b/crates/agent_servers/src/acp.rs index e613c8cd2c8..f188705177f 100644 --- a/crates/agent_servers/src/acp.rs +++ b/crates/agent_servers/src/acp.rs @@ -181,6 +181,11 @@ fn v1_only_mismatch(error: &agent_client_protocol::Error) -> bool { } fn initialization_error(error: anyhow::Error, debug_log: &AcpDebugLog) -> anyhow::Error { + // `connect` classifies the agent's answer in-band, but an agent that exits right after + // answering surfaces as a process exit, so its answer is recovered from the debug log. + if error.is::() || error.is::() { + return error; + } match debug_log.initialization_response() { Some(Ok(Some(value))) => { if serde_json::from_value::(value).is_ok_and(|response| { @@ -193,11 +198,8 @@ fn initialization_error(error: anyhow::Error, debug_log: &AcpDebugLog) -> anyhow } Some(Err(protocol_error)) => { let retry_v1 = v1_only_mismatch(&protocol_error); - let error = if error.is::() { - error - } else { - anyhow!(protocol_error).context(format!("ACP initialization failed: {error}")) - }; + let error = + anyhow!(protocol_error).context(format!("ACP initialization failed: {error}")); if retry_v1 { error.context(UnsupportedVersion) } else { @@ -688,6 +690,15 @@ fn first_class_terminal_auth_task( }) } +fn is_disconnect_error(error: &anyhow::Error) -> bool { + error + .downcast_ref::() + .is_some_and(|error| { + agent_client_protocol::is_incoming_transport_closed(error) + || agent_client_protocol::is_connection_terminated(error) + }) +} + fn map_acp_error(err: acp::Error) -> anyhow::Error { let error = anyhow!(err); match AuthRequired::from_error(&error) { diff --git a/crates/agent_servers/src/acp/v2.rs b/crates/agent_servers/src/acp/v2.rs index 11eda73ec4f..e6c9929532a 100644 --- a/crates/agent_servers/src/acp/v2.rs +++ b/crates/agent_servers/src/acp/v2.rs @@ -2,7 +2,7 @@ use super::{ AcpDebugLog, AcpDebugMessage, AcpSession, ClientContext, ConfigOptions, ContextServerConfiguration, ForegroundWork, PendingSessionUpdates, UnsupportedVersion, apply_v2_session_notification, close_undelivered_session, drain_foreground_queue, - enqueue_notification, first_class_terminal_auth_task, map_acp_error, + enqueue_notification, first_class_terminal_auth_task, is_disconnect_error, map_acp_error, mcp_configurations_for_project, runtime, transport, with_v2_client_ingress, work_dirs_from_session_info, }; @@ -13,8 +13,8 @@ use acp_thread::{ }; use action_log::ActionLog; use agent_client_protocol::{ - Agent, Client, ConnectTo, JsonRpcRequest, JsonRpcResponse, Lines, PreparedRequest, - RequestCancellationHandle, V2ConnectionTo, + Agent, Client, ConnectTo, InitializeOutcome, JsonRpcRequest, JsonRpcResponse, Lines, + PreparedRequest, RequestCancellationHandle, V2ConnectionTo, schema::{ProtocolVersion, v2 as acp}, }; use anyhow::{Context as _, Result, anyhow}; @@ -35,6 +35,10 @@ use task::SpawnInTerminal; use util::ResultExt as _; use util::path_list::PathList; +fn is_connection_closed(connection: &V2ConnectionTo) -> bool { + connection.is_incoming_closed() || connection.termination().is_some() +} + fn cancel_request_elicitations( request_id: acp::RequestId, cx: &mut AsyncApp, @@ -410,14 +414,25 @@ impl AcpV2Connection { } let initialize_request = acp::InitializeRequest::new(ProtocolVersion::V2, info).capabilities(capabilities); - let initialize_response = connection - .send_request(initialize_request.clone()) + let initialize_response = match connection + .send_initialize(initialize_request.clone()) .block_task() .await - .map_err(map_acp_error)?; - if initialize_response.protocol_version != ProtocolVersion::V2 { - return Err(UnsupportedVersion.into()); - } + { + Ok(InitializeOutcome::Initialized(response)) => response, + Ok(InitializeOutcome::LowerVersion(mismatch)) => { + return Err(anyhow!(mismatch).context(UnsupportedVersion)); + } + Ok(outcome) => { + return Err(anyhow!( + "Unexpected ACP initialization outcome: {outcome:?}" + )); + } + Err(error) if super::v1_only_mismatch(&error) => { + return Err(anyhow!(error).context(UnsupportedVersion)); + } + Err(error) => return Err(map_acp_error(error)), + }; anyhow::ensure!( runtime.borrow().is_some(), "Agent connection shut down during startup" @@ -855,10 +870,8 @@ impl AcpV2Connection { .with_context(|| format!("Failed to apply default config option {config_id}")); if result.as_ref().err().is_some_and(|error| { error.is::() - || self.connection.is_incoming_closed() - || error - .downcast_ref::() - .is_some_and(agent_client_protocol::is_incoming_transport_closed) + || is_connection_closed(&self.connection) + || is_disconnect_error(&error) }) { return result.map(|_| ()); } @@ -915,7 +928,7 @@ impl AcpV2Connection { replay_history: bool, cx: &mut App, ) -> Task>> { - if self.connection.is_incoming_closed() { + if is_connection_closed(&self.connection) { return Task::ready(Err(anyhow!("Agent connection is closed"))); } let path_style = project.read(cx).path_style(cx); @@ -1437,7 +1450,7 @@ impl AgentConnection for AcpV2Connection { if let Some(closing) = closing { return Self::observe_close(closing, thread.upgrade(), cx); } - if self.connection.is_incoming_closed() { + if is_connection_closed(&self.connection) { return Task::ready(Err(anyhow!("Agent connection is closed"))); } if initializing { @@ -1602,7 +1615,7 @@ impl AgentConnection for AcpV2Connection { "Session has been released" ); anyhow::ensure!( - !self.connection.is_incoming_closed(), + !is_connection_closed(&self.connection), "Agent connection is closed" ); self.connection @@ -1859,10 +1872,8 @@ impl V2SessionSubmissions { let response = async move { response.await.map_err(|error| { if error.is::() - || connection.is_incoming_closed() - || error - .downcast_ref::() - .is_some_and(agent_client_protocol::is_incoming_transport_closed) + || is_connection_closed(&connection) + || is_disconnect_error(&error) { error.context(SubmissionDisconnected) } else { @@ -7023,7 +7034,11 @@ done .await .err() .expect("reject unimplemented fallback"); - assert!(error.to_string().contains("protocol"), "{error}"); + assert!(error.is::(), "{error:?}"); + assert!( + error.is::(), + "{error:?}" + ); } #[gpui::test] @@ -7325,24 +7340,12 @@ exec sleep 60 assert_eq!(actual.code, expected.code); assert_eq!(actual.data, expected.data); } else { - let actual = error - .downcast_ref::() + assert!(error.is::(), "{error:?}"); + let mismatch = error + .downcast_ref::() .unwrap_or_else(|| panic!("version diagnostic was replaced: {error:?}")); - assert_eq!( - actual.code, - agent_client_protocol::Error::new(-32600, "Invalid request").code - ); - assert!( - actual - .data - .as_ref() - .and_then(|data| data.as_str()) - .is_some_and( - |message| message.contains("required ACP protocol version 2") - && message.contains("peer negotiated 1") - ), - "{actual:?}" - ); + assert_eq!(mismatch.requested, ProtocolVersion::V2); + assert_eq!(mismatch.selected, ProtocolVersion::V1); } } }