relux-runtime 0.7.1

Internal: runtime for Relux. No semver guarantees.
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use std::collections::HashMap;
use std::time::Duration;

use serde::Deserialize;
use serde::Serialize;
use ts_rs::TS;

use super::SourceLocation;
use super::span::SpanId;

pub type EventSeq = u64;

/// Structured representation of an effective timeout (the `IrTimeout` value
/// that bounded a wait or was installed by a `timeout` statement). Pre-formatted
/// with humantime so consumers never do duration arithmetic.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
#[cfg_attr(
    feature = "ts-export",
    ts(export, export_to = "../../../viewer/src/types/")
)]
#[serde(tag = "type", rename_all = "kebab-case")]
pub enum TimeoutValue {
    Tolerance {
        duration: String,
        multiplier: String,
        total_duration: String,
        source: Option<SourceLocation>,
    },
    Assertion {
        duration: String,
        source: Option<SourceLocation>,
    },
}

/// Per-pattern descriptor for a `MultiMatch*` event. Carries the pattern's
/// source text and whether it is a regex or literal. The matched substring
/// and offsets live on the corresponding `BufferEventKind::Matched` event
/// referenced by `MultiMatchPatternDone.buffer_seq` - they are not
/// duplicated here.
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[cfg_attr(
    feature = "ts-export",
    ts(export, export_to = "../../../viewer/src/types/")
)]
pub struct MultiMatchPattern {
    pub pattern: String,
    pub is_regex: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[cfg_attr(
    feature = "ts-export",
    ts(export, export_to = "../../../viewer/src/types/")
)]
pub struct Event {
    pub seq: EventSeq,
    #[serde(with = "super::ts_duration_ms")]
    #[ts(as = "f64")]
    pub ts: Duration,
    pub span: SpanId,
    pub shell: Option<String>,
    /// Stable identity for the shell, when present. Present iff
    /// `shell` is present. Viewers index by marker; `shell` is the
    /// display name at emit time (qualified post-export, bare pre).
    pub shell_marker: Option<String>,
    /// Source byte range that produced this event, when one is in
    /// scope at the emit site. Resolves against `StructuredLog.sources`.
    pub source: Option<SourceLocation>,
    #[serde(flatten)]
    pub kind: EventKind,
}

#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[cfg_attr(
    feature = "ts-export",
    ts(export, export_to = "../../../viewer/src/types/")
)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum EventKind {
    // Shell lifecycle
    ShellSpawn {
        name: String,
        command: String,
    },
    ShellReady {
        name: String,
    },
    ShellSwitch {
        name: String,
    },
    ShellTerminate {
        name: String,
    },

    // Effect exposes - emitted at the end of effect setup, one per
    // expose decl. Hidden from the viewer timeline; surfaced as inline
    // props on the owning effect-setup span.
    EffectExposeShell {
        /// Caller-visible name (the rename target, or the source name
        /// when no `as <name>`).
        name: String,
        /// Source name in the local scope: a local shell key, or an
        /// imported dep's exposed-shell key.
        target: String,
        /// `Some(alias)` when re-exposing from a dependency
        /// (`expose shell <alias>.<target> as <name>`).
        qualifier: Option<String>,
    },
    EffectExposeVar {
        name: String,
        target: String,
        qualifier: Option<String>,
        /// Resolved value at expose time.
        value: String,
    },

    // I/O
    Send {
        data: String,
    },
    Recv {
        data: String,
    },

    // Matching - buffer_seq references the corresponding buffer_events entry.
    MatchStart {
        pattern: String,
        is_regex: bool,
        /// The timeout that bounds this wait.
        effective: TimeoutValue,
    },
    MatchDone {
        matched: String,
        #[serde(with = "super::ts_duration_ms")]
        #[ts(as = "f64")]
        elapsed: Duration,
        captures: Option<HashMap<String, String>>,
        buffer_seq: EventSeq,
    },
    Timeout {
        pattern: String,
        /// `None` when no buffer event corresponds (the failure record's
        /// `buffer_tail` is canonical for the timeout state).
        buffer_seq: Option<EventSeq>,
        /// The timeout that fired.
        effective: TimeoutValue,
    },

    // Fail patterns
    FailPatternSet {
        pattern: String,
        is_regex: bool,
    },
    FailPatternCleared,
    FailPatternTriggered {
        pattern: String,
        is_regex: bool,
        matched_line: String,
        /// `None` for fail-pattern hits - they observe without advancing the
        /// cursor, so no `Matched` buffer event corresponds.
        buffer_seq: Option<EventSeq>,
    },

    // Control flow
    SleepStart {
        #[serde(with = "super::ts_duration_ms")]
        #[ts(as = "f64")]
        duration: Duration,
    },
    SleepDone,
    TimeoutSet {
        timeout: TimeoutValue,
        previous: TimeoutValue,
    },

    // Values
    VarLet {
        name: String,
        value: String,
    },
    VarAssign {
        name: String,
        value: String,
        previous: String,
    },
    StringEval {
        result: String,
    },
    Interpolation {
        template: String,
        result: String,
        bindings: Vec<(String, String)>,
    },
    /// Pure string-match. Today: marker `expr ? ^pat$` conditions.
    /// `result` is the matched substring (`$0`) or `""` on no match.
    /// `captures` mirrors shell-buffer match captures.
    PureMatch {
        match_kind: super::span::MatchKind,
        value: String,
        pattern: String,
        result: String,
        captures: HashMap<String, String>,
    },
    /// Pure variable read: a bare-var expression resolved against the
    /// active scope or environment. `value` is the resolved string
    /// (`""` when the variable is undefined). Read counterpart to
    /// `var-let` / `var-assign`.
    VarRead {
        name: String,
        value: String,
    },
    /// Final truthy/falsy evaluation of a marker condition. Carries
    /// the shape-specific payload (Unconditional / Bare / Eq / Regex)
    /// and the `met` outcome that determined the marker's decision.
    /// Emitted as the last event inside a `marker-eval` span.
    BoolCheck {
        evaluation: super::span::MarkerEvalDetail,
    },

    // Diagnostics
    Annotate {
        text: String,
    },
    Log {
        message: String,
    },
    Warning {
        message: String,
    },
    Error {
        message: String,
    },

    // External interruption observed by the VM. Tagged with the reason
    // (test-timeout, suite-timeout, fail-fast, sigint). Emitted on the
    // span the VM was in when it noticed `cancel.is_cancelled()`.
    Cancelled {
        reason: CancelReasonRecord,
    },

    // --- Multimatch (R014) -------------------------------
    //
    // `<{ ?... =... }` and timed variants record this set of events:
    //   Success: MultiMatchStart + N MultiMatchPatternDone (in completion
    //            order, not source order) + MultiMatchDone.
    //   Timeout: MultiMatchStart + 0..N MultiMatchPatternDone +
    //            MultiMatchTimeout.
    //   Fail-pattern abort: MultiMatchStart + 0..N MultiMatchPatternDone,
    //            then the existing FailPatternTriggered path takes over.
    MultiMatchStart {
        /// The block-level timeout that bounds this wait.
        effective: TimeoutValue,
        /// All patterns in source order. Indices into this vec are
        /// referenced by `MultiMatchPatternDone.index` and
        /// `MultiMatchTimeout.unmatched`.
        patterns: Vec<MultiMatchPattern>,
    },
    MultiMatchPatternDone {
        /// Index into `MultiMatchStart.patterns`.
        index: usize,
        /// Time since block entry (not test start).
        #[serde(with = "super::ts_duration_ms")]
        #[ts(as = "f64")]
        elapsed: Duration,
        /// `EventSeq` of the per-pattern `BufferEventKind::Matched` event
        /// emitted under the same buf lock as this pattern's success.
        /// Single source of truth for matched text and absolute offsets.
        buffer_seq: EventSeq,
    },
    MultiMatchDone {
        /// `EventSeq` of the per-pattern `Matched` whose match ends
        /// farthest in the buffer. The viewer applies the block-end
        /// cursor advance by `len(before) + len(matched)` of this event.
        advance_to: EventSeq,
    },
    MultiMatchTimeout {
        /// Indices into `MultiMatchStart.patterns` that did not match
        /// before the block timeout fired.
        unmatched: Vec<usize>,
    },
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
#[cfg_attr(
    feature = "ts-export",
    ts(export, export_to = "../../../viewer/src/types/")
)]
#[serde(tag = "type", rename_all = "kebab-case")]
pub enum CancelReasonRecord {
    TestTimeout { duration_ms: u64 },
    SuiteTimeout { duration_ms: u64 },
    FailFast { trigger_test: String },
    Sigint,
}

