agentmux_srv/drone/executor/scheduler/
attention.rs1use serde_json::Value;
15
16use super::Scheduler;
17use crate::backend::drone_decision::{self, Decision};
18use crate::drone::executor::engine::{mark_state, RunEvent};
19use crate::drone::types::BlockState;
20use agentmux_common::time::now_ms;
21
22impl<'a> Scheduler<'a> {
23 pub(super) async fn settle_in_flight(&mut self, id: &'a str, forced: bool) -> Result<(), String> {
26 let run_id = self.run_id.to_string();
27 let answer = drone_decision::wait_for_decision(&run_id);
28 let reason = format!("block {id} was running when the run stopped and it has effects, so it may already have done its work");
29 let _ = self.tx.send(RunEvent::NeedsAttention { run_id: run_id.clone(), block_id: id.to_string(), reason });
30 let cancel = self.cancel.clone();
31 let decision = tokio::select! {
32 biased;
33 () = cancel.cancelled() => {
34 drone_decision::stop_waiting(&run_id);
35 self.cancelled = true;
36 return Err(format!("stopped: {}", cancel.reason()));
37 }
38 decision = answer => decision,
39 };
40 let Ok(decision) = decision else { return Err("the run stopped waiting for an answer".to_string()) };
41 let choice = match &decision {
42 Decision::RunAgain => "run_again",
43 Decision::Skip { .. } => "skip",
44 };
45 let _ = self.tx.send(RunEvent::AttentionAnswered { run_id, block_id: id.to_string(), choice: choice.to_string() });
46 match decision {
47 Decision::RunAgain => {
48 self.in_flight.retain(|(b, _)| *b != id);
50 self.enqueue(id, forced);
51 Ok(())
52 }
53 Decision::Skip { output } => self.skip_in_flight(id, output).await,
54 }
55 }
56
57 async fn skip_in_flight(&mut self, id: &'a str, mut output: Value) -> Result<(), String> {
60 let node = self.node_of[id];
61 self.in_flight.retain(|(b, _)| *b != id);
62 self.finished.insert(id);
63 self.keep_history(id, &mut output);
64 let cost = output.get("cost_usd").and_then(Value::as_f64).unwrap_or(0.0);
67 self.spent += cost;
68 *self.spent_by.entry(id).or_default() += cost;
69 self.scope.outputs.insert(id.to_string(), output.clone());
70 self.scope.bits.insert(id.to_string(), (true, false));
71 let _ = self.tx.send(RunEvent::BlockDone { run_id: self.run_id.to_string(), block_id: id.to_string(), output: output.clone() });
72 mark_state(self.states, id, BlockState { status: "done".to_string(), output: Some(output.clone()), error: None, started_at: None, completed_at: Some(now_ms()) }).await;
73 self.route(node, &output, false)?;
74 self.checkpoint(Some(id))
75 }
76}