theta 0.1.0-alpha.56

An Rust Actor Framework
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
use std::sync::{Arc, LazyLock, Mutex};

use futures::channel::oneshot;
use iroh::Endpoint;
use theta_flume::unbounded_with_id;
use tracing::{error, trace};
use uuid::Uuid;

#[cfg(feature = "monitor")]
use crate::monitor::HDLS;
use crate::{
    actor::{Actor, ActorArgs, ActorId},
    actor_instance::ActorConfig,
    actor_ref::{ActorHdl, ActorRef, AnyActorRef, WeakActorHdl, WeakActorRef},
    base::{BindingError, Hex, Ident, parse_ident},
    compat,
    message::RawSignal,
};
#[cfg(feature = "remote")]
use {
    crate::remote::{base::ActorTypeId, peer::LocalPeer},
    iroh::PublicKey,
};

/// Global registry mapping identifiers to actor references for named bindings.
/// Backed by `ConcurrentMap` for reduced contention on high lookup concurrency.
pub(crate) static BINDINGS: LazyLock<compat::ConcurrentMap<Ident, Arc<dyn AnyActorRef>>> =
    LazyLock::new(compat::ConcurrentMap::default);

/// Actor execution context providing communication and spawning capabilities.
///
/// The `Context` is passed to actor methods and provides access to:
/// - Self reference for sending messages to itself via `ctx.this.upgrade()`
/// - Child actor management via `ctx.spawn()`
/// - Actor metadata via `ctx.id()`
/// - Lifecycle control via `ctx.terminate()`
///
/// # Type Parameters
///
/// * `A` - The actor type this context belongs to
#[derive(Debug)]
pub struct Context<A: Actor> {
    /// Weak reference to this actor instance
    pub this: WeakActorRef<A>,
    pub(crate) this_hdl: ActorHdl,
    pub(crate) child_hdls: Arc<Mutex<Vec<WeakActorHdl>>>,
}

impl<A: Actor> Clone for Context<A> {
    fn clone(&self) -> Self {
        Self {
            this: self.this.clone(),
            this_hdl: self.this_hdl.clone(),
            child_hdls: self.child_hdls.clone(),
        }
    }
}

/// Root context for the actor system.
///
/// `RootContext` serves as the entry point for the actor system and provides
/// methods for:
/// - Initializing the actor system (local or remote)
/// - Spawning top-level actors
/// - Binding actors to names for lookup
/// - Looking up remote actors (with `remote` feature)
///
/// # Examples
///
/// ```
/// use theta::prelude::*;
///
/// #[derive(Debug, Clone, ActorArgs)]
/// struct MyActor { value: i32 }
///
/// #[actor("12345678-1234-5678-9abc-123456789abc")]
/// impl Actor for MyActor {}
///
/// // Compile-only example
/// fn example() {
///     let ctx = RootContext::init_local();
///     let actor = ctx.spawn(MyActor { value: 0 });
///     ctx.bind("my_actor", actor);
/// }
/// ```
#[derive(Debug, Clone)]
pub struct RootContext {
    pub(crate) this_hdl: ActorHdl,
    pub(crate) child_hdls: Arc<Mutex<Vec<WeakActorHdl>>>,
}

impl<A: Actor> Context<A> {
    /// Get the unique identifier of this actor.
    ///
    /// # Return
    ///
    /// `ActorId` uniquely identifying this actor instance.
    pub fn id(&self) -> ActorId {
        self.this_hdl.id()
    }

    /// Spawn a new child actor with the given arguments.
    ///
    /// # Arguments
    ///
    /// * `args` - Initialization arguments for the new actor
    ///
    /// # Return
    ///
    /// `ActorRef<Args::Actor>` reference to the newly spawned actor.
    ///
    /// # Panics
    ///
    /// Panics if the internal child handle lock is poisoned.
    pub fn spawn<Args: ActorArgs>(&self, args: Args) -> ActorRef<Args::Actor> {
        let (hdl, actor) = spawn_impl(&self.this_hdl, args);

        self.child_hdls.lock().unwrap().push(hdl.downgrade());

        actor
    }

    /// Terminate this actor and all its children.
    ///
    /// # Panics
    ///
    /// Panics if the signal channel is unexpectedly closed.
    pub async fn terminate(&self) {
        let (tx, rx) = oneshot::channel();

        self.this_hdl
            .raw_send(RawSignal::Terminate(Some(tx)))
            .unwrap();

        let _ = rx.await;
    }
}

