#![forbid(unsafe_code)]
use crate::baseline_capture::FixtureFamily;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PrimaryLever {
AlgorithmChange,
DataLayout,
WorkElimination,
IoReduction,
Concurrency,
AllocationReduction,
BranchOptimization,
HardwareAcceleration,
}
impl PrimaryLever {
#[must_use]
pub const fn label(&self) -> &'static str {
match self {
Self::AlgorithmChange => "algorithm-change",
Self::DataLayout => "data-layout",
Self::WorkElimination => "work-elimination",
Self::IoReduction => "io-reduction",
Self::Concurrency => "concurrency",
Self::AllocationReduction => "allocation-reduction",
Self::BranchOptimization => "branch-optimization",
Self::HardwareAcceleration => "hardware-acceleration",
}
}
#[must_use]
pub const fn typical_risk(&self) -> RiskLevel {
match self {
Self::AlgorithmChange => RiskLevel::High,
Self::DataLayout => RiskLevel::Medium,
Self::WorkElimination => RiskLevel::Low,
Self::IoReduction => RiskLevel::Low,
Self::Concurrency => RiskLevel::High,
Self::AllocationReduction => RiskLevel::Medium,
Self::BranchOptimization => RiskLevel::Medium,
Self::HardwareAcceleration => RiskLevel::High,
}
}
pub const ALL: &'static [PrimaryLever] = &[
Self::AlgorithmChange,
Self::DataLayout,
Self::WorkElimination,
Self::IoReduction,
Self::Concurrency,
Self::AllocationReduction,
Self::BranchOptimization,
Self::HardwareAcceleration,
];
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum ImpactScore {
Negligible,
Low,
Medium,
High,
Transformative,
}
impl ImpactScore {
#[must_use]
pub const fn numeric(&self) -> f64 {
match self {
Self::Negligible => 1.0,
Self::Low => 2.0,
Self::Medium => 4.0,
Self::High => 7.0,
Self::Transformative => 10.0,
}
}
#[must_use]
pub const fn label(&self) -> &'static str {
match self {
Self::Negligible => "negligible",
Self::Low => "low",
Self::Medium => "medium",
Self::High => "high",
Self::Transformative => "transformative",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum ConfidenceLevel {
Speculative,
Estimated,
Measured,
Proven,
}
impl ConfidenceLevel {
#[must_use]
pub const fn multiplier(&self) -> f64 {
match self {
Self::Speculative => 0.3,
Self::Estimated => 0.6,
Self::Measured => 0.85,
Self::Proven => 1.0,
}
}
#[must_use]
pub const fn label(&self) -> &'static str {
match self {
Self::Speculative => "speculative",
Self::Estimated => "estimated",
Self::Measured => "measured",
Self::Proven => "proven",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum EffortEstimate {
Trivial,
Low,
Medium,
High,
VeryHigh,
}
impl EffortEstimate {
#[must_use]
pub const fn divisor(&self) -> f64 {
match self {
Self::Trivial => 1.0,
Self::Low => 1.5,
Self::Medium => 3.0,
Self::High => 5.0,
Self::VeryHigh => 8.0,
}
}
#[must_use]
pub const fn label(&self) -> &'static str {
match self {
Self::Trivial => "trivial",
Self::Low => "low",
Self::Medium => "medium",
Self::High => "high",
Self::VeryHigh => "very-high",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum RiskLevel {
Low,
Medium,
High,
}
impl RiskLevel {
#[must_use]
pub const fn penalty(&self) -> f64 {
match self {
Self::Low => 0.0,
Self::Medium => 0.5,
Self::High => 1.5,
}
}
#[must_use]
pub const fn label(&self) -> &'static str {
match self {
Self::Low => "low",
Self::Medium => "medium",
Self::High => "high",
}
}
}
#[derive(Debug, Clone)]
pub struct OptimizationCandidate {
pub id: String,
pub lever: PrimaryLever,
pub impact: ImpactScore,
pub confidence: ConfidenceLevel,
pub effort: EffortEstimate,
pub risk: RiskLevel,
pub lane_family: FixtureFamily,
pub rationale: String,
pub baseline_id: String,
pub hotspot_id: String,
pub fixture_ids: Vec<String>,
pub strategic_exception: bool,
pub exception_justification: String,
}
impl OptimizationCandidate {
#[must_use]
pub fn new(id: &str, lever: PrimaryLever) -> Self {
Self {
id: id.to_string(),
lever,
impact: ImpactScore::Medium,
confidence: ConfidenceLevel::Estimated,
effort: EffortEstimate::Medium,
risk: lever.typical_risk(),
lane_family: FixtureFamily::Render,
rationale: String::new(),
baseline_id: String::new(),
hotspot_id: String::new(),
fixture_ids: Vec::new(),
strategic_exception: false,
exception_justification: String::new(),
}
}
#[must_use]
pub fn impact(mut self, i: ImpactScore) -> Self {
self.impact = i;
self
}
#[must_use]
pub fn confidence(mut self, c: ConfidenceLevel) -> Self {
self.confidence = c;
self
}
#[must_use]
pub fn effort(mut self, e: EffortEstimate) -> Self {
self.effort = e;
self
}
#[must_use]
pub fn risk(mut self, r: RiskLevel) -> Self {
self.risk = r;
self
}
#[must_use]
pub fn lane(mut self, f: FixtureFamily) -> Self {
self.lane_family = f;
self
}
#[must_use]
pub fn rationale(mut self, r: &str) -> Self {
self.rationale = r.to_string();
self
}
#[must_use]
pub fn baseline_id(mut self, b: &str) -> Self {
self.baseline_id = b.to_string();
self
}
#[must_use]
pub fn hotspot_id(mut self, h: &str) -> Self {
self.hotspot_id = h.to_string();
self
}
#[must_use]
pub fn fixtures(mut self, f: Vec<&str>) -> Self {
self.fixture_ids = f.into_iter().map(String::from).collect();
self
}
#[must_use]
pub fn strategic_exception(mut self, justification: &str) -> Self {
self.strategic_exception = true;
self.exception_justification = justification.to_string();
self
}
#[must_use]
pub fn score(&self) -> f64 {
let raw = self.impact.numeric() * self.confidence.multiplier() / self.effort.divisor();
(raw - self.risk.penalty()).max(0.0)
}
#[must_use]
pub fn to_json(&self) -> String {
let fixtures: Vec<String> = self
.fixture_ids
.iter()
.map(|f| format!("\"{f}\""))
.collect();
format!(
r#"{{
"id": "{}",
"lever": "{}",
"impact": "{}",
"confidence": "{}",
"effort": "{}",
"risk": "{}",
"lane": "{}",
"score": {:.3},
"rationale": "{}",
"baseline_id": "{}",
"hotspot_id": "{}",
"fixture_ids": [{}],
"strategic_exception": {},
"exception_justification": "{}"
}}"#,
self.id,
self.lever.label(),
self.impact.label(),
self.confidence.label(),
self.effort.label(),
self.risk.label(),
self.lane_family.label(),
self.score(),
self.rationale.replace('"', "\\\""),
self.baseline_id,
self.hotspot_id,
fixtures.join(", "),
self.strategic_exception,
self.exception_justification.replace('"', "\\\""),
)
}
}
#[derive(Debug, Clone)]
pub struct RequiredArtifacts {
pub pre_baseline: bool,
pub pre_hotspot_table: bool,
pub pre_rollback_plan: bool,
pub post_reprofile: bool,
pub post_shadow_run: bool,
pub post_replay_proof: bool,
pub post_negative_control: bool,
}
impl RequiredArtifacts {
#[must_use]
pub const fn standard() -> Self {
Self {
pre_baseline: true,
pre_hotspot_table: true,
pre_rollback_plan: true,
post_reprofile: true,
post_shadow_run: true,
post_replay_proof: true,
post_negative_control: true,
}
}
#[must_use]
pub const fn exception() -> Self {
Self {
pre_baseline: true,
pre_hotspot_table: false,
pre_rollback_plan: true,
post_reprofile: true,
post_shadow_run: true,
post_replay_proof: false,
post_negative_control: true,
}
}
#[must_use]
pub const fn count(&self) -> u32 {
let mut n = 0;
if self.pre_baseline {
n += 1;
}
if self.pre_hotspot_table {
n += 1;
}
if self.pre_rollback_plan {
n += 1;
}
if self.post_reprofile {
n += 1;
}
if self.post_shadow_run {
n += 1;
}
if self.post_replay_proof {
n += 1;
}
if self.post_negative_control {
n += 1;
}
n
}
}
#[derive(Debug, Clone)]
pub struct AdmissionVerdict {
pub admitted: bool,
pub score: f64,
pub threshold: f64,
pub reason: String,
pub required_artifacts: RequiredArtifacts,
pub warnings: Vec<String>,
}
impl AdmissionVerdict {
#[must_use]
pub fn to_json(&self) -> String {
let warnings: Vec<String> = self.warnings.iter().map(|w| format!("\"{w}\"")).collect();
format!(
r#"{{
"admitted": {},
"score": {:.3},
"threshold": {:.3},
"reason": "{}",
"required_artifact_count": {},
"warnings": [{}]
}}"#,
self.admitted,
self.score,
self.threshold,
self.reason.replace('"', "\\\""),
self.required_artifacts.count(),
warnings.join(", "),
)
}
}
#[derive(Debug, Clone)]
pub struct AdmissionPolicy {
pub score_threshold: f64,
pub allow_exceptions: bool,
pub allow_speculative: bool,
pub max_concurrent_per_lane: u32,
}
impl AdmissionPolicy {
#[must_use]
pub const fn default_strict() -> Self {
Self {
score_threshold: 1.0,
allow_exceptions: true,
allow_speculative: false,
max_concurrent_per_lane: 2,
}
}
#[must_use]
pub const fn exploration() -> Self {
Self {
score_threshold: 0.5,
allow_exceptions: true,
allow_speculative: true,
max_concurrent_per_lane: 4,
}
}
#[must_use]
pub fn evaluate(&self, candidate: &OptimizationCandidate) -> AdmissionVerdict {
let score = candidate.score();
let mut warnings = Vec::new();
if candidate.confidence == ConfidenceLevel::Speculative && !self.allow_speculative {
return AdmissionVerdict {
admitted: false,
score,
threshold: self.score_threshold,
reason: "Speculative-confidence candidates are not allowed by this policy. \
Profile the target workload to upgrade confidence."
.to_string(),
required_artifacts: RequiredArtifacts::standard(),
warnings,
};
}
if candidate.baseline_id.is_empty() {
warnings.push(
"No baseline_id specified — pre-implementation evidence will be weak".to_string(),
);
}
if candidate.hotspot_id.is_empty() {
warnings.push(
"No hotspot_id specified — optimization target is not profiling-driven".to_string(),
);
}
if candidate.rationale.is_empty() {
warnings.push(
"No rationale provided — justification will be unclear to reviewers".to_string(),
);
}
if candidate.strategic_exception {
if !self.allow_exceptions {
return AdmissionVerdict {
admitted: false,
score,
threshold: self.score_threshold,
reason: "Strategic exceptions are disabled by this policy.".to_string(),
required_artifacts: RequiredArtifacts::standard(),
warnings,
};
}
if candidate.exception_justification.is_empty() {
return AdmissionVerdict {
admitted: false,
score,
threshold: self.score_threshold,
reason: "Strategic exception requested but no justification provided."
.to_string(),
required_artifacts: RequiredArtifacts::exception(),
warnings,
};
}
warnings.push(format!(
"Admitted as strategic exception (score {score:.3} < threshold {:.3})",
self.score_threshold
));
return AdmissionVerdict {
admitted: true,
score,
threshold: self.score_threshold,
reason: format!("Strategic exception: {}", candidate.exception_justification),
required_artifacts: RequiredArtifacts::exception(),
warnings,
};
}
if score >= self.score_threshold {
AdmissionVerdict {
admitted: true,
score,
threshold: self.score_threshold,
reason: format!(
"Score {score:.3} >= threshold {:.3}. Lever: {}, Impact: {}, Confidence: {}",
self.score_threshold,
candidate.lever.label(),
candidate.impact.label(),
candidate.confidence.label(),
),
required_artifacts: RequiredArtifacts::standard(),
warnings,
}
} else {
AdmissionVerdict {
admitted: false,
score,
threshold: self.score_threshold,
reason: format!(
"Score {score:.3} < threshold {:.3}. Consider: higher-impact lever, \
better profiling data, or strategic exception with justification.",
self.score_threshold,
),
required_artifacts: RequiredArtifacts::standard(),
warnings,
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn high_impact_measured_low_effort_admits() {
let candidate = OptimizationCandidate::new("test-opt", PrimaryLever::WorkElimination)
.impact(ImpactScore::High)
.confidence(ConfidenceLevel::Measured)
.effort(EffortEstimate::Low)
.rationale("Eliminate redundant diff computation");
let policy = AdmissionPolicy::default_strict();
let verdict = policy.evaluate(&candidate);
assert!(
verdict.admitted,
"high-impact measured candidate should be admitted"
);
assert!(verdict.score > policy.score_threshold);
}
#[test]
fn negligible_speculative_high_effort_rejects() {
let candidate = OptimizationCandidate::new("bad-opt", PrimaryLever::HardwareAcceleration)
.impact(ImpactScore::Negligible)
.confidence(ConfidenceLevel::Speculative)
.effort(EffortEstimate::VeryHigh);
let policy = AdmissionPolicy::default_strict();
let verdict = policy.evaluate(&candidate);
assert!(
!verdict.admitted,
"speculative candidates rejected by strict policy"
);
}
#[test]
fn strategic_exception_admits_low_score() {
let candidate = OptimizationCandidate::new("strategic", PrimaryLever::DataLayout)
.impact(ImpactScore::Low)
.confidence(ConfidenceLevel::Estimated)
.effort(EffortEstimate::High)
.strategic_exception("Required for cache-line alignment before SIMD work");
let policy = AdmissionPolicy::default_strict();
let verdict = policy.evaluate(&candidate);
assert!(verdict.admitted, "strategic exception should be admitted");
assert!(
verdict.reason.contains("Strategic exception"),
"reason should mention exception"
);
}
#[test]
fn strategic_exception_without_justification_rejects() {
let mut candidate = OptimizationCandidate::new("no-reason", PrimaryLever::DataLayout)
.impact(ImpactScore::Low)
.effort(EffortEstimate::High);
candidate.strategic_exception = true;
let policy = AdmissionPolicy::default_strict();
let verdict = policy.evaluate(&candidate);
assert!(
!verdict.admitted,
"exception without justification should be rejected"
);
}
#[test]
fn exploration_policy_allows_speculative() {
let candidate = OptimizationCandidate::new("explore", PrimaryLever::AlgorithmChange)
.impact(ImpactScore::Medium)
.confidence(ConfidenceLevel::Speculative)
.effort(EffortEstimate::Medium);
let policy = AdmissionPolicy::exploration();
let verdict = policy.evaluate(&candidate);
assert!(!verdict.admitted);
}
#[test]
fn score_formula_correct() {
let candidate = OptimizationCandidate::new("formula", PrimaryLever::WorkElimination)
.impact(ImpactScore::High) .confidence(ConfidenceLevel::Measured) .effort(EffortEstimate::Low) .risk(RiskLevel::Low);
let score = candidate.score();
assert!((score - 3.967).abs() < 0.01, "expected ~3.967, got {score}");
}
#[test]
fn score_clamped_to_zero() {
let candidate = OptimizationCandidate::new("negative", PrimaryLever::Concurrency)
.impact(ImpactScore::Negligible) .confidence(ConfidenceLevel::Speculative) .effort(EffortEstimate::VeryHigh) .risk(RiskLevel::High);
assert!(
candidate.score() < 0.01,
"negative score should be clamped to 0"
);
}
#[test]
fn missing_evidence_generates_warnings() {
let candidate = OptimizationCandidate::new("no-evidence", PrimaryLever::WorkElimination)
.impact(ImpactScore::High)
.confidence(ConfidenceLevel::Measured)
.effort(EffortEstimate::Trivial);
let policy = AdmissionPolicy::default_strict();
let verdict = policy.evaluate(&candidate);
assert!(verdict.admitted, "should still admit on score");
assert!(
verdict.warnings.len() >= 3,
"expected at least 3 warnings for missing evidence, got {}",
verdict.warnings.len()
);
}
#[test]
fn primary_lever_labels_and_risk() {
for lever in PrimaryLever::ALL {
assert!(!lever.label().is_empty());
assert!(!lever.typical_risk().label().is_empty());
}
}
#[test]
fn all_impact_scores_ordered() {
assert!(ImpactScore::Negligible < ImpactScore::Low);
assert!(ImpactScore::Low < ImpactScore::Medium);
assert!(ImpactScore::Medium < ImpactScore::High);
assert!(ImpactScore::High < ImpactScore::Transformative);
}
#[test]
fn all_confidence_levels_ordered() {
assert!(ConfidenceLevel::Speculative < ConfidenceLevel::Estimated);
assert!(ConfidenceLevel::Estimated < ConfidenceLevel::Measured);
assert!(ConfidenceLevel::Measured < ConfidenceLevel::Proven);
}
#[test]
fn required_artifacts_counts() {
assert_eq!(RequiredArtifacts::standard().count(), 7);
assert!(RequiredArtifacts::exception().count() < RequiredArtifacts::standard().count());
}
#[test]
fn candidate_to_json_valid() {
let candidate = OptimizationCandidate::new("json-test", PrimaryLever::IoReduction)
.impact(ImpactScore::Medium)
.confidence(ConfidenceLevel::Measured)
.effort(EffortEstimate::Low)
.rationale("test rationale")
.baseline_id("baseline-001")
.hotspot_id("mod::func");
let json = candidate.to_json();
assert!(json.contains("\"id\": \"json-test\""));
assert!(json.contains("\"lever\": \"io-reduction\""));
assert!(json.contains("\"score\":"));
assert!(json.contains("\"baseline_id\": \"baseline-001\""));
}
#[test]
fn verdict_to_json_valid() {
let verdict = AdmissionVerdict {
admitted: true,
score: 3.5,
threshold: 1.0,
reason: "test reason".to_string(),
required_artifacts: RequiredArtifacts::standard(),
warnings: vec!["test warning".to_string()],
};
let json = verdict.to_json();
assert!(json.contains("\"admitted\": true"));
assert!(json.contains("\"score\": 3.500"));
assert!(json.contains("\"required_artifact_count\": 7"));
}
#[test]
fn default_strict_policy_values() {
let policy = AdmissionPolicy::default_strict();
assert!((policy.score_threshold - 1.0).abs() < 0.01);
assert!(policy.allow_exceptions);
assert!(!policy.allow_speculative);
assert_eq!(policy.max_concurrent_per_lane, 2);
}
#[test]
fn exploration_policy_values() {
let policy = AdmissionPolicy::exploration();
assert!((policy.score_threshold - 0.5).abs() < 0.01);
assert!(policy.allow_speculative);
assert_eq!(policy.max_concurrent_per_lane, 4);
}
#[test]
fn one_lever_discipline() {
let candidate = OptimizationCandidate::new("single-lever", PrimaryLever::WorkElimination);
assert_eq!(candidate.lever, PrimaryLever::WorkElimination);
}
}