Skip to main content

agentmux_srv/drone/executor/blocks/
transform.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Transform block: shapes a list or an object with a chain of operations in a
5//! restricted expression language, and runs no code
6//! (docs/specs/SPEC_DRONE_GENERAL_AUTOMATION_2026_11_11.md ยง3.9; the evaluator
7//! is `drone/transform.rs`).
8//!
9//! `data.input` is a template for what to shape: one `{{...}}` token gives the
10//! value itself (a list, an object), anything else gives text. `data.ops` is the
11//! chain, such as `[{"op": "filter", "expr": "item.state == 'open'"}, {"op":
12//! "count"}]`. The output is `{ "value": <result> }`, and its taint bits follow
13//! the chain: tainted in, tainted out, except a `count`, `sum`, `min`, `max` or
14//! `exists`, which cannot carry text and come out derived.
15
16use serde_json::{json, Value};
17
18use crate::drone::data_flow::ExecutionScope;
19use crate::drone::transform::{evaluate, Transform, TransformOp};
20use crate::drone::types::FlowNode;
21use crate::drone::value::Tv;
22
23pub async fn run(node: &FlowNode, scope: &mut ExecutionScope) -> Result<Value, String> {
24    let template = node.data.get("input").and_then(Value::as_str).ok_or_else(|| "Transform block missing `input`".to_string())?;
25    let ops: Vec<TransformOp> = serde_json::from_value(node.data.get("ops").cloned().unwrap_or_else(|| json!([])))
26        .map_err(|e| format!("transform: `ops` is not a list of operations: {e}"))?;
27    let (tainted, derived) = scope.bits_of(template);
28    let input = Tv { v: scope.value_of(template), tainted, derived };
29    let out = evaluate(&Transform { ops }, &input).map_err(|e| e.to_string())?;
30    scope.bits.insert(node.id.clone(), (out.tainted, out.derived));
31    Ok(json!({ "value": out.v }))
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37    use crate::drone::types::NodePosition;
38
39    fn node(data: Value) -> FlowNode {
40        FlowNode { id: "t".to_string(), position: NodePosition::default(), data, node_type: None }
41    }
42
43    fn scope() -> ExecutionScope {
44        let mut scope = ExecutionScope::with_trigger(json!({ "prs": [
45            { "n": 1, "state": "open", "author": "a" },
46            { "n": 2, "state": "closed", "author": "b" },
47            { "n": 3, "state": "open", "author": "c" },
48        ]}));
49        scope.outputs.insert("api".to_string(), json!({ "body": { "items": [3, 1, 2] } }));
50        scope
51    }
52
53    #[tokio::test]
54    async fn shapes_the_list_a_template_names() {
55        let mut s = scope();
56        let n = node(json!({ "kind": "transform", "input": "{{trigger.prs}}", "ops": [
57            { "op": "filter", "expr": "item.state == 'open'" },
58            { "op": "map", "expr": "item.n" },
59        ]}));
60        assert_eq!(run(&n, &mut s).await.unwrap(), json!({ "value": [1, 3] }));
61        let sorted = node(json!({ "kind": "transform", "input": "{{api.body.items}}", "ops": [{ "op": "sort", "by": "item" }] }));
62        assert_eq!(run(&sorted, &mut s).await.unwrap(), json!({ "value": [1, 2, 3] }));
63    }
64
65    #[tokio::test]
66    async fn taint_follows_the_chain_and_a_count_is_derived() {
67        let mut s = scope();
68        let list = node(json!({ "kind": "transform", "input": "{{trigger.prs}}", "ops": [{ "op": "pick", "fields": ["n"] }] }));
69        run(&list, &mut s).await.unwrap();
70        assert_eq!(s.bits["t"], (true, false), "a list from the trigger is tainted, and so is what is picked from it");
71        let count = node(json!({ "kind": "transform", "input": "{{trigger.prs}}", "ops": [{ "op": "count" }] }));
72        assert_eq!(run(&count, &mut s).await.unwrap(), json!({ "value": 3 }));
73        assert_eq!(s.bits["t"], (false, true), "a count of a tainted list is a number made in the run");
74        let clean = node(json!({ "kind": "transform", "input": "{{api.body.items}}", "ops": [{ "op": "count" }] }));
75        run(&clean, &mut s).await.unwrap();
76        assert_eq!(s.bits["t"], (false, false), "nothing tainted was read");
77    }
78
79    #[tokio::test]
80    async fn a_bad_chain_or_a_missing_input_fails_the_block_naming_the_step() {
81        let mut s = scope();
82        let no_input = node(json!({ "kind": "transform", "ops": [] }));
83        assert!(run(&no_input, &mut s).await.unwrap_err().contains("missing `input`"));
84        let bad_op = node(json!({ "kind": "transform", "input": "{{trigger.prs}}", "ops": [{ "op": "launch" }] }));
85        assert!(run(&bad_op, &mut s).await.unwrap_err().contains("not a list of operations"));
86        let bad_expr = node(json!({ "kind": "transform", "input": "{{trigger.prs}}", "ops": [{ "op": "map", "expr": "item +" }] }));
87        assert!(run(&bad_expr, &mut s).await.unwrap_err().contains("step 0"));
88    }
89}