Skip to main content

vlo_types/
transition.rs

1//! The write-set a transition produces, and the rows it is built from.
2//!
3//! A journal applies a [`Transition`] atomically under the run lock and never interprets
4//! commands itself (see `docs/journal.md`).
5
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8use serde_json::Value;
9
10use crate::{Envelope, KernelState, RetryPolicy, RunStatus, Status};
11
12/// The run row as the host loaded it under the per-run lock.
13#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
14pub struct RunRef {
15    /// Run identifier.
16    pub run_id: String,
17    /// Workflow name the run executes.
18    pub workflow: String,
19    /// Content digest of the workflow bundle the run is pinned to.
20    pub bundle_digest: String,
21    /// Run-level status.
22    pub status: RunStatus,
23    /// Seq of the last journaled event; `0` before `started`.
24    pub last_seq: u64,
25    /// The parent run, when this run was spawned as a child.
26    #[serde(default, skip_serializing_if = "Option::is_none")]
27    pub parent_run_id: Option<String>,
28    /// The parent's `spawn` step, when this run was spawned as a child.
29    #[serde(default, skip_serializing_if = "Option::is_none")]
30    pub parent_step_id: Option<String>,
31}
32
33/// The run-row columns a transition rewrites.
34#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
35pub struct RunPatch {
36    /// New run status.
37    pub status: RunStatus,
38    /// New `last_seq`: the seq of the last event in `Transition::events`.
39    pub last_seq: u64,
40}
41
42/// A task row to insert. It doubles as the outbox entry the host pushes to a worker.
43#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
44pub struct NewTask {
45    /// `<run_id>:<step_id>`.
46    pub task_id: String,
47    /// The step that owns the task.
48    pub step_id: String,
49    /// Always `1` for a new task.
50    pub attempt: u32,
51    /// Activity name the worker dispatches on.
52    pub activity: String,
53    /// Queue the task is pushed to.
54    pub queue: String,
55    /// Input schema reference, when the reducer named one.
56    #[serde(default, skip_serializing_if = "Option::is_none")]
57    pub schema_ref: Option<String>,
58    /// Activity input.
59    pub input: Value,
60    /// `<run_id>:<step_id>:<attempt>`; unique per dispatch attempt.
61    pub idempotency_key: String,
62    /// Retry policy resolved for this task.
63    pub retry: RetryPolicy,
64    /// Lease duration in milliseconds before the reaper may re-dispatch.
65    pub lease_ttl_ms: u64,
66    /// Per-attempt wall-clock budget in milliseconds; `None` means no budget.
67    #[serde(default, skip_serializing_if = "Option::is_none")]
68    pub timeout_ms: Option<u64>,
69}
70
71/// A status change on an existing task row.
72#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
73pub struct TaskUpdate {
74    /// `<run_id>:<step_id>`.
75    pub task_id: String,
76    /// The attempt the update applies to.
77    pub attempt: u32,
78    /// New task status.
79    pub status: Status,
80    /// For `retrying → scheduled`: the earliest dispatch time.
81    #[serde(default, skip_serializing_if = "Option::is_none")]
82    pub not_before_ms: Option<u64>,
83}
84
85/// A timer row to insert.
86#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
87pub struct NewTimer {
88    /// The step that owns the timer.
89    pub step_id: String,
90    /// Absolute fire time in milliseconds since the Unix epoch.
91    pub fire_at_ms: u64,
92}
93
94/// A child run to create.
95#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
96pub struct NewSpawn {
97    /// The parent's step that owns the child.
98    pub step_id: String,
99    /// Deterministic child run id (see `derive_ids`).
100    pub child_run_id: String,
101    /// Workflow name of the child.
102    pub workflow: String,
103    /// Child run input.
104    pub input: Value,
105}
106
107/// An open `await_signal` step the host registers so a matching signal can be delivered.
108#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
109pub struct SignalWait {
110    /// The step that is waiting.
111    pub step_id: String,
112    /// Signal name.
113    pub name: String,
114    /// Payload schema reference, when the reducer named one.
115    #[serde(default, skip_serializing_if = "Option::is_none")]
116    pub schema_ref: Option<String>,
117    /// Absolute expiry in milliseconds since the Unix epoch; `None` waits forever.
118    #[serde(default, skip_serializing_if = "Option::is_none")]
119    pub expires_at_ms: Option<u64>,
120}
121
122/// A side-channel emission. Never journaled.
123#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
124pub struct Emit {
125    /// Channel name.
126    pub channel: String,
127    /// Payload.
128    pub data: Value,
129}
130
131/// The run's terminal outcome.
132#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
133pub struct Completion {
134    /// `completed` or `continued`.
135    pub status: RunStatus,
136    /// Workflow output for `completed`; `None` for `continued`.
137    #[serde(default, skip_serializing_if = "Option::is_none")]
138    pub output: Option<Value>,
139}
140
141/// Input for the successor run when the reducer issued `continue_as_new`.
142#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
143pub struct ContinueAsNew {
144    /// Input for the successor run.
145    pub input: Value,
146}
147
148/// The write-set. Journals apply it atomically under the run lock and never interpret
149/// commands themselves (see `docs/journal.md`).
150#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
151pub struct Transition {
152    /// Run-row patch.
153    pub run: RunPatch,
154    /// Kernel state after the transition.
155    pub kernel_state: KernelState,
156    /// User state, byte-exact JSON. Stored inline when `state_inline`, else offloaded.
157    pub state: String,
158    /// Hash of `(kernel_state, state)` (see `docs/kernel-protocol.md`).
159    pub state_hash: String,
160    /// Whether `state` fits the inline limit (`INLINE_LIMIT_BYTES`).
161    pub state_inline: bool,
162    /// Trigger event first, then lifecycle `scheduled` events for each new task/timer/spawn/wait.
163    pub events: Vec<Envelope>,
164    /// Task rows to insert.
165    #[serde(default)]
166    pub tasks: Vec<NewTask>,
167    /// Status changes to existing task rows.
168    #[serde(default)]
169    pub task_updates: Vec<TaskUpdate>,
170    /// Timer rows to insert.
171    #[serde(default)]
172    pub timers: Vec<NewTimer>,
173    /// Child runs to create.
174    #[serde(default)]
175    pub spawns: Vec<NewSpawn>,
176    /// Signal waits to register.
177    #[serde(default)]
178    pub signal_waits: Vec<SignalWait>,
179    /// Side-channel emissions to publish.
180    #[serde(default)]
181    pub emits: Vec<Emit>,
182    /// Set when the run ended in this transition.
183    #[serde(default, skip_serializing_if = "Option::is_none")]
184    pub completion: Option<Completion>,
185    /// Set when the run continues as a successor run.
186    #[serde(default, skip_serializing_if = "Option::is_none")]
187    pub continue_as_new: Option<ContinueAsNew>,
188}