nmbrs_runtime/lib.rs
1// Copyright 2024-2026 Jonathan Shook
2// SPDX-License-Identifier: Apache-2.0
3
4//! # nmbrs-runtime
5//!
6//! Workload execution runtime for nmbrs. Owns the async dispatch
7//! loop, the adapter trait that workload backends implement, op
8//! sequencing across stanzas, error-handler integration, observer
9//! callbacks, and the runner that ties everything together.
10//!
11//! This is the integration crate — it depends on every other
12//! `nb-*` library and is depended on by `nmbrs` (and by every
13//! persona binary). External consumers shouldn't usually need to
14//! reach into it directly; the public-facing path is `nmbrs run`
15//! or [`runner::run`].
16//!
17//! ## Pieces
18//!
19//! - [`adapter::DriverAdapter`] — the trait every workload
20//! backend implements. CQL, HTTP, stdout, testkit, plotter, and
21//! user-supplied adapters all register via the inventory
22//! pattern in `adapters`.
23//! - [`activity::Activity`] — one running concurrency unit. Owns
24//! the cycle source, the op sequencer, the fiber pool, the
25//! error router, and the metrics scope. Multiple activities
26//! can run concurrently within one phase.
27//! - [`runner::run`] / [`runner::run_with_observer`] — orchestrate
28//! the whole session: parse
29//! workload, build component tree, route metrics, walk the
30//! scenario tree, supervise activities.
31//! - [`scope_tree`] / [`scene_tree`] — the canonical scenario-
32//! tree shape and the runtime presentation surface (SRD 18b).
33//! - `scheduler` — `schedule=` CLI param parses to a
34//! `scheduler::ScheduleSpec`; the `scheduler::TreeScheduler`
35//! walks the tree and forks concurrent siblings via
36//! `tokio::JoinSet` + `Semaphore` based on per-level limits.
37//! - [`observer::RunObserver`] — lifecycle callbacks (phase
38//! start / progress / complete / fail). The TUI is one
39//! implementor; stderr is the default.
40//! - [`bindings`] / [`scope`] — workload bindings → Polydat Kernel
41//! compilation, with cache-and-rebind across phase iterations.
42//!
43//! ## Out of scope
44//!
45//! - Polydat DSL parsing and compilation: see [`polydat`].
46//! - Workload YAML parsing: see [`nmbrs_workload`].
47//! - Component tree, instruments, cadence reporter: see
48//! [`nmbrs_metrics`].
49//! - Rate limiting: see [`nmbrs_rate`].
50//! - Error handler primitives: see [`nmbrs_errorhandler`].
51//!
52//! ## See also
53//!
54//! - SRD 29 (`docs/SRD/29_execution_engine.md`) — the engine
55//! front door: this crate's public contract surface, the
56//! load-bearing axioms, and the SRD ↔ module map.
57//! - SRD 01 (`docs/SRD/01_system_overview.md`) — overall
58//! architecture.
59//! - SRD 18b — scenario-tree, scope-tree, scheduler.
60//! - SRD 22 (`docs/SRD/22_op_sequencing.md`) — op sequencing
61//! and stanza model.
62//! - SRD 30 (`docs/SRD/30_adapter_interface.md`) — adapter
63//! trait surface.
64
65// Polydat node registrations that need nmbrs-runtime's runtime
66// services (component tree + controls + fiber context). Moved out
67// of polydat itself so polydat can publish standalone — see
68// `polydat_nodes/mod.rs` for the rationale.
69pub mod activity;
70pub mod adapter;
71pub(crate) mod adapters;
72/// SRD-115 §4.8 — the admission check: every row a predicated search
73/// returns must be a row its predicate admits.
74pub(crate) mod admission;
75pub mod bindings;
76pub mod checkpoint;
77/// SRD-92 / ExecUnification Step 5a — the unified child-stream contract
78/// (`ChildSource` + `Realizability`). Additive; callers land in 5b+.
79pub(crate) mod child_source;
80mod comprehension_checks;
81pub mod concurrent;
82pub mod control_catalog;
83pub(crate) mod daemon_pool;
84/// Boolean `NMBRS_DEBUG_*` developer switches.
85pub mod debug_env;
86pub(crate) mod describe;
87pub(crate) mod error_policy;
88pub mod exec_events;
89pub mod execution_context;
90pub(crate) mod executor;
91pub mod fiber_engine;
92pub(crate) mod fiber_pool;
93pub mod fixture;
94/// Lifecycle event vocabulary: the kind-tag [`lifecycle::EventType`]
95/// and its [`lifecycle::SubjectKind`], shared by the readout
96/// binder and the checkpoint log.
97pub mod lifecycle;
98pub mod log_sink;
99pub mod observer;
100pub mod op_cells;
101pub mod op_modifier;
102pub mod opseq;
103/// SRD-86 — the optimizer service boundary: the `Optimizer`/`Objective`
104/// contract + registry the phase-execution driver uses, defined here in the
105/// core with no dependency on any algorithm crate. Public so algorithm crates
106/// (e.g. `nmbrs-optimizers`) can register against it and the CLI can discover.
107pub mod optimize;
108pub mod output_channel;
109pub(crate) mod params;
110/// Phase-end trigger registry — content-agnostic callbacks
111/// that fire after every phase completion or failure. Used by
112/// the `watch=plots` / `watch=report` CLI flags to keep an
113/// external view (plot image, report html) up-to-date as the
114/// run progresses.
115pub mod phase_end_triggers;
116pub(crate) mod phase_filter;
117/// SRD-76 phase outcome disposition (structured
118/// per-phase status + error list).
119pub mod phase_outcome;
120/// SRD-71 P3 phase-scoped CLI parameter overrides
121/// (`<phase-pattern>.<param>=<value>`).
122pub(crate) mod phase_params;
123pub mod polydat_nodes;
124pub(crate) mod profiler;
125pub(crate) mod readout_context;
126pub mod readouts;
127/// SRD-77 refine plan — pre-computed skip set + next-execution
128/// id, derived from a session's prior `phase_outcomes` rows.
129/// The runner builds one when `nmbrs refine` re-attaches to an
130/// existing session; the executor's phase-walk gate checks it
131/// before dispatching each phase's per-cycle work.
132pub mod refine_plan;
133pub(crate) mod relevancy;
134pub mod resource_pool;
135pub mod runner;
136pub mod scene_tree;
137pub(crate) mod scheduler;
138pub mod scope;
139pub(crate) mod scope_elision;
140pub mod scope_kernel;
141pub mod scope_synth;
142pub mod scope_tree;
143pub mod session;
144pub mod session_signals;
145pub(crate) mod stop_conditions;
146pub mod suggest;
147pub mod synthesis;
148pub mod sysmon;
149pub mod throttle;
150pub mod timeval;
151pub(crate) mod trace_router;
152pub mod validation;
153pub mod wires;
154pub mod workload_lint;
155pub(crate) mod workload_shell;
156pub(crate) mod wrapper_registrations;
157pub mod wrapper_registry;
158pub mod wrapper_resolver;
159pub mod wrappers;
160/// SRD-100 P2 — the per-phase status builder, re-exported for the display
161/// consumer (nmbrs-tui) that now folds `active_phases` and renders each
162/// phase's status line itself (the module otherwise stays crate-private).
163pub use readout_context::{build_inline_refresh_context, is_internal_counter};
164pub mod report_anchor;
165
166/// A scratch-name suffix unique across parallel tests and processes:
167/// parallel tests can read the same clock tick, so the pid and a
168/// per-process sequence keep their directories apart.
169#[cfg(test)]
170pub(crate) fn scratch_suffix() -> String {
171 use std::sync::atomic::{AtomicU64, Ordering};
172 static SEQ: AtomicU64 = AtomicU64::new(0);
173 let n = std::time::SystemTime::now()
174 .duration_since(std::time::UNIX_EPOCH)
175 .unwrap()
176 .as_nanos();
177 let seq = SEQ.fetch_add(1, Ordering::Relaxed);
178 format!("{}-{n:x}-{seq}", std::process::id())
179}