jamjet-core 0.5.0

JamJet core execution model — workflow states, node types, retry/timeout policies
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
//! Pure, total evaluator for `Condition`-node branch expressions.
//!
//! # Expression grammar
//!
//! Only three forms are authored in practice (grounding §1c):
//!
//! ```text
//! expr    := path                        # truthiness
//!          | path "==" literal           # equality
//!          | path "!=" literal           # inequality
//! path    := "state" ("." ident)+        # always rooted at `state`, dotted segments
//! literal := '"' … '"'                   # double-quoted JSON string
//!          | "true" | "false" | "null"   # JSON keywords (bare, lowercase)
//!          | number                       # JSON number
//! ```
//!
//! # Truthiness rule
//!
//! For the bare-path form a value is **falsy** iff it is:
//! - absent (path does not resolve),
//! - JSON `null`,
//! - `false`,
//! - the number `0` (or `0.0`),
//! - the empty string `""`,
//! - an empty array `[]`, or
//! - an empty object `{}`.
//!
//! Everything else is truthy. This mirrors JS/Python truthiness and matches the
//! budget code that sets `__cost_exceeded__` as a bool.
//!
//! # Total / never-panics contract
//!
//! A malformed or unsupported expression always returns `false` — no panic.
//! Unrecognised forms emit a `tracing::warn!` to aid debugging.
//!
//! # Path resolution
//!
//! The path after `state.` is split on `.` and each segment descends into the
//! JSON object (or an array by zero-based numeric index when the segment is all
//! digits). A missing or non-traversable segment resolves to absent (treated as
//! `null` for comparisons and as falsy for truthiness).

use serde_json::Value;

/// Evaluate a Condition-node branch expression against committed workflow state.
///
/// Pure + total: never panics; a malformed or unsupported expression returns `false`.
///
/// # Truthiness rule (bare-path form)
///
/// Falsy: absent path, `null`, `false`, `0`/`0.0`, `""`, `[]`, `{}`.
/// Everything else is truthy.
///
/// # Literal parsing (comparison form)
///
/// The RHS must be a valid JSON literal: a double-quoted string (`"x"`),
/// `true`, `false`, `null`, or a JSON number. Bare words (e.g. `done`) are NOT
/// valid literals; an unparseable RHS causes the whole comparison to return
/// `false` (fail-closed). Comparison uses typed `serde_json::Value` equality:
/// `"5" != 5`.
pub fn eval_condition(expr: &str, state: &Value) -> bool {
    let expr = expr.trim();
    if expr.is_empty() {
        return false;
    }

    match find_first_op(expr) {
        Some((op_pos, op)) => eval_comparison(expr, state, op_pos, op),
        None => eval_truthiness(expr, state),
    }
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

/// Scan left-to-right for the first `!=` or `==` operator.
///
/// Returns `(byte_position_of_operator, operator_str)`.
fn find_first_op(expr: &str) -> Option<(usize, &'static str)> {
    let b = expr.as_bytes();
    let len = b.len();
    if len < 2 {
        return None;
    }
    for i in 0..len - 1 {
        match (b[i], b[i + 1]) {
            (b'!', b'=') => return Some((i, "!=")),
            (b'=', b'=') => return Some((i, "==")),
            _ => {}
        }
    }
    None
}

/// Evaluate a comparison expression: `lhs OP rhs`.
fn eval_comparison(expr: &str, state: &Value, op_pos: usize, op: &str) -> bool {
    let lhs = expr[..op_pos].trim();
    // op_pos + 2 is safe: find_first_op only returns positions where i+1 < len,
    // so i+2 <= len is guaranteed.
    let rhs = expr[op_pos + 2..].trim();

    let path = match lhs.strip_prefix("state.") {
        Some(p) if !p.is_empty() => p,
        _ => {
            tracing::warn!(
                "eval_condition: lhs `{}` does not start with `state.<path>`; returning false",
                lhs
            );
            return false;
        }
    };

    // Fail closed: an unparseable RHS literal (e.g. bare word `done`) is an
    // invalid expression — return false rather than silently coercing to a string.
    let rhs_val = match parse_literal(rhs) {
        Some(v) => v,
        None => {
            tracing::warn!(
                "eval_condition: RHS `{}` is not a valid literal \
                 (must be a quoted string, true, false, null, or a number); returning false",
                rhs
            );
            return false;
        }
    };

    let resolved = resolve_path(state, path);
    let equal = values_equal(resolved, &rhs_val);

    match op {
        "==" => equal,
        "!=" => !equal,
        _ => false, // unreachable guard; keeps the function total
    }
}

/// Evaluate a truthiness expression: the whole expr is a `state.<path>`.
fn eval_truthiness(expr: &str, state: &Value) -> bool {
    let path = match expr.strip_prefix("state.") {
        Some(p) if !p.is_empty() => p,
        _ => {
            tracing::warn!(
                "eval_condition: expr `{}` does not start with `state.<path>`; returning false",
                expr
            );
            return false;
        }
    };
    is_truthy(resolve_path(state, path))
}

/// Walk a dotted path (e.g. `"a.b.c"`) through a JSON value.
///
/// Returns `None` for any missing or non-traversable segment.
/// Array indexing by zero-based numeric segment is supported: `"arr.0"` reads index 0.
fn resolve_path<'a>(root: &'a Value, dotted_path: &str) -> Option<&'a Value> {
    let mut current = root;
    for segment in dotted_path.split('.') {
        if segment.is_empty() {
            return None;
        }
        match current {
            Value::Object(map) => {
                current = map.get(segment)?;
            }
            Value::Array(arr) => {
                let idx: usize = segment.parse().ok()?;
                current = arr.get(idx)?;
            }
            _ => return None,
        }
    }
    Some(current)
}

