use std::path::Path;
use super::{source_digest_hex, CheckReceiptSourceDigest};
pub const SCIENTIFIC_RUNTIME_SCHEMA: &str = "buildlang-scientific-runtime-receipt/v1";
pub const SCIENTIFIC_RUNTIME_SCHEMA_V0: &str = "buildlang-scientific-runtime-receipt/v0";
pub fn is_scientific_runtime_schema(schema: &str) -> bool {
schema == SCIENTIFIC_RUNTIME_SCHEMA || schema == SCIENTIFIC_RUNTIME_SCHEMA_V0
}
pub const LABEL_NUMERICAL_RESULT: &str = "NUMERICAL_RESULT";
pub const LABEL_BEST_FOUND_WITHIN_BUDGET: &str = "BEST_FOUND_WITHIN_BUDGET";
pub const SCOPE_BASE: &[&str] = &[
"source_and_build_digests",
"observed_series_invariant",
"rerun_agreement",
];
pub const SCOPE_REQUIRED: &str = "observed_series_invariant";
pub const SCOPE_BUDGET: &str = "best_found_within_budget";
pub const MC_EXECUTED_SCOPE: &[&str] = &["interval_arithmetic", "sample_denominator"];
pub const LABEL_NUMERICAL_RESULT_V0: &str = "NOT_A_NEW_PHYSICAL_LAW";
pub const LABEL_BEST_FOUND_WITHIN_BUDGET_V0: &str = "NOT_PROVES_OPTIMALITY";
pub struct ReceiptVocabulary {
pub numerical_label: &'static str,
pub list_name: &'static str,
pub required_entry: &'static str,
pub missing_class: &'static str,
pub budget_label: &'static str,
pub budget_entry: &'static str,
pub mc_entries: &'static [&'static str],
}
pub fn receipt_vocabulary(schema: &str) -> ReceiptVocabulary {
if schema == SCIENTIFIC_RUNTIME_SCHEMA_V0 {
ReceiptVocabulary {
numerical_label: LABEL_NUMERICAL_RESULT_V0,
list_name: "not_claimed",
required_entry: "physical_law",
missing_class: "OVERCLAIM_BOUNDARY_MISSING",
budget_label: LABEL_BEST_FOUND_WITHIN_BUDGET_V0,
budget_entry: "optimality",
mc_entries: MC_EXECUTED_NOT_CLAIMED,
}
} else {
ReceiptVocabulary {
numerical_label: LABEL_NUMERICAL_RESULT,
list_name: "scope",
required_entry: SCOPE_REQUIRED,
missing_class: "SCOPE_MISSING",
budget_label: LABEL_BEST_FOUND_WITHIN_BUDGET,
budget_entry: SCOPE_BUDGET,
mc_entries: MC_EXECUTED_SCOPE,
}
}
}
pub const SCIENTIFIC_STDIO_MODE_NATIVE: &str = "native";
pub const SCIENTIFIC_STDIO_MODE_PORTABLE_LF: &str = "portable-lf";
pub const ENERGY_MONOTONE_INVARIANT: &str = "energy_monotone_nonincreasing";
pub const CONSERVATION_INVARIANT: &str = "conserved_quantity_constant";
pub const ENERGY_MONOTONE_TOLERANCE: f64 = 1e-12;
pub const CONSERVATION_TOLERANCE: f64 = 1e-9;
pub const BOUNDED_INVARIANT: &str = "bounded_by_initial_maximum";
pub const BOUNDED_TOLERANCE: f64 = 1e-9;
pub const CONSERVED_BAND_INVARIANT: &str = "conserved_within_band";
pub const CONSERVED_BAND_TOLERANCE: f64 = 5e-3;
pub const NON_NEGATIVE_INVARIANT: &str = "non_negative";
pub const NON_NEGATIVE_TOLERANCE: f64 = 1e-9;
pub const ENERGY_IDENTITY_INVARIANT: &str = "energy_identity_residual";
pub const ENERGY_IDENTITY_TOLERANCE: f64 = 1e-9;
pub const RELATION_INVARIANT: &str = "relation_columns_agree";
pub const RELATION_TOLERANCE: f64 = 1e-9;
pub const CROSS_BACKEND_INVARIANT: &str = "cross_backend_columns_agree";
pub const CROSS_BACKEND_TOLERANCE: f64 = 1e-5;
pub const MC_EXECUTED_ESTIMATOR_PROPORTION: &str = "proportion";
pub const MC_INTERVAL_NORMAL_APPROX_95: &str = "normal-approx-95";
pub const MC_INTERVAL_WILSON_95: &str = "wilson-95";
pub const MC_INTERVAL_CLOPPER_PEARSON_95: &str = "clopper-pearson-95";
pub const MC_INTERVAL_Z_95: f64 = 1.959963984540054;
pub const MC_RECOMPUTE_TOLERANCE: f64 = 1e-12;
pub const MC_EXECUTED_NOT_CLAIMED: &[&str] = &[
"sample_independence",
"interval_coverage",
"estimator_semantics",
];
pub const RESEARCH_SOURCE_HASH: &str =
"b3021c14b0e5dc8adeddadf0d22e2780dbf259c349caf5cbc2ba255b591fd7d5";
#[cfg_attr(not(test), allow(dead_code))]
pub const NOT_CLAIMED_BOUNDARY: &[&str] = &[
"numerical_correctness",
"convergence",
"pde_accuracy",
"physical_law",
];
pub const SERIES_EXTRACTION_POLICY: &str =
"whitespace-f64/v1: whitespace-split tokens parsed as finite f64; a non-finite token truncates the series and marks the run diverged; other tokens are skipped";
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ScientificOracle {
pub kind: String,
pub name: String,
pub status: String,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ScientificToolchain {
pub c_compiler: String,
pub c_compiler_version: String,
pub version_output_digest: ScientificDigest,
pub target: String,
pub buildc_binary_digest: ScientificDigest,
pub program_executable_digest: ScientificDigest,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ScientificEffectPolicy {
pub facts_digest: ScientificDigest,
pub observed_capabilities: Vec<String>,
pub reads_stdin: bool,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ScientificWitnessedField {
pub status: String,
pub grounds: String,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ScientificDeterminism {
pub deterministic_modulo_args: bool,
pub grounds: String,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ScientificNumericalMethod {
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub status: String,
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ScientificMonteCarlo {
pub estimator: String,
pub samples: u64,
pub interval_method: String,
pub status: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub estimate: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub interval_low: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub interval_high: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub n_effective: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub successes: Option<u64>,
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ScientificBudget {
pub steps_limit: u64,
pub steps_consumed: u64,
pub exhausted: bool,
pub status: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub wall_seconds_limit: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub wall_exceeded: Option<bool>,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ScientificCrossBackend {
pub secondary_target: String,
pub secondary_toolchain_version: String,
pub secondary_toolchain_digest: ScientificDigest,
pub secondary_executable_digest: ScientificDigest,
pub secondary_raw_stdout_digest: ScientificDigest,
pub secondary_exit_code: i32,
pub status: String,
}
pub fn witnessed_fields_from_capabilities(
observed_capabilities: &[String],
reads_stdin: bool,
sealed_seed: Option<u64>,
) -> (
ScientificWitnessedField,
ScientificWitnessedField,
ScientificDeterminism,
) {
let mut dataset_hazards: Vec<String> = Vec::new();
let mut determinism_hazards: Vec<String> = Vec::new();
let mut uses_random = false;
for cap in observed_capabilities {
let (feeds_dataset, varies) = match cap.as_str() {
"Console" => (reads_stdin, reads_stdin),
"Process" => (false, false),
"Clock" => (false, true),
"Random" => {
uses_random = true;
(false, sealed_seed.is_none())
}
_ => (true, true),
};
let label = if cap == "Console" && reads_stdin {
"stdin".to_string()
} else if cap == "Random" && sealed_seed.is_none() {
"unseeded Random".to_string()
} else {
cap.clone()
};
if feeds_dataset {
dataset_hazards.push(label.clone());
}
if varies {
determinism_hazards.push(label);
}
}
let input_dataset = if dataset_hazards.is_empty() {
ScientificWitnessedField {
status: "NONE_WITNESSED".to_string(),
grounds: "no observed capability can feed an external dataset (no FileSystem, Network, stdin, Environment, Foreign, or GPU access), so the program provably consumed none".to_string(),
}
} else {
ScientificWitnessedField {
status: "POSSIBLE_UNWITNESSED".to_string(),
grounds: format!(
"observed capabilities include {}, which can feed an external dataset; buildc does not track which resources were touched",
dataset_hazards.join(", ")
),
}
};
let seed = match (uses_random, sealed_seed) {
(false, _) => ScientificWitnessedField {
status: "NOT_APPLICABLE".to_string(),
grounds: "the program exercises no Random capability; there is no seed to record"
.to_string(),
},
(true, Some(value)) => ScientificWitnessedField {
status: "SEALED".to_string(),
grounds: format!(
"the Random capability draws from a PRNG seeded with the sealed seed {value}; re-running under the same seed reproduces the stream"
),
},
(true, None) => ScientificWitnessedField {
status: "UNSEEDED".to_string(),
grounds: "the Random capability draws with no sealed seed; emit refuses this state, so a receipt carrying it cannot have come from buildc".to_string(),
},
};
let determinism = if determinism_hazards.is_empty() {
let grounds = if uses_random {
"no observed capability varies between runs given the sealed seed (no Clock, Environment, FileSystem, Network, stdin, Foreign, or GPU access; Random is seeded)".to_string()
} else {
"no observed capability varies between runs (no Clock, Environment, FileSystem, Network, stdin, Foreign, GPU, or unseeded Random access)".to_string()
};
ScientificDeterminism {
deterministic_modulo_args: true,
grounds,
}
} else {
ScientificDeterminism {
deterministic_modulo_args: false,
grounds: format!(
"observed capabilities include {}, which can vary between runs",
determinism_hazards.join(", ")
),
}
};
(input_dataset, seed, determinism)
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ScientificFencedBranch {
pub status: String,
}
impl ScientificFencedBranch {
pub fn fenced() -> Self {
ScientificFencedBranch {
status: "UNAVAILABLE_FENCED".to_string(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ScientificDigest {
pub algorithm: String,
pub hex: String,
}
impl From<&CheckReceiptSourceDigest> for ScientificDigest {
fn from(value: &CheckReceiptSourceDigest) -> Self {
ScientificDigest {
algorithm: value.algorithm.to_string(),
hex: value.hex.clone(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ScientificBuildState {
pub target: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub stdio_mode: Option<String>,
pub compiler_status: String,
pub flags: Vec<String>,
pub toolchain: ScientificToolchain,
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ScientificRuntimeState {
pub os: String,
pub exit_code: i32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub wall_seconds: Option<f64>,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ScientificProblem {
#[serde(skip_serializing_if = "Option::is_none")]
pub label: Option<String>,
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ScientificMeasurement {
pub metric: String,
pub observed_values: Vec<f64>,
pub count: usize,
pub column_count: usize,
pub raw_stdout_digest: ScientificDigest,
pub series_extraction_policy: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub units: Option<String>,
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct InvariantObserved {
pub violation_count: usize,
#[serde(skip_serializing_if = "Option::is_none")]
pub first_violation_step: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub initial_value: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub final_value: Option<f64>,
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ScientificInvariant {
pub name: String,
pub expectation: String,
pub tolerance: f64,
pub observed: InvariantObserved,
pub status: String,
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ScientificProvenance {
pub research_source_hash: String,
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ScientificRuntimeReceipt {
pub schema: String,
pub compiler: String,
pub compiler_version: String,
pub language_version: String,
pub source: String,
pub source_digest: ScientificDigest,
pub input_graph_digest: ScientificDigest,
pub build_state: ScientificBuildState,
pub runtime_state: ScientificRuntimeState,
pub args: Vec<String>,
pub problem: ScientificProblem,
pub oracle: ScientificOracle,
pub effect_policy: ScientificEffectPolicy,
pub input_dataset: ScientificWitnessedField,
pub seed: ScientificWitnessedField,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub seed_value: Option<u64>,
pub determinism: ScientificDeterminism,
pub numerical_method: ScientificNumericalMethod,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub monte_carlo: Option<ScientificMonteCarlo>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub budget: Option<ScientificBudget>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cross_backend: Option<ScientificCrossBackend>,
pub measurement: ScientificMeasurement,
pub invariant: ScientificInvariant,
pub telemetry_branch: ScientificFencedBranch,
pub lineage_branch: ScientificFencedBranch,
pub negative_fixture: bool,
pub diverged: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub not_claimed: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub scope: Vec<String>,
pub labels: Vec<String>,
pub receipt_status: String,
pub seal: ScientificDigest,
pub provenance: ScientificProvenance,
}
pub fn energy_monotone_nonincreasing(series: &[f64], tol: f64) -> InvariantObserved {
let mut violation_count = 0usize;
let mut first_violation_step = None;
for k in 0..series.len().saturating_sub(1) {
if series[k + 1] > series[k] + tol {
violation_count += 1;
if first_violation_step.is_none() {
first_violation_step = Some(k);
}
}
}
InvariantObserved {
violation_count,
first_violation_step,
initial_value: series.first().copied(),
final_value: series.last().copied(),
}
}
pub fn conserved_quantity_constant(series: &[f64], tol: f64) -> InvariantObserved {
let reference = series.first().copied();
let mut violation_count = 0usize;
let mut first_violation_step = None;
if let Some(ref0) = reference {
for (k, &value) in series.iter().enumerate() {
if (value - ref0).abs() > tol {
violation_count += 1;
if first_violation_step.is_none() {
first_violation_step = Some(k);
}
}
}
}
InvariantObserved {
violation_count,
first_violation_step,
initial_value: reference,
final_value: series.last().copied(),
}
}
pub fn bounded_by_initial_maximum(series: &[f64], tol: f64) -> InvariantObserved {
let reference = series.first().copied();
let mut violation_count = 0usize;
let mut first_violation_step = None;
if let Some(ref0) = reference {
for (k, &value) in series.iter().enumerate() {
if value > ref0 + tol {
violation_count += 1;
if first_violation_step.is_none() {
first_violation_step = Some(k);
}
}
}
}
InvariantObserved {
violation_count,
first_violation_step,
initial_value: reference,
final_value: series.last().copied(),
}
}
pub fn energy_identity_residual(series: &[f64], tol: f64) -> InvariantObserved {
let mut violation_count = 0usize;
let mut first_violation_step = None;
for (k, &value) in series.iter().enumerate() {
if value.abs() > tol {
violation_count += 1;
if first_violation_step.is_none() {
first_violation_step = Some(k);
}
}
}
InvariantObserved {
violation_count,
first_violation_step,
initial_value: series.first().copied(),
final_value: series.last().copied(),
}
}
pub fn non_negative(series: &[f64], tol: f64) -> InvariantObserved {
let mut violation_count = 0usize;
let mut first_violation_step = None;
for (k, &value) in series.iter().enumerate() {
if value < -tol {
violation_count += 1;
if first_violation_step.is_none() {
first_violation_step = Some(k);
}
}
}
InvariantObserved {
violation_count,
first_violation_step,
initial_value: series.first().copied(),
final_value: series.last().copied(),
}
}
pub fn invariant_passes(series_len: usize, observed: &InvariantObserved) -> bool {
series_len >= 2 && observed.violation_count == 0
}
pub const KNOWN_INVARIANTS: &[&str] = &[
ENERGY_MONOTONE_INVARIANT,
CONSERVATION_INVARIANT,
BOUNDED_INVARIANT,
ENERGY_IDENTITY_INVARIANT,
RELATION_INVARIANT,
CONSERVED_BAND_INVARIANT,
NON_NEGATIVE_INVARIANT,
CROSS_BACKEND_INVARIANT,
];
pub fn is_known_invariant(name: &str) -> bool {
KNOWN_INVARIANTS.contains(&name)
}
pub fn column_count_matches_invariant(name: &str, column_count: usize, mc_executed: bool) -> bool {
if name == RELATION_INVARIANT {
!mc_executed && column_count >= 2
} else if name == CROSS_BACKEND_INVARIANT {
!mc_executed && column_count == 2
} else if mc_executed {
column_count == 3
} else {
column_count == 1
}
}
pub fn invariant_tolerance(name: &str) -> f64 {
match name {
CONSERVATION_INVARIANT => CONSERVATION_TOLERANCE,
BOUNDED_INVARIANT => BOUNDED_TOLERANCE,
ENERGY_IDENTITY_INVARIANT => ENERGY_IDENTITY_TOLERANCE,
RELATION_INVARIANT => RELATION_TOLERANCE,
CONSERVED_BAND_INVARIANT => CONSERVED_BAND_TOLERANCE,
NON_NEGATIVE_INVARIANT => NON_NEGATIVE_TOLERANCE,
CROSS_BACKEND_INVARIANT => CROSS_BACKEND_TOLERANCE,
_ => ENERGY_MONOTONE_TOLERANCE,
}
}
pub fn invariant_expectation(name: &str) -> &'static str {
match name {
CONSERVATION_INVARIANT => "no step deviates the conserved quantity beyond tolerance",
BOUNDED_INVARIANT => "no step exceeds the initial value beyond tolerance",
ENERGY_IDENTITY_INVARIANT => {
"every step's energy-balance residual stays within tolerance of zero"
}
RELATION_INVARIANT => "every row's columns agree within tolerance",
CONSERVED_BAND_INVARIANT => {
"the quantity stays within the error budget of its initial value"
}
NON_NEGATIVE_INVARIANT => "no value drops below zero beyond tolerance",
CROSS_BACKEND_INVARIANT => {
"every row's two backend columns agree within the cross-backend tolerance"
}
_ => "no step increases energy beyond tolerance",
}
}
pub fn evaluate_invariant(name: &str, series: &[f64], tol: f64) -> InvariantObserved {
match name {
CONSERVATION_INVARIANT | CONSERVED_BAND_INVARIANT => {
conserved_quantity_constant(series, tol)
}
BOUNDED_INVARIANT => bounded_by_initial_maximum(series, tol),
ENERGY_IDENTITY_INVARIANT => energy_identity_residual(series, tol),
NON_NEGATIVE_INVARIANT => non_negative(series, tol),
_ => energy_monotone_nonincreasing(series, tol),
}
}
pub fn relation_columns_agree(
series: &[f64],
tol: f64,
column_count: usize,
) -> (InvariantObserved, usize) {
let ragged = column_count < 2 || series.is_empty() || series.len() % column_count != 0;
let rows = if ragged {
0
} else {
series.len() / column_count
};
let mut violation_count = 0usize;
let mut first_violation_step = None;
for k in 0..rows {
let base = k * column_count;
let col0 = series[base];
let disagrees = (1..column_count).any(|c| (series[base + c] - col0).abs() > tol);
if disagrees {
violation_count += 1;
if first_violation_step.is_none() {
first_violation_step = Some(k);
}
}
}
(
InvariantObserved {
violation_count,
first_violation_step,
initial_value: series.first().copied(),
final_value: series.last().copied(),
},
rows,
)
}
pub struct MeasurementVerdict {
pub observed: InvariantObserved,
pub effective_len: usize,
}
pub fn evaluate_measurement(
name: &str,
series: &[f64],
tol: f64,
column_count: usize,
) -> MeasurementVerdict {
match name {
RELATION_INVARIANT | CROSS_BACKEND_INVARIANT => {
let (observed, rows) = relation_columns_agree(series, tol, column_count);
MeasurementVerdict {
observed,
effective_len: rows,
}
}
_ if column_count == 3 => {
let ragged = series.is_empty() || series.len() % 3 != 0;
let col0: Vec<f64> = if ragged {
Vec::new()
} else {
series.iter().step_by(3).copied().collect()
};
let rows = col0.len();
MeasurementVerdict {
observed: evaluate_invariant(name, &col0, tol),
effective_len: rows,
}
}
_ => MeasurementVerdict {
observed: evaluate_invariant(name, series, tol),
effective_len: series.len(),
},
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct McExecutedComputed {
pub estimate: f64,
pub interval_low: f64,
pub interval_high: f64,
pub n_effective: u64,
pub successes: u64,
}
pub fn compute_mc_executed(
series: &[f64],
column_count: usize,
samples: u64,
interval_method: &str,
) -> Result<McExecutedComputed, String> {
const MAX_EXACT_INTEGER: f64 = 9007199254740992.0;
if column_count != 3 {
return Err(format!(
"column_count {column_count} is not 3: an EXECUTED monte_carlo receipt requires exactly three columns per row"
));
}
if series.is_empty() || series.len() % 3 != 0 {
return Err(format!(
"series length {} is not a positive multiple of 3: ragged rows cannot witness a Bernoulli count",
series.len()
));
}
let rows = series.len() / 3;
let mut prev_trials: Option<f64> = None;
let mut prev_successes: Option<f64> = None;
for k in 0..rows {
let successes_k = series[k * 3 + 1];
let trials_k = series[k * 3 + 2];
for (label, value) in [("successes", successes_k), ("trials", trials_k)] {
if value.fract() != 0.0 || !value.is_finite() || value.abs() >= MAX_EXACT_INTEGER {
return Err(format!(
"row {k}: {label} = {value} is not an exact non-negative integer below 2^53"
));
}
}
if let Some(prev) = prev_trials {
if trials_k != prev + 1.0 {
return Err(format!(
"row {k}: trials {trials_k} does not follow row {}'s trials {prev} by exactly 1",
k - 1
));
}
}
if let Some(prev) = prev_successes {
let delta = successes_k - prev;
if delta != 0.0 && delta != 1.0 {
return Err(format!(
"row {k}: successes {successes_k} does not follow row {}'s successes {prev} by 0 or 1",
k - 1
));
}
}
if successes_k > trials_k {
return Err(format!(
"row {k}: successes {successes_k} exceeds trials {trials_k}"
));
}
prev_trials = Some(trials_k);
prev_successes = Some(successes_k);
}
let trials_final = prev_trials.expect("rows > 0, checked above") as u64;
let successes_final = prev_successes.expect("rows > 0, checked above") as u64;
if trials_final != samples {
return Err(format!(
"witnessed final trials {trials_final} does not equal the declared samples {samples}: the denominator is not witnessed"
));
}
let n = trials_final as f64;
let p_hat = successes_final as f64 / n;
let z = MC_INTERVAL_Z_95;
let (interval_low, interval_high) = match interval_method {
MC_INTERVAL_NORMAL_APPROX_95 => {
if successes_final == 0 || successes_final == trials_final {
return Err(format!(
"normal-approx-95 is degenerate at the boundary proportion (successes = {successes_final} of {trials_final}); use wilson-95"
));
}
let half_width = z * (p_hat * (1.0 - p_hat) / n).sqrt();
(p_hat - half_width, p_hat + half_width)
}
MC_INTERVAL_WILSON_95 => {
let z2 = z * z;
let denom = 1.0 + z2 / n;
let center = (p_hat + z2 / (2.0 * n)) / denom;
let margin = (z / denom) * (p_hat * (1.0 - p_hat) / n + z2 / (4.0 * n * n)).sqrt();
(center - margin, center + margin)
}
other => {
return Err(format!(
"interval_method `{other}` is not in the EXECUTED executable vocabulary (v1: normal-approx-95, wilson-95); `{MC_INTERVAL_CLOPPER_PEARSON_95}` needs a verified inverse incomplete beta and is not executable"
));
}
};
Ok(McExecutedComputed {
estimate: p_hat,
interval_low,
interval_high,
n_effective: trials_final,
successes: successes_final,
})
}
pub struct ScientificReceiptInputs<'a> {
pub source_path: &'a Path,
pub compiler_version: &'a str,
pub language_version: String,
pub source_digest: ScientificDigest,
pub input_graph_digest: ScientificDigest,
pub target: &'a str,
pub stdio_mode: Option<&'a str>,
pub os: &'a str,
pub exit_code: i32,
pub wall_seconds: Option<f64>,
pub toolchain: ScientificToolchain,
pub effect_policy: ScientificEffectPolicy,
pub method_description: Option<String>,
pub raw_stdout_digest: ScientificDigest,
pub series: Vec<f64>,
pub series_parsed: bool,
pub diverged: bool,
pub args: Vec<String>,
pub seed_value: Option<u64>,
pub monte_carlo: Option<ScientificMonteCarlo>,
pub budget: Option<ScientificBudget>,
pub cross_backend: Option<ScientificCrossBackend>,
pub invariant_name: String,
pub metric: String,
pub units: Option<String>,
pub column_count: usize,
pub problem_label: Option<String>,
pub negative_fixture: bool,
pub flags: Vec<String>,
}
pub fn build_scientific_runtime_receipt(
inputs: ScientificReceiptInputs<'_>,
) -> ScientificRuntimeReceipt {
let ScientificReceiptInputs {
source_path,
compiler_version,
language_version,
source_digest,
input_graph_digest,
target,
stdio_mode,
os,
exit_code,
wall_seconds,
toolchain,
effect_policy,
method_description,
raw_stdout_digest,
series,
series_parsed,
diverged,
args,
seed_value,
monte_carlo,
budget,
cross_backend,
invariant_name,
metric,
units,
column_count,
problem_label,
negative_fixture,
flags,
} = inputs;
let tolerance = invariant_tolerance(&invariant_name);
let verdict = evaluate_measurement(&invariant_name, &series, tolerance, column_count);
let observed = verdict.observed;
let has_series = series_parsed && !series.is_empty();
let passes = has_series && !diverged && invariant_passes(verdict.effective_len, &observed);
let invariant_status = if passes { "PASS" } else { "FAIL" };
let receipt_status = if !has_series || diverged {
"UNVERIFIABLE"
} else if passes {
"PASS"
} else if negative_fixture {
"FAIL_EXPECTED"
} else {
"FAIL_UNEXPECTED"
};
let mut labels = vec![LABEL_NUMERICAL_RESULT.to_string()];
if negative_fixture {
labels.push("NEGATIVE_FIXTURE".to_string());
}
if diverged {
labels.push("NONFINITE_OBSERVED".to_string());
}
let mut scope: Vec<String> = SCOPE_BASE.iter().map(|s| s.to_string()).collect();
if budget.is_some() {
labels.push(LABEL_BEST_FOUND_WITHIN_BUDGET.to_string());
scope.push(SCOPE_BUDGET.to_string());
}
if let Some(mc) = &monte_carlo {
if mc.status == "EXECUTED" {
scope.extend(MC_EXECUTED_SCOPE.iter().map(|s| s.to_string()));
}
}
let (input_dataset, seed, determinism) = witnessed_fields_from_capabilities(
&effect_policy.observed_capabilities,
effect_policy.reads_stdin,
seed_value,
);
let count = series.len();
let mut receipt = ScientificRuntimeReceipt {
schema: SCIENTIFIC_RUNTIME_SCHEMA.to_string(),
compiler: "buildc".to_string(),
compiler_version: compiler_version.to_string(),
language_version,
source: source_path.to_string_lossy().to_string(),
source_digest,
input_graph_digest,
build_state: ScientificBuildState {
target: target.to_string(),
stdio_mode: stdio_mode
.filter(|mode| *mode != SCIENTIFIC_STDIO_MODE_NATIVE)
.map(str::to_string),
compiler_status: "compiled_and_executed".to_string(),
flags,
toolchain,
},
runtime_state: ScientificRuntimeState {
os: os.to_string(),
exit_code,
wall_seconds,
},
args,
problem: ScientificProblem {
label: problem_label,
},
oracle: ScientificOracle {
kind: "declared_invariant".to_string(),
name: invariant_name.clone(),
status: "DECLARED".to_string(),
},
input_dataset,
seed,
seed_value,
determinism,
numerical_method: ScientificNumericalMethod {
status: if method_description.is_some() {
"DECLARED".to_string()
} else {
"UNDECLARED".to_string()
},
description: method_description,
},
monte_carlo,
budget,
cross_backend,
effect_policy,
measurement: ScientificMeasurement {
metric,
observed_values: series,
count,
column_count,
raw_stdout_digest,
series_extraction_policy: SERIES_EXTRACTION_POLICY.to_string(),
units,
},
invariant: ScientificInvariant {
name: invariant_name.clone(),
expectation: invariant_expectation(&invariant_name).to_string(),
tolerance,
observed,
status: invariant_status.to_string(),
},
telemetry_branch: ScientificFencedBranch::fenced(),
lineage_branch: ScientificFencedBranch::fenced(),
negative_fixture,
diverged,
not_claimed: Vec::new(),
scope,
labels,
receipt_status: receipt_status.to_string(),
seal: ScientificDigest {
algorithm: "sha256".to_string(),
hex: String::new(),
},
provenance: ScientificProvenance {
research_source_hash: RESEARCH_SOURCE_HASH.to_string(),
},
};
seal_receipt(&mut receipt);
receipt
}
pub fn seal_receipt(receipt: &mut ScientificRuntimeReceipt) {
receipt.seal.algorithm = "sha256".to_string();
receipt.seal.hex.clear();
let canonical = serde_json::to_vec(receipt).expect("serialize scientific-runtime receipt");
receipt.seal.hex = source_digest_hex(&canonical);
}
pub fn recompute_seal_hex(receipt: &ScientificRuntimeReceipt) -> String {
let mut probe = receipt.clone();
probe.seal.algorithm = "sha256".to_string();
probe.seal.hex.clear();
let canonical = serde_json::to_vec(&probe).expect("serialize scientific-runtime receipt");
source_digest_hex(&canonical)
}
#[derive(Clone, Debug)]
pub struct SelfTestCase {
pub label: String,
pub tampered: serde_json::Value,
pub expected_class: String,
#[cfg_attr(not(test), allow(dead_code))]
pub resealed: bool,
}
fn reseal_json(value: &serde_json::Value) -> Result<serde_json::Value, String> {
let mut receipt: ScientificRuntimeReceipt = serde_json::from_value(value.clone())
.map_err(|e| format!("re-seal target is not a scientific-runtime receipt: {e}"))?;
seal_receipt(&mut receipt);
serde_json::to_value(&receipt).map_err(|e| format!("re-serialize after re-seal failed: {e}"))
}
pub fn build_self_test_cases(
receipt_json: &serde_json::Value,
) -> Result<Vec<SelfTestCase>, String> {
let _: ScientificRuntimeReceipt = serde_json::from_value(receipt_json.clone())
.map_err(|e| format!("self-test input is not a scientific-runtime receipt: {e}"))?;
let mut cases = Vec::new();
{
let mut v = receipt_json.clone();
v["compiler"] = serde_json::Value::String("gcc".to_string());
cases.push(SelfTestCase {
label: "compiler tag changed to a foreign compiler".to_string(),
tampered: v,
expected_class: "COMPILER_MISMATCH".to_string(),
resealed: false,
});
}
{
let mut v = receipt_json.clone();
let cur = v["invariant"]["observed"]["violation_count"]
.as_i64()
.unwrap_or(0);
v["invariant"]["observed"]["violation_count"] = serde_json::Value::from(cur + 7);
cases.push(SelfTestCase {
label: "observed violation_count edited without re-sealing".to_string(),
tampered: v,
expected_class: "SEAL_MISMATCH".to_string(),
resealed: false,
});
}
{
let mut v = receipt_json.clone();
if let Some(obj) = v.as_object_mut() {
obj.remove("args");
}
cases.push(SelfTestCase {
label: "required `args` field removed".to_string(),
tampered: v,
expected_class: "MALFORMED".to_string(),
resealed: false,
});
}
{
let mut v = receipt_json.clone();
let tol = v["invariant"]["tolerance"].as_f64().unwrap_or(1e-9);
v["invariant"]["tolerance"] = serde_json::Value::from(tol * 10.0 + 1.0);
let v = reseal_json(&v)?;
cases.push(SelfTestCase {
label: "sealed tolerance loosened then re-sealed".to_string(),
tampered: v,
expected_class: "FIELD_CONTRACT_VIOLATION".to_string(),
resealed: true,
});
}
{
let mut v = receipt_json.clone();
v["invariant"]["name"] = serde_json::Value::String("not_a_real_invariant".to_string());
let v = reseal_json(&v)?;
cases.push(SelfTestCase {
label: "invariant name replaced with an unknown invariant".to_string(),
tampered: v,
expected_class: "INVARIANT_UNSUPPORTED".to_string(),
resealed: true,
});
}
{
let mut v = receipt_json.clone();
if v.get("seed_value").is_none() {
v["seed_value"] = serde_json::Value::from(7u64);
} else if let Some(obj) = v.as_object_mut() {
obj.remove("seed_value");
}
let v = reseal_json(&v)?;
cases.push(SelfTestCase {
label: "sealed seed_value flipped against the program's capabilities".to_string(),
tampered: v,
expected_class: "FIELD_CONTRACT_VIOLATION".to_string(),
resealed: true,
});
}
{
let mut v = receipt_json.clone();
match v.get_mut("monte_carlo") {
Some(mc) if !mc.is_null() => {
mc["samples"] = serde_json::Value::from(0u64);
}
_ => {
v["monte_carlo"] = serde_json::json!({
"estimator": "mean",
"samples": 0u64,
"interval_method": "normal-approx-95",
"status": "DECLARED",
});
}
}
let v = reseal_json(&v)?;
cases.push(SelfTestCase {
label: "monte_carlo block with a zero sample denominator".to_string(),
tampered: v,
expected_class: "FIELD_CONTRACT_VIOLATION".to_string(),
resealed: true,
});
}
{
let mut v = receipt_json.clone();
match v.get_mut("budget") {
Some(budget) if !budget.is_null() => {
let limit = budget["steps_limit"].as_u64().unwrap_or(10);
budget["steps_consumed"] = serde_json::Value::from(limit + 1);
}
_ => {
v["budget"] = serde_json::json!({
"steps_limit": 10u64,
"steps_consumed": 11u64,
"exhausted": false,
"status": "DECLARED",
});
}
}
let v = reseal_json(&v)?;
cases.push(SelfTestCase {
label: "budget block with steps_consumed above steps_limit".to_string(),
tampered: v,
expected_class: "FIELD_CONTRACT_VIOLATION".to_string(),
resealed: true,
});
}
{
let mut v = receipt_json.clone();
match v.get("cross_backend") {
Some(cb) if !cb.is_null() => {
if let Some(obj) = v.as_object_mut() {
obj.remove("cross_backend");
}
}
_ => {
v["cross_backend"] = serde_json::json!({
"secondary_target": "rust",
"secondary_toolchain_version": "rustc 0.0.0 (self-test)",
"secondary_toolchain_digest": {"algorithm": "sha256", "hex": "a".repeat(64)},
"secondary_executable_digest": {"algorithm": "sha256", "hex": "a".repeat(64)},
"secondary_raw_stdout_digest": {"algorithm": "sha256", "hex": "a".repeat(64)},
"secondary_exit_code": 0,
"status": "EXECUTED",
});
}
}
let v = reseal_json(&v)?;
cases.push(SelfTestCase {
label: "cross_backend block swapped against the invariant/block biconditional"
.to_string(),
tampered: v,
expected_class: "FIELD_CONTRACT_VIOLATION".to_string(),
resealed: true,
});
}
{
let mut v = receipt_json.clone();
match v.get_mut("monte_carlo") {
Some(mc)
if !mc.is_null()
&& mc.get("status").and_then(|s| s.as_str()) == Some("EXECUTED") =>
{
let bumped = mc["interval_high"].as_f64().unwrap_or(0.5) + 0.25;
mc["interval_high"] = serde_json::Value::from(bumped);
}
_ => {
v["monte_carlo"] = serde_json::json!({
"estimator": "proportion",
"samples": 4u64,
"interval_method": "wilson-95",
"status": "EXECUTED",
"estimate": 0.5,
"interval_low": 0.15,
"interval_high": 0.85,
"n_effective": 4u64,
"successes": 2u64,
});
}
}
let v = reseal_json(&v)?;
cases.push(SelfTestCase {
label: "EXECUTED monte_carlo interval_high nudged against its Stage A recompute"
.to_string(),
tampered: v,
expected_class: "FIELD_CONTRACT_VIOLATION".to_string(),
resealed: true,
});
}
Ok(cases)
}
pub const RECEIPT_CHAIN_SCHEMA: &str = "buildlang-scientific-receipt-chain/v0";
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ReceiptChainLink {
pub index: usize,
pub receipt: String,
pub source: String,
pub receipt_seal: String,
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ReceiptChainManifest {
pub schema: String,
pub links: Vec<ReceiptChainLink>,
pub chain_seal: ScientificDigest,
}
pub fn receipt_chain_seal_hex(links: &[ReceiptChainLink]) -> String {
let ordered: Vec<(usize, &str)> = links
.iter()
.map(|l| (l.index, l.receipt_seal.as_str()))
.collect();
let canonical = serde_json::to_vec(&ordered).expect("serialize chain seal input");
source_digest_hex(&canonical)
}
pub fn build_receipt_chain(members: &[(String, String, String)]) -> ReceiptChainManifest {
let links: Vec<ReceiptChainLink> = members
.iter()
.enumerate()
.map(|(index, (receipt, source, seal))| ReceiptChainLink {
index,
receipt: receipt.clone(),
source: source.clone(),
receipt_seal: seal.clone(),
})
.collect();
let hex = receipt_chain_seal_hex(&links);
ReceiptChainManifest {
schema: RECEIPT_CHAIN_SCHEMA.to_string(),
links,
chain_seal: ScientificDigest {
algorithm: "sha256".to_string(),
hex,
},
}
}
pub const RECEIPT_CORPUS_SCHEMA: &str = "buildlang-scientific-receipt-corpus/v0";
fn default_corpus_columns() -> u32 {
1
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ScientificCorpusMember {
pub source: String,
pub invariant: String,
#[serde(default = "default_corpus_columns")]
pub columns: u32,
#[serde(default)]
pub negative_fixture: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub seed: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub mc_estimator: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub mc_samples: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub mc_interval: Option<String>,
#[serde(default)]
pub mc_executed: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub budget_steps: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub budget_consumed: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cross_backend: Option<String>,
pub expected_status: String,
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ScientificCorpusManifest {
pub schema: String,
pub members: Vec<ScientificCorpusMember>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ParsedSeries {
pub series: Vec<f64>,
pub any_parsed: bool,
pub diverged: bool,
}
fn is_nonfinite_spelling(token: &str) -> bool {
let t = token.trim_start_matches(['+', '-']).to_ascii_lowercase();
t.starts_with("nan(")
|| t.starts_with("1.#inf")
|| t.starts_with("1.#ind")
|| t.starts_with("1.#qnan")
|| t.starts_with("1.#snan")
}
pub fn parse_numeric_series(stdout: &str) -> ParsedSeries {
let mut series = Vec::new();
let mut any_parsed = false;
let mut diverged = false;
for token in stdout.split_whitespace() {
match token.parse::<f64>() {
Ok(value) if value.is_finite() => {
series.push(value);
any_parsed = true;
}
Ok(_) => {
any_parsed = true;
diverged = true;
break;
}
Err(_) => {
if is_nonfinite_spelling(token) {
any_parsed = true;
diverged = true;
break;
}
}
}
}
ParsedSeries {
series,
any_parsed,
diverged,
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RecomputedVerdict {
pub invariant_status: &'static str,
pub violation_count: usize,
pub receipt_status: &'static str,
}
pub fn recompute_verdict(
invariant_name: &str,
series: &[f64],
series_parsed: bool,
diverged: bool,
negative_fixture: bool,
column_count: usize,
) -> RecomputedVerdict {
let verdict = evaluate_measurement(
invariant_name,
series,
invariant_tolerance(invariant_name),
column_count,
);
let observed = verdict.observed;
let has_series = series_parsed && !series.is_empty();
let passes = has_series && !diverged && invariant_passes(verdict.effective_len, &observed);
let invariant_status = if passes { "PASS" } else { "FAIL" };
let receipt_status = if !has_series || diverged {
"UNVERIFIABLE"
} else if passes {
"PASS"
} else if negative_fixture {
"FAIL_EXPECTED"
} else {
"FAIL_UNEXPECTED"
};
RecomputedVerdict {
invariant_status,
violation_count: observed.violation_count,
receipt_status,
}
}
pub struct RederivedFacts {
pub source_digest: ScientificDigest,
pub input_graph_digest: ScientificDigest,
pub effect_policy: ScientificEffectPolicy,
}
pub struct SecondaryObservation {
pub parsed: ParsedSeries,
pub exit_code: i32,
pub raw_stdout_digest: ScientificDigest,
pub executable_digest: ScientificDigest,
pub probed_toolchain_version: String,
pub probed_toolchain_digest: ScientificDigest,
}
pub struct RerunObservation {
pub parsed: ParsedSeries,
pub exit_code: i32,
pub secondary: Option<SecondaryObservation>,
pub raw_stdout_digest: ScientificDigest,
pub executable_digest: ScientificDigest,
pub wall_seconds: f64,
}
#[allow(clippy::too_many_arguments)]
pub fn evaluate_scientific_runtime_receipt(
receipt_json: &serde_json::Value,
source_override: Option<&Path>,
json: bool,
current_compiler_version: &str,
current_language_version: &str,
probed_toolchain: Option<&ScientificToolchain>,
rederive_digests: impl FnOnce(&Path) -> Result<RederivedFacts, i32>,
rerun_series: impl FnOnce(
&Path,
&[String],
Option<u64>,
Option<&str>,
&str,
) -> Result<RerunObservation, i32>,
) -> Result<ScientificVerifyReport, i32> {
let receipt: ScientificRuntimeReceipt =
serde_json::from_value(receipt_json.clone()).map_err(|err| {
eprintln!("Error: scientific-runtime receipt is malformed: {}", err);
verify_failure_class(json, "MALFORMED", 1)
})?;
if !is_scientific_runtime_schema(&receipt.schema) {
eprintln!(
"Error: unsupported scientific-runtime receipt schema `{}`",
receipt.schema
);
return Err(verify_failure_class(json, "SCHEMA_UNSUPPORTED", 1));
}
if receipt.compiler != "buildc" {
eprintln!(
"Error: receipt compiler mismatch: expected buildc, got {}",
receipt.compiler
);
return Err(verify_failure_class(json, "COMPILER_MISMATCH", 1));
}
let recomputed_seal = recompute_seal_hex(&receipt);
if !recomputed_seal.eq_ignore_ascii_case(&receipt.seal.hex) {
eprintln!(
"Error: seal mismatch: receipt sha256:{}, recomputed sha256:{}",
receipt.seal.hex, recomputed_seal
);
return Err(verify_failure_class(json, "SEAL_MISMATCH", 1));
}
let vocab = receipt_vocabulary(&receipt.schema);
let (entries, other) = if vocab.list_name == "scope" {
(&receipt.scope, &receipt.not_claimed)
} else {
(&receipt.not_claimed, &receipt.scope)
};
if !other.is_empty() {
eprintln!(
"Error: a `{}` receipt carries only the `{}` list",
receipt.schema, vocab.list_name
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
if !entries.iter().any(|c| c == vocab.required_entry) {
eprintln!(
"Error: the receipt's `{}` list must include `{}`, got {:?}",
vocab.list_name, vocab.required_entry, entries
);
return Err(verify_failure_class(json, vocab.missing_class, 1));
}
if !receipt.labels.iter().any(|l| l == vocab.numerical_label) {
eprintln!(
"Error: the receipt's labels must include {}, got {:?}",
vocab.numerical_label, receipt.labels
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
let sealed_policy_tag = receipt
.measurement
.series_extraction_policy
.split(':')
.next()
.unwrap_or("");
let verifier_policy_tag = SERIES_EXTRACTION_POLICY.split(':').next().unwrap_or("");
if sealed_policy_tag != verifier_policy_tag {
eprintln!(
"Error: series extraction policy mismatch: receipt `{}`, this verifier `{}`",
receipt.measurement.series_extraction_policy, SERIES_EXTRACTION_POLICY
);
return Err(verify_failure_class(json, "EXTRACTION_POLICY_MISMATCH", 1));
}
let sealed_stdio_mode = receipt
.build_state
.stdio_mode
.as_deref()
.unwrap_or(SCIENTIFIC_STDIO_MODE_NATIVE);
if !matches!(
sealed_stdio_mode,
SCIENTIFIC_STDIO_MODE_NATIVE | SCIENTIFIC_STDIO_MODE_PORTABLE_LF
) {
eprintln!(
"Error: build_state.stdio_mode `{sealed_stdio_mode}` is unsupported; supported: native, portable-lf"
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
if sealed_stdio_mode == SCIENTIFIC_STDIO_MODE_PORTABLE_LF && receipt.build_state.target != "c" {
eprintln!(
"Error: build_state.stdio_mode portable-lf is supported only by the C backend; receipt target `{}` does not use the C runtime",
receipt.build_state.target
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
for (field, digest) in [
("source_digest", &receipt.source_digest),
("input_graph_digest", &receipt.input_graph_digest),
(
"build_state.toolchain.version_output_digest",
&receipt.build_state.toolchain.version_output_digest,
),
(
"build_state.toolchain.buildc_binary_digest",
&receipt.build_state.toolchain.buildc_binary_digest,
),
(
"build_state.toolchain.program_executable_digest",
&receipt.build_state.toolchain.program_executable_digest,
),
(
"measurement.raw_stdout_digest",
&receipt.measurement.raw_stdout_digest,
),
] {
if !digest_is_well_formed(digest) {
eprintln!(
"Error: malformed digest in `{}`: algorithm `{}`, hex `{}` (must be sha256 with 64 hex chars)",
field, digest.algorithm, digest.hex
);
return Err(verify_failure_class(json, "DIGEST_MALFORMED", 1));
}
}
if receipt.oracle.kind != "declared_invariant" {
eprintln!(
"Error: unsupported oracle kind `{}` (this verifier re-checks `declared_invariant` only)",
receipt.oracle.kind
);
return Err(verify_failure_class(json, "ORACLE_KIND_UNSUPPORTED", 1));
}
if receipt.oracle.status != "DECLARED" {
eprintln!(
"Error: unsupported oracle status `{}` for kind `declared_invariant` (a declared oracle cannot claim an executed status)",
receipt.oracle.status
);
return Err(verify_failure_class(json, "ORACLE_STATUS_UNSUPPORTED", 1));
}
if !is_known_invariant(&receipt.invariant.name) {
eprintln!(
"Error: unsupported invariant `{}` (this verifier implements {})",
receipt.invariant.name,
KNOWN_INVARIANTS.join(", ")
);
return Err(verify_failure_class(json, "INVARIANT_UNSUPPORTED", 1));
}
if receipt.oracle.name != receipt.invariant.name {
eprintln!(
"Error: oracle binding mismatch: oracle names `{}`, invariant is `{}`",
receipt.oracle.name, receipt.invariant.name
);
return Err(verify_failure_class(json, "ORACLE_BINDING_MISMATCH", 1));
}
let canonical_tolerance = invariant_tolerance(&receipt.invariant.name);
if receipt.invariant.tolerance != canonical_tolerance {
eprintln!(
"Error: invariant tolerance mismatch: receipt {}, canonical {} for `{}`",
receipt.invariant.tolerance, canonical_tolerance, receipt.invariant.name
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
let mc_executed_for_columns = receipt
.monte_carlo
.as_ref()
.is_some_and(|mc| mc.status == "EXECUTED");
if !column_count_matches_invariant(
&receipt.invariant.name,
receipt.measurement.column_count,
mc_executed_for_columns,
) {
eprintln!(
"Error: column_count {} violates the contract for invariant `{}` (relation requires >= 2, an EXECUTED monte_carlo receipt requires exactly 3, every other single-scalar invariant requires exactly 1)",
receipt.measurement.column_count, receipt.invariant.name
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
if receipt.telemetry_branch.status != "UNAVAILABLE_FENCED"
|| receipt.lineage_branch.status != "UNAVAILABLE_FENCED"
{
eprintln!(
"Error: unexpected fence status: telemetry `{}`, lineage `{}` (v0 produces neither; the only valid value is UNAVAILABLE_FENCED)",
receipt.telemetry_branch.status, receipt.lineage_branch.status
);
return Err(verify_failure_class(json, "FENCE_STATUS_UNEXPECTED", 1));
}
let method_consistent = match receipt.numerical_method.status.as_str() {
"DECLARED" => receipt.numerical_method.description.is_some(),
"UNDECLARED" => receipt.numerical_method.description.is_none(),
_ => false,
};
if !method_consistent {
eprintln!(
"Error: numerical_method status `{}` is inconsistent with its description (present: {})",
receipt.numerical_method.status,
receipt.numerical_method.description.is_some()
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
if receipt.compiler_version != current_compiler_version {
eprintln!(
"Warning: compiler version differs: receipt {}, current {} (re-checking the verdict anyway)",
receipt.compiler_version, current_compiler_version
);
}
if receipt.language_version != current_language_version {
eprintln!(
"Warning: language version differs: receipt {}, current {} (re-checking the verdict anyway)",
receipt.language_version, current_language_version
);
}
let source_path = match source_override {
Some(path) => path.to_path_buf(),
None => Path::new(&receipt.source).to_path_buf(),
};
let rederived = rederive_digests(&source_path)
.map_err(|code| verify_failure_class(json, "REDERIVATION_FAILED", code))?;
let (source_digest, input_graph_digest) =
(rederived.source_digest, rederived.input_graph_digest);
if !digests_match(&source_digest, &receipt.source_digest) {
eprintln!(
"Error: source digest mismatch: receipt {}:{}, actual {}:{}",
receipt.source_digest.algorithm,
receipt.source_digest.hex,
source_digest.algorithm,
source_digest.hex
);
return Err(verify_failure_class(json, "SOURCE_DIGEST_MISMATCH", 1));
}
if !digests_match(&input_graph_digest, &receipt.input_graph_digest) {
eprintln!(
"Error: input graph digest mismatch: receipt {}:{}, actual {}:{}",
receipt.input_graph_digest.algorithm,
receipt.input_graph_digest.hex,
input_graph_digest.algorithm,
input_graph_digest.hex
);
return Err(verify_failure_class(json, "INPUT_GRAPH_DIGEST_MISMATCH", 1));
}
if !digests_match(
&rederived.effect_policy.facts_digest,
&receipt.effect_policy.facts_digest,
) || rederived.effect_policy.observed_capabilities
!= receipt.effect_policy.observed_capabilities
|| rederived.effect_policy.reads_stdin != receipt.effect_policy.reads_stdin
{
eprintln!(
"Error: effect policy drift: receipt capabilities {:?} reads_stdin={} (facts sha256:{}), re-derived {:?} reads_stdin={} (facts sha256:{})",
receipt.effect_policy.observed_capabilities,
receipt.effect_policy.reads_stdin,
receipt.effect_policy.facts_digest.hex,
rederived.effect_policy.observed_capabilities,
rederived.effect_policy.reads_stdin,
rederived.effect_policy.facts_digest.hex
);
return Err(verify_failure_class(json, "EFFECT_POLICY_DRIFT", 1));
}
if rederived
.effect_policy
.observed_capabilities
.iter()
.any(|cap| cap == "Model")
{
eprintln!(
"Error: the program uses the Model capability, so a scientific receipt cannot cover this run. Check the model output with a model-free oracled kernel."
);
return Err(verify_failure_class(json, "CAPABILITY_INADMISSIBLE", 1));
}
let rederived_uses_random = rederived
.effect_policy
.observed_capabilities
.iter()
.any(|cap| cap == "Random");
if rederived_uses_random && receipt.seed_value.is_none() {
eprintln!(
"Error: the program observes the Random capability but the receipt seals no seed_value (a Random-using run requires `--seed N`, and the receipt must carry it)"
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
if !rederived_uses_random && receipt.seed_value.is_some() {
eprintln!(
"Error: the receipt seals seed_value {} but the program observes no Random capability (nothing draws from a seed)",
receipt.seed_value.unwrap_or_default()
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
if let Some(mc) = &receipt.monte_carlo {
if !rederived_uses_random || receipt.seed_value.is_none() {
eprintln!(
"Error: the receipt carries a monte_carlo block but the program is not a seeded Random run (an MC estimate needs the Random capability and a sealed seed to re-derive)"
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
if mc.samples == 0 {
eprintln!(
"Error: monte_carlo.samples is 0: an MC claim without its denominator is unpriceable"
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
if mc.estimator.trim().is_empty() || mc.interval_method.trim().is_empty() {
eprintln!(
"Error: monte_carlo declares an empty estimator or interval_method: the claim is the interval, never the point, so both must be named"
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
let executed_fields_present = mc.estimate.is_some()
|| mc.interval_low.is_some()
|| mc.interval_high.is_some()
|| mc.n_effective.is_some()
|| mc.successes.is_some();
match mc.status.as_str() {
"DECLARED" => {
if executed_fields_present {
eprintln!(
"Error: monte_carlo.status is DECLARED but an executed field is present: a DECLARED block never carries estimate/interval_low/interval_high/n_effective/successes"
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
}
"EXECUTED" => {
let (
Some(estimate),
Some(interval_low),
Some(interval_high),
Some(n_effective),
Some(successes),
) = (
mc.estimate,
mc.interval_low,
mc.interval_high,
mc.n_effective,
mc.successes,
)
else {
eprintln!(
"Error: monte_carlo.status is EXECUTED but not all five executed fields (estimate, interval_low, interval_high, n_effective, successes) are present"
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
};
if mc.estimator != MC_EXECUTED_ESTIMATOR_PROPORTION {
eprintln!(
"Error: EXECUTED monte_carlo estimator `{}` is not in the executable vocabulary (v1: proportion)",
mc.estimator
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
let computed = compute_mc_executed(
&receipt.measurement.observed_values,
receipt.measurement.column_count,
mc.samples,
&mc.interval_method,
)
.map_err(|reason| {
eprintln!(
"Error: EXECUTED monte_carlo Stage A (sealed series, no re-run) recompute failed: {reason}"
);
verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1)
})?;
if computed.n_effective != n_effective || n_effective != mc.samples {
eprintln!(
"Error: sealed n_effective {n_effective} does not match the Stage A witnessed denominator {} (declared samples {})",
computed.n_effective, mc.samples
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
if computed.successes != successes {
eprintln!(
"Error: sealed successes {successes} does not match the Stage A recompute {}",
computed.successes
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
if (computed.estimate - estimate).abs() > MC_RECOMPUTE_TOLERANCE
|| (computed.interval_low - interval_low).abs() > MC_RECOMPUTE_TOLERANCE
|| (computed.interval_high - interval_high).abs() > MC_RECOMPUTE_TOLERANCE
{
eprintln!(
"Error: sealed EXECUTED interval fields do not match the Stage A recompute over the sealed series (estimate {estimate} vs {}, low {interval_low} vs {}, high {interval_high} vs {})",
computed.estimate, computed.interval_low, computed.interval_high
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
}
other => {
eprintln!(
"Error: monte_carlo.status `{other}` is not expressible (only DECLARED or EXECUTED)"
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
}
}
if let Some(budget) = &receipt.budget {
if budget.steps_limit == 0 {
eprintln!("Error: budget.steps_limit is 0: a zero ceiling is not a budget");
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
if budget.steps_consumed > budget.steps_limit {
eprintln!(
"Error: budget.steps_consumed {} exceeds budget.steps_limit {}: a consumption above its ceiling is incoherent",
budget.steps_consumed, budget.steps_limit
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
if budget.exhausted != (budget.steps_consumed == budget.steps_limit) {
eprintln!(
"Error: budget.exhausted `{}` disagrees with steps_consumed == steps_limit ({} == {}): exhausted is DERIVED, never hand-set",
budget.exhausted, budget.steps_consumed, budget.steps_limit
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
if budget.status != "DECLARED" {
eprintln!(
"Error: budget.status `{}` is not expressible: v0 declares the facts, it does not execute them (the only valid status is DECLARED)",
budget.status
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
if let Some(limit) = budget.wall_seconds_limit {
if limit <= 0.0 || !limit.is_finite() {
eprintln!(
"Error: budget.wall_seconds_limit {} is not a positive, finite value",
limit
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
let Some(sealed_wall) = receipt.runtime_state.wall_seconds else {
eprintln!(
"Error: budget.wall_seconds_limit is declared but runtime_state.wall_seconds is not sealed: a wall ceiling needs a measured wall time to derive wall_exceeded against"
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
};
let expected_exceeded = sealed_wall > limit;
if budget.wall_exceeded != Some(expected_exceeded) {
eprintln!(
"Error: budget.wall_exceeded {:?} disagrees with sealed wall_seconds {} > sealed wall_seconds_limit {} ({}): wall_exceeded is DERIVED from the two SEALED numbers, never hand-set",
budget.wall_exceeded, sealed_wall, limit, expected_exceeded
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
} else if budget.wall_exceeded.is_some() {
eprintln!(
"Error: budget.wall_exceeded is present without budget.wall_seconds_limit: the flag cannot exist without the ceiling it is derived from"
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
}
let cross_backend_paired =
receipt.cross_backend.is_some() == (receipt.invariant.name == CROSS_BACKEND_INVARIANT);
if !cross_backend_paired {
eprintln!(
"Error: cross_backend block present={} but invariant is `{}`: the block and the invariant are a strict biconditional",
receipt.cross_backend.is_some(),
receipt.invariant.name
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
if let Some(cb) = &receipt.cross_backend {
if rederived_uses_random {
eprintln!(
"Error: cross_backend block present but the re-derived capabilities include Random: the Rust lane has no seeded PRNG, so the two streams could not agree; emit refuses this combination and verify must too"
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
if cb.secondary_target != "rust" {
eprintln!(
"Error: cross_backend.secondary_target `{}` is not supported (v0: rust only)",
cb.secondary_target
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
if cb.status != "EXECUTED" {
eprintln!(
"Error: cross_backend.status `{}` is not expressible: this block witnesses a run that actually happened (the only valid status is EXECUTED)",
cb.status
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
for (field, digest) in [
(
"cross_backend.secondary_toolchain_digest",
&cb.secondary_toolchain_digest,
),
(
"cross_backend.secondary_executable_digest",
&cb.secondary_executable_digest,
),
(
"cross_backend.secondary_raw_stdout_digest",
&cb.secondary_raw_stdout_digest,
),
] {
if !digest_is_well_formed(digest) {
eprintln!(
"Error: malformed digest in `{}`: algorithm `{}`, hex `{}` (must be sha256 with 64 hex chars)",
field, digest.algorithm, digest.hex
);
return Err(verify_failure_class(json, "DIGEST_MALFORMED", 1));
}
}
}
let has_budget_label = receipt.labels.iter().any(|l| l == vocab.budget_label);
if has_budget_label != receipt.budget.is_some() {
eprintln!(
"Error: the {} label is present={} and the budget block is present={}; they must match",
vocab.budget_label,
has_budget_label,
receipt.budget.is_some()
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
let has_budget_entry = entries.iter().any(|c| c == vocab.budget_entry);
if has_budget_entry != receipt.budget.is_some() {
eprintln!(
"Error: the `{}` entry `{}` is present={} and the budget block is present={}; they must match",
vocab.list_name,
vocab.budget_entry,
has_budget_entry,
receipt.budget.is_some()
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
let mc_executed = receipt
.monte_carlo
.as_ref()
.is_some_and(|mc| mc.status == "EXECUTED");
let has_all_mc_entries = vocab
.mc_entries
.iter()
.all(|c| entries.iter().any(|x| x == c));
let has_any_mc_entry = vocab
.mc_entries
.iter()
.any(|c| entries.iter().any(|x| x == c));
if mc_executed != has_all_mc_entries || (has_any_mc_entry && !has_all_mc_entries) {
eprintln!(
"Error: the `{}` monte_carlo entries {:?} are present={} and the monte_carlo block is EXECUTED={}; they must match",
vocab.list_name, vocab.mc_entries, has_any_mc_entry, mc_executed
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
if receipt.budget.is_some() {
let problem_claims_optimal = receipt
.problem
.label
.as_deref()
.is_some_and(|s| s.to_lowercase().contains("optimal"));
let method_claims_optimal = receipt
.numerical_method
.description
.as_deref()
.is_some_and(|s| s.to_lowercase().contains("optimal"));
if problem_claims_optimal || method_claims_optimal {
eprintln!(
"Error: a budgeted search reports the best result it found within its budget, so the problem and method text cannot contain the word `optimal` in any case, including inside a word such as `suboptimal`. Rename the label and run again."
);
return Err(verify_failure_class(json, "FIELD_CONTRACT_VIOLATION", 1));
}
}
let (expected_input_dataset, expected_seed, expected_determinism) =
witnessed_fields_from_capabilities(
&rederived.effect_policy.observed_capabilities,
rederived.effect_policy.reads_stdin,
receipt.seed_value,
);
if receipt.input_dataset != expected_input_dataset
|| receipt.seed != expected_seed
|| receipt.determinism != expected_determinism
{
eprintln!(
"Error: effect policy drift: the sealed input_dataset/seed/determinism fields do not re-derive from the observed capabilities"
);
return Err(verify_failure_class(json, "EFFECT_POLICY_DRIFT", 1));
}
let toolchain_matched = match probed_toolchain {
None => {
eprintln!(
"Error: no C compiler available to re-run the program (receipt was sealed under `{}`)",
receipt.build_state.toolchain.c_compiler
);
return Err(verify_failure_class(json, "TOOL_UNAVAILABLE", 4));
}
Some(probed) => {
let matched = probed.c_compiler == receipt.build_state.toolchain.c_compiler
&& probed.version_output_digest
== receipt.build_state.toolchain.version_output_digest
&& probed.target == receipt.build_state.toolchain.target;
if !matched {
eprintln!(
"Warning: toolchain differs: receipt `{}` ({}) on {}, local `{}` ({}) on {} (re-checking the verdict anyway; any drift below may be environmental)",
receipt.build_state.toolchain.c_compiler,
receipt.build_state.toolchain.c_compiler_version,
receipt.build_state.toolchain.target,
probed.c_compiler,
probed.c_compiler_version,
probed.target
);
}
matched
}
};
let secondary_target = receipt
.cross_backend
.as_ref()
.map(|cb| cb.secondary_target.as_str());
let observation = rerun_series(
&source_path,
&receipt.args,
receipt.seed_value,
secondary_target,
sealed_stdio_mode,
)
.map_err(|code| verify_failure_class(json, "RERUN_FAILED", code))?;
let wall_seconds_sealed = receipt.runtime_state.wall_seconds;
let wall_seconds_remeasured = observation.wall_seconds;
let parsed = observation.parsed;
if observation.exit_code != receipt.runtime_state.exit_code {
eprintln!(
"Error: exit code drift: receipt {}, re-run {}",
receipt.runtime_state.exit_code, observation.exit_code
);
return Err(verify_failure_class(json, "RERUN_EXIT_MISMATCH", 1));
}
let raw_stdout_reproduced = digests_match(
&observation.raw_stdout_digest,
&receipt.measurement.raw_stdout_digest,
);
let executable_reproduced = digests_match(
&observation.executable_digest,
&receipt.build_state.toolchain.program_executable_digest,
);
let (
verdict_series,
secondary_raw_stdout_reproduced,
secondary_executable_reproduced,
secondary_toolchain_matched,
) = if let Some(cb) = &receipt.cross_backend {
let secondary = observation.secondary.ok_or_else(|| {
eprintln!(
"Error: cross-backend receipt requires a secondary (rust) re-run observation, but none was produced"
);
verify_failure_class(json, "RERUN_FAILED", 1)
})?;
let secondary_toolchain_matched = digests_match(
&secondary.probed_toolchain_digest,
&cb.secondary_toolchain_digest,
);
if !secondary_toolchain_matched {
eprintln!(
"Warning: secondary (rust) toolchain differs: receipt `{}`, local `{}` (re-checking the verdict anyway; any drift below may be environmental)",
cb.secondary_toolchain_version,
secondary.probed_toolchain_version
);
}
if secondary.exit_code != cb.secondary_exit_code {
eprintln!(
"Error: secondary (rust) exit code drift: receipt {}, re-run {}",
cb.secondary_exit_code, secondary.exit_code
);
return Err(verify_failure_class(json, "RERUN_EXIT_MISMATCH", 1));
}
if parsed.diverged
|| secondary.parsed.diverged
|| parsed.series.is_empty()
|| secondary.parsed.series.is_empty()
|| parsed.series.len() != secondary.parsed.series.len()
{
eprintln!(
"Error: cross-backend re-run series mismatch: primary (C) produced {} values (diverged={}), secondary (rust) produced {} values (diverged={})",
parsed.series.len(),
parsed.diverged,
secondary.parsed.series.len(),
secondary.parsed.diverged
);
return Err(verify_failure_class(json, "MEASUREMENT_COUNT_DRIFT", 1));
}
let mut interleaved = Vec::with_capacity(parsed.series.len() * 2);
for (c, r) in parsed.series.iter().zip(secondary.parsed.series.iter()) {
interleaved.push(*c);
interleaved.push(*r);
}
let secondary_raw_stdout_reproduced = digests_match(
&secondary.raw_stdout_digest,
&cb.secondary_raw_stdout_digest,
);
let secondary_executable_reproduced = digests_match(
&secondary.executable_digest,
&cb.secondary_executable_digest,
);
(
interleaved,
secondary_raw_stdout_reproduced,
secondary_executable_reproduced,
secondary_toolchain_matched,
)
} else {
(parsed.series.clone(), true, true, true)
};
let both_diverged = receipt.diverged && parsed.diverged;
if !both_diverged && verdict_series.len() != receipt.measurement.count {
eprintln!(
"Error: measurement count drift: receipt {}, re-run {}",
receipt.measurement.count,
verdict_series.len()
);
return Err(verify_failure_class(json, "MEASUREMENT_COUNT_DRIFT", 1));
}
if let Some(mc) = &receipt.monte_carlo {
if mc.status == "EXECUTED" {
let estimate = mc.estimate.expect("Stage A validated presence");
let interval_low = mc.interval_low.expect("Stage A validated presence");
let interval_high = mc.interval_high.expect("Stage A validated presence");
let computed = compute_mc_executed(
&verdict_series,
receipt.measurement.column_count,
mc.samples,
&mc.interval_method,
)
.map_err(|reason| {
eprintln!(
"Error: EXECUTED monte_carlo Stage B (re-run series) recompute failed: {reason}"
);
verify_failure_class(json, "MC_INTERVAL_DRIFT", 1)
})?;
if computed.n_effective != mc.samples
|| computed.successes != mc.successes.expect("Stage A validated presence")
|| (computed.estimate - estimate).abs() > MC_RECOMPUTE_TOLERANCE
|| (computed.interval_low - interval_low).abs() > MC_RECOMPUTE_TOLERANCE
|| (computed.interval_high - interval_high).abs() > MC_RECOMPUTE_TOLERANCE
{
eprintln!(
"Error: EXECUTED monte_carlo interval drift: sealed fields do not match the Stage B recompute over the re-run series"
);
return Err(verify_failure_class(json, "MC_INTERVAL_DRIFT", 1));
}
}
}
let recomputed = recompute_verdict(
&receipt.invariant.name,
&verdict_series,
parsed.any_parsed,
parsed.diverged,
receipt.negative_fixture,
receipt.measurement.column_count,
);
let stored_increase = receipt.invariant.observed.violation_count;
if recomputed.invariant_status != receipt.invariant.status {
eprintln!(
"Error: invariant status drift: receipt {}, re-run {}",
receipt.invariant.status, recomputed.invariant_status
);
return Err(verify_failure_class(json, "INVARIANT_STATUS_DRIFT", 1));
}
if !both_diverged && recomputed.violation_count != stored_increase {
eprintln!(
"Error: violation_count drift: receipt {}, re-run {}",
stored_increase, recomputed.violation_count
);
return Err(verify_failure_class(json, "VIOLATION_COUNT_DRIFT", 1));
}
if recomputed.receipt_status != receipt.receipt_status {
eprintln!(
"Error: receipt_status drift: receipt {}, re-run {}",
receipt.receipt_status, recomputed.receipt_status
);
return Err(verify_failure_class(json, "RECEIPT_STATUS_DRIFT", 1));
}
Ok(ScientificVerifyReport {
invariant_status: recomputed.invariant_status,
invariant_name: receipt.invariant.name.clone(),
violation_count: recomputed.violation_count,
receipt_status: recomputed.receipt_status,
invariant_held: matches!(recomputed.receipt_status, "PASS" | "FAIL_EXPECTED"),
toolchain_matched,
raw_stdout_reproduced,
executable_reproduced,
secondary_raw_stdout_reproduced,
secondary_executable_reproduced,
secondary_toolchain_matched,
tolerance: receipt.invariant.tolerance,
negative_fixture: receipt.negative_fixture,
diverged: receipt.diverged,
source: source_path.to_string_lossy().to_string(),
source_digest_hex: receipt.source_digest.hex.clone(),
raw_stdout_digest_hex: receipt.measurement.raw_stdout_digest.hex.clone(),
stdio_mode: sealed_stdio_mode.to_string(),
args: receipt.args.clone(),
seal_hex: receipt.seal.hex.clone(),
wall_seconds_sealed,
wall_seconds_remeasured,
})
}
pub struct ScientificVerifyReport {
pub invariant_status: &'static str,
pub invariant_name: String,
pub violation_count: usize,
pub receipt_status: &'static str,
pub invariant_held: bool,
pub toolchain_matched: bool,
pub raw_stdout_reproduced: bool,
pub executable_reproduced: bool,
pub secondary_raw_stdout_reproduced: bool,
pub secondary_executable_reproduced: bool,
pub secondary_toolchain_matched: bool,
#[allow(dead_code)]
pub tolerance: f64,
pub negative_fixture: bool,
pub diverged: bool,
pub source: String,
pub source_digest_hex: String,
pub raw_stdout_digest_hex: String,
pub stdio_mode: String,
pub args: Vec<String>,
pub seal_hex: String,
pub wall_seconds_sealed: Option<f64>,
pub wall_seconds_remeasured: f64,
}
pub const CRUCIBLE_MEASUREMENT_EXPORT_SCHEMA: &str = "buildlang-crucible-measurement-export/v0";
pub const CRUCIBLE_EXPORT_METHOD: &str = "buildc-receipt-verify/reexecuted-v1";
pub fn crucible_measurement_from_report(
report: &ScientificVerifyReport,
claim_id: &str,
claim_sha256: &str,
claim_expects_failure: bool,
receipt_path: &str,
receipt_file_sha256: &str,
measured_at: f64,
) -> serde_json::Value {
let deviation = if report.receipt_status == "UNVERIFIABLE" {
serde_json::Value::Null
} else if claim_expects_failure {
if report.receipt_status == "FAIL_EXPECTED" {
serde_json::json!(0.0)
} else {
serde_json::json!(1.0)
}
} else {
serde_json::json!(report.violation_count as f64)
};
let expected_violation_count = if report.diverged {
serde_json::Value::Null
} else {
serde_json::json!(report.violation_count)
};
let mut evidence = vec![
format!("receipt_seal:sha256:{}", report.seal_hex),
format!("source:sha256:{}", report.source_digest_hex),
format!("sealed_raw_stdout:sha256:{}", report.raw_stdout_digest_hex),
format!("receipt_status:{}", report.receipt_status),
format!("invariant:{}", report.invariant_name),
format!("negative_fixture:{}", report.negative_fixture),
];
if claim_expects_failure {
evidence.push("claim_expectation:expects_failure".to_string());
}
if report.stdio_mode != SCIENTIFIC_STDIO_MODE_NATIVE {
evidence.push(format!("stdio_mode:{}", report.stdio_mode));
}
serde_json::json!({
"claim_id": claim_id,
"claim_sha256": claim_sha256,
"deviation": deviation,
"tolerance": 0.5,
"method": CRUCIBLE_EXPORT_METHOD,
"measured_at": measured_at,
"evidence": evidence,
"reproduction": {
"toolchain_matched": report.toolchain_matched,
"raw_stdout_reproduced": report.raw_stdout_reproduced,
"executable_reproduced": report.executable_reproduced,
},
"recheck": {
"oracle": "buildc.receipt.verify",
"receipt_path": receipt_path,
"receipt_sha256": receipt_file_sha256,
"source": report.source,
"source_sha256": report.source_digest_hex,
"args": report.args,
"command": ["buildc", "receipt", "verify", receipt_path, "--json"],
"diverged": report.diverged,
"expected": {
"receipt_status": report.receipt_status,
"invariant_status": report.invariant_status,
"violation_count": expected_violation_count,
"exit_code": if report.invariant_held { 0 } else { 3 },
},
},
})
}
#[allow(clippy::too_many_arguments)]
pub fn verify_scientific_runtime_receipt(
receipt_json: &serde_json::Value,
source_override: Option<&Path>,
json: bool,
current_compiler_version: &str,
current_language_version: &str,
probed_toolchain: Option<&ScientificToolchain>,
rederive_digests: impl FnOnce(&Path) -> Result<RederivedFacts, i32>,
rerun_series: impl FnOnce(
&Path,
&[String],
Option<u64>,
Option<&str>,
&str,
) -> Result<RerunObservation, i32>,
) -> Result<(), i32> {
let report = evaluate_scientific_runtime_receipt(
receipt_json,
source_override,
json,
current_compiler_version,
current_language_version,
probed_toolchain,
rederive_digests,
rerun_series,
)?;
if json {
let mut out = serde_json::json!({
"schema": SCIENTIFIC_RUNTIME_SCHEMA,
"status": if report.invariant_held { "match" } else { "invariant_not_held" },
"faithful": true,
"invariant_held": report.invariant_held,
"source": report.source,
"invariant_status": report.invariant_status,
"violation_count": report.violation_count,
"receipt_status": report.receipt_status,
"toolchain_matched": report.toolchain_matched,
"raw_stdout_reproduced": report.raw_stdout_reproduced,
"executable_reproduced": report.executable_reproduced,
"secondary_raw_stdout_reproduced": report.secondary_raw_stdout_reproduced,
"secondary_executable_reproduced": report.secondary_executable_reproduced,
"seal": { "algorithm": "sha256", "hex": report.seal_hex },
});
if report.invariant_name == CROSS_BACKEND_INVARIANT {
out["secondary_toolchain_matched"] =
serde_json::Value::Bool(report.secondary_toolchain_matched);
}
if let Some(sealed) = report.wall_seconds_sealed {
out["wall_seconds"] = serde_json::json!({
"sealed": sealed,
"remeasured": report.wall_seconds_remeasured,
});
}
if report.stdio_mode != SCIENTIFIC_STDIO_MODE_NATIVE {
out["stdio_mode"] = serde_json::Value::String(report.stdio_mode.clone());
}
if !report.invariant_held {
out["failure_class"] = serde_json::Value::String("INVARIANT_NOT_HELD".to_string());
}
let text = serde_json::to_string_pretty(&out).map_err(|err| {
eprintln!(
"Error serializing scientific-runtime verification report: {}",
err
);
1
})?;
println!("{}", text);
} else if report.invariant_held {
let wall_seconds_suffix = match report.wall_seconds_sealed {
Some(sealed) => format!(
", wall_seconds={}~{}",
sealed, report.wall_seconds_remeasured
),
None => String::new(),
};
let stdio_mode_suffix = if report.stdio_mode == SCIENTIFIC_STDIO_MODE_NATIVE {
String::new()
} else {
format!(", stdio_mode={}", report.stdio_mode)
};
if report.invariant_name == CROSS_BACKEND_INVARIANT {
println!(
"MATCH: scientific-runtime receipt re-runs and re-checks clean ({}, violation_count={}; toolchain_matched={}, raw_stdout_reproduced={}, executable_reproduced={}, secondary_toolchain_matched={}, secondary_raw_stdout_reproduced={}, secondary_executable_reproduced={}{}{})",
report.receipt_status,
report.violation_count,
report.toolchain_matched,
report.raw_stdout_reproduced,
report.executable_reproduced,
report.secondary_toolchain_matched,
report.secondary_raw_stdout_reproduced,
report.secondary_executable_reproduced,
wall_seconds_suffix,
stdio_mode_suffix
);
} else {
println!(
"MATCH: scientific-runtime receipt re-runs and re-checks clean ({}, violation_count={}; toolchain_matched={}, raw_stdout_reproduced={}, executable_reproduced={}{}{})",
report.receipt_status,
report.violation_count,
report.toolchain_matched,
report.raw_stdout_reproduced,
report.executable_reproduced,
wall_seconds_suffix,
stdio_mode_suffix
);
}
} else {
eprintln!(
"FAIL: scientific-runtime receipt faithfully reproduces, but the invariant did not hold ({}, violation_count={}). `receipt verify` exits nonzero so it is safe as a pass/fail gate.",
report.receipt_status, report.violation_count
);
}
if report.invariant_held {
Ok(())
} else {
eprintln!("failure_class: INVARIANT_NOT_HELD");
Err(3)
}
}
fn digests_match(actual: &ScientificDigest, expected: &ScientificDigest) -> bool {
!actual.hex.is_empty()
&& actual.algorithm.eq_ignore_ascii_case(&expected.algorithm)
&& actual.hex.eq_ignore_ascii_case(&expected.hex)
}
fn digest_is_well_formed(digest: &ScientificDigest) -> bool {
digest.algorithm.eq_ignore_ascii_case("sha256")
&& digest.hex.len() == 64
&& digest.hex.chars().all(|ch| ch.is_ascii_hexdigit())
}
fn verify_failure_class(json: bool, failure_class: &str, exit_code: i32) -> i32 {
eprintln!("failure_class: {failure_class}");
if json {
let report = serde_json::json!({
"schema": SCIENTIFIC_RUNTIME_SCHEMA,
"status": "failed",
"failure_class": failure_class,
});
if let Ok(text) = serde_json::to_string_pretty(&report) {
println!("{text}");
}
}
exit_code
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::Path;
fn hex_digest(fill: char) -> ScientificDigest {
ScientificDigest {
algorithm: "sha256".to_string(),
hex: fill.to_string().repeat(64),
}
}
fn test_toolchain() -> ScientificToolchain {
ScientificToolchain {
c_compiler: "test-cc".to_string(),
c_compiler_version: "test-cc 1.0".to_string(),
version_output_digest: hex_digest('d'),
target: "test-os/test-arch".to_string(),
buildc_binary_digest: hex_digest('e'),
program_executable_digest: hex_digest('f'),
}
}
fn test_effect_policy() -> ScientificEffectPolicy {
ScientificEffectPolicy {
facts_digest: hex_digest('7'),
observed_capabilities: vec!["Console".to_string()],
reads_stdin: false,
}
}
fn rederive_facts(
source_digest: ScientificDigest,
input_graph_digest: ScientificDigest,
) -> RederivedFacts {
RederivedFacts {
source_digest,
input_graph_digest,
effect_policy: test_effect_policy(),
}
}
fn base_inputs<'a>(
path: &'a Path,
series: Vec<f64>,
parsed: bool,
negative_fixture: bool,
) -> ScientificReceiptInputs<'a> {
ScientificReceiptInputs {
source_path: path,
compiler_version: "0.0.0",
language_version: "1.0.0".to_string(),
source_digest: hex_digest('a'),
input_graph_digest: hex_digest('b'),
target: "c",
stdio_mode: None,
os: "test-os",
exit_code: 0,
wall_seconds: None,
toolchain: test_toolchain(),
effect_policy: test_effect_policy(),
method_description: None,
raw_stdout_digest: hex_digest('c'),
series,
series_parsed: parsed,
diverged: false,
args: Vec::new(),
seed_value: None,
monte_carlo: None,
budget: None,
cross_backend: None,
invariant_name: ENERGY_MONOTONE_INVARIANT.to_string(),
metric: "series".to_string(),
units: None,
column_count: 1,
problem_label: None,
negative_fixture,
flags: Vec::new(),
}
}
fn base_inputs_for<'a>(
invariant_name: &str,
path: &'a Path,
series: Vec<f64>,
parsed: bool,
negative_fixture: bool,
) -> ScientificReceiptInputs<'a> {
ScientificReceiptInputs {
invariant_name: invariant_name.to_string(),
..base_inputs(path, series, parsed, negative_fixture)
}
}
#[test]
fn monotone_series_has_zero_increases() {
let series = [4.0, 3.0, 3.0, 2.5, 1.0];
let observed = energy_monotone_nonincreasing(&series, ENERGY_MONOTONE_TOLERANCE);
assert_eq!(observed.violation_count, 0);
assert_eq!(observed.first_violation_step, None);
assert_eq!(observed.initial_value, Some(4.0));
assert_eq!(observed.final_value, Some(1.0));
assert!(invariant_passes(series.len(), &observed));
}
#[test]
fn one_bump_is_counted_with_first_violation_step() {
let series = [4.0, 3.0, 2.0, 5.0, 1.0];
let observed = energy_monotone_nonincreasing(&series, ENERGY_MONOTONE_TOLERANCE);
assert_eq!(observed.violation_count, 1);
assert_eq!(observed.first_violation_step, Some(2));
assert!(!invariant_passes(series.len(), &observed));
}
#[test]
fn tolerance_absorbs_tiny_jitter_but_not_real_growth() {
let jitter = [1.0, 1.0 + 5e-13, 1.0];
let observed = energy_monotone_nonincreasing(&jitter, ENERGY_MONOTONE_TOLERANCE);
assert_eq!(observed.violation_count, 0);
let growth = [1.0, 1.0 + 1e-9, 1.0 + 2e-9];
let observed = energy_monotone_nonincreasing(&growth, ENERGY_MONOTONE_TOLERANCE);
assert_eq!(observed.violation_count, 2);
assert_eq!(observed.first_violation_step, Some(0));
}
#[test]
fn single_point_series_does_not_pass() {
let series = [1.0];
let observed = energy_monotone_nonincreasing(&series, ENERGY_MONOTONE_TOLERANCE);
assert_eq!(observed.violation_count, 0);
assert!(!invariant_passes(series.len(), &observed));
}
#[test]
fn empty_series_does_not_pass() {
let observed = energy_monotone_nonincreasing(&[], ENERGY_MONOTONE_TOLERANCE);
assert_eq!(observed.violation_count, 0);
assert_eq!(observed.initial_value, None);
assert_eq!(observed.final_value, None);
assert!(!invariant_passes(0, &observed));
}
#[test]
fn status_rule_pass_for_monotone_series() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
assert_eq!(receipt.receipt_status, "PASS");
assert_eq!(receipt.invariant.status, "PASS");
assert_eq!(receipt.invariant.observed.violation_count, 0);
assert!(receipt.labels.contains(&LABEL_NUMERICAL_RESULT.to_string()));
assert!(!receipt.labels.contains(&"NEGATIVE_FIXTURE".to_string()));
}
#[test]
fn status_rule_fail_expected_when_negative_fixture() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![1.0, 2.0, 3.0], true, true));
assert_eq!(receipt.receipt_status, "FAIL_EXPECTED");
assert_eq!(receipt.invariant.status, "FAIL");
assert_eq!(receipt.invariant.observed.violation_count, 2);
assert_eq!(receipt.invariant.observed.first_violation_step, Some(0));
assert!(receipt.labels.contains(&"NEGATIVE_FIXTURE".to_string()));
}
#[test]
fn status_rule_fail_unexpected_without_negative_fixture() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![1.0, 2.0, 3.0], true, false));
assert_eq!(receipt.receipt_status, "FAIL_UNEXPECTED");
assert_eq!(receipt.invariant.status, "FAIL");
assert!(!receipt.labels.contains(&"NEGATIVE_FIXTURE".to_string()));
}
#[test]
fn status_rule_unverifiable_when_series_unparseable() {
let path = Path::new("k.bld");
let receipt = build_scientific_runtime_receipt(base_inputs(path, vec![], false, false));
assert_eq!(receipt.receipt_status, "UNVERIFIABLE");
assert_eq!(receipt.measurement.count, 0);
}
#[test]
fn always_labels_numerical_result() {
let path = Path::new("k.bld");
for (series, parsed, neg) in [
(vec![4.0, 3.0], true, false),
(vec![1.0, 2.0], true, true),
(vec![], false, false),
] {
let receipt = build_scientific_runtime_receipt(base_inputs(path, series, parsed, neg));
assert!(
receipt.labels.contains(&LABEL_NUMERICAL_RESULT.to_string()),
"every receipt must carry NUMERICAL_RESULT"
);
}
}
#[test]
fn seal_is_64_hex_and_stable_and_reproducible() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
assert_eq!(receipt.seal.algorithm, "sha256");
assert_eq!(receipt.seal.hex.len(), 64);
assert!(receipt
.seal
.hex
.chars()
.all(|ch| ch.is_ascii_hexdigit() && !ch.is_ascii_uppercase()));
assert_eq!(recompute_seal_hex(&receipt), receipt.seal.hex);
}
#[test]
fn seal_detects_tampering() {
let path = Path::new("k.bld");
let mut receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
let original = receipt.seal.hex.clone();
receipt.invariant.observed.violation_count = 99;
assert_ne!(recompute_seal_hex(&receipt), original);
}
#[test]
fn self_test_cases_cover_distinct_failure_classes_and_seal_states() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
let json = serde_json::to_value(&receipt).expect("serialize receipt");
let cases = build_self_test_cases(&json).expect("build self-test cases");
let classes: Vec<&str> = cases.iter().map(|c| c.expected_class.as_str()).collect();
assert_eq!(
classes,
vec![
"COMPILER_MISMATCH",
"SEAL_MISMATCH",
"MALFORMED",
"FIELD_CONTRACT_VIOLATION",
"INVARIANT_UNSUPPORTED",
"FIELD_CONTRACT_VIOLATION",
"FIELD_CONTRACT_VIOLATION",
"FIELD_CONTRACT_VIOLATION",
"FIELD_CONTRACT_VIOLATION",
"FIELD_CONTRACT_VIOLATION",
]
);
for case in cases.iter().filter(|c| c.resealed) {
let r: ScientificRuntimeReceipt =
serde_json::from_value(case.tampered.clone()).expect("resealed case parses");
assert_eq!(
recompute_seal_hex(&r),
r.seal.hex,
"re-sealed case `{}` must carry a valid seal",
case.label
);
}
let seal_case = cases
.iter()
.find(|c| c.expected_class == "SEAL_MISMATCH")
.expect("seal-mismatch case present");
let r: ScientificRuntimeReceipt =
serde_json::from_value(seal_case.tampered.clone()).expect("seal case parses");
assert_ne!(
recompute_seal_hex(&r),
r.seal.hex,
"the seal-mismatch case must carry a stale seal"
);
}
#[test]
fn receipt_chain_seal_binds_order_and_membership() {
let mk = |i: usize, seal: &str| ReceiptChainLink {
index: i,
receipt: format!("r{i}.json"),
source: format!("s{i}.bld"),
receipt_seal: seal.to_string(),
};
let links = vec![mk(0, "aaa"), mk(1, "bbb"), mk(2, "ccc")];
let seal = receipt_chain_seal_hex(&links);
assert_eq!(seal.len(), 64);
let reordered = vec![mk(0, "bbb"), mk(1, "aaa"), mk(2, "ccc")];
assert_ne!(receipt_chain_seal_hex(&reordered), seal);
let dropped = vec![mk(0, "aaa"), mk(1, "bbb")];
assert_ne!(receipt_chain_seal_hex(&dropped), seal);
let index_tampered = vec![mk(5, "aaa"), mk(1, "bbb"), mk(2, "ccc")];
assert_ne!(receipt_chain_seal_hex(&index_tampered), seal);
let same = vec![mk(0, "aaa"), mk(1, "bbb"), mk(2, "ccc")];
assert_eq!(receipt_chain_seal_hex(&same), seal);
let manifest = build_receipt_chain(&[
("r0.json".into(), "s0.bld".into(), "aaa".into()),
("r1.json".into(), "s1.bld".into(), "bbb".into()),
("r2.json".into(), "s2.bld".into(), "ccc".into()),
]);
assert_eq!(manifest.schema, RECEIPT_CHAIN_SCHEMA);
assert_eq!(manifest.chain_seal.hex, seal);
assert_eq!(manifest.links[1].index, 1);
}
#[test]
fn corpus_manifest_parses_with_column_and_fixture_defaults() {
let json = r#"{
"schema": "buildlang-scientific-receipt-corpus/v0",
"members": [
{"source": "examples/a.bld", "invariant": "conservation", "expected_status": "PASS"},
{"source": "examples/b.bld", "invariant": "relation", "columns": 2,
"negative_fixture": true, "expected_status": "FAIL_EXPECTED"}
]
}"#;
let manifest: ScientificCorpusManifest = serde_json::from_str(json).expect("parse corpus");
assert_eq!(manifest.schema, RECEIPT_CORPUS_SCHEMA);
assert_eq!(manifest.members.len(), 2);
assert_eq!(manifest.members[0].columns, 1);
assert!(!manifest.members[0].negative_fixture);
assert_eq!(manifest.members[0].expected_status, "PASS");
assert_eq!(manifest.members[1].columns, 2);
assert!(manifest.members[1].negative_fixture);
assert_eq!(manifest.members[1].expected_status, "FAIL_EXPECTED");
}
#[test]
fn receipt_round_trips_as_json() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
let json = serde_json::to_string(&receipt).expect("serialize");
let back: ScientificRuntimeReceipt = serde_json::from_str(&json).expect("deserialize");
assert_eq!(back.schema, SCIENTIFIC_RUNTIME_SCHEMA);
assert_eq!(back.receipt_status, "PASS");
assert_eq!(back.seal.hex, receipt.seal.hex);
assert_eq!(recompute_seal_hex(&back), back.seal.hex);
}
#[test]
fn parse_series_accepts_plain_and_scientific() {
let stdout = "0.530827\n0.530404\n1.59908e+28\n6.10352e-05\n";
let parsed = parse_numeric_series(stdout);
assert!(parsed.any_parsed);
assert!(!parsed.diverged);
assert_eq!(parsed.series.len(), 4);
assert!((parsed.series[0] - 0.530827).abs() < 1e-9);
assert!(parsed.series[2] > 1e28);
assert!(parsed.series[3] > 0.0 && parsed.series[3] < 1e-3);
}
#[test]
fn parse_series_reports_no_parse_for_non_numeric() {
let parsed = parse_numeric_series("no numbers here\n");
assert!(!parsed.any_parsed);
assert!(!parsed.diverged);
assert!(parsed.series.is_empty());
}
#[test]
fn parse_series_flags_divergence_and_keeps_finite_prefix() {
for tail in ["inf", "-inf", "nan", "-nan", "infinity"] {
let parsed = parse_numeric_series(&format!("4.0\n3.0\n{tail}\n{tail}\n"));
assert!(parsed.diverged, "`{tail}` must signal divergence");
assert!(parsed.any_parsed);
assert_eq!(parsed.series, vec![4.0, 3.0], "tail={tail}");
assert!(parsed.series.iter().all(|v| v.is_finite()));
}
}
#[test]
fn parse_series_flags_platform_nonfinite_spellings() {
for tail in ["-nan(ind)", "nan(ind)", "1.#INF", "-1.#IND", "1.#QNAN"] {
let parsed = parse_numeric_series(&format!("4.0\n3.0\n{tail}\n"));
assert!(parsed.diverged, "`{tail}` must signal divergence");
assert_eq!(parsed.series, vec![4.0, 3.0], "tail={tail}");
}
}
#[test]
fn parse_series_does_not_flag_ordinary_words() {
let parsed = parse_numeric_series("information: 4.0 nanometers 3.0\n");
assert!(!parsed.diverged);
assert_eq!(parsed.series, vec![4.0, 3.0]);
}
#[test]
fn parse_series_does_not_flag_hash_labels() {
for label in ["step#info:", "cell#index=3", "grid#index", "x#snapshot"] {
let parsed = parse_numeric_series(&format!("{label} 4.0\n3.0\n2.0\n"));
assert!(!parsed.diverged, "`{label}` must not flag divergence");
assert_eq!(parsed.series, vec![4.0, 3.0, 2.0], "label={label}");
}
}
#[test]
fn diverged_run_is_unverifiable_not_a_false_pass() {
let path = Path::new("k.bld");
let mut inputs = base_inputs(path, vec![4.0, 3.0], true, false);
inputs.diverged = true;
let receipt = build_scientific_runtime_receipt(inputs);
assert_eq!(receipt.receipt_status, "UNVERIFIABLE");
assert_eq!(receipt.invariant.status, "FAIL");
assert!(receipt.diverged, "the diverged flag must be sealed in-band");
assert!(receipt.labels.contains(&"NONFINITE_OBSERVED".to_string()));
let json = serde_json::to_string(&receipt).expect("serialize");
assert!(
!json.contains("null"),
"observed_values must be finite: {json}"
);
let back: ScientificRuntimeReceipt = serde_json::from_str(&json).expect("deserialize");
assert_eq!(back.measurement.observed_values, vec![4.0, 3.0]);
assert!(back.diverged);
}
#[test]
fn recompute_verdict_matches_builder_for_monotone_series() {
let verdict = recompute_verdict(
ENERGY_MONOTONE_INVARIANT,
&[4.0, 3.0, 2.0],
true,
false,
false,
1,
);
assert_eq!(verdict.invariant_status, "PASS");
assert_eq!(verdict.receipt_status, "PASS");
assert_eq!(verdict.violation_count, 0);
}
#[test]
fn recompute_verdict_distinguishes_expected_from_unexpected_failure() {
let expected = recompute_verdict(
ENERGY_MONOTONE_INVARIANT,
&[1.0, 2.0, 3.0],
true,
false,
true,
1,
);
assert_eq!(expected.invariant_status, "FAIL");
assert_eq!(expected.receipt_status, "FAIL_EXPECTED");
assert_eq!(expected.violation_count, 2);
let unexpected = recompute_verdict(
ENERGY_MONOTONE_INVARIANT,
&[1.0, 2.0, 3.0],
true,
false,
false,
1,
);
assert_eq!(unexpected.receipt_status, "FAIL_UNEXPECTED");
}
#[test]
fn recompute_verdict_is_unverifiable_when_nothing_parsed() {
let verdict = recompute_verdict(ENERGY_MONOTONE_INVARIANT, &[], false, false, false, 1);
assert_eq!(verdict.receipt_status, "UNVERIFIABLE");
assert_eq!(verdict.invariant_status, "FAIL");
}
#[test]
fn recompute_verdict_is_unverifiable_when_diverged() {
let verdict =
recompute_verdict(ENERGY_MONOTONE_INVARIANT, &[4.0, 3.0], true, true, false, 1);
assert_eq!(verdict.receipt_status, "UNVERIFIABLE");
assert_eq!(verdict.invariant_status, "FAIL");
}
#[test]
fn conservation_holds_for_a_constant_series() {
let obs = conserved_quantity_constant(&[2.0, 2.0, 2.0, 2.0], CONSERVATION_TOLERANCE);
assert_eq!(obs.violation_count, 0);
assert!(invariant_passes(4, &obs));
let obs =
conserved_quantity_constant(&[2.0, 2.0 + 1e-12, 2.0 - 1e-12], CONSERVATION_TOLERANCE);
assert_eq!(obs.violation_count, 0);
}
#[test]
fn conservation_flags_a_leak() {
let obs = conserved_quantity_constant(&[2.0, 2.0, 1.5, 1.0], CONSERVATION_TOLERANCE);
assert_eq!(obs.violation_count, 2);
assert_eq!(obs.first_violation_step, Some(2));
assert!(!invariant_passes(4, &obs));
assert_eq!(obs.initial_value, Some(2.0));
assert_eq!(obs.final_value, Some(1.0));
}
#[test]
fn invariant_registry_dispatches_by_name() {
assert!(is_known_invariant(ENERGY_MONOTONE_INVARIANT));
assert!(is_known_invariant(CONSERVATION_INVARIANT));
assert!(is_known_invariant(BOUNDED_INVARIANT));
assert!(!is_known_invariant("no_such_invariant"));
for name in KNOWN_INVARIANTS {
assert!(is_known_invariant(name));
let _ = invariant_expectation(name);
let _ = evaluate_measurement(name, &[1.0, 1.0], invariant_tolerance(name), 2);
}
assert_eq!(
invariant_tolerance(CONSERVATION_INVARIANT),
CONSERVATION_TOLERANCE
);
assert_eq!(
invariant_tolerance(ENERGY_MONOTONE_INVARIANT),
ENERGY_MONOTONE_TOLERANCE
);
assert_eq!(invariant_tolerance(BOUNDED_INVARIANT), BOUNDED_TOLERANCE);
let decreasing = [3.0, 2.0, 1.0];
let mono = evaluate_invariant(
ENERGY_MONOTONE_INVARIANT,
&decreasing,
ENERGY_MONOTONE_TOLERANCE,
);
assert_eq!(mono.violation_count, 0);
let cons = evaluate_invariant(CONSERVATION_INVARIANT, &decreasing, CONSERVATION_TOLERANCE);
assert_eq!(cons.violation_count, 2);
}
#[test]
fn verify_round_trips_a_conservation_receipt() {
let path = Path::new("k.bld");
let receipt = build_scientific_runtime_receipt(base_inputs_for(
CONSERVATION_INVARIANT,
path,
vec![2.0, 2.0, 2.0],
true,
false,
));
assert_eq!(receipt.invariant.name, CONSERVATION_INVARIANT);
assert_eq!(receipt.oracle.name, CONSERVATION_INVARIANT);
assert_eq!(receipt.invariant.tolerance, CONSERVATION_TOLERANCE);
assert_eq!(receipt.receipt_status, "PASS");
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![2.0, 2.0, 2.0])),
);
assert!(
result.is_ok(),
"a faithful conservation receipt must verify: {result:?}"
);
}
#[test]
fn verify_fails_a_faithful_conservation_leak() {
let path = Path::new("k.bld");
let receipt = build_scientific_runtime_receipt(base_inputs_for(
CONSERVATION_INVARIANT,
path,
vec![2.0, 2.0, 1.0],
true,
false,
));
assert_eq!(receipt.receipt_status, "FAIL_UNEXPECTED");
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![2.0, 2.0, 1.0])),
);
assert_eq!(
result,
Err(3),
"a faithful conservation leak must exit 3 (FAIL_UNEXPECTED)"
);
}
#[test]
fn verify_rejects_a_non_canonical_invariant_tolerance() {
let path = Path::new("k.bld");
let mut receipt = build_scientific_runtime_receipt(base_inputs_for(
CONSERVATION_INVARIANT,
path,
vec![2.0, 2.0, 2.0],
true,
false,
));
receipt.invariant.tolerance = 1e6;
seal_receipt(&mut receipt);
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![2.0, 2.0, 2.0])),
);
assert_eq!(result, Err(1), "a non-canonical tolerance must be rejected");
}
#[test]
fn bounded_holds_for_a_capped_series() {
let obs = bounded_by_initial_maximum(&[1.0, 0.5, 0.8, 1.0, 0.3], BOUNDED_TOLERANCE);
assert_eq!(obs.violation_count, 0);
assert!(invariant_passes(5, &obs));
let obs = bounded_by_initial_maximum(&[1.0, 1.0 + 1e-12], BOUNDED_TOLERANCE);
assert_eq!(obs.violation_count, 0);
}
#[test]
fn bounded_flags_an_overshoot() {
let obs = bounded_by_initial_maximum(&[1.0, 1.5, 0.5, 2.0], BOUNDED_TOLERANCE);
assert_eq!(obs.violation_count, 2);
assert_eq!(obs.first_violation_step, Some(1));
assert!(!invariant_passes(4, &obs));
assert_eq!(obs.initial_value, Some(1.0));
assert_eq!(obs.final_value, Some(2.0));
}
#[test]
fn bounded_is_distinct_from_the_other_invariants() {
let dip_return = [1.0, 0.0, 1.0];
let bounded = evaluate_invariant(BOUNDED_INVARIANT, &dip_return, BOUNDED_TOLERANCE);
assert_eq!(bounded.violation_count, 0, "bounded accepts the capped dip");
let cons = evaluate_invariant(CONSERVATION_INVARIANT, &dip_return, CONSERVATION_TOLERANCE);
assert_eq!(cons.violation_count, 1, "conservation flags the deviation");
let mono = evaluate_invariant(
ENERGY_MONOTONE_INVARIANT,
&dip_return,
ENERGY_MONOTONE_TOLERANCE,
);
assert_eq!(
mono.violation_count, 1,
"monotone flags the rise back to 1.0"
);
}
#[test]
fn verify_round_trips_a_bounded_receipt() {
let path = Path::new("k.bld");
let receipt = build_scientific_runtime_receipt(base_inputs_for(
BOUNDED_INVARIANT,
path,
vec![1.0, 0.5, 1.0],
true,
false,
));
assert_eq!(receipt.invariant.name, BOUNDED_INVARIANT);
assert_eq!(receipt.oracle.name, BOUNDED_INVARIANT);
assert_eq!(receipt.invariant.tolerance, BOUNDED_TOLERANCE);
assert_eq!(receipt.receipt_status, "PASS");
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![1.0, 0.5, 1.0])),
);
assert!(
result.is_ok(),
"a faithful bounded receipt must verify: {result:?}"
);
}
#[test]
fn verify_fails_a_faithful_bounded_overshoot() {
let path = Path::new("k.bld");
let receipt = build_scientific_runtime_receipt(base_inputs_for(
BOUNDED_INVARIANT,
path,
vec![1.0, 2.0, 1.0],
true,
false,
));
assert_eq!(receipt.receipt_status, "FAIL_UNEXPECTED");
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![1.0, 2.0, 1.0])),
);
assert_eq!(
result,
Err(3),
"a faithful bounded overshoot must exit 3 (FAIL_UNEXPECTED)"
);
}
#[test]
fn verify_rejects_a_non_canonical_bounded_tolerance() {
let path = Path::new("k.bld");
let mut receipt = build_scientific_runtime_receipt(base_inputs_for(
BOUNDED_INVARIANT,
path,
vec![1.0, 0.5, 1.0],
true,
false,
));
receipt.invariant.tolerance = 1e6;
seal_receipt(&mut receipt);
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![1.0, 0.5, 1.0])),
);
assert_eq!(result, Err(1), "a non-canonical tolerance must be rejected");
}
#[test]
fn energy_identity_holds_for_near_zero_residuals() {
let obs = energy_identity_residual(&[1e-12, -5e-13, 2e-14, 0.0], ENERGY_IDENTITY_TOLERANCE);
assert_eq!(obs.violation_count, 0);
assert!(invariant_passes(4, &obs));
}
#[test]
fn energy_identity_flags_a_large_residual() {
let obs = energy_identity_residual(&[1e-3, -1e-3, 0.0], ENERGY_IDENTITY_TOLERANCE);
assert_eq!(obs.violation_count, 2);
assert_eq!(obs.first_violation_step, Some(0));
assert!(!invariant_passes(3, &obs));
}
#[test]
fn energy_identity_references_zero_not_the_initial_value() {
let s = [0.1, 0.0, 0.0];
let ei = evaluate_invariant(ENERGY_IDENTITY_INVARIANT, &s, ENERGY_IDENTITY_TOLERANCE);
assert_eq!(ei.violation_count, 1);
assert_eq!(ei.first_violation_step, Some(0));
let cons = evaluate_invariant(CONSERVATION_INVARIANT, &s, CONSERVATION_TOLERANCE);
assert_eq!(cons.violation_count, 2);
assert_eq!(cons.first_violation_step, Some(1));
let bounded = evaluate_invariant(BOUNDED_INVARIANT, &s, BOUNDED_TOLERANCE);
assert_eq!(bounded.violation_count, 0);
}
#[test]
fn verify_round_trips_an_energy_identity_receipt() {
let path = Path::new("k.bld");
let receipt = build_scientific_runtime_receipt(base_inputs_for(
ENERGY_IDENTITY_INVARIANT,
path,
vec![1e-12, -1e-13, 2e-14],
true,
false,
));
assert_eq!(receipt.invariant.name, ENERGY_IDENTITY_INVARIANT);
assert_eq!(receipt.oracle.name, ENERGY_IDENTITY_INVARIANT);
assert_eq!(receipt.invariant.tolerance, ENERGY_IDENTITY_TOLERANCE);
assert_eq!(receipt.receipt_status, "PASS");
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![1e-12, -1e-13, 2e-14])),
);
assert!(
result.is_ok(),
"a faithful energy-identity receipt must verify: {result:?}"
);
}
#[test]
fn verify_fails_a_faithful_energy_identity_breakage() {
let path = Path::new("k.bld");
let receipt = build_scientific_runtime_receipt(base_inputs_for(
ENERGY_IDENTITY_INVARIANT,
path,
vec![1e-3, 1e-3, 1e-3],
true,
false,
));
assert_eq!(receipt.receipt_status, "FAIL_UNEXPECTED");
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![1e-3, 1e-3, 1e-3])),
);
assert_eq!(
result,
Err(3),
"a faithful energy-identity breakage must exit 3 (FAIL_UNEXPECTED)"
);
}
#[test]
fn verify_rejects_a_non_canonical_energy_identity_tolerance() {
let path = Path::new("k.bld");
let mut receipt = build_scientific_runtime_receipt(base_inputs_for(
ENERGY_IDENTITY_INVARIANT,
path,
vec![1e-12, -1e-13, 2e-14],
true,
false,
));
receipt.invariant.tolerance = 1e6;
seal_receipt(&mut receipt);
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![1e-12, -1e-13, 2e-14])),
);
assert_eq!(result, Err(1), "a non-canonical tolerance must be rejected");
}
fn relation_inputs<'a>(
path: &'a Path,
series: Vec<f64>,
parsed: bool,
negative_fixture: bool,
) -> ScientificReceiptInputs<'a> {
ScientificReceiptInputs {
invariant_name: RELATION_INVARIANT.to_string(),
column_count: 2,
..base_inputs(path, series, parsed, negative_fixture)
}
}
#[test]
fn relation_holds_when_every_row_agrees() {
let (obs, rows) =
relation_columns_agree(&[1.0, 1.0, 2.0, 2.0, 3.0, 3.0], RELATION_TOLERANCE, 2);
assert_eq!(obs.violation_count, 0);
assert_eq!(rows, 3);
assert!(invariant_passes(rows, &obs));
}
#[test]
fn relation_flags_a_disagreeing_row() {
let (obs, rows) =
relation_columns_agree(&[1.0, 1.0, 2.0, 9.0, 3.0, 3.0], RELATION_TOLERANCE, 2);
assert_eq!(obs.violation_count, 1);
assert_eq!(obs.first_violation_step, Some(1));
assert_eq!(rows, 3);
assert!(!invariant_passes(rows, &obs));
}
#[test]
fn relation_reports_zero_rows_for_ragged_or_underwide_data() {
let (_, rows) = relation_columns_agree(&[1.0, 1.0, 2.0], RELATION_TOLERANCE, 2);
assert_eq!(rows, 0);
let (_, rows) = relation_columns_agree(&[1.0, 2.0], RELATION_TOLERANCE, 1);
assert_eq!(rows, 0);
}
#[test]
fn evaluate_measurement_uses_row_count_for_relations_and_len_otherwise() {
let rel = evaluate_measurement(
RELATION_INVARIANT,
&[1.0, 1.0, 2.0, 2.0],
RELATION_TOLERANCE,
2,
);
assert_eq!(rel.effective_len, 2);
assert_eq!(rel.observed.violation_count, 0);
let mono = evaluate_measurement(
ENERGY_MONOTONE_INVARIANT,
&[4.0, 3.0, 2.0, 1.0],
ENERGY_MONOTONE_TOLERANCE,
1,
);
assert_eq!(mono.effective_len, 4);
}
#[test]
fn evaluate_measurement_deinterleaves_column_zero_for_three_columns() {
let series = [
1.0, -5.0, -5.0, 2.0, -6.0, -6.0, ];
let verdict =
evaluate_measurement(NON_NEGATIVE_INVARIANT, &series, NON_NEGATIVE_TOLERANCE, 3);
assert_eq!(verdict.effective_len, 2, "row count, not token count");
assert_eq!(
verdict.observed.violation_count, 0,
"column 0 alone is non-negative; columns 1-2 must be ignored by the invariant check"
);
}
#[test]
fn evaluate_measurement_three_column_ragged_series_cannot_witness() {
let verdict = evaluate_measurement(
NON_NEGATIVE_INVARIANT,
&[1.0, 2.0, 3.0, 4.0],
NON_NEGATIVE_TOLERANCE,
3,
);
assert_eq!(verdict.effective_len, 0);
}
#[test]
fn column_count_matches_invariant_refuses_mc_executed_with_relation() {
assert!(!column_count_matches_invariant(RELATION_INVARIANT, 3, true));
assert!(!column_count_matches_invariant(
CROSS_BACKEND_INVARIANT,
3,
true
));
assert!(column_count_matches_invariant(RELATION_INVARIANT, 3, false));
assert!(column_count_matches_invariant(
NON_NEGATIVE_INVARIANT,
3,
true
));
assert!(!column_count_matches_invariant(
NON_NEGATIVE_INVARIANT,
3,
false
));
}
#[test]
fn compute_mc_executed_recovers_a_coherent_wilson_series() {
let series = [
0.0, 1.0, 10.0, 0.0, 1.0, 11.0, 0.0, 2.0, 12.0, 0.0, 2.0, 13.0, ];
let result = compute_mc_executed(&series, 3, 13, MC_INTERVAL_WILSON_95);
let computed = result.expect("a coherent series must compute");
assert_eq!(computed.n_effective, 13);
assert_eq!(computed.successes, 2);
assert!((computed.estimate - 2.0 / 13.0).abs() < 1e-12);
assert!(computed.interval_low < computed.estimate);
assert!(computed.estimate < computed.interval_high);
}
#[test]
fn compute_mc_executed_rejects_before_any_rerun_on_bad_trials_step() {
let series = [0.0, 1.0, 10.0, 0.0, 1.0, 12.0];
let err = compute_mc_executed(&series, 3, 12, MC_INTERVAL_WILSON_95)
.expect_err("a trials step of 2 must be rejected");
assert!(err.contains("row 1"), "error must name the row: {err}");
assert!(err.contains("trials"), "error must name the field: {err}");
}
#[test]
fn compute_mc_executed_rejects_successes_decrease() {
let series = [0.0, 2.0, 10.0, 0.0, 1.0, 11.0];
let err = compute_mc_executed(&series, 3, 11, MC_INTERVAL_WILSON_95)
.expect_err("a successes decrease must be rejected");
assert!(err.contains("row 1"), "error must name the row: {err}");
assert!(
err.contains("successes"),
"error must name the field: {err}"
);
}
#[test]
fn compute_mc_executed_rejects_successes_exceeding_trials() {
let series = [0.0, 5.0, 4.0];
let err = compute_mc_executed(&series, 3, 4, MC_INTERVAL_WILSON_95)
.expect_err("successes exceeding trials must be rejected");
assert!(err.contains("exceeds"), "error must name the reason: {err}");
}
#[test]
fn compute_mc_executed_rejects_non_integer_column() {
let series = [0.0, 1.5, 10.0];
let err = compute_mc_executed(&series, 3, 10, MC_INTERVAL_WILSON_95)
.expect_err("a non-integer successes value must be rejected");
assert!(
err.contains("integer"),
"error must name the integrality reason: {err}"
);
}
#[test]
fn compute_mc_executed_witnessed_denominator_must_equal_samples() {
let series = [0.0, 1000.0, 2000.0];
let err = compute_mc_executed(&series, 3, 1999, MC_INTERVAL_WILSON_95)
.expect_err("a witnessed/declared denominator mismatch must be rejected");
assert!(
err.contains("2000"),
"error must name the witnessed trials: {err}"
);
assert!(
err.contains("1999"),
"error must name the declared samples: {err}"
);
}
#[test]
fn compute_mc_executed_rejects_ragged_series() {
let series = [0.0, 1.0, 10.0, 0.0];
let err = compute_mc_executed(&series, 3, 10, MC_INTERVAL_WILSON_95)
.expect_err("a series length not a multiple of 3 must be rejected");
assert!(err.contains("ragged"), "error must name the reason: {err}");
}
#[test]
fn compute_mc_executed_normal_approx_degenerate_at_zero_successes() {
let series = [0.0, 0.0, 10.0];
let err = compute_mc_executed(&series, 3, 10, MC_INTERVAL_NORMAL_APPROX_95)
.expect_err("zero successes must be rejected for normal-approx-95");
assert!(
err.contains("wilson-95"),
"error must point at the alternative: {err}"
);
}
#[test]
fn compute_mc_executed_normal_approx_degenerate_at_all_successes() {
let series = [0.0, 10.0, 10.0];
let err = compute_mc_executed(&series, 3, 10, MC_INTERVAL_NORMAL_APPROX_95)
.expect_err("all-successes must be rejected for normal-approx-95");
assert!(
err.contains("wilson-95"),
"error must point at the alternative: {err}"
);
}
#[test]
fn compute_mc_executed_rejects_clopper_pearson() {
let series = [0.0, 5.0, 10.0];
let err = compute_mc_executed(&series, 3, 10, MC_INTERVAL_CLOPPER_PEARSON_95)
.expect_err("clopper-pearson-95 is not executable in v1");
assert!(
err.contains("inverse incomplete beta"),
"error must name the reason: {err}"
);
}
#[test]
fn compute_mc_executed_wilson_matches_hand_computed_value() {
let series = [0.0, 3.0, 10.0];
let computed = compute_mc_executed(&series, 3, 10, MC_INTERVAL_WILSON_95)
.expect("a coherent series must compute");
let z = MC_INTERVAL_Z_95;
let z2 = z * z;
let n = 10.0f64;
let p = 0.3f64;
let denom = 1.0 + z2 / n;
let center = (p + z2 / (2.0 * n)) / denom;
let margin = (z / denom) * (p * (1.0 - p) / n + z2 / (4.0 * n * n)).sqrt();
assert!((computed.interval_low - (center - margin)).abs() < 1e-9);
assert!((computed.interval_high - (center + margin)).abs() < 1e-9);
assert!((computed.estimate - 0.3).abs() < 1e-12);
}
#[test]
fn verify_round_trips_a_relation_receipt() {
let path = Path::new("k.bld");
let receipt = build_scientific_runtime_receipt(relation_inputs(
path,
vec![1.0, 1.0, 2.0, 2.0, 3.0, 3.0],
true,
false,
));
assert_eq!(receipt.invariant.name, RELATION_INVARIANT);
assert_eq!(receipt.oracle.name, RELATION_INVARIANT);
assert_eq!(receipt.measurement.column_count, 2);
assert_eq!(receipt.receipt_status, "PASS");
assert_eq!(receipt.invariant.observed.violation_count, 0);
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![1.0, 1.0, 2.0, 2.0, 3.0, 3.0])),
);
assert!(
result.is_ok(),
"a faithful relation receipt must verify: {result:?}"
);
}
#[test]
fn verify_fails_a_faithful_relation_disagreement() {
let path = Path::new("k.bld");
let receipt = build_scientific_runtime_receipt(relation_inputs(
path,
vec![1.0, 1.0, 2.0, 9.0],
true,
false,
));
assert_eq!(receipt.receipt_status, "FAIL_UNEXPECTED");
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![1.0, 1.0, 2.0, 9.0])),
);
assert_eq!(
result,
Err(3),
"a faithful relation disagreement must exit 3 (FAIL_UNEXPECTED)"
);
}
#[test]
fn verify_rejects_a_non_canonical_relation_tolerance() {
let path = Path::new("k.bld");
let mut receipt = build_scientific_runtime_receipt(relation_inputs(
path,
vec![1.0, 1.0, 2.0, 2.0],
true,
false,
));
receipt.invariant.tolerance = 1e6;
seal_receipt(&mut receipt);
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![1.0, 1.0, 2.0, 2.0])),
);
assert_eq!(result, Err(1), "a non-canonical tolerance must be rejected");
}
#[test]
fn verify_rejects_a_receipt_missing_column_count() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
let mut value = serde_json::to_value(&receipt).expect("to_value");
value["measurement"]
.as_object_mut()
.expect("measurement object")
.remove("column_count")
.expect("column_count was present");
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| panic!("a malformed receipt must be rejected before re-derivation"),
|_, _, _, _, _| panic!("a malformed receipt must be rejected before the re-run"),
);
assert_eq!(
result,
Err(1),
"a receipt missing the required column_count must be rejected as malformed"
);
}
#[test]
fn conserved_band_holds_within_the_budget_and_flags_drift() {
let obs = evaluate_invariant(
CONSERVED_BAND_INVARIANT,
&[1.0, 1.002, 0.998, 1.001],
CONSERVED_BAND_TOLERANCE,
);
assert_eq!(obs.violation_count, 0);
let obs = evaluate_invariant(
CONSERVED_BAND_INVARIANT,
&[1.0, 1.0, 1.1, 1.2],
CONSERVED_BAND_TOLERANCE,
);
assert_eq!(obs.violation_count, 2);
assert_eq!(obs.first_violation_step, Some(2));
}
#[test]
fn conserved_band_is_distinct_from_conservation_and_bounded() {
let band = [1.0, 1.002, 0.998];
let cb = evaluate_invariant(CONSERVED_BAND_INVARIANT, &band, CONSERVED_BAND_TOLERANCE);
assert_eq!(cb.violation_count, 0, "conserved-band accepts the band");
let cons = evaluate_invariant(CONSERVATION_INVARIANT, &band, CONSERVATION_TOLERANCE);
assert!(
cons.violation_count > 0,
"conservation rejects the deviation"
);
let bounded = evaluate_invariant(BOUNDED_INVARIANT, &band, BOUNDED_TOLERANCE);
assert!(
bounded.violation_count > 0,
"bounded rejects the rise above s[0]"
);
}
#[test]
fn verify_round_trips_a_conserved_band_receipt() {
let path = Path::new("k.bld");
let receipt = build_scientific_runtime_receipt(base_inputs_for(
CONSERVED_BAND_INVARIANT,
path,
vec![1.0, 1.002, 0.998],
true,
false,
));
assert_eq!(receipt.invariant.name, CONSERVED_BAND_INVARIANT);
assert_eq!(receipt.oracle.name, CONSERVED_BAND_INVARIANT);
assert_eq!(receipt.invariant.tolerance, CONSERVED_BAND_TOLERANCE);
assert_eq!(receipt.receipt_status, "PASS");
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![1.0, 1.002, 0.998])),
);
assert!(
result.is_ok(),
"a faithful conserved-band receipt must verify: {result:?}"
);
}
#[test]
fn verify_fails_a_faithful_conserved_band_drift() {
let path = Path::new("k.bld");
let receipt = build_scientific_runtime_receipt(base_inputs_for(
CONSERVED_BAND_INVARIANT,
path,
vec![1.0, 1.0, 1.1],
true,
false,
));
assert_eq!(receipt.receipt_status, "FAIL_UNEXPECTED");
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![1.0, 1.0, 1.1])),
);
assert_eq!(
result,
Err(3),
"a faithful conserved-band drift must exit 3 (FAIL_UNEXPECTED)"
);
}
#[test]
fn verify_rejects_a_non_canonical_conserved_band_tolerance() {
let path = Path::new("k.bld");
let mut receipt = build_scientific_runtime_receipt(base_inputs_for(
CONSERVED_BAND_INVARIANT,
path,
vec![1.0, 1.002, 0.998],
true,
false,
));
receipt.invariant.tolerance = 1e6;
seal_receipt(&mut receipt);
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![1.0, 1.002, 0.998])),
);
assert_eq!(result, Err(1), "a non-canonical tolerance must be rejected");
}
#[test]
fn verify_rejects_a_column_count_that_violates_the_invariant_contract() {
let path = Path::new("k.bld");
let mut receipt = build_scientific_runtime_receipt(base_inputs_for(
CONSERVED_BAND_INVARIANT,
path,
vec![1.0, 1.002, 0.998],
true,
false,
));
assert_eq!(receipt.measurement.column_count, 1);
receipt.measurement.column_count = 2;
seal_receipt(&mut receipt);
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![1.0, 1.002, 0.998])),
);
assert_eq!(
result,
Err(1),
"a single-scalar invariant with column_count != 1 must be rejected"
);
let mut relation = build_scientific_runtime_receipt(relation_inputs(
path,
vec![1.0, 1.0, 2.0, 2.0],
true,
false,
));
relation.measurement.column_count = 1;
seal_receipt(&mut relation);
let value = serde_json::to_value(&relation).expect("to_value");
let sd = relation.source_digest.clone();
let gd = relation.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&relation.compiler_version,
&relation.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![1.0, 1.0, 2.0, 2.0])),
);
assert_eq!(
result,
Err(1),
"a relation invariant with column_count < 2 must be rejected"
);
}
#[test]
fn non_negative_holds_and_flags_undershoot() {
let obs = non_negative(&[0.0, 5.0, 100.0, 0.5], NON_NEGATIVE_TOLERANCE);
assert_eq!(obs.violation_count, 0);
assert!(invariant_passes(4, &obs));
let obs = non_negative(&[1.0, 0.5, -0.3, 2.0], NON_NEGATIVE_TOLERANCE);
assert_eq!(obs.violation_count, 1);
assert_eq!(obs.first_violation_step, Some(2));
}
#[test]
fn non_negative_is_distinct_from_the_family() {
let grow = [1.0, 5.0, 100.0, 0.0];
let nn = evaluate_invariant(NON_NEGATIVE_INVARIANT, &grow, NON_NEGATIVE_TOLERANCE);
assert_eq!(nn.violation_count, 0, "non-negative accepts the growth");
let bounded = evaluate_invariant(BOUNDED_INVARIANT, &grow, BOUNDED_TOLERANCE);
assert!(
bounded.violation_count > 0,
"bounded rejects the rise above s[0]"
);
let cons = evaluate_invariant(CONSERVATION_INVARIANT, &grow, CONSERVATION_TOLERANCE);
assert!(
cons.violation_count > 0,
"conservation rejects the deviation"
);
let mono = evaluate_invariant(ENERGY_MONOTONE_INVARIANT, &grow, ENERGY_MONOTONE_TOLERANCE);
assert!(mono.violation_count > 0, "monotone rejects the rise");
}
#[test]
fn verify_round_trips_a_non_negative_receipt() {
let path = Path::new("k.bld");
let receipt = build_scientific_runtime_receipt(base_inputs_for(
NON_NEGATIVE_INVARIANT,
path,
vec![0.0, 5.0, 100.0],
true,
false,
));
assert_eq!(receipt.invariant.name, NON_NEGATIVE_INVARIANT);
assert_eq!(receipt.oracle.name, NON_NEGATIVE_INVARIANT);
assert_eq!(receipt.invariant.tolerance, NON_NEGATIVE_TOLERANCE);
assert_eq!(receipt.receipt_status, "PASS");
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![0.0, 5.0, 100.0])),
);
assert!(
result.is_ok(),
"a faithful non-negative receipt must verify: {result:?}"
);
}
#[test]
fn verify_fails_a_faithful_non_negative_undershoot() {
let path = Path::new("k.bld");
let receipt = build_scientific_runtime_receipt(base_inputs_for(
NON_NEGATIVE_INVARIANT,
path,
vec![1.0, -0.5, 2.0],
true,
false,
));
assert_eq!(receipt.receipt_status, "FAIL_UNEXPECTED");
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![1.0, -0.5, 2.0])),
);
assert_eq!(
result,
Err(3),
"a faithful non-negative undershoot must exit 3 (FAIL_UNEXPECTED)"
);
}
#[test]
fn verify_rejects_a_non_canonical_non_negative_tolerance() {
let path = Path::new("k.bld");
let mut receipt = build_scientific_runtime_receipt(base_inputs_for(
NON_NEGATIVE_INVARIANT,
path,
vec![0.0, 5.0, 100.0],
true,
false,
));
receipt.invariant.tolerance = 1e6;
seal_receipt(&mut receipt);
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| Ok(rerun(vec![0.0, 5.0, 100.0])),
);
assert_eq!(result, Err(1), "a non-canonical tolerance must be rejected");
}
fn rerun(series: Vec<f64>) -> RerunObservation {
RerunObservation {
parsed: ParsedSeries {
any_parsed: !series.is_empty(),
diverged: false,
series,
},
exit_code: 0,
secondary: None,
raw_stdout_digest: hex_digest('c'),
executable_digest: hex_digest('f'),
wall_seconds: 0.123,
}
}
#[test]
fn native_stdio_mode_is_omitted_and_verified_as_legacy_default() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
let value = serde_json::to_value(&receipt).expect("to_value");
assert!(
value["build_state"].get("stdio_mode").is_none(),
"native receipts must preserve the old missing-field shape: {value:#?}"
);
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, stdio_mode| {
assert_eq!(
stdio_mode, SCIENTIFIC_STDIO_MODE_NATIVE,
"missing sealed stdio_mode must verify as native"
);
Ok(rerun(vec![4.0, 3.0, 2.0]))
},
);
assert_eq!(result, Ok(()));
}
#[test]
fn portable_stdio_mode_is_sealed_and_passed_to_the_rerun() {
let path = Path::new("k.bld");
let receipt = build_scientific_runtime_receipt(ScientificReceiptInputs {
stdio_mode: Some(SCIENTIFIC_STDIO_MODE_PORTABLE_LF),
flags: vec!["stdio-mode=portable-lf".to_string()],
..base_inputs(path, vec![4.0, 3.0, 2.0], true, false)
});
let value = serde_json::to_value(&receipt).expect("to_value");
assert_eq!(value["build_state"]["stdio_mode"], "portable-lf");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, stdio_mode| {
assert_eq!(
stdio_mode, SCIENTIFIC_STDIO_MODE_PORTABLE_LF,
"verify must compile the replay with the sealed stdio mode"
);
Ok(rerun(vec![4.0, 3.0, 2.0]))
},
);
assert_eq!(result, Ok(()));
}
#[test]
fn verify_matches_a_freshly_built_receipt() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
);
assert!(result.is_ok(), "a faithful re-run must verify");
}
#[test]
fn verify_rejects_a_source_digest_mismatch() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
let value = serde_json::to_value(&receipt).expect("to_value");
let graph_digest = receipt.input_graph_digest.clone();
let wrong = ScientificDigest {
algorithm: "sha256".to_string(),
hex: "c".repeat(64),
};
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(wrong.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
);
assert_eq!(result, Err(1), "a source-digest mismatch must fail verify");
}
#[test]
fn verify_rejects_a_verdict_drift() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![1.0, 2.0, 3.0])),
);
assert_eq!(result, Err(1), "an invariant drift must fail verify");
}
#[test]
fn verify_rejects_measurement_count_drift() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
assert_eq!(receipt.measurement.count, 3);
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0])),
);
assert_eq!(result, Err(1), "a measurement count drift must fail verify");
}
#[test]
fn verify_fails_a_faithful_but_unexpected_failure() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![1.0, 2.0, 3.0], true, false));
assert_eq!(receipt.receipt_status, "FAIL_UNEXPECTED");
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![1.0, 2.0, 3.0])),
);
assert_eq!(
result,
Err(3),
"a faithful FAIL_UNEXPECTED receipt must fail verify with exit 3"
);
}
#[test]
fn verify_passes_a_faithful_negative_fixture() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![1.0, 2.0, 3.0], true, true));
assert_eq!(receipt.receipt_status, "FAIL_EXPECTED");
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![1.0, 2.0, 3.0])),
);
assert!(
result.is_ok(),
"a faithful FAIL_EXPECTED receipt must verify"
);
}
#[test]
fn verify_fails_a_faithful_diverged_receipt() {
let path = Path::new("k.bld");
let mut inputs = base_inputs(path, vec![4.0, 3.0], true, false);
inputs.diverged = true;
let receipt = build_scientific_runtime_receipt(inputs);
assert_eq!(receipt.receipt_status, "UNVERIFIABLE");
assert_eq!(receipt.measurement.count, 2);
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| {
let mut observation = rerun(vec![4.0, 3.0, 2.5]);
observation.parsed.diverged = true;
Ok(observation)
},
);
assert_eq!(
result,
Err(3),
"a faithful diverged receipt must exit 3 even when the platform-dependent prefix length shifts"
);
}
#[test]
fn verify_rejects_a_missing_scope_entry() {
let path = Path::new("k.bld");
let mut receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
receipt.scope = vec!["rerun_agreement".to_string()];
seal_receipt(&mut receipt);
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
);
assert_eq!(
result,
Err(1),
"a receipt without the observed_series_invariant scope entry must be rejected"
);
}
#[test]
fn verify_rejects_an_extraction_policy_mismatch() {
let path = Path::new("k.bld");
let mut receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
receipt.measurement.series_extraction_policy = "whitespace-f64/v99".to_string();
seal_receipt(&mut receipt);
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
);
assert_eq!(
result,
Err(1),
"an unknown extraction policy must be rejected"
);
}
#[test]
fn verify_rejects_an_unbound_oracle() {
let path = Path::new("k.bld");
let mut receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
receipt.oracle.name = "some_other_criterion".to_string();
seal_receipt(&mut receipt);
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
);
assert_eq!(result, Err(1), "an unbound oracle must be rejected");
let mut receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
receipt.oracle.kind = "reference_implementation".to_string();
seal_receipt(&mut receipt);
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
);
assert_eq!(
result,
Err(1),
"an oracle kind this verifier cannot re-check must be rejected"
);
}
#[test]
fn empty_digests_never_match() {
let empty = ScientificDigest {
algorithm: "sha256".to_string(),
hex: String::new(),
};
assert!(!digests_match(&empty, &empty));
assert!(digests_match(&hex_digest('a'), &hex_digest('a')));
}
#[test]
fn verify_rejects_a_malformed_digest() {
let path = Path::new("k.bld");
let mut receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
receipt.build_state.toolchain.program_executable_digest.hex = String::new();
seal_receipt(&mut receipt);
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
);
assert_eq!(result, Err(1), "an empty sealed digest must be rejected");
}
#[test]
fn verify_rejects_a_self_consistent_but_unimplemented_oracle() {
let path = Path::new("k.bld");
let mut receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
receipt.oracle.name = "custom_criterion".to_string();
receipt.invariant.name = "custom_criterion".to_string();
seal_receipt(&mut receipt);
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
);
assert_eq!(
result,
Err(1),
"a self-consistent but unimplemented oracle/invariant pair must be rejected"
);
let mut receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
receipt.oracle.status = "EXECUTED".to_string();
seal_receipt(&mut receipt);
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
);
assert_eq!(
result,
Err(1),
"an executed status on a declared oracle must be rejected"
);
}
#[test]
fn verify_rejects_an_edited_fence() {
let path = Path::new("k.bld");
let mut receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
receipt.telemetry_branch.status = "AVAILABLE".to_string();
seal_receipt(&mut receipt);
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
);
assert_eq!(
result,
Err(1),
"a fence claiming availability must be rejected"
);
}
#[test]
fn extraction_policy_matches_by_version_tag_not_prose() {
let path = Path::new("k.bld");
let mut receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
receipt.measurement.series_extraction_policy =
"whitespace-f64/v1: same discipline, different wording".to_string();
seal_receipt(&mut receipt);
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
);
assert!(
result.is_ok(),
"same policy tag with different prose must verify: {result:?}"
);
}
fn report_fixture(
receipt_status: &'static str,
violation_count: usize,
) -> ScientificVerifyReport {
ScientificVerifyReport {
invariant_status: if violation_count == 0 { "PASS" } else { "FAIL" },
invariant_name: ENERGY_MONOTONE_INVARIANT.to_string(),
violation_count,
receipt_status,
invariant_held: matches!(receipt_status, "PASS" | "FAIL_EXPECTED"),
toolchain_matched: true,
raw_stdout_reproduced: true,
executable_reproduced: false,
secondary_raw_stdout_reproduced: true,
secondary_executable_reproduced: true,
secondary_toolchain_matched: true,
tolerance: ENERGY_MONOTONE_TOLERANCE,
negative_fixture: receipt_status == "FAIL_EXPECTED",
diverged: false,
source: "k.bld".to_string(),
source_digest_hex: "a".repeat(64),
raw_stdout_digest_hex: "c".repeat(64),
stdio_mode: SCIENTIFIC_STDIO_MODE_NATIVE.to_string(),
args: vec!["--mode".to_string()],
seal_hex: "e".repeat(64),
wall_seconds_sealed: Some(1.234),
wall_seconds_remeasured: 1.240,
}
}
#[test]
fn export_derives_deviation_from_the_rerun_and_seals_the_replay() {
let row = crucible_measurement_from_report(
&report_fixture("PASS", 0),
"claim-1",
&"b".repeat(64),
false,
"r.json",
&"f".repeat(64),
1000.0,
);
assert_eq!(row["deviation"], 0.0);
assert_eq!(row["tolerance"], 0.5);
assert_eq!(row["method"], CRUCIBLE_EXPORT_METHOD);
assert_eq!(row["recheck"]["oracle"], "buildc.receipt.verify");
assert_eq!(row["recheck"]["command"][0], "buildc");
assert_eq!(row["recheck"]["expected"]["violation_count"], 0);
assert!(row["evidence"]
.as_array()
.unwrap()
.iter()
.any(|e| e == "receipt_status:PASS"));
}
#[test]
fn export_labels_the_invariant_the_receipt_actually_checked() {
let mut conservation = report_fixture("PASS", 0);
conservation.invariant_name = CONSERVATION_INVARIANT.to_string();
let row = crucible_measurement_from_report(
&conservation,
"claim-c",
&"b".repeat(64),
false,
"r.json",
&"f".repeat(64),
1000.0,
);
let evidence = row["evidence"].as_array().unwrap();
assert!(
evidence
.iter()
.any(|e| e == &format!("invariant:{}", CONSERVATION_INVARIANT)),
"conservation export must name conserved_quantity_constant: {evidence:?}"
);
assert!(
!evidence
.iter()
.any(|e| e == &format!("invariant:{}", ENERGY_MONOTONE_INVARIANT)),
"conservation export must NOT carry the monotone name: {evidence:?}"
);
let mono = crucible_measurement_from_report(
&report_fixture("PASS", 0),
"claim-m",
&"b".repeat(64),
false,
"r.json",
&"f".repeat(64),
1000.0,
);
assert!(mono["evidence"]
.as_array()
.unwrap()
.iter()
.any(|e| e == &format!("invariant:{}", ENERGY_MONOTONE_INVARIANT)));
}
#[test]
fn export_maps_unverifiable_to_null_deviation() {
let row = crucible_measurement_from_report(
&report_fixture("UNVERIFIABLE", 0),
"claim-1",
&"b".repeat(64),
false,
"r.json",
&"f".repeat(64),
1000.0,
);
assert!(row["deviation"].is_null());
}
#[test]
fn export_reports_a_failing_receipt_honestly() {
for status in ["FAIL_UNEXPECTED", "FAIL_EXPECTED"] {
let row = crucible_measurement_from_report(
&report_fixture(status, 199),
"claim-1",
&"b".repeat(64),
false,
"r.json",
&"f".repeat(64),
1000.0,
);
assert_eq!(row["deviation"], 199.0, "status={status}");
assert!(row["evidence"]
.as_array()
.unwrap()
.iter()
.any(|e| e.as_str().unwrap() == format!("receipt_status:{status}")));
}
}
#[test]
fn export_expected_failure_binding_is_claim_relative() {
let row = crucible_measurement_from_report(
&report_fixture("FAIL_EXPECTED", 199),
"claim-1",
&"b".repeat(64),
true,
"r.json",
&"f".repeat(64),
1000.0,
);
assert_eq!(row["deviation"], 0.0);
assert!(row["evidence"]
.as_array()
.unwrap()
.iter()
.any(|e| e == "claim_expectation:expects_failure"));
let mut passed = report_fixture("PASS", 0);
passed.negative_fixture = true;
let row = crucible_measurement_from_report(
&passed,
"claim-1",
&"b".repeat(64),
true,
"r.json",
&"f".repeat(64),
1000.0,
);
assert_eq!(
row["deviation"], 1.0,
"a fixture that unexpectedly passes violates the failure-predicting claim"
);
}
#[test]
fn export_never_seals_a_platform_dependent_expectation_for_diverged_receipts() {
let mut report = report_fixture("UNVERIFIABLE", 1957);
report.diverged = true;
let row = crucible_measurement_from_report(
&report,
"claim-1",
&"b".repeat(64),
false,
"r.json",
&"f".repeat(64),
1000.0,
);
assert!(row["recheck"]["expected"]["violation_count"].is_null());
assert_eq!(row["recheck"]["diverged"], true);
assert!(row["deviation"].is_null());
assert_eq!(row["recheck"]["expected"]["exit_code"], 3);
}
#[test]
fn export_carries_the_reproduction_flags_outside_evidence() {
let row = crucible_measurement_from_report(
&report_fixture("PASS", 0),
"claim-1",
&"b".repeat(64),
false,
"r.json",
&"f".repeat(64),
1000.0,
);
assert_eq!(row["reproduction"]["toolchain_matched"], true);
assert_eq!(row["reproduction"]["raw_stdout_reproduced"], true);
assert_eq!(row["reproduction"]["executable_reproduced"], false);
assert_eq!(row["recheck"]["expected"]["exit_code"], 0);
assert!(row["evidence"]
.as_array()
.unwrap()
.iter()
.any(|e| e.as_str().unwrap().starts_with("sealed_raw_stdout:sha256:")));
}
#[test]
fn witnessed_fields_derive_from_capabilities() {
let (dataset, seed, determinism) =
witnessed_fields_from_capabilities(&["Console".to_string()], false, None);
assert_eq!(dataset.status, "NONE_WITNESSED");
assert_eq!(seed.status, "NOT_APPLICABLE");
assert!(determinism.deterministic_modulo_args);
let (dataset, _, determinism) =
witnessed_fields_from_capabilities(&["Console".to_string()], true, None);
assert_eq!(dataset.status, "POSSIBLE_UNWITNESSED");
assert!(dataset.grounds.contains("stdin"));
assert!(!determinism.deterministic_modulo_args);
assert!(determinism.grounds.contains("stdin"));
let caps = vec!["Console".to_string(), "FileSystem".to_string()];
let (dataset, _, determinism) = witnessed_fields_from_capabilities(&caps, false, None);
assert_eq!(dataset.status, "POSSIBLE_UNWITNESSED");
assert!(dataset.grounds.contains("FileSystem"));
assert!(!determinism.deterministic_modulo_args);
let caps = vec!["Clock".to_string(), "Console".to_string()];
let (dataset, _, determinism) = witnessed_fields_from_capabilities(&caps, false, None);
assert_eq!(dataset.status, "NONE_WITNESSED");
assert!(!determinism.deterministic_modulo_args);
assert!(determinism.grounds.contains("Clock"));
let caps = vec!["Console".to_string(), "Foreign".to_string()];
let (dataset, _, determinism) = witnessed_fields_from_capabilities(&caps, false, None);
assert_eq!(dataset.status, "POSSIBLE_UNWITNESSED");
assert!(dataset.grounds.contains("Foreign"));
assert!(!determinism.deterministic_modulo_args);
let caps = vec!["Console".to_string(), "Gpu".to_string()];
let (dataset, _, determinism) = witnessed_fields_from_capabilities(&caps, false, None);
assert_eq!(dataset.status, "POSSIBLE_UNWITNESSED");
assert!(dataset.grounds.contains("Gpu"));
assert!(!determinism.deterministic_modulo_args);
let caps = vec!["Console".to_string(), "Environment".to_string()];
let (dataset, _, determinism) = witnessed_fields_from_capabilities(&caps, false, None);
assert_eq!(dataset.status, "POSSIBLE_UNWITNESSED");
assert!(!determinism.deterministic_modulo_args);
let caps = vec!["Console".to_string(), "Process".to_string()];
let (dataset, _, determinism) = witnessed_fields_from_capabilities(&caps, false, None);
assert_eq!(dataset.status, "NONE_WITNESSED");
assert!(determinism.deterministic_modulo_args);
let caps = vec!["Console".to_string(), "Bluetooth".to_string()];
let (dataset, _, determinism) = witnessed_fields_from_capabilities(&caps, false, None);
assert_eq!(dataset.status, "POSSIBLE_UNWITNESSED");
assert!(dataset.grounds.contains("Bluetooth"));
assert!(!determinism.deterministic_modulo_args);
}
#[test]
fn witnessed_fields_treat_model_as_a_hazard_for_both_claims() {
let caps = vec!["Console".to_string(), "Model".to_string()];
let (dataset, _, determinism) = witnessed_fields_from_capabilities(&caps, false, None);
assert_eq!(dataset.status, "POSSIBLE_UNWITNESSED");
assert!(dataset.grounds.contains("Model"));
assert!(!determinism.deterministic_modulo_args);
assert!(determinism.grounds.contains("Model"));
}
#[test]
fn witnessed_fields_derive_the_seed_trichotomy() {
let caps = vec!["Console".to_string(), "Random".to_string()];
let (dataset, seed, determinism) =
witnessed_fields_from_capabilities(&caps, false, Some(42));
assert_eq!(dataset.status, "NONE_WITNESSED");
assert_eq!(seed.status, "SEALED");
assert!(seed.grounds.contains("42"));
assert!(determinism.deterministic_modulo_args);
assert!(determinism.grounds.contains("seeded"));
let (dataset, seed, determinism) = witnessed_fields_from_capabilities(&caps, false, None);
assert_eq!(dataset.status, "NONE_WITNESSED");
assert_eq!(seed.status, "UNSEEDED");
assert!(!determinism.deterministic_modulo_args);
assert!(determinism.grounds.contains("unseeded Random"));
let (_, seed, _) =
witnessed_fields_from_capabilities(&["Console".to_string()], false, None);
assert_eq!(seed.status, "NOT_APPLICABLE");
}
#[test]
fn verify_enforces_the_seed_capability_pairing() {
let random_policy = || ScientificEffectPolicy {
facts_digest: hex_digest('9'),
observed_capabilities: vec!["Console".to_string(), "Random".to_string()],
reads_stdin: false,
};
let path = Path::new("k.bld");
let receipt = build_scientific_runtime_receipt(ScientificReceiptInputs {
effect_policy: random_policy(),
seed_value: Some(42),
..base_inputs(path, vec![4.0, 3.0, 2.0], true, false)
});
assert_eq!(receipt.seed.status, "SEALED");
assert_eq!(receipt.seed_value, Some(42));
let value = serde_json::to_value(&receipt).expect("to_value");
let src = receipt.source_digest.clone();
let graph = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| {
Ok(RederivedFacts {
source_digest: src.clone(),
input_graph_digest: graph.clone(),
effect_policy: random_policy(),
})
},
|_, _, seed, _, _| {
assert_eq!(seed, Some(42), "the re-run must receive the sealed seed");
Ok(rerun(vec![4.0, 3.0, 2.0]))
},
);
assert_eq!(result, Ok(()), "a seeded Random receipt must verify");
let unseeded = build_scientific_runtime_receipt(ScientificReceiptInputs {
effect_policy: random_policy(),
seed_value: None,
..base_inputs(path, vec![4.0, 3.0, 2.0], true, false)
});
let value = serde_json::to_value(&unseeded).expect("to_value");
let src = unseeded.source_digest.clone();
let graph = unseeded.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&unseeded.compiler_version,
&unseeded.language_version,
Some(&test_toolchain()),
|_| {
Ok(RederivedFacts {
source_digest: src.clone(),
input_graph_digest: graph.clone(),
effect_policy: random_policy(),
})
},
|_, _, _, _, _| panic!("an unseeded Random receipt must be refused before the re-run"),
);
assert_eq!(
result,
Err(1),
"a Random program with no sealed seed must fail"
);
let overclaimed = build_scientific_runtime_receipt(ScientificReceiptInputs {
seed_value: Some(7),
..base_inputs(path, vec![4.0, 3.0, 2.0], true, false)
});
let value = serde_json::to_value(&overclaimed).expect("to_value");
let src = overclaimed.source_digest.clone();
let graph = overclaimed.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&overclaimed.compiler_version,
&overclaimed.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src.clone(), graph.clone())),
|_, _, _, _, _| panic!("a seed nothing consumes must be refused before the re-run"),
);
assert_eq!(
result,
Err(1),
"a seed without a Random capability must fail"
);
let receipt = build_scientific_runtime_receipt(ScientificReceiptInputs {
effect_policy: random_policy(),
seed_value: Some(42),
..base_inputs(path, vec![4.0, 3.0, 2.0], true, false)
});
let mut swapped = receipt.clone();
swapped.seed_value = Some(43);
seal_receipt(&mut swapped);
let value = serde_json::to_value(&swapped).expect("to_value");
let src = swapped.source_digest.clone();
let graph = swapped.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&swapped.compiler_version,
&swapped.language_version,
Some(&test_toolchain()),
|_| {
Ok(RederivedFacts {
source_digest: src.clone(),
input_graph_digest: graph.clone(),
effect_policy: random_policy(),
})
},
|_, _, _, _, _| panic!("a swapped seed must be refused before the re-run"),
);
assert_eq!(result, Err(1), "a re-sealed seed swap must fail");
}
#[test]
fn verify_refuses_a_model_observing_receipt() {
let model_policy = || ScientificEffectPolicy {
facts_digest: hex_digest('9'),
observed_capabilities: vec!["Console".to_string(), "Model".to_string()],
reads_stdin: false,
};
let path = Path::new("k.bld");
let receipt = build_scientific_runtime_receipt(ScientificReceiptInputs {
effect_policy: model_policy(),
..base_inputs(path, vec![4.0, 3.0, 2.0], true, false)
});
let value = serde_json::to_value(&receipt).expect("to_value");
let src = receipt.source_digest.clone();
let graph = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| {
Ok(RederivedFacts {
source_digest: src.clone(),
input_graph_digest: graph.clone(),
effect_policy: model_policy(),
})
},
|_, _, _, _, _| panic!("a model-observing receipt must be refused before the re-run"),
);
assert_eq!(
result,
Err(1),
"a receipt whose capabilities include Model must fail"
);
}
#[test]
fn verify_enforces_the_monte_carlo_admission_contracts() {
fn random_policy() -> ScientificEffectPolicy {
ScientificEffectPolicy {
facts_digest: hex_digest('9'),
observed_capabilities: vec!["Console".to_string(), "Random".to_string()],
reads_stdin: false,
}
}
fn mc() -> ScientificMonteCarlo {
ScientificMonteCarlo {
estimator: "mean".to_string(),
samples: 2000,
interval_method: "normal-approx-95".to_string(),
status: "DECLARED".to_string(),
estimate: None,
interval_low: None,
interval_high: None,
n_effective: None,
successes: None,
}
}
fn run(
receipt: &ScientificRuntimeReceipt,
policy: ScientificEffectPolicy,
) -> Result<(), i32> {
let value = serde_json::to_value(receipt).expect("to_value");
let src = receipt.source_digest.clone();
let graph = receipt.input_graph_digest.clone();
verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
move |_| {
Ok(RederivedFacts {
source_digest: src,
input_graph_digest: graph,
effect_policy: policy,
})
},
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
)
}
let path = Path::new("k.bld");
let seeded_mc = |block: ScientificMonteCarlo| {
build_scientific_runtime_receipt(ScientificReceiptInputs {
effect_policy: random_policy(),
seed_value: Some(42),
monte_carlo: Some(block),
..base_inputs(path, vec![4.0, 3.0, 2.0], true, false)
})
};
let receipt = seeded_mc(mc());
let value = serde_json::to_value(&receipt).unwrap();
assert_eq!(value["monte_carlo"]["samples"], 2000);
assert_eq!(value["monte_carlo"]["interval_method"], "normal-approx-95");
assert_eq!(
run(&receipt, random_policy()),
Ok(()),
"a complete MC declaration on a seeded Random run must verify"
);
let mc_keys: std::collections::BTreeSet<&str> = value["monte_carlo"]
.as_object()
.expect("monte_carlo is an object")
.keys()
.map(|k| k.as_str())
.collect();
assert_eq!(
mc_keys,
["estimator", "samples", "interval_method", "status"]
.into_iter()
.collect::<std::collections::BTreeSet<&str>>(),
"a DECLARED monte_carlo block must serialize with exactly its original four keys"
);
let mut bad = mc();
bad.samples = 0;
assert_eq!(run(&seeded_mc(bad), random_policy()), Err(1));
let mut bad = mc();
bad.interval_method = " ".to_string();
assert_eq!(run(&seeded_mc(bad), random_policy()), Err(1));
let mut bad = mc();
bad.status = "EXECUTED".to_string();
assert_eq!(run(&seeded_mc(bad), random_policy()), Err(1));
let mut bad = mc();
bad.status = "SIMULATED".to_string();
assert_eq!(run(&seeded_mc(bad), random_policy()), Err(1));
let unpaired = build_scientific_runtime_receipt(ScientificReceiptInputs {
monte_carlo: Some(mc()),
..base_inputs(path, vec![4.0, 3.0, 2.0], true, false)
});
assert_eq!(run(&unpaired, test_effect_policy()), Err(1));
let plain =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
let value = serde_json::to_value(&plain).unwrap();
assert!(value.get("monte_carlo").is_none());
}
fn mc_executed_random_policy() -> ScientificEffectPolicy {
ScientificEffectPolicy {
facts_digest: hex_digest('9'),
observed_capabilities: vec!["Console".to_string(), "Random".to_string()],
reads_stdin: false,
}
}
fn coherent_executed_mc_receipt(path: &Path) -> ScientificRuntimeReceipt {
let series = vec![
0.0, 1.0, 1.0, 0.0, 1.0, 2.0, 0.0, 2.0, 3.0, 0.0, 2.0, 4.0, ];
let computed = compute_mc_executed(&series, 3, 4, MC_INTERVAL_WILSON_95)
.expect("fixture series must be coherent");
let mc = ScientificMonteCarlo {
estimator: MC_EXECUTED_ESTIMATOR_PROPORTION.to_string(),
samples: 4,
interval_method: MC_INTERVAL_WILSON_95.to_string(),
status: "EXECUTED".to_string(),
estimate: Some(computed.estimate),
interval_low: Some(computed.interval_low),
interval_high: Some(computed.interval_high),
n_effective: Some(computed.n_effective),
successes: Some(computed.successes),
};
build_scientific_runtime_receipt(ScientificReceiptInputs {
effect_policy: mc_executed_random_policy(),
seed_value: Some(42),
monte_carlo: Some(mc),
column_count: 3,
..base_inputs_for(NON_NEGATIVE_INVARIANT, path, series, true, false)
})
}
fn coherent_executed_mc_receipt_all_failures(path: &Path) -> ScientificRuntimeReceipt {
let series = vec![
0.0, 0.0, 1.0, 0.0, 0.0, 2.0, 0.0, 0.0, 3.0, 0.0, 0.0, 4.0, ];
let computed = compute_mc_executed(&series, 3, 4, MC_INTERVAL_WILSON_95)
.expect("an all-failure series is coherent under wilson-95");
assert_eq!(computed.successes, 0, "fixture precondition");
let mc = ScientificMonteCarlo {
estimator: MC_EXECUTED_ESTIMATOR_PROPORTION.to_string(),
samples: 4,
interval_method: MC_INTERVAL_WILSON_95.to_string(),
status: "EXECUTED".to_string(),
estimate: Some(computed.estimate),
interval_low: Some(computed.interval_low),
interval_high: Some(computed.interval_high),
n_effective: Some(computed.n_effective),
successes: Some(computed.successes),
};
build_scientific_runtime_receipt(ScientificReceiptInputs {
effect_policy: mc_executed_random_policy(),
seed_value: Some(42),
monte_carlo: Some(mc),
column_count: 3,
..base_inputs_for(NON_NEGATIVE_INVARIANT, path, series, true, false)
})
}
#[test]
fn verify_round_trips_a_coherent_executed_monte_carlo_receipt() {
let path = Path::new("k.bld");
let receipt = coherent_executed_mc_receipt(path);
let value = serde_json::to_value(&receipt).expect("to_value");
assert_eq!(value["monte_carlo"]["status"], "EXECUTED");
assert_eq!(value["monte_carlo"]["n_effective"], 4);
assert_eq!(value["monte_carlo"]["successes"], 2);
assert!(receipt.scope.iter().any(|c| c == "interval_arithmetic"));
assert!(receipt.scope.iter().any(|c| c == "sample_denominator"));
let src = receipt.source_digest.clone();
let graph = receipt.input_graph_digest.clone();
let series = receipt.measurement.observed_values.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
move |_| {
Ok(RederivedFacts {
source_digest: src,
input_graph_digest: graph,
effect_policy: mc_executed_random_policy(),
})
},
move |_, _, _, _, _| Ok(rerun(series)),
);
assert_eq!(result, Ok(()), "a faithful EXECUTED receipt must verify");
}
#[test]
fn verify_stage_a_rejects_tampered_executed_interval_before_any_rerun() {
let path = Path::new("k.bld");
let receipt = coherent_executed_mc_receipt(path);
let value = serde_json::to_value(&receipt).expect("to_value");
let mut tampered = value.clone();
let bumped = tampered["monte_carlo"]["interval_high"]
.as_f64()
.expect("interval_high must be present")
+ 0.25;
tampered["monte_carlo"]["interval_high"] = serde_json::Value::from(bumped);
let tampered = reseal_json(&tampered).expect("reseal must succeed");
let src = receipt.source_digest.clone();
let graph = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&tampered,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
move |_| {
Ok(RederivedFacts {
source_digest: src,
input_graph_digest: graph,
effect_policy: mc_executed_random_policy(),
})
},
|_, _, _, _, _| panic!("Stage A must reject a tampered interval before any re-run"),
);
assert_eq!(result, Err(1));
}
#[test]
fn verify_stage_b_reports_mc_interval_drift_on_a_changed_rerun() {
let path = Path::new("k.bld");
let receipt = coherent_executed_mc_receipt(path);
let value = serde_json::to_value(&receipt).expect("to_value");
let src = receipt.source_digest.clone();
let graph = receipt.input_graph_digest.clone();
let drifted_series = vec![
0.0, 1.0, 1.0, 0.0, 2.0, 2.0, 0.0, 3.0, 3.0, 0.0, 3.0, 4.0, ];
let report = evaluate_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
move |_| {
Ok(RederivedFacts {
source_digest: src,
input_graph_digest: graph,
effect_policy: mc_executed_random_policy(),
})
},
move |_, _, _, _, _| Ok(rerun(drifted_series)),
);
assert!(report.is_err(), "a Stage B interval drift must fail verify");
}
#[test]
fn verify_refuses_declared_block_carrying_an_executed_field() {
let path = Path::new("k.bld");
let mut mc = ScientificMonteCarlo {
estimator: "mean".to_string(),
samples: 2000,
interval_method: "normal-approx-95".to_string(),
status: "DECLARED".to_string(),
estimate: None,
interval_low: None,
interval_high: None,
n_effective: None,
successes: None,
};
mc.estimate = Some(0.5); let receipt = build_scientific_runtime_receipt(ScientificReceiptInputs {
effect_policy: mc_executed_random_policy(),
seed_value: Some(42),
monte_carlo: Some(mc),
..base_inputs(path, vec![4.0, 3.0, 2.0], true, false)
});
let value = serde_json::to_value(&receipt).expect("to_value");
let src = receipt.source_digest.clone();
let graph = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
move |_| {
Ok(RederivedFacts {
source_digest: src,
input_graph_digest: graph,
effect_policy: mc_executed_random_policy(),
})
},
|_, _, _, _, _| {
panic!("a DECLARED-with-executed-field block must reject before any re-run")
},
);
assert_eq!(result, Err(1));
}
#[test]
fn verify_refuses_executed_block_missing_an_executed_field() {
let path = Path::new("k.bld");
let receipt = coherent_executed_mc_receipt_all_failures(path);
let mut value = serde_json::to_value(&receipt).expect("to_value");
value["monte_carlo"]
.as_object_mut()
.unwrap()
.remove("successes");
let value = reseal_json(&value).expect("reseal must succeed");
let src = receipt.source_digest.clone();
let graph = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
move |_| {
Ok(RederivedFacts {
source_digest: src,
input_graph_digest: graph,
effect_policy: mc_executed_random_policy(),
})
},
|_, _, _, _, _| panic!("EXECUTED with a missing field must reject before any re-run"),
);
assert_eq!(result, Err(1));
}
#[test]
fn verify_refuses_executed_estimator_outside_vocabulary() {
let path = Path::new("k.bld");
let receipt = coherent_executed_mc_receipt(path);
let mut value = serde_json::to_value(&receipt).expect("to_value");
value["monte_carlo"]["estimator"] = serde_json::Value::from("mean");
let value = reseal_json(&value).expect("reseal must succeed");
let src = receipt.source_digest.clone();
let graph = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
move |_| {
Ok(RederivedFacts {
source_digest: src,
input_graph_digest: graph,
effect_policy: mc_executed_random_policy(),
})
},
|_, _, _, _, _| panic!("an out-of-vocabulary estimator must reject before any re-run"),
);
assert_eq!(result, Err(1));
}
fn as_version_0(receipt: &ScientificRuntimeReceipt) -> serde_json::Value {
let mut value = serde_json::to_value(receipt).expect("to_value");
value["schema"] = serde_json::Value::from(SCIENTIFIC_RUNTIME_SCHEMA_V0);
value.as_object_mut().unwrap().remove("scope");
value["not_claimed"] = serde_json::json!(NOT_CLAIMED_BOUNDARY);
let labels: Vec<String> = receipt
.labels
.iter()
.map(|l| {
if l == LABEL_NUMERICAL_RESULT {
LABEL_NUMERICAL_RESULT_V0.to_string()
} else {
l.clone()
}
})
.collect();
value["labels"] = serde_json::json!(labels);
reseal_json(&value).expect("reseal must succeed")
}
#[test]
fn a_version_0_receipt_still_verifies() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
let run = |value: &serde_json::Value| {
let src = receipt.source_digest.clone();
let graph = receipt.input_graph_digest.clone();
verify_scientific_runtime_receipt(
value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
move |_| Ok(rederive_facts(src, graph)),
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
)
};
let old = as_version_0(&receipt);
assert_eq!(old["schema"], SCIENTIFIC_RUNTIME_SCHEMA_V0);
assert!(old.get("scope").is_none());
assert_eq!(run(&old), Ok(()), "a version 0 receipt must still verify");
let mut missing = old.clone();
missing["not_claimed"] = serde_json::json!(["convergence"]);
assert_eq!(run(&reseal_json(&missing).expect("reseal")), Err(1));
let mut both = old.clone();
both["scope"] = serde_json::json!(SCOPE_BASE);
assert_eq!(run(&reseal_json(&both).expect("reseal")), Err(1));
let mut mixed = serde_json::to_value(&receipt).expect("to_value");
mixed["not_claimed"] = serde_json::json!(NOT_CLAIMED_BOUNDARY);
assert_eq!(run(&reseal_json(&mixed).expect("reseal")), Err(1));
}
#[test]
fn verify_mc_scope_pair_biconditional() {
let path = Path::new("k.bld");
fn check(receipt: &ScientificRuntimeReceipt, value: serde_json::Value) -> Result<(), i32> {
let src = receipt.source_digest.clone();
let graph = receipt.input_graph_digest.clone();
verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
move |_| {
Ok(RederivedFacts {
source_digest: src,
input_graph_digest: graph,
effect_policy: mc_executed_random_policy(),
})
},
|_, _, _, _, _| panic!("an mc scope pair mismatch must reject before any re-run"),
)
}
let receipt = coherent_executed_mc_receipt(path);
let mut value = serde_json::to_value(&receipt).expect("to_value");
value["scope"] = serde_json::Value::Array(
receipt
.scope
.iter()
.filter(|c| !MC_EXECUTED_SCOPE.contains(&c.as_str()))
.map(|c| serde_json::Value::from(c.clone()))
.collect(),
);
let value = reseal_json(&value).expect("reseal must succeed");
assert_eq!(check(&receipt, value), Err(1));
let plain =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
let mut value = serde_json::to_value(&plain).expect("to_value");
let mut scope: Vec<serde_json::Value> = value["scope"].as_array().unwrap().to_vec();
for c in MC_EXECUTED_SCOPE {
scope.push(serde_json::Value::from(*c));
}
value["scope"] = serde_json::Value::Array(scope);
let value = reseal_json(&value).expect("reseal must succeed");
assert_eq!(check(&plain, value), Err(1));
let receipt = coherent_executed_mc_receipt(path);
let mut value = serde_json::to_value(&receipt).expect("to_value");
value["scope"] = serde_json::Value::Array(
receipt
.scope
.iter()
.filter(|c| c.as_str() != "interval_arithmetic")
.map(|c| serde_json::Value::from(c.clone()))
.collect(),
);
let value = reseal_json(&value).expect("reseal must succeed");
assert_eq!(check(&receipt, value), Err(1));
}
#[test]
fn verify_enforces_the_budget_admission_contracts() {
fn budget() -> ScientificBudget {
ScientificBudget {
steps_limit: 500,
steps_consumed: 437,
exhausted: false,
status: "DECLARED".to_string(),
wall_seconds_limit: None,
wall_exceeded: None,
}
}
fn run(
receipt: &ScientificRuntimeReceipt,
policy: ScientificEffectPolicy,
) -> Result<(), i32> {
let value = serde_json::to_value(receipt).expect("to_value");
let src = receipt.source_digest.clone();
let graph = receipt.input_graph_digest.clone();
verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
move |_| {
Ok(RederivedFacts {
source_digest: src,
input_graph_digest: graph,
effect_policy: policy,
})
},
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
)
}
let path = Path::new("k.bld");
let budgeted = |block: ScientificBudget| {
build_scientific_runtime_receipt(ScientificReceiptInputs {
budget: Some(block),
..base_inputs(path, vec![4.0, 3.0, 2.0], true, false)
})
};
let receipt = budgeted(budget());
let value = serde_json::to_value(&receipt).unwrap();
assert_eq!(value["budget"]["steps_limit"], 500);
assert_eq!(value["budget"]["steps_consumed"], 437);
assert_eq!(value["budget"]["exhausted"], false);
assert!(receipt
.labels
.contains(&LABEL_BEST_FOUND_WITHIN_BUDGET.to_string()));
assert!(receipt.scope.contains(&SCOPE_BUDGET.to_string()));
assert_eq!(
run(&receipt, test_effect_policy()),
Ok(()),
"a complete, coherent budget declaration must verify"
);
let mut bad = budget();
bad.steps_limit = 0;
assert_eq!(run(&budgeted(bad), test_effect_policy()), Err(1));
let mut bad = budget();
bad.steps_consumed = bad.steps_limit + 1;
assert_eq!(run(&budgeted(bad), test_effect_policy()), Err(1));
let mut bad = budget();
bad.exhausted = true;
assert_eq!(run(&budgeted(bad), test_effect_policy()), Err(1));
let mut bad = budget();
bad.status = "METERED".to_string();
assert_eq!(run(&budgeted(bad), test_effect_policy()), Err(1));
let claims_optimal = build_scientific_runtime_receipt(ScientificReceiptInputs {
budget: Some(budget()),
problem_label: Some("greedy-change-optimal".to_string()),
..base_inputs(path, vec![4.0, 3.0, 2.0], true, false)
});
assert_eq!(run(&claims_optimal, test_effect_policy()), Err(1));
let mut stripped = receipt.clone();
stripped
.labels
.retain(|l| l != LABEL_BEST_FOUND_WITHIN_BUDGET);
seal_receipt(&mut stripped);
assert_eq!(run(&stripped, test_effect_policy()), Err(1));
let mut faked =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
faked
.labels
.push(LABEL_BEST_FOUND_WITHIN_BUDGET.to_string());
seal_receipt(&mut faked);
assert_eq!(run(&faked, test_effect_policy()), Err(1));
let plain =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
let value = serde_json::to_value(&plain).unwrap();
assert!(value.get("budget").is_none());
assert!(!plain
.labels
.contains(&LABEL_BEST_FOUND_WITHIN_BUDGET.to_string()));
}
#[test]
fn verify_enforces_the_wall_metering_contracts() {
fn budget_with_wall(limit: Option<f64>, exceeded: Option<bool>) -> ScientificBudget {
ScientificBudget {
steps_limit: 500,
steps_consumed: 437,
exhausted: false,
status: "DECLARED".to_string(),
wall_seconds_limit: limit,
wall_exceeded: exceeded,
}
}
fn run(
receipt: &ScientificRuntimeReceipt,
policy: ScientificEffectPolicy,
) -> Result<(), i32> {
let value = serde_json::to_value(receipt).expect("to_value");
let src = receipt.source_digest.clone();
let graph = receipt.input_graph_digest.clone();
verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
move |_| {
Ok(RederivedFacts {
source_digest: src,
input_graph_digest: graph,
effect_policy: policy,
})
},
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
)
}
let path = Path::new("k.bld");
let metered = |block: ScientificBudget, wall_seconds: Option<f64>| {
build_scientific_runtime_receipt(ScientificReceiptInputs {
budget: Some(block),
wall_seconds,
..base_inputs(path, vec![4.0, 3.0, 2.0], true, false)
})
};
let receipt = metered(budget_with_wall(Some(300.0), Some(false)), Some(12.5));
let value = serde_json::to_value(&receipt).unwrap();
assert_eq!(value["runtime_state"]["wall_seconds"], 12.5);
assert_eq!(value["budget"]["wall_seconds_limit"], 300.0);
assert_eq!(value["budget"]["wall_exceeded"], false);
assert_eq!(
run(&receipt, test_effect_policy()),
Ok(()),
"a coherent wall ceiling declaration must verify"
);
let bad_exceeded = metered(budget_with_wall(Some(300.0), Some(true)), Some(12.5));
assert_eq!(run(&bad_exceeded, test_effect_policy()), Err(1));
let bad_flag_only = metered(budget_with_wall(None, Some(false)), Some(12.5));
assert_eq!(run(&bad_flag_only, test_effect_policy()), Err(1));
let bad_no_measurement = metered(budget_with_wall(Some(300.0), Some(false)), None);
assert_eq!(run(&bad_no_measurement, test_effect_policy()), Err(1));
let mut zeroed_steps = budget_with_wall(Some(300.0), Some(false));
zeroed_steps.steps_limit = 0;
zeroed_steps.steps_consumed = 0;
zeroed_steps.exhausted = true;
let bad_zeroed_steps = metered(zeroed_steps, Some(12.5));
assert_eq!(
run(&bad_zeroed_steps, test_effect_policy()),
Err(1),
"a zeroed steps_limit with coherent wall fields present must still be refused by the pre-existing steps gates"
);
let plain = metered(budget_with_wall(None, None), None);
let value = serde_json::to_value(&plain).unwrap();
assert!(value["runtime_state"].get("wall_seconds").is_none());
assert!(value["budget"].get("wall_seconds_limit").is_none());
assert!(value["budget"].get("wall_exceeded").is_none());
assert_eq!(
run(&plain, test_effect_policy()),
Ok(()),
"a pre-wall-metering-shaped receipt (no wall fields at all) must still verify"
);
}
fn cross_backend_block() -> ScientificCrossBackend {
ScientificCrossBackend {
secondary_target: "rust".to_string(),
secondary_toolchain_version: "rustc 1.0.0 (test)".to_string(),
secondary_toolchain_digest: hex_digest('1'),
secondary_executable_digest: hex_digest('2'),
secondary_raw_stdout_digest: hex_digest('3'),
secondary_exit_code: 0,
status: "EXECUTED".to_string(),
}
}
fn cross_backend_inputs<'a>(
path: &'a Path,
series: Vec<f64>,
parsed: bool,
negative_fixture: bool,
) -> ScientificReceiptInputs<'a> {
ScientificReceiptInputs {
invariant_name: CROSS_BACKEND_INVARIANT.to_string(),
column_count: 2,
cross_backend: Some(cross_backend_block()),
..base_inputs(path, series, parsed, negative_fixture)
}
}
#[test]
fn cross_backend_receipt_round_trips_and_pins_the_secondary_target() {
let path = Path::new("k.bld");
let series = vec![1.0, 1.0000005, 0.5, 0.5000001];
let receipt =
build_scientific_runtime_receipt(cross_backend_inputs(path, series, true, false));
assert_eq!(receipt.invariant.name, CROSS_BACKEND_INVARIANT);
assert_eq!(receipt.oracle.name, CROSS_BACKEND_INVARIANT);
assert_eq!(receipt.measurement.column_count, 2);
assert_eq!(receipt.measurement.count, 4);
assert_eq!(receipt.receipt_status, "PASS");
assert_eq!(receipt.invariant.observed.violation_count, 0);
assert!(receipt.cross_backend.is_some());
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let primary_rerun = vec![1.0, 0.5];
let secondary_rerun = vec![1.0000005, 0.5000001];
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, target, _| {
assert_eq!(
target,
Some("rust"),
"the rerun closure must receive the secondary target from the sealed block"
);
let mut observation = rerun(primary_rerun.clone());
observation.secondary = Some(SecondaryObservation {
parsed: ParsedSeries {
series: secondary_rerun.clone(),
any_parsed: true,
diverged: false,
},
exit_code: 0,
raw_stdout_digest: hex_digest('3'),
executable_digest: hex_digest('2'),
probed_toolchain_version: "rustc 1.0.0 (test)".to_string(),
probed_toolchain_digest: hex_digest('1'),
});
Ok(observation)
},
);
assert!(
result.is_ok(),
"a faithful cross-backend receipt must verify: {result:?}"
);
}
#[test]
fn cross_backend_evaluator_fails_a_genuine_divergence() {
let path = Path::new("k.bld");
let series = vec![1.0, 1.1, 0.5, 0.5];
let receipt = build_scientific_runtime_receipt(cross_backend_inputs(
path,
series.clone(),
true,
false,
));
assert_eq!(receipt.receipt_status, "FAIL_UNEXPECTED");
assert_eq!(receipt.invariant.status, "FAIL");
assert_eq!(receipt.invariant.observed.violation_count, 1);
assert_eq!(receipt.invariant.observed.first_violation_step, Some(0));
let negative =
build_scientific_runtime_receipt(cross_backend_inputs(path, series, true, true));
assert_eq!(negative.receipt_status, "FAIL_EXPECTED");
}
#[test]
fn verify_enforces_the_cross_backend_admission_contracts() {
let path = Path::new("k.bld");
let series = vec![1.0, 1.0000005, 0.5, 0.5000001];
let receipt =
build_scientific_runtime_receipt(cross_backend_inputs(path, series, true, false));
fn run(receipt: &ScientificRuntimeReceipt) -> Result<(), i32> {
let value = serde_json::to_value(receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
move |_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, _, _| {
panic!("a biconditional or shape violation must be refused before any re-run")
},
)
}
let mut block_without_invariant =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
block_without_invariant.cross_backend = Some(cross_backend_block());
seal_receipt(&mut block_without_invariant);
assert_eq!(
run(&block_without_invariant),
Err(1),
"a cross_backend block without the cross-backend invariant must be refused"
);
let mut invariant_without_block = receipt.clone();
invariant_without_block.cross_backend = None;
seal_receipt(&mut invariant_without_block);
assert_eq!(
run(&invariant_without_block),
Err(1),
"the cross-backend invariant without its block must be refused"
);
let mut wrong_target = receipt.clone();
wrong_target
.cross_backend
.as_mut()
.unwrap()
.secondary_target = "vulkan".to_string();
seal_receipt(&mut wrong_target);
assert_eq!(
run(&wrong_target),
Err(1),
"an unsupported secondary_target must be refused"
);
let mut wrong_status = receipt.clone();
wrong_status.cross_backend.as_mut().unwrap().status = "DECLARED".to_string();
seal_receipt(&mut wrong_status);
assert_eq!(
run(&wrong_status),
Err(1),
"a non-EXECUTED cross_backend.status must be refused"
);
}
#[test]
fn verify_refuses_a_cross_backend_receipt_over_a_random_kernel() {
fn random_policy() -> ScientificEffectPolicy {
ScientificEffectPolicy {
facts_digest: hex_digest('9'),
observed_capabilities: vec!["Console".to_string(), "Random".to_string()],
reads_stdin: false,
}
}
let path = Path::new("k.bld");
let series = vec![1.0, 1.0000005, 0.5, 0.5000001];
let receipt = build_scientific_runtime_receipt(ScientificReceiptInputs {
effect_policy: random_policy(),
seed_value: Some(42),
..cross_backend_inputs(path, series, true, false)
});
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
move |_| {
Ok(RederivedFacts {
source_digest: sd,
input_graph_digest: gd,
effect_policy: random_policy(),
})
},
|_, _, _, _, _| panic!("must be refused before the re-run"),
);
assert_eq!(
result,
Err(1),
"a cross_backend receipt over a re-derived Random kernel must be refused"
);
}
#[test]
fn verify_passes_none_as_the_secondary_target_when_no_cross_backend_block() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
assert!(receipt.cross_backend.is_none());
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, target, _| {
assert_eq!(
target, None,
"a receipt without a cross_backend block must pass None as the secondary target"
);
Ok(rerun(vec![4.0, 3.0, 2.0]))
},
);
assert!(
result.is_ok(),
"a plain receipt must still verify: {result:?}"
);
}
#[test]
fn verify_rejects_a_cross_backend_length_mismatch() {
let path = Path::new("k.bld");
let series = vec![1.0, 1.0000005, 0.5, 0.5000001];
let receipt =
build_scientific_runtime_receipt(cross_backend_inputs(path, series, true, false));
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, target, _| {
assert_eq!(target, Some("rust"));
let mut observation = rerun(vec![1.0, 0.5]);
observation.secondary = Some(SecondaryObservation {
parsed: ParsedSeries {
series: vec![1.0000005, 0.5000001, 0.7],
any_parsed: true,
diverged: false,
},
exit_code: 0,
raw_stdout_digest: hex_digest('3'),
executable_digest: hex_digest('2'),
probed_toolchain_version: "rustc 1.0.0 (test)".to_string(),
probed_toolchain_digest: hex_digest('1'),
});
Ok(observation)
},
);
assert_eq!(
result,
Err(1),
"a primary/secondary re-run length mismatch must be refused"
);
}
#[test]
fn verify_reports_secondary_toolchain_matched_when_probe_agrees_with_the_seal() {
let path = Path::new("k.bld");
let series = vec![1.0, 1.0000005, 0.5, 0.5000001];
let receipt =
build_scientific_runtime_receipt(cross_backend_inputs(path, series, true, false));
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let report = evaluate_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, target, _| {
assert_eq!(target, Some("rust"));
let mut observation = rerun(vec![1.0, 0.5]);
observation.secondary = Some(SecondaryObservation {
parsed: ParsedSeries {
series: vec![1.0000005, 0.5000001],
any_parsed: true,
diverged: false,
},
exit_code: 0,
raw_stdout_digest: hex_digest('3'),
executable_digest: hex_digest('2'),
probed_toolchain_version: "rustc 1.0.0 (test)".to_string(),
probed_toolchain_digest: hex_digest('1'),
});
Ok(observation)
},
)
.expect("a faithful cross-backend receipt with a matching secondary toolchain must verify");
assert!(
report.secondary_toolchain_matched,
"a probed rustc matching the sealed digest must report secondary_toolchain_matched=true"
);
}
#[test]
fn verify_warns_but_still_matches_on_a_mismatched_secondary_toolchain() {
let path = Path::new("k.bld");
let series = vec![1.0, 1.0000005, 0.5, 0.5000001];
let receipt =
build_scientific_runtime_receipt(cross_backend_inputs(path, series, true, false));
let value = serde_json::to_value(&receipt).expect("to_value");
let sd = receipt.source_digest.clone();
let gd = receipt.input_graph_digest.clone();
let report = evaluate_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(sd.clone(), gd.clone())),
|_, _, _, target, _| {
assert_eq!(target, Some("rust"));
let mut observation = rerun(vec![1.0, 0.5]);
observation.secondary = Some(SecondaryObservation {
parsed: ParsedSeries {
series: vec![1.0000005, 0.5000001],
any_parsed: true,
diverged: false,
},
exit_code: 0,
raw_stdout_digest: hex_digest('3'),
executable_digest: hex_digest('2'),
probed_toolchain_version: "rustc 9.9.9 (substituted)".to_string(),
probed_toolchain_digest: hex_digest('9'),
});
Ok(observation)
},
)
.expect(
"a mismatched secondary toolchain must WARN, not fail: cross-toolchain \
re-verification stays legitimate by design",
);
assert!(
!report.secondary_toolchain_matched,
"a probed rustc differing from the sealed digest must report secondary_toolchain_matched=false"
);
}
#[test]
fn verify_rejects_effect_policy_drift() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| {
let mut facts = rederive_facts(src_digest.clone(), graph_digest.clone());
facts
.effect_policy
.observed_capabilities
.push("FileSystem".to_string());
facts.effect_policy.facts_digest = hex_digest('8');
Ok(facts)
},
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
);
assert_eq!(result, Err(1), "effect-policy drift must fail verify");
}
#[test]
fn verify_rejects_witnessed_fields_that_do_not_rederive() {
let path = Path::new("k.bld");
let mut receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
receipt.determinism.deterministic_modulo_args = false;
receipt.determinism.grounds = "edited".to_string();
seal_receipt(&mut receipt);
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
);
assert_eq!(
result,
Err(1),
"witnessed fields that do not re-derive must fail verify"
);
}
#[test]
fn verify_rejects_field_contract_violations() {
let path = Path::new("k.bld");
let mut receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
receipt.seed.status = "RECORDED".to_string();
seal_receipt(&mut receipt);
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
);
assert_eq!(
result,
Err(1),
"an inexpressible seed status must be rejected"
);
let mut receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
receipt.numerical_method.status = "DECLARED".to_string();
receipt.numerical_method.description = None;
seal_receipt(&mut receipt);
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
);
assert_eq!(
result,
Err(1),
"an inconsistent numerical_method must be rejected"
);
}
#[test]
fn verify_checks_the_seal_before_interpreting_fields() {
let path = Path::new("k.bld");
let mut receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
receipt.seed.status = "RECORDED".to_string();
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| panic!("an unsealed receipt must be rejected before any re-run"),
);
assert_eq!(result, Err(1), "an unsealed field edit must be rejected");
}
#[test]
fn verify_fails_with_exit_4_when_no_toolchain_is_available() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
None,
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| panic!("the re-run must never be attempted without a toolchain"),
);
assert_eq!(result, Err(4), "a missing toolchain must exit 4");
}
#[test]
fn verify_warns_but_proceeds_on_a_different_toolchain() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let mut other = test_toolchain();
other.c_compiler = "other-cc".to_string();
other.version_output_digest = hex_digest('9');
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&other),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0, 2.0])),
);
assert!(
result.is_ok(),
"a different toolchain must warn, not fail: {result:?}"
);
}
#[test]
fn verify_rejects_an_exit_code_drift() {
let path = Path::new("k.bld");
let receipt =
build_scientific_runtime_receipt(base_inputs(path, vec![4.0, 3.0, 2.0], true, false));
assert_eq!(receipt.runtime_state.exit_code, 0);
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| {
let mut observation = rerun(vec![4.0, 3.0, 2.0]);
observation.exit_code = 9;
Ok(observation)
},
);
assert_eq!(result, Err(1), "an exit-code drift must fail verify");
}
#[test]
fn verify_rejects_a_receipt_whose_divergence_does_not_reproduce() {
let path = Path::new("k.bld");
let mut inputs = base_inputs(path, vec![4.0, 3.0], true, false);
inputs.diverged = true;
let receipt = build_scientific_runtime_receipt(inputs);
assert_eq!(receipt.receipt_status, "UNVERIFIABLE");
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, _, _, _, _| Ok(rerun(vec![4.0, 3.0])),
);
assert_eq!(
result,
Err(1),
"a recorded divergence that does not reproduce is non-reproduction"
);
}
#[test]
fn verify_reruns_with_the_recorded_args() {
let path = Path::new("k.bld");
let mut inputs = base_inputs(path, vec![4.0, 3.0, 2.0], true, false);
inputs.args = vec!["--mode".to_string(), "stable".to_string()];
let receipt = build_scientific_runtime_receipt(inputs);
let value = serde_json::to_value(&receipt).expect("to_value");
let src_digest = receipt.source_digest.clone();
let graph_digest = receipt.input_graph_digest.clone();
let result = verify_scientific_runtime_receipt(
&value,
None,
true,
&receipt.compiler_version,
&receipt.language_version,
Some(&test_toolchain()),
|_| Ok(rederive_facts(src_digest.clone(), graph_digest.clone())),
|_, args, _, _, _| {
assert_eq!(
args,
["--mode".to_string(), "stable".to_string()],
"verify must re-run with the receipt's recorded args"
);
Ok(rerun(vec![4.0, 3.0, 2.0]))
},
);
assert!(result.is_ok(), "recorded-args re-run must verify");
}
}