vlo_types/protocol.rs
1//! Inputs and outputs of the eight kernel exports (see `docs/kernel-protocol.md`).
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
6
7use crate::{Cause, EventInput, KernelState, Reduction, RunRef, Status, Transition, Usage};
8
9/// Output of `kernel_version`.
10#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
11pub struct KernelVersion {
12 /// Crate version of the kernel.
13 pub version: String,
14 /// Protocol version; the host refuses a kernel whose value differs from its own.
15 pub protocol: u32,
16 /// Feature names the host may branch on. Additive only.
17 pub features: Vec<String>,
18}
19
20/// Every export returns this shape: `{"ok":true,"value":…}` or `{"ok":false,"error":…}`.
21#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
22pub struct KernelResult<T> {
23 /// `true` when `value` is set, `false` when `error` is.
24 pub ok: bool,
25 /// The export's output when `ok`.
26 #[serde(default, skip_serializing_if = "Option::is_none")]
27 pub value: Option<T>,
28 /// The failure when not `ok`.
29 #[serde(default, skip_serializing_if = "Option::is_none")]
30 pub error: Option<KernelError>,
31}
32
33impl<T> KernelResult<T> {
34 /// Wrap a successful value.
35 pub fn ok(value: T) -> Self {
36 KernelResult {
37 ok: true,
38 value: Some(value),
39 error: None,
40 }
41 }
42 /// Wrap a failure.
43 pub fn err(error: KernelError) -> Self {
44 KernelResult {
45 ok: false,
46 value: None,
47 error: Some(error),
48 }
49 }
50}
51
52/// A kernel failure. `code` is one of the closed set the kernel defines; the host
53/// branches on it, never on `message`.
54#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
55pub struct KernelError {
56 /// Machine-readable code, e.g. `invalid_event`.
57 pub code: String,
58 /// Human-readable detail. Not stable; never branch on it.
59 pub message: String,
60 /// The seq the failure concerns, when it concerns one.
61 #[serde(default, skip_serializing_if = "Option::is_none")]
62 pub seq: Option<u64>,
63 /// The step the failure concerns, when it concerns one.
64 #[serde(default, skip_serializing_if = "Option::is_none")]
65 pub step_id: Option<String>,
66}
67
68/// Output of `validate_event`.
69#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
70pub struct ValidateEventOutput {
71 /// The event after validation; currently a byte-exact copy of the input.
72 pub normalized: EventInput,
73}
74
75/// Input to `prepare_event`.
76#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
77pub struct PrepareEventInput {
78 /// The run row as loaded under the run lock.
79 pub run: RunRef,
80 /// `None` only for the `started` event.
81 #[serde(default, skip_serializing_if = "Option::is_none")]
82 pub kernel_state: Option<KernelState>,
83 /// The current user-state checkpoint, byte-exact. Lifecycle transitions echo it and
84 /// hash it without calling the reducer, so the host must pass it for any event that
85 /// is not `started`.
86 #[serde(default, skip_serializing_if = "Option::is_none")]
87 pub state: Option<String>,
88 /// The event to prepare.
89 pub event: EventInput,
90}
91
92/// What the reducer sees. The kernel translates journal headers into these; the
93/// reducer never sees `seq`, `random` or lifecycle noise except where it needs them.
94///
95/// `usage_total` rides on every variant: it is the run's banked total *including* this
96/// event, so a reducer can decide on a budget without keeping its own tally
97/// (see `docs/workflows.md`).
98#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
99#[serde(tag = "type", rename_all = "snake_case")]
100pub enum ReducerEvent {
101 /// The run's first event, delivered right after `init(input)`.
102 Started {
103 /// Event time in milliseconds since the Unix epoch; the reducer's clock.
104 ts_ms: u64,
105 /// 32 hex chars of journaled randomness; the reducer's only entropy.
106 random: String,
107 /// Workflow input.
108 input: Value,
109 /// Banked usage for the run, including this event.
110 #[serde(default)]
111 usage_total: Usage,
112 },
113 /// A task reached `succeeded` or `failed`.
114 TaskDone {
115 /// Event time in milliseconds since the Unix epoch; the reducer's clock.
116 ts_ms: u64,
117 /// 32 hex chars of journaled randomness; the reducer's only entropy.
118 random: String,
119 /// The step that finished.
120 step_id: String,
121 /// The attempt that finished (1-based).
122 attempt: u32,
123 /// Activity name recorded on the step.
124 activity: String,
125 /// `succeeded` or `failed`.
126 status: Status,
127 /// Worker output on `succeeded`.
128 #[serde(default, skip_serializing_if = "Option::is_none")]
129 output: Option<Value>,
130 /// Failure cause on `failed`.
131 #[serde(default, skip_serializing_if = "Option::is_none")]
132 cause: Option<Cause>,
133 /// Free-text error detail on `failed`.
134 #[serde(default, skip_serializing_if = "Option::is_none")]
135 error: Option<String>,
136 /// What this attempt itself consumed; zeroed when the worker reported none.
137 #[serde(default)]
138 usage: Usage,
139 /// Banked usage for the run, including this event.
140 #[serde(default)]
141 usage_total: Usage,
142 },
143 /// A signal was delivered to a parked step.
144 Signal {
145 /// Event time in milliseconds since the Unix epoch; the reducer's clock.
146 ts_ms: u64,
147 /// 32 hex chars of journaled randomness; the reducer's only entropy.
148 random: String,
149 /// The step that was waiting.
150 step_id: String,
151 /// Signal name recorded on the step.
152 name: String,
153 /// Signal payload; JSON `null` when the event carried none.
154 payload: Value,
155 /// Banked usage for the run, including this event.
156 #[serde(default)]
157 usage_total: Usage,
158 },
159 /// A timer fired.
160 Timer {
161 /// Event time in milliseconds since the Unix epoch; the reducer's clock.
162 ts_ms: u64,
163 /// 32 hex chars of journaled randomness; the reducer's only entropy.
164 random: String,
165 /// The timer step.
166 step_id: String,
167 /// Banked usage for the run, including this event.
168 #[serde(default)]
169 usage_total: Usage,
170 },
171 /// A child run reached a terminal status.
172 ChildDone {
173 /// Event time in milliseconds since the Unix epoch; the reducer's clock.
174 ts_ms: u64,
175 /// 32 hex chars of journaled randomness; the reducer's only entropy.
176 random: String,
177 /// The parent's `spawn` step.
178 step_id: String,
179 /// The child run.
180 child_run_id: String,
181 /// `succeeded`, `failed` or `cancelled`.
182 status: Status,
183 /// Child output, when the event carried a payload.
184 #[serde(default, skip_serializing_if = "Option::is_none")]
185 output: Option<Value>,
186 /// Banked usage for the run, including this event.
187 #[serde(default)]
188 usage_total: Usage,
189 },
190}
191
192/// Output of `prepare_event`: what the host does next.
193#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
194#[serde(tag = "mode", rename_all = "snake_case")]
195pub enum Prepared {
196 /// Call the reducer. For `started`, `init_input` is set: call `init(input)` first,
197 /// then `on.started`.
198 Reduce {
199 /// The event to hand to the reducer.
200 reducer_event: ReducerEvent,
201 /// Workflow input for `init`; set only for `started`.
202 #[serde(default, skip_serializing_if = "Option::is_none")]
203 init_input: Option<Value>,
204 /// The kernel state to pass back into `reduce_step` (fresh for `started`).
205 kernel_state: KernelState,
206 },
207 /// Pure bookkeeping (task `running`, `retrying`, `cancelled`, …). No reducer call;
208 /// apply the transition as-is. `state` in it echoes the current checkpoint.
209 Lifecycle {
210 /// The write-set to apply as-is.
211 transition: Transition,
212 },
213}
214
215/// Input to `reduce_step`.
216#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
217pub struct ReduceStepInput {
218 /// The run row as loaded under the run lock.
219 pub run: RunRef,
220 /// The kernel state `prepare_event` returned.
221 pub kernel_state: KernelState,
222 /// The trigger event, as passed to `prepare_event`.
223 pub event: EventInput,
224 /// What the reducer returned.
225 pub reduction: Reduction,
226}
227
228/// Input to `compute_state_hash`.
229#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
230pub struct StateHashInput {
231 /// Kernel state to hash.
232 pub kernel_state: KernelState,
233 /// User state, byte-exact JSON.
234 pub state: String,
235}
236
237/// Output of `compute_state_hash`.
238#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
239pub struct StateHashOutput {
240 /// Lowercase hex sha256 (see `docs/kernel-protocol.md`).
241 pub hash: String,
242}
243
244/// A `(seq, state_hash)` pair, from the journal or from a re-fold.
245#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
246pub struct HashAtSeq {
247 /// Sequence number.
248 pub seq: u64,
249 /// State hash after that seq.
250 pub hash: String,
251}
252
253/// Input to `refold_check`.
254#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
255pub struct RefoldCheckInput {
256 /// Hashes as journaled, ascending by seq.
257 pub expected: Vec<HashAtSeq>,
258 /// Hashes the re-fold produced, ascending by seq.
259 pub actual: Vec<HashAtSeq>,
260}
261
262/// `matches: false` carries the first divergent seq and both hashes.
263#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
264pub struct RefoldCheckOutput {
265 /// Whether every pair agreed.
266 pub matches: bool,
267 /// The first divergent seq.
268 #[serde(default, skip_serializing_if = "Option::is_none")]
269 pub seq: Option<u64>,
270 /// Journaled hash at `seq`; empty when the journal has no entry there.
271 #[serde(default, skip_serializing_if = "Option::is_none")]
272 pub expected: Option<String>,
273 /// Re-folded hash at `seq`; empty when the re-fold produced none.
274 #[serde(default, skip_serializing_if = "Option::is_none")]
275 pub actual: Option<String>,
276}
277
278/// Input to `derive_ids`.
279#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
280pub struct DeriveIdsInput {
281 /// Run identifier.
282 pub run_id: String,
283 /// Step identifier.
284 pub step_id: String,
285 /// Attempt for the idempotency key.
286 pub attempt: u32,
287}
288
289/// Output of `derive_ids`.
290#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
291pub struct DeriveIdsOutput {
292 /// `<run_id>:<step_id>`.
293 pub task_id: String,
294 /// `<run_id>:<step_id>:<attempt>`.
295 pub idempotency_key: String,
296 /// `run_` followed by the first 26 hex chars of `sha256(task_id)`.
297 pub child_run_id: String,
298}