forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
5216 lines (4932 loc) · 222 KB
/
Copy pathmod.rs
File metadata and controls
5216 lines (4932 loc) · 222 KB
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Actor runtime system for Nulang.
//!
//! Provides: actor lifecycle, scheduler, mailbox, heap, GC, supervision,
//! distribution.
use std::collections::{HashMap, HashSet};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::mpsc;
use std::sync::Arc;
use std::time::Instant;
use tracing::warn;
mod actor;
mod gc;
pub mod heap;
pub(crate) mod heap_serialize;
mod mailbox;
mod scheduler;
pub use heap_serialize::*;
mod cluster;
mod distributed;
mod distributed_context;
mod network;
mod orca_cycle;
mod supervision;
mod supervisor;
use distributed_context::DistributedContext;
#[cfg(feature = "ai-runtime")]
mod agent;
#[cfg(feature = "ai-runtime")]
mod ai_impls;
pub(crate) mod callbacks;
pub mod crdt;
pub mod crdt_manager;
pub mod crdt_reg;
mod distribution;
mod exit;
mod http_server;
#[cfg(feature = "ai-runtime")]
mod llm;
mod metrics;
mod persistence;
mod process_groups;
mod registry;
mod spawn;
mod timer;
mod trace;
mod workflow;
pub use trace::TraceContext;
#[cfg(test)]
mod cluster_sim;
#[cfg(test)]
mod tests;
pub use actor::*;
pub use callbacks::RuntimeVmCallbacks;
pub(crate) use callbacks::{BytecodeDistributedCallbacks, BytecodeRuntimeCallbacks};
pub use cluster::*;
pub use crdt::*;
pub use crdt_manager::*;
pub use crdt_reg::{LWWRegister, MVRegister, RGAElement, RGA};
pub use distributed::*;
pub use gc::{ForeignRefOp, GcStats, OrcaCoordinator, OrcaGc, OrcaHeap};
pub use heap::*;
pub use http_server::HttpServerState;
pub use mailbox::*;
pub use network::NetworkTransport;
pub use network::*;
pub use orca_cycle::*;
pub use persistence::*;
pub use process_groups::*;
pub use registry::*;
pub use scheduler::*;
pub use supervisor::*;
pub use timer::*;
use crate::types::{ExitReason, VmSuspension};
use crate::vm::Value;
#[cfg(feature = "ai-runtime")]
use nulang_ai::{
AiRuntimeRegistry, LlmClient, LlmError, LlmMessage, LlmRequest, LlmResponse,
SupervisorTeamRegistry,
};
// ---------------------------------------------------------------------------
// Global actor ID generator
// ---------------------------------------------------------------------------
static ACTOR_ID_COUNTER: AtomicU64 = AtomicU64::new(1);
/// Generate a fresh, globally unique actor ID.
pub fn fresh_actor_id() -> u64 {
ACTOR_ID_COUNTER.fetch_add(1, Ordering::Relaxed)
}
/// Sentinel actor id for `Runtime::main_heap`/`main_gc`, the fallback used
/// for allocation outside any real actor's behavior. Below
/// `ACTOR_ID_COUNTER`'s start value of 1, so it can never collide with a
/// real `fresh_actor_id()` result.
const MAIN_HEAP_ACTOR_ID: u64 = 0;
/// Maximum number of membership entries carried by a single gossip packet.
const GOSSIP_PAYLOAD_MAX_ENTRIES: usize = 256;
/// Native handler for durable workflow timer-fired messages.
///
/// Advances the workflow's step_index so the workflow can proceed past the
/// step that was waiting on the timer.
fn timer_fired_handler(actor: &mut Actor, _args: &[Value]) {
if let Some(n) = actor.get_state_field("step_index").and_then(|v| v.as_int()) {
actor.set_state_field("step_index", Value::int(n + 1));
}
}
/// Placeholder native handler for bytecode workflow steps.
///
/// Workflow steps are dispatched via `bytecode_offsets`, but the behavior-id
/// space is shared with native handlers. Empty-name placeholders reserve the
/// step ids so internal runtime behaviors (e.g. `__timer_fired`) can live at
/// higher indices without colliding.
fn bytecode_step_placeholder(_actor: &mut Actor, _args: &[Value]) {}
/// Persisted `waiting_signal` marker for a workflow step suspended on a
/// background LLM call. A signal wait stores the awaited signal's name so
/// recovery can re-trigger the in-flight step; an LLM suspend has no
/// signal, so this reserved marker plays the same role. The suspended VM
/// state itself cannot be persisted, so recovery re-runs the step from
/// its last pre-suspend checkpoint and the re-executed `LLM.ask` starts
/// a fresh background call.
const LLM_SUSPEND_MARKER: &str = "__llm_ask_pending__";
/// Choose the `waiting_signal` value for a freshly captured suspension:
/// the awaited signal's name for a signal wait, or the reserved LLM
/// marker for a workflow step suspended on a background LLM call (plain
/// actors store nothing; their suspensions are not re-driven on
/// recovery).
fn suspension_marker(actor: &Actor, signal_name: Option<String>) -> Option<String> {
match signal_name {
Some(name) => Some(name),
None if actor.is_workflow => Some(LLM_SUSPEND_MARKER.to_string()),
None => None,
}
}
/// Map the argument of `perform Actor.exit(reason)` onto an `ExitReason`.
/// Ints and strings select the reason kind (`0`/`"normal"`, `1`/`"error"`,
/// `2`/`"kill"`); any other value is a custom reason, and a missing or
/// non-int/non-string argument defaults to a normal exit.
fn actor_exit_reason(value: Option<&Value>, constants: &[crate::bytecode::Constant]) -> ExitReason {
let Some(value) = value else {
return ExitReason::Normal;
};
if let Some(n) = value.as_int() {
return match n {
0 => ExitReason::Normal,
1 => ExitReason::Error("error".to_string()),
2 => ExitReason::Kill,
other => ExitReason::Custom(other.to_string()),
};
}
if let Some(id) = value.as_string_id() {
let name = match constants.get(id as usize) {
Some(crate::bytecode::Constant::String(s)) => s.as_str(),
_ => "",
};
return match name {
"normal" => ExitReason::Normal,
"error" => ExitReason::Error("error".to_string()),
"kill" => ExitReason::Kill,
other => ExitReason::Custom(other.to_string()),
};
}
ExitReason::Normal
}
// ---------------------------------------------------------------------------
// Runtime
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Cross-shard message type for multi-threaded scheduler
// ---------------------------------------------------------------------------
/// A message routed between Runtime shards in a multi-threaded deployment.
///
/// Each shard owns a disjoint subset of actors (by `actor_id % shard_count`).
/// Cross-shard messages carry only value-type payloads (ints, strings, bools,
/// unit, nil) - heap pointers are stripped before sending, matching the
/// network wire-protocol restriction. This keeps ORCA reference counting
/// local to each shard.
#[derive(Debug)]
enum CrossShardMsg {
/// Deliver a message to an actor on the target shard.
DeliverMessage {
target_id: u64,
behavior_id: u16,
payload: Vec<Value>,
sender: u64,
trace_id: Option<String>,
},
/// Enqueue an actor on the target shard (wake from idle/waiting).
EnqueueActor {
actor_id: u64,
priority: ActorPriority,
},
}
pub struct Runtime {
pub actors: HashMap<u64, Actor>,
pub supervisors: HashMap<u64, Supervisor>,
pub scheduler: Scheduler,
pub current_actor: Option<u64>,
/// W3C trace context of the message currently being handled on this
/// shard's scheduler thread. Sends performed while handling a message
/// stamp their outgoing `traceparent` as a child of this context, so
/// causal chains span actor, shard, and node boundaries.
pub current_trace: Option<TraceContext>,
// Fallback heap/GC for allocation performed OUTSIDE any actor's
// behavior (e.g. `main()`'s own top-level bytecode: string
// concatenation, `Int.to_string`, and similar). See
// `RuntimeVmCallbacks::alloc`'s doc comment for why this exists.
pub main_heap: ActorHeap,
pub main_gc: OrcaGc,
pub next_reductions: u32,
pub coordinator: OrcaCoordinator,
pub cycle_detector: CycleDetector,
// Heaps of exited actors that still have outstanding foreign
// references. Dropping a heap while another actor holds a pointer
// into it would dangle, so such heaps are retired here instead and
// reclaimed by `reclaim_retired_heaps` once every foreign reference
// (in-flight op or receiver hold) has drained.
retired_heaps: Vec<ActorHeap>,
// Distributed actor system (v0.5)
pub distributed: DistributedContext,
// Operator cluster configuration (split-brain resolver, probe interval),
// applied when distribution is enabled.
pub cluster_config: ClusterConfig,
// Acknowledged packet sequence numbers (transport-level reliability).
pub acked_packets: HashSet<u64>,
// Cross-node supervision (RFC 0012)
pub remote_links: supervision::RemoteLinkRegistry,
pub remote_monitors: supervision::RemoteMonitorRegistry,
/// Actors that have migrated to another node. Key is the local
/// actor id (the id they had here before migrating); value is
/// `(target_node, migrated_at)`. `send_message_by_id` checks this
/// table and forwards messages to the new location. Entries are
/// garbage-collected after `MIGRATED_ACTOR_TTL` seconds.
pub migrated_actors: HashMap<u64, (NodeId, Instant)>,
// CRDT manager (v0.6)
pub crdt_manager: Option<CrdtManager>,
// Number of `sync_crdts` calls made; delta-state syncs run on most
// rounds, with a full-state repair sync every CRDT_FULL_SYNC_INTERVAL.
pub(crate) crdt_sync_rounds: u64,
// Timer wheel (v0.7)
pub timer_wheel: TimerWheel,
// Virtual clock for deterministic testing (v0.14). When set, all timer
// expiry and deadline calculations use this clock instead of wall time.
pub virtual_clock: Option<VirtualClock>,
/// Prometheus-format metrics server (background TCP listener).
/// Started via [`Runtime::enable_metrics_server`]; periodically
/// updated via [`Runtime::publish_metrics`].
pub metrics: Option<metrics::MetricsServer>,
/// Python foreign-interop bridge (feature `python`). Lazily initialised
/// on first `Python.*` builtin effect invocation.
#[cfg(feature = "python")]
pub foreign_interop: Option<Box<dyn crate::backends::ForeignInterop>>,
// LLM subsystem (v0.9 AI Runtime): client, worker thread, token budget,
// completion channel, and non-blocking suspension state.
#[cfg(feature = "ai-runtime")]
pub llm: llm::LlmState,
// Actor name registry (v0.7)
pub registry: ActorRegistry,
// Process groups (v0.7)
pub process_groups: ProcessGroups,
// Persistence engine (v0.7)
pub persistence: Box<dyn PersistenceStore>,
// VM used to execute bytecode behavior handlers.
vm: Option<crate::vm::VM>,
// Depth of in-flight calls on the shared runtime VM
// (`run_bytecode_at_offset`, `resume_suspended_*`). While > 0 a
// behavior is mid-execution, so receive-wait wakes requested by
// `send_message_by_id` must be deferred: resuming the target would
// nest a second `vm.resume()`/`run_from` inside the running one and
// clobber the shared frames.
vm_execution_depth: u32,
/// True while executing a scheduler-driven bytecode behavior, enabling
/// non-blocking suspension on `perform LLM.ask` and on `receive ...
/// after ms =>` timed waits. Nested synchronous entry points
/// (`ask_actor_sync`: pipelines, supervisors, debates) force it back
/// to false so they keep blocking behavior. Not LLM-specific - lives
/// on `Runtime` directly (not `LlmState`) because core receive-wait
/// suspension depends on it regardless of the `ai-runtime` feature.
pub(crate) suspend_enabled: bool,
// Actors whose receive-wait wake was deferred while the shared VM was
// executing (deduplicated). Drained by `vm_exec_end` once the
// outermost VM call returns; a resumed behavior can itself send and
// re-queue a wake, so the drain loops until empty.
pending_receive_wakes: Vec<u64>,
// True while `vm_exec_end` is draining `pending_receive_wakes`. Nested
// `vm_exec_end` calls (from resumes issued by the drain) then skip
// their own drain, so the backlog is processed iteratively instead of
// by unbounded recursion.
draining_receive_wakes: bool,
// Bytecode modules for actors that may need to be recovered after a
// runtime restart. Maps actor_id -> (bytecode_module, behavior_offsets,
// compensation_offsets).
pub(crate) recovery_modules:
HashMap<u64, (crate::bytecode::CodeModule, Vec<usize>, Vec<Option<usize>>)>,
/// Content-addressed bytecode cache for fetch-on-demand.
/// When a node receives a message for an unknown content hash, it can
/// request the bytecode from the sender and cache it here keyed by hash.
pub behavior_cache: HashMap<[u8; 32], crate::bytecode::CodeModule>,
/// Messages pending retry after a bytecode fetch completes.
/// Keyed by content hash; drained when the matching FetchBehaviorResponse
/// arrives and the module is cached.
pub(crate) pending_fetched_messages:
HashMap<[u8; 32], Vec<(u64, String, Message, Vec<String>)>>,
// Pipelines and debates (v0.9 AI Runtime) - extracted into a registry so
// the god-object shrinks and the subsystems can evolve independently.
#[cfg(feature = "ai-runtime")]
pub ai: AiRuntimeRegistry,
// Supervisor teams (v0.9 AI Runtime) - extracted into a registry so the
// god-object shrinks and the subsystem can evolve independently.
#[cfg(feature = "ai-runtime")]
pub supervisor_teams: SupervisorTeamRegistry,
// Remote spawn support (v0.5+): behaviors a remote node may spawn here
// by name (see `register_spawnable_behavior`), plus the results of
// spawn requests WE issued, keyed by request id
// (`Some(actor_id)` = spawned, `None` = rejected).
pub spawnable_behaviors: HashMap<String, fn(&mut Actor, &[Value])>,
pub pending_spawn_responses: HashMap<u64, Option<u64>>,
/// Bare actor id → hosting node for every remote actor this runtime
/// can address BY VALUE (RFC-0007 cross-node routing): spawn@node
/// placeholders (request id → target node, recorded at spawn time)
/// and inbound senders recorded for reply-by-ref. Actor-ref Values
/// carry only a 48-bit id — no node — so `send`/`ask` on a bare ref
/// consult this index to decide wire vs local routing. Scheduler-
/// thread confined like the rest of the distributed state. Bounded
/// at `REMOTE_REFS_MAX`; when full, new entries are dropped (the
/// forward `RemoteActorCache` still covers explicit
/// `ActorAddress::remote` sends).
pub(crate) remote_refs: HashMap<u64, NodeId>,
/// Messages sent to a spawn@node placeholder before its SpawnResponse
/// arrived, pre-resolved to wire form (string payloads rewritten to
/// table indices + contents captured in `string_table`) so the flush
/// on SpawnResponse doesn't need the sender's module-pool context.
pub(crate) pending_spawn_messages: HashMap<u64, Vec<distribution::PendingSpawnMessage>>,
/// Value ids that are spawn@node PLACEHOLDERS (request ids) whose
/// SpawnResponse has not arrived. Distinct from `remote_refs`: a
/// placeholder must QUEUE messages until its real actor id is known,
/// while an ordinary remote ref (inbound sender, real spawned id)
/// sends directly. Removed on SpawnResponse (success or failure).
pub(crate) spawn_placeholders: HashSet<u64>,
/// Placeholder VALUE id → real remote actor id, recorded on a
/// successful SpawnResponse. Independent of `pending_spawn_responses`
/// (consumed by `take_spawn_response`): the placeholder ref the
/// program holds must keep routing to the real actor even after the
/// response was observed (or not) by application code.
pub(crate) spawn_translations: HashMap<u64, u64>,
/// AOT-compiled modules registered for native behavior dispatch, keyed by
/// actor type name → module pointer. Ownership lives in
/// `aot_module_storage`; the pointers are stable (each module is Boxed).
pub aot_modules: std::collections::HashMap<String, *const crate::aot::AotModule>,
/// Owns the registered AOT modules so the raw pointers in `aot_modules`
/// (and on actors) stay valid for the Runtime's lifetime.
pub aot_module_storage: Vec<Box<crate::aot::AotModule>>,
/// Actor ID of the dead-letter queue (created lazily).
/// Undeliverable messages are routed here.
pub dlq_actor_id: Option<u64>,
/// Callback invoked when the scheduler loop reaches true quiescence
/// (empty run queue, no inflight LLM calls, no pending timers).
/// The embedder (e.g. NLC guest agent) wires this to host signaling.
pub idle_callback: Option<Box<dyn FnMut()>>,
// Test effect handlers - installed via `install_test_handler` to
// intercept `perform Effect.op` calls in tests. Key is the qualified
// name (e.g. "IO.print", "DB.write"). A handler returns `Some(value)`
// to mock the effect or `None` to fall through to real dispatch.
// HTTP server state (v0.7+).
pub http_server: Option<HttpServerState>,
pub test_handlers: HashMap<String, Box<dyn Fn(&[Value]) -> Option<Value>>>,
/// Cryptographic provider (hashing, random, signing).
/// Defaults to [`crate::backends::DefaultCryptoProvider`].
pub crypto: Box<dyn crate::backends::CryptoProvider>,
/// HTTP provider for outbound requests (health checks, webhooks, etc.).
/// Defaults to [`crate::backends::ReqwestHttpProvider`].
#[cfg(any(feature = "ai-runtime", feature = "http-client"))]
pub http: Box<dyn crate::backends::HttpProvider>,
/// TLS provider for network encryption.
/// Defaults to [`crate::backends::DefaultTlsProvider`] when TLS feature is enabled.
#[cfg(feature = "tls")]
pub tls_provider: Box<dyn crate::backends::TlsProvider>,
// -- Multi-threaded scheduler sharding --
/// This shard's index (0-based). Always 0 for a single-shard runtime.
pub shard_idx: u16,
/// Total number of shards. Always 1 for a single-shard runtime.
pub shard_count: u16,
/// Channels to send messages to every shard (including self - unused).
/// `None` when `shard_count == 1` (single-shard, no cross-shard routing).
cross_shard_tx: Option<Vec<mpsc::SyncSender<CrossShardMsg>>>,
/// Channel to receive messages for this shard.
/// `None` when `shard_count == 1`.
cross_shard_rx: Option<mpsc::Receiver<CrossShardMsg>>,
}
// SAFETY: in sharded mode each Runtime runs on exactly one thread (shard
// ownership by actor_id % shard_count). Cross-shard communication uses
// mpsc channels; no two threads access the same Runtime's internal state.
// The contained VM, callback trait objects, and raw ORCA pointers are all
// thread-confined.
unsafe impl Send for Runtime {}
/// Outcome of `Runtime::run_scheduler_deterministic`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeterministicRunResult {
/// No actor has a non-empty mailbox; the run completed normally.
Quiescent { steps: u64 },
/// `max_steps` was reached with actors still having pending
/// messages -- a real invariant violation (deadlock/livelock),
/// since every step executed real actor code via `step_actor`, not
/// a simulated stand-in.
StepLimitExceeded { steps: u64 },
}
/// No-op foreign-interop bridge used when `DefaultForeignInterop` fails
/// to initialise (e.g. missing Python runtime). Every call returns an
/// error, causing `perform_python_builtin` to emit a nil result.
#[cfg(feature = "python")]
struct NoOpForeignInterop;
#[cfg(feature = "python")]
impl crate::backends::ForeignInterop for NoOpForeignInterop {
fn call(&mut self, _module: &str, _function: &str, _args: &[Value]) -> Result<Value, String> {
Err("Python bridge not available".to_string())
}
fn import(&mut self, _name: &str) -> Result<(), String> {
Err("Python bridge not available".to_string())
}
}
impl Runtime {
pub fn new() -> Self {
Runtime {
actors: HashMap::new(),
supervisors: HashMap::new(),
scheduler: Scheduler::new(4),
current_actor: None,
current_trace: None,
main_heap: {
let mut heap = ActorHeap::new(64 * 1024);
heap.set_actor_id(MAIN_HEAP_ACTOR_ID);
heap
},
main_gc: OrcaGc::new(MAIN_HEAP_ACTOR_ID),
next_reductions: 1000,
coordinator: OrcaCoordinator::new(),
cycle_detector: CycleDetector::new(),
vm_execution_depth: 0,
suspend_enabled: false,
retired_heaps: Vec::new(),
distributed: DistributedContext::new(),
cluster_config: ClusterConfig::default(),
acked_packets: HashSet::new(),
remote_links: supervision::RemoteLinkRegistry::new(),
remote_monitors: supervision::RemoteMonitorRegistry::new(),
migrated_actors: HashMap::new(),
crdt_manager: None,
virtual_clock: None,
metrics: None,
#[cfg(feature = "python")]
foreign_interop: None,
crdt_sync_rounds: 0,
timer_wheel: TimerWheel::new(),
registry: ActorRegistry::new(),
process_groups: ProcessGroups::new(),
pending_fetched_messages: HashMap::new(),
persistence: Box::new(MemoryStore::new()),
vm: None,
#[cfg(feature = "ai-runtime")]
llm: llm::LlmState::new(),
behavior_cache: HashMap::new(),
pending_receive_wakes: Vec::new(),
draining_receive_wakes: false,
idle_callback: None,
recovery_modules: HashMap::new(),
#[cfg(feature = "ai-runtime")]
ai: AiRuntimeRegistry::new(),
#[cfg(feature = "ai-runtime")]
supervisor_teams: SupervisorTeamRegistry::new(),
crypto: Box::new(crate::backends::DefaultCryptoProvider::new()),
spawnable_behaviors: HashMap::new(),
aot_modules: std::collections::HashMap::new(),
aot_module_storage: Vec::new(),
#[cfg(any(feature = "ai-runtime", feature = "http-client"))]
http: Box::new(crate::backends::ReqwestHttpProvider::new()),
#[cfg(feature = "tls")]
tls_provider: Box::new(crate::backends::DefaultTlsProvider::new()),
pending_spawn_responses: HashMap::new(),
remote_refs: HashMap::new(),
pending_spawn_messages: HashMap::new(),
spawn_placeholders: HashSet::new(),
spawn_translations: HashMap::new(),
dlq_actor_id: None,
http_server: None,
test_handlers: HashMap::new(),
shard_idx: 0,
shard_count: 1,
cross_shard_tx: None,
cross_shard_rx: None,
}
}
/// Compute the BLAKE3 hash of `data` using the configured [`CryptoProvider`].
pub fn hash_bytes(&self, data: &[u8]) -> [u8; 32] {
self.crypto.hash(data)
}
/// Fill `buf` with cryptographically secure random bytes.
pub fn random_bytes(&self, buf: &mut [u8]) {
self.crypto.random_bytes(buf)
}
/// Make an HTTP POST request with a JSON body.
/// Delegates to the configured [`HttpProvider`](crate::backends::HttpProvider).
#[cfg(any(feature = "ai-runtime", feature = "http-client"))]
pub fn http_post_json(&self, url: &str, body: &str) -> Result<String, String> {
self.http.post_json(url, body)
}
/// Make an HTTP GET request.
/// Delegates to the configured [`HttpProvider`](crate::backends::HttpProvider).
#[cfg(any(feature = "ai-runtime", feature = "http-client"))]
pub fn http_get(&self, url: &str) -> Result<String, String> {
self.http.get(url)
}
/// Create a shard Runtime. Private - use [`Runtime::new_sharded`] to
/// create a set of N shards with cross-shard channels wired.
fn new_shard(
shard_idx: u16,
shard_count: u16,
cross_shard_tx: Vec<mpsc::SyncSender<CrossShardMsg>>,
cross_shard_rx: mpsc::Receiver<CrossShardMsg>,
) -> Self {
let mut rt = Runtime::new();
rt.shard_idx = shard_idx;
rt.shard_count = shard_count;
rt.cross_shard_tx = Some(cross_shard_tx);
rt.cross_shard_rx = Some(cross_shard_rx);
// Only shard 0 binds network transport; others skip it.
if shard_idx != 0 {
rt.distributed.enabled = false;
}
rt
}
/// Create `num_shards` Runtime instances, each wired with cross-shard
/// channels. Returns a `Vec` of Runtimes ready to `run_scheduler()` in
/// their own threads. Shard 0 owns network binding and cycle detection;
/// all shards process their local actors independently.
///
/// Actor assignment: `actor_id % num_shards` determines the owning shard.
/// Cross-shard messages carry only value types (no heap pointers), keeping
/// ORCA reference counting local to each shard.
pub fn new_sharded(num_shards: usize) -> Vec<Runtime> {
assert!(num_shards > 0, "num_shards must be >= 1");
if num_shards == 1 {
return vec![Runtime::new()];
}
// Create a sync_channel per shard (bounded, 1024 messages deep).
let channels: Vec<(
mpsc::SyncSender<CrossShardMsg>,
mpsc::Receiver<CrossShardMsg>,
)> = (0..num_shards).map(|_| mpsc::sync_channel(1024)).collect();
let senders: Vec<mpsc::SyncSender<CrossShardMsg>> =
channels.iter().map(|(tx, _)| tx.clone()).collect();
let mut shards = Vec::with_capacity(num_shards);
for (i, (_tx, rx)) in channels.into_iter().enumerate() {
shards.push(Runtime::new_shard(
i as u16,
num_shards as u16,
senders.clone(),
rx,
));
}
shards
}
/// Install a test handler that intercepts `perform Effect.op` calls.
///
/// The `effect_name` should be the qualified operation name (e.g.
/// `"IO.print"`, `"DB.write"`). The handler receives the frame
/// registers (r0..rn as set up by the compiler before `Perform`) and
/// returns `Some(value)` to mock the effect or `None` to fall through
/// to real dispatch.
///
/// # Example
/// ```ignore
/// rt.install_test_handler("DB.write", |regs| {
/// // regs[0] = key, regs[1] = value
/// Some(Value::unit()) // pretend write succeeded
pub fn install_test_handler<F>(&mut self, effect_name: &str, handler: F)
where
F: Fn(&[Value]) -> Option<Value> + 'static,
{
self.test_handlers
.insert(effect_name.to_string(), Box::new(handler));
}
/// Check whether a test handler is installed for `qualified_name` and
/// return its result if so.
pub fn check_test_handler(&self, qualified_name: &str, regs: &[Value]) -> Option<Value> {
self.test_handlers
.get(qualified_name)
.and_then(|handler| handler(regs))
}
#[tracing::instrument(level = "trace", skip(self, init))]
pub fn spawn_actor(&mut self, init: Box<dyn FnOnce() -> Vec<(String, Value)>>) -> u64 {
spawn::spawn_actor_with_models(self, init, HashMap::new(), false, None)
}
pub fn spawn_persistent_actor(
&mut self,
init: Box<dyn FnOnce() -> Vec<(String, Value)>>,
state_models: HashMap<String, StateModel>,
) -> u64 {
spawn::spawn_actor_with_models(self, init, state_models, true, None)
}
/// Spawn a durable workflow actor. Workflows are always persistent and
/// keep an append-only event journal in addition to snapshots.
pub fn spawn_workflow_actor(
&mut self,
name: &str,
init: Box<dyn FnOnce() -> Vec<(String, Value)>>,
state_models: HashMap<String, StateModel>,
) -> u64 {
spawn::spawn_actor_with_models(self, init, state_models, true, Some(name))
}
/// Spawn an actor for `module`'s behavior `behavior_idx`, seeded with
/// the `init` state fields, and wire up its bytecode handlers. Shared
/// body of both VM-callback `spawn_actor` impls: `RuntimeVmCallbacks`
/// (spawns from the top-level VM) and `BytecodeRuntimeCallbacks`
/// (spawns from inside a scheduler-driven behavior on the shared
/// runtime VM).
pub fn spawn_from_module(
&mut self,
module: &crate::bytecode::CodeModule,
behavior_idx: usize,
init: Vec<(String, Value)>,
) -> Value {
spawn::spawn_from_module(self, module, behavior_idx, init)
}
/// Register bytecode metadata so that a persistent actor can be recovered
/// after a runtime restart. The runtime stores the module, behavior
/// offsets, and saga compensation offsets; `recover_actor` will restore
/// them on the recreated actor.
pub fn register_recovery_module(
&mut self,
actor_id: u64,
module: crate::bytecode::CodeModule,
offsets: Vec<usize>,
compensation_offsets: Vec<Option<usize>>,
) {
spawn::register_recovery_module(self, actor_id, module, offsets, compensation_offsets)
}
/// Install an LLM client for `perform LLM.ask(...)` calls.
#[cfg(feature = "ai-runtime")]
pub fn set_llm_client(&mut self, client: Box<dyn LlmClient>) {
agent::set_llm_client(self, client)
}
/// Create a new empty pipeline and return its ID.
#[cfg(feature = "ai-runtime")]
pub fn pipeline_new(&mut self) -> u64 {
agent::pipeline_new(self)
}
/// Add a stage to an existing pipeline. Returns the same pipeline ID on
/// success so fluent construction can continue.
#[cfg(feature = "ai-runtime")]
pub fn pipeline_stage(
&mut self,
id: u64,
name: &str,
agent_id: u64,
template: &str,
) -> Result<u64, String> {
agent::pipeline_stage(self, id, name, agent_id, template)
}
/// Run a pipeline, returning the output of the final stage.
#[cfg(feature = "ai-runtime")]
pub fn pipeline_run(&mut self, id: u64, input: &str) -> Result<String, String> {
agent::pipeline_run(self, id, input)
}
#[cfg(feature = "ai-runtime")]
pub fn supervisor_new(&mut self) -> u64 {
agent::supervisor_new(self)
}
#[cfg(feature = "ai-runtime")]
pub fn supervisor_worker(
&mut self,
id: u64,
name: &str,
agent_id: u64,
description: &str,
) -> Result<u64, String> {
agent::supervisor_worker(self, id, name, agent_id, description)
}
#[cfg(feature = "ai-runtime")]
pub fn supervisor_run(&mut self, id: u64, task: &str) -> Result<String, String> {
agent::supervisor_run(self, id, task)
}
/// Create a new debate and return its ID.
#[cfg(feature = "ai-runtime")]
pub fn debate_new(&mut self, topic: &str, rounds: i64, threshold: f64) -> u64 {
agent::debate_new(self, topic, rounds, threshold)
}
/// Add a participant to an existing debate. Returns the same debate ID on
/// success so fluent construction can continue.
#[cfg(feature = "ai-runtime")]
pub fn debate_participant(
&mut self,
id: u64,
name: &str,
stance: &str,
agent_id: u64,
) -> Result<u64, String> {
agent::debate_participant(self, id, name, stance, agent_id)
}
/// Run a debate and return the moderator's synthesis.
#[cfg(feature = "ai-runtime")]
pub fn debate_run(&mut self, id: u64) -> Result<String, String> {
agent::debate_run(self, id)
}
/// Convert a VM value to a Rust string using the actor's bytecode module
/// constant pool for string-id values and reading pointer payloads as
/// null-terminated UTF-8.
#[cfg(feature = "ai-runtime")]
fn vm_value_to_string(
value: &crate::vm::Value,
module: Option<&crate::bytecode::CodeModule>,
) -> Option<String> {
agent::vm_value_to_string(value, module)
}
/// Execute an LLM request for an agent actor, reading the agent's model,
/// system prompt, and episodic memory from durable state. The memory is
/// updated with the user prompt and assistant response before being saved
/// back to state.
#[cfg(feature = "ai-runtime")]
pub fn complete_agent_llm(&mut self, actor_id: u64, prompt: &str) -> Option<String> {
agent::complete_agent_llm(self, actor_id, prompt)
}
/// Build a bare LLM request for a non-agent actor bytecode behavior,
/// with `tools` filled from the actor's bytecode module. Pure
/// read/build: safe to run before handing the request to a background
/// worker thread.
#[cfg(feature = "ai-runtime")]
fn build_actor_llm_request(
&self,
actor_id: u64,
model: &str,
prompt: &str,
) -> Option<LlmRequest> {
agent::build_actor_llm_request(self, actor_id, model, prompt)
}
/// Read an actor's state field as a plain string, resolving string-id
/// values through the runtime VM's constant pools (heap pointer values
/// are read directly). Useful for tests and tooling that inspect actor
/// state produced by bytecode behaviors.
#[cfg(feature = "ai-runtime")]
pub fn actor_state_string(&self, actor_id: u64, field: &str) -> Option<String> {
agent::actor_state_string(self, actor_id, field)
}
/// Set a token budget that caps total LLM token consumption.
///
/// After the budget is exhausted `complete_llm_request` returns
/// `LlmError::BudgetExceeded`. Charges are applied after each
/// successful response based on the actual token count returned
/// by the provider.
#[cfg(feature = "ai-runtime")]
pub fn set_token_budget(&mut self, limit: u64) {
agent::set_token_budget(self, limit)
}
/// Remove any configured token budget.
#[cfg(feature = "ai-runtime")]
pub fn clear_token_budget(&mut self) {
agent::clear_token_budget(self)
}
/// Execute a chat-completion request using the configured LLM client.
///
/// The provided `memory` messages are stored on the request before it is
/// sent to the provider.
#[cfg(feature = "ai-runtime")]
pub fn complete_llm_request(
&self,
request: LlmRequest,
memory: Vec<LlmMessage>,
) -> Result<LlmResponse, LlmError> {
agent::complete_llm_request(self, request, memory)
}
/// Execute an LLM request, optionally running tool calls from the response.
///
/// The request's `tools` list is populated from `module.tools`. If the
/// response contains tool calls, the named functions are looked up in the
/// module exports, invoked with the provided JSON arguments, and the results
/// are sent back to the model for a final response. The supplied `memory`
/// messages are preserved across tool-call rounds.
#[cfg(feature = "ai-runtime")]
pub fn complete_llm_with_tools(
&mut self,
request: LlmRequest,
memory: Vec<LlmMessage>,
module: &crate::bytecode::CodeModule,
) -> Result<LlmResponse, LlmError> {
agent::complete_llm_with_tools(self, request, memory, module)
}
/// Post-process an LLM response on the scheduler thread: invoke any tool
/// calls named in the response against `module` and synthesize the
/// response content from their results.
#[cfg(feature = "ai-runtime")]
pub(crate) fn finish_tool_calls(
&mut self,
module: &crate::bytecode::CodeModule,
response: LlmResponse,
) -> Result<LlmResponse, LlmError> {
agent::finish_tool_calls(self, module, response)
}
/// Record an emitted event on an actor. Delegates to the workflow subsystem.
pub fn emit_event(&mut self, actor_id: u64, event: &str, args: &[crate::vm::Value]) {
workflow::emit_event(self, actor_id, event, args)
}
/// Append a `TimerSet` workflow event and checkpoint the actor.
pub fn append_timer_set(
&mut self,
actor_id: u64,
name: &str,
duration_ms: u64,
) -> std::io::Result<()> {
workflow::append_timer_set(self, actor_id, name, duration_ms)
}
/// Append a `TimerFired` workflow event and checkpoint the actor.
pub fn append_timer_fired(&mut self, actor_id: u64, name: &str) -> std::io::Result<()> {
workflow::append_timer_fired(self, actor_id, name)
}
/// Append a `SignalReceived` workflow event and checkpoint the actor.
pub fn append_signal_received(
&mut self,
actor_id: u64,
name: &str,
payload: Option<String>,
) -> std::io::Result<()> {
workflow::append_signal_received(self, actor_id, name, payload)
}
/// Append a `SagaCompensated` workflow event and checkpoint the actor.
pub fn append_saga_compensated(
&mut self,
actor_id: u64,
step_name: &str,
) -> std::io::Result<()> {
workflow::append_saga_compensated(self, actor_id, step_name)
}
/// Send a named signal to a workflow actor.
///
/// The signal is appended to the durable workflow journal and, if the actor
/// is currently suspended waiting for this signal, its execution is resumed.
/// Deliver a signal to a workflow actor. Delegates to workflow subsystem.
pub fn signal_workflow(&mut self, actor_id: u64, name: &str, payload: Option<String>) {
workflow::signal_workflow(self, actor_id, name, payload)
}
/// Register a read-only query handler on a workflow actor.
///
/// The handler is a function/closure value invoked by `query_workflow`
/// with the workflow actor bound as `self`, so it can read the actor's
/// current state. Registration is a no-op for missing or non-workflow
/// actors: queries are a workflow-only concept. Handlers are not
/// journaled, so they must be re-registered after a node restart.
/// Register a read-only query handler on a workflow actor.
pub fn register_workflow_query(&mut self, actor_id: u64, name: &str, handler: Value) {
workflow::register_workflow_query(self, actor_id, name, handler)
}
/// Invoke a registered query handler on a workflow actor and return its
/// result. Returns `None` when the actor is missing, is not a workflow,
/// has no handler registered under `name`, or the handler value does not
/// resolve to a function in the actor's bytecode module.
///
/// Queries are read-only: unlike `signal_workflow` they append nothing
/// to the durable workflow journal, force no checkpoint, and never
/// resume a suspended step. The handler runs on a private VM with the
/// workflow actor bound as `self`, so a query performed from inside a
/// running behavior cannot disturb that behavior's frames; handlers
/// must therefore be immediate (non-capturing) functions, since closure
/// environments live on the VM that created them.
/// Invoke a registered query handler on a workflow actor. Delegates to workflow subsystem.
pub fn query_workflow(&mut self, actor_id: u64, name: &str) -> Option<Value> {
workflow::query_workflow(self, actor_id, name)
}
/// Drain completed background LLM calls and resume the suspended actors
/// waiting for them.
#[cfg(feature = "ai-runtime")]
pub(crate) fn poll_llm_completions(&mut self) {
llm::poll_llm_completions(self)
}
/// Record a completed background LLM call on its actor and resume the
/// actor's suspended behavior, if any. Errors trigger the retry/fallback
/// pipeline when the actor has a configured agent retry or fallback.
#[cfg(feature = "ai-runtime")]
pub(crate) fn store_llm_completion(
&mut self,
actor_id: u64,
result: Result<LlmResponse, LlmError>,
) {
llm::store_llm_completion(self, actor_id, result)
}
/// Re-dispatch an in-flight LLM request on retry timer fire.
#[cfg(feature = "ai-runtime")]
pub(crate) fn handle_llm_retry_timer(&mut self, actor_id: u64) {
llm::handle_llm_retry_timer(self, actor_id)
}
/// Send an LLM request to the persistent worker thread for execution.
/// Returns true if the request was dispatched, false if the worker
/// channel is unavailable (caller should roll back in-flight state).
#[cfg(feature = "ai-runtime")]
pub(crate) fn dispatch_llm_request(
&mut self,
actor_id: u64,
request: LlmRequest,
prompt: &str,
) -> bool {
llm::dispatch_llm_request(self, actor_id, request, prompt)
}
/// Re-enqueue an actor whose suspension has resolved if messages queued
/// up while it was suspended. step_actor refuses to run new messages
/// while a suspension is live, so without this the queued mail would
/// sit until an unrelated send happened to re-enqueue the actor.
fn requeue_if_mail_pending(&mut self, actor_id: u64) {
let needs_requeue = self
.actors
.get(&actor_id)
.map(|a| a.suspended_execution.is_none() && !a.mailbox.is_empty())
.unwrap_or(false);
if needs_requeue {
self.enqueue_actor(actor_id);
}
}
/// Resume an actor that yielded at a JIT safepoint.
///
/// Mirrors the structure of `resume_suspended_llm_step` but without