shape-runtime 0.3.0

Bytecode compiler, builtins, and runtime infrastructure for Shape
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
//! Platform-agnostic event queue for async operations
//!
//! This module provides a generic event queue abstraction that works across:
//! - Native Tokio runtime
//! - Bare metal / no_std environments
//!
//! The design avoids Tokio-specific async primitives in the core trait,
//! allowing Shape to run on any platform.

use serde::{Deserialize, Serialize};
// ADR-006 §2.7: GENERIC_CARRIER vector storage uses `KindedSlot`. Event
// payloads (`QueuedEvent::DataPoint::data`) and suspension-state buffers
// (`SuspensionState::saved_locals`/`saved_stack`) hold heterogeneous
// runtime values whose `NativeKind` isn't statically determined here.
// Serialization of the slot bytes is deferred (see snapshot.rs comment
// at line 650) — the data fields stay `#[serde(skip)]` for now.
use shape_value::KindedSlot;
use std::sync::Arc;

/// Events that can be queued for processing
#[derive(Debug, Clone)]
pub enum QueuedEvent {
    /// New data point arrived from a data source
    DataPoint {
        /// Name of the data source (e.g., "data", "iot_sensors")
        source: String,
        /// The data payload
        data: KindedSlot,
    },

    /// Timer fired
    Timer {
        /// Timer ID for matching with awaiting code
        id: u64,
    },

    /// External signal from a plugin or external system
    External {
        /// Raw payload bytes (typically MessagePack encoded)
        payload: Vec<u8>,
    },

    /// Subscription update (streaming data)
    Subscription {
        /// Subscription ID
        subscription_id: u64,
        /// Source name
        source: String,
        /// Data payload
        data: KindedSlot,
    },

    /// Error from a data source or plugin
    Error {
        /// Source of the error
        source: String,
        /// Error message
        message: String,
    },

    /// Shutdown request
    Shutdown,
}

/// Platform-agnostic event queue trait
///
/// Implementations provide different backing stores:
/// - `MemoryEventQueue`: Lock-free queue for general use
/// - `TokioEventQueue`: Integrates with Tokio channels (native only)
pub trait EventQueue: Send + Sync {
    /// Poll for the next event (non-blocking)
    ///
    /// Returns `None` if the queue is empty.
    fn poll(&self) -> Option<QueuedEvent>;

    /// Push an event onto the queue
    fn push(&self, event: QueuedEvent);

    /// Check if the queue is empty
    fn is_empty(&self) -> bool;

    /// Get the number of pending events
    fn len(&self) -> usize;

    /// Try to receive multiple events at once (batch poll)
    ///
    /// Returns up to `max` events. Default implementation polls repeatedly.
    fn poll_batch(&self, max: usize) -> Vec<QueuedEvent> {
        let mut events = Vec::with_capacity(max);
        while events.len() < max {
            if let Some(event) = self.poll() {
                events.push(event);
            } else {
                break;
            }
        }
        events
    }
}

/// In-memory event queue using crossbeam's lock-free queue
///
/// This implementation works everywhere (native, no_std with alloc).
pub struct MemoryEventQueue {
    queue: crossbeam_queue::SegQueue<QueuedEvent>,
}

impl MemoryEventQueue {
    /// Create a new empty queue
    pub fn new() -> Self {
        Self {
            queue: crossbeam_queue::SegQueue::new(),
        }
    }
}

impl Default for MemoryEventQueue {
    fn default() -> Self {
        Self::new()
    }
}

impl EventQueue for MemoryEventQueue {
    fn poll(&self) -> Option<QueuedEvent> {
        self.queue.pop()
    }

    fn push(&self, event: QueuedEvent) {
        self.queue.push(event);
    }

    fn is_empty(&self) -> bool {
        self.queue.is_empty()
    }

    fn len(&self) -> usize {
        self.queue.len()
    }
}

