Skip to main content

agentmux_srv/drone/executor/scheduler/
attention.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A run that needs attention: a step with effects was running when AgentMux
5//! stopped, so it may already have done its work
6//! (docs/specs/SPEC_DRONE_GENERAL_AUTOMATION_2026_11_11.md ยง3.3).
7//!
8//! The run does not repeat the step on its own. It waits, `needs_attention`, for
9//! a person or an agent to say: *run it again*, *skip it* (saying what it is taken
10//! to have said, which is outside text, so tainted) or *cancel the run*. A run
11//! that is stopped meanwhile (its job's timeout, the next run of its job) ends as
12//! any run does.
13
14use 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    /// Wait for the answer about the in-flight block `id`, then do it: the block
24    /// is ready to run again, or it is taken as finished with the output given.
25    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                // It runs as the next block, marked running again before it starts.
49                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    /// The in-flight block is taken as finished with `output`, which came from
58    /// outside the graph and so is tainted, and the run goes on from there.
59    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        // What it cost counts as for any block that finished, against the run's
65        // cap and its loop's.
66        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}