Skip to main content

agentmux_srv_core/backend/
drone_wait.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The event a Drone run's Wait block (`on`) waits for
5//! (docs/specs/SPEC_DRONE_GENERAL_AUTOMATION_2026_11_11.md ยง3.3, "Waiting").
6//!
7//! The wait is a random key made when it starts, not derived from anything an
8//! outsider can see. Whoever holds the key can end the wait, once, and what they
9//! send with it is outside text: the run reads it as tainted. The key goes out in
10//! the run's `Waiting` event and is kept in the run's checkpoint, so a restart
11//! takes the same wait up again; an event that arrives while AgentMux is stopped
12//! is not kept, its source sends it again. It lives in the core so the RPC handler
13//! can answer without reaching up into `drone/`; the executor is the one that waits.
14
15use std::collections::HashMap;
16use std::sync::{LazyLock, Mutex};
17
18use serde_json::Value;
19use tokio::sync::oneshot;
20
21/// The waits that are open, by key.
22static WAITING: LazyLock<Mutex<HashMap<String, oneshot::Sender<Value>>>> = LazyLock::new(|| Mutex::new(HashMap::new()));
23
24/// A new key: 256 random bits as hex, so it cannot be guessed.
25pub fn new_key() -> String {
26    format!("{}{}", uuid::Uuid::new_v4().simple(), uuid::Uuid::new_v4().simple())
27}
28
29/// A wait on `key` is open: the receiver gets what the event brings.
30pub fn open(key: &str) -> oneshot::Receiver<Value> {
31    let (tx, rx) = oneshot::channel();
32    WAITING.lock().unwrap_or_else(|e| e.into_inner()).insert(key.to_string(), tx);
33    rx
34}
35
36/// The wait on `key` is over (it was answered, or its run ended).
37pub fn close(key: &str) {
38    WAITING.lock().unwrap_or_else(|e| e.into_inner()).remove(key);
39}
40
41/// End the wait on `key` with `payload`. `Err` when none is open: the key is
42/// wrong, the wait was answered already, or its run ended.
43pub fn signal(key: &str, payload: Value) -> Result<(), String> {
44    let open = WAITING.lock().unwrap_or_else(|e| e.into_inner()).remove(key);
45    let Some(tx) = open else { return Err("no run is waiting on that key".to_string()) };
46    tx.send(payload).map_err(|_| "the run stopped waiting".to_string())
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52    use serde_json::json;
53
54    #[test]
55    fn keys_are_long_and_never_repeat() {
56        let (a, b) = (new_key(), new_key());
57        assert_eq!(a.len(), 64);
58        assert_ne!(a, b);
59    }
60
61    #[tokio::test]
62    async fn a_wait_is_answered_once_with_what_the_event_brought() {
63        let key = new_key();
64        let rx = open(&key);
65        signal(&key, json!({ "ok": true })).unwrap();
66        assert_eq!(rx.await.unwrap(), json!({ "ok": true }));
67        assert!(signal(&key, json!(1)).is_err(), "a second answer finds nothing waiting");
68    }
69
70    #[test]
71    fn a_wrong_key_or_a_closed_wait_is_refused() {
72        assert!(signal("not-a-key", json!(1)).is_err());
73        let key = new_key();
74        let _rx = open(&key);
75        close(&key);
76        assert!(signal(&key, json!(1)).is_err());
77    }
78}