KiThe 0.3.22

A numerical suite for chemical kinetics and thermodynamics, combustion, heat and mass transfer,chemical equilibrium, chemical engeneering
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
//! Exportable immutable evidence for reproducing an accepted equilibrium run.
//!
//! A solver result intentionally does not retain a mutable request or a live
//! repository handle. This module therefore captures the exact resolved record
//! identities and the *effective* numerical policy beside the accepted result.
//! It does not claim to hash arbitrary JSON payloads: a reviewed data-release
//! label is optional until the repository owns a versioned payload manifest.

use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};

use crate::Thermodynamics::ChemEquilibrium::equilibrium_candidate_selection::EquilibriumCandidateSelectionReport;
use crate::Thermodynamics::ChemEquilibrium::phase_equilibrium_problem::PhaseEquilibriumInputKind;
use crate::Thermodynamics::ChemEquilibrium::phase_equilibrium_workflow::{
    EquilibriumSolveOptions, EquilibriumSolveOptionsSnapshot, ResolvedPhaseEquilibriumOutcome,
};
use crate::Thermodynamics::thermo_lib_api::ThermoCatalogConsistencyReport;

fn sha256_hex(bytes: &[u8]) -> String {
    use std::fmt::Write as _;

    let digest = Sha256::digest(bytes);
    let mut encoded = String::with_capacity(digest.len() * 2);
    for byte in digest {
        write!(&mut encoded, "{byte:02x}").expect("writing to String is infallible");
    }
    encoded
}

/// Current JSON-compatible capsule schema.
pub const EQUILIBRIUM_REPRODUCIBILITY_SCHEMA_VERSION: u32 = 2;
pub const THERMO_DATA_CONTENT_MANIFEST_SCHEMA_VERSION: u32 = 1;

/// A pinned content hash for one bundled input to thermo-catalog resolution.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ThermoDataAssetHash {
    pub path: String,
    pub sha256: String,
}

/// Versioned SHA-256 manifest for the bundled thermochemistry catalog inputs.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ThermoDataContentManifest {
    pub schema_version: u32,
    pub release_id: String,
    pub assets: Vec<ThermoDataAssetHash>,
}

/// Failure while loading or verifying the bundled thermo-data manifest.
#[derive(Debug)]
pub enum ThermoDataManifestError {
    Json(serde_json::Error),
    UnsupportedSchema(u32),
    Read {
        path: PathBuf,
        source: std::io::Error,
    },
    HashMismatch {
        path: String,
        expected: String,
        actual: String,
    },
}

impl fmt::Display for ThermoDataManifestError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Json(error) => write!(formatter, "invalid thermo-data manifest: {error}"),
            Self::UnsupportedSchema(version) => write!(
                formatter,
                "unsupported thermo-data manifest schema {version}"
            ),
            Self::Read { path, source } => {
                write!(
                    formatter,
                    "failed to read thermo-data asset {}: {source}",
                    path.display()
                )
            }
            Self::HashMismatch {
                path,
                expected,
                actual,
            } => write!(
                formatter,
                "thermo-data SHA-256 mismatch for {path}: expected {expected}, got {actual}"
            ),
        }
    }
}

impl std::error::Error for ThermoDataManifestError {}

impl ThermoDataContentManifest {
    /// Loads and verifies every asset in the checked-in manifest.
    pub fn load_and_verify_bundled() -> Result<Self, ThermoDataManifestError> {
        let manifest: Self = serde_json::from_str(include_str!(
            "frozen_reference/data/thermochemistry_content_manifest.json"
        ))
        .map_err(ThermoDataManifestError::Json)?;
        if manifest.schema_version != THERMO_DATA_CONTENT_MANIFEST_SCHEMA_VERSION {
            return Err(ThermoDataManifestError::UnsupportedSchema(
                manifest.schema_version,
            ));
        }
        let root = Path::new(env!("CARGO_MANIFEST_DIR"));
        for asset in &manifest.assets {
            let path = root.join(&asset.path);
            let bytes = fs::read(&path).map_err(|source| ThermoDataManifestError::Read {
                path: path.clone(),
                source,
            })?;
            let actual = sha256_hex(&bytes);
            if actual != asset.sha256 {
                return Err(ThermoDataManifestError::HashMismatch {
                    path: asset.path.clone(),
                    expected: asset.sha256.clone(),
                    actual,
                });
            }
        }
        Ok(manifest)
    }
}

