Skip to main content

agentmux_srv/drone/executor/scheduler/
join.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A Join block: where branches that split come together
5//! (docs/specs/SPEC_DRONE_GENERAL_AUTOMATION_2026_11_11.md ยง3.7).
6//!
7//! The scheduler already readies a block once every edge into it is decided, so a
8//! join runs when its branches have each arrived, failed or been pruned. It reads
9//! how each ended from the edges and from what their source blocks said, and
10//! `blocks::join` says whether that holds.
11
12use serde_json::Value;
13
14use super::Scheduler;
15use crate::drone::executor::blocks::join::{self as config, Branches};
16use crate::drone::types::{BlockKind, FlowNode};
17
18impl<'a> Scheduler<'a> {
19    pub(super) fn run_join(&self, node: &'a FlowNode) -> Result<Value, String> {
20        let spec = config::parse(node)?;
21        config::evaluate(&node.id, spec, self.branches_of(&node.id))
22    }
23
24    /// How each branch into the join `id` ended, by the block it came from.
25    fn branches_of(&self, id: &str) -> Branches {
26        let mut edges = self.incoming.get(id).cloned().unwrap_or_default();
27        edges.sort_by(|a, b| a.source.cmp(&b.source));
28        let mut branches = Branches::default();
29        for edge in edges {
30            let source = edge.source.clone();
31            let said = self.scope.outputs.get(&source);
32            if self.decided.get(edge.id.as_str()) == Some(&true) {
33                branches.arrived.push((source, said.cloned().unwrap_or(Value::Null)));
34            } else if self.failed.contains(source.as_str()) {
35                let why = said.and_then(|o| o.get("error")).and_then(Value::as_str).unwrap_or("it failed").to_string();
36                branches.failed.push((source, why));
37            } else {
38                branches.pruned.push(source);
39            }
40        }
41        branches
42    }
43
44    /// What a join's output is made of: tainted when a branch that arrived was, and
45    /// when one failed (what it said is outside text), derived when one was.
46    pub(super) fn join_bits(&self, id: &str) -> (bool, bool) {
47        let branches = self.branches_of(id);
48        let bit = |(source, _): &(String, Value)| self.scope.bits.get(source).copied().unwrap_or((false, false));
49        let bits: Vec<(bool, bool)> = branches.arrived.iter().map(bit).collect();
50        let tainted = !branches.failed.is_empty() || bits.iter().any(|(t, _)| *t);
51        (tainted, !tainted && bits.iter().any(|(_, d)| *d))
52    }
53
54    /// Whether `id` is a Join that a branch failed into: it runs then even when no branch arrived,
55    /// so the failures are said and not hidden by a join that was skipped.
56    pub(super) fn join_has_failed_branch(&self, id: &str) -> bool {
57        self.node_of.get(id).is_some_and(|n| super::kind_of(n).is_ok_and(|k| k == BlockKind::Join)) && !self.branches_of(id).failed.is_empty()
58    }
59
60    /// Whether a failure of block `id` is the graph's to handle: it has an error edge,
61    /// or every way out of it is into a Join that leaves failed branches out.
62    pub(super) fn handles_failure(&self, id: &str) -> bool {
63        if self.has_error_edge(id) {
64            return true;
65        }
66        let Some(edges) = self.outgoing.get(id).filter(|e| !e.is_empty()) else { return false };
67        edges.iter().all(|e| {
68            let join = self.node_of.get(e.target.as_str()).filter(|n| super::kind_of(n).is_ok_and(|k| k == BlockKind::Join));
69            join.is_some_and(|n| config::parse(n).is_ok_and(|spec| spec.skip_failed))
70        })
71    }
72}
73
74#[cfg(test)]
75#[path = "join_tests.rs"]
76mod tests;