llm-toolkit 0.63.1

A low-level, unopinionated Rust toolkit for the LLM last mile problem.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! Dependency analysis for parallel orchestrator.
//!
//! This module provides tools for analyzing template dependencies and building
//! execution dependency graphs from strategy maps.

use crate::orchestrator::{OrchestratorError, StrategyMap};
use minijinja::machinery::{ast, parse};
use std::collections::{HashMap, HashSet};

use super::DependencyGraph;

/// Extracts top-level variables from a Jinja2 template using AST parsing.
///
/// This function parses the template into an AST and traverses it to find all
/// variable references. Only top-level variable names are extracted (before any dot notation).
///
/// # Arguments
///
/// * `template` - A Jinja2 template string
///
/// # Returns
///
/// A set of variable names found in the template
///
/// # Examples
///
/// ```ignore
/// let vars = extract_template_variables("Process {{ step_1_output }}").unwrap();
/// assert!(vars.contains("step_1_output"));
/// ```
pub fn extract_template_variables(template: &str) -> Result<HashSet<String>, OrchestratorError> {
    use minijinja::machinery::WhitespaceConfig;
    use minijinja::syntax::SyntaxConfig;

    let ast = parse(
        template,
        "template",
        SyntaxConfig,
        WhitespaceConfig::default(),
    )
    .map_err(|e| OrchestratorError::ExecutionFailed(format!("Template parse error: {}", e)))?;

    let mut variables = HashSet::new();

    // The parse function returns a Stmt which wraps a Template
    // We need to extract the Template from it
    match &ast {
        ast::Stmt::Template(template) => {
            for stmt in &template.children {
                extract_vars_from_stmt(stmt, &mut variables);
            }
        }
        _ => {
            // Fallback: treat the whole thing as a statement
            extract_vars_from_stmt(&ast, &mut variables);
        }
    }

    Ok(variables)
}

/// Recursively extracts variables from an AST statement.
fn extract_vars_from_stmt(stmt: &ast::Stmt<'_>, vars: &mut HashSet<String>) {
    match stmt {
        ast::Stmt::Template(template) => {
            for child in &template.children {
                extract_vars_from_stmt(child, vars);
            }
        }
        ast::Stmt::EmitExpr(emit) => extract_vars_from_expr(&emit.expr, vars),
        ast::Stmt::EmitRaw(_) => {}
        ast::Stmt::ForLoop(for_loop) => {
            extract_vars_from_expr(&for_loop.iter, vars);
            if let Some(filter_expr) = &for_loop.filter_expr {
                extract_vars_from_expr(filter_expr, vars);
            }
            for stmt in &for_loop.body {
                extract_vars_from_stmt(stmt, vars);
            }
            for stmt in &for_loop.else_body {
                extract_vars_from_stmt(stmt, vars);
            }
        }
        ast::Stmt::IfCond(if_cond) => {
            extract_vars_from_expr(&if_cond.expr, vars);
            for stmt in &if_cond.true_body {
                extract_vars_from_stmt(stmt, vars);
            }
            for stmt in &if_cond.false_body {
                extract_vars_from_stmt(stmt, vars);
            }
        }
        ast::Stmt::WithBlock(with_block) => {
            for (_, value_expr) in &with_block.assignments {
                extract_vars_from_expr(value_expr, vars);
            }
            for stmt in &with_block.body {
                extract_vars_from_stmt(stmt, vars);
            }
        }
        ast::Stmt::Set(set_stmt) => {
            extract_vars_from_expr(&set_stmt.expr, vars);
        }
        ast::Stmt::SetBlock(set_block) => {
            if let Some(filter_expr) = &set_block.filter {
                extract_vars_from_expr(filter_expr, vars);
            }
            for stmt in &set_block.body {
                extract_vars_from_stmt(stmt, vars);
            }
        }
        ast::Stmt::AutoEscape(auto_escape) => {
            extract_vars_from_expr(&auto_escape.enabled, vars);
            for stmt in &auto_escape.body {
                extract_vars_from_stmt(stmt, vars);
            }
        }
        ast::Stmt::FilterBlock(filter_block) => {
            extract_vars_from_expr(&filter_block.filter, vars);
            for stmt in &filter_block.body {
                extract_vars_from_stmt(stmt, vars);
            }
        }
        ast::Stmt::Do(do_stmt) => extract_vars_from_call(&do_stmt.call, vars),
        _ => {}
    }
}

/// Recursively extracts variables from an AST expression.
fn extract_vars_from_expr(expr: &ast::Expr<'_>, vars: &mut HashSet<String>) {
    match expr {
        ast::Expr::Var(var) => {
            // Extract top-level variable name (before any dots)
            let name = var.id.split('.').next().unwrap_or(var.id);
            vars.insert(name.to_string());
        }
        ast::Expr::Const(_) => {}
        ast::Expr::GetAttr(get_attr) => {
            extract_vars_from_expr(&get_attr.expr, vars);
        }
        ast::Expr::GetItem(get_item) => {
            extract_vars_from_expr(&get_item.expr, vars);
            extract_vars_from_expr(&get_item.subscript_expr, vars);
        }
        ast::Expr::Filter(filter) => {
            if let Some(ref expr) = filter.expr {
                extract_vars_from_expr(expr, vars);
            }
            for arg in &filter.args {
                extract_vars_from_call_arg(arg, vars);
            }
        }
        ast::Expr::Test(test) => {
            extract_vars_from_expr(&test.expr, vars);
            for arg in &test.args {
                extract_vars_from_call_arg(arg, vars);
            }
        }
        ast::Expr::BinOp(bin_op) => {
            extract_vars_from_expr(&bin_op.left, vars);
            extract_vars_from_expr(&bin_op.right, vars);
        }
        ast::Expr::UnaryOp(unary_op) => {
            extract_vars_from_expr(&unary_op.expr, vars);
        }
        ast::Expr::Call(call) => extract_vars_from_call(call, vars),
        ast::Expr::IfExpr(if_expr) => {
            extract_vars_from_expr(&if_expr.test_expr, vars);
            extract_vars_from_expr(&if_expr.true_expr, vars);
            if let Some(false_expr) = &if_expr.false_expr {
                extract_vars_from_expr(false_expr, vars);
            }
        }
        ast::Expr::List(list) => {
            for item in &list.items {
                extract_vars_from_expr(item, vars);
            }
        }
        ast::Expr::Map(map) => {
            for key in &map.keys {
                extract_vars_from_expr(key, vars);
            }
            for value in &map.values {
                extract_vars_from_expr(value, vars);
            }
        }
        ast::Expr::Slice(slice) => {
            extract_vars_from_expr(&slice.expr, vars);
            if let Some(start) = &slice.start {
                extract_vars_from_expr(start, vars);
            }
            if let Some(stop) = &slice.stop {
                extract_vars_from_expr(stop, vars);
            }
            if let Some(step) = &slice.step {
                extract_vars_from_expr(step, vars);
            }
        }
    }
}

fn extract_vars_from_call(call: &ast::Call<'_>, vars: &mut HashSet<String>) {
    extract_vars_from_expr(&call.expr, vars);
    for arg in &call.args {
        extract_vars_from_call_arg(arg, vars);
    }
}

fn extract_vars_from_call_arg(arg: &ast::CallArg<'_>, vars: &mut HashSet<String>) {
    match arg {
        ast::CallArg::Pos(expr)
        | ast::CallArg::PosSplat(expr)
        | ast::CallArg::Kwarg(_, expr)
        | ast::CallArg::KwargSplat(expr) => extract_vars_from_expr(expr, vars),
    }
}