/// Phase-stability mathematics represented by a reproducibility capsule.
///
/// The tag prevents a historical `driving_force` artifact from being read as
/// evidence for the canonical constrained TPD workflow.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PhaseStabilitySemantics {
    CanonicalTpdV1,
}

/// Stable symbolic representation of the physical input origin.
///
/// This is intentionally a capsule-local type so the JSON contract does not
/// depend on the internal bridge enum layout or derives.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum EquilibriumReproducibilityInputKind {
    ExplicitComposition,
    ElementInventory,
}

/// Typed failure while loading a reproducibility artifact.
#[derive(Debug)]
pub enum ReproducibilityCapsuleError {
    Json(serde_json::Error),
    UnsupportedSchema { found: Option<u64>, expected: u32 },
    ObsoleteStabilityField { field: String },
}

impl fmt::Display for ReproducibilityCapsuleError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Json(error) => write!(formatter, "invalid reproducibility JSON: {error}"),
            Self::UnsupportedSchema { found, expected } => write!(
                formatter,
                "unsupported equilibrium reproducibility schema {:?}; expected {expected}",
                found
            ),
            Self::ObsoleteStabilityField { field } => write!(
                formatter,
                "reproducibility capsule contains obsolete phase-stability field '{field}'"
            ),
        }
    }
}

impl std::error::Error for ReproducibilityCapsuleError {}

/// Immutable component-level identity of one selected thermochemistry record.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EquilibriumRecordIdentity {
    /// Collision-free `phase::substance` component label.
    pub component: String,
    /// Semantic phase name.
    pub phase: String,
    /// Bare substance name used for record lookup.
    pub substance: String,
    /// Thermochemistry library that supplied the record.
    pub library: String,
    /// Exact record key selected in that library.
    pub record_key: String,
    /// Lookup-priority label describing how the record was resolved.
    pub lookup_priority: String,
}

/// Immutable declaration of one resolved physical phase.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EquilibriumPhaseSpecSnapshot {
    /// Semantic phase name.
    pub phase: String,
    /// Physical state (`Gas`, `Liquid`, ...) selected for the phase.
    pub physical_state: String,
    /// Domain-level phase model label.
    pub model: String,
    /// Component labels belonging to this phase, in declared order.
    pub components: Vec<String>,
}

/// Read-only structural evidence for the repository used by a run.
///
/// This is a catalog-structure fingerprint, not a payload-content hash. A
/// production data release should additionally supply `data_release_label`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ThermoCatalogSnapshot {
    /// Stable structural fingerprint over all indexed/payload record identities.
    pub structure_fingerprint: u64,
    /// Number of index entries in the catalog.
    pub indexed_pair_count: usize,
    /// Number of unique index entries.
    pub unique_indexed_pair_count: usize,
    /// Number of payload entries.
    pub payload_pair_count: usize,
    /// Number of duplicated index pairs.
    pub duplicate_index_pair_count: usize,
    /// Number of indexed records without a matching payload.
    pub indexed_without_payload_count: usize,
    /// Number of payload records without a matching index.
    pub payload_without_index_count: usize,
    /// Whether the catalog is structurally consistent.
    pub consistent: bool,
}

/// Candidate-selection evidence when a top-level element query built the run.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EquilibriumCandidateSelectionSnapshot {
    /// Elements requested by the selection transaction.
    pub requested_elements: Vec<String>,
    /// Element-search mode label.
    pub element_mode: String,
    /// Library preference order used by selection.
    pub library_preference: Vec<String>,
    /// Optional physical-state filter applied during selection.
    pub physical_states: Option<Vec<String>>,
    /// Optional temperature-domain filter in K.
    pub temperature_range_kelvin: Option<(f64, f64)>,
    /// Optional cap on the number of selected candidates.
    pub max_candidates: Option<usize>,
    /// Selected candidate records in deterministic order.
    pub selected_records: Vec<EquilibriumCandidateRecordSnapshot>,
    /// Number of candidates rejected during selection.
    pub rejected_record_count: usize,
    /// Full deterministic rejection evidence retained for replay/audit.
    pub rejected_records: Vec<EquilibriumCandidateRejectionSnapshot>,
    /// Whether the candidate limit excluded otherwise eligible records.
    pub truncated: bool,
}

