nmbrs_runtime/wrapper_registry.rs
1// Copyright 2024-2026 Jonathan Shook
2// SPDX-License-Identifier: Apache-2.0
3
4//! SRD-32a — Op Wrapper Registry.
5//!
6//! Single source of truth for the named wrappers that compose
7//! around an adapter's base dispenser: which op-template fields
8//! each owns, what triggers them, what constraints they declare,
9//! and how they describe their assignment to an op.
10//!
11//! The companion module [`crate::wrapper_resolver`] consumes the
12//! registrations submitted via `inventory::submit!` to compute
13//! the per-op composition order. The wrapper implementations
14//! themselves live in [`crate::wrappers`] and
15//! [`crate::validation`] — this module is data, not code.
16
17use nmbrs_workload::model::{ParsedOp, WorkloadPhase};
18
19/// Stable identifier for a wrapper. Used in workload override
20/// directives, CLI flags, and registry lookups. Wrapped around a
21/// `&'static str` so the value is always interned at the
22/// registration site and lookups are pointer-cheap.
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
24pub struct WrapperName(pub &'static str);
25
26impl WrapperName {
27 pub const fn new(name: &'static str) -> Self {
28 Self(name)
29 }
30
31 pub fn as_str(&self) -> &'static str {
32 self.0
33 }
34}
35
36impl std::fmt::Display for WrapperName {
37 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38 f.write_str(self.0)
39 }
40}
41
42/// SRD-92 / ExecUnification — the execution-graph level(s) a wrapper is legal
43/// at. Today every wrapper is op-level; the unified scaffold (Step 5) reads
44/// this to place a layer at the right level once layering goes cross-level.
45/// Kept decoupled from executor's WIP `ShellKind` until the unification lands.
46#[derive(Debug, Clone, Copy, PartialEq, Eq)]
47pub enum WrapperLevel {
48 Op,
49 Stanza,
50 Phase,
51 Scenario,
52 Session,
53}
54
55/// SRD-82/92 — the execution unit a wrapper's trigger inspects, generalising
56/// the registry across levels. An op wrapper reads the `Op` variant; a phase
57/// wrapper the `Phase` variant (scenario/session variants join when those
58/// levels wire up). Op-level wrappers guard with [`Self::op`]; the resolver
59/// only offers a wrapper subjects of the level it declares (`applies_at`), so
60/// a well-formed wrapper never sees a foreign subject — the guard is a
61/// belt-and-braces `None` return.
62#[derive(Clone, Copy)]
63pub enum WrapperSubject<'a> {
64 Op(&'a ParsedOp),
65 Phase(&'a WorkloadPhase),
66}
67
68impl<'a> WrapperSubject<'a> {
69 /// The execution level of this subject; pairs with [`WrapperRegistration::applies_at`].
70 pub fn level(&self) -> WrapperLevel {
71 match self {
72 WrapperSubject::Op(_) => WrapperLevel::Op,
73 WrapperSubject::Phase(_) => WrapperLevel::Phase,
74 }
75 }
76
77 /// The op template, if this is an op subject (op-wrapper triggers guard on it).
78 pub fn op(&self) -> Option<&'a ParsedOp> {
79 match self {
80 WrapperSubject::Op(op) => Some(op),
81 _ => None,
82 }
83 }
84
85 /// The phase, if this is a phase subject (phase-wrapper triggers guard on it).
86 pub fn phase(&self) -> Option<&'a WorkloadPhase> {
87 match self {
88 WrapperSubject::Phase(p) => Some(p),
89 _ => None,
90 }
91 }
92
93 /// Uniform "is this wrapper-owned field present?" — the canonical field
94 /// names mapped to each unit's actual storage. Op fields are spread
95 /// across `params`/`condition`/`delay`/`rate`; phase fields are typed
96 /// slots. Used by the parse-time misplaced-field guard.
97 pub fn has_owned_field(&self, field: &str) -> bool {
98 match self {
99 WrapperSubject::Op(op) => match field {
100 "if" => op.condition.is_some(),
101 "delay" => op.delay.is_some(),
102 "while" => op.while_cond.is_some(),
103 "rate" => op.rate.is_some(),
104 _ => op.params.contains_key(field),
105 },
106 WrapperSubject::Phase(p) => match field {
107 "interval" => p.interval.is_some(),
108 "repeat" => p.repeat.is_some(),
109 _ => false,
110 },
111 }
112 }
113}
114
115/// One entry per registered wrapper. Entries are submitted at
116/// link time via `inventory::submit!` and collected at startup
117/// into the [`WrapperRegistry`] view.
118///
119/// The fields here are pure declaration: which fields the
120/// wrapper consumes, when it applies, what relationships it
121/// has to other wrappers, and how it describes its assignment.
122/// Construction of the dispenser layer itself is NOT in the
123/// registration — the cascade in `activity.rs` continues to
124/// hold the per-wrapper `wrap()` calls (each has a different
125/// signature). The registry decides PRESENCE and ORDER; the
126/// cascade looks up the resolved plan and dispatches by name.
127pub struct WrapperRegistration {
128 /// Stable name (`"validate"`, `"poll"`, `"delay"`,
129 /// `"if"`, `"emit"`, `"result"`, `"metrics"`, `"traverse"`).
130 pub name: WrapperName,
131
132 /// Op-template field names this wrapper exclusively owns.
133 /// Listed for parse-time validation: a misplaced field like
134 /// `poll_interval_ms: 5000` on an op without `poll:` becomes
135 /// a hard error pointing at THIS registration, not an opaque
136 /// "unknown param".
137 ///
138 /// Pure data — the parse-time guard reads this; the wrapper
139 /// implementation reads its own fields directly off the
140 /// `ParsedOp`.
141 pub owned_fields: &'static [&'static str],
142
143 /// Predicate over the op template: "does this wrapper apply
144 /// to this op?" Default behaviour: any owned field present.
145 /// Wrappers with no owned fields (e.g. `result`, which fires
146 /// whenever the op declares any `result:` wires) override
147 /// this with their own logic.
148 pub triggers: fn(WrapperSubject) -> bool,
149
150 /// Wrappers that MUST sit inside this one (closer to the
151 /// adapter, called *after* this one per cycle).
152 /// Activates transitively: triggering `validate` pulls in
153 /// `traverse` whether or not a traverse field was declared.
154 pub requires_inner: &'static [WrapperName],
155
156 /// Wrappers that MUST NOT sit outside this one. Hard error
157 /// when the constraint graph would permit any of the listed
158 /// wrappers to wrap this one.
159 pub forbids_outer: &'static [WrapperName],
160
161 /// Wrappers that cannot coexist with this one on a given
162 /// op. Triggering both is a hard error.
163 pub mutually_exclusive_with: &'static [WrapperName],
164
165 /// One-line summary of what this wrapper, configured for
166 /// the given op template, will do at runtime.
167 /// Emitted at Info level once per op-template activation,
168 /// alongside the other wrappers in the resolved plan.
169 ///
170 /// Examples:
171 /// - `validate`: `"validate: min_rows ≥ 1 (strict)"`
172 /// - `poll`: `"poll: every 5s, timeout 600s, on \`await_empty\`"`
173 /// - `if`: `"if: cql_dialect == 'cass5'"`
174 ///
175 /// Returns `None` for wrappers that have nothing useful to
176 /// say (e.g. an always-on `traverse` with no per-op
177 /// configuration); operators see the wrappers that actually
178 /// shape behaviour, the boilerplate stays at Debug.
179 ///
180 /// Distinct from `OpDispenser::describe()` which describes
181 /// the *runtime op* — e.g. the CQL statement text — for
182 /// error-context dumps. This describes the *wrapper's
183 /// contribution* for init-time diagnostics.
184 pub describe_assignment: fn(WrapperSubject) -> Option<String>,
185
186 /// SRD-92 / ExecUnification — the execution-graph level(s) this wrapper is
187 /// legal at. Every current wrapper is `&[WrapperLevel::Op]`; the unified
188 /// scaffold reads this to know where a layer may sit. Carried as metadata
189 /// now; consumed when layering goes cross-level (Step 5).
190 pub levels: &'static [WrapperLevel],
191}
192
193inventory::collect!(WrapperRegistration);
194
195impl WrapperRegistration {
196 /// SRD-92 — whether this wrapper is legal at `level`.
197 pub fn applies_at(&self, level: WrapperLevel) -> bool {
198 self.levels.contains(&level)
199 }
200}
201
202/// Live view over every registered wrapper. Built once at
203/// startup from the `inventory` collection.
204///
205/// The struct is cheap to clone (it borrows the static
206/// registrations) and is passed to the [`crate::wrapper_resolver::WrapperResolver`]
207/// for per-op-template plan computation.
208pub struct WrapperRegistry {
209 entries: Vec<&'static WrapperRegistration>,
210}
211
212impl WrapperRegistry {
213 /// Build the registry from every `inventory::submit!`
214 /// entry currently linked into the binary. Invoked once
215 /// at startup.
216 pub fn from_inventory() -> Self {
217 let mut entries: Vec<&'static WrapperRegistration> =
218 inventory::iter::<WrapperRegistration>().collect();
219 entries.sort_by_key(|r| r.name);
220 Self { entries }
221 }
222
223 /// Iterate every registered wrapper.
224 pub fn iter(&self) -> impl Iterator<Item = &'static WrapperRegistration> + '_ {
225 self.entries.iter().copied()
226 }
227
228 /// Whether `field` is an op-template key declared as owned by ANY
229 /// registered wrapper (its trigger or a knob it consumes). This is
230 /// the single structural source of truth for "this params key is a
231 /// wrapper field" — driven by each wrapper's `owned_fields`
232 /// declaration, not a hand-maintained parallel list. The op
233 /// closed-vocabulary guard consults it so a wrapper field is
234 /// accepted because a wrapper *declares* it, not because it happens
235 /// to also be a CLI param (SRD-32a). Adding a wrapper therefore
236 /// never requires touching `CORE_OP_PARAMS` or the CLI vocabulary.
237 pub fn owns_field(&self, field: &str) -> bool {
238 self.iter().any(|reg| reg.owned_fields.contains(&field))
239 }
240
241 /// Every field name owned by some registered wrapper, deduplicated.
242 /// Exposed for diagnostics (the closed-vocab guard names the full
243 /// accepted wrapper vocabulary) and for the drift-guard test.
244 pub fn all_owned_fields(&self) -> std::collections::BTreeSet<&'static str> {
245 self.iter()
246 .flat_map(|reg| reg.owned_fields.iter().copied())
247 .collect()
248 }
249
250 /// Look up by name. Returns `None` for unknown names; the
251 /// caller surfaces that as a typo diagnostic with a
252 /// closest-match suggestion (see [`Self::closest_match`]).
253 pub fn get(&self, name: WrapperName) -> Option<&'static WrapperRegistration> {
254 self.entries.iter().copied().find(|r| r.name == name)
255 }
256
257 /// Look up by raw string. Convenience for parsing the
258 /// `--wrap-default-order` / `wrappers.order` lists.
259 pub fn get_str(&self, name: &str) -> Option<&'static WrapperRegistration> {
260 self.entries
261 .iter()
262 .copied()
263 .find(|r| r.name.as_str() == name)
264 }
265
266 /// Number of registered wrappers.
267 pub fn len(&self) -> usize {
268 self.entries.len()
269 }
270
271 pub fn is_empty(&self) -> bool {
272 self.entries.is_empty()
273 }
274
275 /// Find the registered wrapper name closest to `query` by
276 /// Levenshtein distance. Used for "did you mean … ?"
277 /// diagnostics on unknown names.
278 pub fn closest_match(&self, query: &str) -> Option<&'static str> {
279 crate::suggest::closest_match(query, self.entries.iter().map(|r| r.name.as_str()))
280 }
281
282 /// SRD-32a Push 2 — find every owned field that's present
283 /// on the op template but whose owning wrapper does NOT
284 /// trigger. Returns one (wrapper, field) pair per
285 /// violation; an empty vec means every field is in its
286 /// proper place.
287 ///
288 /// `field_present_on_template` is the predicate the
289 /// caller uses to ask "is field X set on this op?" — it
290 /// abstracts the fact that some wrapper-owned fields
291 /// (e.g. `if`, `delay`) live outside `params:`. In
292 /// practice the owned fields that AREN'T also their
293 /// wrapper's trigger always live under `params:`, so a
294 /// caller can pass a closure over `template.params
295 /// .contains_key`.
296 pub fn misplaced_fields(&self, subject: WrapperSubject) -> Vec<(WrapperName, &'static str)> {
297 let mut out: Vec<(WrapperName, &'static str)> = Vec::new();
298 for reg in self.iter() {
299 // Only wrappers legal at this subject's level can claim its
300 // fields; and a triggered wrapper's fields are correctly placed.
301 if !reg.applies_at(subject.level()) || (reg.triggers)(subject) {
302 continue;
303 }
304 for &field in reg.owned_fields {
305 if subject.has_owned_field(field) {
306 out.push((reg.name, field));
307 }
308 }
309 }
310 out
311 }
312}
313
314#[cfg(test)]
315mod level_tests {
316 use super::*;
317
318 fn no_trigger(_: WrapperSubject) -> bool {
319 false
320 }
321 fn no_describe(_: WrapperSubject) -> Option<String> {
322 None
323 }
324
325 #[test]
326 fn applies_at_reads_declared_levels() {
327 let reg = WrapperRegistration {
328 name: WrapperName::new("t"),
329 owned_fields: &[],
330 triggers: no_trigger,
331 requires_inner: &[],
332 forbids_outer: &[],
333 mutually_exclusive_with: &[],
334 describe_assignment: no_describe,
335 levels: &[WrapperLevel::Op, WrapperLevel::Phase],
336 };
337 assert!(reg.applies_at(WrapperLevel::Op));
338 assert!(reg.applies_at(WrapperLevel::Phase));
339 assert!(!reg.applies_at(WrapperLevel::Session));
340 }
341}