diagprint 0.8.1

A Rust diagnostics lifecycle framework for structured diagnostics, rendering, remediation, CI, editors, and telemetry.
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
use crate::history::{DiagnosticHistory, DiagnosticLineage};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};

/// Stable schema for privacy-light diagnostic forensic case files.
///
/// V1 is intentionally evidence-based. It records only information derivable
/// from the verified diagnostic-history chain and does not infer source-control
/// blame, root cause, authorship, or remediation success.
pub const DIAGNOSTIC_CASE_FILE_V1_SCHEMA: &str = "diagprint.forensics.case-file/v1";

/// Current lifecycle state of one logical diagnostic.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DiagnosticCaseStatus {
    /// The fingerprint is present in the latest history run.
    Active,

    /// The fingerprint was observed historically but is absent from the latest
    /// history run.
    Resolved,
}

impl DiagnosticCaseStatus {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Active => "active",
            Self::Resolved => "resolved",
        }
    }
}

/// Evidence for one history run in which the diagnostic was actually present.
///
/// This representation deliberately retains no diagnostic message, source
/// path, source text, arbitrary attribute, help, note, cause, or remediation
/// payload.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DiagnosticCaseRun {
    pub run_index: usize,

    pub label: String,

    pub report_digest: String,

    pub run_digest: String,

    pub instances: usize,

    /// Per-severity instance counts for this fingerprint in this run.
    ///
    /// A string-keyed map deliberately remains forward-compatible with future
    /// severity names stored by a later history schema.
    pub severities: BTreeMap<String, usize>,

    /// Unique canonical diagnostic content digests observed for this
    /// fingerprint in this run.
    pub diagnostic_digests: Vec<String>,
}

/// One contiguous period during which a logical diagnostic was present.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DiagnosticEpisode {
    /// One-based episode number within this case file.
    pub episode: usize,

    pub started_run: usize,

    pub last_active_run: usize,

    /// First run after this episode in which the fingerprint was absent.
    ///
    /// `None` means the episode remains active in the latest history run.
    pub resolved_run: Option<usize>,

    pub observed_runs: usize,

    pub instances: usize,

    pub peak_instances: usize,
}

/// Privacy-light forensic summary for one logical diagnostic fingerprint.
///
/// A case file is a deterministic interpretation of an already verified
/// [`DiagnosticHistory`]. It does not introduce a second persistence format or
/// database.
///
/// Case-file v1 intentionally answers only questions supported by retained
/// history evidence:
///
/// - when the fingerprint first and last appeared in run order;
/// - whether it is active now;
/// - how many contiguous episodes it has had;
/// - whether it disappeared and later reappeared;
/// - how many instances were introduced, resolved, or changed;
/// - whether severity increased;
/// - which history runs and canonical diagnostic digests support the result;
/// - which cryptographic history-chain head anchors the evidence.
///
/// Source-control blame and causal inference are deliberately outside v1.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DiagnosticCaseFile {
    pub schema: String,

    pub fingerprint: String,

    pub status: DiagnosticCaseStatus,

    /// Total number of runs in the verified history, including runs where this
    /// fingerprint was absent.
    pub history_runs: usize,

    /// Number of runs containing at least one instance of this fingerprint.
    pub observed_runs: usize,

    /// Total number of matching diagnostic instances across all retained runs.
    pub observed_instances: usize,

    /// Number of distinct canonical diagnostic content digests seen for this
    /// logical fingerprint.
    pub unique_digests: usize,

    pub first_seen_run: usize,

    pub last_seen_run: usize,

    /// Number of matching instances in the latest history run.
    pub active_instances: usize,

    /// Number of instances classified as newly introduced across lineage
    /// transitions.
    pub introduced_instances: usize,

    /// Number of instances classified as resolved across lineage transitions.
    pub resolved_instances: usize,

    /// Number of same-fingerprint instances whose canonical content changed.
    pub changed_instances: usize,

    pub severity_increases: usize,

    /// Number of times a diagnostic began a new episode after at least one
    /// previous episode had ended.
    pub reappearances: usize,

    /// Current cryptographic history-chain head.
    pub chain_head: Option<String>,

    pub episodes: Vec<DiagnosticEpisode>,

    /// Evidence-bearing runs in chronological run order.
    pub evidence: Vec<DiagnosticCaseRun>,
}

