#![forbid(unsafe_code)]
use std::collections::HashSet;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EquivalenceDimension {
RenderOutput,
CellContent,
CellStyle,
EventOrdering,
SubscriptionSet,
CommandSequence,
TieBreaking,
ShutdownBehavior,
ArtifactContent,
TimingBounds,
}
impl EquivalenceDimension {
#[must_use]
pub const fn is_strict(&self) -> bool {
!matches!(self, Self::TimingBounds)
}
#[must_use]
pub const fn description(&self) -> &'static str {
match self {
Self::RenderOutput => "Frame buffer checksums (BLAKE3) must be identical",
Self::CellContent => "Per-cell character content must be identical",
Self::CellStyle => "Per-cell fg/bg/attrs must be identical",
Self::EventOrdering => "Model update event sequence must be identical",
Self::SubscriptionSet => "Active subscription IDs must be identical",
Self::CommandSequence => "Commands from model updates must be identical",
Self::TieBreaking => "Deterministic ordering under ambiguity must be identical",
Self::ShutdownBehavior => "Exit path and cleanup must be identical",
Self::ArtifactContent => "Evidence artifact content must be identical",
Self::TimingBounds => "Performance must remain within SLO bounds (allowed to differ)",
}
}
#[must_use]
pub const fn domain(&self) -> EquivalenceDomain {
match self {
Self::RenderOutput | Self::CellContent | Self::CellStyle => EquivalenceDomain::Render,
Self::EventOrdering
| Self::SubscriptionSet
| Self::CommandSequence
| Self::TieBreaking
| Self::ShutdownBehavior => EquivalenceDomain::Runtime,
Self::ArtifactContent => EquivalenceDomain::Doctor,
Self::TimingBounds => EquivalenceDomain::Performance,
}
}
pub const ALL: &'static [EquivalenceDimension] = &[
Self::RenderOutput,
Self::CellContent,
Self::CellStyle,
Self::EventOrdering,
Self::SubscriptionSet,
Self::CommandSequence,
Self::TieBreaking,
Self::ShutdownBehavior,
Self::ArtifactContent,
Self::TimingBounds,
];
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EquivalenceDomain {
Render,
Runtime,
Doctor,
Performance,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DifferenceClass {
Semantic,
PresentationOnly,
PerformanceOnly,
}
#[derive(Debug, Clone)]
pub struct BehaviorProof {
pub change_description: String,
pub equivalence_justification: String,
pub verified_dimensions: HashSet<EquivalenceDimension>,
pub relaxed_dimensions: Vec<RelaxedDimension>,
pub old_checksums: Vec<String>,
pub new_checksums: Vec<String>,
pub replay_command: String,
pub scenario: String,
pub seed: u64,
pub viewport: (u16, u16),
}
#[derive(Debug, Clone)]
pub struct RelaxedDimension {
pub dimension: EquivalenceDimension,
pub justification: String,
pub difference_class: DifferenceClass,
}
#[derive(Debug, Clone)]
pub struct ProofValidation {
pub valid: bool,
pub missing_dimensions: Vec<EquivalenceDimension>,
pub issues: Vec<String>,
}
#[must_use]
pub fn validate_proof(proof: &BehaviorProof) -> ProofValidation {
let mut issues = Vec::new();
let mut missing = Vec::new();
let relaxed_dims: HashSet<EquivalenceDimension> = proof
.relaxed_dimensions
.iter()
.map(|r| r.dimension)
.collect();
for dim in EquivalenceDimension::ALL {
if dim.is_strict()
&& !proof.verified_dimensions.contains(dim)
&& !relaxed_dims.contains(dim)
{
missing.push(*dim);
}
}
if !missing.is_empty() {
issues.push(format!(
"Missing coverage for {} strict dimension(s)",
missing.len()
));
}
for relaxed in &proof.relaxed_dimensions {
if relaxed.justification.is_empty() {
issues.push(format!(
"Relaxed dimension {:?} has empty justification",
relaxed.dimension
));
}
}
if proof.replay_command.is_empty() {
issues.push("Replay command is empty".to_string());
}
if proof.old_checksums.is_empty() && proof.new_checksums.is_empty() {
issues.push("Both old and new checksums are empty".to_string());
}
let valid = issues.is_empty();
ProofValidation {
valid,
missing_dimensions: missing,
issues,
}
}
#[must_use]
pub fn replay_command(scenario: &str, seed: u64, viewport: (u16, u16)) -> String {
format!(
"GOLDEN_SEED={seed} cargo test -p ftui-harness -- {scenario} --nocapture \
# viewport: {w}x{h}",
seed = seed,
scenario = scenario,
w = viewport.0,
h = viewport.1,
)
}
#[must_use]
pub fn dimensions_for_domain(domain: EquivalenceDomain) -> Vec<EquivalenceDimension> {
EquivalenceDimension::ALL
.iter()
.filter(|d| d.domain() == domain)
.copied()
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn all_dimensions_have_descriptions() {
for dim in EquivalenceDimension::ALL {
assert!(
!dim.description().is_empty(),
"{:?} must have a description",
dim
);
}
}
#[test]
fn all_dimensions_have_domains() {
for dim in EquivalenceDimension::ALL {
let _domain = dim.domain(); }
}
#[test]
fn strict_dimensions_are_majority() {
let strict_count = EquivalenceDimension::ALL
.iter()
.filter(|d| d.is_strict())
.count();
assert!(
strict_count >= 8,
"most dimensions should be strict, got {} strict out of {}",
strict_count,
EquivalenceDimension::ALL.len()
);
}
#[test]
fn timing_is_relaxed() {
assert!(
!EquivalenceDimension::TimingBounds.is_strict(),
"timing must be relaxed — optimizations change timing by definition"
);
}
#[test]
fn render_domain_has_output_and_content() {
let render_dims = dimensions_for_domain(EquivalenceDomain::Render);
assert!(render_dims.contains(&EquivalenceDimension::RenderOutput));
assert!(render_dims.contains(&EquivalenceDimension::CellContent));
assert!(render_dims.contains(&EquivalenceDimension::CellStyle));
}
#[test]
fn runtime_domain_has_ordering_and_shutdown() {
let runtime_dims = dimensions_for_domain(EquivalenceDomain::Runtime);
assert!(runtime_dims.contains(&EquivalenceDimension::EventOrdering));
assert!(runtime_dims.contains(&EquivalenceDimension::ShutdownBehavior));
}
#[test]
fn doctor_domain_has_artifact_content() {
let doctor_dims = dimensions_for_domain(EquivalenceDomain::Doctor);
assert!(doctor_dims.contains(&EquivalenceDimension::ArtifactContent));
}
#[test]
fn all_domains_covered() {
let domains: HashSet<EquivalenceDomain> = EquivalenceDimension::ALL
.iter()
.map(|d| d.domain())
.collect();
assert!(domains.contains(&EquivalenceDomain::Render));
assert!(domains.contains(&EquivalenceDomain::Runtime));
assert!(domains.contains(&EquivalenceDomain::Doctor));
assert!(domains.contains(&EquivalenceDomain::Performance));
}
#[test]
fn replay_command_includes_seed_and_scenario() {
let cmd = replay_command("test_resize_80x24", 42, (80, 24));
assert!(cmd.contains("GOLDEN_SEED=42"));
assert!(cmd.contains("test_resize_80x24"));
assert!(cmd.contains("80x24"));
}
#[test]
fn validate_complete_proof_passes() {
let proof = BehaviorProof {
change_description: "Optimize buffer diff to skip unchanged rows".to_string(),
equivalence_justification: "Only unchanged rows are skipped; output is identical"
.to_string(),
verified_dimensions: EquivalenceDimension::ALL
.iter()
.filter(|d| d.is_strict())
.copied()
.collect(),
relaxed_dimensions: vec![RelaxedDimension {
dimension: EquivalenceDimension::TimingBounds,
justification: "Optimization reduces frame time by ~15%".to_string(),
difference_class: DifferenceClass::PerformanceOnly,
}],
old_checksums: vec!["blake3:abc123".to_string()],
new_checksums: vec!["blake3:abc123".to_string()],
replay_command: "GOLDEN_SEED=42 cargo test -p ftui-harness -- resize".to_string(),
scenario: "resize_80x24".to_string(),
seed: 42,
viewport: (80, 24),
};
let result = validate_proof(&proof);
assert!(
result.valid,
"complete proof should pass: {:?}",
result.issues
);
assert!(result.missing_dimensions.is_empty());
}
#[test]
fn validate_incomplete_proof_fails() {
let proof = BehaviorProof {
change_description: "Some optimization".to_string(),
equivalence_justification: "Trust me".to_string(),
verified_dimensions: HashSet::new(), relaxed_dimensions: vec![],
old_checksums: vec!["old".to_string()],
new_checksums: vec!["new".to_string()],
replay_command: "cargo test".to_string(),
scenario: "test".to_string(),
seed: 0,
viewport: (80, 24),
};
let result = validate_proof(&proof);
assert!(!result.valid, "incomplete proof should fail");
assert!(
!result.missing_dimensions.is_empty(),
"should report missing dimensions"
);
}
#[test]
fn validate_proof_without_replay_command_fails() {
let proof = BehaviorProof {
change_description: "test".to_string(),
equivalence_justification: "test".to_string(),
verified_dimensions: EquivalenceDimension::ALL
.iter()
.filter(|d| d.is_strict())
.copied()
.collect(),
relaxed_dimensions: vec![],
old_checksums: vec!["a".to_string()],
new_checksums: vec!["a".to_string()],
replay_command: String::new(), scenario: "test".to_string(),
seed: 0,
viewport: (80, 24),
};
let result = validate_proof(&proof);
assert!(!result.valid);
assert!(result.issues.iter().any(|i| i.contains("Replay command")));
}
#[test]
fn validate_relaxed_without_justification_fails() {
let mut verified: HashSet<EquivalenceDimension> = EquivalenceDimension::ALL
.iter()
.filter(|d| d.is_strict())
.copied()
.collect();
verified.remove(&EquivalenceDimension::RenderOutput);
let proof = BehaviorProof {
change_description: "test".to_string(),
equivalence_justification: "test".to_string(),
verified_dimensions: verified,
relaxed_dimensions: vec![RelaxedDimension {
dimension: EquivalenceDimension::RenderOutput,
justification: String::new(), difference_class: DifferenceClass::PresentationOnly,
}],
old_checksums: vec!["a".to_string()],
new_checksums: vec!["b".to_string()],
replay_command: "cargo test".to_string(),
scenario: "test".to_string(),
seed: 0,
viewport: (80, 24),
};
let result = validate_proof(&proof);
assert!(!result.valid);
assert!(
result
.issues
.iter()
.any(|i| i.contains("empty justification"))
);
}
#[test]
fn difference_classes_are_distinct() {
assert_ne!(DifferenceClass::Semantic, DifferenceClass::PresentationOnly);
assert_ne!(
DifferenceClass::PresentationOnly,
DifferenceClass::PerformanceOnly
);
assert_ne!(DifferenceClass::Semantic, DifferenceClass::PerformanceOnly);
}
#[test]
fn total_dimensions_count() {
assert_eq!(
EquivalenceDimension::ALL.len(),
10,
"should have exactly 10 equivalence dimensions"
);
}
}