From c849463805f00a871a1512d341d37e4205166e26 Mon Sep 17 00:00:00 2001 From: hermes Date: Sat, 14 Nov 2026 12:47:10 +0000 Subject: [PATCH] Abort reaches all running subagents, incl. background-continued ones --- crates/hx-core/src/subagent.rs | 81 +++++++++++++++++++++++++++++----- crates/hx-tui/src/lib.rs | 3 ++ 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/crates/hx-core/src/subagent.rs b/crates/hx-core/src/subagent.rs index c1a092b..e4f3376 100644 --- a/crates/hx-core/src/subagent.rs +++ b/crates/hx-core/src/subagent.rs @@ -96,6 +96,7 @@ pub struct Subs { list: Mutex>>, /// (child id, event) for the UI; set by interactive frontends pub events: OnceLock>, + watching: std::sync::atomic::AtomicBool, } const KEEP: usize = 20; @@ -107,6 +108,36 @@ impl Subs { pub fn get(&self, id: usize) -> Option> { self.list().into_iter().find(|s| s.sh.id == id) } + /// Abort every running child (also those continued in the background after their task ended). + pub fn abort_all(&self) { + for s in self.list() { + if s.sh.status() == Status::Running { + s.abort(); + } + } + } + pub fn any_running(&self) -> bool { + self.list().iter().any(|s| s.sh.status() == Status::Running) + } + /// Once per session: every parent abort reaches all running children. The parent flag resets + /// after each run, so the loop re-arms. + fn watch(&self, state: &Arc) { + let Some(mut rx) = state.abort.get().cloned() else { + return; + }; + if self.watching.swap(true, Ordering::Relaxed) { + return; + } + let st = state.clone(); + tokio::spawn(async move { + while rx.wait_for(|a| *a).await.is_ok() { + st.subs.abort_all(); + if rx.wait_for(|a| !*a).await.is_err() { + break; + } + } + }); + } fn add(&self, s: Arc) { let mut l = self.list.lock().unwrap_or_else(|e| e.into_inner()); l.push(s); @@ -357,23 +388,12 @@ impl Task { sem: self.sem.clone(), }); cx.state.subs.add(sub.clone()); - // parent abort stops the child - let bridge = cx.state.abort.get().cloned().map(|mut rx| { - let s = sub.clone(); - tokio::spawn(async move { - if rx.wait_for(|a| *a).await.is_ok() { - s.abort(); - } - }) - }); + cx.state.subs.watch(&cx.state); // waiting for a slot may have outlasted an abort if aborted() { sub.abort(); } let r = sub.drive(prompt.to_string()).await; - if let Some(b) = bridge { - b.abort(); - } let u = sh.usage.lock().unwrap_or_else(|e| e.into_inner()).clone(); let tok = u.input_tokens + u.cache_read_tokens + u.cache_write_tokens + u.output_tokens; let stats = format!( @@ -682,4 +702,41 @@ mod tests { assert_eq!(reqs[1].messages.len(), 3); assert_eq!(cx.state.sub_usage.lock().unwrap().input_tokens, 2000); } + + #[tokio::test] + async fn parent_abort_reaches_background_child() { + let d = tempfile::tempdir().unwrap(); + let p = Arc::new(Script( + Mutex::new(vec![text("a"), call("read", json!({}))]), + Default::default(), + )); + let t = Task::new( + role(&p, "cheap"), + role(&p, "main"), + d.path().join("s.sub"), + 2, + ); + let (parent, h) = Agent::new( + p.clone(), + Registry::default(), + Session::open(d.path().join("p.jsonl")).unwrap(), + ToolCtx { + cwd: d.path().into(), + state: Default::default(), + }, + ); + let cx = parent.cx.clone(); + t.call(json!({"prompt":"one"}), &cx).await; + let sub = cx.state.subs.get(1).unwrap(); + sub.prompt("two".into()); + assert!(cx.state.subs.any_running()); + h.abort(); + for _ in 0..100 { + if sub.sh.status() == Status::Aborted { + break; + } + tokio::time::sleep(std::time::Duration::from_millis(20)).await; + } + assert_eq!(sub.sh.status(), Status::Aborted); + } } diff --git a/crates/hx-tui/src/lib.rs b/crates/hx-tui/src/lib.rs index b1f2265..6a4e185 100644 --- a/crates/hx-tui/src/lib.rs +++ b/crates/hx-tui/src/lib.rs @@ -1241,6 +1241,8 @@ fn on_key(app: &mut App, k: KeyEvent, abort: &AbortHandle) -> Option { if app.busy { app.queue.clear(); abort.abort(); + } else if app.state.subs.any_running() { + app.state.subs.abort_all(); } else if app.ed.buf.is_empty() { app.quit = true; } else { @@ -1254,6 +1256,7 @@ fn on_key(app: &mut App, k: KeyEvent, abort: &AbortHandle) -> Option { app.queue.clear(); abort.abort(); } + KeyCode::Esc if app.state.subs.any_running() => app.state.subs.abort_all(), KeyCode::Char('o') if ctrl => app.expanded = !app.expanded, KeyCode::Esc if app.panel.is_some() => app.panel = None, KeyCode::Char('j') if ctrl => app.ed.insert("\n"), -- 2.54.1