impl CancelReasonRecord {
    pub fn tag(&self) -> &'static str {
        match self {
            Self::TestTimeout { .. } => "test-timeout",
            Self::SuiteTimeout { .. } => "suite-timeout",
            Self::FailFast { .. } => "fail-fast",
            Self::Sigint => "sigint",
        }
    }
}

impl From<&crate::cancel::CancelReason> for CancelReasonRecord {
    fn from(r: &crate::cancel::CancelReason) -> Self {
        use crate::cancel::CancelReason;
        match r {
            CancelReason::TestTimeout { duration } => Self::TestTimeout {
                duration_ms: duration.as_millis() as u64,
            },
            CancelReason::SuiteTimeout { duration } => Self::SuiteTimeout {
                duration_ms: duration.as_millis() as u64,
            },
            CancelReason::FailFast { trigger_test } => Self::FailFast {
                trigger_test: trigger_test.clone(),
            },
            CancelReason::Sigint => Self::Sigint,
        }
    }
}

#[cfg(test)]
mod pure_match_tests {
    use super::*;
    use std::collections::HashMap;

    #[test]
    fn pure_match_event_kind_serialises() {
        let mut caps = HashMap::new();
        caps.insert("0".to_string(), "abc".to_string());
        let k = EventKind::PureMatch {
            match_kind: super::super::span::MatchKind::Regex,
            value: "abc".into(),
            pattern: "^a.c$".into(),
            result: "abc".into(),
            captures: caps,
        };
        let v = serde_json::to_value(&k).unwrap();
        assert_eq!(v["kind"], serde_json::json!("pure-match"));
        assert_eq!(v["match_kind"], serde_json::json!("regex"));
        assert_eq!(v["result"], serde_json::json!("abc"));
    }

