lens: attribute in-run file changes to the agent, not External; RunEnd event

This commit is contained in:
hermes committed 2026-11-12 20:53:39 +00:00
1 parent c6c4add124
commit ed9a9184d0
5 files changed
+50 -8

No files matched your search

+4
View File
@@ -417,6 +417,10 @@ impl Agent {
/// An abort ends this run only; the next `run` starts un-aborted.
pub async fn run(&mut self, prompt: impl Into<String>) -> Result<Message, Error> {
let r = self.run_inner(prompt.into()).await;
self.emit(Event::RunEnd {
aborted: matches!(r, Err(Error::Aborted)),
})
.await;
self.abort_tx
.send_if_modified(|a| std::mem::replace(a, false));
r
+6
View File
@@ -26,6 +26,10 @@ pub enum Event {
TurnEnd,
/// Model finished without tool calls. `Inject` makes the agent continue.
AgentEnd,
/// A run ended for any reason (done, error, abort). Fires after the last turn.
RunEnd {
aborted: bool,
},
/// Streaming output from the model (UI).
Stream(StreamEvent),
Message(Message),
@@ -57,6 +61,7 @@ impl Event {
"content": content, "is_error": is_error}),
Event::TurnEnd => json!({"type": "turn_end"}),
Event::AgentEnd => json!({"type": "agent_end"}),
Event::RunEnd { aborted } => json!({"type": "run_end", "aborted": aborted}),
Event::Stream(e) => json!({"type": "stream", "event": e}),
Event::Message(m) => json!({"type": "message", "message": m}),
Event::Info(t) => json!({"type": "info", "text": t}),
@@ -74,6 +79,7 @@ impl Event {
Event::ToolResult { .. } => "tool_result",
Event::TurnEnd => "turn_end",
Event::AgentEnd => "agent_end",
Event::RunEnd { .. } => "run_end",
Event::Stream(_) => "stream",
Event::Message(_) => "message",
Event::Info(_) => "info",
+16 -1
View File
@@ -370,6 +370,21 @@ pub fn format_findings(v: &[&Finding]) -> String {
#[async_trait]
impl Hook for Lens {
async fn on(&self, ev: &Event) -> HookAction {
// Anything that changed on disk during the run is ours (bash, subagents, an aborted
// tool that already wrote), not the user's. Re-baseline the read-guard too.
if matches!(ev, Event::TurnEnd | Event::AgentEnd | Event::RunEnd { .. }) {
for p in self.watch.scan(&self.seq, Source::Agent) {
if let Some(st) = self.state.get() {
st.mark_seen(&p);
}
if matches!(ev, Event::RunEnd { aborted: true }) {
self.note(format!(
"{} changed by your aborted previous run (may be half-done); re-read it",
self.rel(&p)
));
}
}
}
match ev {
Event::ToolResult {
name,
@@ -395,7 +410,7 @@ impl Hook for Lens {
}
Event::TurnStart => {
// edits made outside hx while the model was thinking
self.watch.scan(&self.seq);
self.watch.scan(&self.seq, Source::External);
HookAction::Continue
}
Event::TurnEnd if self.mode == Mode::TurnEnd => self
+2
View File
@@ -10,6 +10,8 @@ pub enum Source {
AgentEdit,
Format,
Autofix,
/// Changed during an agent run by something other than a file tool (bash, subagent, aborted tool).
Agent,
External,
}
+22 -7
View File
@@ -30,14 +30,14 @@ impl Watch {
}
/// Record files whose mtime changed since last seen. Returns them.
pub fn scan(&self, seq: &SeqLog) -> Vec<PathBuf> {
pub fn scan(&self, seq: &SeqLog, src: Source) -> Vec<PathBuf> {
let mut map = self.mtimes.lock().unwrap_or_else(|e| e.into_inner());
let mut changed = vec![];
for (p, old) in map.iter_mut() {
let now = mtime(p);
if now != *old {
*old = now;
seq.bump(p, Source::External);
seq.bump(p, src);
changed.push(p.clone());
}
}
@@ -54,7 +54,7 @@ impl Watch {
let mut t = tokio::time::interval(every);
loop {
t.tick().await;
w.scan(&seq);
w.scan(&seq, Source::External);
}
})
}
@@ -71,16 +71,31 @@ mod tests {
std::fs::write(&f, "1").unwrap();
let (w, s) = (Watch::default(), SeqLog::default());
w.track(f.clone());
assert!(w.scan(&s).is_empty());
assert!(w.scan(&s, Source::External).is_empty());
std::thread::sleep(Duration::from_millis(20));
std::fs::write(&f, "2").unwrap();
assert_eq!(w.scan(&s), std::slice::from_ref(&f));
assert!(w.scan(&s).is_empty());
assert_eq!(w.scan(&s, Source::External), std::slice::from_ref(&f));
assert!(w.scan(&s, Source::External).is_empty());
assert_eq!(s.project(), 1);
// our own write: re-baseline, no bump
std::thread::sleep(Duration::from_millis(20));
std::fs::write(&f, "3").unwrap();
w.track(f);
assert!(w.scan(&s).is_empty());
assert!(w.scan(&s, Source::External).is_empty());
}
#[test]
fn agent_scan_not_external() {
let d = tempfile::tempdir().unwrap();
let f = d.path().join("a.rs");
std::fs::write(&f, "1").unwrap();
let (w, s) = (Watch::default(), SeqLog::default());
w.track(f.clone());
std::thread::sleep(Duration::from_millis(20));
std::fs::write(&f, "2").unwrap(); // e.g. bash sed, or an aborted edit
assert_eq!(w.scan(&s, Source::Agent).len(), 1);
// later turn start: nothing left to call external
assert!(w.scan(&s, Source::External).is_empty());
assert!(s.since(0).len() == 1);
}
}