Skip to main content

agentmux_srv/drone/executor/blocks/
once.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The Once block: "do this once for this key" (docs/specs/SPEC_DRONE_REAL_WORLD_JOBS_2026_11_11.md ยง4.2).
5//!
6//! `{ "kind": "once", "key": "{{trigger.run_id}}" }`. The first run of the drone to reach it with a key goes on;
7//! a later run with the same key takes the other branch. It answers like a Condition does: `result` is true for the
8//! first and false for every later one, and the edges off its `true` and `false` handles carry the run on by it. The
9//! keys are kept in this machine's store, per drone, until the drone is deleted. The cloud form (across machines)
10//! comes with the cloud phases.
11
12use serde_json::{json, Value};
13
14use crate::drone::data_flow::ExecutionScope;
15use crate::drone::host;
16use crate::drone::storage::DroneStore;
17use crate::drone::types::FlowNode;
18use agentmux_common::time::now_ms;
19
20/// The longest key. A key is a short name for the thing done once (a run id, a commit, an issue number).
21const MAX_KEY: usize = 200;
22
23/// The key the block names, with its templates filled in.
24fn key_of(node: &FlowNode, scope: &ExecutionScope) -> Result<String, String> {
25    let raw = node.data.get("key").and_then(Value::as_str).unwrap_or_default();
26    let key = scope.resolve(raw).trim().to_string();
27    if key.is_empty() {
28        return Err("once block: `key` is empty once filled in, so there is nothing to do once for".to_string());
29    }
30    // A reference the run cannot fill stays as written; one key for every run would make the first the only one.
31    if key.contains("{{") {
32        return Err(format!("once block: `key` refers to something this run does not have ({key})"));
33    }
34    if key.chars().count() > MAX_KEY {
35        return Err(format!("once block: `key` is at most {MAX_KEY} characters"));
36    }
37    Ok(key)
38}
39
40pub async fn run(node: &FlowNode, scope: &ExecutionScope) -> Result<Value, String> {
41    let key = key_of(node, scope)?;
42    if scope.drone_id.is_empty() {
43        return Err("once block: a key is kept per saved drone, and this run is not of one".to_string());
44    }
45    let state = host::state()?;
46    let first = state.id_store.drone_once_claim(&scope.drone_id, &key, now_ms()).map_err(|e| format!("once block: {e}"))?;
47    Ok(json!({ "result": first, "first": first, "key": key }))
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    fn node(key: &str) -> FlowNode {
55        serde_json::from_value(json!({ "id": "o", "position": { "x": 0, "y": 0 }, "data": { "kind": "once", "key": key } })).unwrap()
56    }
57
58    #[test]
59    fn the_key_is_filled_in_and_must_have_text() {
60        let mut scope = ExecutionScope::for_drone("d1", json!({ "run_id": 42 }));
61        scope.drone_id = "d1".into();
62        assert_eq!(key_of(&node("run-{{trigger.run_id}}"), &scope).unwrap(), "run-42");
63        let err = key_of(&node(""), &scope).unwrap_err();
64        assert!(err.contains("nothing to do once for"), "{err}");
65        let err = key_of(&node("{{trigger.missing}}"), &scope).unwrap_err();
66        assert!(err.contains("does not have"), "{err}");
67        let long = "x".repeat(MAX_KEY + 1);
68        assert!(key_of(&node(&long), &scope).unwrap_err().contains("at most"));
69    }
70
71    #[tokio::test]
72    async fn the_first_run_with_a_key_is_first_and_a_later_one_is_not_and_another_drone_has_its_own() {
73        let state = crate::server::tests::test_state();
74        host::install(&state);
75        let run = |drone: &str, key: &str| {
76            let scope = ExecutionScope::for_drone(drone, Value::Null);
77            let n = node(key);
78            async move { run(&n, &scope).await.unwrap() }
79        };
80        assert_eq!(run("d1", "a").await["result"], true);
81        assert_eq!(run("d1", "a").await["result"], false, "the same key again");
82        assert_eq!(run("d1", "b").await["result"], true, "another key");
83        assert_eq!(run("d2", "a").await["result"], true, "another drone's keys are its own");
84        state.id_store.drone_delete("d1").unwrap();
85        assert_eq!(run("d1", "a").await["result"], true, "deleting a drone forgets its keys");
86    }
87}