prodigy 0.4.4

Turn ad-hoc Claude sessions into reproducible development pipelines with parallel AI agents
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
//! Expression parser for conditional workflow execution

use super::value::Value;
use anyhow::{anyhow, Result};

/// Expression types
#[derive(Debug, Clone, PartialEq)]
pub enum Expression {
    /// Variable reference (e.g., ${var})
    Variable(String),
    /// Literal value
    Literal(Value),
    /// Comparison operation
    Comparison {
        left: Box<Expression>,
        op: ComparisonOp,
        right: Box<Expression>,
    },
    /// Logical operation
    Logical {
        left: Box<Expression>,
        op: LogicalOp,
        right: Box<Expression>,
    },
    /// Negation
    Not(Box<Expression>),
    /// Check if variable exists
    Exists(String),
}

/// Comparison operators
#[derive(Debug, Clone, PartialEq)]
pub enum ComparisonOp {
    Equal,
    NotEqual,
    GreaterThan,
    LessThan,
    GreaterThanOrEqual,
    LessThanOrEqual,
}

/// Logical operators
#[derive(Debug, Clone, PartialEq)]
pub enum LogicalOp {
    And,
    Or,
}

/// Expression parser
#[derive(Debug)]
pub struct ExpressionParser;

impl ExpressionParser {
    /// Create a new parser
    pub fn new() -> Self {
        Self
    }

    /// Parse an expression string
    pub fn parse(&self, input: &str) -> Result<Expression> {
        let mut tokens = tokenize(input)?;
        parse_logical_or(&mut tokens)
    }
}

/// Token types
#[derive(Debug, Clone, PartialEq)]
enum Token {
    Variable(String),
    String(String),
    Number(f64),
    Bool(bool),
    ComparisonOp(ComparisonOp),
    LogicalOp(LogicalOp),
    Not,
    LeftParen,
    RightParen,
}

/// Tokenize the input string
fn tokenize(input: &str) -> Result<Vec<Token>> {
    let mut tokens = Vec::new();
    let mut chars = input.chars().peekable();

    while let Some(&ch) = chars.peek() {
        match ch {
            ' ' | '\t' | '\n' | '\r' => {
                chars.next();
            }
            '$' => {
                chars.next();
                if chars.peek() == Some(&'{') {
                    chars.next();
                    let var_name = consume_until(&mut chars, '}')?;

                    // Check for .exists suffix
                    if var_name.ends_with(".exists") {
                        let base_name = var_name.trim_end_matches(".exists");
                        tokens.push(Token::Variable(format!("{}.exists", base_name)));
                    } else {
                        tokens.push(Token::Variable(var_name));
                    }
                } else {
                    return Err(anyhow!("Expected '{{' after '$'"));
                }
            }
            '\'' | '"' => {
                let quote = ch;
                chars.next();
                let string = consume_until(&mut chars, quote)?;
                tokens.push(Token::String(string));
            }
            '(' => {
                chars.next();
                tokens.push(Token::LeftParen);
            }
            ')' => {
                chars.next();
                tokens.push(Token::RightParen);
            }
            '!' => {
                chars.next();
                if chars.peek() == Some(&'=') {
                    chars.next();
                    tokens.push(Token::ComparisonOp(ComparisonOp::NotEqual));
                } else {
                    tokens.push(Token::Not);
                }
            }
            '=' => {
                chars.next();
                if chars.peek() == Some(&'=') {
                    chars.next();
                    tokens.push(Token::ComparisonOp(ComparisonOp::Equal));
                } else {
                    return Err(anyhow!("Expected '==' for equality comparison"));
                }
            }
            '>' => {
                chars.next();
                if chars.peek() == Some(&'=') {
                    chars.next();
                    tokens.push(Token::ComparisonOp(ComparisonOp::GreaterThanOrEqual));
                } else {
                    tokens.push(Token::ComparisonOp(ComparisonOp::GreaterThan));
                }
            }
            '<' => {
                chars.next();
                if chars.peek() == Some(&'=') {
                    chars.next();
                    tokens.push(Token::ComparisonOp(ComparisonOp::LessThanOrEqual));
                } else {
                    tokens.push(Token::ComparisonOp(ComparisonOp::LessThan));
                }
            }
            '&' => {
                chars.next();
                if chars.peek() == Some(&'&') {
                    chars.next();
                    tokens.push(Token::LogicalOp(LogicalOp::And));
                } else {
                    return Err(anyhow!("Expected '&&' for logical AND"));
                }
            }
            '|' => {
                chars.next();
                if chars.peek() == Some(&'|') {
                    chars.next();
                    tokens.push(Token::LogicalOp(LogicalOp::Or));
                } else {
                    return Err(anyhow!("Expected '||' for logical OR"));
                }
            }
            _ if ch.is_ascii_digit() || ch == '-' => {
                let num_str = consume_number(&mut chars)?;
                let num = num_str
                    .parse::<f64>()
                    .map_err(|_| anyhow!("Invalid number: {}", num_str))?;
                tokens.push(Token::Number(num));
            }
            _ if ch.is_ascii_alphabetic() => {
                let word = consume_word(&mut chars);
                match word.as_str() {
                    "true" => tokens.push(Token::Bool(true)),
                    "false" => tokens.push(Token::Bool(false)),
                    _ => return Err(anyhow!("Unexpected word: {}", word)),
                }
            }
            _ => {
                return Err(anyhow!("Unexpected character: '{}'", ch));
            }
        }
    }

    Ok(tokens)
}

