nmbrs-runtime 0.3.0

Workload execution runtime for nmbrs
Documentation
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0

//! `phase_summary` — the post-run summary line readout.
//!
//! Renders the `[ok] [N/total] name duration` form the
//! TUI observer's post-run roll-up emits today. Push 8
//! ships the readout itself; the live observer keeps its
//! direct format for now (Push 8b wires it through the
//! readout engine alongside the rest of the post-run
//! integration).
//!
//! Available so workloads can bind it via the `readouts:`
//! block — e.g. as an alternate `on_phase_end` body that
//! emits the bracket form instead of the ✓ form:
//!
//! ```yaml
//! readouts:
//!   on_phase_end: phase_summary
//! ```

use std::fmt::Write as _;

use crate::lifecycle::SubjectKind;
use crate::readouts::buf::ReadoutBuf;
use crate::readouts::context::{LifecycleState, ReadoutContext};
use crate::readouts::readout::{ContentMode, Lod, Readout, ReadoutOptions};

pub struct PhaseSummary;

impl Readout for PhaseSummary {
    fn name(&self) -> &'static str {
        "phase_summary"
    }
    fn accepts(&self) -> &'static [SubjectKind] {
        &[SubjectKind::Phase]
    }

    fn render(
        &self,
        ctx: &dyn ReadoutContext,
        lod: Lod,
        mode: ContentMode,
        opts: &ReadoutOptions,
        out: &mut dyn ReadoutBuf,
    ) -> usize {
        // Push 8 surface: Labeled / Value only. The
        // explanation overlay falls through to a
        // descriptor; other LODs stub to zero bytes.
        match (lod, mode) {
            (Lod::Labeled, ContentMode::Value) => render_labeled_value(ctx, opts, out),
            (Lod::Labeled, ContentMode::Explanation) => render_labeled_explanation(ctx, out),
            _ => 0,
        }
    }
}

fn render_labeled_value(
    ctx: &dyn ReadoutContext,
    opts: &ReadoutOptions,
    out: &mut dyn ReadoutBuf,
) -> usize {
    // Color palette per docs/guide/color_style.md:
    //   [ok] → OK (green), [!!] → ERROR (red),
    //   [..] → INFO (sky), [  ] → MUTED (dim),
    //   phase name → bold INFO, duration → MUTED.
    let color = ctx.use_color();
    let bold = if color { "\x1b[1m" } else { "" };
    let dim = if color { "\x1b[2m" } else { "" };
    let yellow = if color { "\x1b[33m" } else { "" };
    let blue = if color { "\x1b[34m" } else { "" };
    let green = if color { "\x1b[32m" } else { "" };
    let red = if color { "\x1b[1;31m" } else { "" };
    let reset = if color { "\x1b[0m" } else { "" };

    // Status marker mirrors the TUI observer's existing
    // bracket vocabulary — same characters so the
    // post-run summary reads the same whether it routed
    // through the legacy direct emit or through the
    // engine. (Push 8b's whole point.)
    let (marker, suffix) = match ctx.subject_state() {
        LifecycleState::Completed => (format!("{green}[ok]{reset}"), String::new()),
        LifecycleState::Running => (
            format!("{blue}[..]{reset}"),
            format!(" {dim}(still running){reset}"),
        ),
        LifecycleState::Pending => (
            format!("{dim}[  ]{reset}"),
            format!(" {dim}(not run){reset}"),
        ),
        LifecycleState::Failed(err) => (format!("{red}[!!]{reset}"), format!(" ({err})")),
    };
    let seq_part: String = match ctx.subject_seq() {
        Some((s, t)) => format!("{dim}[{s}/{t}]{reset} "),
        None => String::new(),
    };
    let depth_indent = ctx.depth_indent();
    let phase_name = ctx.subject_name();
    let labels = ctx.subject_labels();
    // Push 9e: `show_labels=true` opts into rendering the
    // iter-tuple coord-path inline as ` ({labels})` between
    // the name and the duration / suffix. Default off — the
    // post-run summary's primary row hides them since the
    // scope-header rows above already carry the coords.
    // The failed-phase inset sets it on so each failure
    // line is self-contained without scrolling.
    let show_labels = opts.get_bool("show_labels").unwrap_or(false);
    let labels_part = if labels.is_empty() || !show_labels {
        String::new()
    } else {
        format!(" {bold}{yellow}({labels}){reset}")
    };
    let elapsed = ctx.elapsed_secs();
    let dur_part = if elapsed > 0.0 {
        format!(" {dim}{elapsed:.2}s{reset}")
    } else {
        String::new()
    };

    // Memo row: phase displays surface the latest published memo
    // as `[[ <memo> ]]` — a detail row BELOW the bracket-form
    // header (SRD-92 header-first composition). Empty memo → no
    // row. Bold yellow per the style guide's EMPHASIS tier.
    let memo = ctx.phase_memo();
    let memo_row = if memo.is_empty() {
        String::new()
    } else {
        let bold_yellow = if color { "\x1b[1;33m" } else { "" };
        format!("\n{depth_indent}    {bold_yellow}[[ {memo} ]]{reset}")
    };

    let mut tmp = String::with_capacity(160);
    let _ = write!(
        &mut tmp,
        "{depth_indent}{marker} {seq_part}{bold}{blue}{phase_name}{reset}{labels_part}{dur_part}{suffix}{memo_row}",
    );
    let len = tmp.len();
    let _ = out.write_str(&tmp);
    len
}

