Skip to main content

sim_expr_tree_calc/calc/
scheduler.rs

1use sim_incremental_core::ContinuationToken;
2
3use super::{CalcLimits, RequestId};
4
5/// Bound applied to one automatic scheduler turn.
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7pub struct AutomaticBudget {
8    /// Maximum queued roots attempted in this turn.
9    pub max_requests: usize,
10    /// Incremental budget applied to each attempted root.
11    pub limits: CalcLimits,
12}
13
14impl AutomaticBudget {
15    /// Builds an explicit automatic-work bound.
16    #[must_use]
17    pub const fn new(max_requests: usize, limits: CalcLimits) -> Self {
18        Self {
19            max_requests,
20            limits,
21        }
22    }
23}
24
25impl Default for AutomaticBudget {
26    fn default() -> Self {
27        Self::new(16, CalcLimits::default())
28    }
29}
30
31/// Opaque explicit continuation for remaining automatic queue work.
32#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
33pub struct AutomaticContinuation {
34    generation: u64,
35}
36
37impl AutomaticContinuation {
38    pub(super) const fn new(generation: u64) -> Self {
39        Self { generation }
40    }
41
42    /// Returns the queue generation represented by this token.
43    #[must_use]
44    pub const fn generation(self) -> u64 {
45        self.generation
46    }
47}
48
49/// One deterministic automatic queue entry suitable for persistence.
50#[derive(Clone, Debug, Eq, PartialEq)]
51pub struct QueuedCalculation {
52    /// Stable automatic request identity.
53    pub request_id: RequestId,
54    /// Canonical cell path.
55    pub cell: String,
56    /// Earliest wall-clock millisecond at which the entry is ready.
57    pub ready_at_ms: u64,
58    /// Effective policy priority.
59    pub priority: i16,
60    /// Stable insertion order.
61    pub sequence: u64,
62    /// Number of ready selections that bypassed this entry.
63    pub bypasses: u8,
64    /// Incremental continuation token retained after budget exhaustion.
65    pub incremental_continuation: Option<ContinuationToken>,
66}
67
68/// Restartable snapshot of the deterministic automatic queue.
69#[derive(Clone, Debug, Eq, PartialEq)]
70pub struct AutomaticQueueSnapshot {
71    /// Queue generation.
72    pub generation: u64,
73    /// Next stable insertion sequence.
74    pub next_sequence: u64,
75    /// Queue entries in canonical cell order.
76    pub entries: Vec<QueuedCalculation>,
77}
78
79/// Evidence returned by one bounded automatic scheduler turn.
80#[derive(Clone, Debug, Eq, PartialEq)]
81pub struct AutomaticRun {
82    /// Requests that reached a terminal success/failure/block outcome.
83    pub completed: Vec<RequestId>,
84    /// Requests that stopped on an incremental budget and remain queued.
85    pub budget_exhausted: Vec<RequestId>,
86    /// Explicit continuation when any queue work remains.
87    pub continuation: Option<AutomaticContinuation>,
88}
89
90pub(super) const MAX_READY_BYPASSES: u8 = 3;