bcinr_cmca/lib.rs
1//! # CMCA: Chatman Multifractal Consequence Allocation
2//!
3//! `bcinr-cmca` is an authoritative systems library implementing deterministic,
4//! branchless, and allocation-free algorithms for runtime telemetry, calibration,
5//! monitoring, and cascade allocation (see [`allocator`]) of bounded computational
6//! systems. "Covariance Monitoring and Calibration Assessment" was this crate's
7//! earlier, calibration-only framing; see `docs/CMCA_EXPLANATION.md` for the
8//! canonical-name reconciliation across this crate's calibration (`observatory`)
9//! and cascade-allocation (`allocator`) responsibilities.
10//!
11//! Under the strict constraints of the **Radon Law** (Cyclomatic Complexity $CC=1$),
12//! this module provides tools to audit the health and stability of mathematical
13//! substrates without introducing timing side-channels, dynamic heap allocations,
14//! or panic-prone execution paths.
15//!
16//! ## Architectural Foundations
17//!
18//! The crate enforces an autonomic feedback loop structured around the standard **MAPE-K**
19//! (Monitor, Analyze, Plan, Execute, Shared Knowledge) control loop:
20//! 1. **Observe**: Collect telemetry metrics (such as scaling factors, condition bounds, and drift metrics).
21//! 2. **Infer**: Compute safety flags branchlessly using fixed-point arithmetic invariants.
22//! 3. **Propose**: Report validation states indicating whether to proceed or trigger recertification.
23//!
24//! ## Key Modules
25//!
26//! * [`fixed`]: Q16.16 fixed-point arithmetic implementation designed for $CC=1$ operations.
27//! * [`allocator`]: Linear bounds-checked and panic-free bump allocators.
28//! * [`observatory`]: The evaluation engine that computes calibration safety flags based on mathematical thresholds.
29//! * [`dme_route`]: Canonical content-addressed DME route selector. It performs SELECT only;
30//! its decisions carry no authority and cannot execute consequences.
31//!
32//! ## `allocator::allocate` is fixed to this crate's own `N=8`/`K=4`/`Q=4` shape (CMCA-108)
33//!
34//! [`allocator::allocate`] and [`allocator::allocate_single_lens`] -- this crate's
35//! certified, branchless, allocation-free allocation path -- are compiled
36//! against this crate's own object/measure/lens counts (`N = 8`, `K = 4`,
37//! `Q = 4`, from `generated::consequence_mass::case_studies`), **not**
38//! generic over caller-chosen sizes. A caller can supply different factor
39//! *values* but not a different *shape* without this crate being
40//! regenerated against a different `ontology/*.ttl`. See
41//! [`allocator::allocate`]'s doc comment for why (the branchless kernel
42//! beneath it unrolls against the literal constants 8/4/4) and for
43//! `cascade::consequence_mass`, the escape hatch for arbitrary tree
44//! shapes.
45//!
46//! ## Which entry point do I want? (CMCA-119)
47//!
48//! Five overlapping ways to get an allocation share out of this crate --
49//! this section is a navigation aid, not a restatement of each one's own
50//! doc comment (follow the links below for the real detail):
51//!
52//! * [`allocator::allocate`] -- **start here** for the common case: one
53//! LAMBDA-weighted allocation vector across the whole `N`/`K`/`Q` object
54//! registry, evaluated against `FeasibleRegion::CURRENT`. See
55//! `examples/basic_allocation.rs` for a full runnable walkthrough.
56//! * [`allocator::allocate_in`] -- the same computation as `allocate`, but
57//! over a caller-chosen [`allocator::FeasibleRegion`] instead of always
58//! `CURRENT`; use this when evaluating a hypothetical/candidate region
59//! rather than the live one.
60//! * [`allocator::allocate_single_lens`] -- returns exactly one lens's
61//! allocation vector for one measure, bypassing the LAMBDA blend across
62//! all `K x Q` pairs; use this to inspect "what would lens `q` alone
63//! say" instead of the combined answer.
64//! * `cascade::consequence_mass` (requires `alloc`) -- the escape hatch
65//! for tree shapes other than this crate's fixed `N = 8`/`K = 4`/`Q = 4`;
66//! use this when the object/measure/lens counts aren't the ones this
67//! crate was regenerated against.
68//! * `escort::escort_distribution` (requires `alloc`) -- the
69//! fractional-exponent escort distribution `L_q(i) = p_i^q / SUM_j p_j^q`
70//! built directly on [`allocator::power`]; use this when you need an
71//! escort weighting over an arbitrary mass vector, independent of the
72//! object-registry allocation machinery above.
73//!
74//! ## Example Usage
75//!
76//! ```rust
77//! use bcinr_cmca::fixed::NonNegativeFixed;
78//! use bcinr_cmca::observatory::{evaluate_calibration, ObservatoryFlag};
79//!
80//! let kappa_hat = NonNegativeFixed::from_bits(131072);
81//! let kappa_under = NonNegativeFixed::from_bits(131072);
82//! let epsilon_on = NonNegativeFixed::from_bits(65536);
83//! let gamma_min_plus_hat = NonNegativeFixed::from_bits(131072);
84//! let gamma_min_plus_under = NonNegativeFixed::from_bits(131072);
85//! let epsilon_gram = NonNegativeFixed::from_bits(65536);
86//! let d_js = NonNegativeFixed::ZERO;
87//! let epsilon_drift = NonNegativeFixed::from_bits(65536);
88//! let s_meas = NonNegativeFixed::ONE;
89//! let s_leaf = NonNegativeFixed::from_bits(32768);
90//!
91//! use bcinr_cmca::observatory::{MeasurementArtifact, SupportStanding, ModeDelta};
92//! let artifact = MeasurementArtifact {
93//! point_estimate: kappa_hat,
94//! lower_bound: kappa_under,
95//! upper_bound: kappa_hat,
96//! support_standing: SupportStanding { is_supported: true, smoothing_applied: false },
97//! effective_sample_size: NonNegativeFixed::ONE,
98//! dependence_standing: 0,
99//! numeric_error: NonNegativeFixed::ZERO,
100//! drift: d_js,
101//! gram_lower_bound: gamma_min_plus_under,
102//! graph_digest: 0,
103//! control_mode_digest: 42,
104//! proposal: ModeDelta::ProposeDelta,
105//! };
106//! let status = evaluate_calibration(
107//! &artifact, epsilon_on, epsilon_gram, epsilon_drift, s_meas, s_leaf,
108//! );
109//! # if cfg!(any(
110//! # feature = "mutant_1", feature = "mutant_2", feature = "mutant_3",
111//! # feature = "mutant_4", feature = "mutant_5", feature = "mutant_6",
112//! # feature = "mutant_7", feature = "mutant_8", feature = "mutant_9",
113//! # feature = "mutant_10", feature = "mutant_11"
114//! # )) { return; }
115//! assert!(status.is_ok());
116//! ```
117
118#![cfg_attr(not(feature = "std"), no_std)]
119#![deny(unsafe_code)]
120
121#[cfg(feature = "std")]
122extern crate std;
123
124pub mod allocation_receipt;
125pub mod allocator;
126#[cfg(feature = "alloc")]
127pub mod cascade;
128/// DME route selection: deterministic SELECT over admitted bounded execution classes.
129/// This module grants no authority and contains no execution/DO surface.
130pub mod dme_route;
131/// Fractional-exponent escort distribution (`L_q(i) = p_i^q / SUM_j p_j^q`)
132/// built on [`allocator::power`]. See the module docs for why this exists
133/// alongside `cascade::escort_weight`, which only covers integer `q`.
134#[cfg(feature = "alloc")]
135pub mod escort;
136pub mod fixed;
137pub mod generated;
138pub mod generated_profile;
139pub mod lrc;
140pub mod observatory;
141/// Hand-transcribed exact-rational reference oracle for the CMCA escort
142/// distribution, mirroring `~/mfw`'s `MFW/CMCA/Semantics/Escort.lean`. Not a
143/// machine-checked bridge -- see the module docs for exact scope.
144#[cfg(feature = "alloc")]
145pub mod reference_escort;
146/// Authority-free SA2A resource-allocation consumer boundary.
147pub mod sa2a;
148pub mod stability_theorem;
149
150pub use allocator::{
151 check_hierarchy_acyclic, HierarchyRefusal, LensSelectionRefusal, StabilityRefusal,
152};
153pub use dme_route::{
154 select_dme_route, select_gall_route, verify_dme_route_decision, verify_gall_route_decision,
155 AuthorityStanding, ConsequenceClass, DmeRouteDecision, DmeRouteRefusal, DmeRouteRequest,
156 GallRouteDecision, GallRouteRefusal, GallRouteRequest, GallWorkIdentity, RouteCandidate,
157 RouteClass, RouteExplanation, WorkKnowledge, WorkStanding,
158};
159
160// INTEGRATION NOTE (v26.7.24, re-fenced explicitly per CMCA-102 Branch B):
161// Recovery-only authority modules remain defined in allocator.rs and deliberately
162// NOT re-exported from this crate root pending their formal dependency-closure proof.
163
164/// A branchless mock/dummy function utilized in tests and contract validation.
165pub fn dummy_branchless(val: u64) -> u64 {
166 val.wrapping_add(1)
167}
168
169#[cfg(test)]
170mod tests {
171 use super::*;
172
173 #[test]
174 fn test_dummy_branchless() {
175 assert_eq!(dummy_branchless(0), 1);
176 assert_eq!(dummy_branchless(42), 43);
177 assert_eq!(dummy_branchless(u64::MAX), 0);
178 }
179}