History

action_log: Fix race condition when committing changes (#53884)

Sometimes the action log would not auto-accept agent edits when commiting. Gpt-5.4 identified this race condition: This fixes a race where `keep_committed_edits` could run after `head_commit` changed but before the new git base text had been applied, leaving committed agent edits marked as unreviewed; `ActionLog` now waits for an explicit `BufferDiffEvent::BaseTextChanged` instead of inferring readiness from generic `DiffChanged` activity, so it only accepts edits after the diff base itself is actually updated. - `ReloadGitState` updates `head_commit` before `ReloadBufferDiffBases` finishes loading and applying the new HEAD text. - In that gap, an unrelated `DiffChanged` can fire from a normal diff recalculation. - The old logic treated that event as the commit signal and ran `keep_committed_edits` too early. - `keep_committed_edits` then read stale diff base text, so it failed to match the committed agent edits. - When the real base-text update arrived later, the HEAD had already been overwritten (`old_head`), and the edits stayed unreviewed. Self-Review Checklist: - [x] I've reviewed my own diff for quality, security, and reliability - [x] Unsafe blocks (if any) have justifying comments - [x] The content is consistent with the [UI/UX checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist) - [x] Tests cover the new/changed behavior - [x] Performance impact has been considered and is acceptable Closes #ISSUE Release Notes: - Fixed an issue where committing agent written code would sometimes not mark edits as accepted

Bennet Bo Fenner committed 7 months ago

Commit dae1b201 parent bbf08774 files · +140 −32Browse files

Showing the changes to crates/buffer_diff/src/buffer_diff.rs (1 of 4 files). Show all

Unified · Split

crates/buffer_diff/src/buffer_diff.rs+25−10
@@ -1515,11 +1515,17 @@ pub struct DiffChanged { · 1514 unmodified lines
15151515
15161516 #[derive(Clone, Debug)]
15171517 pub enum BufferDiffEvent {
1518+ BaseTextChanged,
15181519 DiffChanged(DiffChanged),
15191520 LanguageChanged,
15201521 HunksStagedOrUnstaged(Option<Rope>),
15211522 }
15221523
1524+struct SetSnapshotResult {
1525+ change: DiffChanged,
1526+ base_text_changed: bool,
1527+}
1528+
15231529 impl EventEmitter<BufferDiffEvent> for BufferDiff {}
15241530
15251531 impl BufferDiff {
@@ -1784,7 +1790,7 @@ impl BufferDiff { · 258 unmodified lines
17841790 secondary_diff_change: Option<Range<Anchor>>,
17851791 clear_pending_hunks: bool,
17861792 cx: &mut Context<Self>,
1787− ) -> impl Future<Output = DiffChanged> + use<> {
1793+ ) -> impl Future<Output = SetSnapshotResult> + use<> {
17881794 log::debug!("set snapshot with secondary {secondary_diff_change:?}");
17891795
17901796 let old_snapshot = self.snapshot(cx);
@@ -1904,10 +1910,13 @@ impl BufferDiff { · 113 unmodified lines
19041910 if let Some(parsing_idle) = parsing_idle {
19051911 parsing_idle.await;
19061912 }
1907− DiffChanged {
1908− changed_range,
1909− base_text_changed_range,
1910− extended_range,
1913+ SetSnapshotResult {
1914+ change: DiffChanged {
1915+ changed_range,
1916+ base_text_changed_range,
1917+ extended_range,
1918+ },
1919+ base_text_changed,
19111920 }
19121921 }
19131922 }
@@ -1938,12 +1947,15 @@ impl BufferDiff { · 24 unmodified lines
19381947 );
19391948
19401949 cx.spawn(async move |this, cx| {
1941− let change = fut.await;
1950+ let result = fut.await;
19421951 this.update(cx, |_, cx| {
1943− cx.emit(BufferDiffEvent::DiffChanged(change.clone()));
1952+ if result.base_text_changed {
1953+ cx.emit(BufferDiffEvent::BaseTextChanged);
1954+ }
1955+ cx.emit(BufferDiffEvent::DiffChanged(result.change.clone()));
19441956 })
19451957 .ok();
1946− change.changed_range
1958+ result.change.changed_range
19471959 })
19481960 }
19491961
@@ -2019,8 +2031,11 @@ impl BufferDiff { · 69 unmodified lines
20192031 let fg_executor = cx.foreground_executor().clone();
20202032 let snapshot = fg_executor.block_on(fut);
20212033 let fut = self.set_snapshot_with_secondary_inner(snapshot, buffer, None, false, cx);
2022− let change = fg_executor.block_on(fut);
2023− cx.emit(BufferDiffEvent::DiffChanged(change));
2034+ let result = fg_executor.block_on(fut);
2035+ if result.base_text_changed {
2036+ cx.emit(BufferDiffEvent::BaseTextChanged);
2037+ }
2038+ cx.emit(BufferDiffEvent::DiffChanged(result.change));
20242039 }
20252040
20262041 pub fn base_text_buffer(&self) -> &Entity<language::Buffer> {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t