pleiades-data 0.5.4

Packaged offline ephemeris data (precomputed Sun, Moon, Mercury through Pluto and 433 Eros 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
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
787
788
789
use super::*;

/// Structured fit envelope for the packaged artifact.
#[derive(Clone, Debug, PartialEq)]
pub struct PackagedArtifactFitEnvelopeSummary {
    /// Number of successfully measured segment samples.
    pub sample_count: usize,
    /// Number of planned segment samples for the current artifact layout.
    pub expected_sample_count: usize,
    /// Number of bundled bodies covered by the measured sample set.
    pub body_count: usize,
    /// Mean absolute longitude delta in degrees.
    pub mean_longitude_delta_degrees: f64,
    /// Mean absolute latitude delta in degrees.
    pub mean_latitude_delta_degrees: f64,
    /// Mean absolute distance delta in AU.
    pub mean_distance_delta_au: f64,
    /// Maximum absolute longitude delta in degrees.
    pub max_longitude_delta_degrees: f64,
    /// Maximum absolute latitude delta in degrees.
    pub max_latitude_delta_degrees: f64,
    /// Maximum absolute distance delta in AU.
    pub max_distance_delta_au: f64,
}

/// A packaged-artifact fit threshold violation.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PackagedArtifactFitThresholdViolation {
    /// The field that exceeds the calibrated threshold.
    pub field: &'static str,
    /// The measured value, encoded as raw bits so the summary stays lossless.
    pub measured_bits: u64,
    /// The calibrated threshold, encoded as raw bits so the summary stays lossless.
    pub threshold_bits: u64,
    /// The amount by which the measured value exceeds the calibrated threshold.
    pub overage_bits: u64,
}

impl PackagedArtifactFitThresholdViolation {
    fn summary_line(&self) -> String {
        format!(
            "`{}` measured={:.12}, threshold={:.12}, overage={:+.12}",
            self.field,
            f64::from_bits(self.measured_bits),
            f64::from_bits(self.threshold_bits),
            f64::from_bits(self.overage_bits),
        )
    }
}

pub(crate) fn packaged_artifact_fit_threshold_violations_from_envelope_and_thresholds(
    envelope: &PackagedArtifactFitEnvelopeSummary,
    thresholds: &PackagedArtifactFitThresholdSummary,
) -> Vec<PackagedArtifactFitThresholdViolation> {
    let mut violations = Vec::new();

    macro_rules! check_threshold {
        ($field:literal, $measured:expr, $threshold:expr) => {
            if $measured > $threshold {
                violations.push(PackagedArtifactFitThresholdViolation {
                    field: $field,
                    measured_bits: $measured.to_bits(),
                    threshold_bits: $threshold.to_bits(),
                    overage_bits: ($measured - $threshold).to_bits(),
                });
            }
        };
    }

    check_threshold!(
        "mean_longitude_delta_degrees",
        envelope.mean_longitude_delta_degrees,
        thresholds.max_mean_longitude_delta_degrees
    );
    check_threshold!(
        "mean_latitude_delta_degrees",
        envelope.mean_latitude_delta_degrees,
        thresholds.max_mean_latitude_delta_degrees
    );
    check_threshold!(
        "mean_distance_delta_au",
        envelope.mean_distance_delta_au,
        thresholds.max_mean_distance_delta_au
    );
    check_threshold!(
        "max_longitude_delta_degrees",
        envelope.max_longitude_delta_degrees,
        thresholds.max_longitude_delta_degrees
    );
    check_threshold!(
        "max_latitude_delta_degrees",
        envelope.max_latitude_delta_degrees,
        thresholds.max_latitude_delta_degrees
    );
    check_threshold!(
        "max_distance_delta_au",
        envelope.max_distance_delta_au,
        thresholds.max_distance_delta_au
    );

    violations
}

/// Validation error for a packaged-artifact fit envelope that drifted from the current posture.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PackagedArtifactFitEnvelopeSummaryValidationError {
    /// A rendered summary field no longer matches the current packaged-artifact fit envelope.
    FieldOutOfSync { field: &'static str },
    /// One or more measured fit fields exceed the calibrated packaged-artifact fit thresholds.
    ThresholdExceeded {
        violations: Vec<PackagedArtifactFitThresholdViolation>,
    },
}

impl PackagedArtifactFitEnvelopeSummaryValidationError {
    /// Returns the number of threshold violations captured by the validation error.
    pub fn violation_count(&self) -> usize {
        match self {
            Self::FieldOutOfSync { .. } => 0,
            Self::ThresholdExceeded { violations } => violations.len(),
        }
    }

