lellm_graph/compiler/
inline_pass.rs1use super::context::CompilerContext;
4use super::pass::CompilerPass;
5use crate::Graph;
6use crate::MergeStrategy;
7use crate::node::NodeKind;
8use crate::state::workflow_state::WorkflowState;
9
10pub struct InlinePass;
36
37impl InlinePass {
38 pub fn new() -> Self {
40 Self
41 }
42}
43
44impl Default for InlinePass {
45 fn default() -> Self {
46 Self::new()
47 }
48}
49
50impl<S: WorkflowState, M: MergeStrategy<S>> CompilerPass<S, M> for InlinePass {
51 fn name(&self) -> &str {
52 "inline"
53 }
54
55 fn run(&self, graph: &mut Graph<S, M>, ctx: &mut CompilerContext<S>) -> bool {
56 ctx.stats.total_nodes_before = graph.nodes.len();
58
59 let subgraph_nodes: Vec<String> = graph
61 .nodes
62 .iter()
63 .filter(|(_, kind)| matches!(kind, NodeKind::Subgraph(_)))
64 .map(|(name, _)| name.clone())
65 .collect();
66
67 ctx.stats.subgraph_count = subgraph_nodes.len();
68
69 if ctx.debug {
70 tracing::debug!(
71 subgraph_count = subgraph_nodes.len(),
72 "InlinePass: found subgraph nodes"
73 );
74 }
75
76 let mut modified = false;
85 for node_name in subgraph_nodes {
86 if let Some(NodeKind::Subgraph(_subgraph)) = graph.nodes.get(&node_name) {
87 ctx.stats.not_inlined_count += 1;
89 if ctx.debug {
90 tracing::debug!(
91 node = %node_name,
92 "InlinePass: skipping subgraph (inlining not yet implemented)"
93 );
94 }
95 }
96 let _ = &mut modified;
98 }
99
100 ctx.stats.total_nodes_after = graph.nodes.len();
102
103 modified
104 }
105}
106
107#[cfg(test)]
108mod tests {
109 use super::*;
110 use crate::{GraphBuilder, NodeKind, State, StateMerge, TaskNode};
111
112 #[test]
113 fn test_inline_pass_no_subgraphs() {
114 let mut builder = GraphBuilder::<State, StateMerge>::new("test");
115 builder.start("a");
116 builder.node("a", NodeKind::Task(TaskNode::new("a", |_| Ok(()))));
117 builder.end("a");
118 let mut graph = builder.build().unwrap();
119
120 let mut ctx = CompilerContext::new();
121 let pass = InlinePass::new();
122
123 let modified = pass.run(&mut graph, &mut ctx);
124
125 assert!(!modified);
126 assert_eq!(ctx.stats.subgraph_count, 0);
127 }
128}