/// Tokio-backed event queue for native async integration
///
/// Uses unbounded MPSC channels for integration with Tokio's async runtime.
#[cfg(feature = "tokio-runtime")]
pub struct TokioEventQueue {
    sender: tokio::sync::mpsc::UnboundedSender<QueuedEvent>,
    receiver: std::sync::Mutex<tokio::sync::mpsc::UnboundedReceiver<QueuedEvent>>,
}

#[cfg(feature = "tokio-runtime")]
impl TokioEventQueue {
    /// Create a new Tokio-backed event queue
    pub fn new() -> Self {
        let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();
        Self {
            sender,
            receiver: std::sync::Mutex::new(receiver),
        }
    }

    /// Get a sender handle for pushing events from async contexts
    pub fn sender(&self) -> tokio::sync::mpsc::UnboundedSender<QueuedEvent> {
        self.sender.clone()
    }

    /// Async receive - waits for next event
    pub async fn recv_async(&self) -> Option<QueuedEvent> {
        // Note: This requires holding the lock across await, which is not ideal
        // In practice, we'd use a different pattern for true async
        self.sender.clone();
        None // Placeholder - actual impl would use proper async patterns
    }
}

#[cfg(feature = "tokio-runtime")]
impl Default for TokioEventQueue {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "tokio-runtime")]
impl EventQueue for TokioEventQueue {
    fn poll(&self) -> Option<QueuedEvent> {
        if let Ok(mut receiver) = self.receiver.try_lock() {
            receiver.try_recv().ok()
        } else {
            None
        }
    }

    fn push(&self, event: QueuedEvent) {
        let _ = self.sender.send(event);
    }

    fn is_empty(&self) -> bool {
        self.sender.is_closed() || self.len() == 0
    }

    fn len(&self) -> usize {
        // Unbounded channels don't expose length directly
        // This is an approximation
        0
    }
}

/// Suspension state for resumable execution
///
/// When a Shape program yields or awaits, this state captures
/// everything needed to resume execution later.
///
/// **WB2.5 retain-on-read.** `saved_locals` / `saved_stack` hold
/// owning shares of heap-tagged values. ADR-006 §2.7 GENERIC_CARRIER
/// vector storage: `Vec<KindedSlot>` carries the explicit `Drop`/`Clone`
/// discipline so push/pop/clone preserve refcount semantics. The serde
/// skip-on-payload pattern matches the snapshot.rs:650 comment — slot
/// (de)serialization is deferred to the kind-threaded follow-up API.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SuspensionState {
    /// What condition we're waiting for
    pub waiting_for: WaitCondition,
    /// Program counter to resume at (for VM/JIT)
    pub resume_pc: usize,
    /// Saved local variables
    #[serde(skip)]
    #[serde(default)]
    pub saved_locals: Vec<KindedSlot>,
    /// Saved stack (for VM)
    #[serde(skip)]
    #[serde(default)]
    pub saved_stack: Vec<KindedSlot>,
}

impl SuspensionState {
    /// Create a new suspension state
    pub fn new(waiting_for: WaitCondition, resume_pc: usize) -> Self {
        Self {
            waiting_for,
            resume_pc,
            saved_locals: Vec::new(),
            saved_stack: Vec::new(),
        }
    }

    /// Create with saved locals. `KindedSlot::Drop` retires refcounts on
    /// drop; `KindedSlot::Clone` retains them on copy.
    pub fn with_locals(mut self, locals: Vec<KindedSlot>) -> Self {
        self.saved_locals = locals;
        self
    }

    /// Create with saved stack
    pub fn with_stack(mut self, stack: Vec<KindedSlot>) -> Self {
        self.saved_stack = stack;
        self
    }
}

/// Condition that caused suspension
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum WaitCondition {
    /// Waiting for next data bar from a source
    NextBar {
        /// Data source name
        source: String,
    },

    /// Waiting for a timer to fire
    Timer {
        /// Timer ID
        id: u64,
        /// Deadline (milliseconds since epoch)
        deadline_ms: u64,
    },

    /// Waiting for an external event
    External {
        /// Event type filter
        event_type: String,
    },

    /// Waiting for any event from the queue
    AnyEvent,

    /// Yielded for cooperative scheduling (no specific wait)
    Yield,
    /// Explicit snapshot suspension
    Snapshot,
    /// Waiting for a future to resolve
    Future { id: u64 },
}

