pleiades-data 0.5.3

Packaged offline ephemeris data (precomputed Sun and Moon positions, derived from JPL public-domain ephemerides) and its EphemerisBackend for the pleiades astrology workspace.
Documentation
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
use super::*;

/// Release-state for the packaged-artifact target thresholds.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PackagedArtifactTargetThresholdState {
    /// Calibrated fit envelope is recorded, but production thresholds are not yet release-ready.
    Draft,
    /// Production thresholds have been finalized for the current profile.
    ProductionReady,
}

/// Validation error for a packaged-artifact target-threshold state that is not release-ready.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PackagedArtifactTargetThresholdStateValidationError {
    /// The target-threshold state is still draft.
    Draft,
}

impl fmt::Display for PackagedArtifactTargetThresholdStateValidationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Draft => write!(
                f,
                "the packaged-artifact target-threshold state is draft; production thresholds are not yet release-ready"
            ),
        }
    }
}

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

impl PackagedArtifactTargetThresholdState {
    /// Returns the compact label used in release-facing summaries.
    pub const fn label(self) -> &'static str {
        match self {
            Self::Draft => {
                "calibrated fit envelope recorded; production thresholds not yet release-ready"
            }
            Self::ProductionReady => "production thresholds recorded",
        }
    }

    /// Returns a compact human-readable line for the target-threshold state.
    pub fn summary_line(self) -> String {
        format!("target-threshold state: {}", self)
    }

    /// Returns whether the target thresholds are finalized for production release.
    pub const fn is_production_ready(self) -> bool {
        matches!(self, Self::ProductionReady)
    }

    /// Returns `Ok(())` when the state is release-ready.
    pub fn validate_production_ready(
        self,
    ) -> Result<(), PackagedArtifactTargetThresholdStateValidationError> {
        if self.is_production_ready() {
            Ok(())
        } else {
            Err(PackagedArtifactTargetThresholdStateValidationError::Draft)
        }
    }

    /// Returns the validated target-threshold state as a compact human-readable line.
    pub fn validated_summary_line(
        self,
    ) -> Result<String, PackagedArtifactTargetThresholdStateValidationError> {
        self.validate_production_ready()?;
        Ok(self.summary_line())
    }
}

impl fmt::Display for PackagedArtifactTargetThresholdState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.label())
    }
}

const PACKAGED_ARTIFACT_TARGET_THRESHOLD_STATE: PackagedArtifactTargetThresholdState =
    PackagedArtifactTargetThresholdState::ProductionReady;
pub(crate) const PACKAGED_ARTIFACT_TARGET_THRESHOLD_SCOPES: &[&str] = &[
    "luminaries",
    "major planets",
    "pluto",
    "lunar points",
    "selected asteroids",
    "custom bodies",
];

