reflow_network 0.2.1

Network executor for Reflow — routes messages between actors, manages subgraphs, and emits runtime events.
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;

use futures::StreamExt;
use reflow_tracing_protocol::client::TracingIntegration;

use crate::actor::{Actor, ActorBehavior, ActorConfig, ActorLoad, Port};
use crate::connector::{ConnectionPoint, Connector};
use crate::graph::types::GraphExport;
use crate::message::Message;
use crate::network::{Network, NetworkConfig};

/// A lightweight actor that bridges an inner network's outport to the
/// SubgraphActor's external outport channel. Registered as a node inside the
/// inner network and connected via a standard Connector.
struct OutportBridge {
    /// The external sender — messages forwarded here appear on the SubgraphActor's outport
    external_sender: flume::Sender<HashMap<String, Message>>,
    /// The external port name used as the HashMap key when forwarding
    external_port_name: String,
    /// The inner port name to extract from incoming packets
    inner_port_name: String,
    inports: Port,
    outports: Port,
    load: Arc<ActorLoad>,
}

impl OutportBridge {
    fn new(
        external_sender: flume::Sender<HashMap<String, Message>>,
        external_port_name: String,
        inner_port_name: String,
    ) -> Self {
        OutportBridge {
            external_sender,
            external_port_name,
            inner_port_name,
            inports: flume::unbounded(),
            outports: flume::unbounded(),
            load: Arc::new(ActorLoad::new(0)),
        }
    }
}

impl Actor for OutportBridge {
    fn get_behavior(&self) -> ActorBehavior {
        // Routing is handled in create_process; behavior is a no-op.
        Box::new(|_ctx| Box::pin(async { Ok(HashMap::new()) }))
    }

    fn get_inports(&self) -> Port {
        self.inports.clone()
    }

    fn get_outports(&self) -> Port {
        self.outports.clone()
    }

    fn load_count(&self) -> Arc<ActorLoad> {
        self.load.clone()
    }

    fn create_instance(&self) -> Arc<dyn Actor> {
        Arc::new(Self::new(
            self.external_sender.clone(),
            self.external_port_name.clone(),
            self.inner_port_name.clone(),
        ))
    }

    fn create_process(
        &self,
        _config: ActorConfig,
        _tracing: Option<TracingIntegration>,
    ) -> Pin<Box<dyn futures::Future<Output = ()> + 'static + Send>> {
        let receiver = self.inports.1.clone();
        let external_sender = self.external_sender.clone();
        let ext_port = self.external_port_name.clone();
        let inner_port = self.inner_port_name.clone();

        Box::pin(async move {
            while let Some(mut packet) = receiver.clone().stream().next().await {
                if let Some(msg) = packet.remove(&inner_port) {
                    let out = HashMap::from([(ext_port.clone(), msg)]);
                    if external_sender.send(out).is_err() {
                        break;
                    }
                }
            }
        })
    }
}

/// A SubgraphActor wraps an inner `Network` and exposes it as a single Actor
/// in the parent network. Messages route through inport/outport boundary maps
/// derived from `GraphExport.inports` / `GraphExport.outports`.
///
/// Outbound routing uses `OutportBridge` actors registered inside the inner
/// network, connected via standard `Connector`s — the same pattern used for
/// all other inter-actor communication.
///
/// **Note**: The connector architecture uses competing consumers (flume MPMC).
/// Inner actors whose ports are mapped to external outports should not also
/// have internal connections from those same ports, as messages would be
/// split between consumers rather than broadcast.
pub struct SubgraphActor {
    /// The inner network this subgraph wraps
    inner_network: Arc<parking_lot::Mutex<Network>>,
    /// Original graph export, retained so the runtime can instantiate a
    /// fresh isolated copy for each parent node that uses this subgraph.
    graph_export: Option<GraphExport>,
    /// Actor templates required by the graph export.
    actor_templates: HashMap<String, Arc<dyn Actor>>,
    /// Maps external inport name → (inner_actor_id, inner_port_name)
    inport_map: HashMap<String, (String, String)>,
    /// Maps external outport name → (inner_actor_id, inner_port_name)
    outport_map: HashMap<String, (String, String)>,
    /// External inports — parent network sends messages here
    inports: Port,
    /// External outports — parent network reads messages from here
    outports: Port,
    /// Load counter
    load: Arc<ActorLoad>,
    /// Cancellation signal — send `true` to stop the inbound routing loop
    shutdown_tx: Arc<tokio::sync::watch::Sender<bool>>,
    shutdown_rx: tokio::sync::watch::Receiver<bool>,
}