fn render_labeled_explanation(_ctx: &dyn ReadoutContext, out: &mut dyn ReadoutBuf) -> usize {
    let s = "[ok|!!] [idx/total] phase-name (elapsed)";
    let _ = out.write_str(s);
    s.len()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::readouts::OptionValue;
    use crate::readouts::buf::StringBuf;

    struct TestCtx {
        phase_name: String,
        phase_seq: Option<(usize, usize)>,
        phase_labels: String,
        elapsed_secs: f64,
        depth_indent: String,
        state: LifecycleState,
    }
    impl TestCtx {
        // Replaces #[derive(Default)] now that LifecycleState
        // requires explicit construction.
        fn defaults() -> Self {
            Self {
                phase_name: String::new(),
                phase_seq: None,
                phase_labels: String::new(),
                elapsed_secs: 0.0,
                depth_indent: String::new(),
                state: LifecycleState::Running,
            }
        }
    }
    impl ReadoutContext for TestCtx {
        fn subject_name(&self) -> &str {
            &self.phase_name
        }
        fn subject_seq(&self) -> Option<(usize, usize)> {
            self.phase_seq
        }
        fn subject_labels(&self) -> &str {
            &self.phase_labels
        }
        fn cycles_completed(&self) -> u64 {
            0
        }
        fn cycles_total(&self) -> u64 {
            0
        }
        fn ops_ok(&self) -> u64 {
            0
        }
        fn errors(&self) -> u64 {
            0
        }
        fn retries(&self) -> u64 {
            0
        }
        fn concurrency(&self) -> usize {
            1
        }
        fn elapsed_secs(&self) -> f64 {
            self.elapsed_secs
        }
        fn consumed(&self) -> u64 {
            0
        }
        fn status_metric_chips(&self) -> String {
            String::new()
        }
        fn depth_indent(&self) -> &str {
            &self.depth_indent
        }
        fn use_color(&self) -> bool {
            false
        }
        fn event(&self) -> crate::lifecycle::EventType {
            crate::lifecycle::EventType::PhaseEnd
        }
        fn subject_state(&self) -> LifecycleState {
            self.state.clone()
        }
    }

    fn render(ctx: &TestCtx) -> String {
        let mut s = String::new();
        let mut buf = StringBuf::new(&mut s);
        PhaseSummary.render(
            ctx,
            Lod::Labeled,
            ContentMode::Value,
            &ReadoutOptions::new(),
            &mut buf,
        );
        s
    }

    #[test]
    fn completed_with_seq_and_duration() {
        let ctx = TestCtx {
            phase_name: "setup".into(),
            phase_seq: Some((1, 2)),
            elapsed_secs: 0.02,
            state: LifecycleState::Completed,
            ..TestCtx::defaults()
        };
        assert_eq!(render(&ctx), "[ok] [1/2] setup 0.02s");
    }

    #[test]
    fn completed_no_seq_no_duration() {
        let ctx = TestCtx {
            phase_name: "x".into(),
            state: LifecycleState::Completed,
            ..TestCtx::defaults()
        };
        assert_eq!(render(&ctx), "[ok] x");
    }

    #[test]
    fn failed_state_emits_double_bang_and_error() {
        let ctx = TestCtx {
            phase_name: "load".into(),
            phase_seq: Some((2, 3)),
            elapsed_secs: 1.0,
            state: LifecycleState::Failed("boom".into()),
            ..TestCtx::defaults()
        };
        assert_eq!(render(&ctx), "[!!] [2/3] load 1.00s (boom)");
    }

    #[test]
    fn pending_state_emits_blank_marker() {
        let ctx = TestCtx {
            phase_name: "verify".into(),
            phase_seq: Some((3, 3)),
            state: LifecycleState::Pending,
            ..TestCtx::defaults()
        };
        assert_eq!(render(&ctx), "[  ] [3/3] verify (not run)");
    }

    #[test]
    fn running_state_emits_double_dot_marker() {
        let ctx = TestCtx {
            phase_name: "run".into(),
            phase_seq: Some((1, 1)),
            elapsed_secs: 0.5,
            state: LifecycleState::Running,
            ..TestCtx::defaults()
        };
        assert_eq!(render(&ctx), "[..] [1/1] run 0.50s (still running)");
    }

    #[test]
    fn explanation_describes_each_field() {
        let ctx = TestCtx::defaults();
        let mut s = String::new();
        let mut buf = StringBuf::new(&mut s);
        let n = PhaseSummary.render(
            &ctx,
            Lod::Labeled,
            ContentMode::Explanation,
            &ReadoutOptions::new(),
            &mut buf,
        );
        assert!(n > 0);
        assert!(s.contains("phase-name"));
        assert!(s.contains("idx/total"));
        assert!(s.contains("elapsed"));
    }

    #[test]
    fn other_lods_are_zero_bytes() {
        let ctx = TestCtx {
            phase_name: "x".into(),
            ..TestCtx::defaults()
        };
        for lod in [Lod::Compact, Lod::Expanded] {
            let mut s = String::new();
            let mut buf = StringBuf::new(&mut s);
            let n = PhaseSummary.render(
                &ctx,
                lod,
                ContentMode::Value,
                &ReadoutOptions::new(),
                &mut buf,
            );
            assert_eq!(n, 0, "{lod:?} should render zero bytes");
        }
    }

    #[test]
    fn colorized_emits_ansi_for_status_and_name() {
        // When use_color is on, the bracket marker, phase
        // name, and duration MUST carry ANSI codes per
        // docs/guide/color_style.md. This is a regression
        // guard — pre-Push the post-run summary went to the
        // surface as plain text even on a TTY.
        struct TestCtxColor;
        impl ReadoutContext for TestCtxColor {
            fn subject_name(&self) -> &str {
                "setup"
            }
            fn subject_seq(&self) -> Option<(usize, usize)> {
                Some((1, 2))
            }
            fn subject_labels(&self) -> &str {
                ""
            }
            fn cycles_completed(&self) -> u64 {
                0
            }
            fn cycles_total(&self) -> u64 {
                0
            }
            fn ops_ok(&self) -> u64 {
                0
            }
            fn errors(&self) -> u64 {
                0
            }
            fn retries(&self) -> u64 {
                0
            }
            fn concurrency(&self) -> usize {
                1
            }
            fn elapsed_secs(&self) -> f64 {
                0.5
            }
            fn consumed(&self) -> u64 {
                0
            }
            fn status_metric_chips(&self) -> String {
                String::new()
            }
            fn depth_indent(&self) -> &str {
                ""
            }
            fn use_color(&self) -> bool {
                true
            }
            fn event(&self) -> crate::lifecycle::EventType {
                crate::lifecycle::EventType::PhaseEnd
            }
            fn subject_state(&self) -> LifecycleState {
                LifecycleState::Completed
            }
        }
        let ctx = TestCtxColor;
        let mut s = String::new();
        let mut buf = StringBuf::new(&mut s);
        PhaseSummary.render(
            &ctx,
            Lod::Labeled,
            ContentMode::Value,
            &ReadoutOptions::new(),
            &mut buf,
        );
        // Green wrapper around the [ok] marker.
        assert!(
            s.contains("\x1b[32m[ok]\x1b[0m"),
            "expected green [ok] marker, got: {s:?}"
        );
        // Bold + blue around the phase name.
        assert!(
            s.contains("\x1b[1m\x1b[34msetup\x1b[0m"),
            "expected bold-blue phase name, got: {s:?}"
        );
        // Dim duration tail.
        assert!(
            s.contains("\x1b[2m0.50s\x1b[0m"),
            "expected dim duration, got: {s:?}"
        );
    }

    #[test]
    fn show_labels_inserts_iter_tuple_between_name_and_dur() {
        // Push 9e: failed-phase inset uses this option to
        // surface the iter-tuple coord-path inline so the
        // failure block in `failures:` is self-contained.
        let ctx = TestCtx {
            phase_name: "ann_query".into(),
            phase_labels: "profile=alpha, k=10".into(),
            elapsed_secs: 0.0,
            state: LifecycleState::Failed("connection lost".into()),
            ..TestCtx::defaults()
        };
        let mut s = String::new();
        let mut buf = StringBuf::new(&mut s);
        let mut opts = ReadoutOptions::new();
        opts.set("show_labels", OptionValue::Bool(true));
        PhaseSummary.render(&ctx, Lod::Labeled, ContentMode::Value, &opts, &mut buf);
        assert_eq!(s, "[!!] ann_query (profile=alpha, k=10) (connection lost)",);
    }

    #[test]
    fn default_omits_labels_even_when_present() {
        // Without `show_labels=true`, labels are dropped —
        // the post-run summary's primary tree carries them
        // in scope-header rows above the phase row, so
        // duplicating them would be noisy.
        let ctx = TestCtx {
            phase_name: "ann_query".into(),
            phase_labels: "profile=alpha".into(),
            state: LifecycleState::Completed,
            ..TestCtx::defaults()
        };
        let mut s = String::new();
        let mut buf = StringBuf::new(&mut s);
        PhaseSummary.render(
            &ctx,
            Lod::Labeled,
            ContentMode::Value,
            &ReadoutOptions::new(),
            &mut buf,
        );
        assert_eq!(s, "[ok] ann_query");
    }
}