/// Consume characters until the delimiter
fn consume_until(
    chars: &mut std::iter::Peekable<std::str::Chars>,
    delimiter: char,
) -> Result<String> {
    let mut result = String::new();
    for ch in chars.by_ref() {
        if ch == delimiter {
            return Ok(result);
        }
        result.push(ch);
    }
    Err(anyhow!("Expected '{}' but reached end of input", delimiter))
}

/// Consume a number
fn consume_number(chars: &mut std::iter::Peekable<std::str::Chars>) -> Result<String> {
    let mut result = String::new();

    // Handle negative numbers
    if chars.peek() == Some(&'-') {
        result.push(chars.next().unwrap());
    }

    let mut has_dot = false;
    while let Some(&ch) = chars.peek() {
        if ch.is_ascii_digit() {
            result.push(chars.next().unwrap());
        } else if ch == '.' && !has_dot {
            has_dot = true;
            result.push(chars.next().unwrap());
        } else {
            break;
        }
    }

    if result.is_empty() || result == "-" {
        Err(anyhow!("Invalid number"))
    } else {
        Ok(result)
    }
}

/// Consume a word
fn consume_word(chars: &mut std::iter::Peekable<std::str::Chars>) -> String {
    let mut result = String::new();
    while let Some(&ch) = chars.peek() {
        if ch.is_ascii_alphanumeric() || ch == '_' {
            result.push(chars.next().unwrap());
        } else {
            break;
        }
    }
    result
}

/// Parse logical OR expressions (lowest precedence)
fn parse_logical_or(tokens: &mut Vec<Token>) -> Result<Expression> {
    let mut left = parse_logical_and(tokens)?;

    while !tokens.is_empty() {
        if let Some(Token::LogicalOp(LogicalOp::Or)) = tokens.first() {
            tokens.remove(0);
            let right = parse_logical_and(tokens)?;
            left = Expression::Logical {
                left: Box::new(left),
                op: LogicalOp::Or,
                right: Box::new(right),
            };
        } else {
            break;
        }
    }

    Ok(left)
}

/// Parse logical AND expressions
fn parse_logical_and(tokens: &mut Vec<Token>) -> Result<Expression> {
    let mut left = parse_comparison(tokens)?;

    while !tokens.is_empty() {
        if let Some(Token::LogicalOp(LogicalOp::And)) = tokens.first() {
            tokens.remove(0);
            let right = parse_comparison(tokens)?;
            left = Expression::Logical {
                left: Box::new(left),
                op: LogicalOp::And,
                right: Box::new(right),
            };
        } else {
            break;
        }
    }

    Ok(left)
}