/// Phase-2 corpus evidence used to keep the packaged-artifact threshold policy aligned
/// with the current reference, fixture-exactness, comparison, hold-out, selected-asteroid, boundary-overlay, and production-generation corpora.
#[derive(Clone, Debug, PartialEq)]
pub struct PackagedArtifactPhase2CorpusAlignmentSummary {
    /// Source-material evidence from the checked-in reference snapshot.
    pub reference_snapshot_source: pleiades_jpl::ReferenceSnapshotSourceSummary,
    /// Body-class coverage evidence from the checked-in reference snapshot.
    pub reference_snapshot: pleiades_jpl::ReferenceSnapshotBodyClassCoverageSummary,
    /// Exact J2000 fixture-exactness evidence from the checked-in reference snapshot.
    pub reference_snapshot_exact_j2000: pleiades_jpl::ReferenceSnapshotExactJ2000EvidenceSummary,
    /// Source-material evidence from the checked-in comparison snapshot.
    pub comparison_snapshot_source: pleiades_jpl::ComparisonSnapshotSourceSummary,
    /// Body-class coverage evidence from the checked-in comparison snapshot.
    pub comparison_snapshot: pleiades_jpl::ComparisonSnapshotBodyClassCoverageSummary,
    /// Source-material evidence from the checked-in independent hold-out snapshot.
    pub independent_holdout_source: pleiades_jpl::IndependentHoldoutSourceSummary,
    /// Body-class coverage evidence from the checked-in independent hold-out snapshot.
    pub independent_holdout: pleiades_jpl::IndependentHoldoutSnapshotBodyClassCoverageSummary,
    /// Source-backed evidence for the selected-asteroid validation corpus.
    pub selected_asteroid_source: pleiades_jpl::SelectedAsteroidSourceSummary,
    /// Source-backed window evidence for the selected-asteroid validation corpus.
    pub selected_asteroid_source_windows: pleiades_jpl::SelectedAsteroidSourceWindowSummary,
    /// Checked-in request-corpus evidence for the selected-asteroid validation corpus in the ecliptic frame.
    pub selected_asteroid_source_request_corpus:
        pleiades_jpl::SelectedAsteroidSourceRequestCorpusSummary,
    /// Checked-in request-corpus evidence for the selected-asteroid validation corpus in the equatorial frame.
    pub selected_asteroid_source_request_corpus_equatorial:
        pleiades_jpl::SelectedAsteroidSourceRequestCorpusSummary,
    /// Source-material evidence for the checked-in production-generation boundary overlay.
    pub production_generation_boundary_source: pleiades_jpl::IndependentHoldoutSourceSummary,
    /// Body-class coverage evidence for the checked-in production-generation corpus.
    pub production_generation_body_class_coverage:
        pleiades_jpl::ProductionGenerationSnapshotBodyClassCoverageSummary,
    /// Combined provenance for the checked-in production-generation corpus.
    pub production_generation_source: ProductionGenerationSourceSummary,
}

/// Validation error for a phase-2 corpus alignment summary that drifted from the current posture.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PackagedArtifactPhase2CorpusAlignmentSummaryValidationError {
    /// A summary field is out of sync with the current packaged-artifact posture.
    FieldOutOfSync { field: &'static str },
}

impl fmt::Display for PackagedArtifactPhase2CorpusAlignmentSummaryValidationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::FieldOutOfSync { field } => write!(
                f,
                "the packaged artifact phase-2 corpus alignment summary field `{field}` is out of sync with the current posture"
            ),
        }
    }
}

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