    /// Returns the compact release-facing summary for the validation error.
    pub fn summary_line(&self) -> String {
        match self {
            Self::FieldOutOfSync { field } => format!(
                "the packaged artifact fit envelope summary field `{field}` is out of sync with the current posture"
            ),
            Self::ThresholdExceeded { violations } => {
                let rendered = violations
                    .iter()
                    .map(PackagedArtifactFitThresholdViolation::summary_line)
                    .collect::<Vec<_>>()
                    .join("; ");
                let violation_count = violations.len();
                let violation_label = if violation_count == 1 {
                    "violation"
                } else {
                    "violations"
                };
                format!(
                    "the packaged artifact fit envelope summary exceeds the calibrated fit thresholds ({violation_count} {violation_label}): {rendered}"
                )
            }
        }
    }
}

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

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

/// Calibrated fit thresholds for the packaged artifact.
#[derive(Clone, Debug, PartialEq)]
pub struct PackagedArtifactFitThresholdSummary {
    /// Maximum allowed mean absolute longitude delta in degrees.
    pub max_mean_longitude_delta_degrees: f64,
    /// Maximum allowed mean absolute latitude delta in degrees.
    pub max_mean_latitude_delta_degrees: f64,
    /// Maximum allowed mean absolute distance delta in AU.
    pub max_mean_distance_delta_au: f64,
    /// Maximum allowed absolute longitude delta in degrees.
    pub max_longitude_delta_degrees: f64,
    /// Maximum allowed absolute latitude delta in degrees.
    pub max_latitude_delta_degrees: f64,
    /// Maximum allowed absolute distance delta in AU.
    pub max_distance_delta_au: f64,
}

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

impl PackagedArtifactFitThresholdSummaryValidationError {
    /// Returns the compact release-facing summary for the validation error.
    pub fn summary_line(&self) -> String {
        match self {
            Self::FieldOutOfSync { field } => format!(
                "the packaged artifact fit threshold summary field `{field}` is out of sync with the current posture"
            ),
        }
    }
}

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

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

impl PackagedArtifactFitThresholdSummary {
    /// Returns the calibrated fit thresholds as a compact human-readable line.
    pub fn summary_line(&self) -> String {
        format!(
            "fit thresholds: mean Δlon≤{:.12}°, mean Δlat≤{:.12}°, mean Δdist≤{:.12} AU; max Δlon≤{:.12}°, max Δlat≤{:.12}°, max Δdist≤{:.12} AU",
            self.max_mean_longitude_delta_degrees,
            self.max_mean_latitude_delta_degrees,
            self.max_mean_distance_delta_au,
            self.max_longitude_delta_degrees,
            self.max_latitude_delta_degrees,
            self.max_distance_delta_au,
        )
    }

    /// Returns `Ok(())` when the summary still matches the current packaged-artifact fit thresholds.
    pub fn validate(&self) -> Result<(), PackagedArtifactFitThresholdSummaryValidationError> {
        if self != &PACKAGED_ARTIFACT_FIT_THRESHOLD_SUMMARY {
            return Err(
                PackagedArtifactFitThresholdSummaryValidationError::FieldOutOfSync {
                    field: "thresholds",
                },
            );
        }

        Ok(())
    }

    /// Returns the validated calibrated fit thresholds as a compact human-readable line.
    pub fn validated_summary_line(
        &self,
    ) -> Result<String, PackagedArtifactFitThresholdSummaryValidationError> {
        self.validate()?;
        Ok(self.summary_line())
    }
}

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

/// Structured fit margins for the packaged artifact.
#[derive(Clone, Debug, PartialEq)]
pub struct PackagedArtifactFitMarginSummary {
    /// Measured fit envelope for the current packaged artifact.
    pub envelope: PackagedArtifactFitEnvelopeSummary,
    /// Calibrated thresholds used to compute the current margins.
    pub thresholds: PackagedArtifactFitThresholdSummary,
}

