use serde::{Deserialize, Serialize};
use xtalforge_commands::{CommandRecord, Origin};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ChangeBurst {
pub start: usize,
pub len: usize,
pub descriptions: Vec<String>,
pub undo_steps: Option<usize>,
}
#[must_use]
pub fn changes_for(history: &[CommandRecord]) -> Vec<ChangeBurst> {
let mut bursts = Vec::new();
let mut index = 0;
while let Some(record) = history.get(index) {
if record.origin != Origin::Agent {
index += 1;
continue;
}
let start = index;
while let Some(record) = history.get(index) {
if record.origin != Origin::Agent {
break;
}
index += 1;
}
let Some(run) = history.get(start..index) else {
break;
};
let len = index - start;
let descriptions = run
.iter()
.map(|record| record.description.clone())
.collect();
let at_end = index == history.len();
bursts.push(ChangeBurst {
start,
len,
descriptions,
undo_steps: if at_end { Some(len) } else { None },
});
}
bursts
}
#[must_use]
pub fn agent_edit_count(history: &[CommandRecord]) -> usize {
history
.iter()
.filter(|record| record.origin == Origin::Agent)
.count()
}