Skip to main content

nmbrs_runtime/
lifecycle.rs

1// Copyright 2024-2026 Jonathan Shook
2// SPDX-License-Identifier: Apache-2.0
3
4//! Lifecycle event vocabulary — the kind-tag
5//! [`EventType`](crate::lifecycle::EventType)
6//! plus its [`SubjectKind`](crate::lifecycle::SubjectKind).
7//!
8//! [`EventType`](crate::lifecycle::EventType) names the lifecycle
9//! slot that triggered a render (`on_phase_end`, `on_update`, …);
10//! [`SubjectKind`](crate::lifecycle::SubjectKind)
11//! names what kind of subject the firing context represents
12//! (session / phase / iteration / scope). Together they are
13//! the shared lifecycle vocabulary used by the readout binder
14//! (to validate and dispatch readouts) and by the checkpoint
15//! log (to tie a durable data record to its lifecycle kind-tag).
16//!
17//! See SRD-63 §4.1. Two kinds of events exist:
18//!
19//! - **Lifecycle events** — fire exactly once per
20//!   `(slot, subject)`. `_start` and `_end` are
21//!   delaminated; nothing fires twice for the same
22//!   subject under one slot.
23//! - **Refresh events** — fire repeatedly while the
24//!   subject is in flight. Currently only
25//!   [`EventType::Update`](crate::lifecycle::EventType::Update).
26//!
27//! Push 2 wires `Update` and `PhaseEnd` through the activity
28//! pipeline; the remaining variants are reachable from Push 3
29//! onward as the workload-side `readouts:` parser maps slot
30//! names to events and Push 4 wires wildcard binding to
31//! cover scope and session lifecycles.
32
33#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
34pub enum EventType {
35    /// `on_session_start` — the run is opening.
36    SessionStart,
37    /// `on_session_end` — the run is closing. Final render
38    /// for any session-scoped readout.
39    SessionEnd,
40
41    /// `on_phase_start` — a phase is opening. Equivalent to
42    /// today's deleted phase-starting log row when the
43    /// `phase_starting` readout is bound.
44    PhaseStart,
45    /// `on_phase_end` — a phase is closing. The ✓ DONE line
46    /// (`phase_outcome` readout) is bound here today.
47    PhaseEnd,
48
49    /// `on_each_start` — a `for_each` / `for_combinations`
50    /// iteration is opening. The current scope-ancestor
51    /// header (`· for_each profile=…`) becomes the
52    /// `scope_header` readout bound here in Push 3.
53    EachStart,
54    /// `on_each_end` — an iteration is closing.
55    EachEnd,
56
57    /// `on_scope_start` — a non-iteration scope group is
58    /// opening.
59    ScopeStart,
60    /// `on_scope_end` — a non-iteration scope group is
61    /// closing.
62    ScopeEnd,
63
64    /// `on_update` — periodic refresh tick. Today's inline
65    /// progress thread fires this at 0.5 s; the TUI fires
66    /// it per-frame. Drives the live status content.
67    Update,
68}
69
70impl EventType {
71    /// Lower-snake-case name matching the `readouts:` slot
72    /// keyword (`on_update`, `on_phase_end`, …). Used by
73    /// the `trace` diagnostic readout and by Push 3's
74    /// workload-block parser.
75    pub fn slot_name(self) -> &'static str {
76        match self {
77            EventType::SessionStart => "on_session_start",
78            EventType::SessionEnd => "on_session_end",
79            EventType::PhaseStart => "on_phase_start",
80            EventType::PhaseEnd => "on_phase_end",
81            EventType::EachStart => "on_each_start",
82            EventType::EachEnd => "on_each_end",
83            EventType::ScopeStart => "on_scope_start",
84            EventType::ScopeEnd => "on_scope_end",
85            EventType::Update => "on_update",
86        }
87    }
88
89    /// What kind of subject the context that fires this
90    /// event represents. Used by the binder to validate at
91    /// bind-time that bound readouts accept the slot's
92    /// subject kind. `Update` rides on the surrounding
93    /// phase, so it reports `Phase`.
94    pub fn subject_kind(self) -> SubjectKind {
95        match self {
96            EventType::SessionStart | EventType::SessionEnd => SubjectKind::Session,
97            EventType::PhaseStart | EventType::PhaseEnd | EventType::Update => SubjectKind::Phase,
98            EventType::EachStart | EventType::EachEnd => SubjectKind::Iteration,
99            EventType::ScopeStart | EventType::ScopeEnd => SubjectKind::Scope,
100        }
101    }
102}
103
104/// What kind of subject a context (and a render) is
105/// scoped to. Determined by the firing event and the
106/// surface that built the context. Builtins declare
107/// which kinds they accept; the binder validates at
108/// bake-time.
109#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
110pub enum SubjectKind {
111    /// The whole run. Used for `on_session_start` /
112    /// `on_session_end`.
113    Session,
114    /// A single phase activity. Used for `on_phase_start` /
115    /// `on_phase_end` / `on_update`.
116    Phase,
117    /// One iteration of a `for_each` / `for_combinations`
118    /// scope. Used for `on_each_start` / `on_each_end`.
119    Iteration,
120    /// A non-iteration scope group (`do_while` /
121    /// `do_until`). Used for `on_scope_start` /
122    /// `on_scope_end`.
123    Scope,
124}
125
126impl SubjectKind {
127    /// Lower-snake-case name for the storage / replay
128    /// surface. Stored in the `readout_snapshots.subject_kind`
129    /// column so `nmbrs replay` can group rows by subject.
130    pub fn as_str(self) -> &'static str {
131        match self {
132            SubjectKind::Session => "session",
133            SubjectKind::Phase => "phase",
134            SubjectKind::Iteration => "iteration",
135            SubjectKind::Scope => "scope",
136        }
137    }
138}