Skip to main content

vlo_types/
kernel_state.rs

1//! Kernel-owned per-run bookkeeping, hashed together with the user state.
2
3use std::collections::BTreeMap;
4
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7
8use crate::{Kind, RetryPolicy, Status, Usage};
9
10/// Run-level status (the `runs.status` column).
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
12#[serde(rename_all = "snake_case")]
13pub enum RunStatus {
14    /// Row exists, `started` not yet folded (child runs and continue-as-new go through the outbox).
15    Created,
16    /// `started` folded; steps may be open.
17    Running,
18    /// Every open step is a signal wait: nothing progresses until a signal arrives.
19    AwaitingSignal,
20    /// Terminal: the reducer issued `complete`.
21    Completed,
22    /// Terminal: the reducer threw, nondeterminism was detected, or the host failed the run.
23    Failed,
24    /// Terminal: the user cancelled the run.
25    Cancelled,
26    /// Terminal: the reducer issued `continue_as_new`; a successor run carries on.
27    Continued,
28}
29
30impl RunStatus {
31    /// `true` for `completed`, `failed`, `cancelled` and `continued`.
32    pub fn is_terminal(self) -> bool {
33        matches!(
34            self,
35            RunStatus::Completed | RunStatus::Failed | RunStatus::Cancelled | RunStatus::Continued
36        )
37    }
38}
39
40/// Kernel bookkeeping for one open step.
41#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
42pub struct StepState {
43    /// Step kind.
44    pub kind: Kind,
45    /// Current step status.
46    pub status: Status,
47    /// Current attempt (1-based for tasks, `0` for every other kind).
48    pub attempt: u32,
49    /// Activity name (tasks only).
50    #[serde(default, skip_serializing_if = "Option::is_none")]
51    pub activity: Option<String>,
52    /// Queue name (tasks only).
53    #[serde(default, skip_serializing_if = "Option::is_none")]
54    pub queue: Option<String>,
55    /// Retry policy (tasks only).
56    #[serde(default, skip_serializing_if = "Option::is_none")]
57    pub retry: Option<RetryPolicy>,
58    /// Child run id (children only).
59    #[serde(default, skip_serializing_if = "Option::is_none")]
60    pub child_run_id: Option<String>,
61    /// Signal name (signal waits only).
62    #[serde(default, skip_serializing_if = "Option::is_none")]
63    pub signal: Option<String>,
64    /// Fire time in milliseconds since the Unix epoch (timers only).
65    #[serde(default, skip_serializing_if = "Option::is_none")]
66    pub fire_at_ms: Option<u64>,
67}
68
69/// Kernel-owned state persisted beside the user's state and included in the state hash.
70/// Terminal steps are pruned deterministically (`closed_steps` keeps the count) so the
71/// structure stays bounded on long runs.
72#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
73pub struct KernelState {
74    /// Format version of this structure; currently `1`.
75    pub v: u32,
76    /// Run-level status as the kernel derives it from the open steps.
77    pub status: RunStatus,
78    /// Monotonic counter used to derive default step ids.
79    pub step_no: u64,
80    /// Open steps keyed by step id. Terminal steps are removed, not kept.
81    pub steps: BTreeMap<String, StepState>,
82    /// Number of steps pruned from `steps` so far.
83    #[serde(default)]
84    pub closed_steps: u64,
85    /// Usage banked across every event of the run, retried attempts included.
86    #[serde(default)]
87    pub usage_total: Usage,
88}
89
90impl KernelState {
91    /// A fresh state: `v: 1`, status `created`, no steps, zero usage.
92    pub fn new() -> Self {
93        KernelState {
94            v: 1,
95            status: RunStatus::Created,
96            step_no: 0,
97            steps: BTreeMap::new(),
98            closed_steps: 0,
99            usage_total: Usage::default(),
100        }
101    }
102}
103
104impl Default for KernelState {
105    fn default() -> Self {
106        Self::new()
107    }
108}