impl RootContext {
    /// Initialize the root context with remote capabilities.
    ///
    /// # Arguments
    ///
    /// * `endpoint` - The iroh network endpoint for remote communication
    ///
    /// # Return
    ///
    /// `RootContext` with remote communication enabled.
    #[cfg(feature = "remote")]
    pub fn init(endpoint: Endpoint) -> Self {
        LocalPeer::init(endpoint);

        Self::init_local()
    }

    /// Initialize a local-only root context.
    ///
    /// # Return
    ///
    /// `RootContext` for local actor system operations.
    pub fn init_local() -> Self {
        Self::default()
    }

    /// Get the public key of this node for remote communication.
    ///
    /// # Return
    ///
    /// `PublicKey` for this node's identity in remote operations.
    #[cfg(feature = "remote")]
    pub fn public_key(&self) -> PublicKey {
        LocalPeer::inst().public_key()
    }

    /// Get a reference to the underlying iroh [`Endpoint`].
    ///
    /// This provides access to iroh's networking layer, useful for:
    /// - Checking connection type via [`Endpoint::remote_info`]
    /// - Monitoring path changes on connections
    /// - Retrieving the endpoint's address with [`Endpoint::addr`]
    ///
    /// [`Endpoint`]: iroh::Endpoint
    /// [`Endpoint::remote_info`]: iroh::Endpoint::remote_info
    /// [`Endpoint::addr`]: iroh::Endpoint::addr
    #[cfg(feature = "remote")]
    pub fn endpoint(&self) -> &Endpoint {
        LocalPeer::inst().endpoint()
    }

    /// Spawn a new top-level actor.
    ///
    /// # Arguments
    ///
    /// * `args` - The initialization arguments for the new actor
    ///
    /// # Return
    ///
    /// `ActorRef<Args::Actor>` reference to the newly spawned actor.
    ///
    /// # Panics
    ///
    /// Panics if the internal child handle lock is poisoned.
    pub fn spawn<Args: ActorArgs>(&self, args: Args) -> ActorRef<Args::Actor> {
        let (actor_hdl, actor) = spawn_impl(&self.this_hdl, args);

        self.child_hdls.lock().unwrap().push(actor_hdl.downgrade());

        actor
    }

    /// Bind an actor to a global identifier for lookup.
    ///
    /// # Arguments
    ///
    /// * `ident` - A name to bind the actor to (max 16 bytes)
    /// * `actor` - The actor reference to bind
    ///
    /// # Errors
    ///
    /// Returns `BindingError` if the identifier is invalid.
    ///
    /// # Panics
    /// * If the identifier length exceeds 16 bytes
    pub fn bind<A: Actor>(
        &self,
        ident: impl AsRef<str>,
        actor: ActorRef<A>,
    ) -> Result<(), BindingError> {
        let ident = parse_ident(ident.as_ref())?;

        trace!(ident = % Hex(&ident), % actor, "binding");
        Self::bind_impl(ident, actor);

        Ok(())
    }

    /// Remove an actor binding from the global registry.
    ///
    /// # Arguments
    ///
    /// * `ident` - The identifier to remove from the registry
    ///
    /// # Return
    ///
    /// `Option<Arc<dyn AnyActorRef>>` - The removed binding if it existed.
    pub fn free(&self, ident: &[u8]) -> Option<Arc<dyn AnyActorRef>> {
        if ident.len() > 16 {
            return None;
        }

        let mut normalized: Ident = [0u8; 16];

        normalized[..ident.len()].copy_from_slice(ident);

        Self::free_impl(&normalized)
    }

    /// Terminate the root context and all spawned actors.
    ///
    /// Initiates system-wide shutdown and waits for all actors to terminate.
    ///
    /// # Panics
    ///
    /// Panics if the signal channel is unexpectedly closed.
    ///
    /// # Note
    ///
    /// TODO: Make it support timeout for graceful shutdown with fallback to forced termination.
    pub async fn terminate(&self) {
        let (tx, rx) = oneshot::channel();

        self.this_hdl
            .raw_send(RawSignal::Terminate(Some(tx)))
            .unwrap();

        let _ = rx.await;
    }

    #[allow(dead_code)]
    pub(crate) fn is_bound_impl<A: Actor>(ident: &[u8; 16]) -> bool {
        BINDINGS
            .with(ident, |actor| {
                actor.as_any().is_some_and(|a| a.is::<ActorRef<A>>())
            })
            .unwrap_or(false)
    }

    pub(crate) fn bind_impl<A: AnyActorRef>(ident: Ident, actor: A) {
        let _ = BINDINGS.insert(ident, Arc::new(actor));
    }