impl SubgraphActor {
    /// Create a SubgraphActor from a `GraphExport` and a set of pre-registered actors.
    ///
    /// The `actors` map must contain entries for every `component` referenced by
    /// `graph_export.processes`. Inport/outport boundary maps are built from
    /// `graph_export.inports` and `graph_export.outports`.
    ///
    /// For each outport mapping, an `OutportBridge` actor is automatically
    /// registered inside the inner network and connected via a standard Connector.
    pub fn from_graph_export(
        graph_export: &GraphExport,
        actors: HashMap<String, Arc<dyn Actor>>,
    ) -> Result<Self, anyhow::Error> {
        let mut inner_network = Network::new(NetworkConfig::default());
        let actor_templates = actors.clone();

        // Register actors (components) in the inner network
        for (name, actor) in actors {
            inner_network.register_actor_arc(&name, actor)?;
        }

        // Add nodes (processes) from the graph export
        for (id, node) in &graph_export.processes {
            inner_network.add_node(id, &node.component, node.metadata.clone())?;
        }

        // Add internal connections
        for conn in &graph_export.connections {
            inner_network.add_connection(Connector::new(
                ConnectionPoint::new(
                    &conn.from.node_id,
                    &conn.from.port_id,
                    conn.data.clone().map(Message::from),
                ),
                ConnectionPoint::new(&conn.to.node_id, &conn.to.port_id, None),
            ));
        }

        // Build inport map: external port name → (inner actor id, inner port name)
        let mut inport_map = HashMap::new();
        for (ext_port, edge) in &graph_export.inports {
            inport_map.insert(
                ext_port.clone(),
                (edge.node_id.clone(), edge.port_name.clone()),
            );
        }

        // Build outport map and register OutportBridge actors inside the inner network
        let outports: Port = flume::unbounded();
        let mut outport_map = HashMap::new();

        for (ext_port, edge) in &graph_export.outports {
            outport_map.insert(
                ext_port.clone(),
                (edge.node_id.clone(), edge.port_name.clone()),
            );

            // Create a bridge actor that forwards to the external outport sender
            let bridge =
                OutportBridge::new(outports.0.clone(), ext_port.clone(), edge.port_name.clone());

            let bridge_component = format!("__outport_bridge_{}", ext_port);
            let bridge_node_id = format!("__outport_bridge_{}", ext_port);

            inner_network.register_actor_arc(&bridge_component, Arc::new(bridge))?;
            inner_network.add_node(&bridge_node_id, &bridge_component, None)?;

            // Connect inner actor's outport → bridge's inport via standard Connector
            inner_network.add_connection(Connector::new(
                ConnectionPoint::new(&edge.node_id, &edge.port_id, None),
                ConnectionPoint::new(&bridge_node_id, &edge.port_name, None),
            ));
        }

        let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false);

        Ok(SubgraphActor {
            inner_network: Arc::new(parking_lot::Mutex::new(inner_network)),
            graph_export: Some(graph_export.clone()),
            actor_templates,
            inport_map,
            outport_map,
            inports: flume::unbounded(),
            outports,
            load: Arc::new(ActorLoad::new(0)),
            shutdown_tx: Arc::new(shutdown_tx),
            shutdown_rx,
        })
    }

    /// Create a SubgraphActor from a pre-built inner network and explicit port maps.
    pub fn new(
        inner_network: Network,
        inport_map: HashMap<String, (String, String)>,
        outport_map: HashMap<String, (String, String)>,
    ) -> Self {
        let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false);

        SubgraphActor {
            inner_network: Arc::new(parking_lot::Mutex::new(inner_network)),
            graph_export: None,
            actor_templates: HashMap::new(),
            inport_map,
            outport_map,
            inports: flume::unbounded(),
            outports: flume::unbounded(),
            load: Arc::new(ActorLoad::new(0)),
            shutdown_tx: Arc::new(shutdown_tx),
            shutdown_rx,
        }
    }

    /// Get a reference to the inner network (locked).
    pub fn inner_network(&self) -> &Arc<parking_lot::Mutex<Network>> {
        &self.inner_network
    }

    /// Get the inport map.
    pub fn inport_map(&self) -> &HashMap<String, (String, String)> {
        &self.inport_map
    }

    /// Get the outport map.
    pub fn outport_map(&self) -> &HashMap<String, (String, String)> {
        &self.outport_map
    }
}

