Abort reaches all running subagents #189
No files matched your search
@@ -96,6 +96,7 @@ pub struct Subs {
|
||||
list: Mutex<Vec<Arc<Sub>>>,
|
||||
/// (child id, event) for the UI; set by interactive frontends
|
||||
pub events: OnceLock<mpsc::UnboundedSender<(usize, Event)>>,
|
||||
watching: std::sync::atomic::AtomicBool,
|
||||
}
|
||||
|
||||
const KEEP: usize = 20;
|
||||
@@ -107,6 +108,36 @@ impl Subs {
|
||||
pub fn get(&self, id: usize) -> Option<Arc<Sub>> {
|
||||
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<crate::State>) {
|
||||
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<Sub>) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1241,6 +1241,8 @@ fn on_key(app: &mut App, k: KeyEvent, abort: &AbortHandle) -> Option<String> {
|
||||
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<String> {
|
||||
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"),
|
||||
|
||||
Reference in new issue
Block a user