impl DiagnosticCaseFile {
    /// Builds a forensic case file from one verified in-memory history.
    ///
    /// Returns `None` when the fingerprint has never been observed.
    pub fn from_history(history: &DiagnosticHistory, fingerprint: &str) -> Option<Self> {
        let lineage = history.lineage(fingerprint);

        let first_seen_run = lineage.first_seen_run()?;
        let last_seen_run = lineage.last_seen_run()?;

        let status = if lineage.active_in_latest() {
            DiagnosticCaseStatus::Active
        } else {
            DiagnosticCaseStatus::Resolved
        };

        let active_instances = lineage.steps.last().map_or(0, |step| step.active_instances);

        let severity_increases = lineage
            .steps
            .iter()
            .map(|step| step.severity_increases)
            .sum();

        let mut unique_digests = BTreeSet::new();

        let mut evidence = Vec::new();

        for run in history.runs() {
            let mut instances = 0usize;

            let mut severities = BTreeMap::<String, usize>::new();

            let mut run_digests = BTreeSet::<String>::new();

            for observation in run
                .observations
                .iter()
                .filter(|observation| observation.fingerprint == fingerprint)
            {
                instances = instances.saturating_add(1);

                *severities.entry(observation.severity.clone()).or_default() += 1;

                run_digests.insert(observation.digest.clone());

                unique_digests.insert(observation.digest.clone());
            }

            if instances == 0 {
                continue;
            }

            evidence.push(DiagnosticCaseRun {
                run_index: run.index,

                label: run.label.clone(),

                report_digest: run.report_digest.clone(),

                run_digest: run.run_digest.to_string(),

                instances,

                severities,

                diagnostic_digests: run_digests.into_iter().collect(),
            });
        }

        let episodes = build_episodes(&lineage);

        let observed_instances = evidence.iter().map(|run| run.instances).sum();

        let reappearances = episodes.len().saturating_sub(1);

        Some(Self {
            schema: DIAGNOSTIC_CASE_FILE_V1_SCHEMA.to_owned(),

            fingerprint: fingerprint.to_owned(),

            status,

            history_runs: history.len(),

            observed_runs: evidence.len(),

            observed_instances,

            unique_digests: unique_digests.len(),

            first_seen_run,

            last_seen_run,

            active_instances,

            introduced_instances: lineage.appearances(),

            resolved_instances: lineage.resolutions(),

            changed_instances: lineage.changes(),

            severity_increases,

            reappearances,

            chain_head: history.head_digest().map(|digest| digest.to_string()),

            episodes,

            evidence,
        })
    }

    pub const fn is_active(&self) -> bool {
        matches!(self.status, DiagnosticCaseStatus::Active)
    }

    pub const fn is_resolved(&self) -> bool {
        matches!(self.status, DiagnosticCaseStatus::Resolved)
    }
}

impl DiagnosticHistory {
    /// Builds a privacy-light forensic case file for one exact canonical
    /// diagnostic fingerprint.
    ///
    /// Prefix resolution belongs to presentation layers such as the diagprint
    /// CLI; this API deliberately requires the complete fingerprint.
    pub fn case_file(&self, fingerprint: &str) -> Option<DiagnosticCaseFile> {
        DiagnosticCaseFile::from_history(self, fingerprint)
    }
}

fn build_episodes(lineage: &DiagnosticLineage) -> Vec<DiagnosticEpisode> {
    let mut episodes = Vec::new();

    let mut current: Option<DiagnosticEpisode> = None;

    for step in &lineage.steps {
        if step.active_instances != 0 {
            match current.as_mut() {
                Some(episode) => {
                    episode.last_active_run = step.to_run;

                    episode.observed_runs = episode.observed_runs.saturating_add(1);

                    episode.instances = episode.instances.saturating_add(step.active_instances);

                    episode.peak_instances = episode.peak_instances.max(step.active_instances);
                }

                None => {
                    current = Some(DiagnosticEpisode {
                        episode: episodes.len() + 1,

                        started_run: step.to_run,

                        last_active_run: step.to_run,

                        resolved_run: None,

                        observed_runs: 1,

                        instances: step.active_instances,

                        peak_instances: step.active_instances,
                    });
                }
            }

            continue;
        }

        if let Some(mut episode) = current.take() {
            episode.resolved_run = Some(step.to_run);

            episodes.push(episode);
        }
    }

    if let Some(episode) = current {
        episodes.push(episode);
    }

    episodes
}