/// Shared event queue type alias
pub type SharedEventQueue = Arc<dyn EventQueue>;

/// Create a default memory-based event queue
pub fn create_event_queue() -> SharedEventQueue {
    Arc::new(MemoryEventQueue::new())
}

/// Create a Tokio-backed event queue (when feature enabled)
#[cfg(feature = "tokio-runtime")]
pub fn create_tokio_event_queue() -> SharedEventQueue {
    Arc::new(TokioEventQueue::new())
}

#[cfg(test)]
mod tests {
    use super::*;
    #[allow(unused_imports)]
    use std::sync::Arc;

    #[test]
    fn test_memory_event_queue_basic() {
        let queue = MemoryEventQueue::new();

        assert!(queue.is_empty());
        assert_eq!(queue.len(), 0);

        queue.push(QueuedEvent::Timer { id: 1 });
        assert!(!queue.is_empty());
        assert_eq!(queue.len(), 1);

        let event = queue.poll();
        assert!(matches!(event, Some(QueuedEvent::Timer { id: 1 })));
        assert!(queue.is_empty());
    }

    #[test]
    fn test_memory_event_queue_fifo() {
        let queue = MemoryEventQueue::new();

        queue.push(QueuedEvent::Timer { id: 1 });
        queue.push(QueuedEvent::Timer { id: 2 });
        queue.push(QueuedEvent::Timer { id: 3 });

        assert!(matches!(queue.poll(), Some(QueuedEvent::Timer { id: 1 })));
        assert!(matches!(queue.poll(), Some(QueuedEvent::Timer { id: 2 })));
        assert!(matches!(queue.poll(), Some(QueuedEvent::Timer { id: 3 })));
        assert!(queue.poll().is_none());
    }

    #[test]
    fn test_poll_batch() {
        let queue = MemoryEventQueue::new();

        for i in 0..5 {
            queue.push(QueuedEvent::Timer { id: i });
        }

        let batch = queue.poll_batch(3);
        assert_eq!(batch.len(), 3);
        assert_eq!(queue.len(), 2);

        let remaining = queue.poll_batch(10);
        assert_eq!(remaining.len(), 2);
        assert!(queue.is_empty());
    }

    #[test]
    fn test_suspension_state() {
        let state = SuspensionState::new(
            WaitCondition::NextBar {
                source: "data".to_string(),
            },
            42,
        )
        .with_locals(vec![KindedSlot::from_number(1.0), KindedSlot::from_number(2.0)]);

        assert_eq!(state.resume_pc, 42);
        assert_eq!(state.saved_locals.len(), 2);
        assert!(matches!(
            &state.waiting_for,
            WaitCondition::NextBar { source } if source == "data"
        ));
    }

    #[test]
    fn test_event_types_data_point() {
        let queue = MemoryEventQueue::new();

        queue.push(QueuedEvent::DataPoint {
            source: "iot_sensors".to_string(),
            data: KindedSlot::from_number(42.5),
        });

        let event = queue.poll().unwrap();
        match event {
            QueuedEvent::DataPoint { source, data } => {
                assert_eq!(source, "iot_sensors");
                assert!((data.slot().as_f64() - 42.5).abs() < 0.001);
            }
            _ => panic!("Expected DataPoint event"),
        }
    }

    #[test]
    fn test_event_types_external() {
        let queue = MemoryEventQueue::new();

        queue.push(QueuedEvent::External {
            payload: vec![1, 2, 3, 4],
        });

        let event = queue.poll().unwrap();
        match event {
            QueuedEvent::External { payload } => {
                assert_eq!(payload, vec![1, 2, 3, 4]);
            }
            _ => panic!("Expected External event"),
        }
    }

