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
//! The event stream: what an operation does, as JSON values an embedder shows or a command line
//! prints one per line (`isothrum … --json`). It evolves from Isoloom's lifecycle events (the
//! `event` tag, `step`/`log`/`check`/`result`), adding the run, the operation, a sequence number,
//! the time on every event, and its own version `v`, independent of the manifest's.
//!
//! Rules: a secret's value never appears (masked before the event is built); `log` lines are
//! decoded lossily; an operation ends with exactly one `result` or `error`.
mod emitter;
use std::io::Write;
use std::sync::{Mutex, PoisonError};
use isoloom_manifest::OutputRole;
use serde::{Deserialize, Serialize};
pub(crate) use emitter::Emitter;
use crate::error::ErrorCode;
use crate::state::{Op, RunState};
/// The event stream's version.
pub const EVENTS_VERSION: u32 = 0;
/// Where a step stands.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum StepState {
/// Started.
Running,
/// Finished and succeeded.
Done,
/// Finished and failed (an `error` follows unless the step's failure is tolerated).
Failed,
/// Not run: an earlier step failed.
Skipped,
}
/// Which output of a tool a line came from.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Stream {
/// Its standard output.
Stdout,
/// Its standard error.
Stderr,
}
/// Where a ready condition stands.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ReadyState {
/// Not yet; polled again.
Waiting,
/// It holds.
Ok,
/// It didn't hold in time.
Failed,
}
/// What happened.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(tag = "event", rename_all = "snake_case")]
#[non_exhaustive]
pub enum EventKind {
/// The run moved from one state to another.
State {
/// Before.
from: RunState,
/// After.
to: RunState,
},
/// A step started, finished, failed or was skipped.
Step {
/// The step's id.
step: String,
/// Where it stands.
state: StepState,
/// The manifest's line for it, when it starts.
#[serde(default, skip_serializing_if = "Option::is_none")]
note: Option<String>,
},
/// A line a step's tool printed.
Log {
/// The step's id.
step: String,
/// Which output.
stream: Stream,
/// The line, without its line break.
line: String,
},
/// A ready condition was checked.
Ready {
/// The condition (`step_ok up`).
condition: String,
/// Where it stands.
state: ReadyState,
/// What was seen.
detail: String,
},
/// An output's value, once the run is up.
Output {
/// The output's name.
name: String,
/// What it is for.
role: OutputRole,
/// Its value (`***` when sensitive), or null when it can't be read.
value: Option<String>,
/// Whether the value is sensitive.
sensitive: bool,
},
/// One check's result.
Check {
/// The check runner's id.
runner: String,
/// Where it ran from.
from: String,
/// The check's name.
name: String,
/// Whether it passed.
ok: bool,
/// Why it failed (empty when it passed).
detail: String,
},
/// Something the embedder may show, that didn't fail the operation.
Warning {
/// What kind.
code: WarningCode,
/// The text.
message: String,
},
/// The operation's outcome, when it ran to its end.
Result {
/// Whether it reached its goal (false: a check failed).
ok: bool,
/// What it found or produced.
data: serde_json::Value,
},
/// Why the operation failed.
Error {
/// The stable code.
code: ErrorCode,
/// The message.
message: String,
/// The step that failed, when one did.
#[serde(default, skip_serializing_if = "Option::is_none")]
step: Option<String>,
},
}
/// One event of an operation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
#[non_exhaustive]
pub struct Event {
/// The stream's version ([`EVENTS_VERSION`]).
pub v: u32,
/// Its place in the operation, from 1.
pub seq: u64,
/// When (`YYYY-MM-DDTHH:MM:SSZ`, UTC).
pub time: String,
/// The run's name.
pub run: String,
/// The operation.
pub op: Op,
/// What happened.
#[serde(flatten)]
pub kind: EventKind,
}
/// Where an operation's events go. Implemented for closures (`|e: &Event| …`).
pub trait Events: Send + Sync {
/// Receives one event.
fn emit(&self, event: &Event);
}
impl<F: Fn(&Event) + Send + Sync> Events for F {
fn emit(&self, event: &Event) {
self(event);
}
}
/// Events written as JSON, one per line, flushed each time.
#[derive(Debug)]
pub struct JsonLines<W>(Mutex<W>);
impl<W: Write + Send> JsonLines<W> {
/// Writes to `w`.
pub fn new(w: W) -> JsonLines<W> {
JsonLines(Mutex::new(w))
}
}
impl<W: Write + Send> Events for JsonLines<W> {
fn emit(&self, event: &Event) {
let mut w = self.0.lock().unwrap_or_else(PoisonError::into_inner);
let written = serde_json::to_string(event)
.map_err(std::io::Error::from)
.and_then(|text| writeln!(w, "{text}"))
.and_then(|()| w.flush());
// A closed output (a pipe whose reader left) mustn't stop the operation: the run's own log
// keeps every event.
if let Err(e) = written {
tracing::debug!(error = %e, "an event wasn't written");
}
}
}
/// What kind of warning a `warning` event is, the same in the Rust API and the event stream.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum WarningCode {
/// A step whose failure the manifest tolerates failed.
StepTolerated,
/// An embedder's hook failed.
HookFailed,
/// A check runner failed or ran out of time: its checks that didn't report fail.
CheckRunnerFailed,
/// The clean-up of a check runner that ran out of time failed.
CheckCleanupFailed,
/// What an earlier run left is removed before a start.
LeftoversRemoved,
/// A VM (or its folder) an earlier run left is removed.
OrphanRemoved,
/// The lab last ran on another Vagrant provider: that run is cleared first.
OtherProvider,
/// A VM's saved state was cut off: it boots fresh.
SavedStateDiscarded,
/// A start failed on stale state: cleared, then started again.
Recovering,
/// An apply failed and is tried again.
Retrying,
}
/// Events kept in memory, for an embedder that reads them later (and for tests).
#[derive(Debug, Default)]
pub struct Recorder(Mutex<Vec<Event>>);
impl Recorder {
/// An empty recorder.
#[must_use]
pub fn new() -> Recorder {
Recorder::default()
}
/// Every event so far, in order.
pub fn events(&self) -> Vec<Event> {
self.0.lock().unwrap_or_else(PoisonError::into_inner).clone()
}
}
impl Events for Recorder {
fn emit(&self, event: &Event) {
self.0.lock().unwrap_or_else(PoisonError::into_inner).push(event.clone());
}
}
/// The JSON Schema of one event.
#[must_use]
pub fn events_schema() -> serde_json::Value {
let mut schema = serde_json::to_value(schemars::schema_for!(Event)).unwrap_or_default();
if let Some(fields) = schema.as_object_mut() {
fields.insert("title".to_owned(), serde_json::json!("Isothrum runner event, version 0"));
}
schema
}
#[cfg(test)]
mod tests {
use super::*;
fn event(kind: EventKind) -> Event {
Event {
v: 0,
seq: 1,
time: "2026-10-15T12:00:00Z".into(),
run: "flag-portal".into(),
op: Op::Up,
kind,
}
}
#[test]
fn an_event_has_the_documented_shape() {
let e = event(EventKind::Step {
step: "up".into(),
state: StepState::Running,
note: Some("Starting…".into()),
});
assert_eq!(
serde_json::to_value(&e).unwrap(),
serde_json::json!({"v":0,"seq":1,"time":"2026-10-15T12:00:00Z","run":"flag-portal","op":"up","event":"step","step":"up","state":"running","note":"Starting…"})
);
let error = event(EventKind::Error {
code: ErrorCode::PortInUse,
message: "m".into(),
step: None,
});
let back: Event = serde_json::from_value(serde_json::to_value(&error).unwrap()).unwrap();
assert_eq!(back, error);
}
#[test]
fn json_lines_are_one_event_per_line() {
let sink = JsonLines::new(Vec::new());
sink.emit(&event(EventKind::State {
from: RunState::Absent,
to: RunState::Starting,
}));
sink.emit(&event(EventKind::Result {
ok: true,
data: serde_json::json!({}),
}));
let text = String::from_utf8(sink.0.into_inner().unwrap()).unwrap();
let lines: Vec<Event> = text.lines().map(|l| serde_json::from_str(l).unwrap()).collect();
assert_eq!(lines.len(), 2);
}
#[test]
fn the_schema_names_every_event_kind() {
let schema = events_schema().to_string();
for kind in ["state", "step", "log", "ready", "output", "check", "warning", "result", "error"] {
assert!(schema.contains(&format!("\"{kind}\"")), "{kind}");
}
}
}