fn phase2_corpus_alignment_validation_field_path(field: &'static str) -> &'static str {
    match field {
        "reference_snapshot_source" => "phase2_corpus_alignment.reference_snapshot_source",
        "reference_snapshot" => "phase2_corpus_alignment.reference_snapshot",
        "reference_snapshot_exact_j2000" => {
            "phase2_corpus_alignment.reference_snapshot_exact_j2000"
        }
        "comparison_snapshot_source" => "phase2_corpus_alignment.comparison_snapshot_source",
        "comparison_snapshot" => "phase2_corpus_alignment.comparison_snapshot",
        "independent_holdout_source" => "phase2_corpus_alignment.independent_holdout_source",
        "independent_holdout" => "phase2_corpus_alignment.independent_holdout",
        "selected_asteroid_source" => "phase2_corpus_alignment.selected_asteroid_source",
        "selected_asteroid_source_windows" => {
            "phase2_corpus_alignment.selected_asteroid_source_windows"
        }
        "selected_asteroid_source_request_corpus" => {
            "phase2_corpus_alignment.selected_asteroid_source_request_corpus"
        }
        "selected_asteroid_source_request_corpus_equatorial" => {
            "phase2_corpus_alignment.selected_asteroid_source_request_corpus_equatorial"
        }
        "production_generation_boundary_source" => {
            "phase2_corpus_alignment.production_generation_boundary_source"
        }
        "production_generation_body_class_coverage" => {
            "phase2_corpus_alignment.production_generation_body_class_coverage"
        }
        "production_generation_source" => "phase2_corpus_alignment.production_generation_source",
        _ => "phase2_corpus_alignment",
    }
}

impl PackagedArtifactPhase2CorpusAlignmentSummary {
    /// Returns the phase-2 corpus alignment posture as a compact human-readable line.
    pub fn summary_line(&self) -> String {
        format!(
            "reference source={}; reference snapshot={}; reference exact J2000 evidence={}; comparison source={}; comparison snapshot={}; independent hold-out source={}; independent hold-out={}; selected asteroid source evidence={}; selected asteroid source windows={}; selected asteroid source request corpus={}; selected asteroid source request corpus equatorial={}; production generation boundary source={}; production generation body-class coverage={}; production generation source={}",
            self.reference_snapshot_source.summary_line(),
            self.reference_snapshot.summary_line(),
            self.reference_snapshot_exact_j2000.summary_line(),
            self.comparison_snapshot_source.summary_line(),
            self.comparison_snapshot.summary_line(),
            self.independent_holdout_source.summary_line(),
            self.independent_holdout.summary_line(),
            self.selected_asteroid_source.summary_line(),
            self.selected_asteroid_source_windows.summary_line(),
            self.selected_asteroid_source_request_corpus.summary_line(),
            self.selected_asteroid_source_request_corpus_equatorial.summary_line(),
            pleiades_jpl::format_production_generation_boundary_source_summary(
                &self.production_generation_boundary_source,
            ),
            self.production_generation_body_class_coverage.summary_line(),
            self.production_generation_source.summary_line(),
        )
    }

    /// Returns `Ok(())` when the phase-2 corpus evidence still matches the current packaged-artifact posture.
    pub fn validate(
        &self,
    ) -> Result<(), PackagedArtifactPhase2CorpusAlignmentSummaryValidationError> {
        let Some(expected) = packaged_artifact_phase2_corpus_alignment_summary_details() else {
            return Err(
                PackagedArtifactPhase2CorpusAlignmentSummaryValidationError::FieldOutOfSync {
                    field: "phase2_corpus_alignment",
                },
            );
        };

        let field_out_of_sync = |field| {
            PackagedArtifactPhase2CorpusAlignmentSummaryValidationError::FieldOutOfSync { field }
        };

        if self.reference_snapshot_source != expected.reference_snapshot_source {
            return Err(field_out_of_sync("reference_snapshot_source"));
        }
        self.reference_snapshot_source
            .validate()
            .map_err(|_| field_out_of_sync("reference_snapshot_source"))?;

        if self.reference_snapshot != expected.reference_snapshot {
            return Err(field_out_of_sync("reference_snapshot"));
        }
        self.reference_snapshot
            .validate()
            .map_err(|_| field_out_of_sync("reference_snapshot"))?;

        if self.reference_snapshot_exact_j2000 != expected.reference_snapshot_exact_j2000 {
            return Err(field_out_of_sync("reference_snapshot_exact_j2000"));
        }
        self.reference_snapshot_exact_j2000
            .validate()
            .map_err(|_| field_out_of_sync("reference_snapshot_exact_j2000"))?;

        if self.comparison_snapshot_source != expected.comparison_snapshot_source {
            return Err(field_out_of_sync("comparison_snapshot_source"));
        }
        self.comparison_snapshot_source
            .validate()
            .map_err(|_| field_out_of_sync("comparison_snapshot_source"))?;

        if self.comparison_snapshot != expected.comparison_snapshot {
            return Err(field_out_of_sync("comparison_snapshot"));
        }
        self.comparison_snapshot
            .validate()
            .map_err(|_| field_out_of_sync("comparison_snapshot"))?;

        if self.independent_holdout_source != expected.independent_holdout_source {
            return Err(field_out_of_sync("independent_holdout_source"));
        }
        self.independent_holdout_source
            .validate()
            .map_err(|_| field_out_of_sync("independent_holdout_source"))?;

        if self.independent_holdout != expected.independent_holdout {
            return Err(field_out_of_sync("independent_holdout"));
        }
        self.independent_holdout
            .validate()
            .map_err(|_| field_out_of_sync("independent_holdout"))?;

        if self.selected_asteroid_source != expected.selected_asteroid_source {
            return Err(field_out_of_sync("selected_asteroid_source"));
        }
        self.selected_asteroid_source
            .validate()
            .map_err(|_| field_out_of_sync("selected_asteroid_source"))?;

        if self.selected_asteroid_source_windows != expected.selected_asteroid_source_windows {
            return Err(field_out_of_sync("selected_asteroid_source_windows"));
        }
        self.selected_asteroid_source_windows
            .validate()
            .map_err(|_| field_out_of_sync("selected_asteroid_source_windows"))?;

        if self.selected_asteroid_source_request_corpus
            != expected.selected_asteroid_source_request_corpus
        {
            return Err(field_out_of_sync("selected_asteroid_source_request_corpus"));
        }
        self.selected_asteroid_source_request_corpus
            .validate()
            .map_err(|_| field_out_of_sync("selected_asteroid_source_request_corpus"))?;

        if self.selected_asteroid_source_request_corpus_equatorial
            != expected.selected_asteroid_source_request_corpus_equatorial
        {
            return Err(field_out_of_sync(
                "selected_asteroid_source_request_corpus_equatorial",
            ));
        }
        self.selected_asteroid_source_request_corpus_equatorial
            .validate()
            .map_err(|_| field_out_of_sync("selected_asteroid_source_request_corpus_equatorial"))?;

        if self.production_generation_boundary_source
            != expected.production_generation_boundary_source
        {
            return Err(field_out_of_sync("production_generation_boundary_source"));
        }
        self.production_generation_boundary_source
            .validate()
            .map_err(|_| field_out_of_sync("production_generation_boundary_source"))?;

        if self.production_generation_body_class_coverage
            != expected.production_generation_body_class_coverage
        {
            return Err(field_out_of_sync(
                "production_generation_body_class_coverage",
            ));
        }
        self.production_generation_body_class_coverage
            .validate()
            .map_err(|_| field_out_of_sync("production_generation_body_class_coverage"))?;

        if self.production_generation_source != expected.production_generation_source {
            return Err(field_out_of_sync("production_generation_source"));
        }
        self.production_generation_source
            .validate()
            .map_err(|_| field_out_of_sync("production_generation_source"))?;

        Ok(())
    }

    /// Returns the validated phase-2 corpus alignment posture as a compact human-readable line.
    pub fn validated_summary_line(
        &self,
    ) -> Result<String, PackagedArtifactPhase2CorpusAlignmentSummaryValidationError> {
        self.validate()?;
        Ok(self.summary_line())
    }
}

impl fmt::Display for PackagedArtifactPhase2CorpusAlignmentSummary {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.summary_line())
    }
}

