Skip to main content

agentmux_srv/drone/executor/
effect.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! What a block does to the world, which decides what a restart may do with one
5//! that was running when the machine stopped
6//! (docs/specs/SPEC_DRONE_GENERAL_AUTOMATION_2026_11_11.md ยง3.3, "the step that was in flight").
7//!
8//! A **read-only** or **idempotent** block is simply run again. An **effectful**
9//! one is not run again on its own, because it may already have done its work:
10//! the run waits for a person or an agent to say whether to run it again, skip
11//! it, or cancel the run (at most once for effects).
12
13use serde_json::Value;
14
15use crate::drone::types::{BlockKind, FlowNode};
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum Effect {
19    /// Reads, or only works on what the run already holds.
20    ReadOnly,
21    /// Does something, but doing it twice leaves the world as doing it once
22    /// (a request that carries an idempotency key).
23    Idempotent,
24    /// Does something that a second time would repeat: sends, starts, writes.
25    Effectful,
26}
27
28/// What `node`, a block of `kind`, does. `body` is what a for-each's body block
29/// does, and is ignored for any other kind. `resolve` fills in a template with what
30/// the run holds now: a key is a key only when it has text once it is filled in.
31pub 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        // An Agent block starts a session that costs money and may act; a Message,
36        // a Notify and a Spawn reach someone or start something. A Task is filed
37        // again once the task it dedupes against is over, so its `dedupe` key does
38        // not make a second run of it harmless.
39        BlockKind::Task | BlockKind::Agent | BlockKind::Message | BlockKind::Notify | BlockKind::Spawn => Effect::Effectful,
40        // A command may change anything it can reach, and a restart cannot tell how far it got.
41        BlockKind::Run => Effect::Effectful,
42        // It claims a key: claimed again by a run that is taken up after a restart it would say "seen", so a restart asks.
43        BlockKind::Once => Effect::Effectful,
44        // The callee may do anything, and a restart cannot tell how far it got.
45        BlockKind::Drone => Effect::Effectful,
46        // The items already done would be done again, so it is as the body is.
47        BlockKind::ForEach => body,
48    }
49}
50
51/// A request that only reads is read-only; one that writes is idempotent when it
52/// sends an `Idempotency-Key` header that has text once filled in, and effectful
53/// when it does not.
54fn 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        // A dedupe key reuses only a task that is still open: one that finished before the restart is filed again.
114        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}