mecha10-behavior-runtime 0.8.1

Behavior tree runtime for Mecha10 - unified AI and logic composition system
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Action node that relays live target requests to the `instance-track` node's control topic.
//!
//! Mirrors `set_rl_target.rs`, but relays a `bbox` instead of a `target_class`.

use crate::{BehaviorNode, Context, NodeStatus};
use async_trait::async_trait;
use mecha10_core::messages::Message;
use mecha10_core::topics::Topic;
use mecha10_core::Receiver;
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub(crate) struct BoundingBox {
    x: f32,
    y: f32,
    width: f32,
    height: f32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct InstanceTrackControlCommand {
    action: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    params: Option<Value>,
}

impl Message for InstanceTrackControlCommand {}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct InstanceTrackTargetRequest {
    #[serde(default)]
    pub(crate) bbox: Option<BoundingBox>,
}

impl Message for InstanceTrackTargetRequest {}

fn default_topic() -> String {
    "/instance-track/control".to_string()
}

/// The topic live target requests (clicks/drag-boxes on the dashboard's camera feed) arrive on.
/// `pub(crate)` so `InstanceTrackTaskNode` can subscribe to the same topic to track whether a
/// target is currently live, without hardcoding the string a second time.
pub(crate) fn default_request_topic() -> String {
    "/instance-track/target/request".to_string()
}

pub struct SetInstanceTrackTargetNode {
    topic: String,
    request_topic: String,
    target: Option<BoundingBox>,
    receiver: Option<Receiver<Value>>,
    published_initial: bool,
}

impl std::fmt::Debug for SetInstanceTrackTargetNode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SetInstanceTrackTargetNode")
            .field("topic", &self.topic)
            .field("request_topic", &self.request_topic)
            .field("target", &self.target)
            .field("subscribed", &self.receiver.is_some())
            .field("published_initial", &self.published_initial)
            .finish()
    }
}

#[derive(Debug, Serialize, Deserialize)]
struct SetInstanceTrackTargetConfig {
    #[serde(default = "default_topic")]
    topic: String,
    #[serde(default = "default_request_topic")]
    request_topic: String,
    #[serde(default)]
    bbox: Option<BoundingBox>,
}

impl SetInstanceTrackTargetNode {
    /// Create a new SetInstanceTrackTargetNode from JSON configuration. See `SetRlTargetNode::from_json`'s
    /// doc comment for why `null` is normalized to `{}` here.
    pub fn from_json(config: Value) -> anyhow::Result<Self> {
        let config = if config.is_null() {
            serde_json::json!({})
        } else {
            config
        };
        let cfg: SetInstanceTrackTargetConfig = serde_json::from_value(config)?;
        Ok(Self {
            topic: cfg.topic,
            request_topic: cfg.request_topic,
            target: cfg.bbox,
            receiver: None,
            published_initial: false,
        })
    }

    async fn publish_set_target(&self, ctx: &Context, bbox: BoundingBox) -> anyhow::Result<()> {
        let command = InstanceTrackControlCommand {
            action: "set_target".to_string(),
            params: Some(serde_json::json!({ "bbox": bbox })),
        };

        let topic_static: &'static str = Box::leak(self.topic.clone().into_boxed_str());
        let topic = Topic::<InstanceTrackControlCommand>::new(topic_static);

        ctx.publish_to(topic, &command).await?;
        tracing::debug!("SetInstanceTrackTargetNode: published set_target({:?})", bbox);
        Ok(())
    }

    async fn publish_clear(&self, ctx: &Context) -> anyhow::Result<()> {
        let command = InstanceTrackControlCommand {
            action: "clear".to_string(),
            params: None,
        };

        let topic_static: &'static str = Box::leak(self.topic.clone().into_boxed_str());
        let topic = Topic::<InstanceTrackControlCommand>::new(topic_static);

        ctx.publish_to(topic, &command).await?;
        tracing::debug!("SetInstanceTrackTargetNode: published clear");
        Ok(())
    }

    /// Drains every pending request off `receiver`, returning the last one seen (if any) - only
    /// the most recent request matters, same as `SetRlTargetNode::poll_latest_request`. A
    /// request with `bbox: null` (or no `bbox` key at all) means "clear".
    fn poll_latest_request(receiver: &mut Receiver<Value>) -> Option<Option<BoundingBox>> {
        let mut latest = None;
        while let Ok(value) = receiver.try_recv() {
            match serde_json::from_value::<InstanceTrackTargetRequest>(value) {
                Ok(request) => latest = Some(request.bbox),
                Err(e) => tracing::warn!("SetInstanceTrackTargetNode: dropping malformed target request: {}", e),
            }
        }
        latest
    }
}