/// Returns the structured phase-2 corpus alignment posture used to keep the packaged-artifact threshold policy aligned with the current corpus.
pub fn packaged_artifact_phase2_corpus_alignment_summary_details(
) -> Option<PackagedArtifactPhase2CorpusAlignmentSummary> {
    Some(PackagedArtifactPhase2CorpusAlignmentSummary {
        reference_snapshot_source: pleiades_jpl::reference_snapshot_source_summary(),
        reference_snapshot: pleiades_jpl::reference_snapshot_body_class_coverage_summary()?,
        reference_snapshot_exact_j2000:
            pleiades_jpl::reference_snapshot_exact_j2000_evidence_summary()?,
        comparison_snapshot_source: pleiades_jpl::comparison_snapshot_source_summary(),
        comparison_snapshot: comparison_snapshot_body_class_coverage_summary()?,
        independent_holdout_source: pleiades_jpl::independent_holdout_source_summary(),
        independent_holdout: independent_holdout_snapshot_body_class_coverage_summary()?,
        selected_asteroid_source: pleiades_jpl::selected_asteroid_source_evidence_summary()?,
        selected_asteroid_source_windows: pleiades_jpl::selected_asteroid_source_window_summary()?,
        selected_asteroid_source_request_corpus: selected_asteroid_source_request_corpus_summary(
            CoordinateFrame::Ecliptic,
        )?,
        selected_asteroid_source_request_corpus_equatorial:
            selected_asteroid_source_request_corpus_summary(CoordinateFrame::Equatorial)?,
        production_generation_boundary_source:
            pleiades_jpl::production_generation_boundary_source_summary(),
        production_generation_body_class_coverage:
            pleiades_jpl::production_generation_snapshot_body_class_coverage_summary()?,
        production_generation_source: production_generation_source_summary(),
    })
}