    #[test]
    fn multimatch_start_event_kind_serialises() {
        let k = EventKind::MultiMatchStart {
            effective: TimeoutValue::Assertion {
                duration: "5s".into(),
                source: None,
            },
            patterns: vec![
                MultiMatchPattern {
                    pattern: "^ok$".into(),
                    is_regex: true,
                },
                MultiMatchPattern {
                    pattern: "batch complete".into(),
                    is_regex: false,
                },
            ],
        };
        let v = serde_json::to_value(&k).unwrap();
        assert_eq!(v["kind"], serde_json::json!("multi-match-start"));
        assert_eq!(v["patterns"][0]["is_regex"], serde_json::json!(true));
        assert_eq!(
            v["patterns"][1]["pattern"],
            serde_json::json!("batch complete")
        );
    }

    #[test]
    fn multimatch_pattern_done_event_kind_serialises() {
        let k = EventKind::MultiMatchPatternDone {
            index: 1,
            elapsed: Duration::from_millis(42),
            buffer_seq: 17,
        };
        let v = serde_json::to_value(&k).unwrap();
        assert_eq!(v["kind"], serde_json::json!("multi-match-pattern-done"));
        assert_eq!(v["index"], serde_json::json!(1));
        assert_eq!(v["buffer_seq"], serde_json::json!(17));
    }

    #[test]
    fn multimatch_done_event_kind_serialises() {
        let k = EventKind::MultiMatchDone { advance_to: 23 };
        let v = serde_json::to_value(&k).unwrap();
        assert_eq!(v["kind"], serde_json::json!("multi-match-done"));
        assert_eq!(v["advance_to"], serde_json::json!(23));
    }

    #[test]
    fn multimatch_timeout_event_kind_serialises() {
        let k = EventKind::MultiMatchTimeout {
            unmatched: vec![0, 2],
        };
        let v = serde_json::to_value(&k).unwrap();
        assert_eq!(v["kind"], serde_json::json!("multi-match-timeout"));
        assert_eq!(v["unmatched"], serde_json::json!([0, 2]));
    }
}