impl PackagedArtifactFitMarginSummary {
    /// Returns the fit margins relative to the calibrated thresholds as a compact human-readable line.
    pub fn summary_line(&self) -> String {
        format!(
            "fit margins: mean Δlon={:+.12}°, mean Δlat={:+.12}°, mean Δdist={:+.12} AU; max Δlon={:+.12}°, max Δlat={:+.12}°, max Δdist={:+.12} AU",
            self.thresholds.max_mean_longitude_delta_degrees - self.envelope.mean_longitude_delta_degrees,
            self.thresholds.max_mean_latitude_delta_degrees - self.envelope.mean_latitude_delta_degrees,
            self.thresholds.max_mean_distance_delta_au - self.envelope.mean_distance_delta_au,
            self.thresholds.max_longitude_delta_degrees - self.envelope.max_longitude_delta_degrees,
            self.thresholds.max_latitude_delta_degrees - self.envelope.max_latitude_delta_degrees,
            self.thresholds.max_distance_delta_au - self.envelope.max_distance_delta_au,
        )
    }

    /// Returns `Ok(())` when the summary still matches the current packaged-artifact fit posture.
    pub fn validate(&self) -> Result<(), PackagedArtifactFitEnvelopeSummaryValidationError> {
        let current_envelope = packaged_artifact_fit_envelope_summary_details();
        if self.envelope != current_envelope {
            return Err(
                PackagedArtifactFitEnvelopeSummaryValidationError::FieldOutOfSync {
                    field: "envelope",
                },
            );
        }

        let current_thresholds = packaged_artifact_fit_threshold_summary_details();
        if self.thresholds != current_thresholds {
            return Err(
                PackagedArtifactFitEnvelopeSummaryValidationError::FieldOutOfSync {
                    field: "thresholds",
                },
            );
        }

        self.envelope.validate_against_thresholds(&self.thresholds)
    }

    /// Returns the validated fit margins relative to the calibrated thresholds as a compact human-readable line.
    pub fn validated_summary_line(
        &self,
    ) -> Result<String, PackagedArtifactFitEnvelopeSummaryValidationError> {
        self.validate()?;
        Ok(self.summary_line())
    }
}

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

/// Packaged-artifact fit threshold violations captured for the current posture.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PackagedArtifactFitThresholdViolationsSummary {
    /// Threshold violations ordered by field as they appear in the envelope.
    pub violations: Vec<PackagedArtifactFitThresholdViolation>,
}

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

impl PackagedArtifactFitThresholdViolationsSummaryValidationError {
    /// Returns the compact release-facing summary for the validation error.
    pub fn summary_line(&self) -> String {
        match self {
            Self::FieldOutOfSync { field } => format!(
                "the packaged artifact fit threshold violation summary field `{field}` is out of sync with the current posture"
            ),
        }
    }
}

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

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

impl PackagedArtifactFitThresholdViolationsSummary {
    /// Returns the packaged-artifact fit threshold violations as a compact human-readable line.
    pub fn summary_line(&self) -> String {
        let violation_count = self.violations.len();
        if violation_count == 0 {
            return "fit threshold violations: 0; details: none".to_string();
        }

        let rendered = self
            .violations
            .iter()
            .map(PackagedArtifactFitThresholdViolation::summary_line)
            .collect::<Vec<_>>()
            .join("; ");
        let violation_label = if violation_count == 1 {
            "violation"
        } else {
            "violations"
        };
        format!(
            "fit threshold violations: {violation_count} {violation_label}; details: {rendered}"
        )
    }