/// Portable rejection evidence for one candidate-selection record.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EquilibriumCandidateRejectionSnapshot {
    /// Bare substance name considered by the selector.
    pub substance: String,
    /// Library containing the considered record.
    pub library: String,
    /// Stable selector reason label.
    pub reason: String,
}

/// Candidate record identity retained before phase-plan construction.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EquilibriumCandidateRecordSnapshot {
    /// Bare substance name.
    pub substance: String,
    /// Thermochemistry library that supplied the record.
    pub library: String,
    /// Exact record key selected in that library.
    pub record_key: String,
    /// Optional physical state selected for this record.
    pub physical_state: Option<String>,
    /// Elements present in this record's composition.
    pub elements: Vec<String>,
    /// Temperature-support interval label reported by the library.
    pub temperature_support: String,
    /// Zero-based priority rank within the library preference order.
    pub library_rank: usize,
}

/// Portable metadata for one accepted fixed-`P,T` equilibrium result.
///
/// The capsule is intentionally descriptive, never executable: it has no
/// write path, no live closures, and no mutable repository. Consumers may
/// serialize it alongside a result, then reconstruct a fresh request through
/// the public phase/candidate APIs under the recorded data release.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EquilibriumReproducibilityCapsule {
    pub schema_version: u32,
    pub phase_stability_semantics: PhaseStabilitySemantics,
    /// Physical input origin. Optional for compatibility with older schema v2
    /// artifacts written before input provenance was exported.
    #[serde(default)]
    pub input_kind: Option<EquilibriumReproducibilityInputKind>,
    /// Full canonical element order paired by index with `canonical_b`.
    #[serde(default)]
    pub canonical_element_labels: Vec<String>,
    /// Physical conserved elemental totals, before numerical trace seeding.
    #[serde(default)]
    pub canonical_b: Vec<f64>,
    pub temperature_kelvin: f64,
    pub pressure_pa: f64,
    pub reference_pressure_pa: f64,
    pub layout_fingerprint: u64,
    /// Stable FNV-1a identity hash over the exact selected component records.
    pub selected_record_identity_fingerprint: u64,
    /// Effective NIST fallback policy label used by the run.
    pub nist_fallback_policy: String,
    /// Declared physical phases in canonical order.
    pub phases: Vec<EquilibriumPhaseSpecSnapshot>,
    /// Exact selected record identities consumed by the run.
    pub selected_records: Vec<EquilibriumRecordIdentity>,
    /// Effective numerical options snapshot submitted to the pipeline.
    pub solve_options: EquilibriumSolveOptionsSnapshot,
    /// Backend that accepted the final candidate.
    pub accepted_backend: String,
    /// Accepted residual L2 norm.
    pub residual_l2_norm: f64,
    /// Maximum absolute elemental-balance error of the accepted result.
    pub max_abs_element_balance_error: f64,
    /// Optional user/release-supplied payload manifest identifier.
    pub data_release_label: Option<String>,
    /// Repository structural evidence, when a live repository was used.
    pub catalog: Option<ThermoCatalogSnapshot>,
    /// Candidate-selection evidence, when an element query built the run.
    pub candidate_selection: Option<EquilibriumCandidateSelectionSnapshot>,
}