impl Actor for SubgraphActor {
    fn get_behavior(&self) -> ActorBehavior {
        // SubgraphActor doesn't use a simple behavior function — routing is
        // handled entirely in create_process. Return a no-op behavior.
        Box::new(|_context| Box::pin(async { Ok(HashMap::new()) }))
    }

    fn get_inports(&self) -> Port {
        self.inports.clone()
    }

    fn get_outports(&self) -> Port {
        self.outports.clone()
    }

    fn load_count(&self) -> Arc<ActorLoad> {
        self.load.clone()
    }

    fn create_instance(&self) -> Arc<dyn Actor> {
        if let Some(graph_export) = &self.graph_export {
            return Arc::new(
                SubgraphActor::from_graph_export(graph_export, self.actor_templates.clone())
                    .expect("Failed to clone subgraph actor from graph export"),
            );
        }

        Arc::new(SubgraphActor::new(
            self.inner_network.lock().clone(),
            self.inport_map.clone(),
            self.outport_map.clone(),
        ))
    }

    fn create_process(
        &self,
        _config: ActorConfig,
        _tracing_integration: Option<TracingIntegration>,
    ) -> Pin<Box<dyn futures::Future<Output = ()> + 'static + Send>> {
        let inner_network = self.inner_network.clone();
        let inport_map = self.inport_map.clone();
        let inport_receiver = self.inports.1.clone();
        let load = self.load.clone();
        let mut shutdown_rx = self.shutdown_rx.clone();

        Box::pin(async move {
            // 1. Start the inner network (initializes actors, connectors, IIPs).
            //    OutportBridge actors are started as part of this — their connectors
            //    handle outbound routing automatically.
            {
                let mut network = inner_network.lock();
                if let Err(e) = network.start() {
                    tracing::error!("[SUBGRAPH] Failed to start inner network: {}", e);
                    return;
                }
            }

            // 2. Inbound routing loop: receive on external inports and route
            //    to the appropriate inner actor via send_to_actor.
            let mut inport_stream = inport_receiver.stream();
            loop {
                tokio::select! {
                    biased;
                    _ = shutdown_rx.changed() => {
                        break;
                    }
                    packet = inport_stream.next() => {
                        match packet {
                            Some(pkt) => {
                                load.inc();
                                for (port_name, message) in pkt {
                                    if let Some((inner_actor_id, inner_port)) = inport_map.get(&port_name) {
                                        let network = inner_network.lock();
                                        if let Err(e) = network.send_to_actor(inner_actor_id, inner_port, message) {
                                            tracing::error!(
                                                "[SUBGRAPH] Failed to route inport '{}' to {}:{} — {}",
                                                port_name, inner_actor_id, inner_port, e
                                            );
                                        }
                                    } else {
                                        tracing::warn!("[SUBGRAPH] No inport mapping for port '{}'", port_name);
                                    }
                                }
                                load.dec();
                            }
                            None => break,
                        }
                    }
                }
            }
        })
    }

    fn shutdown(&self) {
        // Signal the inbound routing loop to stop
        let _ = self.shutdown_tx.send(true);

        // Shutdown the inner network (cascades to inner actors, bridges, and processes)
        let network = self.inner_network.lock();
        network.shutdown();
    }

    fn cleanup(&self) {
        let _ = self.shutdown_tx.send(true);
    }
}

#[cfg(test)]
#[cfg(not(target_arch = "wasm32"))]
mod tests {
    use super::*;
    use crate::graph::types::{GraphConnection, GraphEdge, GraphNode};

    /// Simple pass-through actor: receives on "in", sends the same message on "out".
    /// Uses the default `create_process` via ActorProcess — no boilerplate loop.
    struct PassthroughActor {
        inports: Port,
        outports: Port,
        load: Arc<ActorLoad>,
    }