/// Stable schema for run-by-run diagnostic forensic timelines.
///
/// Timeline v1 is derived entirely from verified diagnostic history. It
/// preserves the evidence-only boundary established by case-file v1.
pub const DIAGNOSTIC_TIMELINE_V1_SCHEMA: &str = "diagprint.forensics.timeline/v1";

/// Presence state of one logical diagnostic in one retained history run.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DiagnosticTimelinePhase {
    /// The run occurred before this fingerprint had ever been observed.
    Unseen,

    /// One or more matching instances are present in this run.
    Active,

    /// The fingerprint had previously been observed but is absent in this run.
    Absent,
}

impl DiagnosticTimelinePhase {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Unseen => "unseen",
            Self::Active => "active",
            Self::Absent => "absent",
        }
    }

    pub const fn is_active(self) -> bool {
        matches!(self, Self::Active)
    }
}

/// Notable lifecycle event occurring at one timeline run.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DiagnosticTimelineEvent {
    /// First retained observation of this fingerprint.
    FirstSeen,

    /// The diagnostic remained active without a more significant transition.
    Persisting,

    /// Canonical content changed while logical fingerprint identity remained.
    Changed,

    /// At least one same-fingerprint instance increased in severity.
    SeverityIncreased,

    /// The previous run contained the fingerprint and this run does not.
    Resolved,

    /// The fingerprint became active after at least one absent run.
    Reappeared,
}

impl DiagnosticTimelineEvent {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::FirstSeen => "first_seen",
            Self::Persisting => "persisting",
            Self::Changed => "changed",
            Self::SeverityIncreased => "severity_increased",
            Self::Resolved => "resolved",
            Self::Reappeared => "reappeared",
        }
    }
}

/// One complete run in a diagnostic forensic timeline.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DiagnosticTimelineRun {
    pub run_index: usize,

    pub label: String,

    pub phase: DiagnosticTimelinePhase,

    /// One-based active episode number.
    ///
    /// Absent and unseen runs carry `None`.
    pub episode: Option<usize>,

    pub instances: usize,

    pub severities: BTreeMap<String, usize>,

    pub diagnostic_digests: Vec<String>,

    pub introduced_instances: usize,

    pub resolved_instances: usize,

    pub persisting_instances: usize,

    pub changed_instances: usize,

    pub severity_increases: usize,

    pub events: Vec<DiagnosticTimelineEvent>,
}

/// Classification for one contiguous run window where the diagnostic is absent.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DiagnosticCleanWindowKind {
    /// Runs retained before the diagnostic was ever observed.
    BeforeFirstSeen,

    /// Absence separating two active episodes.
    BetweenEpisodes,

    /// Absence after the final retained active episode.
    AfterResolution,
}

impl DiagnosticCleanWindowKind {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::BeforeFirstSeen => "before_first_seen",
            Self::BetweenEpisodes => "between_episodes",
            Self::AfterResolution => "after_resolution",
        }
    }
}

/// One contiguous history interval where the fingerprint is absent.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DiagnosticCleanWindow {
    pub kind: DiagnosticCleanWindowKind,

    pub start_run: usize,

    pub end_run: usize,

    pub runs: usize,
}

/// Complete run-by-run forensic lifecycle for one logical fingerprint.
///
/// Unlike [`DiagnosticCaseFile`], which summarizes the lifecycle, a timeline
/// retains one row for every history run, including runs where the fingerprint
/// was not present.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DiagnosticTimeline {
    pub schema: String,

    pub fingerprint: String,

    pub status: DiagnosticCaseStatus,

    pub history_runs: usize,

    pub first_seen_run: usize,

    pub last_seen_run: usize,

    pub active_instances: usize,

    pub reappearances: usize,

    pub chain_head: Option<String>,

    pub runs: Vec<DiagnosticTimelineRun>,

    pub clean_windows: Vec<DiagnosticCleanWindow>,
}

