use std::collections::{BTreeMap, BTreeSet};
use super::{
Classification, Context, Detection, Diagnostic, Failure, Function, FunctionObservation,
MIN_REPEATS, Observation, Outcome, Reason, Scope, Summary,
};
use crate::output::AnswerName;
#[must_use]
pub fn classify(input: &Classification) -> Outcome {
checked(input).unwrap_or_else(Outcome::Inconclusive)
}
fn digest(value: &str) -> bool {
value.len() == 64
&& value
.bytes()
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
}
fn context_valid(context: &Context) -> bool {
[
&context.baseline_sha256,
&context.runtime_sha256,
&context.solver_sha256,
&context.options_sha256,
]
.into_iter()
.all(|v| digest(v))
}
fn function_valid(function: &Function) -> bool {
!function.package_id.is_empty()
&& !function.target.is_empty()
&& !function.function.is_empty()
&& !function.target_kinds.is_empty()
&& function.target_kinds.iter().all(|k| !k.is_empty())
&& function.target_kinds.windows(2).all(|w| w[0] < w[1])
}
fn valid_observation(
observation: &Observation,
context: &Context,
source: &str,
) -> Result<(), Reason> {
if &observation.context != context || observation.source_sha256 != source {
return Err(Reason::IdentityMismatch);
}
let mut seen = BTreeSet::new();
for function in &observation.functions {
if !function_valid(&function.identity) || !seen.insert(&function.identity) {
return Err(Reason::InconsistentEvidence);
}
if function.verified
&& function.checks.iter().any(|check| {
check.failure != Failure::None
|| matches!(check.answer, Some(AnswerName::Sat | AnswerName::Unknown))
})
{
return Err(Reason::InconsistentEvidence);
}
}
for diagnostic in observation
.diagnostics
.iter()
.filter(|d| d.level == "error")
{
if let Some(identity) = &diagnostic.function {
let observed = observation
.functions
.iter()
.find(|f| &f.identity == identity);
if observed.is_some_and(|f| f.verified)
|| (observed.is_none() && observation.functions_complete && !observation.unviable)
{
return Err(Reason::InconsistentEvidence);
}
}
}
if observation.unviable
&& observation.completed
&& !observation.diagnostics.iter().any(|d| d.level == "error")
{
return Err(Reason::InconsistentEvidence);
}
if observation.diagnostics.iter().any(|d| {
d.level != d.original.level
|| d.code != d.original.code
|| d.message != d.original.message
|| d.function.as_ref().is_some_and(|f| !function_valid(f))
}) {
return Err(Reason::InconsistentEvidence);
}
Ok(())
}
fn target<'a>(
observation: &'a Observation,
function: &Function,
) -> Result<&'a FunctionObservation, Reason> {
observation
.functions
.iter()
.find(|f| &f.identity == function)
.ok_or(Reason::AmbiguousFunction)
}
fn effort(check: &super::Check) -> sail_ledger::Effort {
sail_ledger::Effort {
limit: check.rlimit_limit,
used: check.rlimit_used,
reason: check.reason_unknown.clone(),
}
}
fn headroom(function: &FunctionObservation) -> Result<(), Reason> {
if function.unstable {
return Err(Reason::BaselineUnstable);
}
if !function.verified {
return Err(Reason::BaselineFailed);
}
if !function.checks_complete {
return Err(Reason::BaselineHeadroomMissing);
}
for check in &function.checks {
if check.failure != Failure::None || effort(check).exhausted() {
return Err(Reason::BaselineFailed);
}
let (Some(used), Some(limit)) = (check.rlimit_used, check.rlimit_limit) else {
return Err(Reason::BaselineHeadroomMissing);
};
if check.source.is_none() || check.answer != Some(AnswerName::Unsat) {
return Err(Reason::BaselineHeadroomMissing);
}
if limit > 0 && used > limit / 2 {
return Err(Reason::BaselineNearLimit);
}
}
Ok(())
}
fn detection(function: &FunctionObservation) -> Option<Detection> {
function
.checks
.iter()
.any(|c| c.failure == Failure::Verifier && !effort(c).exhausted())
.then(|| {
if function.checks.iter().any(|c| {
c.failure == Failure::Verifier
&& !effort(c).exhausted()
&& c.answer == Some(AnswerName::Sat)
&& c.source.is_some()
}) {
Detection::CounterexampleConfirmed
} else {
Detection::VerifierRejected
}
})
}
type Message<'a> = (Option<&'a Function>, &'a str, Option<&'a str>, &'a str);
fn messages(diagnostics: &[Diagnostic]) -> Vec<Message<'_>> {
let mut messages: Vec<_> = diagnostics
.iter()
.map(|d| {
(
d.function.as_ref(),
d.level.as_str(),
d.code.as_deref(),
d.message.as_str(),
)
})
.collect();
messages.sort_unstable();
messages
}
fn stable(runs: &[&Observation], function: &Function) -> bool {
let Some(first) = runs.first() else {
return false;
};
let verdicts = |r: &Observation| -> BTreeMap<Function, bool> {
r.functions
.iter()
.map(|f| (f.identity.clone(), f.verified))
.collect()
};
runs.iter().skip(1).all(|run| {
verdicts(first) == verdicts(run)
&& messages(&first.diagnostics) == messages(&run.diagnostics)
&& target(first, function).ok().and_then(detection)
== target(run, function).ok().and_then(detection)
&& first.unviable == run.unviable
})
}
fn complete(runs: &[&Observation]) -> bool {
runs.iter()
.all(|r| r.completed && (r.unviable || r.functions_complete))
}
fn failures(observation: &Observation) -> BTreeSet<&Function> {
observation
.functions
.iter()
.filter(|f| !f.verified)
.map(|f| &f.identity)
.collect()
}
fn outcome(
run: &Observation,
baseline: &Observation,
function: &Function,
scope: Scope,
) -> Result<Outcome, Reason> {
if run.unviable {
return Ok(Outcome::Unviable);
}
let observed = target(run, function)?;
if observed.unstable {
return Err(Reason::Unstable);
}
if observed.verified {
if observed.checks.iter().any(|c| c.failure != Failure::None) {
return Err(Reason::InconsistentEvidence);
}
if !failures(run).is_subset(&failures(baseline)) {
return Err(Reason::CollateralFailure);
}
return Ok(if scope == Scope::Full {
Outcome::Survived
} else {
Outcome::SurvivedScoped
});
}
if !run
.diagnostics
.iter()
.any(|d| d.function.as_ref() == Some(function) && d.level == "error")
{
return Err(Reason::DiagnosticUnattributed);
}
if observed
.checks
.iter()
.any(|c| c.failure == Failure::Verifier && c.answer == Some(AnswerName::Unsat))
{
return Err(Reason::InconsistentEvidence);
}
let evidence = detection(observed).ok_or_else(|| {
if !observed.checks.is_empty()
&& observed.checks.iter().all(|check| {
matches!(
check.failure,
Failure::ResourceLimit | Failure::Timeout | Failure::Cancelled
) || effort(check).exhausted()
})
{
Reason::ResourceOnly
} else {
Reason::FailureUnattributed
}
})?;
Ok(Outcome::Rejected(evidence))
}
fn group(observations: &[Observation], scope: Scope) -> Vec<&Observation> {
observations
.iter()
.filter(|run| run.scope == scope)
.collect()
}
fn baseline_runs(
observations: &[Observation],
function: &Function,
required: usize,
) -> Result<(), Reason> {
let baseline = group(observations, Scope::Full);
let baseline_scoped = group(observations, Scope::Scoped);
if baseline.len() < required {
return Err(Reason::BaselineMissing);
}
for runs in [&baseline, &baseline_scoped] {
if runs.is_empty() {
continue;
}
if runs.len() < required {
return Err(Reason::BaselineMissing);
}
if !complete(runs) || runs.iter().any(|r| r.unviable) {
return Err(Reason::BaselineFailed);
}
if !stable(runs, function) {
return Err(Reason::BaselineUnstable);
}
for run in runs {
headroom(target(run, function)?)?;
}
}
Ok(())
}
fn baseline_for(observations: &[Observation], scope: Scope) -> Result<&Observation, Reason> {
observations
.iter()
.find(|run| run.scope == scope)
.or_else(|| observations.iter().find(|run| run.scope == Scope::Full))
.ok_or(Reason::BaselineMissing)
}
fn attempt_runs<'a>(
observations: &'a [Observation],
function: &Function,
required: usize,
) -> Result<(Vec<&'a Observation>, Scope), Reason> {
let full = group(observations, Scope::Full);
let scoped = group(observations, Scope::Scoped);
for runs in [&full, &scoped] {
if runs.is_empty() {
continue;
}
if !complete(runs) {
return Err(Reason::Incomplete);
}
if runs.len() < required {
return Err(Reason::FullConfirmationMissing);
}
if !stable(runs, function) {
return Err(Reason::Unstable);
}
for run in runs {
if !run.unviable && target(run, function)?.unstable {
return Err(Reason::Unstable);
}
}
}
if let (Some(full), Some(scoped)) = (full.first(), scoped.first()) {
let signature = |run: &Observation| -> Result<_, Reason> {
Ok((
run.unviable,
if run.unviable {
None
} else {
let observed = target(run, function)?;
Some((observed.verified, detection(observed)))
},
))
};
if signature(full)? != signature(scoped)? {
return Err(Reason::Unstable);
}
}
if full.len() >= required {
Ok((full, Scope::Full))
} else if full.is_empty() && scoped.len() >= required {
Ok((scoped, Scope::Scoped))
} else {
Err(Reason::FullConfirmationMissing)
}
}
fn checked(input: &Classification) -> Result<Outcome, Reason> {
let function = input
.mutation
.function
.as_ref()
.ok_or(Reason::AmbiguousFunction)?;
if !function_valid(function)
|| !context_valid(&input.context)
|| !digest(&input.mutation.source_sha256)
|| input.mutation.source_sha256 == input.context.baseline_sha256
|| input.mutation.id.is_empty()
|| input.mutation.before == input.mutation.after
|| input.mutation.start > input.mutation.end
|| input.mutation.end.saturating_sub(input.mutation.start)
!= u64::try_from(input.mutation.before.len()).unwrap_or(u64::MAX)
|| input.mutation.file.as_os_str().is_empty()
|| input
.mutation
.file
.components()
.any(|p| !matches!(p, std::path::Component::Normal(_)))
|| !(MIN_REPEATS..=20).contains(&input.repeats)
{
return Err(Reason::IdentityMismatch);
}
for run in &input.baseline {
valid_observation(run, &input.context, &input.context.baseline_sha256)?;
}
for run in &input.attempts {
valid_observation(run, &input.context, &input.mutation.source_sha256)?;
}
let required = usize::try_from(input.repeats).map_err(|_| Reason::IdentityMismatch)?;
baseline_runs(&input.baseline, function, required)?;
let (attempts, scope) = attempt_runs(&input.attempts, function, required)?;
for mode in [Scope::Scoped, Scope::Full] {
let base = baseline_for(&input.baseline, mode)?;
let mut expected = None;
for run in input.attempts.iter().filter(|run| run.scope == mode) {
let result = outcome(run, base, function, mode)?;
if expected.as_ref().is_some_and(|prior| prior != &result) {
return Err(Reason::Unstable);
}
expected = Some(result);
}
}
let first = attempts.first().ok_or(Reason::Incomplete)?;
let base = baseline_for(&input.baseline, scope)?;
let expected = outcome(first, base, function, scope)?;
if scope == Scope::Scoped && matches!(expected, Outcome::Rejected(_)) {
return Err(Reason::FullConfirmationMissing);
}
for run in attempts.iter().skip(1) {
if outcome(run, base, function, scope)? != expected {
return Err(Reason::Unstable);
}
}
Ok(expected)
}
#[must_use]
pub fn summarize<'a>(
outcomes: impl IntoIterator<Item = &'a Outcome>,
omitted: u64,
cancelled: u64,
) -> Summary {
let mut summary = Summary {
omitted,
cancelled,
..Summary::default()
};
for outcome in outcomes {
match outcome {
Outcome::Rejected(evidence) => {
summary.rejected = summary.rejected.saturating_add(1);
match evidence {
Detection::CounterexampleConfirmed => {
summary.counterexample_confirmed =
summary.counterexample_confirmed.saturating_add(1);
}
Detection::VerifierRejected => {
summary.verifier_rejected = summary.verifier_rejected.saturating_add(1);
}
}
}
Outcome::Survived => summary.survived = summary.survived.saturating_add(1),
Outcome::SurvivedScoped => {
summary.survived_scoped = summary.survived_scoped.saturating_add(1);
}
Outcome::Inconclusive(reason) => {
let n = summary.inconclusive.entry(*reason).or_default();
*n = n.saturating_add(1);
}
Outcome::Unviable => summary.unviable = summary.unviable.saturating_add(1),
Outcome::Unsupported(_) => summary.unsupported = summary.unsupported.saturating_add(1),
}
}
let denominator = summary
.rejected
.saturating_add(summary.survived)
.saturating_add(summary.survived_scoped);
if denominator != 0 {
#[allow(
clippy::cast_precision_loss,
reason = "bounded inventory counts are represented as a display ratio, not evidence"
)]
{
summary.score = Some(summary.rejected as f64 / denominator as f64);
}
}
summary
}