/// Parse comparison expressions
fn parse_comparison(tokens: &mut Vec<Token>) -> Result<Expression> {
    let left = parse_unary(tokens)?;

    if !tokens.is_empty() {
        if let Some(Token::ComparisonOp(op)) = tokens.first() {
            let op = op.clone();
            tokens.remove(0);
            let right = parse_unary(tokens)?;
            return Ok(Expression::Comparison {
                left: Box::new(left),
                op,
                right: Box::new(right),
            });
        }
    }

    Ok(left)
}

/// Parse unary expressions (NOT, parentheses, literals)
fn parse_unary(tokens: &mut Vec<Token>) -> Result<Expression> {
    if tokens.is_empty() {
        return Err(anyhow!("Unexpected end of expression"));
    }

    match tokens.remove(0) {
        Token::Not => {
            let inner = parse_unary(tokens)?;
            Ok(Expression::Not(Box::new(inner)))
        }
        Token::LeftParen => {
            let inner = parse_logical_or(tokens)?;
            if tokens.is_empty() || tokens.remove(0) != Token::RightParen {
                return Err(anyhow!("Expected closing parenthesis"));
            }
            Ok(inner)
        }
        Token::Variable(name) => {
            if name.ends_with(".exists") {
                let base_name = name.trim_end_matches(".exists");
                Ok(Expression::Exists(base_name.to_string()))
            } else {
                Ok(Expression::Variable(name))
            }
        }
        Token::String(s) => Ok(Expression::Literal(Value::String(s))),
        Token::Number(n) => Ok(Expression::Literal(Value::Number(n))),
        Token::Bool(b) => Ok(Expression::Literal(Value::Bool(b))),
        _ => Err(anyhow!("Unexpected token in expression")),
    }
}

/// Parse a simple expression string (public helper)
pub fn parse_expression(input: &str) -> Result<Expression> {
    let parser = ExpressionParser::new();
    parser.parse(input)
}

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

    #[test]
    fn test_tokenize_simple() {
        let tokens = tokenize("${var} == 'value'").unwrap();
        assert_eq!(tokens.len(), 3);
        assert_eq!(tokens[0], Token::Variable("var".to_string()));
        assert_eq!(tokens[1], Token::ComparisonOp(ComparisonOp::Equal));
        assert_eq!(tokens[2], Token::String("value".to_string()));
    }

    #[test]
    fn test_tokenize_complex() {
        let tokens = tokenize("${a} > 10 && ${b} != 'test'").unwrap();
        assert_eq!(tokens.len(), 7);
    }

    #[test]
    fn test_parse_simple_variable() {
        let expr = parse_expression("${test}").unwrap();
        assert!(matches!(expr, Expression::Variable(ref name) if name == "test"));
    }

    #[test]
    fn test_parse_comparison() {
        let expr = parse_expression("${score} >= 80").unwrap();
        assert!(matches!(expr, Expression::Comparison { .. }));
    }

    #[test]
    fn test_parse_logical() {
        let expr = parse_expression("${a} && ${b}").unwrap();
        assert!(matches!(expr, Expression::Logical { .. }));
    }

    #[test]
    fn test_parse_exists() {
        let expr = parse_expression("${var.exists}").unwrap();
        assert!(matches!(expr, Expression::Exists(ref name) if name == "var"));
    }

    #[test]
    fn test_parse_parentheses() {
        let expr = parse_expression("(${a} || ${b}) && ${c}").unwrap();
        assert!(matches!(expr, Expression::Logical { .. }));
    }

    #[test]
    fn test_parse_not() {
        let expr = parse_expression("!${flag}").unwrap();
        assert!(matches!(expr, Expression::Not(_)));
    }
}