diff --git a/crates/hx-core/src/agent.rs b/crates/hx-core/src/agent.rs index 3144a76..e09b7ee 100644 --- a/crates/hx-core/src/agent.rs +++ b/crates/hx-core/src/agent.rs @@ -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) -> Result { 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 diff --git a/crates/hx-core/src/hook.rs b/crates/hx-core/src/hook.rs index ad609f7..4da5b4f 100644 --- a/crates/hx-core/src/hook.rs +++ b/crates/hx-core/src/hook.rs @@ -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", diff --git a/crates/hx-lens/src/lib.rs b/crates/hx-lens/src/lib.rs index 2dbd83c..39ff0d6 100644 --- a/crates/hx-lens/src/lib.rs +++ b/crates/hx-lens/src/lib.rs @@ -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 diff --git a/crates/hx-lens/src/seq.rs b/crates/hx-lens/src/seq.rs index eb1b6ed..a7ef1e6 100644 --- a/crates/hx-lens/src/seq.rs +++ b/crates/hx-lens/src/seq.rs @@ -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, } diff --git a/crates/hx-lens/src/watch.rs b/crates/hx-lens/src/watch.rs index 940d26f..6fcc6b0 100644 --- a/crates/hx-lens/src/watch.rs +++ b/crates/hx-lens/src/watch.rs @@ -30,14 +30,14 @@ impl Watch { } /// Record files whose mtime changed since last seen. Returns them. - pub fn scan(&self, seq: &SeqLog) -> Vec { + pub fn scan(&self, seq: &SeqLog, src: Source) -> Vec { 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); } }