/// Structured target-threshold posture for the packaged artifact generator.
#[derive(Clone, Debug, PartialEq)]
pub struct PackagedArtifactTargetThresholdSummary {
    /// Stable identifier for the release profile that the thresholds apply to.
    pub profile_id: &'static str,
    /// Current release posture for the production thresholds.
    pub state: PackagedArtifactTargetThresholdState,
    /// Body-class scopes covered by the current threshold policy.
    pub scopes: &'static [&'static str],
    /// Measured fit envelope captured for the current packaged artifact posture.
    pub fit_envelope: PackagedArtifactFitEnvelopeSummary,
    /// Body-class-specific fit envelopes captured for the current packaged artifact posture.
    pub scope_envelopes: PackagedArtifactTargetThresholdScopeEnvelopesSummary,
    /// Phase-2 corpus evidence that keeps the threshold posture aligned with the current corpus.
    pub phase2_corpus_alignment: PackagedArtifactPhase2CorpusAlignmentSummary,
}

/// Validation error for a packaged-artifact target-threshold summary that drifted from the current posture.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PackagedArtifactTargetThresholdSummaryValidationError {
    /// A summary field is out of sync with the current packaged-artifact posture.
    FieldOutOfSync { field: &'static str },
}

impl fmt::Display for PackagedArtifactTargetThresholdSummaryValidationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::FieldOutOfSync { field } => write!(
                f,
                "the packaged artifact target-threshold summary field `{field}` is out of sync with the current posture"
            ),
        }
    }
}

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

impl PackagedArtifactTargetThresholdSummary {
    /// Returns the target-threshold posture as a compact human-readable line.
    pub fn summary_line(&self) -> String {
        format!(
            "profile id={}; target thresholds: {}; scopes={}; {}; scope envelopes={}; phase 2 corpus alignment={}",
            self.profile_id,
            self.state,
            self.scopes.join(", "),
            self.fit_envelope.summary_line(),
            join_display(&self.scope_envelopes.scope_envelopes),
            self.phase2_corpus_alignment.summary_line(),
        )
    }

    /// Returns `Ok(())` when the summary still matches the current packaged-artifact posture.
    pub fn validate(&self) -> Result<(), PackagedArtifactTargetThresholdSummaryValidationError> {
        if self.profile_id != ARTIFACT_PROFILE_ID {
            return Err(
                PackagedArtifactTargetThresholdSummaryValidationError::FieldOutOfSync {
                    field: "profile_id",
                },
            );
        }
        self.state.validate_production_ready().map_err(|_| {
            PackagedArtifactTargetThresholdSummaryValidationError::FieldOutOfSync { field: "state" }
        })?;
        if self.state != PACKAGED_ARTIFACT_TARGET_THRESHOLD_STATE {
            return Err(
                PackagedArtifactTargetThresholdSummaryValidationError::FieldOutOfSync {
                    field: "state",
                },
            );
        }
        if self.scopes != PACKAGED_ARTIFACT_TARGET_THRESHOLD_SCOPES {
            return Err(
                PackagedArtifactTargetThresholdSummaryValidationError::FieldOutOfSync {
                    field: "scopes",
                },
            );
        }

        let expected_fit_envelope = packaged_artifact_fit_envelope_summary_details();
        if self.fit_envelope != expected_fit_envelope {
            return Err(
                PackagedArtifactTargetThresholdSummaryValidationError::FieldOutOfSync {
                    field: "fit_envelope",
                },
            );
        }
        if self.state.is_production_ready() {
            let thresholds = packaged_artifact_fit_threshold_summary_details();
            self.fit_envelope
                .validate_against_thresholds(&thresholds)
                .map_err(|_| {
                    PackagedArtifactTargetThresholdSummaryValidationError::FieldOutOfSync {
                        field: "fit_envelope",
                    }
                })?;
        }
        let expected_scope_envelopes =
            packaged_artifact_target_threshold_scope_envelopes_summary_details();
        if self.scope_envelopes != expected_scope_envelopes {
            return Err(
                PackagedArtifactTargetThresholdSummaryValidationError::FieldOutOfSync {
                    field: "scope_envelopes",
                },
            );
        }
        self.scope_envelopes.validate().map_err(|_| {
            PackagedArtifactTargetThresholdSummaryValidationError::FieldOutOfSync {
                field: "scope_envelopes",
            }
        })?;

        let expected_phase2_corpus_alignment =
            packaged_artifact_phase2_corpus_alignment_summary_details().ok_or(
                PackagedArtifactTargetThresholdSummaryValidationError::FieldOutOfSync {
                    field: "phase2_corpus_alignment",
                },
            )?;
        self.phase2_corpus_alignment
            .validate()
            .map_err(|error| match error {
                PackagedArtifactPhase2CorpusAlignmentSummaryValidationError::FieldOutOfSync {
                    field,
                } => PackagedArtifactTargetThresholdSummaryValidationError::FieldOutOfSync {
                    field: phase2_corpus_alignment_validation_field_path(field),
                },
            })?;
        if self.phase2_corpus_alignment != expected_phase2_corpus_alignment {
            return Err(
                PackagedArtifactTargetThresholdSummaryValidationError::FieldOutOfSync {
                    field: "phase2_corpus_alignment",
                },
            );
        }

        let thresholds = packaged_artifact_fit_threshold_summary_details();
        for scope_envelope in &self.scope_envelopes.scope_envelopes {
            scope_envelope
                .fit_envelope
                .validate_against_thresholds(&thresholds)
                .map_err(|_| {
                    PackagedArtifactTargetThresholdSummaryValidationError::FieldOutOfSync {
                        field: "scope_envelopes",
                    }
                })?;
        }

        Ok(())
    }

