Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f1acd045b | ||
|
|
c849463805 | ||
|
|
742f2a124a |
No files matched your search
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"version": "0.1.12",
|
"version": "0.1.13",
|
||||||
"sha256": {
|
"sha256": {
|
||||||
"aarch64-apple-darwin": "6968aaabddcfc54d7cde0df3e5fa5769fb4aeb2d8f79334661e850e39451edf5",
|
"aarch64-apple-darwin": "bbc47b4ef0225201654b0c0330dd161bc88100ad499e1e23652414c6a1d84910",
|
||||||
"x86_64-unknown-linux-musl": "3d1b524c3dabf824035486376580270bf8c0a76276ba54a4ed1b82b826869733"
|
"x86_64-unknown-linux-musl": "38c000648bb34f99e49437b79dfe9f518f6ca476f6cd9e04892982f0a11a0601"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -96,6 +96,7 @@ pub struct Subs {
|
|||||||
list: Mutex<Vec<Arc<Sub>>>,
|
list: Mutex<Vec<Arc<Sub>>>,
|
||||||
/// (child id, event) for the UI; set by interactive frontends
|
/// (child id, event) for the UI; set by interactive frontends
|
||||||
pub events: OnceLock<mpsc::UnboundedSender<(usize, Event)>>,
|
pub events: OnceLock<mpsc::UnboundedSender<(usize, Event)>>,
|
||||||
|
watching: std::sync::atomic::AtomicBool,
|
||||||
}
|
}
|
||||||
|
|
||||||
const KEEP: usize = 20;
|
const KEEP: usize = 20;
|
||||||
@@ -107,6 +108,36 @@ impl Subs {
|
|||||||
pub fn get(&self, id: usize) -> Option<Arc<Sub>> {
|
pub fn get(&self, id: usize) -> Option<Arc<Sub>> {
|
||||||
self.list().into_iter().find(|s| s.sh.id == id)
|
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>) {
|
fn add(&self, s: Arc<Sub>) {
|
||||||
let mut l = self.list.lock().unwrap_or_else(|e| e.into_inner());
|
let mut l = self.list.lock().unwrap_or_else(|e| e.into_inner());
|
||||||
l.push(s);
|
l.push(s);
|
||||||
@@ -357,23 +388,12 @@ impl Task {
|
|||||||
sem: self.sem.clone(),
|
sem: self.sem.clone(),
|
||||||
});
|
});
|
||||||
cx.state.subs.add(sub.clone());
|
cx.state.subs.add(sub.clone());
|
||||||
// parent abort stops the child
|
cx.state.subs.watch(&cx.state);
|
||||||
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();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
});
|
|
||||||
// waiting for a slot may have outlasted an abort
|
// waiting for a slot may have outlasted an abort
|
||||||
if aborted() {
|
if aborted() {
|
||||||
sub.abort();
|
sub.abort();
|
||||||
}
|
}
|
||||||
let r = sub.drive(prompt.to_string()).await;
|
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 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 tok = u.input_tokens + u.cache_read_tokens + u.cache_write_tokens + u.output_tokens;
|
||||||
let stats = format!(
|
let stats = format!(
|
||||||
@@ -682,4 +702,41 @@ mod tests {
|
|||||||
assert_eq!(reqs[1].messages.len(), 3);
|
assert_eq!(reqs[1].messages.len(), 3);
|
||||||
assert_eq!(cx.state.sub_usage.lock().unwrap().input_tokens, 2000);
|
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 {
|
if app.busy {
|
||||||
app.queue.clear();
|
app.queue.clear();
|
||||||
abort.abort();
|
abort.abort();
|
||||||
|
} else if app.state.subs.any_running() {
|
||||||
|
app.state.subs.abort_all();
|
||||||
} else if app.ed.buf.is_empty() {
|
} else if app.ed.buf.is_empty() {
|
||||||
app.quit = true;
|
app.quit = true;
|
||||||
} else {
|
} else {
|
||||||
@@ -1254,6 +1256,7 @@ fn on_key(app: &mut App, k: KeyEvent, abort: &AbortHandle) -> Option<String> {
|
|||||||
app.queue.clear();
|
app.queue.clear();
|
||||||
abort.abort();
|
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::Char('o') if ctrl => app.expanded = !app.expanded,
|
||||||
KeyCode::Esc if app.panel.is_some() => app.panel = None,
|
KeyCode::Esc if app.panel.is_some() => app.panel = None,
|
||||||
KeyCode::Char('j') if ctrl => app.ed.insert("\n"),
|
KeyCode::Char('j') if ctrl => app.ed.insert("\n"),
|
||||||
|
|||||||
Reference in new issue
Block a user