    impl PassthroughActor {
        fn new() -> Self {
            Self {
                inports: flume::unbounded(),
                outports: flume::unbounded(),
                load: Arc::new(ActorLoad::new(0)),
            }
        }
    }

    impl Actor for PassthroughActor {
        fn get_behavior(&self) -> ActorBehavior {
            Box::new(|context| {
                let payload = context.get_payload().clone();
                Box::pin(async move {
                    if let Some(msg) = payload.get("in") {
                        let result = HashMap::from([("out".to_string(), msg.clone())]);
                        return Ok(result);
                    }
                    Ok(HashMap::new())
                })
            })
        }

        fn get_inports(&self) -> Port {
            self.inports.clone()
        }

        fn get_outports(&self) -> Port {
            self.outports.clone()
        }

        fn load_count(&self) -> Arc<ActorLoad> {
            self.load.clone()
        }

        fn create_instance(&self) -> Arc<dyn Actor> {
            Arc::new(Self::new())
        }

        fn inport_names(&self) -> Vec<String> {
            vec!["in".into()]
        }

        fn outport_names(&self) -> Vec<String> {
            vec!["out".into()]
        }

        // create_process() uses the default trait impl via ActorProcess
    }

    /// Build a simple subgraph: one inner PassthroughActor.
    /// External inport "input" → inner actor "pass" port "in"
    /// Inner actor "pass" port "out" → external outport "output"
    fn build_simple_subgraph() -> SubgraphActor {
        let graph_export = GraphExport {
            processes: HashMap::from([(
                "pass".to_string(),
                GraphNode {
                    id: "pass".to_string(),
                    component: "Passthrough".to_string(),
                    metadata: None,
                },
            )]),
            inports: HashMap::from([(
                "input".to_string(),
                GraphEdge {
                    node_id: "pass".to_string(),
                    port_name: "in".to_string(),
                    port_id: "in".to_string(),
                    ..Default::default()
                },
            )]),
            outports: HashMap::from([(
                "output".to_string(),
                GraphEdge {
                    node_id: "pass".to_string(),
                    port_name: "out".to_string(),
                    port_id: "out".to_string(),
                    ..Default::default()
                },
            )]),
            ..Default::default()
        };

        let actors: HashMap<String, Arc<dyn Actor>> = HashMap::from([(
            "Passthrough".to_string(),
            Arc::new(PassthroughActor::new()) as Arc<dyn Actor>,
        )]);

        SubgraphActor::from_graph_export(&graph_export, actors).expect("Failed to build subgraph")
    }

    #[tokio::test]
    async fn test_subgraph_actor_creation() {
        let subgraph = build_simple_subgraph();

        assert_eq!(subgraph.inport_map().len(), 1);
        assert_eq!(subgraph.outport_map().len(), 1);
        assert!(subgraph.inport_map().contains_key("input"));
        assert!(subgraph.outport_map().contains_key("output"));

        let (inner_actor, inner_port) = &subgraph.inport_map()["input"];
        assert_eq!(inner_actor, "pass");
        assert_eq!(inner_port, "in");

        let (inner_actor, inner_port) = &subgraph.outport_map()["output"];
        assert_eq!(inner_actor, "pass");
        assert_eq!(inner_port, "out");
    }

    #[tokio::test]
    async fn test_subgraph_in_parent_network() {
        let subgraph = build_simple_subgraph();
        let mut parent = Network::new(NetworkConfig::default());

        parent
            .register_actor_arc("SubgraphComponent", Arc::new(subgraph))
            .unwrap();
        parent
            .add_node("sub_node", "SubgraphComponent", None)
            .unwrap();

        parent.start().unwrap();

        let msg = Message::from(serde_json::json!({"hello": "world"}));
        parent
            .send_to_actor("sub_node", "input", msg.clone())
            .unwrap();

        tokio::time::sleep(std::time::Duration::from_millis(200)).await;

        let sub_actor = parent.initialized_actors.get("sub_node").unwrap();
        let outport_receiver = sub_actor.get_outports().1;

        match outport_receiver.try_recv() {
            Ok(result) => {
                assert!(
                    result.contains_key("output"),
                    "Expected 'output' port in result"
                );
                let output_msg = &result["output"];
                let expected: serde_json::Value = serde_json::json!({"hello": "world"});
                let actual: serde_json::Value = output_msg.clone().into();
                assert_eq!(actual, expected);
            }
            Err(_) => {
                panic!("Expected a message on the subgraph outport but got none");
            }
        }

        parent.shutdown();
    }

