This commit is contained in:
jimmyfraiture
2025-09-09 14:46:21 -07:00
parent 3150f50c63
commit 82ce78b1ea
2 changed files with 33 additions and 0 deletions

View File

@@ -108,6 +108,8 @@ struct PatchInFlight {
working_dir: PathBuf,
}
const MAX_COMPLETED_UNDO_TURNS: usize = 1;
#[derive(Default)]
struct PatchUndoHistory {
trackers: HashMap<String, PatchInFlight>,
@@ -129,6 +131,7 @@ impl PatchUndoHistory {
let completed = self.take_active_turn()?;
let count = completed.len();
self.completed_turns.push(completed);
self.prune_completed_turns();
Some(count)
}
@@ -179,6 +182,12 @@ impl PatchUndoHistory {
}
}
fn prune_completed_turns(&mut self) {
while self.completed_turns.len() > MAX_COMPLETED_UNDO_TURNS {
self.completed_turns.remove(0);
}
}
fn begin_undo(&mut self) -> Option<Arc<[AppliedPatchDiff]>> {
if self.undo_in_progress.is_some() {
return None;
@@ -198,6 +207,7 @@ impl PatchUndoHistory {
&& !success
{
self.completed_turns.push(turn);
self.prune_completed_turns();
}
}

View File

@@ -78,6 +78,29 @@ fn upgrade_event_payload_for_tests(mut payload: serde_json::Value) -> serde_json
payload
}
#[test]
fn patch_undo_history_is_bounded() {
fn make_diff(label: &str) -> Arc<[AppliedPatchDiff]> {
Arc::from(vec![AppliedPatchDiff {
diff: format!("diff --git a/{label} b/{label}"),
working_dir: PathBuf::from("/repo"),
}])
}
let mut history = PatchUndoHistory::new();
history.completed_turns.push(make_diff("first"));
history.prune_completed_turns();
assert_eq!(history.completed_turns.len(), 1);
history.completed_turns.push(make_diff("second"));
history.prune_completed_turns();
assert_eq!(history.completed_turns.len(), 1);
assert_eq!(
history.completed_turns[0][0].diff,
"diff --git a/second b/second"
);
}
#[test]
fn final_answer_without_newline_is_flushed_immediately() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual();