limen-core 0.1.0-alpha.1

Limen core contracts and primitives.
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
//! Policies for batching, budgets, deadlines, admission, and capacities.

use crate::types::{DeadlineNs, QoSClass, Ticks};

/// Configuration for a sliding window.
///
/// A sliding window produces overlapping (or non-overlapping) windows of `size` items,
/// advancing by `stride` items per step.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SlidingWindow {
    /// Number of items the window start advances between consecutive windows.
    stride: usize,
}

impl SlidingWindow {
    /// Creates a new sliding window configuration.
    ///
    /// `stride` is how many items the window start advances between consecutive windows.
    #[must_use]
    pub fn new(stride: usize) -> Self {
        Self { stride }
    }

    /// Returns how many items the window start advances between consecutive windows.
    #[must_use]
    pub fn stride(&self) -> &usize {
        &self.stride
    }
}

/// How batches are formed over an input stream.
///
/// The window policy controls how items from a stream are grouped into `Window`s.
///
/// - [`WindowKind::Disjoint`] (default): non-overlapping windows; each window consumes
///   `size` items from the stream and the next window begins immediately after.
/// - [`WindowKind::Sliding`]: fixed-size windows that advance by a fixed step. Each
///   produced window contains `size` items, and the next window starts `stride` items
///   after the previous window start. When `stride == size`, this is equivalent to
///   [`WindowKind::Disjoint`].
///
/// For [`WindowKind::Sliding`], `stride <= size` is expected.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WindowKind {
    /// Non-overlapping (disjoint) windows; default.
    Disjoint,
    /// Sliding windows: each produced window has `size` items and windows advance
    /// by `stride` items between steps.
    Sliding(SlidingWindow),
}

/// Batch formation policy: fixed-N and/or Δt micro-batching.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BatchingPolicy {
    /// Fixed number of items per batch (>= 1). If `None`, do not use fixed-N.
    fixed_n: Option<usize>,

    /// Maximum micro-batching window in ticks (Δt). If `None`, no Δt cap.
    max_delta_t: Option<Ticks>,

    /// Windowing style: disjoint (default) or sliding (opt-in).
    window_kind: WindowKind,
}

impl BatchingPolicy {
    /// No batching (batch size 1).
    pub const fn none() -> Self {
        Self {
            fixed_n: Some(1),
            max_delta_t: None,
            window_kind: WindowKind::Disjoint,
        }
    }

    /// Fixed-N batching.
    pub const fn fixed(n: usize) -> Self {
        Self {
            fixed_n: Some(n),
            max_delta_t: None,
            window_kind: WindowKind::Disjoint,
        }
    }

    /// Δt-bounded micro-batching (runtime may still emit batch < N if time cap hits).
    pub const fn delta_t(cap: Ticks) -> Self {
        Self {
            fixed_n: None,
            max_delta_t: Some(cap),
            window_kind: WindowKind::Disjoint,
        }
    }

    /// Combined fixed-N and Δt caps.
    pub const fn fixed_and_delta_t(n: usize, cap: Ticks) -> Self {
        Self {
            fixed_n: Some(n),
            max_delta_t: Some(cap),
            window_kind: WindowKind::Disjoint,
        }
    }

    /// Combined fixed-N and Δt caps with explicit window kind (e.g., sliding).
    #[inline]
    pub const fn with_window(
        n: Option<usize>,
        cap: Option<Ticks>,
        window_kind: WindowKind,
    ) -> Self {
        Self {
            fixed_n: n,
            max_delta_t: cap,
            window_kind,
        }
    }

    /// Convenience: fixed-N with explicit window kind.
    #[inline]
    pub const fn fixed_with_window(n: usize, window_kind: WindowKind) -> Self {
        Self {
            fixed_n: Some(n),
            max_delta_t: None,
            window_kind,
        }
    }

    /// Convenience: delta-t with explicit window kind.
    #[inline]
    pub const fn delta_t_with_window(cap: Ticks, window_kind: WindowKind) -> Self {
        Self {
            fixed_n: None,
            max_delta_t: Some(cap),
            window_kind,
        }
    }

    /// Borrow the fixed-N batching value (if any).
    #[inline]
    pub const fn fixed_n(&self) -> &Option<usize> {
        &self.fixed_n
    }

    /// Borrow the delta-t cap (if any).
    #[inline]
    pub const fn max_delta_t(&self) -> &Option<Ticks> {
        &self.max_delta_t
    }

    /// Return the window kind.
    #[inline]
    pub const fn window_kind(&self) -> WindowKind {
        self.window_kind
    }
}

impl Default for BatchingPolicy {
    fn default() -> Self {
        BatchingPolicy::none()
    }
}

/// Budget policy for node execution.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct BudgetPolicy {
    /// Per-step tick budget; exceeding invokes over-budget action.
    tick_budget: Option<Ticks>,
    /// *Hard* guardrail for a single step. If exceeded, the runtime may call
    /// `Node::on_watchdog_timeout` and/or apply `OverBudgetAction`.
    ///
    /// Keep in no_std: only requires a monotonic clock; no OS timers.
    watchdog_ticks: Option<Ticks>,
}

impl BudgetPolicy {
    /// Construct a new budget policy.
    pub const fn new(tick_budget: Option<Ticks>, watchdog_ticks: Option<Ticks>) -> Self {
        Self {
            tick_budget,
            watchdog_ticks,
        }
    }

    /// Borrow the tick budget.
    #[inline]
    pub const fn tick_budget(&self) -> &Option<Ticks> {
        &self.tick_budget
    }

    /// Borrow the watchdog ticks.
    #[inline]
    pub const fn watchdog_ticks(&self) -> &Option<Ticks> {
        &self.watchdog_ticks
    }
}

/// Deadline policy for messages processed by a node.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct DeadlinePolicy {
    /// Whether to require absolute deadlines on inputs (P2).
    require_absolute_deadline: bool,
    /// Optional slack tolerance (ns) before defaulting/degrading.
    slack_tolerance_ns: Option<DeadlineNs>,
    /// Synthesize a deadline when inputs have none: absolute_deadline = now + value.
    /// Leave `None` to avoid synthesizing (strict mode or non-EDF).
    default_deadline_ns: Option<DeadlineNs>,
}

impl DeadlinePolicy {
    /// Construct a deadline policy.
    pub const fn new(
        require_absolute_deadline: bool,
        slack_tolerance_ns: Option<DeadlineNs>,
        default_deadline_ns: Option<DeadlineNs>,
    ) -> Self {
        Self {
            require_absolute_deadline,
            slack_tolerance_ns,
            default_deadline_ns,
        }
    }

    /// Borrow the require_absolute_deadline bool.
    #[inline]
    pub const fn require_absolute_deadline(&self) -> bool {
        self.require_absolute_deadline
    }

    /// Borrow the slack_tolerance_ns.
    #[inline]
    pub const fn slack_tolerance_ns(&self) -> &Option<DeadlineNs> {
        &self.slack_tolerance_ns
    }

    /// Borrow the default_deadline_ns.
    #[inline]
    pub const fn default_deadline_ns(&self) -> &Option<DeadlineNs> {
        &self.default_deadline_ns
    }
}

/// Action to take when budgets or deadlines are breached.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OverBudgetAction {
    /// Drop the message(s).
    Drop,
    /// Skip the stage and signal upstream (if applicable).
    SkipStage,
    /// Degrade to a faster path (e.g., quantized model).
    Degrade,
    /// Emit a default value.
    DefaultOnTimeout,
}

/// Queue capacity and watermark configuration.
///
/// `soft_*` define backpressure **watermarks**; `max_*` define **hard caps**.
/// Watermark state is derived from live occupancy snapshots and drives scheduling/backpressure.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct QueueCaps {
    /// Maximum number of items permitted (hard cap).
    pub max_items: usize,
    /// Soft watermark for items; admission adapts between soft and hard.
    pub soft_items: usize,
    /// Optional byte cap and soft watermark for payload sizes.
    pub max_bytes: Option<usize>,
    /// Optional soft watermark in bytes.
    pub soft_bytes: Option<usize>,
}

impl QueueCaps {
    /// Construct new queue caps.
    pub const fn new(
        max_items: usize,
        soft_items: usize,
        max_bytes: Option<usize>,
        soft_bytes: Option<usize>,
    ) -> Self {
        Self {
            max_items,
            soft_items,
            max_bytes,
            soft_bytes,
        }
    }

    /// Borrow max_items.
    #[inline]
    pub const fn max_items(&self) -> &usize {
        &self.max_items
    }

    /// Borrow soft_items.
    #[inline]
    pub const fn soft_items(&self) -> &usize {
        &self.soft_items
    }

    /// Borrow max_bytes.
    #[inline]
    pub const fn max_bytes(&self) -> &Option<usize> {
        &self.max_bytes
    }

    /// Borrow soft_bytes.
    #[inline]
    pub const fn soft_bytes(&self) -> &Option<usize> {
        &self.soft_bytes
    }

    /// Return `true` if the given occupancy is below soft watermarks.
    pub fn below_soft(&self, items: usize, bytes: usize) -> bool {
        let items_ok = items < self.soft_items;
        let bytes_ok = match (self.max_bytes, self.soft_bytes) {
            (Some(_), Some(soft)) => bytes < soft,
            _ => true,
        };
        items_ok && bytes_ok
    }

    /// Return `true` if the occupancy is at or above hard cap.
    pub fn at_or_above_hard(&self, items: usize, bytes: usize) -> bool {
        if items >= self.max_items {
            return true;
        }
        if let Some(maxb) = self.max_bytes {
            if bytes >= maxb {
                return true;
            }
        }
        false
    }
}

/// Watermark state derived from queue occupancy and caps.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum WatermarkState {
    /// Well below soft watermarks.
    BelowSoft,
    /// Between soft and hard thresholds.
    BetweenSoftAndHard,
    /// At or above hard cap.
    AtOrAboveHard,
}

/// Admission behavior when the queue is not clearly below soft caps.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AdmissionPolicy {
    /// Deadline-aware and QoS-aware admission between soft and hard caps.
    DeadlineAndQoSAware,
    /// Simple drop-newest under pressure.
    DropNewest,
    /// Simple drop-oldest under pressure.
    DropOldest,
    /// Block producer until space is available (P2 only).
    Block,
}

/// Decision returned by an admission controller (`EdgePolicy::decide`); pure and side-effect free.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AdmissionDecision {
    /// Accept the incoming item without evicting anything.
    Admit,

    /// Refuse the incoming item.
    Reject,

    /// Drop the incoming (newest) item; queue unchanged.
    DropNewest,

    /// Evict `n` oldest items, then admit (caller performs evictions).
    /// `n` can be 1 for BetweenSoftAndHard with DropOldest.
    Evict(usize),

    /// Evict oldest items until the queue is below the hard watermark,
    /// then admit if possible. Caller performs repeated evictions.
    EvictUntilBelowHard,

    /// Block the producer until space becomes available (edge decides how to block).
    Block,
}

/// Per-edge policy bundle.
///
/// `caps` → soft/hard watermarks; `admission` → behavior between soft/hard;
/// `over_budget` → action when capacity/budget constraints are breached.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EdgePolicy {
    /// Capacity and watermarks.
    pub caps: QueueCaps,
    /// Admission behavior between soft and hard thresholds.
    pub admission: AdmissionPolicy,
    /// Action to take on over-budget scenarios at this edge.
    pub over_budget: OverBudgetAction,
}

impl EdgePolicy {
    /// Construct a new `EdgePolicy`.
    pub const fn new(
        caps: QueueCaps,
        admission: AdmissionPolicy,
        over_budget: OverBudgetAction,
    ) -> Self {
        Self {
            caps,
            admission,
            over_budget,
        }
    }

    /// Borrow caps.
    #[inline]
    pub const fn caps(&self) -> &QueueCaps {
        &self.caps
    }

    /// Borrow admission policy.
    #[inline]
    pub const fn admission(&self) -> &AdmissionPolicy {
        &self.admission
    }

    /// Borrow over-budget action.
    #[inline]
    pub const fn over_budget(&self) -> &OverBudgetAction {
        &self.over_budget
    }

    /// Compute a watermark state from occupancy stats.
    pub fn watermark(&self, items: usize, bytes: usize) -> WatermarkState {
        if self.caps.at_or_above_hard(items, bytes) {
            WatermarkState::AtOrAboveHard
        } else if self.caps.below_soft(items, bytes) {
            WatermarkState::BelowSoft
        } else {
            WatermarkState::BetweenSoftAndHard
        }
    }

    /// Apply admission logic based on header hints (deadline/qos) and occupancy.
    ///
    /// # Behaviour
    ///
    /// - `BelowSoft`  => `Admit`.
    /// - `BetweenSoftAndHard`:
    ///     - `DeadlineAndQoSAware` => consult deadline/qos to decide (TODO: full impl).
    ///     - `DropNewest` => `DropNewest`.
    ///     - `DropOldest` => `Evict(1)`.
    ///     - `Block` => `Block`.
    /// - `AtOrAboveHard`:
    ///     - If the *single* incoming item cannot fit under the hard cap (even when
    ///       the queue is empty), the item is `Reject`ed immediately.
    ///     - Otherwise:
    ///         - `DropNewest` => `DropNewest`.
    ///         - `DropOldest` => `EvictUntilBelowHard`.
    ///         - `DeadlineAndQoSAware` => conservative default `Reject` (or consult
    ///           deadline/qos if implemented).
    ///         - `Block` => `Block`.
    ///
    /// # Warning
    ///
    /// QoS-aware behaviour is **not yet implemented**.  The `DeadlineAndQoSAware`
    /// branch currently uses conservative defaults:
    /// - between soft/hard it defaults to `Admit`, and
    /// - at-or-above-hard it defaults to `Reject`.
    ///
    /// Implementors should update this method to use deadline and QoS hints
    /// when the scheduler rules are available.
    pub fn decide(
        &self,
        items: usize,
        bytes: usize,
        item_bytes: usize,
        _deadline: Option<DeadlineNs>,
        _qos: QoSClass,
    ) -> AdmissionDecision {
        match self.watermark(items, bytes) {
            WatermarkState::BelowSoft => AdmissionDecision::Admit,

            WatermarkState::BetweenSoftAndHard => match self.admission {
                AdmissionPolicy::DeadlineAndQoSAware => {
                    // TODO: implement full deadline/qos logic. For now, admit.
                    AdmissionDecision::Admit
                }
                AdmissionPolicy::DropNewest => AdmissionDecision::DropNewest,
                AdmissionPolicy::DropOldest => AdmissionDecision::Evict(1),
                AdmissionPolicy::Block => AdmissionDecision::Block,
            },

            WatermarkState::AtOrAboveHard => {
                // If item alone cannot fit under hard caps, reject immediately.
                if self.caps.at_or_above_hard(0, item_bytes) {
                    return AdmissionDecision::Reject;
                }

                match self.admission {
                    AdmissionPolicy::DeadlineAndQoSAware => {
                        // Conservative default: reject at or above hard.
                        // Or evaluate deadline/qos here and map to DropOldest/DropNewest.
                        AdmissionDecision::Reject
                    }
                    AdmissionPolicy::DropNewest => AdmissionDecision::DropNewest,
                    AdmissionPolicy::DropOldest => AdmissionDecision::EvictUntilBelowHard,
                    AdmissionPolicy::Block => AdmissionDecision::Block,
                }
            }
        }
    }
}

/// Policy bundle attached to a node.
///
/// Used by schedulers:
/// - `batching.fixed_n`/`max_delta_t` guide batch formation.
/// - `budget.tick_budget` (soft) and `budget.watchdog_ticks` (hard) guide time budgeting.
/// - `deadline.default_deadline_ns` allows EDF synthesis when inputs have no deadlines,
///   `deadline.slack_tolerance_ns` provides grace, and `require_absolute_deadline` enforces strictness.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct NodePolicy {
    /// Batch formation policy.
    batching: BatchingPolicy,
    /// Budget policy for execution steps.
    budget: BudgetPolicy,
    /// Deadline policy for inputs/outputs.
    deadline: DeadlinePolicy,
}

impl NodePolicy {
    /// Construct a `NodePolicy` explicitly.
    pub const fn new(
        batching: BatchingPolicy,
        budget: BudgetPolicy,
        deadline: DeadlinePolicy,
    ) -> Self {
        Self {
            batching,
            budget,
            deadline,
        }
    }

    /// Borrow the batching policy.
    #[inline]
    pub const fn batching(&self) -> &BatchingPolicy {
        &self.batching
    }

    /// Borrow the budget policy.
    #[inline]
    pub const fn budget(&self) -> &BudgetPolicy {
        &self.budget
    }

    /// Borrow the deadline policy.
    #[inline]
    pub const fn deadline(&self) -> &DeadlinePolicy {
        &self.deadline
    }
}