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;
80pub mod concurrent;
81pub mod control_catalog;
82pub(crate) mod daemon_pool;
83/// Boolean `NMBRS_DEBUG_*` developer switches.
84pub mod debug_env;
85pub(crate) mod describe;
86pub(crate) mod error_policy;
87pub mod exec_events;
88pub mod execution_context;
89pub(crate) mod executor;
90pub mod fiber_engine;
91pub(crate) mod fiber_pool;
92pub mod fixture;
93/// Lifecycle event vocabulary: the kind-tag [`lifecycle::EventType`]
94/// and its [`lifecycle::SubjectKind`], shared by the readout
95/// binder and the checkpoint log.
96pub mod lifecycle;
97pub mod log_sink;
98pub mod observer;
99pub mod op_cells;
100pub mod op_modifier;
101pub mod opseq;
102/// SRD-86 — the optimizer service boundary: the `Optimizer`/`Objective`
103/// contract + registry the phase-execution driver uses, defined here in the
104/// core with no dependency on any algorithm crate. Public so algorithm crates
105/// (e.g. `nmbrs-optimizers`) can register against it and the CLI can discover.
106pub mod optimize;
107pub mod output_channel;
108pub(crate) mod params;
109/// Phase-end trigger registry — content-agnostic callbacks
110/// that fire after every phase completion or failure. Used by
111/// the `watch=plots` / `watch=report` CLI flags to keep an
112/// external view (plot image, report html) up-to-date as the
113/// run progresses.
114pub mod phase_end_triggers;
115pub(crate) mod phase_filter;
116/// SRD-76 phase outcome disposition (structured
117/// per-phase status + error list).
118pub mod phase_outcome;
119/// SRD-71 P3 phase-scoped CLI parameter overrides
120/// (`<phase-pattern>.<param>=<value>`).
121pub(crate) mod phase_params;
122pub mod polydat_nodes;
123pub(crate) mod profiler;
124pub(crate) mod readout_context;
125pub mod readouts;
126/// SRD-77 refine plan — pre-computed skip set + next-execution
127/// id, derived from a session's prior `phase_outcomes` rows.
128/// The runner builds one when `nmbrs refine` re-attaches to an
129/// existing session; the executor's phase-walk gate checks it
130/// before dispatching each phase's per-cycle work.
131pub mod refine_plan;
132pub(crate) mod relevancy;
133pub mod resource_pool;
134pub mod runner;
135pub mod scene_tree;
136pub(crate) mod scheduler;
137pub mod scope;
138pub(crate) mod scope_elision;
139pub mod scope_kernel;
140pub mod scope_synth;
141pub mod scope_tree;
142pub mod session;
143pub mod session_signals;
144pub(crate) mod stop_conditions;
145pub mod suggest;
146pub mod synthesis;
147pub mod sysmon;
148pub mod throttle;
149pub mod timeval;
150pub(crate) mod trace_router;
151pub mod validation;
152pub mod wires;
153pub mod workload_lint;
154pub(crate) mod workload_shell;
155pub(crate) mod wrapper_registrations;
156pub mod wrapper_registry;
157pub mod wrapper_resolver;
158pub mod wrappers;
159/// SRD-100 P2 — the per-phase status builder, re-exported for the display
160/// consumer (nmbrs-tui) that now folds `active_phases` and renders each
161/// phase's status line itself (the module otherwise stays crate-private).
162pub use readout_context::{build_inline_refresh_context, is_internal_counter};
163pub mod report_anchor;
164
165/// A scratch-name suffix unique across parallel tests and processes:
166/// parallel tests can read the same clock tick, so the pid and a
167/// per-process sequence keep their directories apart.
168#[cfg(test)]
169pub(crate) fn scratch_suffix() -> String {
170 use std::sync::atomic::{AtomicU64, Ordering};
171 static SEQ: AtomicU64 = AtomicU64::new(0);
172 let n = std::time::SystemTime::now()
173 .duration_since(std::time::UNIX_EPOCH)
174 .unwrap()
175 .as_nanos();
176 let seq = SEQ.fetch_add(1, Ordering::Relaxed);
177 format!("{}-{n:x}-{seq}", std::process::id())
178}