Improve tracking for agent edits (#27857)

Release Notes:

- N/A

---------

Co-authored-by: Nathan Sobo <nathan@zed.dev>
Co-authored-by: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
Antonio Scandurra 2025-04-02 00:13:28 +02:00 committed by GitHub
parent d26c477d86
commit 4a252515b1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 757 additions and 795 deletions

View file

@ -14,7 +14,6 @@ path = "src/assistant_tools.rs"
[dependencies]
anyhow.workspace = true
assistant_tool.workspace = true
clock.workspace = true
chrono.workspace = true
collections.workspace = true
feature_flags.workspace = true
@ -42,7 +41,6 @@ worktree.workspace = true
open = { workspace = true }
[dev-dependencies]
clock = { workspace = true, features = ["test-support"] }
collections = { workspace = true, features = ["test-support"] }
gpui = { workspace = true, features = ["test-support"] }
language = { workspace = true, features = ["test-support"] }

View file

@ -92,10 +92,11 @@ impl Tool for CreateFileTool {
})?
.await
.map_err(|err| anyhow!("Unable to open buffer for {destination_path}: {err}"))?;
let edit_id = buffer.update(cx, |buffer, cx| buffer.set_text(contents, cx))?;
action_log.update(cx, |action_log, cx| {
action_log.will_create_buffer(buffer.clone(), edit_id, cx)
cx.update(|cx| {
buffer.update(cx, |buffer, cx| buffer.set_text(contents, cx));
action_log.update(cx, |action_log, cx| {
action_log.will_create_buffer(buffer.clone(), cx)
});
})?;
project

View file

@ -174,7 +174,6 @@ enum EditorResponse {
struct AppliedAction {
source: String,
buffer: Entity<language::Buffer>,
edit_ids: Vec<clock::Lamport>,
}
#[derive(Debug)]
@ -339,18 +338,17 @@ impl EditToolRequest {
self.push_search_error(error);
}
DiffResult::Diff(diff) => {
let edit_ids = buffer.update(cx, |buffer, cx| {
buffer.finalize_last_transaction();
buffer.apply_diff(diff, false, cx);
let transaction = buffer.finalize_last_transaction();
transaction.map_or(Vec::new(), |transaction| transaction.edit_ids.clone())
cx.update(|cx| {
buffer.update(cx, |buffer, cx| {
buffer.finalize_last_transaction();
buffer.apply_diff(diff, cx);
buffer.finalize_last_transaction();
});
self.action_log
.update(cx, |log, cx| log.buffer_edited(buffer.clone(), cx));
})?;
self.push_applied_action(AppliedAction {
source,
buffer,
edit_ids,
});
self.push_applied_action(AppliedAction { source, buffer });
}
}
@ -473,9 +471,6 @@ impl EditToolRequest {
for action in applied {
changed_buffers.insert(action.buffer.clone());
self.action_log.update(cx, |log, cx| {
log.buffer_edited(action.buffer, action.edit_ids, cx)
})?;
write!(&mut output, "\n\n{}", action.source)?;
}

View file

@ -1,7 +1,7 @@
use crate::{replace::replace_with_flexible_indent, schema::json_schema_for};
use anyhow::{Context as _, Result, anyhow};
use assistant_tool::{ActionLog, Tool};
use gpui::{App, AppContext, Entity, Task};
use gpui::{App, AppContext, AsyncApp, Entity, Task};
use language_model::{LanguageModelRequestMessage, LanguageModelToolSchemaFormat};
use project::Project;
use schemars::JsonSchema;
@ -165,7 +165,7 @@ impl Tool for FindReplaceFileTool {
Err(err) => return Task::ready(Err(anyhow!(err))),
};
cx.spawn(async move |cx| {
cx.spawn(async move |cx: &mut AsyncApp| {
let project_path = project.read_with(cx, |project, cx| {
project
.find_project_path(&input.path, cx)
@ -225,20 +225,18 @@ impl Tool for FindReplaceFileTool {
return Err(err)
};
let (edit_ids, snapshot) = buffer.update(cx, |buffer, cx| {
let snapshot = buffer.update(cx, |buffer, cx| {
buffer.finalize_last_transaction();
buffer.apply_diff(diff, false, cx);
let transaction = buffer.finalize_last_transaction();
let edit_ids = transaction.map_or(Vec::new(), |transaction| transaction.edit_ids.clone());
(edit_ids, buffer.snapshot())
buffer.apply_diff(diff, cx);
buffer.finalize_last_transaction();
buffer.snapshot()
})?;
action_log.update(cx, |log, cx| {
log.buffer_edited(buffer.clone(), edit_ids, cx)
log.buffer_edited(buffer.clone(), cx)
})?;
project.update(cx, |project, cx| {
project.update( cx, |project, cx| {
project.save_buffer(buffer, cx)
})?.await?;
@ -249,6 +247,7 @@ impl Tool for FindReplaceFileTool {
Ok(format!("Edited {}:\n\n```diff\n{}\n```", input.path.display(), diff_str))
})
}
}

View file

@ -518,7 +518,7 @@ mod tests {
// Call replace_flexible and transform the result
replace_with_flexible_indent(old, new, &buffer_snapshot).map(|diff| {
buffer.update(cx, |buffer, cx| {
let _ = buffer.apply_diff(diff, false, cx);
let _ = buffer.apply_diff(diff, cx);
buffer.text()
})
})