    #[cfg(feature = "remote")]
    pub(crate) fn lookup_any_local_impl(
        actor_ty_id: ActorTypeId,
        ident: &[u8; 16],
    ) -> Result<Arc<dyn AnyActorRef>, BindingError> {
        match Self::lookup_any_local_unchecked_impl(ident) {
            Err(err) => Err(err),
            Ok(actor) => {
                if actor.ty_id() == actor_ty_id {
                    Ok(actor)
                } else {
                    Err(BindingError::TypeMismatch)
                }
            }
        }
    }

    #[cfg(feature = "remote")]
    pub(crate) fn lookup_any_local_unchecked_impl(
        ident: &[u8; 16],
    ) -> Result<Arc<dyn AnyActorRef>, BindingError> {
        BINDINGS
            .with(ident, std::clone::Clone::clone)
            .ok_or(BindingError::NotFound)
    }

    pub(crate) fn free_impl(ident: &[u8; 16]) -> Option<Arc<dyn AnyActorRef>> {
        BINDINGS.remove(ident).map(|(_, v)| v)
    }
}

impl Default for RootContext {
    fn default() -> Self {
        let (sig_tx, sig_rx) = unbounded_with_id(Uuid::new_v4());
        let this_hdl = ActorHdl(sig_tx);
        let child_hdls = Arc::new(Mutex::new(Vec::<WeakActorHdl>::new()));

        compat::spawn({
            let child_hdls = child_hdls.clone();

            async move {
                while let Some(sig) = sig_rx.recv().await {
                    match sig {
                        RawSignal::Escalation(child_hdl, esc) => {
                            error!(child = % child_hdl.id(), ? esc, "root received escalation");

                            if let Err(e) = child_hdl.raw_send(RawSignal::Terminate(None)) {
                                error!(?e, "failed to send terminate to escalating child");
                            }
                        }
                        RawSignal::ChildDropped => {
                            let mut child_hdls = child_hdls.lock().unwrap();

                            child_hdls.retain(|hdl| match hdl.upgrade() {
                                None => false,
                                Some(hdl) => hdl.0.sender_count() > 0,
                            });
                        }
                        RawSignal::Terminate(k) => {
                            let alive_hdls: Vec<_> = child_hdls
                                .lock()
                                .unwrap()
                                .iter()
                                .filter_map(super::actor_ref::WeakActorHdl::upgrade)
                                .collect();
                            let child_rxs: Vec<_> = alive_hdls
                                .iter()
                                .filter_map(|hdl| {
                                    let (tx, rx) = oneshot::channel();

                                    match hdl.raw_send(RawSignal::Terminate(Some(tx))) {
                                        Ok(()) => Some(rx),
                                        Err(_) => None,
                                    }
                                })
                                .collect();

                            for rx in child_rxs {
                                let _ = rx.await;
                            }

                            if let Some(k) = k {
                                let _ = k.send(());
                            }
                        }
                        RawSignal::Pause(k) | RawSignal::Resume(k) | RawSignal::Restart(k) => {
                            if let Some(k) = k {
                                let _ = k.send(());
                            }
                        }
                        #[cfg(feature = "monitor")]
                        RawSignal::Monitor(_) => {}
                    }
                }
            }
        });

        Self {
            this_hdl,
            child_hdls,
        }
    }
}

/// Create a new actor instance with auto-generated ID.
pub(crate) fn spawn_impl<Args: ActorArgs>(
    parent_hdl: &ActorHdl,
    args: Args,
) -> (ActorHdl, ActorRef<Args::Actor>) {
    spawn_with_id_impl(Uuid::new_v4(), parent_hdl, args)
}

/// Create a new actor instance with specified ID.
pub(crate) fn spawn_with_id_impl<Args: ActorArgs>(
    actor_id: ActorId,
    parent_hdl: &ActorHdl,
    args: Args,
) -> (ActorHdl, ActorRef<Args::Actor>) {
    let (msg_tx, msg_rx) = unbounded_with_id(actor_id);
    let (sig_tx, sig_rx) = unbounded_with_id(actor_id);
    let actor_hdl = ActorHdl(sig_tx);
    let actor = ActorRef(msg_tx);
    #[cfg(feature = "monitor")]
    let _ = HDLS.insert(actor_id, actor_hdl.clone());

    compat::spawn({
        let actor = actor.downgrade();
        let parent_hdl = parent_hdl.clone();
        let actor_hdl = actor_hdl.clone();

        async move {
            let config = ActorConfig::new(actor, parent_hdl, actor_hdl, sig_rx, msg_rx, args);

            config.exec().await;

            #[cfg(feature = "monitor")]
            let _ = HDLS.remove(&actor_id);
        }
    });

    (actor_hdl, actor)
}