impl DiagnosticTimeline {
    /// Builds a deterministic timeline from one verified in-memory history.
    ///
    /// Returns `None` when the fingerprint has never been observed.
    pub fn from_history(history: &DiagnosticHistory, fingerprint: &str) -> Option<Self> {
        let case = history.case_file(fingerprint)?;
        let lineage = history.lineage(fingerprint);

        let mut runs = Vec::with_capacity(history.len());

        let mut seen_before = false;
        let mut previous_active = false;
        let mut episode = 0usize;

        for run in history.runs() {
            let step = lineage.steps.iter().find(|step| step.to_run == run.index)?;

            let mut instances = 0usize;

            let mut severities = BTreeMap::<String, usize>::new();

            let mut diagnostic_digests = BTreeSet::<String>::new();

            for observation in run
                .observations
                .iter()
                .filter(|observation| observation.fingerprint == fingerprint)
            {
                instances = instances.saturating_add(1);

                *severities.entry(observation.severity.clone()).or_default() += 1;

                diagnostic_digests.insert(observation.digest.clone());
            }

            let active = instances != 0;

            let phase = if active {
                DiagnosticTimelinePhase::Active
            } else if seen_before {
                DiagnosticTimelinePhase::Absent
            } else {
                DiagnosticTimelinePhase::Unseen
            };

            if active && !previous_active {
                episode = episode.saturating_add(1);
            }

            let current_episode = active.then_some(episode);

            let mut events = Vec::new();

            if active {
                if !seen_before {
                    events.push(DiagnosticTimelineEvent::FirstSeen);
                } else if !previous_active {
                    events.push(DiagnosticTimelineEvent::Reappeared);
                }

                if step.counts.changed != 0 {
                    events.push(DiagnosticTimelineEvent::Changed);
                }

                if step.severity_increases != 0 {
                    events.push(DiagnosticTimelineEvent::SeverityIncreased);
                }

                if previous_active && events.is_empty() {
                    events.push(DiagnosticTimelineEvent::Persisting);
                }
            } else if previous_active {
                events.push(DiagnosticTimelineEvent::Resolved);
            }

            runs.push(DiagnosticTimelineRun {
                run_index: run.index,

                label: run.label.clone(),

                phase,

                episode: current_episode,

                instances,

                severities,

                diagnostic_digests: diagnostic_digests.into_iter().collect(),

                introduced_instances: step.counts.new,

                resolved_instances: step.counts.resolved,

                persisting_instances: step.counts.persisting,

                changed_instances: step.counts.changed,

                severity_increases: step.severity_increases,

                events,
            });

            if active {
                seen_before = true;
            }

            previous_active = active;
        }

        let clean_windows = build_clean_windows(&runs, case.first_seen_run);

        Some(Self {
            schema: DIAGNOSTIC_TIMELINE_V1_SCHEMA.to_owned(),

            fingerprint: fingerprint.to_owned(),

            status: case.status,

            history_runs: history.len(),

            first_seen_run: case.first_seen_run,

            last_seen_run: case.last_seen_run,

            active_instances: case.active_instances,

            reappearances: case.reappearances,

            chain_head: case.chain_head,

            runs,

            clean_windows,
        })
    }

    pub fn active_runs(&self) -> usize {
        self.runs.iter().filter(|run| run.phase.is_active()).count()
    }

    pub fn absent_runs(&self) -> usize {
        self.runs
            .iter()
            .filter(|run| matches!(run.phase, DiagnosticTimelinePhase::Absent))
            .count()
    }

    pub fn unseen_runs(&self) -> usize {
        self.runs
            .iter()
            .filter(|run| matches!(run.phase, DiagnosticTimelinePhase::Unseen))
            .count()
    }
}

impl DiagnosticHistory {
    /// Builds a complete privacy-light timeline for one exact canonical
    /// diagnostic fingerprint.
    pub fn timeline(&self, fingerprint: &str) -> Option<DiagnosticTimeline> {
        DiagnosticTimeline::from_history(self, fingerprint)
    }
}

fn build_clean_windows(
    runs: &[DiagnosticTimelineRun],
    first_seen_run: usize,
) -> Vec<DiagnosticCleanWindow> {
    let mut windows = Vec::new();

    let mut index = 0usize;

    while index < runs.len() {
        if runs[index].phase.is_active() {
            index += 1;
            continue;
        }

        let start = index;

        while index < runs.len() && !runs[index].phase.is_active() {
            index += 1;
        }

        let end = index - 1;

        let later_active = runs.iter().skip(index).any(|run| run.phase.is_active());

        let kind = if runs[end].run_index < first_seen_run {
            DiagnosticCleanWindowKind::BeforeFirstSeen
        } else if later_active {
            DiagnosticCleanWindowKind::BetweenEpisodes
        } else {
            DiagnosticCleanWindowKind::AfterResolution
        };

        windows.push(DiagnosticCleanWindow {
            kind,

            start_run: runs[start].run_index,

            end_run: runs[end].run_index,

            runs: end - start + 1,
        });
    }

    windows
}