Skip to main content

rivet/pipeline/
validate_manifest.rs

1//! **Layer: Observability**
2//!
3//! Manifest-aware verification for `--validate` (ADR-0012 §M5 / §M6,
4//! constrained by the ADR-0013 trust-flag contract that says: no new flags;
5//! manifest-aware checks live under the existing `--validate`).
6//!
7//! Although this module reads from a `Destination` (technically L2 surface),
8//! it makes **no execution decisions**: it does not write data, advance
9//! cursors, mutate state, or change the pipeline path.  Its only output is
10//! a structured `ManifestVerification` verdict the run report renders.
11//! Per ADR-0003, that places it firmly in L4 Observability — the
12//! destination read surface is just the carrier.
13//!
14//! Inputs (read-only):
15//! - the destination's `manifest.json` body
16//! - the destination's `_SUCCESS` body, if present
17//! - the listing of every object under the destination prefix
18//!
19//! Outputs:
20//! - [`ManifestVerification`] — a structured verdict the run report renders
21//!   into the operator-facing "Verdicts" section.
22//!
23//! Out of scope here:
24//! - per-file row-count check (that runs *during* the export, against the
25//!   local temp file before upload — see `pipeline::validate::validate_output`).
26//! - source-side reconciliation (lives in [`pipeline::reconcile_cmd`] and
27//!   is what `--reconcile` adds on top of this).
28//! - re-fingerprinting parts (`--validate --deep`, future).
29//!
30//! Failure modes are explicit: each check produces a `Failure` enum variant
31//! that is rendered verbatim in `summary.json` so an Airflow / CI consumer
32//! can branch on the kind, not parse strings.
33
34use serde::{Deserialize, Serialize};
35
36use crate::destination::Destination;
37use crate::error::Result;
38use crate::manifest::{
39    MANIFEST_FILENAME, RunManifest, SUCCESS_FILENAME, join_key, parse_success_marker,
40    success_marker_body,
41};
42use crate::pipeline::manifest_reconcile::{PartPresence, reconcile_manifest_against_listing};
43
44/// Upper bound on a destination control artifact (`manifest.json`) the read
45/// path will materialise into memory.  A `manifest.json` is metadata — a few
46/// KB to low single-digit MB even for very large datasets — so 64 MiB is far
47/// above any legitimate body while still bounding the blast radius.
48///
49/// Security (V21, CWE-400): the manifest readers `head()` an object then read
50/// its full body into a `Vec<u8>`.  An attacker who can write the destination
51/// prefix (a shared bucket prefix, a world-writable export dir) can plant a
52/// multi-GB `manifest.json`; an unbounded read would OOM the next `--resume`,
53/// `--validate`, or `rivet repair`.  [`read_capped`] consults the size the
54/// `head()` already reports and bails before the read when it exceeds this cap.
55pub(crate) const MANIFEST_MAX_BYTES: u64 = 64 * 1024 * 1024;
56
57/// How deep a `rivet validate` pass goes — a graded verify layer over the
58/// same checks, letting an operator trade thoroughness for latency / cost.
59///
60/// The variants are a strict superset chain: `Light ⊂ Sample ⊂ Full`.  Each
61/// level runs every check the level below it does, plus more.  Defined here
62/// (the pipeline layer) and re-exported for the CLI grammar so the **same**
63/// enum gates the checks in [`verify_at_destination`] and parses on the
64/// `--depth` flag — no CLI→pipeline back-dependency.
65///
66/// - **Light**: manifest read + self-consistency + `_SUCCESS` only.  Skips the
67///   `list_prefix` reconcile (no per-part presence/size/checksum) and the
68///   untracked-surplus scan, leaving `parts_verified = 0`.  One `head` + one
69///   `read` of `manifest.json` and `_SUCCESS` — a fast "is this prefix a
70///   complete, marked run?" poll with no prefix listing.
71/// - **Sample**: everything Light does **plus** the part reconcile and
72///   untracked surplus (one `list_prefix`).  This is the pre-graded behaviour
73///   minus the Form B value re-read — full structural verification with no
74///   part downloads.
75/// - **Full** (default): everything Sample does **plus** the Form B value-
76///   checksum re-read (re-reads parts, re-derives per-column checksums).  The
77///   most thorough and the only level that downloads part bodies.  Equivalent
78///   to the pre-graded behaviour, so existing callers are unchanged.
79#[derive(clap::ValueEnum, Debug, Clone, Copy, PartialEq, Eq, Default)]
80pub enum ValidateDepth {
81    /// Manifest read + self-consistency + `_SUCCESS` only (no prefix listing).
82    Light,
83    /// Light + part reconcile + untracked surplus (one `list_prefix`).
84    Sample,
85    /// Sample + the Form B value-checksum re-read (downloads parts).
86    #[default]
87    Full,
88}
89
90impl ValidateDepth {
91    /// True iff this level runs the `list_prefix` reconcile (part presence,
92    /// size, checksum) and the untracked-surplus scan — i.e. anything above
93    /// `Light`.  The single predicate the section-3/5 depth gating keys off.
94    fn runs_part_reconcile(self) -> bool {
95        !matches!(self, ValidateDepth::Light)
96    }
97
98    /// True iff this level downloads part *bodies* — the CDC `__pos` continuity
99    /// check and the Form-B value-checksum re-read, both `Full`-only (the
100    /// `part_reconcile` level above only reads listing metadata). The single
101    /// predicate the section-3/5 part-download gating keys off, parallel to
102    /// [`Self::runs_part_reconcile`] — so adding a depth level edits the enum,
103    /// not the call sites in `validate_cmd`.
104    pub(crate) fn runs_part_download(self) -> bool {
105        matches!(self, ValidateDepth::Full)
106    }
107
108    /// Stable operator-/wire-facing label for the depth a verdict was produced
109    /// at, surfaced in `summary.json` (`depth_level`) and `rivet validate`
110    /// output.
111    pub fn label(self) -> &'static str {
112        match self {
113            ValidateDepth::Light => "light",
114            ValidateDepth::Sample => "sample",
115            ValidateDepth::Full => "full",
116        }
117    }
118}
119
120/// Read `key` into memory only if its `head()`-reported size is within
121/// `max_bytes`; otherwise bail without reading a single byte.
122///
123/// The single enforcement point for the V21 (CWE-400) manifest-read cap shared
124/// by the three control-artifact readers (`--resume` M8 preamble, `--validate`,
125/// `rivet repair`).  Each previously did `head()` then an uncapped `read()`,
126/// discarding the size `head()` already returned; routing through here closes
127/// that gap in one place.
128///
129/// Behaviour:
130/// - object absent (`head` → `None`): `Err` — callers invoke this only after
131///   establishing the object exists, so an absent object here is a hard error,
132///   not the benign "no manifest / legacy prefix" case (which the callers
133///   detect with their own `head()` first).
134/// - oversized (`size_bytes > max_bytes`): `Err` naming the cap, **before** any
135///   body is materialised.
136/// - otherwise: the full body via [`Destination::read`].
137pub(crate) fn read_capped(dest: &dyn Destination, key: &str, max_bytes: u64) -> Result<Vec<u8>> {
138    match dest.head(key)? {
139        None => anyhow::bail!("'{key}' not found at the destination"),
140        Some(meta) => {
141            if meta.size_bytes > max_bytes {
142                anyhow::bail!(
143                    "'{key}' is {} bytes, exceeding the {max_bytes}-byte control-artifact \
144                     read cap — refusing to load it into memory (possible tampering)",
145                    meta.size_bytes
146                );
147            }
148            dest.read(key)
149        }
150    }
151}
152
153/// Outcome of a single `--validate` pass over a destination prefix.
154///
155/// Stable enough to be embedded in `summary.json` directly (see
156/// `pipeline::report::ValidationOutcome`).  Forward-compat: consumers MUST
157/// ignore unknown fields (no `deny_unknown_fields`).
158#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
159pub struct ManifestVerification {
160    /// True iff a `manifest.json` was found at the destination and parsed.
161    /// `false` triggers ADR-0012 M6 fallback (legacy run); higher-level
162    /// check results below are then "skipped" rather than "passed".
163    pub manifest_found: bool,
164    /// Mirrors ADR-0012 M6's required `legacy_run` operator-facing label.
165    pub legacy_run: bool,
166    /// Manifest parts whose presence and recorded `size_bytes` were
167    /// confirmed at the destination.  0 when no manifest was found.
168    pub parts_verified: usize,
169    /// Subset of `parts_verified` whose **content** was confirmed via an MD5
170    /// the store surfaced in its listing (no download) — the rest are size-only.
171    /// Lets `passed: true` say how much of the dataset was content-checked
172    /// rather than implying all of it was.  `#[serde(default)]` for back-compat.
173    #[serde(default)]
174    pub parts_md5_verified: usize,
175    /// Manifest parts that were declared `committed` but not actually
176    /// present, present at a different size, or otherwise mismatched.
177    pub parts_failed: usize,
178    /// True iff `_SUCCESS` exists at the destination AND its body matches
179    /// the fingerprint of the bytes we read for `manifest.json`.  An
180    /// existing `_SUCCESS` whose body diverges from the manifest is itself
181    /// an integrity failure — surfaced via `failures`.
182    pub success_marker_consistent: bool,
183    /// Self-consistency of the manifest (`row_count`, `part_count`,
184    /// duplicate `part_id`s).  Skipped when `manifest_found = false`.
185    pub manifest_self_consistent: bool,
186    /// Final verdict, **derived** (not hand-maintained) — `manifest_found` and
187    /// no *fatal* failure ([`Failure::is_fatal`]).  Stored so it stays in the
188    /// `summary.json` contract, but computed in one place
189    /// ([`ManifestVerification::recompute_passed`]) so a new failure variant is
190    /// fatal by default rather than relying on every site to flip a bool.
191    pub passed: bool,
192    /// Per-failure detail.  May be non-empty with `passed = true` for advisory
193    /// (non-fatal) failures like [`Failure::UntrackedObject`].  Stable variant
194    /// set; new variants land under a new manifest version per ADR-0012.
195    pub failures: Vec<Failure>,
196    /// The graded depth this verdict was produced at: `"light"`, `"sample"`,
197    /// or `"full"` (see [`ValidateDepth`]).  Lets a consumer of `summary.json`
198    /// tell **how much** was actually checked — a `passed: true` at `"light"`
199    /// asserts far less than at `"full"` (no part presence was verified).
200    /// `#[serde(default)]` (→ `"full"`) for back-compat: pre-graded verdicts
201    /// always ran the full pass.
202    #[serde(default = "default_depth_level")]
203    pub depth_level: String,
204}
205
206/// `serde(default)` for [`ManifestVerification::depth_level`]: a verdict that
207/// predates the graded layer always ran the full pass, so an absent field
208/// deserializes to `"full"`.
209fn default_depth_level() -> String {
210    ValidateDepth::Full.label().to_string()
211}
212
213#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
214#[serde(tag = "kind", rename_all = "snake_case")]
215pub enum Failure {
216    /// Manifest declared a part that does not exist at the destination.
217    PartMissing { part_id: u32, path: String },
218    /// Manifest declared a part whose actual size differs from `size_bytes`.
219    PartSizeMismatch {
220        part_id: u32,
221        path: String,
222        expected: u64,
223        actual: u64,
224    },
225    /// Part present at the recorded size but its content MD5 (from the store's
226    /// listing metadata) differs from the manifest's — transit / at-rest
227    /// corruption, caught with no download.
228    PartChecksumMismatch {
229        part_id: u32,
230        path: String,
231        expected: String,
232        actual: String,
233    },
234    /// `_SUCCESS` exists but its body is malformed (not `xxh3:<16-hex>` after
235    /// trim).  ADR-0012 M2 — orchestrators rely on this format being strict.
236    SuccessMarkerMalformed { body_preview: String },
237    /// `_SUCCESS` body parsed but does not match `xxh3(manifest.json bytes)`.
238    /// Two legitimate sources: (a) someone overwrote `_SUCCESS` after the
239    /// manifest was rewritten — orchestrator bug; (b) the manifest was
240    /// edited in place after the run — operator bug.  Either way the
241    /// manifest is no longer trustworthy.
242    SuccessMarkerStale {
243        marker_fingerprint: String,
244        manifest_fingerprint: String,
245    },
246    /// `RunManifest::validate_self_consistency` rejected the manifest.
247    /// Usually a writer bug (declared row_count != sum of committed parts'
248    /// rows); blocks the rest of the verification because the manifest
249    /// itself is unreliable.
250    ManifestSelfInconsistent { detail: String },
251    /// Reading `manifest.json` returned an I/O error other than "absent".
252    ManifestReadError { detail: String },
253    /// Reading `_SUCCESS` returned an I/O error other than "absent".
254    SuccessMarkerReadError { detail: String },
255    /// Listing the destination prefix returned an I/O error.  Reduces the
256    /// untracked-parts check (M5 surplus) to a no-op for this run.
257    ListPrefixError { detail: String },
258    /// A file is present at the destination prefix but no manifest entry
259    /// references it.  M9-adjacent: `--validate` only flags it; quarantine
260    /// belongs to `--resume`.
261    UntrackedObject { key: String, size_bytes: u64 },
262    /// The export declared `verify: content` but some parts could only be
263    /// size-verified (no comparable content checksum from the store) — the
264    /// declared integrity contract was not met.
265    ContentVerificationUnmet { size_only: usize, total: usize },
266    /// A manifest was *required* at this prefix (the operator pinned a literal
267    /// `--prefix`, asserting a real dataset lives here) but none was found.
268    /// Without this, an absent manifest at an operator-pinned prefix maps to
269    /// the M6 legacy-run label and exits 0 — indistinguishable from a verified
270    /// run, so a CI gate `rivet validate && deploy` sails past a destination
271    /// that was never written.  Fatal: a required-but-missing manifest is a
272    /// refusal reason, not a "cannot certify" advisory.
273    ManifestRequiredButAbsent { prefix: String },
274}
275
276impl Failure {
277    /// Whether this failure invalidates the dataset (flips `passed` to false).
278    ///
279    /// Every variant is fatal **except** [`Failure::UntrackedObject`]: surplus
280    /// objects are an audit signal whose cleanup is `--resume`'s job (ADR-0012
281    /// M9), not a corruption of the manifest-listed parts.  New variants are
282    /// fatal by default — opt out here explicitly, so a forgotten case fails
283    /// closed (safe) rather than silently passing.
284    pub fn is_fatal(&self) -> bool {
285        !matches!(self, Failure::UntrackedObject { .. })
286    }
287
288    /// Stable `RIVET_VERIFY_*` error code for this failure variant.
289    ///
290    /// One code per variant, intended for orchestrators / CI to branch on
291    /// without parsing the human `Display` string or the per-variant JSON
292    /// fields.  The code is part of the wire contract: it is emitted next to
293    /// `kind` in the JSON report and prefixed in brackets on each pretty line.
294    /// Codes are append-only — never renamed once shipped (a renamed code is a
295    /// silent break for any consumer keying off it).
296    pub fn error_code(&self) -> &'static str {
297        match self {
298            Failure::PartMissing { .. } => "RIVET_VERIFY_PART_MISSING",
299            Failure::PartSizeMismatch { .. } => "RIVET_VERIFY_PART_SIZE_MISMATCH",
300            Failure::PartChecksumMismatch { .. } => "RIVET_VERIFY_PART_CHECKSUM_MISMATCH",
301            Failure::SuccessMarkerMalformed { .. } => "RIVET_VERIFY_SUCCESS_MALFORMED",
302            Failure::SuccessMarkerStale { .. } => "RIVET_VERIFY_SUCCESS_STALE",
303            Failure::ManifestSelfInconsistent { .. } => "RIVET_VERIFY_MANIFEST_INCONSISTENT",
304            Failure::ManifestReadError { .. } => "RIVET_VERIFY_MANIFEST_READ_ERROR",
305            Failure::SuccessMarkerReadError { .. } => "RIVET_VERIFY_SUCCESS_READ_ERROR",
306            Failure::ListPrefixError { .. } => "RIVET_VERIFY_LIST_ERROR",
307            Failure::UntrackedObject { .. } => "RIVET_VERIFY_UNTRACKED_OBJECT",
308            Failure::ContentVerificationUnmet { .. } => "RIVET_VERIFY_CONTENT_UNMET",
309            Failure::ManifestRequiredButAbsent { .. } => "RIVET_VERIFY_MANIFEST_REQUIRED",
310        }
311    }
312}
313
314impl std::fmt::Display for Failure {
315    /// One operator-facing line per failure variant.  Used by:
316    /// - `pipeline::report::render_markdown` (summary.md "failure:" lines)
317    /// - `pipeline::validate_cmd::render_pretty` (`rivet validate` stdout)
318    /// - any future consumer that wants a human-readable failure label
319    ///
320    /// The wire format (`failures[].kind` + per-variant fields) lives in
321    /// the `Serialize` derive above and is the contract Airflow / CI
322    /// consumers branch on.  This `Display` impl is for humans only.
323    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
324        match self {
325            Failure::PartMissing { part_id, path } => {
326                write!(f, "part {} missing at {}", part_id, path)
327            }
328            Failure::PartSizeMismatch {
329                part_id,
330                path,
331                expected,
332                actual,
333            } => write!(
334                f,
335                "part {} size mismatch at {}: manifest {}, dest {}",
336                part_id, path, expected, actual
337            ),
338            Failure::PartChecksumMismatch {
339                part_id,
340                path,
341                expected,
342                actual,
343            } => write!(
344                f,
345                "part {} content mismatch at {}: manifest md5 {}, dest {}",
346                part_id, path, expected, actual
347            ),
348            Failure::SuccessMarkerMalformed { body_preview } => {
349                write!(f, "_SUCCESS body malformed: {body_preview:?}")
350            }
351            Failure::SuccessMarkerStale {
352                marker_fingerprint,
353                manifest_fingerprint,
354            } => write!(
355                f,
356                "_SUCCESS body {} != manifest fingerprint {} (stale marker)",
357                marker_fingerprint, manifest_fingerprint
358            ),
359            Failure::ManifestSelfInconsistent { detail } => {
360                write!(f, "manifest self-consistency: {detail}")
361            }
362            Failure::ManifestReadError { detail } => {
363                write!(f, "manifest read error: {detail}")
364            }
365            Failure::SuccessMarkerReadError { detail } => {
366                write!(f, "_SUCCESS read error: {detail}")
367            }
368            Failure::ListPrefixError { detail } => {
369                write!(f, "destination listing error: {detail}")
370            }
371            Failure::UntrackedObject { key, size_bytes } => {
372                write!(f, "untracked object: {} ({} bytes)", key, size_bytes)
373            }
374            Failure::ContentVerificationUnmet { size_only, total } => write!(
375                f,
376                "verify: content not met — {size_only} of {total} part(s) only \
377                 size-verified (no store checksum); lower max_file_size so parts \
378                 upload as a single PUT, or the backend exposes no checksum"
379            ),
380            Failure::ManifestRequiredButAbsent { prefix } => write!(
381                f,
382                "no manifest at {prefix}: a manifest was required here (operator \
383                 pinned --prefix) but none was found — this prefix was never \
384                 written, or the data was relocated. Run the export first, or \
385                 drop --prefix to validate the config-resolved destination."
386            ),
387        }
388    }
389}
390
391impl ManifestVerification {
392    /// Base verdict: nothing checked yet (no manifest, all counts zero, all
393    /// sub-checks false, `passed = false`).  Every constructor builds on this
394    /// and overrides only what differs, so a new field lands in **one** place
395    /// rather than several near-identical literals.
396    fn empty() -> Self {
397        Self {
398            manifest_found: false,
399            legacy_run: false,
400            parts_verified: 0,
401            parts_md5_verified: 0,
402            parts_failed: 0,
403            success_marker_consistent: false,
404            manifest_self_consistent: false,
405            passed: false,
406            failures: Vec::new(),
407            // Base level; `verify_at_destination` overwrites this with the
408            // depth it was actually called at before returning any verdict.
409            depth_level: default_depth_level(),
410        }
411    }
412
413    /// Recompute `passed` from the verdict's facts: a manifest was found and no
414    /// **fatal** failure was recorded (advisory failures like `UntrackedObject`
415    /// don't count).  The single source of truth — callers set failures and
416    /// call this once, rather than flipping `passed` by hand at every site.
417    fn recompute_passed(&mut self) {
418        self.passed = self.manifest_found && !self.failures.iter().any(Failure::is_fatal);
419    }
420
421    /// Apply the export's `verify` policy (ADR-0013 / review D).  When content
422    /// verification is required but some parts were only size-verified, record
423    /// a fatal [`Failure::ContentVerificationUnmet`] and re-derive `passed`.
424    /// Policy lives here (one place); the composers — run finalize and the
425    /// `rivet validate` command — just call it with their export's intent.
426    pub fn enforce_content_policy(&mut self, require_content: bool) {
427        if require_content && self.manifest_found {
428            let size_only = self.parts_verified.saturating_sub(self.parts_md5_verified);
429            if size_only > 0 {
430                self.failures.push(Failure::ContentVerificationUnmet {
431                    size_only,
432                    total: self.parts_verified,
433                });
434                self.recompute_passed();
435            }
436        }
437    }
438
439    /// Apply the "a manifest must exist here" policy (finding #20).  When the
440    /// operator pinned a literal `--prefix`, an absent manifest is no longer the
441    /// benign M6 legacy-run case — it almost always means the prefix was never
442    /// written (a misconfigured CI gate). Convert that exact verdict — no
443    /// manifest, no other failure (i.e. the [`ManifestVerification::legacy`]
444    /// shape) — into a fatal [`Failure::ManifestRequiredButAbsent`] so the exit
445    /// gate refuses it loudly instead of silently passing.
446    ///
447    /// Deliberately a no-op for every other shape: a real manifest (passed or
448    /// failed), or an absent manifest that already carries a `ManifestReadError`
449    /// / head failure, is left untouched — those are already classified.  Only
450    /// the "legacy / cannot certify" case is escalated, and only when required.
451    pub fn require_manifest_present(&mut self, prefix: &str) {
452        if !self.manifest_found && !self.has_failures() {
453            self.legacy_run = false;
454            self.failures.push(Failure::ManifestRequiredButAbsent {
455                prefix: prefix.to_string(),
456            });
457            self.recompute_passed();
458        }
459    }
460
461    /// Construct the M6 (legacy run) verdict for a destination that has no
462    /// manifest at all.  Caller composes this with the existing per-file
463    /// row-count check; together they form the legacy `--validate` result.
464    pub fn legacy() -> Self {
465        // `passed = false` is intentional — not "validation failed" but "this
466        // verifier cannot certify"; the caller layers per-file row counts on
467        // top and composes the final verdict.
468        Self {
469            legacy_run: true,
470            ..Self::empty()
471        }
472    }
473
474    /// True iff this verification surfaced any explicit failure (i.e. a
475    /// reason an orchestrator should refuse the run).  Distinct from
476    /// `!passed`, which can also mean "legacy / not applicable".
477    pub fn has_failures(&self) -> bool {
478        !self.failures.is_empty()
479    }
480}
481
482/// Run the manifest-aware verification at `manifest_dir` (the destination-
483/// relative directory containing `manifest.json` and `_SUCCESS`).
484///
485/// `manifest_dir` is the same key shape `Destination::write` was called with
486/// for the manifest itself — typically empty (`""`) for prefix-rooted runs,
487/// or the per-export sub-directory.  Trailing `/` is optional.
488///
489/// This function does not panic on any expected I/O outcome — every read
490/// failure becomes a `Failure::*ReadError` so the caller can render a
491/// useful message instead of bailing.
492///
493/// `depth` selects the graded verify layer (see [`ValidateDepth`]):
494/// - [`ValidateDepth::Light`] skips section 3 (the `list_prefix` part
495///   reconcile) and section 5 (untracked surplus), leaving `parts_verified`
496///   at 0 — a fast manifest + `_SUCCESS` poll with no prefix listing.
497/// - [`ValidateDepth::Sample`] and [`ValidateDepth::Full`] run all five
498///   sections here.  The Form B value re-read is **not** in this function;
499///   it is the caller's concern (`run_validate_command`), gated on `Full`.
500///
501/// Regardless of depth, `depth_level` on the returned verdict records the
502/// level this pass ran at.
503pub fn verify_at_destination(
504    dest: &dyn Destination,
505    manifest_dir: &str,
506    depth: ValidateDepth,
507) -> Result<ManifestVerification> {
508    let manifest_key = join_key(manifest_dir, MANIFEST_FILENAME);
509    let success_key = join_key(manifest_dir, SUCCESS_FILENAME);
510
511    // Stamp the depth this pass ran at onto every verdict before it leaves the
512    // function — including the early-return error/legacy shapes — so a consumer
513    // always sees *how much* was checked.  Each `return Ok(v)` below routes
514    // through `with_depth` (or sets `out.depth_level` for the main path).
515    let with_depth = |mut v: ManifestVerification| -> ManifestVerification {
516        v.depth_level = depth.label().to_string();
517        v
518    };
519
520    // ── 1. Manifest read ───────────────────────────────────────────────
521    //
522    // Error-consistency contract: every I/O outcome here surfaces as a
523    // structured `Failure` variant rather than as `Err`.  An operator gets
524    // one verdict shape regardless of whether the destination is missing,
525    // permission-denied, or temporarily unreachable.  The bubbled `Err`
526    // path is reserved for *programmer* errors (caller passes a malformed
527    // `manifest_dir`, a future destination breaks an internal invariant).
528    let manifest_bytes = match dest.head(&manifest_key) {
529        Ok(None) => return Ok(with_depth(ManifestVerification::legacy())),
530        Ok(Some(_)) => match read_capped(dest, &manifest_key, MANIFEST_MAX_BYTES) {
531            Ok(b) => b,
532            Err(e) => {
533                let mut v = ManifestVerification::legacy();
534                v.legacy_run = false;
535                v.failures.push(Failure::ManifestReadError {
536                    detail: format!("{e:#}"),
537                });
538                v.passed = false;
539                return Ok(with_depth(v));
540            }
541        },
542        Err(e) => {
543            // `head` failure is symmetric to a `read` failure — same kind
544            // (`ManifestReadError`) so consumers don't have to branch on
545            // which method tripped.  Distinct from "manifest absent"
546            // (Ok(None) above) which legitimately means "legacy prefix".
547            let mut v = ManifestVerification::legacy();
548            v.legacy_run = false;
549            v.failures.push(Failure::ManifestReadError {
550                detail: format!("manifest head failed: {e:#}"),
551            });
552            v.passed = false;
553            return Ok(with_depth(v));
554        }
555    };
556
557    let manifest: RunManifest = match serde_json::from_slice(&manifest_bytes) {
558        Ok(m) => m,
559        Err(e) => {
560            // A malformed manifest is treated as a self-inconsistency —
561            // semantically equivalent for the operator (the manifest can't
562            // be trusted) but kept distinct in `failures` so the kind is
563            // explicit on the wire.
564            return Ok(with_depth(ManifestVerification {
565                manifest_found: true,
566                failures: vec![Failure::ManifestSelfInconsistent {
567                    detail: format!("manifest.json parse failed: {e}"),
568                }],
569                ..ManifestVerification::empty()
570            }));
571        }
572    };
573
574    // Optimistic base: a found, self-consistent manifest that passes until a
575    // check below flips it.  Overrides only what differs from `empty()`.
576    // Stamp the depth here so the two early `return Ok(out)` paths in section
577    // 4 (success-marker head error, non-utf8 body) carry the right level too.
578    let mut out = ManifestVerification {
579        manifest_found: true,
580        manifest_self_consistent: true,
581        passed: true,
582        depth_level: depth.label().to_string(),
583        ..ManifestVerification::empty()
584    };
585
586    // ── 2. Self-consistency ─────────────────────────────────────────────
587    if let Err(e) = manifest.validate_self_consistency() {
588        out.manifest_self_consistent = false;
589        out.failures.push(Failure::ManifestSelfInconsistent {
590            detail: format!("{e}"),
591        });
592        // Don't short-circuit — we still want to surface part-presence
593        // failures because the operator may want to know both classes at
594        // once rather than fix-then-rerun.
595    }
596
597    // ── 3. Reconcile parts + surplus against ONE prefix listing ────────
598    //
599    // Presence and untracked-surplus both fall out of a single
600    // `reconcile_manifest_against_listing` over one `list_prefix` — the same
601    // pure walk chunked resume uses (`build_resume_plan`).  This replaces the
602    // old per-part `HEAD` loop (N round-trips) and its separate untracked
603    // listing.  Per-part failures are emitted here (step 3); untracked is
604    // emitted at step 5 so the failure ordering an operator reads is stable.
605    //
606    // Trade-off: presence now rides the listing, not per-part `HEAD`.  If the
607    // listing cannot be read, an audit cannot certify the parts — so a list
608    // failure flips `passed = false` (a `ListPrefixError`), rather than the
609    // old behaviour where per-part HEAD still "verified" parts a failed
610    // listing couldn't enumerate.  Every Rivet destination backend offers
611    // strong read-after-write list consistency, so the happy path is one call.
612    //
613    // Graded depth: `Light` skips this `list_prefix` entirely — no part
614    // reconcile, no `ListPrefixError`, `parts_verified` stays 0, and section 5
615    // (untracked) is a no-op since `reconciliation` is `None`.  `Sample` and
616    // `Full` run it.  A `Light` pass therefore certifies only that the
617    // manifest reads, is self-consistent, and `_SUCCESS` matches — never that
618    // the parts are physically present.
619    let reconciliation = if depth.runs_part_reconcile() {
620        match dest.list_prefix(manifest_dir) {
621            Ok(listing) => Some(reconcile_manifest_against_listing(
622                &manifest,
623                &listing,
624                manifest_dir,
625            )),
626            Err(e) => {
627                out.failures.push(Failure::ListPrefixError {
628                    detail: format!("{e:#}"),
629                });
630                None
631            }
632        }
633    } else {
634        None
635    };
636    if let Some(rec) = &reconciliation {
637        for check in &rec.per_part {
638            match &check.presence {
639                PartPresence::Present { md5_verified } => {
640                    out.parts_verified += 1;
641                    if *md5_verified {
642                        out.parts_md5_verified += 1;
643                    }
644                }
645                PartPresence::SizeMismatch { expected, actual } => {
646                    out.parts_failed += 1;
647                    out.failures.push(Failure::PartSizeMismatch {
648                        part_id: check.part_id,
649                        path: check.path.clone(),
650                        expected: *expected,
651                        actual: *actual,
652                    });
653                }
654                PartPresence::Missing => {
655                    out.parts_failed += 1;
656                    out.failures.push(Failure::PartMissing {
657                        part_id: check.part_id,
658                        path: check.path.clone(),
659                    });
660                }
661                PartPresence::ChecksumMismatch { expected, actual } => {
662                    out.parts_failed += 1;
663                    out.failures.push(Failure::PartChecksumMismatch {
664                        part_id: check.part_id,
665                        path: check.path.clone(),
666                        expected: expected.clone(),
667                        actual: actual.clone(),
668                    });
669                }
670            }
671        }
672    }
673
674    // ── 4. _SUCCESS marker consistency ─────────────────────────────────
675    //
676    // Same error-consistency contract as step 1: head/read failures become
677    // `Failure::SuccessMarkerReadError`, not bubbled `Err`.  Absent marker
678    // (Ok(None)) stays informational, not a failure (M2: only successful
679    // runs land _SUCCESS, so its absence on a failed manifest is correct).
680    let success_head = match dest.head(&success_key) {
681        Ok(h) => h,
682        Err(e) => {
683            out.failures.push(Failure::SuccessMarkerReadError {
684                detail: format!("_SUCCESS head failed: {e:#}"),
685            });
686            out.recompute_passed();
687            return Ok(out);
688        }
689    };
690    match success_head {
691        None => {
692            // Absent _SUCCESS is informational, not a failure: per ADR-0012
693            // M2, only successful runs land it.  A failed-then-rewritten
694            // manifest legitimately lacks _SUCCESS.  Leave
695            // `success_marker_consistent = false` (this is a "no signal"
696            // bool, not a "broken" bool) and let the caller decide.
697        }
698        Some(_) => match dest.read(&success_key) {
699            Err(e) => {
700                out.failures.push(Failure::SuccessMarkerReadError {
701                    detail: format!("{e:#}"),
702                });
703            }
704            Ok(body) => {
705                let body_str = match std::str::from_utf8(&body) {
706                    Ok(s) => s,
707                    Err(_) => {
708                        out.failures.push(Failure::SuccessMarkerMalformed {
709                            body_preview: format!("(non-utf8, {} bytes)", body.len()),
710                        });
711                        out.recompute_passed();
712                        return Ok(out);
713                    }
714                };
715                match parse_success_marker(body_str) {
716                    None => {
717                        out.failures.push(Failure::SuccessMarkerMalformed {
718                            body_preview: preview(body_str),
719                        });
720                    }
721                    Some(marker_fp) => {
722                        let manifest_fp = success_marker_body(&manifest_bytes);
723                        // success_marker_body returns the trailing `\n`
724                        // form; trim before comparing to the parsed marker
725                        // (which already trims).
726                        let manifest_fp_trimmed = manifest_fp.trim_end_matches('\n');
727                        if marker_fp == manifest_fp_trimmed {
728                            out.success_marker_consistent = true;
729                        } else {
730                            out.failures.push(Failure::SuccessMarkerStale {
731                                marker_fingerprint: marker_fp.to_string(),
732                                manifest_fingerprint: manifest_fp_trimmed.to_string(),
733                            });
734                        }
735                    }
736                }
737            }
738        },
739    }
740
741    // ── 5. Untracked surplus ───────────────────────────────────────────
742    //
743    // Already computed by the step-3 reconciliation (sidecars, quarantine,
744    // and the doctor probe are filtered there).  Emit it last so the failure
745    // ordering stays parts → marker → untracked.  A list failure left
746    // `reconciliation = None` and already flipped `passed` above.
747    if let Some(rec) = reconciliation {
748        for obj in rec.untracked {
749            out.failures.push(Failure::UntrackedObject {
750                key: obj.key,
751                size_bytes: obj.size_bytes,
752            });
753        }
754    }
755
756    out.recompute_passed();
757    Ok(out)
758}
759
760/// Truncate `s` to a small printable preview for error messages.
761fn preview(s: &str) -> String {
762    let trimmed: String = s.chars().take(40).collect();
763    if s.chars().count() > 40 {
764        format!("{trimmed}…")
765    } else {
766        trimmed
767    }
768}
769
770#[cfg(test)]
771mod tests {
772    use super::*;
773    use crate::config::{DestinationConfig, DestinationType};
774    use crate::destination::local::LocalDestination;
775    use crate::manifest::{
776        MANIFEST_VERSION, ManifestDestination, ManifestPart, ManifestSource, ManifestStatus,
777        PartStatus, RunManifest,
778    };
779    use std::path::Path;
780
781    fn local_dest(base: &Path) -> LocalDestination {
782        LocalDestination::new(&DestinationConfig {
783            destination_type: DestinationType::Local,
784            path: Some(base.to_string_lossy().into_owned()),
785            ..Default::default()
786        })
787        .unwrap()
788    }
789
790    fn part(part_id: u32, rows: i64, size: u64, fp: &str) -> ManifestPart {
791        ManifestPart {
792            part_id,
793            path: format!("part-{part_id:06}.parquet"),
794            rows,
795            size_bytes: size,
796            content_fingerprint: fp.into(),
797            content_md5: String::new(),
798            status: PartStatus::Committed,
799        }
800    }
801
802    fn build_manifest(parts: Vec<ManifestPart>, status: ManifestStatus) -> RunManifest {
803        let row_count: i64 = parts
804            .iter()
805            .filter(|p| p.status == PartStatus::Committed)
806            .map(|p| p.rows)
807            .sum();
808        let part_count = parts
809            .iter()
810            .filter(|p| p.status == PartStatus::Committed)
811            .count() as u32;
812        RunManifest {
813            mode: "batch".to_string(),
814            manifest_version: MANIFEST_VERSION,
815            run_id: "r".into(),
816            export_name: "public.orders".into(),
817            started_at: "2026-05-21T12:00:00Z".into(),
818            finished_at: "2026-05-21T12:01:00Z".into(),
819            status,
820            source: ManifestSource {
821                engine: "postgres".into(),
822                schema: Some("public".into()),
823                table: Some("orders".into()),
824                extraction: None,
825            },
826            destination: ManifestDestination {
827                kind: "local".into(),
828                uri: "file:///tmp/out".into(),
829            },
830            format: "parquet".into(),
831            compression: "zstd".into(),
832            schema_fingerprint: "xxh3:0123456789abcdef".into(),
833            row_count,
834            part_count,
835            parts,
836            column_checksums: None,
837            checksum_key_column: None,
838        }
839    }
840
841    /// Lay out a clean dataset with manifest + _SUCCESS at the root.
842    fn write_dataset(dir: &Path, m: &RunManifest, parts_with_bytes: &[(&str, &[u8])]) {
843        for (name, bytes) in parts_with_bytes {
844            std::fs::write(dir.join(name), bytes).unwrap();
845        }
846        let body = serde_json::to_vec_pretty(m).unwrap();
847        std::fs::write(dir.join(MANIFEST_FILENAME), &body).unwrap();
848        if matches!(m.status, ManifestStatus::Success) {
849            std::fs::write(dir.join(SUCCESS_FILENAME), success_marker_body(&body)).unwrap();
850        }
851    }
852
853    // ── happy path ───────────────────────────────────────────────────────
854
855    #[test]
856    fn happy_path_verifies_all_parts_and_success_marker() {
857        let dir = tempfile::tempdir().unwrap();
858        let m = build_manifest(
859            vec![
860                part(1, 10, 4, "xxh3:1111111111111111"),
861                part(2, 20, 5, "xxh3:2222222222222222"),
862            ],
863            ManifestStatus::Success,
864        );
865        write_dataset(
866            dir.path(),
867            &m,
868            &[
869                ("part-000001.parquet", b"AAAA"),
870                ("part-000002.parquet", b"BBBBB"),
871            ],
872        );
873        let dest = local_dest(dir.path());
874
875        let v = verify_at_destination(&dest, "", ValidateDepth::Full).unwrap();
876        assert!(v.manifest_found);
877        assert!(!v.legacy_run);
878        assert_eq!(v.parts_verified, 2);
879        assert_eq!(v.parts_failed, 0);
880        assert!(v.success_marker_consistent);
881        assert!(v.manifest_self_consistent);
882        assert!(v.passed);
883        assert!(v.failures.is_empty());
884    }
885
886    // ── M6 legacy run ───────────────────────────────────────────────────
887
888    #[test]
889    fn no_manifest_returns_legacy_run_label() {
890        // Empty prefix — no manifest, no parts.
891        let dir = tempfile::tempdir().unwrap();
892        let dest = local_dest(dir.path());
893        let v = verify_at_destination(&dest, "", ValidateDepth::Full).unwrap();
894        assert!(!v.manifest_found);
895        assert!(v.legacy_run);
896        assert_eq!(v.parts_verified, 0);
897        assert!(!v.passed);
898        assert!(v.failures.is_empty(), "no failures, just a legacy label");
899    }
900
901    // ── M5 part-presence failures ───────────────────────────────────────
902
903    #[test]
904    fn missing_part_is_flagged_with_part_id_and_path() {
905        let dir = tempfile::tempdir().unwrap();
906        let m = build_manifest(
907            vec![
908                part(1, 10, 4, "xxh3:1111111111111111"),
909                part(2, 20, 5, "xxh3:2222222222222222"),
910            ],
911            ManifestStatus::Success,
912        );
913        write_dataset(
914            dir.path(),
915            &m,
916            &[("part-000001.parquet", b"AAAA")], // part 2 missing
917        );
918        let dest = local_dest(dir.path());
919
920        let v = verify_at_destination(&dest, "", ValidateDepth::Full).unwrap();
921        assert_eq!(v.parts_verified, 1);
922        assert_eq!(v.parts_failed, 1);
923        assert!(!v.passed);
924        assert!(
925            v.failures
926                .iter()
927                .any(|f| matches!(f, Failure::PartMissing { part_id: 2, .. }))
928        );
929    }
930
931    #[test]
932    fn part_size_mismatch_is_flagged_with_expected_and_actual() {
933        let dir = tempfile::tempdir().unwrap();
934        let m = build_manifest(
935            vec![part(1, 10, 4, "xxh3:1111111111111111")],
936            ManifestStatus::Success,
937        );
938        // Manifest claims 4 bytes; we write 6.
939        write_dataset(dir.path(), &m, &[("part-000001.parquet", b"OOPSIE")]);
940        let dest = local_dest(dir.path());
941
942        let v = verify_at_destination(&dest, "", ValidateDepth::Full).unwrap();
943        assert!(!v.passed);
944        let mismatch = v
945            .failures
946            .iter()
947            .find_map(|f| match f {
948                Failure::PartSizeMismatch {
949                    part_id,
950                    expected,
951                    actual,
952                    ..
953                } => Some((*part_id, *expected, *actual)),
954                _ => None,
955            })
956            .expect("must surface the size mismatch");
957        assert_eq!(mismatch, (1, 4, 6));
958    }
959
960    // ── _SUCCESS marker integrity ───────────────────────────────────────
961
962    #[test]
963    fn stale_success_marker_is_flagged_as_inconsistent() {
964        // Write a manifest, then overwrite _SUCCESS with the marker for a
965        // *different* manifest body — simulating an orchestrator that
966        // mishandled a re-run.
967        let dir = tempfile::tempdir().unwrap();
968        let m = build_manifest(
969            vec![part(1, 10, 4, "xxh3:1111111111111111")],
970            ManifestStatus::Success,
971        );
972        write_dataset(dir.path(), &m, &[("part-000001.parquet", b"AAAA")]);
973        std::fs::write(
974            dir.path().join(SUCCESS_FILENAME),
975            success_marker_body(b"different manifest body"),
976        )
977        .unwrap();
978        let dest = local_dest(dir.path());
979
980        let v = verify_at_destination(&dest, "", ValidateDepth::Full).unwrap();
981        assert!(!v.success_marker_consistent);
982        assert!(!v.passed);
983        assert!(
984            v.failures
985                .iter()
986                .any(|f| matches!(f, Failure::SuccessMarkerStale { .. }))
987        );
988    }
989
990    #[test]
991    fn malformed_success_marker_body_is_flagged() {
992        let dir = tempfile::tempdir().unwrap();
993        let m = build_manifest(
994            vec![part(1, 10, 4, "xxh3:1111111111111111")],
995            ManifestStatus::Success,
996        );
997        write_dataset(dir.path(), &m, &[("part-000001.parquet", b"AAAA")]);
998        std::fs::write(dir.path().join(SUCCESS_FILENAME), b"not even xxh3 shaped").unwrap();
999        let dest = local_dest(dir.path());
1000
1001        let v = verify_at_destination(&dest, "", ValidateDepth::Full).unwrap();
1002        assert!(!v.passed);
1003        assert!(
1004            v.failures
1005                .iter()
1006                .any(|f| matches!(f, Failure::SuccessMarkerMalformed { .. }))
1007        );
1008    }
1009
1010    #[test]
1011    fn absent_success_marker_does_not_fail_validation_alone() {
1012        // ADR-0012 M2: only successful runs land _SUCCESS.  A failed-then-
1013        // rewritten manifest legitimately lacks one — verification must
1014        // not flip `passed` just for that.
1015        let dir = tempfile::tempdir().unwrap();
1016        let m = build_manifest(
1017            vec![part(1, 10, 4, "xxh3:1111111111111111")],
1018            ManifestStatus::Failed,
1019        );
1020        write_dataset(dir.path(), &m, &[("part-000001.parquet", b"AAAA")]);
1021        // Note: write_dataset only writes _SUCCESS for status == Success,
1022        // so no marker exists here.
1023        assert!(!dir.path().join(SUCCESS_FILENAME).exists());
1024        let dest = local_dest(dir.path());
1025
1026        let v = verify_at_destination(&dest, "", ValidateDepth::Full).unwrap();
1027        assert!(v.manifest_found);
1028        assert!(
1029            !v.success_marker_consistent,
1030            "no marker => false (no signal)"
1031        );
1032        // The parts still verified, so passed = true.
1033        assert!(v.passed);
1034        assert!(v.failures.is_empty());
1035    }
1036
1037    // ── self-consistency ────────────────────────────────────────────────
1038
1039    #[test]
1040    fn self_inconsistent_manifest_is_flagged_but_part_check_still_runs() {
1041        let dir = tempfile::tempdir().unwrap();
1042        let mut m = build_manifest(
1043            vec![part(1, 10, 4, "xxh3:1111111111111111")],
1044            ManifestStatus::Success,
1045        );
1046        m.row_count = 9999; // lie
1047
1048        let body = serde_json::to_vec_pretty(&m).unwrap();
1049        std::fs::write(dir.path().join("part-000001.parquet"), b"AAAA").unwrap();
1050        std::fs::write(dir.path().join(MANIFEST_FILENAME), &body).unwrap();
1051        std::fs::write(
1052            dir.path().join(SUCCESS_FILENAME),
1053            success_marker_body(&body),
1054        )
1055        .unwrap();
1056        let dest = local_dest(dir.path());
1057
1058        let v = verify_at_destination(&dest, "", ValidateDepth::Full).unwrap();
1059        assert!(v.manifest_found);
1060        assert!(!v.manifest_self_consistent);
1061        assert!(!v.passed);
1062        // Parts that are physically present still get their `parts_verified`
1063        // counter bumped — both signals are independently useful.
1064        assert_eq!(v.parts_verified, 1);
1065        assert!(
1066            v.failures
1067                .iter()
1068                .any(|f| matches!(f, Failure::ManifestSelfInconsistent { .. }))
1069        );
1070    }
1071
1072    // ── untracked objects ───────────────────────────────────────────────
1073
1074    #[test]
1075    fn untracked_object_under_prefix_is_flagged() {
1076        let dir = tempfile::tempdir().unwrap();
1077        let m = build_manifest(
1078            vec![part(1, 10, 4, "xxh3:1111111111111111")],
1079            ManifestStatus::Success,
1080        );
1081        write_dataset(dir.path(), &m, &[("part-000001.parquet", b"AAAA")]);
1082        std::fs::write(dir.path().join("rogue.parquet"), b"XX").unwrap();
1083        let dest = local_dest(dir.path());
1084
1085        let v = verify_at_destination(&dest, "", ValidateDepth::Full).unwrap();
1086        assert!(
1087            v.failures.iter().any(
1088                |f| matches!(f, Failure::UntrackedObject { key, .. } if key == "rogue.parquet")
1089            )
1090        );
1091        // Untracked objects are surfaced but do NOT flip `passed` — that
1092        // is the resume-side decision (M9).  Parts and marker are fine,
1093        // so passed remains true.
1094        assert!(v.passed);
1095    }
1096
1097    #[test]
1098    fn quarantine_prefix_objects_are_silently_ignored() {
1099        let dir = tempfile::tempdir().unwrap();
1100        let m = build_manifest(
1101            vec![part(1, 10, 4, "xxh3:1111111111111111")],
1102            ManifestStatus::Success,
1103        );
1104        write_dataset(dir.path(), &m, &[("part-000001.parquet", b"AAAA")]);
1105        std::fs::create_dir_all(dir.path().join(crate::manifest::QUARANTINE_PREFIX)).unwrap();
1106        std::fs::write(
1107            dir.path()
1108                .join(crate::manifest::QUARANTINE_PREFIX)
1109                .join("old.parquet"),
1110            b"OO",
1111        )
1112        .unwrap();
1113        let dest = local_dest(dir.path());
1114
1115        let v = verify_at_destination(&dest, "", ValidateDepth::Full).unwrap();
1116        assert!(v.passed);
1117        assert!(
1118            !v.failures
1119                .iter()
1120                .any(|f| matches!(f, Failure::UntrackedObject { .. })),
1121            "quarantine_prefix is the legitimate home for these — must not flag"
1122        );
1123    }
1124
1125    #[test]
1126    fn doctor_probe_is_not_flagged_as_untracked() {
1127        // Regression: `rivet doctor` writes `.rivet_doctor_probe` at the
1128        // destination prefix and never removes it.  A subsequent
1129        // `rivet run --validate` against the same prefix must treat it as a
1130        // Rivet sidecar, not foreign data — otherwise `has_failures()` trips
1131        // and the run's `validated` flag is downgraded to FAIL.
1132        let dir = tempfile::tempdir().unwrap();
1133        let m = build_manifest(
1134            vec![part(1, 10, 4, "xxh3:1111111111111111")],
1135            ManifestStatus::Success,
1136        );
1137        write_dataset(dir.path(), &m, &[("part-000001.parquet", b"AAAA")]);
1138        std::fs::write(
1139            dir.path().join(crate::manifest::DOCTOR_PROBE_FILENAME),
1140            b"ok",
1141        )
1142        .unwrap();
1143        let dest = local_dest(dir.path());
1144
1145        let v = verify_at_destination(&dest, "", ValidateDepth::Full).unwrap();
1146        assert!(
1147            !v.has_failures(),
1148            "doctor probe must not surface as a failure: {:?}",
1149            v.failures
1150        );
1151        assert!(v.passed);
1152    }
1153
1154    // ── manifest_dir join semantics ─────────────────────────────────────
1155
1156    #[test]
1157    fn verifies_in_subdirectory_when_manifest_dir_is_non_empty() {
1158        let outer = tempfile::tempdir().unwrap();
1159        std::fs::create_dir_all(outer.path().join("sub/run")).unwrap();
1160        let m = build_manifest(
1161            vec![part(1, 10, 4, "xxh3:1111111111111111")],
1162            ManifestStatus::Success,
1163        );
1164        let body = serde_json::to_vec_pretty(&m).unwrap();
1165        std::fs::write(outer.path().join("sub/run/part-000001.parquet"), b"AAAA").unwrap();
1166        std::fs::write(outer.path().join("sub/run").join(MANIFEST_FILENAME), &body).unwrap();
1167        std::fs::write(
1168            outer.path().join("sub/run").join(SUCCESS_FILENAME),
1169            success_marker_body(&body),
1170        )
1171        .unwrap();
1172        let dest = local_dest(outer.path());
1173
1174        let v = verify_at_destination(&dest, "sub/run", ValidateDepth::Full).unwrap();
1175        assert!(v.passed);
1176        assert_eq!(v.parts_verified, 1);
1177
1178        // Trailing slash is normalised — same outcome.
1179        let v2 = verify_at_destination(&dest, "sub/run/", ValidateDepth::Full).unwrap();
1180        assert!(v2.passed);
1181    }
1182
1183    // ── list-failure semantics (presence now rides the listing) ──────────
1184
1185    /// Wraps a real `LocalDestination` but fails every `list_prefix`. Used to
1186    /// pin the post-refactor contract: presence is derived from the listing,
1187    /// so a listing we cannot read means the audit cannot certify the parts.
1188    struct ListFails(LocalDestination);
1189    impl crate::destination::Destination for ListFails {
1190        fn write(&self, p: &Path, k: &str) -> Result<crate::destination::WriteOutcome> {
1191            self.0.write(p, k)
1192        }
1193        fn capabilities(&self) -> crate::destination::DestinationCapabilities {
1194            self.0.capabilities()
1195        }
1196        fn head(&self, k: &str) -> Result<Option<crate::destination::ObjectMeta>> {
1197            self.0.head(k)
1198        }
1199        fn read(&self, k: &str) -> Result<Vec<u8>> {
1200            self.0.read(k)
1201        }
1202        fn list_prefix(&self, _: &str) -> Result<Vec<crate::destination::ObjectMeta>> {
1203            anyhow::bail!("listing unavailable")
1204        }
1205    }
1206
1207    #[test]
1208    fn list_failure_cannot_certify_parts_and_fails_the_audit() {
1209        let dir = tempfile::tempdir().unwrap();
1210        let m = build_manifest(vec![part(0, 3, 3, "xxh3:0")], ManifestStatus::Success);
1211        write_dataset(dir.path(), &m, &[("part-000000.parquet", b"abc")]);
1212        let dest = ListFails(local_dest(dir.path()));
1213
1214        let v = verify_at_destination(&dest, "", ValidateDepth::Full).unwrap();
1215        // The manifest itself reads + parses fine (HEAD/read still work)…
1216        assert!(v.manifest_found);
1217        assert!(v.manifest_self_consistent);
1218        // …but with no listing we verify zero parts and refuse to pass.
1219        assert!(
1220            !v.passed,
1221            "an audit that cannot list the prefix must not pass"
1222        );
1223        assert_eq!(v.parts_verified, 0);
1224        assert!(
1225            v.failures
1226                .iter()
1227                .any(|f| matches!(f, Failure::ListPrefixError { .. })),
1228            "expected a ListPrefixError, got: {:?}",
1229            v.failures
1230        );
1231    }
1232
1233    // ── manifest read-error semantics (explicit failure, not legacy) ─────
1234
1235    /// Wraps a real `LocalDestination` but fails reading `manifest.json`
1236    /// (head still sees it) — EACCES or a transient store error on a
1237    /// manifest that exists.
1238    struct ManifestReadFails(LocalDestination);
1239    impl crate::destination::Destination for ManifestReadFails {
1240        fn write(&self, p: &Path, k: &str) -> Result<crate::destination::WriteOutcome> {
1241            self.0.write(p, k)
1242        }
1243        fn capabilities(&self) -> crate::destination::DestinationCapabilities {
1244            self.0.capabilities()
1245        }
1246        fn head(&self, k: &str) -> Result<Option<crate::destination::ObjectMeta>> {
1247            self.0.head(k)
1248        }
1249        fn read(&self, k: &str) -> Result<Vec<u8>> {
1250            if k.ends_with(MANIFEST_FILENAME) {
1251                anyhow::bail!("permission denied (simulated)")
1252            }
1253            self.0.read(k)
1254        }
1255        fn list_prefix(&self, p: &str) -> Result<Vec<crate::destination::ObjectMeta>> {
1256            self.0.list_prefix(p)
1257        }
1258    }
1259
1260    #[test]
1261    fn unreadable_manifest_is_explicit_failure_not_legacy() {
1262        // The exit gates (`rivet validate`, run finalize) key off this exact
1263        // shape: `manifest_found: false` but `has_failures()` — distinct
1264        // from M6 legacy (`legacy_run: true`, no failures, exit 0).
1265        let dir = tempfile::tempdir().unwrap();
1266        let m = build_manifest(
1267            vec![part(1, 10, 4, "xxh3:1111111111111111")],
1268            ManifestStatus::Success,
1269        );
1270        write_dataset(dir.path(), &m, &[("part-000001.parquet", b"AAAA")]);
1271        let dest = ManifestReadFails(local_dest(dir.path()));
1272
1273        let v = verify_at_destination(&dest, "", ValidateDepth::Full).unwrap();
1274        assert!(!v.manifest_found);
1275        assert!(!v.legacy_run, "a read error is not the M6 legacy label");
1276        assert!(!v.passed);
1277        assert!(v.has_failures(), "orchestrators need a reason to refuse");
1278        assert!(
1279            matches!(v.failures.as_slice(), [Failure::ManifestReadError { .. }]),
1280            "expected exactly one ManifestReadError, got: {:?}",
1281            v.failures
1282        );
1283    }
1284
1285    /// Same contract when `head` itself errors (cannot even stat the
1286    /// manifest): symmetric `ManifestReadError`, never the legacy label.
1287    struct ManifestHeadFails(LocalDestination);
1288    impl crate::destination::Destination for ManifestHeadFails {
1289        fn write(&self, p: &Path, k: &str) -> Result<crate::destination::WriteOutcome> {
1290            self.0.write(p, k)
1291        }
1292        fn capabilities(&self) -> crate::destination::DestinationCapabilities {
1293            self.0.capabilities()
1294        }
1295        fn head(&self, k: &str) -> Result<Option<crate::destination::ObjectMeta>> {
1296            if k.ends_with(MANIFEST_FILENAME) {
1297                anyhow::bail!("io timeout (simulated)")
1298            }
1299            self.0.head(k)
1300        }
1301        fn read(&self, k: &str) -> Result<Vec<u8>> {
1302            self.0.read(k)
1303        }
1304        fn list_prefix(&self, p: &str) -> Result<Vec<crate::destination::ObjectMeta>> {
1305            self.0.list_prefix(p)
1306        }
1307    }
1308
1309    #[test]
1310    fn manifest_head_error_is_explicit_failure_not_legacy() {
1311        let dir = tempfile::tempdir().unwrap();
1312        let m = build_manifest(
1313            vec![part(1, 10, 4, "xxh3:1111111111111111")],
1314            ManifestStatus::Success,
1315        );
1316        write_dataset(dir.path(), &m, &[("part-000001.parquet", b"AAAA")]);
1317        let dest = ManifestHeadFails(local_dest(dir.path()));
1318
1319        let v = verify_at_destination(&dest, "", ValidateDepth::Full).unwrap();
1320        assert!(!v.manifest_found);
1321        assert!(!v.legacy_run);
1322        assert!(!v.passed);
1323        assert!(
1324            matches!(
1325                v.failures.as_slice(),
1326                [Failure::ManifestReadError { detail }] if detail.contains("manifest head failed")
1327            ),
1328            "expected one ManifestReadError naming the head step, got: {:?}",
1329            v.failures
1330        );
1331    }
1332
1333    #[test]
1334    fn passed_is_derived_advisory_failures_do_not_fail() {
1335        // An advisory failure (untracked surplus) keeps the verdict passing…
1336        let mut v = ManifestVerification {
1337            manifest_found: true,
1338            ..ManifestVerification::empty()
1339        };
1340        v.failures.push(Failure::UntrackedObject {
1341            key: "stray.parquet".into(),
1342            size_bytes: 9,
1343        });
1344        v.recompute_passed();
1345        assert!(v.passed, "untracked surplus is advisory, not fatal");
1346
1347        // …while any fatal failure flips it.
1348        v.failures.push(Failure::PartMissing {
1349            part_id: 1,
1350            path: "part-000001.parquet".into(),
1351        });
1352        v.recompute_passed();
1353        assert!(!v.passed, "a missing part is fatal");
1354
1355        // No manifest → never passes regardless of failures.
1356        let mut legacy = ManifestVerification::empty();
1357        legacy.recompute_passed();
1358        assert!(!legacy.passed, "no manifest found → cannot certify");
1359    }
1360
1361    #[test]
1362    fn verify_content_policy_fails_only_size_only_parts() {
1363        // 3 parts, 2 content-checked, 1 size-only.
1364        let base = ManifestVerification {
1365            manifest_found: true,
1366            parts_verified: 3,
1367            parts_md5_verified: 2,
1368            ..ManifestVerification::empty()
1369        };
1370        // verify: size → size-only is acceptable, passes.
1371        let mut sz = base.clone();
1372        sz.recompute_passed();
1373        sz.enforce_content_policy(false);
1374        assert!(sz.passed, "size-only OK under verify: size");
1375
1376        // verify: content → the 1 size-only part is a fatal failure.
1377        let mut ct = base.clone();
1378        ct.recompute_passed();
1379        ct.enforce_content_policy(true);
1380        assert!(!ct.passed, "a size-only part fails verify: content");
1381        assert!(
1382            ct.failures.iter().any(|f| matches!(
1383                f,
1384                Failure::ContentVerificationUnmet {
1385                    size_only: 1,
1386                    total: 3
1387                }
1388            )),
1389            "expected ContentVerificationUnmet, got: {:?}",
1390            ct.failures
1391        );
1392
1393        // verify: content with every part md5-checked → satisfied.
1394        let mut all = ManifestVerification {
1395            parts_md5_verified: 3,
1396            ..base
1397        };
1398        all.recompute_passed();
1399        all.enforce_content_policy(true);
1400        assert!(
1401            all.passed && all.failures.is_empty(),
1402            "all md5 meets verify: content"
1403        );
1404    }
1405
1406    // ── require_manifest_present (finding #20: operator-pinned --prefix) ──────
1407
1408    #[test]
1409    fn require_manifest_escalates_legacy_to_fatal_absent() {
1410        // The exact shape `verify_at_destination` returns for an absent manifest
1411        // (`legacy()`): no manifest, no other failure. With a pinned `--prefix`
1412        // this is escalated to a fatal `ManifestRequiredButAbsent` so the exit
1413        // gate refuses it instead of passing as a benign legacy run.
1414        let mut v = ManifestVerification::legacy();
1415        assert!(v.legacy_run && !v.has_failures());
1416
1417        v.require_manifest_present("exports/2026-06-09/orders/");
1418
1419        assert!(!v.legacy_run, "no longer the benign legacy-run label");
1420        assert!(!v.passed, "an absent-but-required manifest cannot pass");
1421        assert!(
1422            matches!(
1423                v.failures.as_slice(),
1424                [Failure::ManifestRequiredButAbsent { prefix }]
1425                    if prefix == "exports/2026-06-09/orders/"
1426            ),
1427            "expected one ManifestRequiredButAbsent naming the prefix, got: {:?}",
1428            v.failures
1429        );
1430    }
1431
1432    #[test]
1433    fn require_manifest_is_noop_on_a_real_passing_manifest() {
1434        // A found, passing verdict is untouched — `--prefix` plus real data is
1435        // the normal "validate this exact prefix" case and must still pass.
1436        let mut v = ManifestVerification {
1437            manifest_found: true,
1438            manifest_self_consistent: true,
1439            parts_verified: 1,
1440            passed: true,
1441            ..ManifestVerification::empty()
1442        };
1443        v.require_manifest_present("exports/orders/");
1444        assert!(
1445            v.passed && v.failures.is_empty(),
1446            "real dataset still passes"
1447        );
1448    }
1449
1450    #[test]
1451    fn require_manifest_does_not_double_flag_a_read_error() {
1452        // An absent manifest that already carries a `ManifestReadError` (head /
1453        // read failed) is already a fatal, classified failure — requiring a
1454        // manifest here must not add a second, redundant failure.
1455        let mut v = ManifestVerification::legacy();
1456        v.legacy_run = false;
1457        v.failures.push(Failure::ManifestReadError {
1458            detail: "permission denied".into(),
1459        });
1460        v.recompute_passed();
1461
1462        v.require_manifest_present("exports/orders/");
1463
1464        assert!(
1465            matches!(v.failures.as_slice(), [Failure::ManifestReadError { .. }]),
1466            "must leave the existing read-error verdict alone, got: {:?}",
1467            v.failures
1468        );
1469    }
1470
1471    // ── graded verify layer (--depth) ───────────────────────────────────
1472
1473    #[test]
1474    fn light_depth_skips_part_reconcile_even_when_a_part_is_missing() {
1475        // A manifest declaring a part that is NOT on disk. At `Full`/`Sample`
1476        // this is a fatal `PartMissing`; at `Light` the `list_prefix` reconcile
1477        // is skipped entirely, so `parts_verified == 0`, no `ListPrefixError`,
1478        // and — with `_SUCCESS` consistent and the manifest self-consistent —
1479        // the verdict still passes. Light certifies the manifest + marker, not
1480        // the parts.
1481        let dir = tempfile::tempdir().unwrap();
1482        let m = build_manifest(
1483            vec![
1484                part(1, 10, 4, "xxh3:1111111111111111"),
1485                part(2, 20, 5, "xxh3:2222222222222222"),
1486            ],
1487            ManifestStatus::Success,
1488        );
1489        // Deliberately write NEITHER part — only manifest.json + _SUCCESS.
1490        write_dataset(dir.path(), &m, &[]);
1491        let dest = local_dest(dir.path());
1492
1493        let v = verify_at_destination(&dest, "", ValidateDepth::Light).unwrap();
1494        assert_eq!(v.depth_level, "light");
1495        assert_eq!(
1496            v.parts_verified, 0,
1497            "light skips the listing — no part is ever verified"
1498        );
1499        assert_eq!(
1500            v.parts_failed, 0,
1501            "no part reconcile means no part failures"
1502        );
1503        assert!(
1504            !v.failures.iter().any(|f| matches!(
1505                f,
1506                Failure::PartMissing { .. } | Failure::ListPrefixError { .. }
1507            )),
1508            "light must not surface part or list failures, got: {:?}",
1509            v.failures
1510        );
1511        assert!(
1512            v.success_marker_consistent,
1513            "_SUCCESS is still checked at light depth"
1514        );
1515        assert!(v.manifest_self_consistent);
1516        assert!(
1517            v.passed,
1518            "manifest + _SUCCESS are consistent, so a light pass certifies it"
1519        );
1520    }
1521
1522    #[test]
1523    fn light_depth_never_lists_so_a_list_failure_cannot_trip() {
1524        // Even with a destination whose `list_prefix` always errors, a light
1525        // pass succeeds: it never calls `list_prefix`, so no `ListPrefixError`.
1526        // This is the direct contrast to `list_failure_cannot_certify_parts…`
1527        // (which runs at Full and *does* fail on the list error).
1528        let dir = tempfile::tempdir().unwrap();
1529        let m = build_manifest(vec![part(0, 3, 3, "xxh3:0")], ManifestStatus::Success);
1530        write_dataset(dir.path(), &m, &[("part-000000.parquet", b"abc")]);
1531        let dest = ListFails(local_dest(dir.path()));
1532
1533        let v = verify_at_destination(&dest, "", ValidateDepth::Light).unwrap();
1534        assert_eq!(v.depth_level, "light");
1535        assert!(
1536            !v.failures
1537                .iter()
1538                .any(|f| matches!(f, Failure::ListPrefixError { .. })),
1539            "light never lists, so a failing list_prefix cannot surface, got: {:?}",
1540            v.failures
1541        );
1542        assert_eq!(v.parts_verified, 0);
1543        assert!(v.passed, "manifest + _SUCCESS consistent → light passes");
1544    }
1545
1546    #[test]
1547    fn sample_depth_runs_part_reconcile_like_full() {
1548        // `Sample` runs every section `verify_at_destination` owns (1-5) — the
1549        // Form B value re-read it skips lives in the *caller*, not here. So a
1550        // missing part is a fatal `PartMissing` at Sample, identical to Full.
1551        let dir = tempfile::tempdir().unwrap();
1552        let m = build_manifest(
1553            vec![
1554                part(1, 10, 4, "xxh3:1111111111111111"),
1555                part(2, 20, 5, "xxh3:2222222222222222"),
1556            ],
1557            ManifestStatus::Success,
1558        );
1559        write_dataset(dir.path(), &m, &[("part-000001.parquet", b"AAAA")]); // part 2 missing
1560        let dest = local_dest(dir.path());
1561
1562        let v = verify_at_destination(&dest, "", ValidateDepth::Sample).unwrap();
1563        assert_eq!(v.depth_level, "sample");
1564        assert_eq!(v.parts_verified, 1);
1565        assert_eq!(v.parts_failed, 1);
1566        assert!(!v.passed);
1567        assert!(
1568            v.failures
1569                .iter()
1570                .any(|f| matches!(f, Failure::PartMissing { part_id: 2, .. })),
1571            "sample reconciles parts just like full, got: {:?}",
1572            v.failures
1573        );
1574    }
1575
1576    #[test]
1577    fn depth_level_is_stamped_on_every_verdict_shape() {
1578        // The depth label rides the verdict even on the early-return shapes
1579        // (legacy / no manifest), so a consumer always sees how deep the pass
1580        // went.
1581        let dir = tempfile::tempdir().unwrap();
1582        let dest = local_dest(dir.path()); // empty prefix → legacy
1583        for depth in [
1584            ValidateDepth::Light,
1585            ValidateDepth::Sample,
1586            ValidateDepth::Full,
1587        ] {
1588            let v = verify_at_destination(&dest, "", depth).unwrap();
1589            assert!(v.legacy_run, "empty prefix is the legacy shape");
1590            assert_eq!(
1591                v.depth_level,
1592                depth.label(),
1593                "legacy verdict must still carry its depth label"
1594            );
1595        }
1596    }
1597
1598    #[test]
1599    fn error_code_is_stable_and_distinct_per_variant() {
1600        // Each variant maps to its documented `RIVET_VERIFY_*` code. A
1601        // regression guard: renaming a code is a silent break for any CI gate
1602        // keying off it.
1603        let cases: &[(Failure, &str)] = &[
1604            (
1605                Failure::PartMissing {
1606                    part_id: 1,
1607                    path: "p".into(),
1608                },
1609                "RIVET_VERIFY_PART_MISSING",
1610            ),
1611            (
1612                Failure::PartSizeMismatch {
1613                    part_id: 1,
1614                    path: "p".into(),
1615                    expected: 1,
1616                    actual: 2,
1617                },
1618                "RIVET_VERIFY_PART_SIZE_MISMATCH",
1619            ),
1620            (
1621                Failure::PartChecksumMismatch {
1622                    part_id: 1,
1623                    path: "p".into(),
1624                    expected: "a".into(),
1625                    actual: "b".into(),
1626                },
1627                "RIVET_VERIFY_PART_CHECKSUM_MISMATCH",
1628            ),
1629            (
1630                Failure::SuccessMarkerMalformed {
1631                    body_preview: "x".into(),
1632                },
1633                "RIVET_VERIFY_SUCCESS_MALFORMED",
1634            ),
1635            (
1636                Failure::SuccessMarkerStale {
1637                    marker_fingerprint: "a".into(),
1638                    manifest_fingerprint: "b".into(),
1639                },
1640                "RIVET_VERIFY_SUCCESS_STALE",
1641            ),
1642            (
1643                Failure::ManifestSelfInconsistent { detail: "d".into() },
1644                "RIVET_VERIFY_MANIFEST_INCONSISTENT",
1645            ),
1646            (
1647                Failure::ManifestReadError { detail: "d".into() },
1648                "RIVET_VERIFY_MANIFEST_READ_ERROR",
1649            ),
1650            (
1651                Failure::SuccessMarkerReadError { detail: "d".into() },
1652                "RIVET_VERIFY_SUCCESS_READ_ERROR",
1653            ),
1654            (
1655                Failure::ListPrefixError { detail: "d".into() },
1656                "RIVET_VERIFY_LIST_ERROR",
1657            ),
1658            (
1659                Failure::UntrackedObject {
1660                    key: "k".into(),
1661                    size_bytes: 1,
1662                },
1663                "RIVET_VERIFY_UNTRACKED_OBJECT",
1664            ),
1665            (
1666                Failure::ContentVerificationUnmet {
1667                    size_only: 1,
1668                    total: 2,
1669                },
1670                "RIVET_VERIFY_CONTENT_UNMET",
1671            ),
1672            (
1673                Failure::ManifestRequiredButAbsent { prefix: "p".into() },
1674                "RIVET_VERIFY_MANIFEST_REQUIRED",
1675            ),
1676        ];
1677        for (failure, code) in cases {
1678            assert_eq!(&failure.error_code(), code, "code for {failure:?}");
1679            assert!(
1680                failure.error_code().starts_with("RIVET_VERIFY_"),
1681                "every code shares the RIVET_VERIFY_ prefix"
1682            );
1683        }
1684    }
1685}