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: Drain ACP output before reporting agent exit (#64603)

Ben Brandtd3600d664ac3
diff --git a/crates/agent_servers/src/acp.rs b/crates/agent_servers/src/acp.rs
index 5c9f672afb2..854b406274a 100644
--- a/crates/agent_servers/src/acp.rs
+++ b/crates/agent_servers/src/acp.rs
@@ -51,7 +51,21 @@ use crate::{CURSOR_ID, GEMINI_ID};
pub const GEMINI_TERMINAL_AUTH_METHOD_ID: &str = "spawn-gemini-cli";
const PARAMETERIZED_MODEL_PICKER_META_KEY: &str = "parameterizedModelPicker";
-fn exited_load_error_with_stderr(status: ExitStatus, debug_log: &AcpDebugLog) -> LoadError {
+const EXIT_DRAIN_TIMEOUT: std::time::Duration = std::time::Duration::from_millis(250);
+
+async fn exited_load_error_after_drain(
+ status: ExitStatus,
+ drained: impl Future<Output = ()>,
+ debug_log: &AcpDebugLog,
+ cx: &AsyncApp,
+) -> LoadError {
+ // Descendants can keep stdout or stderr open after the direct child exits.
+ let timeout = cx.background_executor().timer(EXIT_DRAIN_TIMEOUT);
+ futures::pin_mut!(drained, timeout);
+ if let futures::future::Either::Right(_) = futures::future::select(drained, timeout).await {
+ log::warn!("Timed out draining ACP output after agent exit");
+ }
+
LoadError::Exited {
status,
stderr: debug_log.trailing_stderr().map(SharedString::from),
@@ -112,6 +126,22 @@ trait ForegroundWorkItem: Send {
type ForegroundWork = Box<dyn ForegroundWorkItem>;
+struct ForegroundBarrier {
+ acknowledgment: futures::channel::oneshot::Sender<()>,
+}
+
+impl ForegroundWorkItem for ForegroundBarrier {
+ fn run(self: Box<Self>, _cx: &mut AsyncApp, _context: &ClientContext) {
+ if self.acknowledgment.send(()).is_err() {
+ log::debug!("ACP exit drain was cancelled before foreground acknowledgment");
+ }
+ }
+
+ fn reject(self: Box<Self>) {
+ log::debug!("ACP foreground dispatch queue closed before exit drain");
+ }
+}
+
struct RequestForegroundWork<Req, Res>
where
Req: Send + 'static,
@@ -224,7 +254,7 @@ pub struct AcpConnection {
_io_task: Task<()>,
_dispatch_task: Task<()>,
_wait_task: Task<Result<()>>,
- _stderr_task: Task<Result<()>>,
+ _stderr_task: Shared<Task<()>>,
}
#[derive(Clone, Default)]
@@ -650,7 +680,11 @@ impl AcpConnection {
let (dispatch_tx, dispatch_rx) = mpsc::unbounded::<ForegroundWork>();
let transport = Lines::new(outgoing, incoming);
- let stderr_task = cx.background_spawn(stderr);
+ let stderr_task = cx
+ .background_spawn(async move {
+ stderr.await.log_err();
+ })
+ .shared();
let (connection_tx, connection_rx) = futures::channel::oneshot::channel();
let connection_future =
@@ -669,20 +703,20 @@ impl AcpConnection {
.boxed_local();
let status_fut = child
.status()
- .map({
- let debug_log = debug_log.clone();
- move |status| match status {
- Ok(status) => Ok(exited_load_error_with_stderr(status, &debug_log)),
- Err(err) => Err(anyhow!("failed to wait for agent server exit: {err}")),
- }
+ .map(|status| {
+ status.map_err(|error| anyhow!("failed to wait for agent server exit: {error}"))
})
.boxed_local();
let (connection, status_fut) = match futures::future::select(connection_rx, status_fut)
.await
{
futures::future::Either::Left((connection, status_fut)) => (connection?, status_fut),
- futures::future::Either::Right((load_error, _connection_rx)) => {
- return Err(load_error?.into());
+ futures::future::Either::Right((status, _connection_rx)) => {
+ return Err(
+ exited_load_error_after_drain(status?, stderr_task, &debug_log, cx)
+ .await
+ .into(),
+ );
}
};
@@ -700,6 +734,27 @@ impl AcpConnection {
}
}
});
+ let drained = {
+ let connection = connection.clone();
+ let stderr_task = stderr_task.clone();
+ async move {
+ let incoming = async move {
+ // EOF follows SDK dispatch, but model updates are still queued on the foreground.
+ connection.incoming_closed().await;
+ let (acknowledgment, received) = futures::channel::oneshot::channel();
+ let barrier: ForegroundWork = Box::new(ForegroundBarrier { acknowledgment });
+ if let Err(error) = dispatch_tx.unbounded_send(barrier) {
+ error.into_inner().reject();
+ return;
+ }
+ received
+ .await
+ .context("ACP foreground dispatch closed during exit drain")
+ .log_err();
+ };
+ futures::join!(incoming, stderr_task);
+ }
+ };
let beta_features_enabled = cx.update(|cx| cx.has_flag::<AcpBetaFeatureFlag>());
let initialize_response = connection
@@ -724,16 +779,24 @@ impl AcpConnection {
.background_executor()
.timer(std::time::Duration::from_millis(250))
.boxed_local();
- if let futures::future::Either::Left((load_error, _timer)) =
+ if let futures::future::Either::Left((status, _timer)) =
futures::future::select(status_fut, timer).await
{
- return Err(load_error?.into());
+ return Err(exited_load_error_after_drain(
+ status?, drained, &debug_log, cx,
+ )
+ .await
+ .into());
}
return Err(error.into());
}
- futures::future::Either::Right((load_error, _initialize_response)) => {
- return Err(load_error?.into());
+ futures::future::Either::Right((status, _initialize_response)) => {
+ return Err(
+ exited_load_error_after_drain(status?, drained, &debug_log, cx)
+ .await
+ .into(),
+ );
}
};
@@ -743,8 +806,11 @@ impl AcpConnection {
let wait_task = cx.spawn({
let sessions = sessions.clone();
+ let debug_log = debug_log.clone();
async move |cx| {
- let load_error = status_fut.await?;
+ let status = status_fut.await?;
+ let load_error =
+ exited_load_error_after_drain(status, drained, &debug_log, cx).await;
emit_load_error_to_all_sessions(&sessions, load_error, cx);
anyhow::Ok(())
}
@@ -868,7 +934,7 @@ impl AcpConnection {
_io_task: io_task,
_dispatch_task: dispatch_task,
_wait_task: Task::ready(Ok(())),
- _stderr_task: Task::ready(Ok(())),
+ _stderr_task: Task::ready(()).shared(),
}
}
@@ -3466,13 +3532,208 @@ mod tests {
.downcast::<LoadError>()
.expect("startup failure should preserve the typed load error");
match load_error {
- LoadError::Exited { status, .. } => {
+ LoadError::Exited { status, stderr } => {
assert!(!status.success(), "expected non-zero exit status");
+ assert_eq!(
+ stderr.as_deref(),
+ Some(
+ "npm error code ETARGET\nnpm error notarget No matching version found for @agentclientprotocol/[email protected] with a date before 4/28/2026, 12:11:38 PM."
+ ),
+ "startup failure should retain the agent's final stderr"
+ );
}
error => panic!("expected exited load error, got: {error:?}"),
};
}
+ #[cfg(unix)]
+ #[gpui::test]
+ async fn agent_exit_preserves_final_updates_and_diagnostics(cx: &mut gpui::TestAppContext) {
+ assert_agent_exit_preserves_final_output(false, cx).await;
+ }
+
+ #[cfg(unix)]
+ #[gpui::test]
+ async fn agent_exit_with_inherited_handles_preserves_final_output(
+ cx: &mut gpui::TestAppContext,
+ ) {
+ assert_agent_exit_preserves_final_output(true, cx).await;
+ }
+
+ #[cfg(unix)]
+ async fn assert_agent_exit_preserves_final_output(
+ inherit_handles: bool,
+ cx: &mut gpui::TestAppContext,
+ ) {
+ cx.update(|cx| {
+ let store = settings::SettingsStore::test(cx);
+ cx.set_global(store);
+ });
+ cx.executor().allow_parking();
+
+ let directory = tempfile::tempdir().expect("create working directory");
+ let project = project::Project::example([directory.path()], &mut cx.to_async()).await;
+ let agent_server_store =
+ project.read_with(cx, |project, _| project.agent_server_store().downgrade());
+ let session_id = acp::SessionId::new("exiting-agent");
+ let initialize_response =
+ serde_json::to_string(&acp::InitializeResponse::new(ProtocolVersion::V1))
+ .expect("serialize initialization");
+ let session_response =
+ serde_json::to_string(&acp::NewSessionResponse::new(session_id.clone()))
+ .expect("serialize session");
+ let prompt_response =
+ serde_json::to_string(&acp::PromptResponse::new(acp::StopReason::EndTurn))
+ .expect("serialize prompt response");
+ let notification = serde_json::json!({
+ "jsonrpc": "2.0",
+ "method": "session/update",
+ "params": acp::SessionNotification::new(
+ session_id,
+ acp::SessionUpdate::AgentMessageChunk(acp::ContentChunk::new(
+ "final answer before exit".into(),
+ )),
+ ),
+ });
+ let inherited_handle_script = inherit_handles.then_some(
+ r#"
+exec 3<&0
+cat <&3 &
+pipe_holder=$!
+(sleep 2; kill "$pipe_holder" 2>/dev/null) </dev/null >/dev/null 2>&1 &"#,
+ );
+ let script = format!(
+ r#"
+read_request() {{
+ IFS= read -r request || exit 2
+ request_id=$(printf '%s' "$request" | sed -n 's/.*"id":\("[^"]*"\).*/\1/p')
+ test -n "$request_id" || exit 3
+}}
+respond() {{
+ printf '{{"jsonrpc":"2.0","id":%s,"result":%s}}\n' "$request_id" "$1"
+}}
+read_request
+respond '{initialize_response}'
+read_request
+respond '{session_response}'
+read_request
+printf '%s\n' '{notification}'
+respond '{prompt_response}'
+printf '%s\n' 'final diagnostic before exit' >&2
+{}
+exit 7
+"#,
+ inherited_handle_script.unwrap_or_default(),
+ );
+ let command = AgentServerCommand {
+ path: "/bin/sh".into(),
+ args: vec!["-c".into(), script],
+ env: None,
+ };
+ let timeout = cx
+ .background_executor
+ .timer(std::time::Duration::from_secs(5))
+ .fuse();
+ let exchange = async {
+ let connection = Rc::new(
+ AcpConnection::stdio(
+ AgentId::new("exiting-agent"),
+ project.clone(),
+ command,
+ agent_server_store,
+ None,
+ HashMap::default(),
+ &mut cx.to_async(),
+ )
+ .await
+ .expect("initialize scripted agent"),
+ );
+ let thread = cx
+ .update(|cx| {
+ connection.clone().new_session(
+ project,
+ PathList::new(&[directory.path()]),
+ cx,
+ )
+ })
+ .await
+ .expect("create scripted session");
+ let (exit_sender, exit_receiver) = async_channel::unbounded();
+ let _subscription = cx.update(|cx| {
+ let debug_log = connection.debug_log.clone();
+ let connection = connection.connection.clone();
+ cx.subscribe(&thread, move |thread, event, cx| {
+ if let acp_thread::AcpThreadEvent::LoadError(error) = event {
+ let (messages, _receiver) = debug_log.subscribe();
+ let stderr_captured = messages.iter().any(|message| {
+ matches!(
+ &message.message,
+ AcpDebugMessageContent::Stderr { line }
+ if line.as_ref() == "final diagnostic before exit"
+ )
+ });
+ exit_sender
+ .try_send((
+ thread.read(cx).to_markdown(cx),
+ error.clone(),
+ connection.is_incoming_closed(),
+ stderr_captured,
+ ))
+ .expect("capture exit event");
+ }
+ })
+ });
+ let response = thread
+ .update(cx, |thread, cx| thread.send_raw("Respond, then exit", cx))
+ .await
+ .expect("the final prompt response should arrive before closure")
+ .expect("prompt should finish");
+ assert_eq!(response.stop_reason, acp::StopReason::EndTurn);
+
+ let (transcript_at_exit, error, incoming_closed_at_exit, stderr_captured_at_exit) =
+ exit_receiver.recv().await.expect("receive process exit");
+ if inherit_handles {
+ assert!(
+ !incoming_closed_at_exit,
+ "the descendant should still hold the agent's stdout open"
+ );
+ } else {
+ assert!(
+ incoming_closed_at_exit,
+ "normal process exit should reach clean stdout EOF before the exit event"
+ );
+ }
+ cx.run_until_parked();
+ let final_transcript = thread.read_with(cx, |thread, cx| thread.to_markdown(cx));
+ assert!(
+ final_transcript.contains("final answer before exit"),
+ "the scripted agent's final notification should reach the thread"
+ );
+ assert!(
+ transcript_at_exit.contains("final answer before exit"),
+ "final notification should be applied before reporting exit: at exit {transcript_at_exit:?}, after settling {final_transcript:?}"
+ );
+ let LoadError::Exited { status, .. } = error else {
+ panic!("expected process exit, got {error:?}");
+ };
+ assert_eq!(status.code(), Some(7));
+ // Pipe reads can interleave, so stderr need not be the debug log's final block.
+ assert!(
+ stderr_captured_at_exit,
+ "the final diagnostic should be logged before reporting exit"
+ );
+ if inherit_handles {
+ connection.connection.incoming_closed().await;
+ }
+ }
+ .fuse();
+ futures::pin_mut!(exchange, timeout);
+ futures::select! {
+ _ = exchange => {}
+ _ = timeout => panic!("timed out waiting for scripted agent exit"),
+ }
+ }
+
async fn connect_fake_agent(
cx: &mut gpui::TestAppContext,
) -> (