    /// Returns `Ok(())` when the summary still matches the current packaged-artifact fit threshold violation posture.
    pub fn validate(
        &self,
    ) -> Result<(), PackagedArtifactFitThresholdViolationsSummaryValidationError> {
        let current = packaged_artifact_fit_threshold_violation_summary_details();
        if self == &current {
            Ok(())
        } else {
            Err(
                PackagedArtifactFitThresholdViolationsSummaryValidationError::FieldOutOfSync {
                    field: "violations",
                },
            )
        }
    }

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

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

impl PackagedArtifactFitEnvelopeSummary {
    /// Returns `Ok(())` when the measured fit envelope stays within the calibrated thresholds.
    pub fn validate_against_thresholds(
        &self,
        thresholds: &PackagedArtifactFitThresholdSummary,
    ) -> Result<(), PackagedArtifactFitEnvelopeSummaryValidationError> {
        let violations = packaged_artifact_fit_threshold_violations_from_envelope_and_thresholds(
            self, thresholds,
        );

        if violations.is_empty() {
            Ok(())
        } else {
            Err(PackagedArtifactFitEnvelopeSummaryValidationError::ThresholdExceeded { violations })
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct PackagedArtifactFitSample {
    pub(crate) body: CelestialBody,
    pub(crate) segment_start: Instant,
    pub(crate) segment_end: Instant,
    pub(crate) sample_instant: Instant,
    pub(crate) sample_fraction: f64,
    pub(crate) longitude_delta_degrees: f64,
    pub(crate) latitude_delta_degrees: f64,
    pub(crate) distance_delta_au: f64,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub(crate) struct PackagedArtifactFitSegmentFamilyKey {
    segment_start_days_bits: u64,
    segment_start_scale: TimeScale,
    segment_end_days_bits: u64,
    segment_end_scale: TimeScale,
}

impl PackagedArtifactFitSegmentFamilyKey {
    pub(crate) fn from_sample(sample: &PackagedArtifactFitSample) -> Self {
        Self {
            segment_start_days_bits: sample.segment_start.julian_day.days().to_bits(),
            segment_start_scale: sample.segment_start.scale,
            segment_end_days_bits: sample.segment_end.julian_day.days().to_bits(),
            segment_end_scale: sample.segment_end.scale,
        }
    }
}

#[derive(Clone, Debug)]
pub(crate) struct PackagedArtifactFitChannelFamilyAccumulator {
    sample_count: usize,
    worst_sample: Option<PackagedArtifactFitSample>,
}

impl PackagedArtifactFitChannelFamilyAccumulator {
    pub(crate) fn new() -> Self {
        Self {
            sample_count: 0,
            worst_sample: None,
        }
    }

    pub(crate) fn push(&mut self, sample: &PackagedArtifactFitSample, channel: ChannelKind) {
        self.sample_count += 1;
        let should_replace = self
            .worst_sample
            .as_ref()
            .map(|existing| {
                let existing_delta = packaged_artifact_fit_channel_delta(existing, channel);
                let candidate_delta = packaged_artifact_fit_channel_delta(sample, channel);
                candidate_delta > existing_delta
                    || (candidate_delta == existing_delta
                        && (sample.segment_end.julian_day.days()
                            - sample.segment_start.julian_day.days())
                            < (existing.segment_end.julian_day.days()
                                - existing.segment_start.julian_day.days()))
            })
            .unwrap_or(true);

        if should_replace {
            self.worst_sample = Some(sample.clone());
        }
    }

    pub(crate) fn finish(self, channel: ChannelKind) -> Option<PackagedArtifactFitChannelOutlier> {
        self.worst_sample.as_ref().map(|sample| {
            PackagedArtifactFitChannelOutlier::from_sample(sample, channel, self.sample_count)
        })
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct PackagedArtifactFitChannelOutlier {
    /// Channel whose fit error is being tracked.
    pub channel: ChannelKind,
    /// Absolute delta for the channel.
    pub delta: f64,
    /// Inclusive segment start for the source interval.
    pub segment_start: Instant,
    /// Inclusive segment end for the source interval.
    pub segment_end: Instant,
    /// Segment span in days.
    pub segment_span_days: f64,
    /// Sample instant that produced the tracked delta.
    pub sample_instant: Instant,
    /// Sample position inside the segment, expressed as a normalized fraction.
    pub sample_fraction: f64,
    /// Number of fit samples that shared the same body/channel/segment family.
    pub sample_count: usize,
}

impl PackagedArtifactFitChannelOutlier {
    fn from_sample(
        sample: &PackagedArtifactFitSample,
        channel: ChannelKind,
        sample_count: usize,
    ) -> Self {
        Self {
            channel,
            delta: packaged_artifact_fit_channel_delta(sample, channel),
            segment_start: sample.segment_start,
            segment_end: sample.segment_end,
            segment_span_days: sample.segment_end.julian_day.days()
                - sample.segment_start.julian_day.days(),
            sample_instant: sample.sample_instant,
            sample_fraction: sample.sample_fraction,
            sample_count,
        }
    }

    fn delta_unit(&self) -> &'static str {
        match self.channel {
            ChannelKind::Longitude | ChannelKind::Latitude => "°",
            ChannelKind::DistanceAu => " AU",
            _ => unreachable!("unsupported packaged-artifact channel kind"),
        }
    }

    fn summary_line(&self) -> String {
        format!(
            "{}={:.12}{} @ {} (segment {} → {}, span={:.12} d, x={:.3}, samples={})",
            self.channel,
            self.delta,
            self.delta_unit(),
            self.sample_instant,
            self.segment_start,
            self.segment_end,
            self.segment_span_days,
            self.sample_fraction,
            self.sample_count,
        )
    }
}

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

#[derive(Clone, Debug, PartialEq)]
pub struct PackagedArtifactFitBodyOutlierSummary {
    /// Bundled body whose fit outliers are summarized.
    pub body: CelestialBody,
    /// Worst sampled delta for each stored channel.
    pub channel_outliers: Vec<PackagedArtifactFitChannelOutlier>,
}

impl PackagedArtifactFitBodyOutlierSummary {
    pub(crate) fn summary_line(&self) -> String {
        format!("{}{{{}}}", self.body, join_display(&self.channel_outliers))
    }
}

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

#[derive(Clone, Debug, PartialEq)]
pub struct PackagedArtifactFitOutlierSummary {
    /// Number of bundled bodies represented in the outlier report.
    pub body_count: usize,
    /// Body-level summaries for the worst sampled fit deltas.
    pub body_summaries: Vec<PackagedArtifactFitBodyOutlierSummary>,
}

impl PackagedArtifactFitOutlierSummary {
    /// Returns the body/channel fit outliers as a compact human-readable line.
    pub fn summary_line(&self) -> String {
        format!(
            "fit outliers: {} bundled bodies; {}",
            self.body_count,
            join_display(&self.body_summaries)
        )
    }

    /// Returns `Ok(())` when the summary still matches the current packaged artifact.
    pub fn validate(&self) -> Result<(), PackagedArtifactFitEnvelopeSummaryValidationError> {
        let expected = packaged_artifact_fit_outlier_summary_details();
        if self != &expected {
            return Err(
                PackagedArtifactFitEnvelopeSummaryValidationError::FieldOutOfSync {
                    field: "outlier summary",
                },
            );
        }

        Ok(())
    }

    /// Returns the validated body/channel fit outliers as a compact human-readable line.
    pub fn validated_summary_line(
        &self,
    ) -> Result<String, PackagedArtifactFitEnvelopeSummaryValidationError> {
        self.validate()?;
        Ok(self.summary_line())
    }
}

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

#[derive(Clone, Debug, PartialEq)]
pub struct PackagedArtifactFitChannelOutlierSummary {
    /// Channel-level summaries for the worst sampled fit deltas.
    pub channel_summaries: Vec<String>,
}

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

impl PackagedArtifactFitChannelOutlierSummaryValidationError {
    /// Returns the compact release-facing summary for the validation error.
    pub fn summary_line(&self) -> String {
        match self {
            Self::FieldOutOfSync { field } => format!(
                "the packaged artifact fit outlier-by-channel summary field `{field}` is out of sync with the current posture"
            ),
        }
    }
}

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

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

impl PackagedArtifactFitChannelOutlierSummary {
    /// Returns the channel-level fit outliers as a compact human-readable line.
    pub fn summary_line(&self) -> String {
        if self.channel_summaries.is_empty() {
            "fit outliers by channel: none".to_string()
        } else {
            format!(
                "fit outliers by channel: {}",
                self.channel_summaries.join("; ")
            )
        }
    }

    /// Returns `Ok(())` when the summary still matches the current packaged artifact.
    pub fn validate(&self) -> Result<(), PackagedArtifactFitChannelOutlierSummaryValidationError> {
        let expected = packaged_artifact_fit_channel_outlier_summary_details();
        if self != &expected {
            return Err(
                PackagedArtifactFitChannelOutlierSummaryValidationError::FieldOutOfSync {
                    field: "channel_summaries",
                },
            );
        }

        Ok(())
    }

    /// Returns the validated channel-level fit outliers as a compact human-readable line.
    pub fn validated_summary_line(
        &self,
    ) -> Result<String, PackagedArtifactFitChannelOutlierSummaryValidationError> {
        self.validate()?;
        Ok(self.summary_line())
    }
}

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

pub(crate) fn packaged_artifact_fit_channel_outlier_summary_for_channel(
    samples: &[PackagedArtifactFitSample],
    channel: ChannelKind,
) -> Option<String> {
    let mut families: HashMap<
        (CelestialBody, PackagedArtifactFitSegmentFamilyKey),
        PackagedArtifactFitChannelFamilyAccumulator,
    > = HashMap::new();

    for sample in samples {
        let family_key = PackagedArtifactFitSegmentFamilyKey::from_sample(sample);
        let entry = families
            .entry((sample.body.clone(), family_key))
            .or_insert_with(PackagedArtifactFitChannelFamilyAccumulator::new);
        entry.push(sample, channel);
    }

    let mut body_outliers: HashMap<CelestialBody, PackagedArtifactFitChannelOutlier> =
        HashMap::new();

    for ((body, _family_key), family) in families {
        let Some(candidate) = family.finish(channel) else {
            continue;
        };
        match body_outliers.get_mut(&body) {
            Some(existing) if existing.delta > candidate.delta => {}
            Some(existing)
                if existing.delta == candidate.delta
                    && existing.segment_span_days <= candidate.segment_span_days => {}
            Some(existing) => *existing = candidate,
            None => {
                body_outliers.insert(body, candidate);
            }
        }
    }

    if body_outliers.is_empty() {
        return None;
    }

    let mut body_entries = body_outliers
        .into_iter()
        .map(|(body, outlier)| format!("{body}{{{outlier}}}"))
        .collect::<Vec<_>>();
    body_entries.sort();

    Some(format!("{channel}{{{}}}", body_entries.join(", ")))
}

/// Returns the current packaged-artifact fit outliers by channel as a structured summary record.
pub fn packaged_artifact_fit_channel_outlier_summary_details(
) -> PackagedArtifactFitChannelOutlierSummary {
    let samples = packaged_artifact_fit_outlier_samples_for_current_artifact();
    let mut channel_summaries = Vec::new();

    for channel in [
        ChannelKind::DistanceAu,
        ChannelKind::Longitude,
        ChannelKind::Latitude,
    ] {
        if let Some(entry) =
            packaged_artifact_fit_channel_outlier_summary_for_channel(samples, channel)
        {
            channel_summaries.push(entry);
        }
    }

    PackagedArtifactFitChannelOutlierSummary { channel_summaries }
}

// SP1 draft baseline: thresholds are pinned to the measured worst case (overall
// envelope and the worst per-scope envelope) of the regenerated dense de440-backed
// draft artifact. Each body is measured against the SAME source it was fit from:
// major bodies against the dense de440 production reference corpus (1900–2100, ≥3
// entries/body, no extrapolation), selected-asteroid/custom bodies against the
// JplSnapshotBackend reference snapshot they were fit against. So the posture
// reflects measured reality, not an enforced target. These are sample-residual
// envelopes (not the per-body hold-out accuracy); they are finite and bounded.
// SP2 tunes these toward real accuracy goals.
pub(crate) const PACKAGED_ARTIFACT_FIT_MAX_MEAN_LONGITUDE_DELTA_DEGREES: f64 = 79.29937281518961;
pub(crate) const PACKAGED_ARTIFACT_FIT_MAX_MEAN_LATITUDE_DELTA_DEGREES: f64 =
    3.320_919_159_432_106_6;
pub(crate) const PACKAGED_ARTIFACT_FIT_MAX_MEAN_DISTANCE_DELTA_AU: f64 = 5240.2473102557;
pub(crate) const PACKAGED_ARTIFACT_FIT_MAX_LONGITUDE_DELTA_DEGREES: f64 = 179.999_799_204_804_48;
pub(crate) const PACKAGED_ARTIFACT_FIT_MAX_LATITUDE_DELTA_DEGREES: f64 = 69.9571184739231;
pub(crate) const PACKAGED_ARTIFACT_FIT_MAX_DISTANCE_DELTA_AU: f64 = 10227288.989857685;

pub(crate) const PACKAGED_ARTIFACT_FIT_THRESHOLD_SUMMARY: PackagedArtifactFitThresholdSummary =
    PackagedArtifactFitThresholdSummary {
        max_mean_longitude_delta_degrees: PACKAGED_ARTIFACT_FIT_MAX_MEAN_LONGITUDE_DELTA_DEGREES,
        max_mean_latitude_delta_degrees: PACKAGED_ARTIFACT_FIT_MAX_MEAN_LATITUDE_DELTA_DEGREES,
        max_mean_distance_delta_au: PACKAGED_ARTIFACT_FIT_MAX_MEAN_DISTANCE_DELTA_AU,
        max_longitude_delta_degrees: PACKAGED_ARTIFACT_FIT_MAX_LONGITUDE_DELTA_DEGREES,
        max_latitude_delta_degrees: PACKAGED_ARTIFACT_FIT_MAX_LATITUDE_DELTA_DEGREES,
        max_distance_delta_au: PACKAGED_ARTIFACT_FIT_MAX_DISTANCE_DELTA_AU,
    };