Skip to main content

agentmux_srv/drone/transform/
condition.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The Condition block's richer expressions run on the Transform expression language
5//! (docs/specs/SPEC_DRONE_REAL_WORLD_JOBS_2026_11_11.md ยง4.1): `&&`, `||`, `!`, grouping, `contains`, `matches`,
6//! `in_set`, `exists` and the filters, with every bound the language has (source size, nesting, operations, a
7//! linear-time regex). The block turns each `{{reference}}` into a name bound to the value it holds, so the
8//! expression is read as written and what a reference holds is never read as syntax.
9
10use serde_json::{Map, Value};
11
12use super::expr::{eval_at, parse_or, State};
13use super::TransformError;
14
15/// What the expression says, as true or false. `bound` gives the values the expression reads as `item.<name>`.
16pub fn evaluate_condition(expr: &str, bound: &Map<String, Value>) -> Result<bool, String> {
17    let ast = parse_or(expr, 0).map_err(message_of)?;
18    let item = Value::Object(bound.clone());
19    match eval_at(&ast, &item, 0, 0, &mut State::default()).map_err(message_of)? {
20        Value::Bool(answer) => Ok(answer),
21        other => Err(format!("the condition gave {}, not true or false", describe(&other))),
22    }
23}
24
25fn message_of(error: TransformError) -> String {
26    match error {
27        TransformError::Expr { message, .. } | TransformError::Op { message, .. } => message,
28        bound @ TransformError::Bound { .. } => bound.to_string(),
29    }
30}
31
32fn describe(value: &Value) -> &'static str {
33    match value {
34        Value::Null => "nothing",
35        Value::Bool(_) => "a boolean",
36        Value::Number(_) => "a number",
37        Value::String(_) => "text",
38        Value::Array(_) => "a list",
39        Value::Object(_) => "an object",
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use serde_json::json;
46
47    use super::*;
48
49    fn bound(v: Value) -> Map<String, Value> {
50        v.as_object().cloned().unwrap()
51    }
52
53    fn eval(expr: &str, v: Value) -> Result<bool, String> {
54        evaluate_condition(expr, &bound(v))
55    }
56
57    #[test]
58    fn combines_with_and_or_not_and_grouping() {
59        let v = json!({ "a": 5, "b": "red" });
60        assert_eq!(eval("item.a > 3 && item.b == 'red'", v.clone()), Ok(true));
61        assert_eq!(eval("item.a > 9 || item.b == 'red'", v.clone()), Ok(true));
62        assert_eq!(eval("!(item.a > 9)", v.clone()), Ok(true));
63        assert_eq!(eval("item.a > 9 && (item.b == 'red' || true)", v.clone()), Ok(false));
64        assert_eq!(eval("item.a > 3 && !(item.b == 'red')", v), Ok(false));
65    }
66
67    #[test]
68    fn reads_text_lists_and_membership() {
69        let v = json!({ "log": "build failed: disk full", "area": "srv", "seats": ["srv", "ui"] });
70        assert_eq!(eval("item.log contains 'disk full'", v.clone()), Ok(true));
71        assert_eq!(eval("item.log matches 'fail(ed|ure)'", v.clone()), Ok(true));
72        assert_eq!(eval("item.log matches '^ok'", v.clone()), Ok(false));
73        assert_eq!(eval("item.seats contains item.area", v.clone()), Ok(true));
74        assert_eq!(eval("item.area in_set ['srv', 'ui', 'cef']", v.clone()), Ok(true));
75        assert_eq!(eval("item.area in_set ['docs']", v), Ok(false));
76    }
77
78    #[test]
79    fn exists_is_false_for_a_missing_value_and_a_hostile_pattern_cannot_hang() {
80        let v = json!({ "a": null, "b": 0 });
81        assert_eq!(eval("exists(item.a)", v.clone()), Ok(false));
82        assert_eq!(eval("exists(item.b)", v.clone()), Ok(true));
83        assert_eq!(eval("exists(item.nope)", v), Ok(false));
84        let hostile = json!({ "text": "a".repeat(5000) + "!" });
85        assert_eq!(eval("item.text matches '(a+)+$'", hostile), Ok(false));
86    }
87
88    #[test]
89    fn an_answer_that_is_not_a_boolean_is_refused_by_name() {
90        assert_eq!(eval("item.a", json!({ "a": 3 })), Err("the condition gave a number, not true or false".into()));
91        assert_eq!(eval("item.a", json!({})), Err("the condition gave nothing, not true or false".into()));
92        assert_eq!(eval("item.a", json!({ "a": "yes" })), Err("the condition gave text, not true or false".into()));
93    }
94
95    #[test]
96    fn a_bad_expression_says_why() {
97        assert!(eval("item.a >", json!({ "a": 1 })).is_err());
98        assert!(eval("item.a matches '('", json!({ "a": "x" })).is_err());
99    }
100}