lion-core 0.3.0

Lion microkernel — production types, state machine, and kernel API
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
// Copyright (C) 2026 HaiyangLi
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Lion State Actor
//!
//! Corresponds to: Lion/State/Actor.lean
//!
//! Actor runtime for message-passing concurrency.

use std::collections::VecDeque;

use crate::types::{ActorId, MsgId, PluginId, SecurityLevel};

/// Message queue (FIFO)
///
/// Corresponds to Lean: `abbrev Queue (a : Type) := List a`
///
/// Using VecDeque for O(1) push_back / pop_front.
pub type Queue<T> = VecDeque<T>;

/// Error type for actor operations
#[derive(Debug)]
pub enum ActorError {
    /// Mailbox is full (capacity, current)
    MailboxFull(usize, usize),
    /// No message available
    NoMessage,
    /// Actor is blocked
    Blocked(ActorId),
}

/// Opaque message payload
///
/// Corresponds to Lean: `opaque Data : Type`
///
/// In Rust, we use a Vec<u8> as the concrete representation.
pub type Data = Vec<u8>;

/// Message between actors
///
/// Corresponds to Lean: `structure Message`
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Message {
    /// Message identifier
    ///
    /// Corresponds to Lean: `id : MsgId`
    pub(crate) id: MsgId,

    /// Source plugin
    ///
    /// Corresponds to Lean: `src : PluginId`
    pub(crate) src: PluginId,

    /// Destination plugin
    ///
    /// Corresponds to Lean: `dst : PluginId`
    pub(crate) dst: PluginId,

    /// Security level of the message
    ///
    /// Corresponds to Lean: `level : SecurityLevel`
    pub(crate) level: SecurityLevel,

    /// Message body
    ///
    /// Corresponds to Lean: `body : Data`
    pub(crate) body: Data,
}

impl Message {
    /// Create a new message
    pub fn new(id: MsgId, src: PluginId, dst: PluginId, level: SecurityLevel, body: Data) -> Self {
        Message {
            id,
            src,
            dst,
            level,
            body,
        }
    }

    /// Get the message ID
    #[inline]
    pub fn id(&self) -> MsgId {
        self.id
    }

    /// Get the source plugin
    #[inline]
    pub fn src(&self) -> PluginId {
        self.src
    }

    /// Get the destination plugin
    #[inline]
    pub fn dst(&self) -> PluginId {
        self.dst
    }

    /// Get the security level
    #[inline]
    pub fn level(&self) -> SecurityLevel {
        self.level
    }

    /// Get the message body
    #[inline]
    pub fn body(&self) -> &Data {
        &self.body
    }

    /// Consume the message and return the body
    pub fn into_body(self) -> Data {
        self.body
    }
}

/// Runtime state of an actor
///
/// Corresponds to Lean: `structure ActorRuntime`
#[derive(Debug, Clone)]
pub struct ActorRuntime {
    /// Router queue (in transit)
    ///
    /// Corresponds to Lean: `pending : Queue Message`
    pub(crate) pending: Queue<Message>,

    /// Delivered, ready to process
    ///
    /// Corresponds to Lean: `mailbox : Queue Message`
    pub(crate) mailbox: Queue<Message>,

    /// Mailbox capacity (bounded)
    ///
    /// Corresponds to Lean: `capacity : Nat`
    pub(crate) capacity: usize,

    /// Wait-for graph edge (for deadlock analysis)
    ///
    /// Corresponds to Lean: `blockedOn : Option ActorId`
    pub(crate) blocked_on: Option<ActorId>,

    /// Message waiting to be delivered
    ///
    /// Corresponds to Lean: `pendingSend : Option Message`
    pub(crate) pending_send: Option<Message>,
}

impl ActorRuntime {
    /// Create empty actor runtime
    ///
    /// Corresponds to Lean: `def ActorRuntime.empty (capacity : Nat) : ActorRuntime`
    pub fn empty(capacity: usize) -> Self {
        ActorRuntime {
            pending: VecDeque::new(),
            mailbox: VecDeque::new(),
            capacity,
            blocked_on: None,
            pending_send: None,
        }
    }

    /// Check if actor can receive (mailbox not empty)
    ///
    /// Corresponds to Lean: `def ActorRuntime.can_receive (ar : ActorRuntime) : Bool`
    #[inline]
    pub fn can_receive(&self) -> bool {
        !self.mailbox.is_empty()
    }

    /// Check if actor can send (pending queue not full)
    ///
    /// Corresponds to Lean: `def ActorRuntime.can_send (ar : ActorRuntime) (targetCapacity : Nat) : Bool`
    #[inline]
    pub fn can_send(&self, target_capacity: usize) -> bool {
        self.pending.len() < target_capacity
    }

    /// Check if mailbox has space
    ///
    /// Corresponds to Lean: `def ActorRuntime.mailbox_has_space (ar : ActorRuntime) : Bool`
    #[inline]
    pub fn mailbox_has_space(&self) -> bool {
        self.mailbox.len() < self.capacity
    }

    /// Enqueue message to pending
    ///
    /// Corresponds to Lean: `def ActorRuntime.enqueue_pending`
    pub fn enqueue_pending(&self, msg: Message) -> Self {
        let mut new_pending = self.pending.clone();
        new_pending.push_back(msg);
        ActorRuntime {
            pending: new_pending,
            mailbox: self.mailbox.clone(),
            capacity: self.capacity,
            blocked_on: self.blocked_on,
            pending_send: self.pending_send.clone(),
        }
    }

    /// Enqueue message to pending (mutating version)
    pub fn enqueue_pending_mut(&mut self, msg: Message) {
        self.pending.push_back(msg);
    }

    /// Deliver message from pending to mailbox
    ///
    /// Corresponds to Lean: `def ActorRuntime.deliver`
    ///
    /// Returns the new state. If pending is empty or mailbox is full,
    /// returns unchanged state.
    pub fn deliver(&self) -> Self {
        // Check if pending is empty
        if self.pending.is_empty() {
            return self.clone();
        }
        // Check if mailbox is full
        if self.mailbox.len() >= self.capacity {
            return self.clone();
        }

        // Dequeue from front of pending, push to mailbox
        let mut new_pending = self.pending.clone();
        let first_msg = new_pending.pop_front().expect("checked non-empty");
        let mut new_mailbox = self.mailbox.clone();
        new_mailbox.push_back(first_msg);

        ActorRuntime {
            pending: new_pending,
            mailbox: new_mailbox,
            capacity: self.capacity,
            blocked_on: self.blocked_on,
            pending_send: self.pending_send.clone(),
        }
    }

    /// Deliver message from pending to mailbox (mutating version)
    ///
    /// Returns Ok(true) if delivered, Ok(false) if mailbox full, Err if no pending.
    ///
    /// # Errors
    ///
    /// Returns `ActorError::NoMessage` if the pending queue is empty.
    pub fn deliver_mut(&mut self) -> Result<bool, ActorError> {
        if self.pending.is_empty() {
            return Err(ActorError::NoMessage);
        }

        if self.mailbox.len() < self.capacity {
            let msg = self.pending.pop_front().expect("checked non-empty");
            self.mailbox.push_back(msg);
            Ok(true)
        } else {
            // Mailbox full, don't modify
            Ok(false)
        }
    }

    /// Consume message from mailbox
    ///
    /// Corresponds to Lean: `def ActorRuntime.consume`
    ///
    /// Returns `(new_state, Option<Message>)`.
    pub fn consume(&self) -> (Self, Option<Message>) {
        if self.mailbox.is_empty() {
            return (self.clone(), None);
        }

        // Get first message
        let mut new_mailbox = self.mailbox.clone();
        let msg = new_mailbox.pop_front().expect("checked non-empty");

        (
            ActorRuntime {
                pending: self.pending.clone(),
                mailbox: new_mailbox,
                capacity: self.capacity,
                blocked_on: self.blocked_on,
                pending_send: self.pending_send.clone(),
            },
            Some(msg),
        )
    }

    /// Consume message from mailbox (mutating version)
    pub fn consume_mut(&mut self) -> Option<Message> {
        self.mailbox.pop_front()
    }

    /// Set blocked state (for deadlock analysis)
    ///
    /// Corresponds to Lean: `def ActorRuntime.set_blocked`
    pub fn set_blocked(&self, on: ActorId) -> Self {
        ActorRuntime {
            pending: self.pending.clone(),
            mailbox: self.mailbox.clone(),
            capacity: self.capacity,
            blocked_on: Some(on),
            pending_send: self.pending_send.clone(),
        }
    }

    /// Set blocked state (mutating version)
    pub fn set_blocked_mut(&mut self, on: ActorId) {
        self.blocked_on = Some(on);
    }

