actr-framework 0.3.0

Actor-RTC framework core (stub for code generation testing)
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
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
//! Guest-side runtime module.
//!
//! Provides the unified [`entry!`] macro and platform-specific runtime
//! glue. Actor developers write one `entry!(MyActor)`; the macro selects
//! the correct ABI at compile time based on the target.
//!
//! # Execution contract
//!
//! - One loaded guest instance corresponds to one logical actor instance.
//! - The runtime serialises dispatch into the guest instance. Concurrent
//!   dispatches within the same instance are forbidden by the host
//!   (wasmtime enforces this via `&mut Store<HostState>`; dynclib hosts
//!   enforce via handle ownership).
//!
//! # Supported platforms
//!
//! - **WASM Component Model** (`target_arch = "wasm32"`, no `web` feature):
//!   wit-bindgen generates the `Guest` trait + `host` imports from
//!   `core/framework/wit/actr-workload.wit`; the [`entry!`] macro
//!   produces an adapter that bridges the user's [`Workload`] impl into
//!   the generated `Guest`. Targets `wasm32-wasip2` and requires
//!   `wasm-component-ld 0.5.22+` as the linker (see
//!   `experiments/component-spike-async/REPORT.md`).
//! - **Web ABI / wasm-bindgen** (`target_arch = "wasm32"` + `feature = "web"`):
//!   expands to a [`wasm_bindgen(start)`][wbgstart] bootstrap that wraps the
//!   user [`Workload`] in `web::WebWorkloadAdapter` and hands it to
//!   `actr_web_abi::host::register_workload`. Per Option U γ-unified §4.5
//!   the same user source compiles against both wasm32 ABIs; only the
//!   macro expansion differs. Targets `wasm32-unknown-unknown`.
//! - **cdylib** (`feature = "cdylib"`): HostVTable function-pointer
//!   bridge used for native shared-library guests (iOS / Android).
//!
//! [wbgstart]: https://rustwasm.github.io/wasm-bindgen/reference/attributes/on-rust-exports/start.html

pub mod dynclib_abi;
pub mod vtable;

// The Component Model wasm runtime glue is gated on `not(feature = "web")`
// so the `wasm32-unknown-unknown` + `web` target (which routes through
// `actr-web-abi` instead) does not link the wit-bindgen host imports that
// only resolve in a `wasm32-wasip2` Component environment.
#[cfg(all(target_arch = "wasm32", not(feature = "web")))]
pub mod wasm;

#[cfg(feature = "cdylib")]
pub mod dynclib;

// Re-exports used by the `entry!` macro so macro expansions under a
// user crate can reference adapter / binding items via a stable path
// without having to know the internal module layout.
#[cfg(all(target_arch = "wasm32", not(feature = "web")))]
#[doc(hidden)]
pub mod __wasm_macro_support {
    // The `entry!` macro expands inside the user's crate, so every name
    // it references must be reachable from an external crate path. Both
    // the adapter helpers and the generated WIT types are re-exported
    // here so the macro has a single stable `$crate::guest::__wasm_macro_support::*`
    // prefix to poke at.
    pub use super::wasm::adapter::{
        WorkloadCell, run_dispatch, run_on_credential_expiring, run_on_credential_renewed,
        run_on_data_stream, run_on_error, run_on_mailbox_backpressure, run_on_ready,
        run_on_signaling_connected, run_on_signaling_connecting, run_on_signaling_disconnected,
        run_on_start, run_on_stop, run_on_webrtc_connected, run_on_webrtc_connecting,
        run_on_webrtc_disconnected, run_on_websocket_connected, run_on_websocket_connecting,
        run_on_websocket_disconnected,
    };
    pub use super::wasm::generated::actr::workload::types::{
        ActrError as WitActrError, ActrId as WitActrId, BackpressureEvent as WitBackpressureEvent,
        CredentialEvent as WitCredentialEvent, DataStream as WitDataStream,
        ErrorEvent as WitErrorEvent, PeerEvent as WitPeerEvent, RpcEnvelope as WitRpcEnvelope,
    };
    pub use super::wasm::generated::exports::actr::workload::workload::Guest;
}

