registerGuardrail method
Registers guardrail for featureId. A feature can have several
guardrails (e.g. both an earn-rate ceiling and a spend-rate floor);
any single violation is enough to trigger rollback.
ENH-89: GuardrailDefinition's own min/max invariant is only an
assert (stripped in release builds) — it stays that way
deliberately, since GuardrailDefinition is const-constructible and
used in const guardrail lists throughout consumers; giving its
constructor a runtime-throwing body would force every one of those
call sites off const. This is the real runtime trust boundary
instead — a guardrail with neither bound set (e.g. one assembled
from a remote-config payload missing both fields) is caught HERE,
unconditionally, in every build mode, rather than silently never
firing violatedBy at all.
Implementation
void registerGuardrail(String featureId, GuardrailDefinition guardrail) {
if (guardrail.min == null && guardrail.max == null) {
throw ArgumentError.value(
guardrail,
'guardrail',
'needs at least a min or a max bound',
);
}
(_guardrails[featureId] ??= []).add(guardrail);
}