    /// Clear blocked state
    ///
    /// Corresponds to Lean: `def ActorRuntime.unblock`
    pub fn unblock(&self) -> Self {
        ActorRuntime {
            pending: self.pending.clone(),
            mailbox: self.mailbox.clone(),
            capacity: self.capacity,
            blocked_on: None,
            pending_send: self.pending_send.clone(),
        }
    }

    /// Clear blocked state (mutating version)
    pub fn unblock_mut(&mut self) {
        self.blocked_on = None;
    }

    /// Get the blocked-on actor (if any)
    #[inline]
    pub fn blocked_on(&self) -> Option<ActorId> {
        self.blocked_on
    }

    /// Check if actor is blocked
    #[inline]
    pub fn is_blocked(&self) -> bool {
        self.blocked_on.is_some()
    }

    /// Get the capacity
    #[inline]
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Get the pending queue length
    #[inline]
    pub fn pending_len(&self) -> usize {
        self.pending.len()
    }

    /// Get the mailbox length
    #[inline]
    pub fn mailbox_len(&self) -> usize {
        self.mailbox.len()
    }

    /// Get the pending send message
    #[inline]
    pub fn pending_send(&self) -> Option<&Message> {
        self.pending_send.as_ref()
    }

    /// Set the pending send message
    pub fn set_pending_send(&mut self, msg: Option<Message>) {
        self.pending_send = msg;
    }
}

