agentmux_srv/drone/executor/
effect.rs1use serde_json::Value;
14
15use crate::drone::types::{BlockKind, FlowNode};
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum Effect {
19 ReadOnly,
21 Idempotent,
24 Effectful,
26}
27
28pub fn effect_of(kind: BlockKind, node: &FlowNode, body: Effect, resolve: &dyn Fn(&str) -> String) -> Effect {
32 match kind {
33 BlockKind::Condition | BlockKind::Response | BlockKind::Variables | BlockKind::Transform | BlockKind::AppQuery | BlockKind::Wait | BlockKind::Approval | BlockKind::Join => Effect::ReadOnly,
34 BlockKind::Api => api_effect(node, resolve),
35 BlockKind::Task | BlockKind::Agent | BlockKind::Message | BlockKind::Notify | BlockKind::Spawn => Effect::Effectful,
40 BlockKind::Run => Effect::Effectful,
42 BlockKind::Once => Effect::Effectful,
44 BlockKind::Drone => Effect::Effectful,
46 BlockKind::ForEach => body,
48 }
49}
50
51fn api_effect(node: &FlowNode, resolve: &dyn Fn(&str) -> String) -> Effect {
55 let method = node.data.get("method").and_then(Value::as_str).unwrap_or("GET").to_uppercase();
56 if matches!(method.as_str(), "GET" | "HEAD" | "OPTIONS") {
57 return Effect::ReadOnly;
58 }
59 let keyed = node
60 .data
61 .get("headers")
62 .and_then(Value::as_object)
63 .is_some_and(|h| h.iter().any(|(name, v)| name.eq_ignore_ascii_case("idempotency-key") && v.as_str().is_some_and(|t| !resolve(t).trim().is_empty())));
64 if keyed {
65 Effect::Idempotent
66 } else {
67 Effect::Effectful
68 }
69}
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74 use crate::drone::types::NodePosition;
75 use serde_json::json;
76
77 fn node(data: Value) -> FlowNode {
78 FlowNode { id: "n".into(), position: NodePosition::default(), data, node_type: None }
79 }
80
81 fn of(kind: BlockKind, data: Value) -> Effect {
82 effect_of(kind, &node(data), Effect::ReadOnly, &|t| t.to_string())
83 }
84
85 #[test]
86 fn the_blocks_that_only_work_on_the_runs_own_state_or_read_are_read_only() {
87 for kind in [BlockKind::Condition, BlockKind::Response, BlockKind::Variables, BlockKind::Transform, BlockKind::AppQuery] {
88 assert_eq!(of(kind, json!({})), Effect::ReadOnly, "{kind:?}");
89 }
90 }
91
92 #[test]
93 fn a_request_is_read_only_to_read_and_effectful_to_write_unless_it_carries_a_key() {
94 for m in ["GET", "get", "HEAD", "OPTIONS"] {
95 assert_eq!(of(BlockKind::Api, json!({ "method": m })), Effect::ReadOnly, "{m}");
96 }
97 assert_eq!(of(BlockKind::Api, json!({})), Effect::ReadOnly, "no method is a GET");
98 for m in ["POST", "put", "PATCH", "DELETE"] {
99 assert_eq!(of(BlockKind::Api, json!({ "method": m })), Effect::Effectful, "{m}");
100 }
101 let keyed = json!({ "method": "POST", "headers": { "idempotency-key": "{{trigger.id}}" } });
102 assert_eq!(of(BlockKind::Api, keyed), Effect::Idempotent);
103 let empty = json!({ "method": "POST", "headers": { "Idempotency-Key": " " } });
104 assert_eq!(of(BlockKind::Api, empty), Effect::Effectful, "a blank key is no key");
105 }
106
107 #[test]
108 fn what_sends_starts_or_costs_is_effectful_and_so_is_a_task_whatever_its_dedupe_key() {
109 for kind in [BlockKind::Agent, BlockKind::Message, BlockKind::Notify, BlockKind::Spawn, BlockKind::Run] {
110 assert_eq!(of(kind, json!({})), Effect::Effectful, "{kind:?}");
111 }
112 assert_eq!(of(BlockKind::Task, json!({ "title": "x" })), Effect::Effectful);
113 assert_eq!(of(BlockKind::Task, json!({ "title": "x", "dedupe": "{{a.id}}" })), Effect::Effectful);
115 }
116
117 #[test]
118 fn a_key_that_is_empty_once_filled_in_is_no_key() {
119 let keyed = node(json!({ "method": "POST", "headers": { "Idempotency-Key": "{{trigger.key}}" } }));
120 let with = |key: &'static str| effect_of(BlockKind::Api, &keyed, Effect::ReadOnly, &move |_| key.to_string());
121 assert_eq!(with("abc"), Effect::Idempotent);
122 assert_eq!(with(""), Effect::Effectful, "the request goes out with an empty key");
123 assert_eq!(with(" "), Effect::Effectful);
124 }
125
126 #[test]
127 fn a_for_each_is_as_its_body_is() {
128 for body in [Effect::ReadOnly, Effect::Idempotent, Effect::Effectful] {
129 assert_eq!(effect_of(BlockKind::ForEach, &node(json!({})), body, &|t| t.to_string()), body);
130 }
131 }
132}