    #[test]
    fn test_event_types_subscription() {
        let queue = MemoryEventQueue::new();

        queue.push(QueuedEvent::Subscription {
            subscription_id: 123,
            source: "live_feed".to_string(),
            data: KindedSlot::from_string_arc(Arc::new("update".to_string())),
        });

        let event = queue.poll().unwrap();
        match event {
            QueuedEvent::Subscription {
                subscription_id,
                source,
                data,
            } => {
                assert_eq!(subscription_id, 123);
                assert_eq!(source, "live_feed");
                // Read the Arc<String> back via the slot's raw bits.
                let s_ref: &str = unsafe {
                    let ptr = data.slot().raw() as *const String;
                    (*ptr).as_str()
                };
                assert_eq!(s_ref, "update");
            }
            _ => panic!("Expected Subscription event"),
        }
    }

    #[test]
    fn test_event_types_error() {
        let queue = MemoryEventQueue::new();

        queue.push(QueuedEvent::Error {
            source: "database".to_string(),
            message: "Connection failed".to_string(),
        });

        let event = queue.poll().unwrap();
        match event {
            QueuedEvent::Error { source, message } => {
                assert_eq!(source, "database");
                assert_eq!(message, "Connection failed");
            }
            _ => panic!("Expected Error event"),
        }
    }

    #[test]
    fn test_event_types_shutdown() {
        let queue = MemoryEventQueue::new();

        queue.push(QueuedEvent::Shutdown);

        let event = queue.poll().unwrap();
        assert!(matches!(event, QueuedEvent::Shutdown));
    }

    #[test]
    fn test_wait_condition_variants() {
        // Test all WaitCondition variants
        let next_bar = WaitCondition::NextBar {
            source: "src".to_string(),
        };
        assert!(matches!(next_bar, WaitCondition::NextBar { .. }));

        let timer = WaitCondition::Timer {
            id: 1,
            deadline_ms: 1000,
        };
        assert!(matches!(
            timer,
            WaitCondition::Timer {
                id: 1,
                deadline_ms: 1000
            }
        ));

        let external = WaitCondition::External {
            event_type: "alert".to_string(),
        };
        assert!(matches!(external, WaitCondition::External { .. }));

        let any = WaitCondition::AnyEvent;
        assert!(matches!(any, WaitCondition::AnyEvent));

        let yield_cond = WaitCondition::Yield;
        assert!(matches!(yield_cond, WaitCondition::Yield));
    }

    #[test]
    fn test_create_event_queue_returns_shared() {
        let queue1 = create_event_queue();
        let queue2 = queue1.clone();

        // Push via one reference
        queue1.push(QueuedEvent::Timer { id: 42 });

        // Poll via other reference
        let event = queue2.poll().unwrap();
        assert!(matches!(event, QueuedEvent::Timer { id: 42 }));
    }

    #[test]
    fn test_suspension_state_with_stack() {
        let state = SuspensionState::new(WaitCondition::Yield, 100).with_stack(vec![
            KindedSlot::from_number(1.0),
            KindedSlot::from_number(2.0),
            KindedSlot::from_number(3.0),
        ]);

        assert_eq!(state.resume_pc, 100);
        assert_eq!(state.saved_stack.len(), 3);
        assert!(state.saved_locals.is_empty());
    }

    #[test]
    fn test_mixed_event_ordering() {
        let queue = MemoryEventQueue::new();

        // Push different event types
        queue.push(QueuedEvent::Timer { id: 1 });
        queue.push(QueuedEvent::Shutdown);
        queue.push(QueuedEvent::DataPoint {
            source: "test".to_string(),
            data: KindedSlot::none(),
        });

        // Verify FIFO ordering preserved across types
        assert!(matches!(queue.poll(), Some(QueuedEvent::Timer { id: 1 })));
        assert!(matches!(queue.poll(), Some(QueuedEvent::Shutdown)));
        assert!(matches!(queue.poll(), Some(QueuedEvent::DataPoint { .. })));
        assert!(queue.poll().is_none());
    }
}