impl Default for ActorRuntime {
    fn default() -> Self {
        ActorRuntime::empty(0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_test_message(id: MsgId) -> Message {
        Message::new(id, 1, 2, SecurityLevel::Public, vec![1, 2, 3])
    }

    #[test]
    fn test_actor_runtime_empty() {
        let ar = ActorRuntime::empty(10);
        assert_eq!(ar.capacity(), 10);
        assert_eq!(ar.pending_len(), 0);
        assert_eq!(ar.mailbox_len(), 0);
        assert!(!ar.can_receive());
        assert!(!ar.is_blocked());
    }

    #[test]
    fn test_actor_runtime_enqueue_pending() {
        let mut ar = ActorRuntime::empty(10);
        let msg = make_test_message(1);

        ar.enqueue_pending_mut(msg);
        assert_eq!(ar.pending_len(), 1);
    }

    #[test]
    fn test_actor_runtime_enqueue_pending_increases() {
        // Corresponds to Lean theorem: ActorRuntime.enqueue_pending_increases
        let ar = ActorRuntime::empty(10);
        let msg = make_test_message(1);

        let ar2 = ar.enqueue_pending(msg);
        assert_eq!(ar2.pending_len(), ar.pending_len() + 1);
    }

    #[test]
    fn test_actor_runtime_enqueue_pending_preserves_mailbox() {
        // Corresponds to Lean theorem: ActorRuntime.enqueue_pending_preserves_mailbox
        let ar = ActorRuntime::empty(10);
        let msg = make_test_message(1);

        let ar2 = ar.enqueue_pending(msg);
        assert_eq!(ar2.mailbox_len(), ar.mailbox_len());
    }

    #[test]
    fn test_actor_runtime_deliver() {
        let mut ar = ActorRuntime::empty(10);
        let msg = make_test_message(1);

        ar.enqueue_pending_mut(msg);
        assert_eq!(ar.pending_len(), 1);
        assert_eq!(ar.mailbox_len(), 0);

        let result = ar.deliver_mut();
        assert!(matches!(result, Ok(true)));
        assert_eq!(ar.pending_len(), 0);
        assert_eq!(ar.mailbox_len(), 1);
    }

    #[test]
    fn test_actor_runtime_deliver_decreases_pending() {
        // Corresponds to Lean theorem: ActorRuntime.deliver_decreases_pending
        let ar = ActorRuntime::empty(10);
        let msg = make_test_message(1);

        let ar = ar.enqueue_pending(msg);
        let ar2 = ar.deliver();

        assert!(ar2.pending_len() < ar.pending_len());
    }

    #[test]
    fn test_actor_runtime_deliver_increases_mailbox() {
        // Corresponds to Lean theorem: ActorRuntime.deliver_increases_mailbox
        let ar = ActorRuntime::empty(10);
        let msg = make_test_message(1);

        let ar = ar.enqueue_pending(msg);
        let ar2 = ar.deliver();

        assert_eq!(ar2.mailbox_len(), ar.mailbox_len() + 1);
    }

    #[test]
    fn test_actor_runtime_consume() {
        let mut ar = ActorRuntime::empty(10);
        let msg = make_test_message(42);

        ar.enqueue_pending_mut(msg);
        ar.deliver_mut().expect("deliver should succeed");

        let consumed = ar.consume_mut();
        assert!(consumed.is_some());
        assert_eq!(consumed.map(|m| m.id()), Some(42));
        assert_eq!(ar.mailbox_len(), 0);
    }

    #[test]
    fn test_actor_runtime_consume_decreases_mailbox() {
        // Corresponds to Lean theorem: ActorRuntime.consume_decreases_mailbox
        let ar = ActorRuntime::empty(10);
        let msg = make_test_message(1);

        let ar = ar.enqueue_pending(msg);
        let ar = ar.deliver();
        let (ar2, _) = ar.consume();

        assert!(ar2.mailbox_len() < ar.mailbox_len());
    }

    #[test]
    fn test_actor_runtime_consume_preserves_capacity() {
        // Corresponds to Lean theorem: ActorRuntime.consume_preserves_capacity
        let ar = ActorRuntime::empty(10);
        let msg = make_test_message(1);

        let ar = ar.enqueue_pending(msg);
        let ar = ar.deliver();
        let (ar2, _) = ar.consume();

        assert_eq!(ar2.capacity(), ar.capacity());
    }

    #[test]
    fn test_actor_runtime_consume_preserves_blocked_on() {
        // Corresponds to Lean theorem: ActorRuntime.consume_preserves_blockedOn
        let ar = ActorRuntime::empty(10);
        let msg = make_test_message(1);

        let ar = ar.enqueue_pending(msg);
        let ar = ar.deliver();
        let ar = ar.set_blocked(99);
        let (ar2, _) = ar.consume();

        assert_eq!(ar2.blocked_on(), ar.blocked_on());
    }

    #[test]
    fn test_actor_runtime_set_blocked() {
        // Corresponds to Lean theorem: ActorRuntime.set_blocked_sets
        let ar = ActorRuntime::empty(10);
        let ar2 = ar.set_blocked(42);

        assert_eq!(ar2.blocked_on(), Some(42));
    }

    #[test]
    fn test_actor_runtime_set_blocked_preserves_mailbox() {
        // Corresponds to Lean theorem: ActorRuntime.set_blocked_preserves_mailbox
        let ar = ActorRuntime::empty(10);
        let ar2 = ar.set_blocked(42);

        assert_eq!(ar2.mailbox_len(), ar.mailbox_len());
    }

    #[test]
    fn test_actor_runtime_unblock_clears() {
        // Corresponds to Lean theorem: ActorRuntime.unblock_clears
        let ar = ActorRuntime::empty(10);
        let ar = ar.set_blocked(42);
        let ar2 = ar.unblock();

        assert_eq!(ar2.blocked_on(), None);
    }

    #[test]
    fn test_actor_runtime_unblock_preserves_mailbox() {
        // Corresponds to Lean theorem: ActorRuntime.unblock_preserves_mailbox
        let ar = ActorRuntime::empty(10);
        let ar = ar.set_blocked(42);
        let ar2 = ar.unblock();

        assert_eq!(ar2.mailbox_len(), ar.mailbox_len());
    }

    #[test]
    fn test_actor_runtime_mailbox_full() {
        let mut ar = ActorRuntime::empty(2);

        // Fill mailbox
        ar.enqueue_pending_mut(make_test_message(1));
        ar.deliver_mut().expect("deliver 1");
        ar.enqueue_pending_mut(make_test_message(2));
        ar.deliver_mut().expect("deliver 2");

        // Mailbox should be full
        assert!(!ar.mailbox_has_space());
        assert_eq!(ar.mailbox_len(), 2);

        // Try to deliver one more
        ar.enqueue_pending_mut(make_test_message(3));
        let result = ar.deliver_mut();
        assert!(matches!(result, Ok(false))); // Not delivered (full)
    }

    #[test]
    fn test_message_accessors() {
        let msg = Message::new(42, 1, 2, SecurityLevel::Confidential, vec![10, 20, 30]);

        assert_eq!(msg.id(), 42);
        assert_eq!(msg.src(), 1);
        assert_eq!(msg.dst(), 2);
        assert_eq!(msg.level(), SecurityLevel::Confidential);
        assert_eq!(msg.body(), &vec![10, 20, 30]);
    }
}