    /// Returns the validated target-threshold posture as a compact human-readable line.
    pub fn validated_summary_line(
        &self,
    ) -> Result<String, PackagedArtifactTargetThresholdSummaryValidationError> {
        self.validate()?;
        Ok(self.summary_line())
    }
}

impl fmt::Display for PackagedArtifactTargetThresholdSummary {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.summary_line())
    }
}

/// Returns the current packaged-artifact target-threshold summary record.
pub fn packaged_artifact_target_threshold_summary_details() -> PackagedArtifactTargetThresholdSummary
{
    let summary = PackagedArtifactTargetThresholdSummary {
        profile_id: ARTIFACT_PROFILE_ID,
        state: PACKAGED_ARTIFACT_TARGET_THRESHOLD_STATE,
        scopes: PACKAGED_ARTIFACT_TARGET_THRESHOLD_SCOPES,
        fit_envelope: packaged_artifact_fit_envelope_summary_details(),
        scope_envelopes: packaged_artifact_target_threshold_scope_envelopes_summary_details(),
        phase2_corpus_alignment: packaged_artifact_phase2_corpus_alignment_summary_details()
            .expect("phase-2 corpus evidence should be available"),
    };
    debug_assert!(summary.validate().is_ok());
    summary
}

/// Structured sync summary for the packaged-artifact source-fit and hold-out checks.
#[derive(Clone, Debug, PartialEq)]
pub struct PackagedArtifactSourceFitHoldoutSyncSummary {
    /// Calibrated fit thresholds used by the current packaged-artifact posture.
    pub fit_thresholds: PackagedArtifactFitThresholdSummary,
    /// Release-threshold posture that keeps the phase-2 corpus alignment synchronized.
    pub target_thresholds: PackagedArtifactTargetThresholdSummary,
    /// Phase-2 corpus evidence that anchors the threshold posture to current source coverage.
    pub phase2_corpus_alignment: PackagedArtifactPhase2CorpusAlignmentSummary,
}

/// Validation error for a packaged-artifact source-fit and hold-out sync summary that drifted from the current posture.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PackagedArtifactSourceFitHoldoutSyncSummaryValidationError {
    /// A summary field is out of sync with the current packaged-artifact posture.
    FieldOutOfSync { field: &'static str },
}