/// Builds a dependency graph from a strategy map.
///
/// This function analyzes all steps in the strategy map, extracts variables
/// from their intent templates, and constructs a dependency graph showing
/// which steps depend on which other steps.
///
/// # Arguments
///
/// * `strategy` - The strategy map to analyze
///
/// # Returns
///
/// A dependency graph representing step dependencies
///
/// # Errors
///
/// Returns an error if:
/// - Template parsing fails
/// - A cycle is detected in the dependency graph
///
/// # Examples
///
/// ```ignore
/// let graph = build_dependency_graph(&strategy_map)?;
/// let ready_steps = graph.get_zero_dependency_steps();
/// ```
pub fn build_dependency_graph(
    strategy: &StrategyMap,
) -> Result<DependencyGraph, OrchestratorError> {
    let mut graph = DependencyGraph::new();

    // Build output lookup: variable_name -> step_id
    let mut output_lookup: HashMap<String, String> = HashMap::new();
    for step in &strategy.steps {
        // Register default output key: {step_id}_output
        output_lookup.insert(format!("{}_output", step.step_id), step.step_id.clone());

        // Register custom output key if exists
        if let Some(ref output_key) = step.output_key {
            output_lookup.insert(output_key.clone(), step.step_id.clone());
        }

        // Add node to graph
        graph.add_node(&step.step_id);
    }

    // Analyze dependencies for each step
    for step in &strategy.steps {
        let variables = extract_template_variables(&step.intent_template)?;

        for var in variables {
            // Skip built-in variables like "task"
            if var == "task" {
                continue;
            }

            // Find which step produces this variable
            if let Some(producer_step_id) = output_lookup.get(&var) {
                // Don't add self-dependency
                if producer_step_id != &step.step_id {
                    graph.add_dependency(&step.step_id, producer_step_id);
                }
            }
        }
    }

    // Detect cycles
    if graph.has_cycle() {
        return Err(OrchestratorError::ExecutionFailed(
            "Cyclic dependency detected in strategy map".to_string(),
        ));
    }

    Ok(graph)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::orchestrator::StrategyStep;

    #[test]
    fn test_extract_variables_simple() {
        let template = "Process {{ previous_output }}";
        let vars = extract_template_variables(template).unwrap();

        assert_eq!(vars.len(), 1);
        assert!(vars.contains("previous_output"));
    }

    #[test]
    fn test_extract_variables_multiple() {
        let template = "Use {{ step_1_output }} and {{ step_2_output }}";
        let vars = extract_template_variables(template).unwrap();

        assert_eq!(vars.len(), 2);
        assert!(vars.contains("step_1_output"));
        assert!(vars.contains("step_2_output"));
    }

    #[test]
    fn test_extract_variables_dot_notation() {
        let template = "Get {{ step_1_output.field }}";
        let vars = extract_template_variables(template).unwrap();

        // Should extract top-level variable only
        assert_eq!(vars.len(), 1);
        assert!(vars.contains("step_1_output"));
    }

    #[test]
    fn test_extract_variables_in_filter() {
        let template = "{{ step_1_output | upper }}";
        let vars = extract_template_variables(template).unwrap();

        assert_eq!(vars.len(), 1);
        assert!(vars.contains("step_1_output"));
    }

    #[test]
    fn test_extract_variables_in_filter_with_args() {
        // Test complex case: filters with arguments that are also variables
        let template = "{{ my_var | filter(other_var) | another_filter(third_var, 'constant') }}";
        let vars = extract_template_variables(template).unwrap();

        // Should extract all three variables
        assert_eq!(vars.len(), 3);
        assert!(vars.contains("my_var"));
        assert!(vars.contains("other_var"));
        assert!(vars.contains("third_var"));
    }

    #[test]
    fn test_extract_variables_complex_expressions() {
        // Test complex expressions with nested filters, conditionals, and operations
        let template = r#"
            {% if condition_var %}
                {{ data.field | process(param_var) }}
                {{ result_var + offset_var }}
            {% endif %}
        "#;
        let vars = extract_template_variables(template).unwrap();

        // Should extract top-level variables from all parts
        assert_eq!(vars.len(), 5);
        assert!(vars.contains("condition_var"));
        assert!(vars.contains("data"));
        assert!(vars.contains("param_var"));
        assert!(vars.contains("result_var"));
        assert!(vars.contains("offset_var"));
    }

    #[test]
    fn test_extract_variables_in_condition() {
        let template = "{% if step_1_output %}Use {{ step_2_output }}{% endif %}";
        let vars = extract_template_variables(template).unwrap();

        assert_eq!(vars.len(), 2);
        assert!(vars.contains("step_1_output"));
        assert!(vars.contains("step_2_output"));
    }

    #[test]
    fn test_extract_variables_no_duplicates() {
        let template = "{{ data }} and {{ data }} again";
        let vars = extract_template_variables(template).unwrap();

        assert_eq!(vars.len(), 1);
        assert!(vars.contains("data"));
    }

    #[test]
    fn test_build_dependency_graph_simple() {
        let mut strategy = StrategyMap::new("Test".to_string());
        strategy.add_step(StrategyStep::new(
            "step_1".to_string(),
            "First".to_string(),
            "Agent1".to_string(),
            "Do {{ task }}".to_string(),
            "Output 1".to_string(),
        ));
        strategy.add_step(StrategyStep::new(
            "step_2".to_string(),
            "Second".to_string(),
            "Agent2".to_string(),
            "Process {{ step_1_output }}".to_string(),
            "Output 2".to_string(),
        ));

        let graph = build_dependency_graph(&strategy).unwrap();

        // step_2 depends on step_1
        assert!(graph.get_dependencies("step_2").contains("step_1"));
        // step_1 has no dependencies
        assert!(graph.get_dependencies("step_1").is_empty());
    }

    #[test]
    fn test_build_dependency_graph_with_output_key() {
        let mut strategy = StrategyMap::new("Test".to_string());
        let mut step1 = StrategyStep::new(
            "step_1".to_string(),
            "First".to_string(),
            "Agent1".to_string(),
            "Do {{ task }}".to_string(),
            "Output 1".to_string(),
        );
        step1.output_key = Some("custom_output".to_string());
        strategy.add_step(step1);

        strategy.add_step(StrategyStep::new(
            "step_2".to_string(),
            "Second".to_string(),
            "Agent2".to_string(),
            "Process {{ custom_output }}".to_string(),
            "Output 2".to_string(),
        ));

        let graph = build_dependency_graph(&strategy).unwrap();

        // step_2 depends on step_1 (via custom_output)
        assert!(graph.get_dependencies("step_2").contains("step_1"));
    }

    #[test]
    fn test_build_dependency_graph_detects_cycle() {
        let mut strategy = StrategyMap::new("Test".to_string());
        strategy.add_step(StrategyStep::new(
            "step_1".to_string(),
            "First".to_string(),
            "Agent1".to_string(),
            "Do {{ step_2_output }}".to_string(),
            "Output 1".to_string(),
        ));
        strategy.add_step(StrategyStep::new(
            "step_2".to_string(),
            "Second".to_string(),
            "Agent2".to_string(),
            "Process {{ step_1_output }}".to_string(),
            "Output 2".to_string(),
        ));

        let result = build_dependency_graph(&strategy);
        assert!(result.is_err());
    }

    #[test]
    fn test_build_dependency_graph_ignores_builtins() {
        let mut strategy = StrategyMap::new("Test".to_string());
        strategy.add_step(StrategyStep::new(
            "step_1".to_string(),
            "First".to_string(),
            "Agent1".to_string(),
            "Do {{ task }}".to_string(),
            "Output 1".to_string(),
        ));

        let graph = build_dependency_graph(&strategy).unwrap();

        // step_1 should have no dependencies (task is a built-in)
        assert!(graph.get_dependencies("step_1").is_empty());
    }

    #[test]
    fn test_build_dependency_graph_diamond() {
        let mut strategy = StrategyMap::new("Test".to_string());
        strategy.add_step(StrategyStep::new(
            "step_1".to_string(),
            "Root".to_string(),
            "Agent1".to_string(),
            "{{ task }}".to_string(),
            "Output 1".to_string(),
        ));
        strategy.add_step(StrategyStep::new(
            "step_2".to_string(),
            "Left".to_string(),
            "Agent2".to_string(),
            "{{ step_1_output }}".to_string(),
            "Output 2".to_string(),
        ));
        strategy.add_step(StrategyStep::new(
            "step_3".to_string(),
            "Right".to_string(),
            "Agent3".to_string(),
            "{{ step_1_output }}".to_string(),
            "Output 3".to_string(),
        ));
        strategy.add_step(StrategyStep::new(
            "step_4".to_string(),
            "Merge".to_string(),
            "Agent4".to_string(),
            "{{ step_2_output }} and {{ step_3_output }}".to_string(),
            "Output 4".to_string(),
        ));

        let graph = build_dependency_graph(&strategy).unwrap();

        // Verify diamond structure
        assert!(graph.get_dependencies("step_1").is_empty());
        assert!(graph.get_dependencies("step_2").contains("step_1"));
        assert!(graph.get_dependencies("step_3").contains("step_1"));
        assert!(graph.get_dependencies("step_4").contains("step_2"));
        assert!(graph.get_dependencies("step_4").contains("step_3"));
    }
}