#[async_trait]
impl BehaviorNode for SetInstanceTrackTargetNode {
    async fn tick(&mut self, ctx: &Context) -> anyhow::Result<NodeStatus> {
        if self.receiver.is_none() {
            self.receiver = Some(ctx.subscribe_raw::<Value>(&self.request_topic).await?);
        }

        if !self.published_initial {
            self.published_initial = true;
            if let Some(bbox) = self.target {
                self.publish_set_target(ctx, bbox).await?;
            }
        }

        let latest = Self::poll_latest_request(self.receiver.as_mut().expect("just subscribed above"));
        if let Some(bbox) = latest {
            self.target = bbox;
            match bbox {
                Some(bbox) => self.publish_set_target(ctx, bbox).await?,
                None => self.publish_clear(ctx).await?,
            }
        }

        Ok(if self.target.is_some() {
            NodeStatus::Running
        } else {
            NodeStatus::Failure
        })
    }

    fn name(&self) -> &str {
        "SetInstanceTrackTargetNode"
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_helpers::create_test_context;

    fn sample_bbox() -> BoundingBox {
        BoundingBox {
            x: 0.1,
            y: 0.2,
            width: 0.3,
            height: 0.4,
        }
    }

    #[test]
    fn from_json_reads_topic_request_topic_and_bbox() {
        let node = SetInstanceTrackTargetNode::from_json(serde_json::json!({
            "topic": "/instance-track/control",
            "request_topic": "/instance-track/target/request",
            "bbox": { "x": 0.1, "y": 0.2, "width": 0.3, "height": 0.4 }
        }))
        .unwrap();

        assert_eq!(node.topic, "/instance-track/control");
        assert_eq!(node.request_topic, "/instance-track/target/request");
        assert_eq!(node.target, Some(sample_bbox()));
    }

    #[test]
    fn from_json_defaults_topics_and_allows_no_target() {
        let node = SetInstanceTrackTargetNode::from_json(serde_json::json!({})).unwrap();

        assert_eq!(node.topic, "/instance-track/control");
        assert_eq!(node.request_topic, "/instance-track/target/request");
        assert_eq!(node.target, None);
    }

    #[test]
    fn from_json_accepts_null_the_same_as_an_empty_object() {
        let node = SetInstanceTrackTargetNode::from_json(Value::Null).unwrap();

        assert_eq!(node.topic, "/instance-track/control");
        assert_eq!(node.request_topic, "/instance-track/target/request");
        assert_eq!(node.target, None);
    }

    #[test]
    fn poll_latest_request_returns_none_when_empty() {
        let (_tx, rx) = tokio::sync::mpsc::unbounded_channel::<Value>();
        let mut receiver = Receiver::from(rx);
        assert_eq!(SetInstanceTrackTargetNode::poll_latest_request(&mut receiver), None);
    }

    #[test]
    fn poll_latest_request_keeps_only_the_most_recent_value() {
        let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<Value>();
        let mut receiver = Receiver::from(rx);
        tx.send(serde_json::json!({ "bbox": { "x": 0.0, "y": 0.0, "width": 0.1, "height": 0.1 } }))
            .unwrap();
        tx.send(serde_json::json!({ "bbox": { "x": 0.1, "y": 0.2, "width": 0.3, "height": 0.4 } }))
            .unwrap();

        assert_eq!(
            SetInstanceTrackTargetNode::poll_latest_request(&mut receiver),
            Some(Some(sample_bbox()))
        );
    }

    #[test]
    fn poll_latest_request_accepts_a_null_bbox_as_a_clear_request() {
        let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<Value>();
        let mut receiver = Receiver::from(rx);
        tx.send(serde_json::json!({ "bbox": null })).unwrap();

        assert_eq!(
            SetInstanceTrackTargetNode::poll_latest_request(&mut receiver),
            Some(None)
        );
    }

    #[test]
    fn poll_latest_request_accepts_an_absent_bbox_key_as_a_clear_request() {
        let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<Value>();
        let mut receiver = Receiver::from(rx);
        tx.send(serde_json::json!({})).unwrap();

        assert_eq!(
            SetInstanceTrackTargetNode::poll_latest_request(&mut receiver),
            Some(None)
        );
    }

    #[test]
    fn poll_latest_request_drops_malformed_requests_and_keeps_polling() {
        let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<Value>();
        let mut receiver = Receiver::from(rx);
        tx.send(serde_json::json!({ "bbox": "not-a-bbox" })).unwrap();
        tx.send(serde_json::json!({ "bbox": { "x": 0.1, "y": 0.2, "width": 0.3, "height": 0.4 } }))
            .unwrap();

        assert_eq!(
            SetInstanceTrackTargetNode::poll_latest_request(&mut receiver),
            Some(Some(sample_bbox()))
        );
    }

    #[tokio::test]
    #[ignore]
    async fn tick_publishes_configured_target_once_then_stays_running() {
        let ctx = create_test_context("test_set_instance_track_target").await;

        let mut node = SetInstanceTrackTargetNode::from_json(serde_json::json!({
            "topic": "/instance-track/control",
            "bbox": { "x": 0.1, "y": 0.2, "width": 0.3, "height": 0.4 }
        }))
        .unwrap();

        let mut receiver = ctx.subscribe_raw::<Value>("/instance-track/control").await.unwrap();

        let status = node.tick(&ctx).await.unwrap();
        assert_eq!(status, NodeStatus::Running);

        let received = tokio::time::timeout(std::time::Duration::from_millis(500), receiver.recv())
            .await
            .expect("timed out waiting for published command")
            .expect("channel closed without a message");

        assert_eq!(received["action"], "set_target");
        assert_eq!(received["params"]["bbox"]["x"], 0.1);

        let status = node.tick(&ctx).await.unwrap();
        assert_eq!(status, NodeStatus::Running);
        let timed_out = tokio::time::timeout(std::time::Duration::from_millis(200), receiver.recv()).await;
        assert!(timed_out.is_err(), "expected no second publish without a new request");
    }

    #[tokio::test]
    #[ignore]
    async fn tick_relays_a_live_request_published_on_request_topic() {
        let ctx = create_test_context("test_set_instance_track_target_request").await;

        let mut node = SetInstanceTrackTargetNode::from_json(serde_json::json!({
            "topic": "/instance-track/control",
            "request_topic": "/instance-track/target/request"
        }))
        .unwrap();

        node.tick(&ctx).await.unwrap();

        let mut control_receiver = ctx.subscribe_raw::<Value>("/instance-track/control").await.unwrap();
        ctx.publish_raw(
            "/instance-track/target/request",
            &serde_json::json!({ "bbox": { "x": 0.5, "y": 0.5, "width": 0.1, "height": 0.1 } }),
        )
        .await
        .unwrap();

        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        node.tick(&ctx).await.unwrap();

        let received = tokio::time::timeout(std::time::Duration::from_millis(500), control_receiver.recv())
            .await
            .expect("timed out waiting for relayed command")
            .expect("channel closed without a message");

        assert_eq!(received["action"], "set_target");
        assert_eq!(received["params"]["bbox"]["x"], 0.5);
        assert_eq!(
            node.tick(&ctx).await.unwrap(),
            NodeStatus::Running,
            "a live target must report Running"
        );
    }

    #[tokio::test]
    #[ignore = "Requires Redis infrastructure"]
    async fn tick_returns_failure_when_no_target_has_ever_been_set() {
        let ctx = create_test_context("test_set_instance_track_target_idle").await;

        let mut node = SetInstanceTrackTargetNode::from_json(serde_json::json!({
            "request_topic": "/instance-track/target/request"
        }))
        .unwrap();

        let status = node.tick(&ctx).await.unwrap();
        assert_eq!(
            status,
            NodeStatus::Failure,
            "no target yet means cede control, not Success"
        );
    }

    #[tokio::test]
    #[ignore = "Requires Redis infrastructure"]
    async fn tick_returns_failure_after_a_clear_request_even_with_a_configured_initial_target() {
        let ctx = create_test_context("test_set_instance_track_target_cleared").await;

        let mut node = SetInstanceTrackTargetNode::from_json(serde_json::json!({
            "request_topic": "/instance-track/target/request",
            "bbox": { "x": 0.1, "y": 0.2, "width": 0.3, "height": 0.4 }
        }))
        .unwrap();

        assert_eq!(node.tick(&ctx).await.unwrap(), NodeStatus::Running);

        ctx.publish_raw("/instance-track/target/request", &serde_json::json!({ "bbox": null }))
            .await
            .unwrap();
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;

        assert_eq!(node.tick(&ctx).await.unwrap(), NodeStatus::Failure);
    }

    #[tokio::test]
    #[ignore]
    async fn tick_relays_a_clear_request_published_on_request_topic() {
        let ctx = create_test_context("test_set_instance_track_target_clear").await;

        let mut node = SetInstanceTrackTargetNode::from_json(serde_json::json!({
            "topic": "/instance-track/control",
            "request_topic": "/instance-track/target/request"
        }))
        .unwrap();

        node.tick(&ctx).await.unwrap();

        let mut control_receiver = ctx.subscribe_raw::<Value>("/instance-track/control").await.unwrap();
        ctx.publish_raw("/instance-track/target/request", &serde_json::json!({ "bbox": null }))
            .await
            .unwrap();

        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        node.tick(&ctx).await.unwrap();

        let received = tokio::time::timeout(std::time::Duration::from_millis(500), control_receiver.recv())
            .await
            .expect("timed out waiting for relayed command")
            .expect("channel closed without a message");

        assert_eq!(received["action"], "clear");
    }
}