impl fmt::Display for PackagedArtifactSourceFitHoldoutSyncSummaryValidationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::FieldOutOfSync { field } => write!(
                f,
                "the packaged artifact source-fit and hold-out sync summary field `{field}` is out of sync with the current posture"
            ),
        }
    }
}

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

impl PackagedArtifactSourceFitHoldoutSyncSummary {
    /// Returns the source-fit and hold-out sync posture as a compact human-readable line.
    pub fn summary_line(&self) -> String {
        format!(
            "source-fit and hold-out sync: fit thresholds={}; target thresholds={}; phase 2 corpus alignment={}",
            self.fit_thresholds.summary_line(),
            self.target_thresholds.summary_line(),
            self.phase2_corpus_alignment.summary_line(),
        )
    }

    /// Returns `Ok(())` when the sync summary still matches the current packaged-artifact posture.
    pub fn validate(
        &self,
    ) -> Result<(), PackagedArtifactSourceFitHoldoutSyncSummaryValidationError> {
        let expected_fit_thresholds = packaged_artifact_fit_threshold_summary_details();
        if self.fit_thresholds != expected_fit_thresholds {
            return Err(
                PackagedArtifactSourceFitHoldoutSyncSummaryValidationError::FieldOutOfSync {
                    field: "fit_thresholds",
                },
            );
        }
        self.fit_thresholds.validate().map_err(|_| {
            PackagedArtifactSourceFitHoldoutSyncSummaryValidationError::FieldOutOfSync {
                field: "fit_thresholds",
            }
        })?;

        let expected_target_thresholds = packaged_artifact_target_threshold_summary_details();
        if self.target_thresholds != expected_target_thresholds {
            return Err(
                PackagedArtifactSourceFitHoldoutSyncSummaryValidationError::FieldOutOfSync {
                    field: "target_thresholds",
                },
            );
        }
        self.target_thresholds.validate().map_err(|_| {
            PackagedArtifactSourceFitHoldoutSyncSummaryValidationError::FieldOutOfSync {
                field: "target_thresholds",
            }
        })?;

        let expected_phase2_corpus_alignment =
            packaged_artifact_phase2_corpus_alignment_summary_details().ok_or(
                PackagedArtifactSourceFitHoldoutSyncSummaryValidationError::FieldOutOfSync {
                    field: "phase2_corpus_alignment",
                },
            )?;
        self.phase2_corpus_alignment
            .validate()
            .map_err(|error| match error {
                PackagedArtifactPhase2CorpusAlignmentSummaryValidationError::FieldOutOfSync {
                    field,
                } => PackagedArtifactSourceFitHoldoutSyncSummaryValidationError::FieldOutOfSync {
                    field: phase2_corpus_alignment_validation_field_path(field),
                },
            })?;
        if self.phase2_corpus_alignment != expected_phase2_corpus_alignment {
            return Err(
                PackagedArtifactSourceFitHoldoutSyncSummaryValidationError::FieldOutOfSync {
                    field: "phase2_corpus_alignment",
                },
            );
        }

        Ok(())
    }

    /// Returns the validated source-fit and hold-out sync posture as a compact human-readable line.
    pub fn validated_summary_line(
        &self,
    ) -> Result<String, PackagedArtifactSourceFitHoldoutSyncSummaryValidationError> {
        self.validate()?;
        Ok(self.summary_line())
    }
}

impl fmt::Display for PackagedArtifactSourceFitHoldoutSyncSummary {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.summary_line())
    }
}

/// Returns the current packaged-artifact source-fit and hold-out sync summary record.
pub fn packaged_artifact_source_fit_holdout_sync_summary_details(
) -> PackagedArtifactSourceFitHoldoutSyncSummary {
    let summary = PackagedArtifactSourceFitHoldoutSyncSummary {
        fit_thresholds: packaged_artifact_fit_threshold_summary_details(),
        target_thresholds: packaged_artifact_target_threshold_summary_details(),
        phase2_corpus_alignment: packaged_artifact_phase2_corpus_alignment_summary_details()
            .expect("phase-2 corpus evidence should be available"),
    };
    debug_assert!(summary.validate().is_ok());
    summary
}