/// Generate Component Model exports for a [`Workload`][crate::Workload]
/// type.
///
/// Platform ABI is auto-selected by target:
///
/// - `#[cfg(all(target_arch = "wasm32", not(feature = "web")))]` — expands
///   to an `impl Guest for __ActrEntryAdapter { ... }` bridging the user's
///   [`Workload`][crate::Workload] into the `actr:workload/workload`
///   export contract. The runtime calls `Dispatcher::dispatch` through
///   the `dispatch` export and every observation hook through its
///   matching WIT export.
/// - `#[cfg(all(target_arch = "wasm32", feature = "web"))]` — expands to a
///   `#[wasm_bindgen(start)]` bootstrap that wraps the user workload in a
///   `WebWorkloadAdapter` and calls `actr_web_abi::host::register_workload`.
///   Only the 17 `#[wasm_bindgen]` entry points generated inside
///   `actr-web-abi::host` are exported to the Service Worker host.
/// - `#[cfg(feature = "cdylib")]` — expands to the legacy
///   `actr_init` / `actr_handle` / `actr_free_response` C-ABI exports
///   used by native shared-library hosts.
///
/// # Arguments
///
/// - `$workload_type`: type implementing
///   `actr_framework::Workload + Send + Sync + 'static`.
/// - `$init_expr` (optional): expression returning a fresh instance of
///   `$workload_type`. Defaults to `<$workload_type as Default>::default()`.
///
/// # Usage
///
/// ```rust,ignore
/// use actr_framework::entry;
///
/// entry!(EchoServiceWorkload<MyService>);
///
/// // Or with a custom constructor:
/// entry!(
///     EchoServiceWorkload<MyService>,
///     EchoServiceWorkload::new(MyService::new())
/// );
/// ```
#[macro_export]
macro_rules! entry {
    // Single-argument form: default-construct the workload.
    ($workload_type:ty) => {
        $crate::entry!($workload_type, <$workload_type as ::core::default::Default>::default());
    };

    // Two-argument form: caller supplies the init expression.
    ($workload_type:ty, $init_expr:expr) => {
        // ── WASM Component Model exports ──────────────────────────────────
        //
        // wit-bindgen generates an `exports::actr::workload::workload::Guest`
        // trait with 17 async methods (one `dispatch` + sixteen hooks). We
        // emit a single zero-sized adapter struct in user-crate scope and
        // route every method through helpers in `actr_framework::guest::wasm::adapter`.
        //
        // Skipped when the `web` feature is on — that path uses the
        // wasm-bindgen + `actr-web-abi` pipeline below instead of the
        // Component Model exports.
        #[cfg(all(target_arch = "wasm32", not(feature = "web")))]
        const _: () = {
            // Module-local singleton cell. Lazy-init on first call; subsequent
            // dispatches reuse the same instance.
            static __ACTR_WORKLOAD: $crate::guest::__wasm_macro_support::WorkloadCell<$workload_type> =
                $crate::guest::__wasm_macro_support::WorkloadCell::new();

            fn __actr_workload() -> &'static $workload_type {
                __ACTR_WORKLOAD.get_or_init(|| -> $workload_type { $init_expr })
            }

            struct __ActrEntryAdapter;

            impl $crate::guest::__wasm_macro_support::Guest for __ActrEntryAdapter {
                async fn dispatch(
                    envelope: $crate::guest::__wasm_macro_support::WitRpcEnvelope,
                ) -> ::core::result::Result<
                    ::std::vec::Vec<u8>,
                    $crate::guest::__wasm_macro_support::WitActrError,
                > {
                    $crate::guest::__wasm_macro_support::run_dispatch(
                        __actr_workload(),
                        envelope,
                    )
                    .await
                }

                async fn on_start() -> ::core::result::Result<
                    (),
                    $crate::guest::__wasm_macro_support::WitActrError,
                > {
                    $crate::guest::__wasm_macro_support::run_on_start(__actr_workload()).await
                }

                async fn on_ready() -> ::core::result::Result<
                    (),
                    $crate::guest::__wasm_macro_support::WitActrError,
                > {
                    $crate::guest::__wasm_macro_support::run_on_ready(__actr_workload()).await
                }

                async fn on_stop() -> ::core::result::Result<
                    (),
                    $crate::guest::__wasm_macro_support::WitActrError,
                > {
                    $crate::guest::__wasm_macro_support::run_on_stop(__actr_workload()).await
                }

                async fn on_error(
                    event: $crate::guest::__wasm_macro_support::WitErrorEvent,
                ) -> ::core::result::Result<
                    (),
                    $crate::guest::__wasm_macro_support::WitActrError,
                > {
                    $crate::guest::__wasm_macro_support::run_on_error(__actr_workload(), event)
                        .await
                }

                async fn on_signaling_connecting() {
                    $crate::guest::__wasm_macro_support::run_on_signaling_connecting(
                        __actr_workload(),
                    )
                    .await
                }

                async fn on_signaling_connected() {
                    $crate::guest::__wasm_macro_support::run_on_signaling_connected(
                        __actr_workload(),
                    )
                    .await
                }

                async fn on_signaling_disconnected() {
                    $crate::guest::__wasm_macro_support::run_on_signaling_disconnected(
                        __actr_workload(),
                    )
                    .await
                }

                async fn on_websocket_connecting(
                    event: $crate::guest::__wasm_macro_support::WitPeerEvent,
                ) {
                    $crate::guest::__wasm_macro_support::run_on_websocket_connecting(
                        __actr_workload(),
                        event,
                    )
                    .await
                }

                async fn on_websocket_connected(
                    event: $crate::guest::__wasm_macro_support::WitPeerEvent,
                ) {
                    $crate::guest::__wasm_macro_support::run_on_websocket_connected(
                        __actr_workload(),
                        event,
                    )
                    .await
                }

                async fn on_websocket_disconnected(
                    event: $crate::guest::__wasm_macro_support::WitPeerEvent,
                ) {
                    $crate::guest::__wasm_macro_support::run_on_websocket_disconnected(
                        __actr_workload(),
                        event,
                    )
                    .await
                }

                async fn on_webrtc_connecting(
                    event: $crate::guest::__wasm_macro_support::WitPeerEvent,
                ) {
                    $crate::guest::__wasm_macro_support::run_on_webrtc_connecting(
                        __actr_workload(),
                        event,
                    )
                    .await
                }

                async fn on_webrtc_connected(
                    event: $crate::guest::__wasm_macro_support::WitPeerEvent,
                ) {
                    $crate::guest::__wasm_macro_support::run_on_webrtc_connected(
                        __actr_workload(),
                        event,
                    )
                    .await
                }

                async fn on_webrtc_disconnected(
                    event: $crate::guest::__wasm_macro_support::WitPeerEvent,
                ) {
                    $crate::guest::__wasm_macro_support::run_on_webrtc_disconnected(
                        __actr_workload(),
                        event,
                    )
                    .await
                }

                async fn on_credential_renewed(
                    event: $crate::guest::__wasm_macro_support::WitCredentialEvent,
                ) {
                    $crate::guest::__wasm_macro_support::run_on_credential_renewed(
                        __actr_workload(),
                        event,
                    )
                    .await
                }

                async fn on_credential_expiring(
                    event: $crate::guest::__wasm_macro_support::WitCredentialEvent,
                ) {
                    $crate::guest::__wasm_macro_support::run_on_credential_expiring(
                        __actr_workload(),
                        event,
                    )
                    .await
                }

                async fn on_mailbox_backpressure(
                    event: $crate::guest::__wasm_macro_support::WitBackpressureEvent,
                ) {
                    $crate::guest::__wasm_macro_support::run_on_mailbox_backpressure(
                        __actr_workload(),
                        event,
                    )
                    .await
                }

                async fn on_data_stream(
                    chunk: $crate::guest::__wasm_macro_support::WitDataStream,
                    sender: $crate::guest::__wasm_macro_support::WitActrId,
                ) -> ::core::result::Result<
                    (),
                    $crate::guest::__wasm_macro_support::WitActrError,
                > {
                    $crate::guest::__wasm_macro_support::run_on_data_stream(chunk, sender).await
                }
            }

            $crate::guest::wasm::generated::export!(__ActrEntryAdapter with_types_in $crate::guest::wasm::generated);
        };

        // ── Web (wasm-bindgen + actr-web-abi) exports ─────────────────────
        //
        // Phase 6b: wrap the user workload in `WebWorkloadAdapter` and hand
        // it to `actr_web_abi::host::register_workload` from a wasm-bindgen
        // `start` hook. The 17 `#[wasm_bindgen]` entry points exported by
        // `actr-web-abi::host` are the public surface the Service Worker
        // dispatches into; they resolve through the registered adapter back
        // into the user's `Workload` impl. See
        // `bindings/web/docs/option-u-phase6-gamma-unified.zh.md` §4.5.
        #[cfg(all(target_arch = "wasm32", feature = "web"))]
        const _: () = {
            // `wasm_bindgen(start)` functions are invoked once per module
            // instantiation by the wasm-bindgen runtime. `register_workload`
            // itself panics on double-registration, so wrapping it inside a
            // `start` fn naturally enforces single-shot bootstrap.
            //
            // The attribute is referenced through its fully-qualified path
            // so the user's crate does not need its own `wasm-bindgen`
            // dependency — `actr-framework` re-exports the attribute through
            // `web::__web_macro_support` under `feature = "web"`.
            #[$crate::web::__web_macro_support::wasm_bindgen(start)]
            fn __actr_web_bootstrap() {
                let workload: $workload_type = $init_expr;
                let adapter =
                    $crate::web::__web_macro_support::WebWorkloadAdapter::new(workload);
                $crate::web::__web_macro_support::register_workload(adapter);
            }
        };

        // ── cdylib ABI exports ────────────────────────────────────────────
        //
        // Unchanged from pre-Phase-1; the Component Model rewrite is WASM-
        // only and does not touch the native shared-library path.
        #[cfg(feature = "cdylib")]
        const _: () = {
            static mut __ACTR_WORKLOAD: Option<$workload_type> = None;
            static mut __ACTR_VTABLE: Option<*const $crate::guest::vtable::HostVTable> = None;

            /// Initialize actor
            ///
            /// Host calls this after dlopen, passing HostVTable and init payload.
            /// Returns 0 on success, negative on error.
            /// Repeated calls return `INIT_FAILED` (one init per guest instance).
            #[unsafe(no_mangle)]
            pub unsafe extern "C" fn actr_init(
                vtable: *const $crate::guest::vtable::HostVTable,
                init_ptr: *const u8,
                init_len: usize,
            ) -> i32 {
                if vtable.is_null() {
                    return $crate::guest::dynclib_abi::code::INIT_FAILED;
                }

                let init_bytes = if init_ptr.is_null() || init_len == 0 {
                    &[][..]
                } else {
                    unsafe { std::slice::from_raw_parts(init_ptr, init_len) }
                };

                // TODO: `actr_init` currently only validates that `InitPayloadV1`
                // is decodable. The payload fields themselves are not yet
                // consumed by the guest runtime on the dynclib path. This is a
                // legacy gap carried forward from the previous init model.
                if $crate::guest::dynclib_abi::decode_message::<$crate::guest::dynclib_abi::InitPayloadV1>(
                    init_bytes,
                )
                .is_err()
                {
                    return $crate::guest::dynclib_abi::code::PROTOCOL_ERROR;
                }

                let workload: $workload_type = $init_expr;
                unsafe {
                    if __ACTR_WORKLOAD.is_some() {
                        return $crate::guest::dynclib_abi::code::INIT_FAILED;
                    }
                    __ACTR_VTABLE = Some(vtable);
                    __ACTR_WORKLOAD = Some(workload);
                }
                $crate::guest::dynclib_abi::code::SUCCESS
            }

            /// Handle one runtime ABI frame.
            #[unsafe(no_mangle)]
            pub unsafe extern "C" fn actr_handle(
                req_ptr: *const u8,
                req_len: usize,
                resp_out: *mut *mut u8,
                resp_len_out: *mut usize,
            ) -> i32 {
                use actr_protocol::prost::Message as ProstMessage;
                use $crate::{MessageDispatcher, Workload};

                // Get vtable
                let vtable = match unsafe { __ACTR_VTABLE } {
                    Some(vt) => vt,
                    None => return $crate::guest::dynclib_abi::code::INIT_FAILED,
                };

                // Read runtime frame
                if req_ptr.is_null() {
                    return $crate::guest::dynclib_abi::code::PROTOCOL_ERROR;
                }
                let req_bytes = unsafe { std::slice::from_raw_parts(req_ptr, req_len) };

                let frame = match $crate::guest::dynclib_abi::decode_message::<
                    $crate::guest::dynclib_abi::AbiFrame,
                >(req_bytes) {
                    Ok(f) => f,
                    Err(_) => return $crate::guest::dynclib_abi::code::PROTOCOL_ERROR,
                };

                if frame.op == $crate::guest::dynclib_abi::op::GUEST_LIFECYCLE {
                    let payload = match <$crate::guest::dynclib_abi::GuestLifecycleV1 as $crate::guest::dynclib_abi::AbiPayload>::decode_payload(&frame.payload) {
                        Ok(payload) => payload,
                        Err(_) => return $crate::guest::dynclib_abi::code::PROTOCOL_ERROR,
                    };

                    let ctx = match unsafe {
                        $crate::guest::dynclib::context::DynclibContext::from_invocation(vtable, payload.ctx)
                    } {
                        Ok(c) => c,
                        Err(_) => return $crate::guest::dynclib_abi::code::HANDLE_FAILED,
                    };

                    let workload = unsafe {
                        match __ACTR_WORKLOAD.as_ref() {
                            Some(w) => w,
                            None => return $crate::guest::dynclib_abi::code::INIT_FAILED,
                        }
                    };

                    let lifecycle_result = match payload.hook {
                        $crate::guest::dynclib_abi::lifecycle_hook::ON_START => {
                            let fut = workload.on_start(&ctx);
                            let waker = std::task::Waker::noop();
                            let mut cx = std::task::Context::from_waker(waker);
                            let mut pinned = std::pin::pin!(fut);
                            match pinned.as_mut().poll(&mut cx) {
                                std::task::Poll::Ready(v) => v,
                                std::task::Poll::Pending => {
                                    return $crate::guest::dynclib_abi::code::HANDLE_FAILED;
                                }
                            }
                        }
                        $crate::guest::dynclib_abi::lifecycle_hook::ON_READY => {
                            let fut = workload.on_ready(&ctx);
                            let waker = std::task::Waker::noop();
                            let mut cx = std::task::Context::from_waker(waker);
                            let mut pinned = std::pin::pin!(fut);
                            match pinned.as_mut().poll(&mut cx) {
                                std::task::Poll::Ready(v) => v,
                                std::task::Poll::Pending => {
                                    return $crate::guest::dynclib_abi::code::HANDLE_FAILED;
                                }
                            }
                        }
                        $crate::guest::dynclib_abi::lifecycle_hook::ON_STOP => {
                            let fut = workload.on_stop(&ctx);
                            let waker = std::task::Waker::noop();
                            let mut cx = std::task::Context::from_waker(waker);
                            let mut pinned = std::pin::pin!(fut);
                            match pinned.as_mut().poll(&mut cx) {
                                std::task::Poll::Ready(v) => v,
                                std::task::Poll::Pending => {
                                    return $crate::guest::dynclib_abi::code::HANDLE_FAILED;
                                }
                            }
                        }
                        _ => return $crate::guest::dynclib_abi::code::UNSUPPORTED_OP,
                    };

                    let resp_bytes = match lifecycle_result {
                        Ok(()) => match $crate::guest::dynclib_abi::success_reply(::std::vec::Vec::new()) {
                            Ok(bytes) => bytes,
                            Err(code) => return code,
                        },
                        Err(err) => match $crate::guest::dynclib_abi::error_reply(
                            $crate::guest::dynclib_abi::code::HANDLE_FAILED,
                            err.to_string().into_bytes(),
                        ) {
                            Ok(bytes) => bytes,
                            Err(code) => return code,
                        },
                    };

                    let resp_len = resp_bytes.len();
                    let layout = match std::alloc::Layout::from_size_align(resp_len.max(1), 1) {
                        Ok(l) => l,
                        Err(_) => return $crate::guest::dynclib_abi::code::GENERIC_ERROR,
                    };
                    let ptr = unsafe { std::alloc::alloc(layout) };
                    if ptr.is_null() {
                        return $crate::guest::dynclib_abi::code::GENERIC_ERROR;
                    }

                    unsafe {
                        std::ptr::copy_nonoverlapping(resp_bytes.as_ptr(), ptr, resp_len);
                        *resp_out = ptr;
                        *resp_len_out = resp_len;
                    }

                    return $crate::guest::dynclib_abi::code::SUCCESS;
                }

                if frame.op == $crate::guest::dynclib_abi::op::GUEST_HOOK {
                    let payload = match <$crate::guest::dynclib_abi::GuestHookV1 as $crate::guest::dynclib_abi::AbiPayload>::decode_payload(&frame.payload) {
                        Ok(payload) => payload,
                        Err(_) => return $crate::guest::dynclib_abi::code::PROTOCOL_ERROR,
                    };

                    let ctx = match unsafe {
                        $crate::guest::dynclib::context::DynclibContext::from_invocation(vtable, payload.ctx)
                    } {
                        Ok(c) => c,
                        Err(_) => return $crate::guest::dynclib_abi::code::HANDLE_FAILED,
                    };

                    let workload = unsafe {
                        match __ACTR_WORKLOAD.as_ref() {
                            Some(w) => w,
                            None => return $crate::guest::dynclib_abi::code::INIT_FAILED,
                        }
                    };

                    macro_rules! __actr_poll_unit {
                        ($future:expr) => {{
                            let fut = $future;
                            let waker = std::task::Waker::noop();
                            let mut cx = std::task::Context::from_waker(waker);
                            let mut pinned = std::pin::pin!(fut);
                            match pinned.as_mut().poll(&mut cx) {
                                std::task::Poll::Ready(()) => {}
                                std::task::Poll::Pending => {
                                    return $crate::guest::dynclib_abi::code::HANDLE_FAILED;
                                }
                            }
                        }};
                    }

                    let peer_event = |peer: $crate::guest::dynclib_abi::PeerEventV1| {
                        $crate::PeerEvent {
                            peer: peer.peer,
                            relayed: peer.relayed,
                        }
                    };

                    let timestamp = |ts: $crate::guest::dynclib_abi::TimestampV1| {
                        std::time::UNIX_EPOCH
                            + std::time::Duration::new(ts.seconds, ts.nanoseconds)
                    };

                    match payload.hook {
                        $crate::guest::dynclib_abi::runtime_hook::ON_SIGNALING_CONNECTING => {
                            __actr_poll_unit!(workload.on_signaling_connecting(Some(&ctx)));
                        }
                        $crate::guest::dynclib_abi::runtime_hook::ON_SIGNALING_CONNECTED => {
                            __actr_poll_unit!(workload.on_signaling_connected(Some(&ctx)));
                        }
                        $crate::guest::dynclib_abi::runtime_hook::ON_SIGNALING_DISCONNECTED => {
                            __actr_poll_unit!(workload.on_signaling_disconnected(&ctx));
                        }
                        $crate::guest::dynclib_abi::runtime_hook::ON_WEBSOCKET_CONNECTING => {
                            let event = match payload.peer {
                                Some(peer) => peer_event(peer),
                                None => return $crate::guest::dynclib_abi::code::PROTOCOL_ERROR,
                            };
                            __actr_poll_unit!(workload.on_websocket_connecting(&ctx, &event));
                        }
                        $crate::guest::dynclib_abi::runtime_hook::ON_WEBSOCKET_CONNECTED => {
                            let event = match payload.peer {
                                Some(peer) => peer_event(peer),
                                None => return $crate::guest::dynclib_abi::code::PROTOCOL_ERROR,
                            };
                            __actr_poll_unit!(workload.on_websocket_connected(&ctx, &event));
                        }
                        $crate::guest::dynclib_abi::runtime_hook::ON_WEBSOCKET_DISCONNECTED => {
                            let event = match payload.peer {
                                Some(peer) => peer_event(peer),
                                None => return $crate::guest::dynclib_abi::code::PROTOCOL_ERROR,
                            };
                            __actr_poll_unit!(workload.on_websocket_disconnected(&ctx, &event));
                        }
                        $crate::guest::dynclib_abi::runtime_hook::ON_WEBRTC_CONNECTING => {
                            let event = match payload.peer {
                                Some(peer) => peer_event(peer),
                                None => return $crate::guest::dynclib_abi::code::PROTOCOL_ERROR,
                            };
                            __actr_poll_unit!(workload.on_webrtc_connecting(&ctx, &event));
                        }
                        $crate::guest::dynclib_abi::runtime_hook::ON_WEBRTC_CONNECTED => {
                            let event = match payload.peer {
                                Some(peer) => peer_event(peer),
                                None => return $crate::guest::dynclib_abi::code::PROTOCOL_ERROR,
                            };
                            __actr_poll_unit!(workload.on_webrtc_connected(&ctx, &event));
                        }
                        $crate::guest::dynclib_abi::runtime_hook::ON_WEBRTC_DISCONNECTED => {
                            let event = match payload.peer {
                                Some(peer) => peer_event(peer),
                                None => return $crate::guest::dynclib_abi::code::PROTOCOL_ERROR,
                            };
                            __actr_poll_unit!(workload.on_webrtc_disconnected(&ctx, &event));
                        }
                        $crate::guest::dynclib_abi::runtime_hook::ON_CREDENTIAL_RENEWED => {
                            let event = match payload.credential {
                                Some(credential) => $crate::CredentialEvent {
                                    new_expiry: timestamp(credential.new_expiry),
                                },
                                None => return $crate::guest::dynclib_abi::code::PROTOCOL_ERROR,
                            };
                            __actr_poll_unit!(workload.on_credential_renewed(&ctx, &event));
                        }
                        $crate::guest::dynclib_abi::runtime_hook::ON_CREDENTIAL_EXPIRING => {
                            let event = match payload.credential {
                                Some(credential) => $crate::CredentialEvent {
                                    new_expiry: timestamp(credential.new_expiry),
                                },
                                None => return $crate::guest::dynclib_abi::code::PROTOCOL_ERROR,
                            };
                            __actr_poll_unit!(workload.on_credential_expiring(&ctx, &event));
                        }
                        $crate::guest::dynclib_abi::runtime_hook::ON_MAILBOX_BACKPRESSURE => {
                            let event = match payload.backpressure {
                                Some(backpressure) => $crate::BackpressureEvent {
                                    queue_len: backpressure.queue_len as usize,
                                    threshold: backpressure.threshold as usize,
                                },
                                None => return $crate::guest::dynclib_abi::code::PROTOCOL_ERROR,
                            };
                            __actr_poll_unit!(workload.on_mailbox_backpressure(&ctx, &event));
                        }
                        _ => return $crate::guest::dynclib_abi::code::UNSUPPORTED_OP,
                    }

                    let resp_bytes = match $crate::guest::dynclib_abi::success_reply(::std::vec::Vec::new()) {
                        Ok(bytes) => bytes,
                        Err(code) => return code,
                    };

                    let resp_len = resp_bytes.len();
                    let layout = match std::alloc::Layout::from_size_align(resp_len.max(1), 1) {
                        Ok(l) => l,
                        Err(_) => return $crate::guest::dynclib_abi::code::GENERIC_ERROR,
                    };
                    let ptr = unsafe { std::alloc::alloc(layout) };
                    if ptr.is_null() {
                        return $crate::guest::dynclib_abi::code::GENERIC_ERROR;
                    }

                    unsafe {
                        std::ptr::copy_nonoverlapping(resp_bytes.as_ptr(), ptr, resp_len);
                        *resp_out = ptr;
                        *resp_len_out = resp_len;
                    }

                    return $crate::guest::dynclib_abi::code::SUCCESS;
                }

                if frame.op == $crate::guest::dynclib_abi::op::GUEST_DATA_STREAM {
                    let payload = match <$crate::guest::dynclib_abi::GuestDataStreamV1 as $crate::guest::dynclib_abi::AbiPayload>::decode_payload(&frame.payload) {
                        Ok(payload) => payload,
                        Err(_) => return $crate::guest::dynclib_abi::code::PROTOCOL_ERROR,
                    };

                    let resp_bytes = match $crate::guest::dynclib::context::dispatch_registered_stream(payload) {
                        Ok(()) => match $crate::guest::dynclib_abi::success_reply(::std::vec::Vec::new()) {
                            Ok(bytes) => bytes,
                            Err(code) => return code,
                        },
                        Err(err) => match $crate::guest::dynclib_abi::error_reply(
                            $crate::guest::dynclib_abi::code::HANDLE_FAILED,
                            err.to_string().into_bytes(),
                        ) {
                            Ok(bytes) => bytes,
                            Err(code) => return code,
                        },
                    };

                    let resp_len = resp_bytes.len();
                    let layout = match std::alloc::Layout::from_size_align(resp_len.max(1), 1) {
                        Ok(l) => l,
                        Err(_) => return $crate::guest::dynclib_abi::code::GENERIC_ERROR,
                    };
                    let ptr = unsafe { std::alloc::alloc(layout) };
                    if ptr.is_null() {
                        return $crate::guest::dynclib_abi::code::GENERIC_ERROR;
                    }

                    unsafe {
                        std::ptr::copy_nonoverlapping(resp_bytes.as_ptr(), ptr, resp_len);
                        *resp_out = ptr;
                        *resp_len_out = resp_len;
                    }

                    return $crate::guest::dynclib_abi::code::SUCCESS;
                }

                if frame.op != $crate::guest::dynclib_abi::op::GUEST_HANDLE {
                    return $crate::guest::dynclib_abi::code::UNSUPPORTED_OP;
                }

                let handle = match <$crate::guest::dynclib_abi::GuestHandleV1 as $crate::guest::dynclib_abi::AbiPayload>::decode_payload(&frame.payload) {
                    Ok(handle) => handle,
                    Err(_) => return $crate::guest::dynclib_abi::code::PROTOCOL_ERROR,
                };

                let envelope = match actr_protocol::RpcEnvelope::decode(handle.rpc_envelope.as_slice()) {
                    Ok(e) => e,
                    Err(_) => return $crate::guest::dynclib_abi::code::PROTOCOL_ERROR,
                };

                let ctx = match unsafe {
                    $crate::guest::dynclib::context::DynclibContext::from_invocation(vtable, handle.ctx)
                } {
                    Ok(c) => c,
                    Err(_) => return $crate::guest::dynclib_abi::code::HANDLE_FAILED,
                };

                // Get workload reference
                let workload = unsafe {
                    match __ACTR_WORKLOAD.as_ref() {
                        Some(w) => w,
                        None => return $crate::guest::dynclib_abi::code::INIT_FAILED,
                    }
                };

                // Route and execute via MessageDispatcher
                type Dispatcher = <$workload_type as Workload>::Dispatcher;

                // cdylib is native environment, can use tokio or synchronous execution
                // Here we use the same single-threaded poll strategy as the old WASM path:
                // All host callbacks (vtable function pointers) are synchronous, Future
                // completes in one poll.
                let resp_result = {
                    let fut = Dispatcher::dispatch(workload, envelope, &ctx);
                    let waker = std::task::Waker::noop();
                    let mut cx = std::task::Context::from_waker(waker);
                    let mut pinned = std::pin::pin!(fut);
                    match pinned.as_mut().poll(&mut cx) {
                        std::task::Poll::Ready(v) => v,
                        std::task::Poll::Pending => {
                            return $crate::guest::dynclib_abi::code::HANDLE_FAILED;
                        }
                    }
                };

                let resp_bytes = match resp_result {
                    Ok(b) => match $crate::guest::dynclib_abi::success_reply(b.to_vec()) {
                        Ok(bytes) => bytes,
                        Err(code) => return code,
                    },
                    Err(err) => match $crate::guest::dynclib_abi::error_reply(
                        $crate::guest::dynclib_abi::code::HANDLE_FAILED,
                        err.to_string().into_bytes(),
                    ) {
                        Ok(bytes) => bytes,
                        Err(code) => return code,
                    },
                };

                // Allocate response buffer on guest heap
                let resp_len = resp_bytes.len();
                let layout = match std::alloc::Layout::from_size_align(resp_len.max(1), 1) {
                    Ok(l) => l,
                    Err(_) => return $crate::guest::dynclib_abi::code::GENERIC_ERROR,
                };
                let ptr = unsafe { std::alloc::alloc(layout) };
                if ptr.is_null() {
                    return $crate::guest::dynclib_abi::code::GENERIC_ERROR;
                }

                unsafe {
                    std::ptr::copy_nonoverlapping(resp_bytes.as_ptr(), ptr, resp_len);
                    *resp_out = ptr;
                    *resp_len_out = resp_len;
                }

                $crate::guest::dynclib_abi::code::SUCCESS
            }

            /// Free guest-allocated response buffer
            ///
            /// Host calls this after using the response data returned by `actr_handle`.
            #[unsafe(no_mangle)]
            pub unsafe extern "C" fn actr_free_response(ptr: *mut u8, len: usize) {
                if ptr.is_null() || len == 0 {
                    return;
                }
                let layout = match std::alloc::Layout::from_size_align(len, 1) {
                    Ok(l) => l,
                    Err(_) => return,
                };
                unsafe { std::alloc::dealloc(ptr, layout) };
            }
        };
    };
}