impl EquilibriumReproducibilityCapsule {
    /// Captures one accepted outcome and the exact options clone submitted to
    /// the pipeline. Pipeline requests consume their options by design, so
    /// callers who need a capsule should keep an inexpensive clone.
    pub fn from_outcome(
        outcome: &ResolvedPhaseEquilibriumOutcome,
        solve_options: &EquilibriumSolveOptions,
    ) -> Self {
        let solution = outcome.solution();
        let presentation = crate::Thermodynamics::ChemEquilibrium::equilibrium_presentation::
            EquilibriumPresentationReport::from_solution(solution);
        let selected_records = presentation
            .components
            .into_iter()
            .map(|component| EquilibriumRecordIdentity {
                component: component.component,
                phase: component.phase,
                substance: component.substance,
                library: component.library,
                record_key: component.record_key,
                lookup_priority: component.lookup_priority,
            })
            .collect::<Vec<_>>();
        let phases = outcome
            .resolved()
            .phase_specs()
            .iter()
            .map(|phase| EquilibriumPhaseSpecSnapshot {
                phase: phase
                    .id()
                    .as_option()
                    .clone()
                    .unwrap_or_else(|| "single".to_string()),
                physical_state: format!("{:?}", phase.physical_state()),
                model: format!("{:?}", phase.model()),
                components: phase.components().to_vec(),
            })
            .collect();
        let validation = solution.accepted_solution().validation();
        let conditions = solution.conditions();
        let build_report = solution.build_report();
        let input_kind = Some(match build_report.input_kind() {
            PhaseEquilibriumInputKind::ExplicitComposition => {
                EquilibriumReproducibilityInputKind::ExplicitComposition
            }
            PhaseEquilibriumInputKind::ElementInventory => {
                EquilibriumReproducibilityInputKind::ElementInventory
            }
        });
        Self {
            schema_version: EQUILIBRIUM_REPRODUCIBILITY_SCHEMA_VERSION,
            phase_stability_semantics: PhaseStabilitySemantics::CanonicalTpdV1,
            input_kind,
            canonical_element_labels: build_report.element_labels().to_vec(),
            canonical_b: build_report.element_totals().to_vec(),
            temperature_kelvin: conditions.temperature(),
            pressure_pa: conditions.pressure(),
            reference_pressure_pa: conditions.reference_pressure(),
            layout_fingerprint: solution.metadata().layout_fingerprint(),
            selected_record_identity_fingerprint: selected_record_fingerprint(&selected_records),
            nist_fallback_policy: format!("{:?}", outcome.lookup_report().nist_fallback_policy()),
            phases,
            selected_records,
            solve_options: solve_options.reproducibility_snapshot(),
            accepted_backend: format!("{:?}", solution.solve_report().accepted_backend),
            residual_l2_norm: validation.residual_l2_norm,
            max_abs_element_balance_error: validation.max_abs_element_balance_error,
            data_release_label: None,
            catalog: None,
            candidate_selection: build_report
                .candidate_selection()
                .map(EquilibriumCandidateSelectionSnapshot::from_report),
        }
    }

    /// Attaches a reviewed immutable data-release label chosen by the caller.
    pub fn with_data_release_label(mut self, label: impl Into<String>) -> Self {
        let label = label.into();
        self.data_release_label = (!label.trim().is_empty()).then_some(label);
        self
    }

    /// Attaches repository structural evidence without performing I/O.
    pub fn with_catalog_consistency(mut self, report: &ThermoCatalogConsistencyReport) -> Self {
        self.catalog = Some(ThermoCatalogSnapshot::from_report(report));
        self
    }

    /// Attaches the selection evidence when the input came from an element
    /// query. Explicit `PhaseSpec` callers simply leave this field absent.
    pub fn with_candidate_selection(
        mut self,
        selection: &EquilibriumCandidateSelectionReport,
    ) -> Self {
        self.candidate_selection = Some(EquilibriumCandidateSelectionSnapshot::from_report(
            selection,
        ));
        self
    }

    /// Produces stable, human-readable JSON for a sidecar result artifact.
    pub fn to_pretty_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(self)
    }

    /// Loads only the current capsule contract.
    ///
    /// Replaying an old physical score under the new TPD name is worse than a
    /// hard error: it would create a plausible but physically ambiguous audit
    /// trail. Migrations must therefore be explicit application-level work.
    pub fn from_json(json: &str) -> Result<Self, ReproducibilityCapsuleError> {
        let value: serde_json::Value =
            serde_json::from_str(json).map_err(ReproducibilityCapsuleError::Json)?;
        let found = value
            .get("schema_version")
            .and_then(serde_json::Value::as_u64);
        if found != Some(u64::from(EQUILIBRIUM_REPRODUCIBILITY_SCHEMA_VERSION)) {
            return Err(ReproducibilityCapsuleError::UnsupportedSchema {
                found,
                expected: EQUILIBRIUM_REPRODUCIBILITY_SCHEMA_VERSION,
            });
        }
        if let Some(field) = find_obsolete_stability_field(&value) {
            return Err(ReproducibilityCapsuleError::ObsoleteStabilityField { field });
        }
        serde_json::from_value(value).map_err(ReproducibilityCapsuleError::Json)
    }
}