    #[tokio::test]
    async fn test_subgraph_chained_actors() {
        let graph_export = GraphExport {
            processes: HashMap::from([
                (
                    "actor_a".to_string(),
                    GraphNode {
                        id: "actor_a".to_string(),
                        component: "PassthroughA".to_string(),
                        metadata: None,
                    },
                ),
                (
                    "actor_b".to_string(),
                    GraphNode {
                        id: "actor_b".to_string(),
                        component: "PassthroughB".to_string(),
                        metadata: None,
                    },
                ),
            ]),
            connections: vec![GraphConnection {
                from: GraphEdge {
                    node_id: "actor_a".to_string(),
                    port_name: "out".to_string(),
                    port_id: "out".to_string(),
                    ..Default::default()
                },
                to: GraphEdge {
                    node_id: "actor_b".to_string(),
                    port_name: "in".to_string(),
                    port_id: "in".to_string(),
                    ..Default::default()
                },
                metadata: None,
                data: None,
            }],
            inports: HashMap::from([(
                "input".to_string(),
                GraphEdge {
                    node_id: "actor_a".to_string(),
                    port_name: "in".to_string(),
                    port_id: "in".to_string(),
                    ..Default::default()
                },
            )]),
            outports: HashMap::from([(
                "output".to_string(),
                GraphEdge {
                    node_id: "actor_b".to_string(),
                    port_name: "out".to_string(),
                    port_id: "out".to_string(),
                    ..Default::default()
                },
            )]),
            ..Default::default()
        };

        let actors: HashMap<String, Arc<dyn Actor>> = HashMap::from([
            (
                "PassthroughA".to_string(),
                Arc::new(PassthroughActor::new()) as Arc<dyn Actor>,
            ),
            (
                "PassthroughB".to_string(),
                Arc::new(PassthroughActor::new()) as Arc<dyn Actor>,
            ),
        ]);

        let subgraph = SubgraphActor::from_graph_export(&graph_export, actors).unwrap();

        let mut parent = Network::new(NetworkConfig::default());
        parent
            .register_actor_arc("ChainedSubgraph", Arc::new(subgraph))
            .unwrap();
        parent
            .add_node("chain_node", "ChainedSubgraph", None)
            .unwrap();
        parent.start().unwrap();

        let msg = Message::from(serde_json::json!(42));
        parent.send_to_actor("chain_node", "input", msg).unwrap();

        tokio::time::sleep(std::time::Duration::from_millis(300)).await;

        let sub_actor = parent.initialized_actors.get("chain_node").unwrap();
        let outport_receiver = sub_actor.get_outports().1;

        match outport_receiver.try_recv() {
            Ok(result) => {
                assert!(result.contains_key("output"));
                let actual: serde_json::Value = result["output"].clone().into();
                assert_eq!(actual, serde_json::json!(42));
            }
            Err(_) => {
                panic!("Expected message on chained subgraph outport");
            }
        }

        parent.shutdown();
    }

    #[tokio::test]
    async fn test_subgraph_shutdown_cleans_up() {
        let subgraph = build_simple_subgraph();
        let mut parent = Network::new(NetworkConfig::default());

        parent
            .register_actor_arc("SubgraphComponent", Arc::new(subgraph))
            .unwrap();
        parent
            .add_node("sub_node", "SubgraphComponent", None)
            .unwrap();

        parent.start().unwrap();

        // Send a message to verify it's working
        let msg = Message::from(serde_json::json!("test"));
        parent.send_to_actor("sub_node", "input", msg).unwrap();
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;

        // Shutdown should complete without hanging
        parent.shutdown();

        // Verify load is zero after shutdown
        let sub_actor = parent.actors.get("SubgraphComponent").unwrap();
        assert_eq!(sub_actor.load_count().get(), 0);
    }
}