diff --git a/Cargo.lock b/Cargo.lock index 44f6caff..be287d87 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1050,6 +1050,14 @@ dependencies = [ "thiserror", ] +[[package]] +name = "o7-closure-classifier" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "o7-harness-policy" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 9a079fd1..086f13f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ members = [ "crates/o7-worker", "crates/o7-worktree", "crates/o7-verifier", + "crates/o7-closure-classifier", "crates/o7-run", "crates/o7-sandbox-protocol", "crates/o7-harness-policy", diff --git a/crates/o7-closure-classifier/Cargo.toml b/crates/o7-closure-classifier/Cargo.toml new file mode 100644 index 00000000..65c3c6c4 --- /dev/null +++ b/crates/o7-closure-classifier/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "o7-closure-classifier" +version = "0.1.0" +edition = "2021" +description = "Pure closure classifier: explicit observations in, deterministic closure predicate out. No network, no GitHub API, no local Git authority, no attestation — those layers live outside and above this crate." +publish = false + +[lib] +name = "o7_closure_classifier" +path = "src/lib.rs" + +[dependencies] +serde = { version = "1", features = ["derive"] } +serde_json = "1" + +# Mirror the workspace discipline: no unsafe, no panics-by-default. +[lints.rust] +unsafe_code = "forbid" +unreachable_pub = "deny" +rust_2018_idioms = { level = "deny", priority = -1 } + +[lints.clippy] +unwrap_used = "deny" +expect_used = "deny" +panic = "deny" +dbg_macro = "deny" +todo = "deny" +unimplemented = "deny" +indexing_slicing = "deny" diff --git a/crates/o7-closure-classifier/src/lib.rs b/crates/o7-closure-classifier/src/lib.rs new file mode 100644 index 00000000..09a7119b --- /dev/null +++ b/crates/o7-closure-classifier/src/lib.rs @@ -0,0 +1,557 @@ +//! Pure closure classifier (issue #147, the step after the frozen Step 0B +//! fixtures and their integrity validator). +//! +//! ```text +//! explicit observations -> classifier -> closure predicate +//! ``` +//! +//! WHAT THIS CRATE IS NOT. It performs no acquisition. It opens no socket, calls +//! no GitHub API, runs no `git`, reads no `.git/`, consults no branch or working +//! tree, and produces no attestation. A later acquisition adapter turns API +//! responses into a [`ClassifierInput`]; a later envelope step wraps the output. +//! Both are outside this crate by design — the classifier must be callable +//! entirely in memory. +//! +//! WHAT THE PREDICATE CLAIMS. Only this: every observation the policy explicitly +//! required has the recorded state, for this immutable subject, under the +//! declared assumptions. `PASS` does NOT mean the observation set is complete, +//! the pull request is correct, review was exhaustive, no unknown defect exists, +//! or that anything may be merged. The policy carries +//! `completeness_claimed = false` and the required set is enumerated in the +//! output so a later reader can see exactly what was asked. +//! +//! ACQUISITION STATUS IS AN INPUT, NOT AN INFERENCE. This crate never decides +//! that an observation is missing by finding an absent key, an empty array, a +//! null or a parse error. The caller states what happened via [`Acquisition`], +//! which is what keeps the two failure modes apart: +//! +//! ```text +//! OWED nobody produced the required evidence +//! CANNOT_CHECK an attempt to obtain it did not yield a trustworthy answer +//! ``` +//! +//! NO PROSE ORACLE. Nothing here reads English. A defect claim reaches the +//! classifier as a structured [`FalsificationFact`] carrying provenance and a +//! [`Verification`] status; extracting such facts from vendor responses is the +//! acquisition layer's job. Note that such a record is NOT necessarily an +//! established defect — a `Claimed` one is precisely a counterexample whose +//! verification is still owed — so this crate decides what each record +//! contributes, never whether some wording amounts to one. + +// Mirror the workspace discipline. +#![forbid(unsafe_code)] + +use serde::Serialize; +use std::collections::BTreeMap; + +/// The five states of #147, kept distinct. Neighbours are never merged. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +pub enum State { + /// Admissible positive evidence exists for the frozen subject. + Pass, + /// An admissible established defect exists for the frozen subject. + Finding, + /// Required evidence does not yet exist; nobody has produced it. + Owed, + /// An attempt to obtain a required observation did not yield a trustworthy + /// answer. This is NOT `Owed`: somebody tried and the attempt failed. + CannotCheck, + /// The pull request head differs from the expected subject, before or after + /// evaluation. + Stale, +} + +impl State { + /// Headline precedence, presentation only: + /// `STALE > FINDING > CANNOT_CHECK > OWED > PASS`. + fn rank(self) -> u8 { + match self { + State::Pass => 0, + State::Owed => 1, + State::CannotCheck => 2, + State::Finding => 3, + State::Stale => 4, + } + } +} + +/// What happened when the caller tried to obtain one observation. The classifier +/// never infers any of these from the shape of missing data. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Acquisition { + /// The observation was obtained. + Available(T), + /// The acquisition succeeded and established that nobody has produced the + /// required evidence — an authoritative absence, not a failure. + NotProduced, + /// The producer was rate-limited, so no verdict is available yet. Still + /// nobody's verdict; never a pass and never a failure. + RateLimited, + /// The attempt itself failed: transport, tool, permission, malformed + /// response. A failed query that yielded no values is not an empty result. + Failed { reason: String }, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum CheckConclusion { + Success, + Failure, +} + +/// A CI check as observed, with the commit it is bound to. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct CheckEvidence { + pub stable_id: String, + pub head_sha: String, + pub conclusion: CheckConclusion, +} + +/// A submitted review object as observed. `commit_id` is its immutable binding; +/// `carries_finding` is supplied by the acquisition layer, never inferred here +/// from body text. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ReviewEvidence { + pub stable_id: String, + pub commit_id: String, + pub author: String, + pub carries_finding: bool, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum ObservationInput { + Check(Acquisition), + Review(Acquisition), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +#[serde(rename_all = "kebab-case")] +pub enum SourceKind { + GithubActionsCheck, + SubmittedReview, + ReviewComment, + IssueComment, +} + +/// How far a falsification-channel record has actually got. #147 defines +/// `FINDING` as an admissible VERIFIED defect, and the wide falsification +/// surface only means "a checkable counterexample may arrive from a surface that +/// could never carry a verdict" — it does not mean an unverified claim is a +/// defect. So this gates, and each value maps to exactly one state. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Verification { + /// Verified: the defect was reproduced. -> `FINDING` + Reproduced, + /// A concrete claim exists and its verification is still owed. -> `OWED` + Claimed, + /// Verification was attempted and did not yield a trustworthy answer. + /// -> `CANNOT_CHECK`. The reason is carried so the predicate can say WHY, + /// not merely that something went wrong. + Failed { reason: String }, +} + +/// One record on the falsification channel — NOT necessarily an established +/// defect: see [`Verification`], which decides what this record contributes. +/// Carries no prose. The classifier never reads text to decide whether some +/// wording amounts to a defect claim. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct FalsificationFact { + pub source_kind: SourceKind, + pub stable_id: String, + /// Present only where the surface has an immutable commit binding. `None` is + /// admissible — an issue comment has no commit binding, and requiring one + /// would silently close the wide surface. But a record explicitly bound to + /// ANOTHER commit is rejected as invalid input rather than allowed to + /// contaminate this subject: a wide surface means more KINDS of surface, not + /// defects of any commit in the universe. + pub subject_sha: Option, + pub author: String, + pub verification: Verification, +} + +/// Refusals to classify. A malformed input never receives a well-formed closure +/// predicate — that is precisely how an inference would sneak in disguised as a +/// domain fact. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum ClassifierError { + /// A policy-required observation had no entry at all. We cannot know whether + /// it was not produced, or lost by a broken adapter, or never fetched. + /// Naming that unknown `OWED` would convert a construction failure into a + /// domain fact, which is the whole thing this design refuses to do. + MissingRequiredObservation { id: String }, + /// A falsification record explicitly bound to a different commit. Evidence + /// is never combined across SHAs. + FalsificationSubjectMismatch { + stable_id: String, + subject_sha: String, + expected_sha: String, + }, +} + +impl std::fmt::Display for ClassifierError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + ClassifierError::MissingRequiredObservation { id } => write!( + f, + "required observation `{id}` has no entry: acquisition state is carried by the \ + caller, never inferred from absence" + ), + ClassifierError::FalsificationSubjectMismatch { + stable_id, + subject_sha, + expected_sha, + } => write!( + f, + "falsification `{stable_id}` is bound to {subject_sha}, not to the subject \ + {expected_sha}" + ), + } + } +} + +impl std::error::Error for ClassifierError {} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Subject { + pub repository: String, + pub pull_request: u64, + /// The frozen subject every admissible positive observation must bind to. + pub expected_sha: String, + pub head_before: String, + pub head_after: String, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Policy { + pub id: String, + /// Authoritative enumeration. Output observation order follows it. + pub required_observations: Vec, + // NOTE: there is deliberately no `completeness_claimed` field. V0 must + // always disclaim completeness, and a caller-settable bool documented as + // "always false" is a false claim waiting to be serialized. The output + // materializes `completenessClaimed: false` unconditionally, so the + // forbidden state cannot be constructed at all. +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ClassifierInput { + pub subject: Subject, + pub policy: Policy, + pub observations: BTreeMap, + pub falsifications: Vec, + /// Debt tracked independently of reviewer cleanliness. A clean reviewer does + /// not discharge it. + pub known_debts: Vec, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +pub struct SourceOut { + pub kind: SourceKind, + pub stable_id: String, +} + +/// How the observation was obtained, kept separate from the state it produced. +/// `NOT_PRODUCED` and `RATE_LIMITED` both yield `OWED`, and collapsing them into +/// the state alone would destroy two distinct observed facts — the same +/// destructive collapse the headline rule forbids, one level down. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +pub enum AcquisitionStatus { + Available, + NotProduced, + RateLimited, + Failed, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct AcquisitionOut { + pub status: AcquisitionStatus, + /// Always present for `FAILED`: #147 requires the predicate to preserve + /// enough provenance to re-evaluate later, and "it failed" alone cannot be + /// re-evaluated. + #[serde(skip_serializing_if = "Option::is_none")] + pub reason: Option, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +pub enum VerificationStatus { + Reproduced, + Claimed, + Failed, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct VerificationOut { + pub status: VerificationStatus, + #[serde(skip_serializing_if = "Option::is_none")] + pub reason: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +pub struct ObservationOut { + pub id: String, + pub state: State, + /// The acquisition outcome that produced `state`. Distinct from it on + /// purpose: several outcomes map to one state. + pub acquisition: AcquisitionOut, + #[serde(skip_serializing_if = "Option::is_none")] + pub source: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct PolicyOut { + pub id: String, + pub required_observations: Vec, + pub completeness_claimed: bool, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct FalsificationOut { + pub source: SourceOut, + #[serde(skip_serializing_if = "Option::is_none")] + pub subject_sha: Option, + pub author: String, + pub verification: VerificationOut, + /// What this record contributes, so a headline of `FINDING`, `OWED` or + /// `CANNOT_CHECK` driven by the falsification channel is explainable from + /// the predicate itself rather than only from this crate's source. + pub state: State, +} + +/// The deterministic closure predicate. No timestamp, no generated id, no +/// iteration-order nondeterminism: the same semantic input serializes to the +/// same bytes. +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Predicate { + pub schema_version: u32, + pub repository: String, + pub pull_request: u64, + pub git_commit: String, + pub policy: PolicyOut, + pub observations: Vec, + /// Falsification-channel records, each with its own state. NOT a list of + /// established defects: a `CLAIMED` record is an unverified counterexample + /// and contributes `OWED`. Kept as their own list because such a record + /// belongs to no required observation, and attaching it to one would + /// misreport which observation produced the state. + pub falsifications: Vec, + /// True when the head moved before or after evaluation. Kept beside the + /// per-observation vector rather than overwriting it, so a stale snapshot + /// still shows what each observation said about the frozen subject. + pub subject_stale: bool, + pub headline: State, + pub known_debts: Vec, +} + +impl Predicate { + /// Deterministic serialization. Provided as a convenience; the classifier + /// core is fully testable without touching a filesystem. + pub fn to_json(&self) -> Result { + serde_json::to_string_pretty(self) + } +} + +/// A check is positive evidence only when it is bound to the frozen subject. +/// A check bound elsewhere is not evidence about this subject at all, so it +/// leaves the observation owed rather than combining across commits. +fn classify_check( + acq: &Acquisition, + expected_sha: &str, +) -> (State, AcquisitionOut, Option) { + match acq { + Acquisition::Available(check) => { + let source = Some(SourceOut { + kind: SourceKind::GithubActionsCheck, + stable_id: check.stable_id.clone(), + }); + let state = if check.head_sha != expected_sha { + State::Owed + } else { + match check.conclusion { + CheckConclusion::Success => State::Pass, + CheckConclusion::Failure => State::Finding, + } + }; + (state, available(), source) + } + Acquisition::NotProduced => (State::Owed, status(AcquisitionStatus::NotProduced), None), + Acquisition::RateLimited => (State::Owed, status(AcquisitionStatus::RateLimited), None), + Acquisition::Failed { reason } => (State::CannotCheck, failed(reason), None), + } +} + +fn available() -> AcquisitionOut { + AcquisitionOut { + status: AcquisitionStatus::Available, + reason: None, + } +} + +fn status(status: AcquisitionStatus) -> AcquisitionOut { + AcquisitionOut { + status, + reason: None, + } +} + +fn failed(reason: &str) -> AcquisitionOut { + AcquisitionOut { + status: AcquisitionStatus::Failed, + reason: Some(reason.to_owned()), + } +} + +/// Positive reviewer evidence is strict: the submitted review's immutable +/// commit binding must equal the frozen subject. A wrong-SHA review yields no +/// verdict here — neither a pass nor a finding. +fn classify_review( + acq: &Acquisition, + expected_sha: &str, +) -> (State, AcquisitionOut, Option) { + match acq { + Acquisition::Available(review) => { + let source = Some(SourceOut { + kind: SourceKind::SubmittedReview, + stable_id: review.stable_id.clone(), + }); + let state = if review.commit_id != expected_sha { + State::Owed + } else if review.carries_finding { + State::Finding + } else { + State::Pass + }; + (state, available(), source) + } + Acquisition::NotProduced => (State::Owed, status(AcquisitionStatus::NotProduced), None), + Acquisition::RateLimited => (State::Owed, status(AcquisitionStatus::RateLimited), None), + Acquisition::Failed { reason } => (State::CannotCheck, failed(reason), None), + } +} + +/// Classify one snapshot. Every invocation is independent: no memory of a +/// previous headline, so a finding that disappears does not linger. +pub fn classify(input: &ClassifierInput) -> Result { + let expected = input.subject.expected_sha.as_str(); + + // Refuse malformed input before deriving anything. A predicate produced from + // an input we could not read correctly is worse than no predicate: it looks + // exactly like one we could. + for id in &input.policy.required_observations { + if !input.observations.contains_key(id) { + return Err(ClassifierError::MissingRequiredObservation { id: id.clone() }); + } + } + for f in &input.falsifications { + if let Some(sha) = &f.subject_sha { + if sha != expected { + return Err(ClassifierError::FalsificationSubjectMismatch { + stable_id: f.stable_id.clone(), + subject_sha: sha.clone(), + expected_sha: expected.to_owned(), + }); + } + } + } + + // The head moving invalidates the SNAPSHOT, not the individual observations, + // which remain statements about the frozen subject. Recorded alongside the + // vector so nothing is overwritten. + let subject_stale = + input.subject.head_before != expected || input.subject.head_after != expected; + + let observations: Vec = input + .policy + .required_observations + .iter() + .map(|id| { + // Presence was established above, so `None` here is unreachable — + // and is still not silently turned into a state. + let (state, acquisition, source) = match input.observations.get(id) { + None => ( + State::CannotCheck, + failed("required observation absent from the input map"), + None, + ), + Some(ObservationInput::Check(acq)) => classify_check(acq, expected), + Some(ObservationInput::Review(acq)) => classify_review(acq, expected), + }; + ObservationOut { + id: id.clone(), + state, + acquisition, + source, + } + }) + .collect(); + + let falsifications: Vec = input + .falsifications + .iter() + .map(|f| FalsificationOut { + source: SourceOut { + kind: f.source_kind, + stable_id: f.stable_id.clone(), + }, + subject_sha: f.subject_sha.clone(), + author: f.author.clone(), + verification: match &f.verification { + Verification::Reproduced => VerificationOut { + status: VerificationStatus::Reproduced, + reason: None, + }, + Verification::Claimed => VerificationOut { + status: VerificationStatus::Claimed, + reason: None, + }, + Verification::Failed { reason } => VerificationOut { + status: VerificationStatus::Failed, + reason: Some(reason.clone()), + }, + }, + state: match f.verification { + Verification::Reproduced => State::Finding, + Verification::Claimed => State::Owed, + Verification::Failed { .. } => State::CannotCheck, + }, + }) + .collect(); + + // Headline is derived presentation over everything recorded: the observation + // vector, plus falsification (which has no required-observation slot of its + // own), plus subject staleness. The vector itself is never rewritten. + let mut headline = observations + .iter() + .map(|o| o.state) + .chain(falsifications.iter().map(|f| f.state)) + .max_by_key(|s| s.rank()) + .unwrap_or(State::Pass); + if subject_stale { + headline = State::Stale; + } + + Ok(Predicate { + schema_version: 1, + repository: input.subject.repository.clone(), + pull_request: input.subject.pull_request, + git_commit: input.subject.expected_sha.clone(), + policy: PolicyOut { + id: input.policy.id.clone(), + required_observations: input.policy.required_observations.clone(), + // Unconditional: V0 never claims completeness, and the input has no + // field through which a caller could claim it. + completeness_claimed: false, + }, + observations, + falsifications, + subject_stale, + headline, + known_debts: input.known_debts.clone(), + }) +} diff --git a/crates/o7-closure-classifier/tests/acceptance.rs b/crates/o7-closure-classifier/tests/acceptance.rs new file mode 100644 index 00000000..4c6080eb --- /dev/null +++ b/crates/o7-closure-classifier/tests/acceptance.rs @@ -0,0 +1,579 @@ +//! Acceptance cases from issue #147 that apply to the pure classifier. +//! +//! These use CONSTRUCTED typed inputs, not the frozen Step 0B fixtures. Where a +//! case has no historical fixture witness — `OWED`, `CANNOT_CHECK` — that is +//! stated on the test itself: a logic-level unit witness is not a claim that the +//! frozen corpus covers the path. + +// Justification for the restriction-lint allowance, per the precedent in +// `tests/a0_candidate_state_e2e.rs`: every panic path below is this test's own +// assertion over inputs it constructed itself. +#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] + +use o7_closure_classifier::{ + classify, Acquisition, AcquisitionStatus, CheckConclusion, CheckEvidence, ClassifierError, + ClassifierInput, FalsificationFact, ObservationInput, Policy, ReviewEvidence, SourceKind, + State, Subject, Verification, VerificationStatus, +}; +use std::collections::BTreeMap; + +const HEAD: &str = "ed7969c4636362bcfc248bcb9957e140ca899f8c"; +const OTHER: &str = "c08abce9dd0356facae400db1950cd9644976114"; + +const CI_IDS: &[&str] = &[ + "ci/worker-gate", + "ci/dependency-policy", + "ci/dependency-advisories", + "ci/worktree-verifier", +]; + +fn policy() -> Policy { + let mut required: Vec = CI_IDS.iter().map(|s| (*s).to_owned()).collect(); + required.push("review/codex".to_owned()); + required.push("review/coderabbit".to_owned()); + Policy { + id: "007/closure/v1".to_owned(), + required_observations: required, + } +} + +fn subject(expected: &str, before: &str, after: &str) -> Subject { + Subject { + repository: "PhysShell/007".to_owned(), + pull_request: 145, + expected_sha: expected.to_owned(), + head_before: before.to_owned(), + head_after: after.to_owned(), + } +} + +fn passing_check(sha: &str, name: &str) -> ObservationInput { + ObservationInput::Check(Acquisition::Available(CheckEvidence { + stable_id: format!("check:{name}"), + head_sha: sha.to_owned(), + conclusion: CheckConclusion::Success, + })) +} + +fn clean_review(sha: &str, author: &str) -> ObservationInput { + ObservationInput::Review(Acquisition::Available(ReviewEvidence { + stable_id: format!("review:{author}"), + commit_id: sha.to_owned(), + author: author.to_owned(), + carries_finding: false, + })) +} + +/// Every CI check passing at `sha`, both reviewers clean at `sha`. +fn all_green(sha: &str) -> BTreeMap { + let mut m = BTreeMap::new(); + for name in CI_IDS { + m.insert((*name).to_owned(), passing_check(sha, name)); + } + m.insert("review/codex".to_owned(), clean_review(sha, "codex")); + m.insert( + "review/coderabbit".to_owned(), + clean_review(sha, "coderabbit"), + ); + m +} + +fn input(subject: Subject, observations: BTreeMap) -> ClassifierInput { + ClassifierInput { + subject, + policy: policy(), + observations, + falsifications: Vec::new(), + known_debts: Vec::new(), + } +} + +/// `classify` now refuses malformed input, so every well-formed case unwraps. +fn ok(inp: &ClassifierInput) -> o7_closure_classifier::Predicate { + classify(inp).expect("this input is well formed") +} + +fn obs_of<'a>( + p: &'a o7_closure_classifier::Predicate, + id: &str, +) -> &'a o7_closure_classifier::ObservationOut { + p.observations + .iter() + .find(|o| o.id == id) + .unwrap_or_else(|| panic!("observation {id} missing from the predicate")) +} + +fn state_of(p: &o7_closure_classifier::Predicate, id: &str) -> State { + p.observations + .iter() + .find(|o| o.id == id) + .map(|o| o.state) + .unwrap_or_else(|| panic!("observation {id} missing from the predicate")) +} + +// ------------------------------------------------------------------ 1, 2, 6 -- + +#[test] +fn case_01_initial_head_differs_from_expected_is_stale() { + let p = ok(&input(subject(HEAD, OTHER, HEAD), all_green(HEAD))); + assert_eq!(p.headline, State::Stale); + assert!(p.subject_stale); +} + +#[test] +fn case_02_final_head_differs_from_expected_is_stale() { + let p = ok(&input(subject(HEAD, HEAD, OTHER), all_green(HEAD))); + assert_eq!(p.headline, State::Stale); + assert!(p.subject_stale); +} + +#[test] +fn case_02b_stale_snapshot_still_preserves_the_observation_vector() { + // The head moving must not erase what each observation said about the frozen + // subject; the headline is derived presentation, not a replacement. + let p = ok(&input(subject(HEAD, HEAD, OTHER), all_green(HEAD))); + assert_eq!(p.headline, State::Stale); + assert_eq!(state_of(&p, "ci/worker-gate"), State::Pass); + assert_eq!(state_of(&p, "review/codex"), State::Pass); +} + +// --------------------------------------------------------------------- 3, 11 -- + +#[test] +fn case_03_clean_review_on_the_wrong_sha_is_not_positive_evidence() { + let mut obs = all_green(HEAD); + obs.insert("review/codex".to_owned(), clean_review(OTHER, "codex")); + let p = ok(&input(subject(HEAD, HEAD, HEAD), obs)); + assert_eq!( + state_of(&p, "review/codex"), + State::Owed, + "a review bound to another commit produces no verdict for this subject" + ); + assert_ne!(p.headline, State::Pass); +} + +#[test] +fn case_11_ci_and_reviews_from_different_shas_cannot_combine() { + // CI genuinely at the subject, reviews genuinely elsewhere. Neither rescues + // the other into a positive snapshot. + let mut obs = all_green(HEAD); + obs.insert("review/codex".to_owned(), clean_review(OTHER, "codex")); + obs.insert( + "review/coderabbit".to_owned(), + clean_review(OTHER, "coderabbit"), + ); + let p = ok(&input(subject(HEAD, HEAD, HEAD), obs)); + for id in ["review/codex", "review/coderabbit"] { + assert_eq!(state_of(&p, id), State::Owed); + } + for id in CI_IDS { + assert_eq!(state_of(&p, id), State::Pass); + } + assert_eq!(p.headline, State::Owed); +} + +#[test] +fn case_11b_a_check_bound_to_another_sha_is_not_evidence_here() { + let mut obs = all_green(HEAD); + obs.insert( + "ci/worker-gate".to_owned(), + passing_check(OTHER, "worker-gate"), + ); + let p = ok(&input(subject(HEAD, HEAD, HEAD), obs)); + assert_eq!(state_of(&p, "ci/worker-gate"), State::Owed); +} + +// ------------------------------------------------------------------------ 4 -- + +#[test] +fn case_04_a_mutable_comment_cannot_be_positive_verdict_evidence() { + // BY CONSTRUCTION, and this test records that rather than pretending to + // discover it: the ONLY channel into a `review/*` observation is + // `ReviewEvidence`, which carries an immutable `commit_id`. There is no + // variant through which an issue comment, reaction, walkthrough or command + // reply could arrive as positive evidence. A reviewer that posted only a + // clean-sounding comment therefore leaves its required observation OWED. + let mut obs = all_green(HEAD); + obs.insert( + "review/coderabbit".to_owned(), + ObservationInput::Review(Acquisition::NotProduced), + ); + let p = ok(&input(subject(HEAD, HEAD, HEAD), obs)); + assert_eq!(state_of(&p, "review/coderabbit"), State::Owed); + assert_eq!(p.headline, State::Owed); +} + +// --------------------------------------------------------------------- 5, 12 -- + +#[test] +fn case_05_structured_falsification_from_a_non_verdict_surface_is_preserved() { + // Everything a policy asks for is green; a falsification arrives from a + // surface that could never carry a verdict. It must still be able to produce + // a finding — the asymmetry #147 requires. + let mut inp = input(subject(HEAD, HEAD, HEAD), all_green(HEAD)); + inp.falsifications.push(FalsificationFact { + source_kind: SourceKind::IssueComment, + stable_id: "issuecomment-5305700001".to_owned(), + subject_sha: None, + author: "coderabbitai[bot]".to_owned(), + verification: Verification::Reproduced, + }); + let p = ok(&inp); + assert_eq!(p.headline, State::Finding); + assert_eq!(p.falsifications.len(), 1); + // The positive observations are NOT rewritten by it: both facts survive. + assert_eq!(state_of(&p, "review/codex"), State::Pass); +} + +#[test] +fn case_05b_falsification_without_a_commit_binding_is_still_admissible() { + // Positive evidence is strict about SHA binding; the falsification channel + // is not. An issue comment has no commit binding at all, and requiring one + // would silently close the wide surface. + let mut inp = input(subject(HEAD, HEAD, HEAD), all_green(HEAD)); + inp.falsifications.push(FalsificationFact { + source_kind: SourceKind::IssueComment, + stable_id: "issuecomment-1".to_owned(), + subject_sha: None, + author: "someone[bot]".to_owned(), + verification: Verification::Reproduced, + }); + assert_eq!(ok(&inp).headline, State::Finding); +} + +#[test] +fn case_05c_a_claimed_falsification_is_owed_not_finding() { + // #147 defines FINDING as an admissible VERIFIED defect. A concrete claim + // whose verification is still owed is exactly that: owed. Treating it as a + // finding would assert a defect nobody has established. + let mut inp = input(subject(HEAD, HEAD, HEAD), all_green(HEAD)); + inp.falsifications.push(FalsificationFact { + source_kind: SourceKind::IssueComment, + stable_id: "issuecomment-2".to_owned(), + subject_sha: None, + author: "someone[bot]".to_owned(), + verification: Verification::Claimed, + }); + let p = ok(&inp); + let record = p.falsifications.first().expect("one record"); + assert_eq!(record.state, State::Owed); + assert_eq!(p.headline, State::Owed); + assert_ne!(p.headline, State::Finding); +} + +#[test] +fn case_05d_failed_verification_of_a_falsification_is_cannot_check() { + // Somebody tried to verify the counterexample and got no trustworthy + // answer. That is not "no defect" and not "defect" — it is the third thing. + let mut inp = input(subject(HEAD, HEAD, HEAD), all_green(HEAD)); + inp.falsifications.push(FalsificationFact { + source_kind: SourceKind::ReviewComment, + stable_id: "discussion-3".to_owned(), + subject_sha: Some(HEAD.to_owned()), + author: "someone[bot]".to_owned(), + verification: Verification::Failed { + reason: "reproducer unavailable".to_owned(), + }, + }); + let p = ok(&inp); + let record = p.falsifications.first().expect("one record"); + assert_eq!(record.state, State::CannotCheck); + assert_eq!(p.headline, State::CannotCheck); +} + +#[test] +fn case_05e_a_falsification_bound_to_another_sha_is_invalid_input() { + // A wide surface means more KINDS of surface, not defects of any commit in + // the universe. Evidence is never combined across SHAs, so this is refused + // rather than quietly contaminating the snapshot. + let mut inp = input(subject(HEAD, HEAD, HEAD), all_green(HEAD)); + inp.falsifications.push(FalsificationFact { + source_kind: SourceKind::ReviewComment, + stable_id: "discussion-4".to_owned(), + subject_sha: Some(OTHER.to_owned()), + author: "someone[bot]".to_owned(), + verification: Verification::Reproduced, + }); + match classify(&inp) { + Err(ClassifierError::FalsificationSubjectMismatch { stable_id, .. }) => { + assert_eq!(stable_id, "discussion-4"); + } + other => panic!("expected a subject-mismatch refusal, got {other:?}"), + } +} + +#[test] +fn case_12_same_vendor_same_sha_conflicting_surfaces_are_not_collapsed() { + // The submitted commit-bound review controls verdict accounting; a claim on + // another surface from the same author is preserved rather than deleted. + let mut obs = all_green(HEAD); + obs.insert( + "review/codex".to_owned(), + ObservationInput::Review(Acquisition::Available(ReviewEvidence { + stable_id: "review:codex".to_owned(), + commit_id: HEAD.to_owned(), + author: "codex[bot]".to_owned(), + carries_finding: true, + })), + ); + let mut inp = input(subject(HEAD, HEAD, HEAD), obs); + inp.falsifications.push(FalsificationFact { + source_kind: SourceKind::ReviewComment, + stable_id: "discussion_r3791453710".to_owned(), + subject_sha: Some(HEAD.to_owned()), + author: "codex[bot]".to_owned(), + verification: Verification::Reproduced, + }); + let p = ok(&inp); + assert_eq!(state_of(&p, "review/codex"), State::Finding); + assert_eq!(p.falsifications.len(), 1); + assert_eq!(p.headline, State::Finding); +} + +// --------------------------------------------------------------------- 6, 7 -- + +#[test] +fn case_06_no_verdict_produced_is_owed_not_pass() { + // LOGIC-LEVEL UNIT WITNESS. The frozen Step 0B corpus has no rate-limit or + // absent-verdict specimen, and this test does not claim otherwise. + for acq in [Acquisition::NotProduced, Acquisition::RateLimited] { + let mut obs = all_green(HEAD); + obs.insert("review/codex".to_owned(), ObservationInput::Review(acq)); + let p = ok(&input(subject(HEAD, HEAD, HEAD), obs)); + assert_eq!(state_of(&p, "review/codex"), State::Owed); + } +} + +#[test] +fn case_07_acquisition_failure_is_cannot_check_not_owed_and_not_empty_success() { + // LOGIC-LEVEL UNIT WITNESS — no Step 0B specimen exists for an API error. + let mut obs = all_green(HEAD); + obs.insert( + "ci/worker-gate".to_owned(), + ObservationInput::Check(Acquisition::Failed { + reason: "503 from the checks endpoint".to_owned(), + }), + ); + let p = ok(&input(subject(HEAD, HEAD, HEAD), obs)); + assert_eq!(state_of(&p, "ci/worker-gate"), State::CannotCheck); + assert_ne!(state_of(&p, "ci/worker-gate"), State::Owed); + assert_ne!(p.headline, State::Pass); +} + +#[test] +fn case_07b_a_missing_required_observation_is_invalid_input_not_owed() { + // We cannot know whether it was never produced, lost by a broken adapter, + // or never fetched. Calling that unknown OWED would turn a construction + // failure into a domain fact — the exact move this design exists to refuse. + let mut obs = all_green(HEAD); + obs.remove("review/coderabbit"); + match classify(&input(subject(HEAD, HEAD, HEAD), obs)) { + Err(ClassifierError::MissingRequiredObservation { id }) => { + assert_eq!(id, "review/coderabbit"); + } + other => panic!("expected a missing-observation refusal, got {other:?}"), + } +} + +// --------------------------------------------------------------------- 8, 9 -- + +#[test] +fn case_08_finding_and_cannot_check_both_survive_headline_is_finding() { + let mut obs = all_green(HEAD); + obs.insert( + "review/codex".to_owned(), + ObservationInput::Review(Acquisition::Available(ReviewEvidence { + stable_id: "review:codex".to_owned(), + commit_id: HEAD.to_owned(), + author: "codex[bot]".to_owned(), + carries_finding: true, + })), + ); + obs.insert( + "ci/worktree-verifier".to_owned(), + ObservationInput::Check(Acquisition::Failed { + reason: "runner vanished".to_owned(), + }), + ); + let p = ok(&input(subject(HEAD, HEAD, HEAD), obs)); + assert_eq!(state_of(&p, "review/codex"), State::Finding); + assert_eq!(state_of(&p, "ci/worktree-verifier"), State::CannotCheck); + assert_eq!(p.headline, State::Finding); +} + +#[test] +fn case_09_a_later_snapshot_without_the_finding_is_cannot_check_not_pass() { + // Each invocation is a fresh snapshot: no memory of a previous headline, and + // equally no inheritance of one. + let mut obs = all_green(HEAD); + obs.insert( + "ci/worktree-verifier".to_owned(), + ObservationInput::Check(Acquisition::Failed { + reason: "runner vanished".to_owned(), + }), + ); + let p = ok(&input(subject(HEAD, HEAD, HEAD), obs)); + assert_eq!(state_of(&p, "ci/worktree-verifier"), State::CannotCheck); + assert_eq!(p.headline, State::CannotCheck); + assert_ne!(p.headline, State::Pass); +} + +// ------------------------------------------------------------- 10, 14, 15 -- + +#[test] +fn case_10_pass_enumerates_the_required_set_and_disclaims_completeness() { + let p = ok(&input(subject(HEAD, HEAD, HEAD), all_green(HEAD))); + assert_eq!(p.headline, State::Pass); + assert_eq!( + p.policy.required_observations, + policy().required_observations + ); + assert!(!p.policy.completeness_claimed); + assert_eq!(p.observations.len(), policy().required_observations.len()); +} + +#[test] +fn case_10b_v0_cannot_emit_a_completeness_claim() { + // Not "we remember to set it false" — there is no input field through which + // `true` could be constructed, so the forbidden state is unreachable. + let p = ok(&input(subject(HEAD, HEAD, HEAD), all_green(HEAD))); + assert!(!p.policy.completeness_claimed); + let json = p.to_json().expect("serializing"); + assert!(json.contains("\"completenessClaimed\": false")); + assert!(!json.contains("\"completenessClaimed\": true")); +} + +#[test] +fn case_14_known_debt_survives_clean_reviewer_evidence() { + let mut inp = input(subject(HEAD, HEAD, HEAD), all_green(HEAD)); + inp.known_debts = vec!["W8".to_owned(), "W9 both arms".to_owned()]; + let p = ok(&inp); + assert_eq!(p.headline, State::Pass); + assert_eq!( + p.known_debts, + vec!["W8".to_owned(), "W9 both arms".to_owned()] + ); +} + +#[test] +fn case_15_predicate_makes_no_merge_authorization_claim() { + let p = ok(&input(subject(HEAD, HEAD, HEAD), all_green(HEAD))); + let json = p.to_json().expect("serializing the predicate"); + for forbidden in ["merge", "approve", "authoriz", "mergeable", "ready_to"] { + assert!( + !json.to_lowercase().contains(forbidden), + "predicate must not speak about {forbidden}: {json}" + ); + } +} + +// --------------------------------------------------------------- 13, output -- + +#[test] +fn case_13_classification_is_a_pure_function_of_its_input() { + // Independence from local Git state is STRUCTURAL: this crate links no git + // API, spawns no process and reads no path, which the PR's scope scan + // checks. What a test can show is the observable half — the same input + // always yields byte-identical output, so nothing ambient leaks in. + let inp = input(subject(HEAD, HEAD, HEAD), all_green(HEAD)); + let a = ok(&inp).to_json().expect("serializing"); + let b = ok(&inp).to_json().expect("serializing"); + assert_eq!(a, b); + assert!(!a.contains("timestamp")); +} + +#[test] +fn observation_order_follows_the_authoritative_policy_order() { + let p = ok(&input(subject(HEAD, HEAD, HEAD), all_green(HEAD))); + let ids: Vec<&str> = p.observations.iter().map(|o| o.id.as_str()).collect(); + assert_eq!(ids, policy().required_observations); +} + +// ------------------------------------------------------- provenance (RED-3) -- + +#[test] +fn case_16_not_produced_and_rate_limited_are_both_owed_yet_distinguishable() { + // Two different observed facts that share one state. If the predicate keeps + // only the state, it has destroyed the distinction — the same destructive + // collapse the headline rule forbids, one level down. + let mut obs = all_green(HEAD); + obs.insert( + "review/codex".to_owned(), + ObservationInput::Review(Acquisition::NotProduced), + ); + obs.insert( + "review/coderabbit".to_owned(), + ObservationInput::Review(Acquisition::RateLimited), + ); + let p = ok(&input(subject(HEAD, HEAD, HEAD), obs)); + + let codex = obs_of(&p, "review/codex"); + let rabbit = obs_of(&p, "review/coderabbit"); + assert_eq!(codex.state, State::Owed); + assert_eq!(rabbit.state, State::Owed); + assert_eq!(codex.acquisition.status, AcquisitionStatus::NotProduced); + assert_eq!(rabbit.acquisition.status, AcquisitionStatus::RateLimited); + assert_ne!(codex.acquisition.status, rabbit.acquisition.status); +} + +#[test] +fn case_17_a_failed_acquisition_keeps_its_reason_in_the_predicate() { + // "It failed" cannot be re-evaluated later; #147 requires enough provenance + // to re-fetch or re-evaluate, so the reason has to survive serialization. + let mut obs = all_green(HEAD); + obs.insert( + "ci/worker-gate".to_owned(), + ObservationInput::Check(Acquisition::Failed { + reason: "503 from the checks endpoint".to_owned(), + }), + ); + let p = ok(&input(subject(HEAD, HEAD, HEAD), obs)); + + let gate = obs_of(&p, "ci/worker-gate"); + assert_eq!(gate.state, State::CannotCheck); + assert_eq!(gate.acquisition.status, AcquisitionStatus::Failed); + assert_eq!( + gate.acquisition.reason.as_deref(), + Some("503 from the checks endpoint") + ); + let json = p.to_json().expect("serializing"); + assert!(json.contains("503 from the checks endpoint")); +} + +#[test] +fn case_18_a_failed_falsification_verification_keeps_its_reason() { + let mut inp = input(subject(HEAD, HEAD, HEAD), all_green(HEAD)); + inp.falsifications.push(FalsificationFact { + source_kind: SourceKind::ReviewComment, + stable_id: "discussion-9".to_owned(), + subject_sha: Some(HEAD.to_owned()), + author: "someone[bot]".to_owned(), + verification: Verification::Failed { + reason: "reproducer unavailable".to_owned(), + }, + }); + let p = ok(&inp); + + let record = p.falsifications.first().expect("one record"); + assert_eq!(record.state, State::CannotCheck); + assert_eq!(record.verification.status, VerificationStatus::Failed); + assert_eq!( + record.verification.reason.as_deref(), + Some("reproducer unavailable") + ); + assert!(p + .to_json() + .expect("serializing") + .contains("reproducer unavailable")); +} + +#[test] +fn case_19_available_observations_record_that_status_too() { + let p = ok(&input(subject(HEAD, HEAD, HEAD), all_green(HEAD))); + for id in policy().required_observations { + assert_eq!( + obs_of(&p, &id).acquisition.status, + AcquisitionStatus::Available + ); + } +} diff --git a/crates/o7-closure-classifier/tests/frozen_fixtures.rs b/crates/o7-closure-classifier/tests/frozen_fixtures.rs new file mode 100644 index 00000000..c32d67a5 --- /dev/null +++ b/crates/o7-closure-classifier/tests/frozen_fixtures.rs @@ -0,0 +1,336 @@ +//! The three frozen Step 0B fixtures, driven through the classifier. +//! +//! The fixtures are read from `tests/fixtures/github/` at the workspace root and +//! are NEVER written to. Each test asserts the fixture's hostile precondition +//! FIRST, straight from the JSON, before asserting anything about classifier +//! behaviour — a test that passes because the adversarial condition was never +//! constructed is the failure mode this corpus exists to prevent. +//! +//! THE ENVELOPE IS NOT THE PRODUCTION INPUT SCHEMA. The fixture files carry +//! several API surfaces in one document because a file must. The loader below +//! unwraps those surfaces and builds typed [`ClassifierInput`]s; it is test +//! infrastructure, and the production input model deliberately looks nothing +//! like the envelope. +//! +//! THE LOADER MAY NOT INJECT OUTCOMES. Nowhere below does a fixture name map to +//! an expected state. The loader supplies observable facts — which commit a +//! review is bound to, whether a review comment references it, which checks +//! exist at which SHA — and the classifier decides the states. + +// Justification for the restriction-lint allowance, per the precedent in +// `tests/a0_candidate_state_e2e.rs`: every panic path below is this test's own +// assertion or its own reading of a file it ships with. +#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] + +use o7_closure_classifier::{ + classify, Acquisition, CheckConclusion, CheckEvidence, ClassifierInput, FalsificationFact, + ObservationInput, Policy, Predicate, ReviewEvidence, SourceKind, State, Subject, Verification, +}; +use serde_json::Value; +use std::collections::BTreeMap; +use std::path::PathBuf; + +/// HARNESS MAPPING, not a fixture fact: #147's policy enumerates observation ids +/// but does not state which vendor check name produces each. Resolving that is +/// the acquisition layer's job; the table is spelled out here so the choice is +/// visible rather than buried in a match arm. +const CHECK_NAME_TO_ID: &[(&str, &str)] = &[ + ("worker gate", "ci/worker-gate"), + ("dependency policy", "ci/dependency-policy"), + ("dependency advisories", "ci/dependency-advisories"), + ("worktree+verifier tests", "ci/worktree-verifier"), +]; + +/// HARNESS MAPPING, same status as above: which policy observation a given bot's +/// submitted review satisfies. +fn reviewer_id_for(login: &str) -> Option<&'static str> { + if login.starts_with("chatgpt-codex-connector") { + Some("review/codex") + } else if login.starts_with("coderabbitai") { + Some("review/coderabbit") + } else { + None + } +} + +fn policy() -> Policy { + Policy { + id: "007/closure/v1".to_owned(), + required_observations: vec![ + "ci/worker-gate".to_owned(), + "ci/dependency-policy".to_owned(), + "ci/dependency-advisories".to_owned(), + "ci/worktree-verifier".to_owned(), + "review/codex".to_owned(), + "review/coderabbit".to_owned(), + ], + } +} + +fn fixture(name: &str) -> Value { + let path: PathBuf = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../../tests/fixtures/github") + .join(name); + let text = std::fs::read_to_string(&path) + .unwrap_or_else(|e| panic!("reading frozen fixture {}: {e}", path.display())); + serde_json::from_str(&text).unwrap_or_else(|e| panic!("parsing frozen fixture {name}: {e}")) +} + +fn ptr<'a>(doc: &'a Value, p: &str) -> &'a Value { + doc.pointer(p) + .unwrap_or_else(|| panic!("frozen fixture pointer {p} does not resolve")) +} + +fn text<'a>(doc: &'a Value, p: &str) -> &'a str { + ptr(doc, p) + .as_str() + .unwrap_or_else(|| panic!("frozen fixture pointer {p} is not a string")) +} + +/// Unwrap the envelope into typed observations. Purely structural: a review +/// carries a finding iff some review comment references it by +/// `pull_request_review_id`. No body text is read. +fn observations_from(doc: &Value) -> BTreeMap { + let mut out = BTreeMap::new(); + + if let Some(runs) = doc + .pointer("/check_runs/check_runs") + .and_then(Value::as_array) + { + for run in runs { + let name = run.get("name").and_then(Value::as_str).unwrap_or_default(); + let Some((_, id)) = CHECK_NAME_TO_ID.iter().find(|(n, _)| *n == name) else { + continue; + }; + let head_sha = run + .get("head_sha") + .and_then(Value::as_str) + .unwrap_or_default(); + let success = run.get("conclusion").and_then(Value::as_str) == Some("success"); + out.insert( + (*id).to_owned(), + ObservationInput::Check(Acquisition::Available(CheckEvidence { + stable_id: run + .get("id") + .map(std::string::ToString::to_string) + .unwrap_or_default(), + head_sha: head_sha.to_owned(), + conclusion: if success { + CheckConclusion::Success + } else { + CheckConclusion::Failure + }, + })), + ); + } + } + + let empty = Vec::new(); + let review_comments = doc + .pointer("/review_comments") + .and_then(Value::as_array) + .unwrap_or(&empty); + + if let Some(reviews) = doc.pointer("/reviews").and_then(Value::as_array) { + for review in reviews { + let login = review + .pointer("/user/login") + .and_then(Value::as_str) + .unwrap_or_default(); + let Some(id) = reviewer_id_for(login) else { + continue; + }; + let review_id = review.get("id").cloned().unwrap_or(Value::Null); + let carries_finding = review_comments + .iter() + .any(|c| c.get("pull_request_review_id") == Some(&review_id)); + out.insert( + id.to_owned(), + ObservationInput::Review(Acquisition::Available(ReviewEvidence { + stable_id: review_id.to_string(), + commit_id: review + .get("commit_id") + .and_then(Value::as_str) + .unwrap_or_default() + .to_owned(), + author: login.to_owned(), + carries_finding, + })), + ); + } + } + + // Any required reviewer with no submitted review object produced no verdict. + // Stated explicitly rather than left to be inferred from a missing key. + for id in policy().required_observations { + if id.starts_with("review/") { + out.entry(id) + .or_insert(ObservationInput::Review(Acquisition::NotProduced)); + } + } + + out +} + +fn classify_fixture(doc: &Value, falsifications: Vec) -> Predicate { + let head = text(doc, "/pull_request/head/sha").to_owned(); + classify(&ClassifierInput { + subject: Subject { + repository: text(doc, "/pull_request/head/repo/full_name").to_owned(), + pull_request: ptr(doc, "/pull_request/number") + .as_u64() + .unwrap_or_default(), + expected_sha: head.clone(), + head_before: head.clone(), + head_after: head, + }, + policy: policy(), + observations: observations_from(doc), + falsifications, + known_debts: Vec::new(), + }) + .expect("the fixture adapter supplies every required observation explicitly") +} + +fn state_of(p: &Predicate, id: &str) -> State { + p.observations + .iter() + .find(|o| o.id == id) + .map(|o| o.state) + .unwrap_or_else(|| panic!("observation {id} missing")) +} + +// --------------------------------------------------- stale-review-wrong-sha -- + +#[test] +fn stale_review_wrong_sha() { + let doc = fixture("stale-review-wrong-sha.json"); + + // HOSTILE PRECONDITION, asserted before any classifier expectation. + let head = text(&doc, "/pull_request/head/sha"); + let review_sha = text(&doc, "/reviews/0/commit_id"); + assert_ne!( + review_sha, head, + "fixture must present a review bound to a commit other than the head" + ); + assert_eq!( + text(&doc, "/check_runs/check_runs/0/head_sha"), + head, + "fixture must present checks bound to the head" + ); + + let p = classify_fixture(&doc, Vec::new()); + + // The review does not bind to the subject, so it yields no verdict for it — + // neither a pass nor a finding. + assert_eq!(state_of(&p, "review/codex"), State::Owed); + // The checks that DO bind to the head must not rescue the wrong-SHA review. + for id in [ + "ci/worker-gate", + "ci/dependency-policy", + "ci/dependency-advisories", + "ci/worktree-verifier", + ] { + assert_eq!(state_of(&p, id), State::Pass); + } + assert_eq!(state_of(&p, "review/coderabbit"), State::Owed); + assert_eq!(p.headline, State::Owed); + assert_ne!(p.headline, State::Pass); + // The SUBJECT is not stale here — the head never moved. It is the REVIEW + // artifact that is bound to a superseded commit, which is a different thing + // and must not be conflated with a stale snapshot. + assert!(!p.subject_stale); +} + +// ------------------------------------------------- falsification-in-comment -- + +#[test] +fn falsification_in_comment() { + let doc = fixture("falsification-in-comment.json"); + + // HOSTILE PRECONDITION first. + let head = text(&doc, "/pull_request/head/sha"); + assert_eq!(text(&doc, "/reviews/0/commit_id"), head); + assert!( + ptr(&doc, "/review_comments") + .as_array() + .is_some_and(Vec::is_empty), + "fixture must present an empty review-comment surface" + ); + let comment_author = text(&doc, "/issue_comments/0/user/login"); + assert!( + !text(&doc, "/issue_comments/0/body").is_empty(), + "fixture must present a defect claim on the issue-comment surface" + ); + + // The frozen README documents `/issue_comments/0` as carrying a concrete, + // reproducible defect claim. The harness supplies that as an already + // established fact WITH ITS PROVENANCE — it does not infer a prose grammar, + // and nothing here reads the body to decide whether it is "real". + let falsification = FalsificationFact { + source_kind: SourceKind::IssueComment, + stable_id: ptr(&doc, "/issue_comments/0/id").to_string(), + subject_sha: None, + author: comment_author.to_owned(), + verification: Verification::Reproduced, + }; + + let p = classify_fixture(&doc, vec![falsification]); + + // The review surface at the head records no finding, so it passes as a + // verdict... + assert_eq!(state_of(&p, "review/codex"), State::Pass); + // ...the other required reviewer produced no verdict object at all... + assert_eq!(state_of(&p, "review/coderabbit"), State::Owed); + // ...and the claim on the verdict-less surface still falsifies. + assert_eq!(p.falsifications.len(), 1); + assert_eq!(p.headline, State::Finding); +} + +// ---------------------------------------------- conflicting-review-surfaces -- + +#[test] +fn conflicting_review_surfaces() { + let doc = fixture("conflicting-review-surfaces.json"); + + // HOSTILE PRECONDITION first: one vendor, one head, two surfaces. + let head = text(&doc, "/pull_request/head/sha"); + assert_eq!(text(&doc, "/reviews/0/commit_id"), head); + assert_eq!(text(&doc, "/review_comments/0/commit_id"), head); + assert_eq!( + text(&doc, "/reviews/0/user/login"), + text(&doc, "/issue_comments/0/user/login"), + "fixture must present the same author on both surfaces" + ); + assert_eq!( + ptr(&doc, "/review_comments/0/pull_request_review_id"), + ptr(&doc, "/reviews/0/id"), + "the review comment must belong to that submitted review" + ); + + let p = classify_fixture(&doc, Vec::new()); + + // The submitted, commit-bound review controls verdict accounting, and it + // carries a finding. The issue comment claiming the same commit is clean has + // NO channel into positive evidence, so it cannot overturn this — and it is + // not deleted either: it remains in the fixture, unread by the classifier. + assert_eq!(state_of(&p, "review/codex"), State::Finding); + assert_eq!(state_of(&p, "review/coderabbit"), State::Owed); + assert_eq!(p.headline, State::Finding); + assert_ne!(p.headline, State::Pass); +} + +#[test] +fn frozen_fixtures_are_never_written_to() { + // Reading is the only access this file makes; recorded as an assertion so a + // future edit that starts writing has to delete a line that says not to. + for name in [ + "stale-review-wrong-sha.json", + "falsification-in-comment.json", + "conflicting-review-surfaces.json", + ] { + let before = fixture(name); + let _ = classify_fixture(&before, Vec::new()); + assert_eq!(before, fixture(name)); + } +}