Implement edit rejection in ActionLog (#28080)

Release Notes:

- Fixed a bug that would prevent rejecting certain agent edits.
This commit is contained in:
Antonio Scandurra 2025-04-04 13:20:18 +02:00 committed by GitHub
parent 5e286897d3
commit 277a3f8d6f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 368 additions and 39 deletions

View file

@ -3,7 +3,7 @@ use anyhow::Result;
use buffer_diff::DiffHunkStatus;
use collections::HashSet;
use editor::{
AnchorRangeExt, Direction, Editor, EditorEvent, MultiBuffer, ToPoint,
Direction, Editor, EditorEvent, MultiBuffer, ToPoint,
actions::{GoToHunk, GoToPreviousHunk},
scroll::Autoscroll,
};
@ -350,13 +350,16 @@ impl AgentDiff {
self.update_selection(&diff_hunks_in_ranges, window, cx);
}
let point_ranges = ranges
.into_iter()
.map(|range| range.to_point(&snapshot))
.collect();
self.editor.update(cx, |editor, cx| {
editor.restore_hunks_in_ranges(point_ranges, window, cx)
});
for hunk in &diff_hunks_in_ranges {
let buffer = self.multibuffer.read(cx).buffer(hunk.buffer_id);
if let Some(buffer) = buffer {
self.thread
.update(cx, |thread, cx| {
thread.reject_edits_in_range(buffer, hunk.buffer_range.clone(), cx)
})
.detach_and_log_err(cx);
}
}
}
fn update_selection(
@ -986,7 +989,7 @@ mod tests {
Point::new(3, 0)..Point::new(3, 0)
);
// Restoring a hunk also moves the cursor to the next hunk, possibly cycling if it's at the end.
// Rejecting a hunk also moves the cursor to the next hunk, possibly cycling if it's at the end.
editor.update_in(cx, |editor, window, cx| {
editor.change_selections(None, window, cx, |selections| {
selections.select_ranges([Point::new(10, 0)..Point::new(10, 0)])

View file

@ -290,7 +290,7 @@ impl Thread {
last_restore_checkpoint: None,
pending_checkpoint: None,
tool_use: ToolUseState::new(tools.clone()),
action_log: cx.new(|_| ActionLog::new()),
action_log: cx.new(|_| ActionLog::new(project.clone())),
initial_project_snapshot: {
let project_snapshot = Self::project_snapshot(project, cx);
cx.foreground_executor()
@ -354,11 +354,11 @@ impl Thread {
pending_completions: Vec::new(),
last_restore_checkpoint: None,
pending_checkpoint: None,
project,
project: project.clone(),
prompt_builder,
tools,
tool_use,
action_log: cx.new(|_| ActionLog::new()),
action_log: cx.new(|_| ActionLog::new(project)),
initial_project_snapshot: Task::ready(serialized.initial_project_snapshot).shared(),
cumulative_token_usage: serialized.cumulative_token_usage,
feedback: None,
@ -1757,6 +1757,17 @@ impl Thread {
.update(cx, |action_log, cx| action_log.keep_all_edits(cx));
}
pub fn reject_edits_in_range(
&mut self,
buffer: Entity<language::Buffer>,
buffer_range: Range<language::Anchor>,
cx: &mut Context<Self>,
) -> Task<Result<()>> {
self.action_log.update(cx, |action_log, cx| {
action_log.reject_edits_in_range(buffer, buffer_range, cx)
})
}
pub fn action_log(&self) -> &Entity<ActionLog> {
&self.action_log
}