fn find_obsolete_stability_field(value: &serde_json::Value) -> Option<String> {
    const OBSOLETE: &[&str] = &[
        "driving_force",
        "phase_stability_model",
        "PhaseStabilityModel",
    ];
    match value {
        serde_json::Value::Object(map) => map.iter().find_map(|(key, value)| {
            OBSOLETE
                .contains(&key.as_str())
                .then(|| key.clone())
                .or_else(|| find_obsolete_stability_field(value))
        }),
        serde_json::Value::Array(values) => values.iter().find_map(find_obsolete_stability_field),
        _ => None,
    }
}

impl ThermoCatalogSnapshot {
    /// Projects a repository consistency report into an immutable snapshot.
    ///
    /// Derives a stable structural fingerprint from the indexed/unique/payload
    /// counts plus every duplicate, missing, and orphan record identity, then
    /// retains the discrete counts and the consistency flag. No repository I/O
    /// is performed; the snapshot only captures already-collected evidence.
    fn from_report(report: &ThermoCatalogConsistencyReport) -> Self {
        let mut identities = Vec::new();
        identities.push(format!("indexed={}", report.indexed_pair_count()));
        identities.push(format!("unique={}", report.unique_indexed_pair_count()));
        identities.push(format!("payload={}", report.payload_pair_count()));
        identities.extend(
            report
                .duplicate_index_pairs()
                .iter()
                .map(|(library, substance)| format!("duplicate:{library}:{substance}")),
        );
        identities.extend(
            report
                .indexed_without_payload()
                .iter()
                .map(|(library, substance)| format!("missing:{library}:{substance}")),
        );
        identities.extend(
            report
                .payload_without_index()
                .iter()
                .map(|(library, substance)| format!("orphan:{library}:{substance}")),
        );
        Self {
            structure_fingerprint: stable_fingerprint(identities.iter().map(String::as_str)),
            indexed_pair_count: report.indexed_pair_count(),
            unique_indexed_pair_count: report.unique_indexed_pair_count(),
            payload_pair_count: report.payload_pair_count(),
            duplicate_index_pair_count: report.duplicate_index_pairs().len(),
            indexed_without_payload_count: report.indexed_without_payload().len(),
            payload_without_index_count: report.payload_without_index().len(),
            consistent: report.is_consistent(),
        }
    }
}

impl EquilibriumCandidateSelectionSnapshot {
    /// Projects an auditable candidate-selection report into an immutable
    /// snapshot for reproducibility.
    ///
    /// Captures the effective selection policy (element mode, library
    /// preference, physical states, temperature range, candidate cap) and the
    /// selected record identities together with the rejected-record count. This
    /// preserves exactly how records were chosen without holding a live
    /// repository handle.
    fn from_report(report: &EquilibriumCandidateSelectionReport) -> Self {
        let policy = report.policy();
        Self {
            requested_elements: report.requested_elements().to_vec(),
            element_mode: format!("{:?}", policy.element_mode()),
            library_preference: policy.library_preference().to_vec(),
            physical_states: policy
                .physical_states()
                .map(|states| states.iter().map(|state| format!("{state:?}")).collect()),
            temperature_range_kelvin: policy
                .temperature_range()
                .map(|range| (range.lower(), range.upper())),
            max_candidates: policy.max_candidates(),
            selected_records: report
                .selected()
                .iter()
                .map(|candidate| EquilibriumCandidateRecordSnapshot {
                    substance: candidate.substance().to_string(),
                    library: candidate.library().to_string(),
                    record_key: candidate.record_key().to_string(),
                    physical_state: candidate.physical_state().map(|state| format!("{state:?}")),
                    elements: candidate.elements().to_vec(),
                    temperature_support: format!("{:?}", candidate.temperature_support()),
                    library_rank: candidate.library_rank(),
                })
                .collect(),
            rejected_record_count: report.rejected().len(),
            rejected_records: report
                .rejected()
                .iter()
                .map(|rejection| EquilibriumCandidateRejectionSnapshot {
                    substance: rejection.substance().to_string(),
                    library: rejection.library().to_string(),
                    reason: format!("{:?}", rejection.reason()),
                })
                .collect(),
            truncated: report.is_truncated(),
        }
    }
}

fn selected_record_fingerprint(records: &[EquilibriumRecordIdentity]) -> u64 {
    stable_fingerprint(records.iter().flat_map(|record| {
        [
            record.component.as_str(),
            record.phase.as_str(),
            record.substance.as_str(),
            record.library.as_str(),
            record.record_key.as_str(),
            record.lookup_priority.as_str(),
        ]
    }))
}

