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: Show error message of edit tool call in UI (#57722)

Bennet Bo Fennera60cf568b76a
diff --git a/crates/agent/src/tools/edit_file_tool.rs b/crates/agent/src/tools/edit_file_tool.rs
index 991288d515c..60d93111316 100644
--- a/crates/agent/src/tools/edit_file_tool.rs
+++ b/crates/agent/src/tools/edit_file_tool.rs
@@ -259,6 +259,7 @@ impl AgentTool for EditFileTool {
run_session(
self.process_streaming_edits(&mut input, &event_stream, cx)
.await,
+ &event_stream,
cx,
)
.await
@@ -492,6 +493,69 @@ mod tests {
);
}
+ /// When the edit fails after a session is created but before any edits are
+ /// actually applied (e.g., the first `old_text` doesn't match), the empty
+ /// diff placeholder in the UI should be replaced with the error message.
+ #[gpui::test]
+ async fn test_streaming_edit_surfaces_error_when_no_edits_applied(cx: &mut TestAppContext) {
+ async fn find_first_text_content_in_events(
+ receiver: &mut crate::ToolCallEventStreamReceiver,
+ ) -> Option<String> {
+ use futures::StreamExt as _;
+ while let Some(event) = receiver.next().await {
+ let Ok(crate::ThreadEvent::ToolCallUpdate(
+ acp_thread::ToolCallUpdate::UpdateFields(update),
+ )) = event
+ else {
+ continue;
+ };
+ let Some(content) = update.fields.content else {
+ continue;
+ };
+ for item in content {
+ if let acp::ToolCallContent::Content(c) = item
+ && let acp::ContentBlock::Text(text) = c.content
+ {
+ return Some(text.text);
+ }
+ }
+ }
+ None
+ }
+
+ let (edit_tool, _project, _action_log, _fs, _thread) =
+ setup_test(cx, json!({"file.txt": "hello world"})).await;
+ let (event_stream, mut receiver) = ToolCallEventStream::test();
+ let task = cx.update(|cx| {
+ edit_tool.clone().run(
+ ToolInput::resolved(EditFileToolInput {
+ path: "root/file.txt".into(),
+ edits: vec![Edit {
+ old_text: "nonexistent text that is not in the file".into(),
+ new_text: "replacement".into(),
+ }],
+ }),
+ event_stream,
+ cx,
+ )
+ });
+
+ let EditFileToolOutput::Error { error, diff, .. } = task.await.unwrap_err() else {
+ panic!("expected error");
+ };
+ assert!(
+ diff.is_empty(),
+ "sanity check: no edits should have been applied",
+ );
+
+ let content_text = find_first_text_content_in_events(&mut receiver).await;
+ assert_eq!(
+ content_text.as_deref(),
+ Some(error.as_str()),
+ "expected the failure message to be surfaced as tool call content",
+ );
+ }
+
#[gpui::test]
async fn test_streaming_early_buffer_open(cx: &mut TestAppContext) {
let (edit_tool, _project, _action_log, _fs, _thread) =
diff --git a/crates/agent/src/tools/edit_session.rs b/crates/agent/src/tools/edit_session.rs
index 7955144f8ee..3eea6e57f3d 100644
--- a/crates/agent/src/tools/edit_session.rs
+++ b/crates/agent/src/tools/edit_session.rs
@@ -277,6 +277,7 @@ pub(crate) enum EditSessionResult {
pub(crate) async fn run_session(
result: EditSessionResult,
+ event_stream: &ToolCallEventStream,
cx: &mut AsyncApp,
) -> Result<EditSessionOutput, EditSessionOutput> {
match result {
@@ -302,6 +303,11 @@ pub(crate) async fn run_session(
.ensure_buffer_saved(&session.buffer, cx)
.await;
let (_new_text, diff) = session.compute_new_text_and_diff(cx).await;
+ if diff.is_empty() {
+ event_stream.update_fields(acp::ToolCallUpdateFields::new().content(vec![
+ acp::ToolCallContent::Content(acp::Content::new(error.clone())),
+ ]));
+ }
Err(EditSessionOutput::Error {
error,
input_path: Some(session.input_path),
@@ -311,11 +317,16 @@ pub(crate) async fn run_session(
EditSessionResult::Failed {
error,
session: None,
- } => Err(EditSessionOutput::Error {
- error,
- input_path: None,
- diff: String::new(),
- }),
+ } => {
+ event_stream.update_fields(acp::ToolCallUpdateFields::new().content(vec![
+ acp::ToolCallContent::Content(acp::Content::new(error.clone())),
+ ]));
+ Err(EditSessionOutput::Error {
+ error,
+ input_path: None,
+ diff: String::new(),
+ })
+ }
}
}
diff --git a/crates/agent/src/tools/write_file_tool.rs b/crates/agent/src/tools/write_file_tool.rs
index d48d574397e..5e8ce0016ce 100644
--- a/crates/agent/src/tools/write_file_tool.rs
+++ b/crates/agent/src/tools/write_file_tool.rs
@@ -237,6 +237,7 @@ impl AgentTool for WriteFileTool {
run_session(
self.process_streaming_writes(&mut input, &event_stream, cx)
.await,
+ &event_stream,
cx,
)
.await