use std::sync::Arc;
use indexmap::IndexMap;
use crate::error::{BuildError, BuildErrors, GraphDiagnostics, GraphError, TerminalError};
use crate::execution_engine::{ExecutionEngine, ExecutorState, NextAction};
use crate::graph_analysis::{self, CycleAnalysis};
use crate::node::{BarrierNode, ConditionNode, ExecutorOperation, FlowNode, LeafNode, NodeKind};
use crate::state::{State, StateMerge};
use crate::workflow_state::{MergeStrategy, WorkflowState};
pub type EdgeCondition<S> = Arc<dyn Fn(&S) -> bool + Send + Sync>;
#[derive(Clone)]
pub struct Edge<S: WorkflowState = State> {
pub from: String,
pub to: String,
pub condition: Option<EdgeCondition<S>>,
pub analysis: Option<EdgeAnalysis>,
pub fallback: bool,
}
impl<S: WorkflowState> Edge<S> {
pub fn is_conditional(&self) -> bool {
self.condition.is_some() && !self.fallback
}
pub fn is_normal(&self) -> bool {
self.condition.is_none() && !self.fallback
}
}
impl<S: WorkflowState> std::fmt::Debug for Edge<S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Edge")
.field("from", &self.from)
.field("to", &self.to)
.field("has_condition", &self.condition.is_some())
.field("analysis", &self.analysis)
.field("fallback", &self.fallback)
.finish()
}
}
#[derive(Debug, Clone)]
pub struct EdgeAnalysis {
pub max_visits: Option<usize>,
}
#[derive(Clone)]
pub struct Graph<S: WorkflowState = State, M: MergeStrategy<S> = StateMerge> {
pub(crate) name: String,
pub(crate) nodes: IndexMap<String, NodeKind<S, M>>,
pub(crate) edges: Vec<Edge<S>>,
pub(crate) start: String,
pub(crate) end: String,
}
impl<S: WorkflowState, M: MergeStrategy<S>> Graph<S, M> {
pub fn name(&self) -> &str {
&self.name
}
pub fn node_names(&self) -> Vec<&str> {
self.nodes.keys().map(|s| s.as_str()).collect()
}
pub fn start_node(&self) -> &str {
&self.start
}
pub fn end_node(&self) -> &str {
&self.end
}
pub fn hash_u64(&self) -> u64 {
let mut s = String::new();
let mut names: Vec<&str> = self.nodes.keys().map(|k| k.as_str()).collect();
names.sort();
s.push_str(&names.join(","));
s.push('|');
let mut edge_strs: Vec<String> = self
.edges
.iter()
.map(|e| {
format!(
"{}->{}{:?}{}",
e.from,
e.to,
if e.condition.is_some() { "?" } else { "" },
if e.fallback { "!" } else { "" }
)
})
.collect();
edge_strs.sort();
s.push_str(&edge_strs.join(","));
fnv_hash(&s)
}
pub fn hash(&self) -> String {
format!("{:016x}", self.hash_u64())
}
pub fn edges_from(&self, from: &str) -> Vec<&Edge<S>> {
self.edges.iter().filter(|e| e.from == from).collect()
}
pub fn find_edge(&self, from: &str, to: &str) -> Option<&Edge<S>> {
self.edges.iter().find(|e| e.from == from && e.to == to)
}
pub fn node_map(&self) -> &IndexMap<String, NodeKind<S, M>> {
&self.nodes
}
fn resolve_next(&self, current: &str, state: &S) -> Option<String> {
for edge in self.edges_from(current) {
if edge.is_conditional() && edge.condition.as_ref().is_some_and(|c| c(state)) {
return Some(edge.to.clone());
}
}
for edge in self.edges_from(current) {
if edge.is_normal() {
return Some(edge.to.clone());
}
}
for edge in self.edges_from(current) {
if edge.fallback {
return Some(edge.to.clone());
}
}
None
}
pub(crate) fn resolve_next_inline(
&self,
current: &str,
state: &S,
) -> Result<String, GraphError> {
if self.edges_from(current).is_empty() {
return Err(GraphError::Terminal(TerminalError::InvalidGraph(format!(
"node '{}' has no outgoing edges and is not the end node",
current
))));
}
self.resolve_next(current, state).ok_or_else(|| {
GraphError::Terminal(TerminalError::InvalidGraph(format!(
"node '{}' has no matching outgoing edge",
current
)))
})
}
pub fn find_fallback_edge(&self, from: &str) -> Option<String> {
self.edges
.iter()
.find(|e| e.from == from && e.fallback)
.map(|e| e.to.clone())
}
pub fn validate(&self) -> Result<(), TerminalError> {
if !self.nodes.contains_key(&self.start) {
return Err(TerminalError::InvalidGraph(format!(
"start node '{}' not found",
self.start
)));
}
if !self.nodes.contains_key(&self.end) {
return Err(TerminalError::InvalidGraph(format!(
"end node '{}' not found",
self.end
)));
}
for edge in &self.edges {
if !self.nodes.contains_key(&edge.from) {
return Err(TerminalError::InvalidGraph(format!(
"edge references non-existent source node '{}'",
edge.from
)));
}
if !self.nodes.contains_key(&edge.to) {
return Err(TerminalError::InvalidGraph(format!(
"edge references non-existent target node '{}'",
edge.to
)));
}
}
Ok(())
}
pub fn analyze(&self) -> GraphDiagnostics {
graph_analysis::analyze_graph(self)
}
pub fn analyze_cycles(&self) -> CycleAnalysis {
let cycles = graph_analysis::find_all_cycles(self);
let unprotected = graph_analysis::filter_unprotected_cycles(self, &cycles);
CycleAnalysis {
has_cycles: !cycles.is_empty(),
cycles,
unprotected_cycles: unprotected,
total_edges: self.edges.len(),
protected_edges: self
.edges
.iter()
.filter(|e| e.analysis.as_ref().is_some_and(|a| a.max_visits.is_some()))
.count(),
}
}
pub async fn run_inline(
&self,
exec_ctx: &mut ExecutionEngine<S>,
max_steps: usize,
) -> Result<(), GraphError> {
let mut current = self.start_node().to_string();
let mut step: usize = 0;
loop {
step += 1;
if step > max_steps {
return Err(GraphError::Terminal(TerminalError::StepsExceeded {
limit: max_steps,
}));
}
let node = self.nodes.get(¤t).ok_or_else(|| {
GraphError::Terminal(TerminalError::NodeNotFound(current.clone()))
})?;
match node {
NodeKind::Task(n) => {
let mut ctx = exec_ctx.build_node_context();
n.execute(&mut ctx).await?;
}
NodeKind::Condition(n) => {
let mut ctx = exec_ctx.build_leaf_context();
<ConditionNode<S> as LeafNode<S>>::execute(n, &mut ctx).await?;
}
NodeKind::Barrier(n) => {
let mut ctx = exec_ctx.build_leaf_context();
<BarrierNode<S> as LeafNode<S>>::execute(n, &mut ctx).await?;
}
NodeKind::External(n) => {
let mut ctx = exec_ctx.build_node_context();
n.execute(&mut ctx).await?;
}
NodeKind::ExternalLeaf(n) => {
let mut ctx = exec_ctx.build_leaf_context();
n.execute(&mut ctx).await?;
}
NodeKind::Parallel(p) => {
p.execute(exec_ctx).await?;
}
}
exec_ctx.commit();
let _flow_events = exec_ctx.take_flow_events();
let (next_action, _signal) = exec_ctx.take_control();
match next_action {
NextAction::End => return Ok(()),
NextAction::Goto(target) => {
current = target;
}
NextAction::Next => {
if current == self.end_node() {
return Ok(());
}
current = self.resolve_next_inline(¤t, exec_ctx.state())?;
}
}
}
}
}
pub struct PendingEdge<'a, S: WorkflowState = State, M: MergeStrategy<S> = StateMerge> {
builder: &'a mut GraphBuilder<S, M>,
edge_index: usize,
}
impl<'a, S: WorkflowState, M: MergeStrategy<S>> PendingEdge<'a, S, M> {
pub fn max_visits(self, n: usize) -> &'a mut GraphBuilder<S, M> {
self.builder.edges[self.edge_index].analysis = Some(EdgeAnalysis {
max_visits: Some(n),
});
self.builder
}
}
pub struct GraphBuilder<S: WorkflowState = State, M: MergeStrategy<S> = StateMerge> {
name: String,
nodes: IndexMap<String, NodeKind<S, M>>,
edges: Vec<Edge<S>>,
start: Option<String>,
end: Option<String>,
}
impl<S: WorkflowState, M: MergeStrategy<S>> GraphBuilder<S, M> {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
nodes: IndexMap::new(),
edges: Vec::new(),
start: None,
end: None,
}
}
pub fn start(&mut self, node: impl Into<String>) -> &mut Self {
self.start = Some(node.into());
self
}
pub fn end(&mut self, node: impl Into<String>) -> &mut Self {
self.end = Some(node.into());
self
}
pub fn node(&mut self, name: impl Into<String>, kind: NodeKind<S, M>) -> &mut Self {
self.nodes.insert(name.into(), kind);
self
}
pub fn edge(
&mut self,
from: impl Into<String>,
to: impl Into<String>,
) -> PendingEdge<'_, S, M> {
let edge_index = self.edges.len();
self.edges.push(Edge {
from: from.into(),
to: to.into(),
condition: None,
analysis: None,
fallback: false,
});
PendingEdge {
builder: self,
edge_index,
}
}
pub fn edge_if(
&mut self,
from: impl Into<String>,
to: impl Into<String>,
condition: impl Fn(&S) -> bool + Send + Sync + 'static,
) -> PendingEdge<'_, S, M> {
let edge_index = self.edges.len();
self.edges.push(Edge {
from: from.into(),
to: to.into(),
condition: Some(Arc::new(condition)),
analysis: None,
fallback: false,
});
PendingEdge {
builder: self,
edge_index,
}
}
pub fn edge_fallback(
&mut self,
from: impl Into<String>,
to: impl Into<String>,
) -> PendingEdge<'_, S, M> {
let edge_index = self.edges.len();
self.edges.push(Edge {
from: from.into(),
to: to.into(),
condition: None,
analysis: None,
fallback: true,
});
PendingEdge {
builder: self,
edge_index,
}
}
pub fn build(self) -> Result<Graph<S, M>, BuildErrors> {
let mut errors = BuildErrors::new();
let start = match self.start {
Some(s) => s,
None => {
errors.push(BuildError::MissingEntryPoint);
return Err(errors);
}
};
let end = match self.end {
Some(s) => s,
None => {
errors.push(BuildError::MissingExitPoint);
return Err(errors);
}
};
let mut seen_nodes = std::collections::HashSet::new();
for name in self.nodes.keys() {
if !seen_nodes.insert(name.clone()) {
errors.push(BuildError::DuplicateNode { id: name.clone() });
}
}
for edge in &self.edges {
if !self.nodes.contains_key(&edge.from) {
errors.push(BuildError::MissingNode {
from: edge.from.clone(),
to: edge.to.clone(),
});
}
if !self.nodes.contains_key(&edge.to) {
errors.push(BuildError::MissingNode {
from: edge.from.clone(),
to: edge.to.clone(),
});
}
}
if !errors.is_empty() {
return Err(errors);
}
let graph = Graph {
name: self.name,
nodes: self.nodes,
edges: self.edges,
start,
end,
};
if let Err(e) = graph.validate() {
return Err(BuildErrors(vec![BuildError::InvalidEdgeDefinition {
from: "graph".to_string(),
to: "graph".to_string(),
reason: e.to_string(),
}]));
}
Ok(graph)
}
pub fn name(&self) -> &str {
&self.name
}
}
fn fnv_hash(s: &str) -> u64 {
let mut hash: u64 = 0xcbf29ce484222325;
for &byte in s.as_bytes() {
hash ^= byte as u64;
hash = hash.wrapping_mul(0x100000001b3);
}
hash
}