fn stable_fingerprint<'a>(parts: impl IntoIterator<Item = &'a str>) -> u64 {
    parts
        .into_iter()
        .flat_map(|part| part.bytes().chain(std::iter::once(0xff)))
        .fold(0xcbf2_9ce4_8422_2325_u64, |hash, byte| {
            (hash ^ u64::from(byte)).wrapping_mul(0x0000_0100_0000_01b3)
        })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Thermodynamics::ChemEquilibrium::equilibrium_element_inventory::ElementInventory;
    use crate::Thermodynamics::ChemEquilibrium::equilibrium_log_moles::Solvers;
    use crate::Thermodynamics::ChemEquilibrium::equilibrium_problem::EquilibriumConditions;
    use crate::Thermodynamics::ChemEquilibrium::equilibrium_solver_policy::{
        SolverBackend, SolverPolicy,
    };
    use crate::Thermodynamics::ChemEquilibrium::phase_equilibrium_workflow::PhaseEquilibriumPipelineRequest;
    use crate::Thermodynamics::User_PhaseOrSolution::{
        SubstanceSystemSpecBuilder, SubstancesContainer,
    };
    use crate::Thermodynamics::thermo_lib_api::ThermoData;

    #[test]
    fn bundled_thermochemistry_content_manifest_matches_checked_in_data() {
        let manifest = ThermoDataContentManifest::load_and_verify_bundled()
            .expect("bundled thermo-data files must match their pinned SHA-256 manifest");
        assert_eq!(
            manifest.schema_version,
            THERMO_DATA_CONTENT_MANIFEST_SCHEMA_VERSION
        );
        assert_eq!(manifest.assets.len(), 6);
        assert!(manifest.release_id.starts_with("kithe-bundled-thermo-"));
    }

    #[test]
    fn local_outcome_exports_stable_policy_provenance_and_catalog_evidence() {
        let repository = ThermoData::try_default_repository().unwrap();
        let spec = SubstanceSystemSpecBuilder::new(SubstancesContainer::SinglePhase(vec![
            "N2".to_string(),
            "O2".to_string(),
        ]))
        .with_library_priorities(vec!["NASA_gas".to_string()])
        .with_search_in_nist(false)
        .build()
        .unwrap();
        let options = EquilibriumSolveOptions::new()
            .with_solver_policy(SolverPolicy::Single(SolverBackend::Native(Solvers::NR)))
            .unwrap();
        let outcome = PhaseEquilibriumPipelineRequest::new(
            spec,
            vec![0.79, 0.21],
            EquilibriumConditions::new(500.0, 101_325.0, 101_325.0).unwrap(),
        )
        .with_repository(repository.clone())
        .with_solve_options(options.clone())
        .solve()
        .unwrap();

        let capsule = EquilibriumReproducibilityCapsule::from_outcome(&outcome, &options)
            .with_data_release_label("bundled-local-test-data")
            .with_catalog_consistency(&repository.consistency_report());
        let repeated = EquilibriumReproducibilityCapsule::from_outcome(&outcome, &options)
            .with_data_release_label("bundled-local-test-data")
            .with_catalog_consistency(&repository.consistency_report());
        assert_eq!(capsule, repeated);
        assert_eq!(
            capsule.schema_version,
            EQUILIBRIUM_REPRODUCIBILITY_SCHEMA_VERSION
        );
        assert_eq!(capsule.selected_records.len(), 2);
        assert_eq!(
            capsule.input_kind,
            Some(EquilibriumReproducibilityInputKind::ExplicitComposition)
        );
        assert_eq!(capsule.canonical_element_labels, ["N", "O"]);
        assert_eq!(capsule.canonical_b, [1.58, 0.42]);
        assert_eq!(
            capsule.solve_options.effective_backend_order,
            vec!["Native(NR)"]
        );
        assert!(capsule.selected_record_identity_fingerprint != 0);
        assert!(capsule.catalog.is_some());
        let json = capsule.to_pretty_json().unwrap();
        assert!(json.contains("NASA_gas"));
        assert!(json.contains("bundled-local-test-data"));
        assert_eq!(
            EquilibriumReproducibilityCapsule::from_json(&json).unwrap(),
            capsule
        );

        // Phase models are stored by their explicit symbolic names, not by a
        // fragile enum ordinal. Adding `IdealSolution` therefore does not
        // reinterpret existing `IdealGas`/`PureCondensed` artifacts or force
        // a schema bump by itself.
        let mut ideal_solution_named = capsule;
        ideal_solution_named.phases[0].model = "IdealSolution".to_string();
        let round_trip = EquilibriumReproducibilityCapsule::from_json(
            &ideal_solution_named.to_pretty_json().unwrap(),
        )
        .unwrap();
        assert_eq!(round_trip.phases[0].model, "IdealSolution");
        assert_eq!(
            round_trip.phase_stability_semantics,
            PhaseStabilitySemantics::CanonicalTpdV1
        );
    }

    #[test]
    fn elemental_outcome_exports_input_kind_and_canonical_b() {
        let repository = ThermoData::try_default_repository().unwrap();
        let spec = SubstanceSystemSpecBuilder::new(SubstancesContainer::SinglePhase(vec![
            "H2".to_string(),
            "O2".to_string(),
            "H2O".to_string(),
        ]))
        .with_library_priorities(vec!["NASA_gas".to_string()])
        .with_search_in_nist(false)
        .build()
        .unwrap();
        let options = EquilibriumSolveOptions::new()
            .with_solver_policy(SolverPolicy::Single(SolverBackend::Native(Solvers::NR)))
            .unwrap();
        let outcome = PhaseEquilibriumPipelineRequest::from_element_inventory(
            spec,
            ElementInventory::from_amounts([("H", 4.0), ("O", 2.0)]).unwrap(),
            EquilibriumConditions::new(1_200.0, 101_325.0, 101_325.0).unwrap(),
        )
        .with_repository(repository)
        .with_solve_options(options.clone())
        .solve()
        .unwrap();

        let capsule = EquilibriumReproducibilityCapsule::from_outcome(&outcome, &options);
        assert_eq!(
            capsule.input_kind,
            Some(EquilibriumReproducibilityInputKind::ElementInventory)
        );
        assert_eq!(capsule.canonical_element_labels, ["H", "O"]);
        assert_eq!(capsule.canonical_b, [4.0, 2.0]);
        assert!(capsule.candidate_selection.is_none());

        let json = capsule.to_pretty_json().unwrap();
        assert!(json.contains("ElementInventory"));
        assert!(json.contains("canonical_b"));
        assert_eq!(
            EquilibriumReproducibilityCapsule::from_json(&json).unwrap(),
            capsule
        );

        let mut legacy_value = serde_json::to_value(&capsule).unwrap();
        let legacy_object = legacy_value.as_object_mut().unwrap();
        legacy_object.remove("input_kind");
        legacy_object.remove("canonical_element_labels");
        legacy_object.remove("canonical_b");
        let legacy = EquilibriumReproducibilityCapsule::from_json(
            &serde_json::to_string(&legacy_value).unwrap(),
        )
        .unwrap();
        assert_eq!(legacy.input_kind, None);
        assert!(legacy.canonical_element_labels.is_empty());
        assert!(legacy.canonical_b.is_empty());
    }

    #[test]
    fn reproducibility_loader_rejects_old_schema_and_obsolete_stability_fields() {
        let old_schema = serde_json::json!({ "schema_version": 1 });
        assert!(matches!(
            EquilibriumReproducibilityCapsule::from_json(&old_schema.to_string()),
            Err(ReproducibilityCapsuleError::UnsupportedSchema { found: Some(1), .. })
        ));

        let obsolete_field = serde_json::json!({
            "schema_version": EQUILIBRIUM_REPRODUCIBILITY_SCHEMA_VERSION,
            "phase_stability": { "driving_force": -1.0 }
        });
        assert!(matches!(
            EquilibriumReproducibilityCapsule::from_json(&obsolete_field.to_string()),
            Err(ReproducibilityCapsuleError::ObsoleteStabilityField { field }) if field == "driving_force"
        ));
    }

    #[test]
    fn options_snapshot_expands_the_implicit_production_cascade() {
        let snapshot = EquilibriumSolveOptions::new().reproducibility_snapshot();
        assert!(
            snapshot
                .effective_backend_order
                .iter()
                .any(|backend| backend.contains("RustedSciThe"))
        );
        assert!(
            snapshot
                .effective_backend_order
                .iter()
                .any(|backend| backend.contains("Native"))
        );
        assert_eq!(snapshot.timing_mode, "Disabled");
        assert!(!snapshot.execution_control_attached);
    }
}