Restore file to original content when rejecting file recreated by agent (#29264)

Release Notes:

- Fixed a bug that could sometimes cause a file to be deleted when
rejecting an agent change.
This commit is contained in:
Antonio Scandurra 2025-04-23 11:42:43 +02:00 committed by GitHub
parent 5e31d86f1f
commit 55ea481707
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 77 additions and 63 deletions

View file

@ -988,7 +988,7 @@ mod tests {
.await .await
.unwrap(); .unwrap();
cx.update(|_, cx| { cx.update(|_, cx| {
action_log.update(cx, |log, cx| log.buffer_read(buffer.clone(), cx)); action_log.update(cx, |log, cx| log.track_buffer(buffer.clone(), cx));
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer buffer
.edit( .edit(

View file

@ -770,24 +770,18 @@ impl Thread {
for ctx in &new_context { for ctx in &new_context {
match ctx { match ctx {
AssistantContext::File(file_ctx) => { AssistantContext::File(file_ctx) => {
log.buffer_added_as_context(file_ctx.context_buffer.buffer.clone(), cx); log.track_buffer(file_ctx.context_buffer.buffer.clone(), cx);
} }
AssistantContext::Directory(dir_ctx) => { AssistantContext::Directory(dir_ctx) => {
for context_buffer in &dir_ctx.context_buffers { for context_buffer in &dir_ctx.context_buffers {
log.buffer_added_as_context(context_buffer.buffer.clone(), cx); log.track_buffer(context_buffer.buffer.clone(), cx);
} }
} }
AssistantContext::Symbol(symbol_ctx) => { AssistantContext::Symbol(symbol_ctx) => {
log.buffer_added_as_context( log.track_buffer(symbol_ctx.context_symbol.buffer.clone(), cx);
symbol_ctx.context_symbol.buffer.clone(),
cx,
);
} }
AssistantContext::Selection(selection_context) => { AssistantContext::Selection(selection_context) => {
log.buffer_added_as_context( log.track_buffer(selection_context.context_buffer.buffer.clone(), cx);
selection_context.context_buffer.buffer.clone(),
cx,
);
} }
AssistantContext::FetchedUrl(_) AssistantContext::FetchedUrl(_)
| AssistantContext::Thread(_) | AssistantContext::Thread(_)

View file

@ -39,10 +39,9 @@ impl ActionLog {
self.edited_since_project_diagnostics_check self.edited_since_project_diagnostics_check
} }
fn track_buffer( fn track_buffer_internal(
&mut self, &mut self,
buffer: Entity<Buffer>, buffer: Entity<Buffer>,
created: bool,
cx: &mut Context<Self>, cx: &mut Context<Self>,
) -> &mut TrackedBuffer { ) -> &mut TrackedBuffer {
let tracked_buffer = self let tracked_buffer = self
@ -59,7 +58,11 @@ impl ActionLog {
let base_text; let base_text;
let status; let status;
let unreviewed_changes; let unreviewed_changes;
if created { if buffer
.read(cx)
.file()
.map_or(true, |file| !file.disk_state().exists())
{
base_text = Rope::default(); base_text = Rope::default();
status = TrackedBufferStatus::Created; status = TrackedBufferStatus::Created;
unreviewed_changes = Patch::new(vec![Edit { unreviewed_changes = Patch::new(vec![Edit {
@ -146,7 +149,7 @@ impl ActionLog {
// resurrected externally, we want to clear the changes we // resurrected externally, we want to clear the changes we
// were tracking and reset the buffer's state. // were tracking and reset the buffer's state.
self.tracked_buffers.remove(&buffer); self.tracked_buffers.remove(&buffer);
self.track_buffer(buffer, false, cx); self.track_buffer_internal(buffer, cx);
} }
cx.notify(); cx.notify();
} }
@ -260,26 +263,15 @@ impl ActionLog {
} }
/// Track a buffer as read, so we can notify the model about user edits. /// Track a buffer as read, so we can notify the model about user edits.
pub fn buffer_read(&mut self, buffer: Entity<Buffer>, cx: &mut Context<Self>) { pub fn track_buffer(&mut self, buffer: Entity<Buffer>, cx: &mut Context<Self>) {
self.track_buffer(buffer, false, cx); self.track_buffer_internal(buffer, cx);
}
/// Track a buffer that was added as context, so we can notify the model about user edits.
pub fn buffer_added_as_context(&mut self, buffer: Entity<Buffer>, cx: &mut Context<Self>) {
self.track_buffer(buffer, false, cx);
}
/// Track a buffer as read, so we can notify the model about user edits.
pub fn will_create_buffer(&mut self, buffer: Entity<Buffer>, cx: &mut Context<Self>) {
self.track_buffer(buffer.clone(), true, cx);
self.buffer_edited(buffer, cx)
} }
/// Mark a buffer as edited, so we can refresh it in the context /// Mark a buffer as edited, so we can refresh it in the context
pub fn buffer_edited(&mut self, buffer: Entity<Buffer>, cx: &mut Context<Self>) { pub fn buffer_edited(&mut self, buffer: Entity<Buffer>, cx: &mut Context<Self>) {
self.edited_since_project_diagnostics_check = true; self.edited_since_project_diagnostics_check = true;
let tracked_buffer = self.track_buffer(buffer.clone(), false, cx); let tracked_buffer = self.track_buffer_internal(buffer.clone(), cx);
if let TrackedBufferStatus::Deleted = tracked_buffer.status { if let TrackedBufferStatus::Deleted = tracked_buffer.status {
tracked_buffer.status = TrackedBufferStatus::Modified; tracked_buffer.status = TrackedBufferStatus::Modified;
} }
@ -287,7 +279,7 @@ impl ActionLog {
} }
pub fn will_delete_buffer(&mut self, buffer: Entity<Buffer>, cx: &mut Context<Self>) { pub fn will_delete_buffer(&mut self, buffer: Entity<Buffer>, cx: &mut Context<Self>) {
let tracked_buffer = self.track_buffer(buffer.clone(), false, cx); let tracked_buffer = self.track_buffer_internal(buffer.clone(), cx);
match tracked_buffer.status { match tracked_buffer.status {
TrackedBufferStatus::Created => { TrackedBufferStatus::Created => {
self.tracked_buffers.remove(&buffer); self.tracked_buffers.remove(&buffer);
@ -397,7 +389,7 @@ impl ActionLog {
// Clear all tracked changes for this buffer and start over as if we just read it. // Clear all tracked changes for this buffer and start over as if we just read it.
self.tracked_buffers.remove(&buffer); self.tracked_buffers.remove(&buffer);
self.track_buffer(buffer.clone(), false, cx); self.track_buffer_internal(buffer.clone(), cx);
cx.notify(); cx.notify();
save save
} }
@ -695,12 +687,20 @@ mod tests {
init_test(cx); init_test(cx);
let fs = FakeFs::new(cx.executor()); let fs = FakeFs::new(cx.executor());
let project = Project::test(fs.clone(), [], cx).await; fs.insert_tree(path!("/dir"), json!({"file": "abc\ndef\nghi\njkl\nmno"}))
.await;
let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;
let action_log = cx.new(|_| ActionLog::new(project.clone())); let action_log = cx.new(|_| ActionLog::new(project.clone()));
let buffer = cx.new(|cx| Buffer::local("abc\ndef\nghi\njkl\nmno", cx)); let file_path = project
.read_with(cx, |project, cx| project.find_project_path("dir/file", cx))
.unwrap();
let buffer = project
.update(cx, |project, cx| project.open_buffer(file_path, cx))
.await
.unwrap();
cx.update(|cx| { cx.update(|cx| {
action_log.update(cx, |log, cx| log.buffer_read(buffer.clone(), cx)); action_log.update(cx, |log, cx| log.track_buffer(buffer.clone(), cx));
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer buffer
.edit([(Point::new(1, 1)..Point::new(1, 2), "E")], None, cx) .edit([(Point::new(1, 1)..Point::new(1, 2), "E")], None, cx)
@ -765,12 +765,23 @@ mod tests {
init_test(cx); init_test(cx);
let fs = FakeFs::new(cx.executor()); let fs = FakeFs::new(cx.executor());
let project = Project::test(fs.clone(), [], cx).await; fs.insert_tree(
path!("/dir"),
json!({"file": "abc\ndef\nghi\njkl\nmno\npqr"}),
)
.await;
let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;
let action_log = cx.new(|_| ActionLog::new(project.clone())); let action_log = cx.new(|_| ActionLog::new(project.clone()));
let buffer = cx.new(|cx| Buffer::local("abc\ndef\nghi\njkl\nmno\npqr", cx)); let file_path = project
.read_with(cx, |project, cx| project.find_project_path("dir/file", cx))
.unwrap();
let buffer = project
.update(cx, |project, cx| project.open_buffer(file_path, cx))
.await
.unwrap();
cx.update(|cx| { cx.update(|cx| {
action_log.update(cx, |log, cx| log.buffer_read(buffer.clone(), cx)); action_log.update(cx, |log, cx| log.track_buffer(buffer.clone(), cx));
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer buffer
.edit([(Point::new(1, 0)..Point::new(2, 0), "")], None, cx) .edit([(Point::new(1, 0)..Point::new(2, 0), "")], None, cx)
@ -839,12 +850,20 @@ mod tests {
init_test(cx); init_test(cx);
let fs = FakeFs::new(cx.executor()); let fs = FakeFs::new(cx.executor());
let project = Project::test(fs.clone(), [], cx).await; fs.insert_tree(path!("/dir"), json!({"file": "abc\ndef\nghi\njkl\nmno"}))
.await;
let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;
let action_log = cx.new(|_| ActionLog::new(project.clone())); let action_log = cx.new(|_| ActionLog::new(project.clone()));
let buffer = cx.new(|cx| Buffer::local("abc\ndef\nghi\njkl\nmno", cx)); let file_path = project
.read_with(cx, |project, cx| project.find_project_path("dir/file", cx))
.unwrap();
let buffer = project
.update(cx, |project, cx| project.open_buffer(file_path, cx))
.await
.unwrap();
cx.update(|cx| { cx.update(|cx| {
action_log.update(cx, |log, cx| log.buffer_read(buffer.clone(), cx)); action_log.update(cx, |log, cx| log.track_buffer(buffer.clone(), cx));
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer buffer
.edit([(Point::new(1, 2)..Point::new(2, 3), "F\nGHI")], None, cx) .edit([(Point::new(1, 2)..Point::new(2, 3), "F\nGHI")], None, cx)
@ -928,25 +947,21 @@ mod tests {
init_test(cx); init_test(cx);
let fs = FakeFs::new(cx.executor()); let fs = FakeFs::new(cx.executor());
fs.insert_tree(path!("/dir"), json!({})).await;
let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await; let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;
let action_log = cx.new(|_| ActionLog::new(project.clone())); let action_log = cx.new(|_| ActionLog::new(project.clone()));
let fs = FakeFs::new(cx.executor());
fs.insert_tree(path!("/dir"), json!({})).await;
let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;
let file_path = project let file_path = project
.read_with(cx, |project, cx| project.find_project_path("dir/file1", cx)) .read_with(cx, |project, cx| project.find_project_path("dir/file1", cx))
.unwrap(); .unwrap();
// Simulate file2 being recreated by a tool.
let buffer = project let buffer = project
.update(cx, |project, cx| project.open_buffer(file_path, cx)) .update(cx, |project, cx| project.open_buffer(file_path, cx))
.await .await
.unwrap(); .unwrap();
cx.update(|cx| { cx.update(|cx| {
action_log.update(cx, |log, cx| log.track_buffer(buffer.clone(), cx));
buffer.update(cx, |buffer, cx| buffer.set_text("lorem", cx)); buffer.update(cx, |buffer, cx| buffer.set_text("lorem", cx));
action_log.update(cx, |log, cx| log.will_create_buffer(buffer.clone(), cx)); action_log.update(cx, |log, cx| log.buffer_edited(buffer.clone(), cx));
}); });
project project
.update(cx, |project, cx| project.save_buffer(buffer.clone(), cx)) .update(cx, |project, cx| project.save_buffer(buffer.clone(), cx))
@ -1067,8 +1082,9 @@ mod tests {
.update(cx, |project, cx| project.open_buffer(file2_path, cx)) .update(cx, |project, cx| project.open_buffer(file2_path, cx))
.await .await
.unwrap(); .unwrap();
action_log.update(cx, |log, cx| log.track_buffer(buffer2.clone(), cx));
buffer2.update(cx, |buffer, cx| buffer.set_text("IPSUM", cx)); buffer2.update(cx, |buffer, cx| buffer.set_text("IPSUM", cx));
action_log.update(cx, |log, cx| log.will_create_buffer(buffer2.clone(), cx)); action_log.update(cx, |log, cx| log.buffer_edited(buffer2.clone(), cx));
project project
.update(cx, |project, cx| project.save_buffer(buffer2.clone(), cx)) .update(cx, |project, cx| project.save_buffer(buffer2.clone(), cx))
.await .await
@ -1113,7 +1129,7 @@ mod tests {
.unwrap(); .unwrap();
cx.update(|cx| { cx.update(|cx| {
action_log.update(cx, |log, cx| log.buffer_read(buffer.clone(), cx)); action_log.update(cx, |log, cx| log.track_buffer(buffer.clone(), cx));
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer buffer
.edit([(Point::new(1, 1)..Point::new(1, 2), "E\nXYZ")], None, cx) .edit([(Point::new(1, 1)..Point::new(1, 2), "E\nXYZ")], None, cx)
@ -1248,7 +1264,7 @@ mod tests {
.unwrap(); .unwrap();
cx.update(|cx| { cx.update(|cx| {
action_log.update(cx, |log, cx| log.buffer_read(buffer.clone(), cx)); action_log.update(cx, |log, cx| log.track_buffer(buffer.clone(), cx));
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer buffer
.edit([(Point::new(1, 1)..Point::new(1, 2), "E\nXYZ")], None, cx) .edit([(Point::new(1, 1)..Point::new(1, 2), "E\nXYZ")], None, cx)
@ -1381,8 +1397,9 @@ mod tests {
.await .await
.unwrap(); .unwrap();
cx.update(|cx| { cx.update(|cx| {
action_log.update(cx, |log, cx| log.track_buffer(buffer.clone(), cx));
buffer.update(cx, |buffer, cx| buffer.set_text("content", cx)); buffer.update(cx, |buffer, cx| buffer.set_text("content", cx));
action_log.update(cx, |log, cx| log.will_create_buffer(buffer.clone(), cx)); action_log.update(cx, |log, cx| log.buffer_edited(buffer.clone(), cx));
}); });
project project
.update(cx, |project, cx| project.save_buffer(buffer.clone(), cx)) .update(cx, |project, cx| project.save_buffer(buffer.clone(), cx))
@ -1438,7 +1455,7 @@ mod tests {
.await .await
.unwrap(); .unwrap();
action_log.update(cx, |log, cx| log.buffer_read(buffer.clone(), cx)); action_log.update(cx, |log, cx| log.track_buffer(buffer.clone(), cx));
for _ in 0..operations { for _ in 0..operations {
match rng.gen_range(0..100) { match rng.gen_range(0..100) {
@ -1490,7 +1507,7 @@ mod tests {
log::info!("quiescing..."); log::info!("quiescing...");
cx.run_until_parked(); cx.run_until_parked();
action_log.update(cx, |log, cx| { action_log.update(cx, |log, cx| {
let tracked_buffer = log.track_buffer(buffer.clone(), false, cx); let tracked_buffer = log.track_buffer_internal(buffer.clone(), cx);
let mut old_text = tracked_buffer.base_text.clone(); let mut old_text = tracked_buffer.base_text.clone();
let new_text = buffer.read(cx).as_rope(); let new_text = buffer.read(cx).as_rope();
for edit in tracked_buffer.unreviewed_changes.edits() { for edit in tracked_buffer.unreviewed_changes.edits() {

View file

@ -159,7 +159,7 @@ impl Tool for CodeActionTool {
}; };
action_log.update(cx, |action_log, cx| { action_log.update(cx, |action_log, cx| {
action_log.buffer_read(buffer.clone(), cx); action_log.track_buffer(buffer.clone(), cx);
})?; })?;
let range = { let range = {

View file

@ -174,7 +174,7 @@ pub async fn file_outline(
}; };
action_log.update(cx, |action_log, cx| { action_log.update(cx, |action_log, cx| {
action_log.buffer_read(buffer.clone(), cx); action_log.track_buffer(buffer.clone(), cx);
})?; })?;
// Wait until the buffer has been fully parsed, so that we can read its outline. // Wait until the buffer has been fully parsed, so that we can read its outline.

View file

@ -209,7 +209,7 @@ impl Tool for ContentsTool {
})?; })?;
action_log.update(cx, |log, cx| { action_log.update(cx, |log, cx| {
log.buffer_read(buffer, cx); log.track_buffer(buffer, cx);
})?; })?;
Ok(result) Ok(result)
@ -221,7 +221,7 @@ impl Tool for ContentsTool {
let result = buffer.read_with(cx, |buffer, _cx| buffer.text())?; let result = buffer.read_with(cx, |buffer, _cx| buffer.text())?;
action_log.update(cx, |log, cx| { action_log.update(cx, |log, cx| {
log.buffer_read(buffer, cx); log.track_buffer(buffer, cx);
})?; })?;
Ok(result) Ok(result)

View file

@ -112,9 +112,12 @@ impl Tool for CreateFileTool {
.await .await
.map_err(|err| anyhow!("Unable to open buffer for {destination_path}: {err}"))?; .map_err(|err| anyhow!("Unable to open buffer for {destination_path}: {err}"))?;
cx.update(|cx| { cx.update(|cx| {
action_log.update(cx, |action_log, cx| {
action_log.track_buffer(buffer.clone(), cx)
});
buffer.update(cx, |buffer, cx| buffer.set_text(contents, cx)); buffer.update(cx, |buffer, cx| buffer.set_text(contents, cx));
action_log.update(cx, |action_log, cx| { action_log.update(cx, |action_log, cx| {
action_log.will_create_buffer(buffer.clone(), cx) action_log.buffer_edited(buffer.clone(), cx)
}); });
})?; })?;

View file

@ -182,7 +182,7 @@ impl Tool for EditFileTool {
let snapshot = cx.update(|cx| { let snapshot = cx.update(|cx| {
action_log.update(cx, |log, cx| { action_log.update(cx, |log, cx| {
log.buffer_read(buffer.clone(), cx) log.track_buffer(buffer.clone(), cx)
}); });
let snapshot = buffer.update(cx, |buffer, cx| { let snapshot = buffer.update(cx, |buffer, cx| {
buffer.finalize_last_transaction(); buffer.finalize_last_transaction();

View file

@ -134,7 +134,7 @@ impl Tool for ReadFileTool {
})?; })?;
action_log.update(cx, |log, cx| { action_log.update(cx, |log, cx| {
log.buffer_read(buffer, cx); log.track_buffer(buffer, cx);
})?; })?;
Ok(result) Ok(result)
@ -147,7 +147,7 @@ impl Tool for ReadFileTool {
let result = buffer.read_with(cx, |buffer, _cx| buffer.text())?; let result = buffer.read_with(cx, |buffer, _cx| buffer.text())?;
action_log.update(cx, |log, cx| { action_log.update(cx, |log, cx| {
log.buffer_read(buffer, cx); log.track_buffer(buffer, cx);
})?; })?;
Ok(result) Ok(result)

View file

@ -106,7 +106,7 @@ impl Tool for RenameTool {
}; };
action_log.update(cx, |action_log, cx| { action_log.update(cx, |action_log, cx| {
action_log.buffer_read(buffer.clone(), cx); action_log.track_buffer(buffer.clone(), cx);
})?; })?;
let position = { let position = {

View file

@ -140,7 +140,7 @@ impl Tool for SymbolInfoTool {
}; };
action_log.update(cx, |action_log, cx| { action_log.update(cx, |action_log, cx| {
action_log.buffer_read(buffer.clone(), cx); action_log.track_buffer(buffer.clone(), cx);
})?; })?;
let position = { let position = {