/// JSON truthiness: falsy = absent | null | false | 0 | "" | [] | {}.
/// Everything else is truthy.
fn is_truthy(v: Option<&Value>) -> bool {
    match v {
        None => false,
        Some(Value::Null) => false,
        Some(Value::Bool(b)) => *b,
        Some(Value::Number(n)) => n.as_f64().is_some_and(|f| f != 0.0),
        Some(Value::String(s)) => !s.is_empty(),
        Some(Value::Array(a)) => !a.is_empty(),
        Some(Value::Object(o)) => !o.is_empty(),
    }
}

/// Parse an RHS literal string into a `serde_json::Value`.
///
/// Valid forms: double-quoted JSON string (`"x"`), `true`, `false`, `null`,
/// or a JSON number. Returns `None` for any other input (e.g. bare words),
/// which the caller must treat as an invalid expression (fail-closed: return
/// `false`).
fn parse_literal(s: &str) -> Option<Value> {
    serde_json::from_str::<Value>(s).ok()
}

/// Compare a resolved path value to an RHS literal.
///
/// A missing path (`None`) is treated as `null`: it equals `Value::Null` and
/// nothing else.
fn values_equal(resolved: Option<&Value>, rhs: &Value) -> bool {
    match resolved {
        None => rhs == &Value::Null,
        Some(v) => v == rhs,
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // ---- string equality ----

    #[test]
    fn string_eq_match() {
        assert!(eval_condition(
            r#"state.last_model_finish_reason == "tool_calls""#,
            &json!({"last_model_finish_reason": "tool_calls"}),
        ));
    }

    #[test]
    fn string_eq_mismatch() {
        assert!(!eval_condition(
            r#"state.last_model_finish_reason == "tool_calls""#,
            &json!({"last_model_finish_reason": "stop"}),
        ));
    }

    #[test]
    fn string_eq_missing_key() {
        assert!(!eval_condition(
            r#"state.last_model_finish_reason == "tool_calls""#,
            &json!({}),
        ));
    }

    // ---- string inequality ----

    #[test]
    fn string_neq_true() {
        assert!(eval_condition(r#"state.x != "a""#, &json!({"x": "b"}),));
    }

    #[test]
    fn string_neq_false() {
        assert!(!eval_condition(r#"state.x != "a""#, &json!({"x": "a"}),));
    }

    // ---- truthiness ----

    #[test]
    fn truthiness_bool_true() {
        assert!(eval_condition(
            "state.__cost_exceeded__",
            &json!({"__cost_exceeded__": true}),
        ));
    }

    #[test]
    fn truthiness_bool_false() {
        assert!(!eval_condition(
            "state.__cost_exceeded__",
            &json!({"__cost_exceeded__": false}),
        ));
    }

    #[test]
    fn truthiness_absent_key() {
        assert!(!eval_condition("state.__cost_exceeded__", &json!({})));
    }

    #[test]
    fn truthiness_non_empty_string() {
        assert!(eval_condition(
            "state.__cost_exceeded__",
            &json!({"__cost_exceeded__": "yes"}),
        ));
    }

    #[test]
    fn truthiness_zero_is_falsy() {
        assert!(!eval_condition("state.count", &json!({"count": 0})));
    }

    #[test]
    fn truthiness_empty_string_is_falsy() {
        assert!(!eval_condition("state.s", &json!({"s": ""})));
    }

    #[test]
    fn truthiness_empty_array_is_falsy() {
        assert!(!eval_condition("state.arr", &json!({"arr": []})));
    }

    #[test]
    fn truthiness_empty_object_is_falsy() {
        assert!(!eval_condition("state.obj", &json!({"obj": {}})));
    }

    // ---- nested paths ----

    #[test]
    fn nested_bool_eq_true() {
        assert!(eval_condition(
            "state.__critic_0_verdict__.passed == true",
            &json!({"__critic_0_verdict__": {"passed": true}}),
        ));
    }

    #[test]
    fn nested_bool_eq_false_value() {
        assert!(!eval_condition(
            "state.__critic_0_verdict__.passed == true",
            &json!({"__critic_0_verdict__": {"passed": false}}),
        ));
    }

    #[test]
    fn nested_missing_parent() {
        assert!(!eval_condition(
            "state.__critic_0_verdict__.passed == true",
            &json!({}),
        ));
    }

    // ---- null literal ----

    #[test]
    fn null_eq_missing_path() {
        // A missing path is treated as null.
        assert!(eval_condition("state.missing_key == null", &json!({})));
    }

    #[test]
    fn null_eq_explicit_null() {
        assert!(eval_condition("state.x == null", &json!({"x": null}),));
    }

    #[test]
    fn null_eq_non_null_is_false() {
        assert!(!eval_condition(
            "state.x == null",
            &json!({"x": "something"}),
        ));
    }

    // ---- number literal ----

    #[test]
    fn number_eq_true() {
        assert!(eval_condition("state.count == 5", &json!({"count": 5})));
    }

    #[test]
    fn number_eq_string_is_false() {
        // Typed equality: "5" (string) != 5 (number).
        assert!(!eval_condition("state.count == 5", &json!({"count": "5"}),));
    }

    // ---- bool literal ----

    #[test]
    fn bool_false_literal_match() {
        assert!(eval_condition(
            "state.flag == false",
            &json!({"flag": false}),
        ));
    }

    // ---- malformed inputs — must return false, never panic ----

    #[test]
    fn malformed_garbage() {
        assert!(!eval_condition("garbage", &json!({})));
    }

    #[test]
    fn malformed_lhs_not_state() {
        assert!(!eval_condition(r#"x == "y""#, &json!({"x": "y"})));
    }

    #[test]
    fn malformed_empty_expr() {
        assert!(!eval_condition("", &json!({})));
    }

    #[test]
    fn malformed_just_state_dot() {
        assert!(!eval_condition("state.", &json!({})));
    }

    #[test]
    fn malformed_whitespace_only() {
        assert!(!eval_condition("   ", &json!({})));
    }

    // ---- fail-closed: invalid (unquoted) RHS literal ----

    #[test]
    fn unquoted_rhs_bareword_fail_closed() {
        // `done` is a bare word, not a valid JSON literal.
        // Even though state.status == "done" would be true, the malformed
        // expression must return false (fail-closed contract).
        assert!(!eval_condition(
            "state.status == done",
            &json!({"status": "done"}),
        ));
    }

    #[test]
    fn quoted_rhs_string_still_works() {
        // The quoted form must continue to work correctly.
        assert!(eval_condition(
            r#"state.status == "done""#,
            &json!({"status": "done"}),
        ));
    }
}