Skip to main content

cmtraceopen_parser/esp/
reducer.rs

1use std::collections::{BTreeMap, BTreeSet, VecDeque};
2
3use chrono::{SecondsFormat, TimeZone, Utc};
4use serde::{Deserialize, Serialize};
5
6use super::correlation::correlate_installer_processes;
7use super::models::*;
8use super::normalize::{
9    decode_oobe_config, extract_guid, normalize_classic_esp_status, normalize_office_detail_status,
10    normalize_office_status, normalize_policy_status, normalize_timestamp, normalize_v2_status,
11    percent_decode_bounded,
12};
13use super::rules::derive_findings;
14use super::timeline::{sort_timeline_entries, stable_record_id, stable_timeline_entry_id};
15use crate::intune::guid_registry::{extract_app_id, extract_app_name, is_fallback_name};
16
17pub const MAX_RETAINED_EVIDENCE_RECORDS: usize = 25_000;
18pub const MAX_RETAINED_EVIDENCE_SERIALIZED_BYTES: usize = 32 * 1024 * 1024;
19pub const MAX_EVIDENCE_IDENTITY_SOURCES: usize = 25_000;
20pub const MAX_TRACKED_EVIDENCE_IDENTITIES: usize = 262_144;
21
22const RETENTION_COVERAGE_ARTIFACT_ID: &str = "session.evidence-retention";
23const RETENTION_COVERAGE_FAMILY: &str = "session-retention";
24const RETENTION_COVERAGE_ORDINAL: usize = usize::MAX;
25const IDENTITY_COVERAGE_ARTIFACT_ID: &str = "session.evidence-identity-limit";
26const IDENTITY_COVERAGE_FAMILY: &str = "session-retention";
27const IDENTITY_COVERAGE_ORDINAL: usize = usize::MAX - 1;
28
29#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
30#[serde(tag = "kind", content = "record", rename_all = "camelCase")]
31pub enum EspEvidenceRecord {
32    Registry(EspRegistryObservation),
33    Json(EspJsonObservation),
34    EventLog(EspEventLogObservation),
35    Ime(EspImeObservation),
36    DeploymentLog(EspDeploymentLogObservation),
37    Process(EspProcessObservation),
38    System(EspSystemObservation),
39    DeliveryOptimizationSummary(EspDeliveryOptimizationEvidence),
40    DeliveryOptimization(EspDeliveryOptimizationObservation),
41    Graph(EspGraphObservation),
42    Coverage(EspArtifactCoverage),
43}
44
45impl EspEvidenceRecord {
46    /// Returns the acquisition timestamp carried by an observation or coverage
47    /// record. Aggregate-only records without an observation context return
48    /// `None`.
49    pub fn observed_at_utc(&self) -> Option<&str> {
50        match self {
51            Self::Coverage(value) => Some(&value.observed_at_utc),
52            _ => record_context(self).map(|context| context.observed_at_utc.as_str()),
53        }
54    }
55}
56
57/// An evidence record whose source-local occurrence identity was assigned at
58/// acquisition time and must remain unchanged across later projections.
59#[doc(hidden)]
60#[derive(Debug, Clone)]
61pub struct EspIdentifiedEvidenceRecord {
62    record: EspEvidenceRecord,
63    occurrence_ordinal: usize,
64}
65
66impl EspIdentifiedEvidenceRecord {
67    pub fn with_occurrence(record: EspEvidenceRecord, occurrence_ordinal: usize) -> Self {
68        Self {
69            record,
70            occurrence_ordinal,
71        }
72    }
73
74    pub fn record(&self) -> &EspEvidenceRecord {
75        &self.record
76    }
77
78    pub fn occurrence_ordinal(&self) -> usize {
79        self.occurrence_ordinal
80    }
81}
82
83/// Bounded session-owned allocator. Counters are isolated by source artifact,
84/// so activity from one provider cannot renumber another provider's records.
85#[doc(hidden)]
86#[derive(Debug, Clone)]
87pub struct EspEvidenceIdentityAllocator {
88    last_by_source: BTreeMap<String, usize>,
89    accepted_identities: BTreeSet<EspEvidenceIdentityKey>,
90    max_sources: usize,
91    max_identities: usize,
92}
93
94#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
95struct EspEvidenceIdentityKey {
96    source_artifact_id: String,
97    evidence_id: String,
98    occurrence_ordinal: usize,
99}
100
101#[doc(hidden)]
102#[derive(Debug, Clone, Copy, PartialEq, Eq)]
103pub enum EspEvidenceIdentityError {
104    SourceLimit,
105    IdentityLimit,
106    OccurrenceOverflow,
107}
108
109#[doc(hidden)]
110#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
111pub struct EspEvidenceIdentityRejectionCounts {
112    source_limit: usize,
113    identity_limit: usize,
114    occurrence_overflow: usize,
115}
116
117impl EspEvidenceIdentityRejectionCounts {
118    pub fn record(&mut self, error: EspEvidenceIdentityError) {
119        match error {
120            EspEvidenceIdentityError::SourceLimit => {
121                self.source_limit = self.source_limit.saturating_add(1);
122            }
123            EspEvidenceIdentityError::IdentityLimit => {
124                self.identity_limit = self.identity_limit.saturating_add(1);
125            }
126            EspEvidenceIdentityError::OccurrenceOverflow => {
127                self.occurrence_overflow = self.occurrence_overflow.saturating_add(1);
128            }
129        }
130    }
131
132    pub fn merge(&mut self, other: Self) {
133        self.source_limit = self.source_limit.saturating_add(other.source_limit);
134        self.identity_limit = self.identity_limit.saturating_add(other.identity_limit);
135        self.occurrence_overflow = self
136            .occurrence_overflow
137            .saturating_add(other.occurrence_overflow);
138    }
139
140    pub fn total(&self) -> usize {
141        self.source_limit
142            .saturating_add(self.identity_limit)
143            .saturating_add(self.occurrence_overflow)
144    }
145}
146
147impl Default for EspEvidenceIdentityAllocator {
148    fn default() -> Self {
149        Self::new()
150    }
151}
152
153impl EspEvidenceIdentityAllocator {
154    pub fn new() -> Self {
155        Self::with_limits(
156            MAX_EVIDENCE_IDENTITY_SOURCES,
157            MAX_TRACKED_EVIDENCE_IDENTITIES,
158        )
159    }
160
161    #[doc(hidden)]
162    pub fn with_source_limit(max_sources: usize) -> Self {
163        Self::with_limits(max_sources, MAX_TRACKED_EVIDENCE_IDENTITIES)
164    }
165
166    #[doc(hidden)]
167    pub fn with_limits(max_sources: usize, max_identities: usize) -> Self {
168        Self {
169            last_by_source: BTreeMap::new(),
170            accepted_identities: BTreeSet::new(),
171            max_sources: max_sources.max(1),
172            max_identities: max_identities.max(1),
173        }
174    }
175
176    pub fn try_identify(
177        &mut self,
178        record: EspEvidenceRecord,
179    ) -> Result<EspIdentifiedEvidenceRecord, EspEvidenceIdentityError> {
180        let source = record_identity_source(&record);
181        // Only the unbounded raw-log tail (stream evidence) is subject to the source
182        // and identity ledger caps. Structured ESP evidence -- registry and JSON
183        // observations that form sessions, workloads, and enrollment state -- is
184        // bounded by the device's registry and is precisely what the live view exists
185        // to surface, so it must never be rejected because the stream exhausted the
186        // budget. Without this, a long-running live session's persistent allocator
187        // saturates on tail lines and drops the late-arriving structured records,
188        // leaving the reduction stuck at phase=NotStarted/sessions=0/workloads=0 while
189        // a fresh restart of the same evidence reduces cleanly. The reducer's
190        // record-count/byte retention (which evicts stream evidence first) remains the
191        // ultimate memory backstop.
192        let enforce_caps = is_stream_evidence(&record);
193        if let Some(last) = self.last_by_source.get(&source) {
194            let occurrence_ordinal = last
195                .checked_add(1)
196                .ok_or(EspEvidenceIdentityError::OccurrenceOverflow)?;
197            if enforce_caps && self.accepted_identities.len() >= self.max_identities {
198                return Err(EspEvidenceIdentityError::IdentityLimit);
199            }
200            self.accepted_identities
201                .insert(record_identity_key(&record, occurrence_ordinal));
202            self.last_by_source.insert(source, occurrence_ordinal);
203            return Ok(EspIdentifiedEvidenceRecord::with_occurrence(
204                record,
205                occurrence_ordinal,
206            ));
207        }
208        if enforce_caps && self.last_by_source.len() >= self.max_sources {
209            return Err(EspEvidenceIdentityError::SourceLimit);
210        }
211        if enforce_caps && self.accepted_identities.len() >= self.max_identities {
212            return Err(EspEvidenceIdentityError::IdentityLimit);
213        }
214        self.accepted_identities
215            .insert(record_identity_key(&record, 0));
216        self.last_by_source.insert(source, 0);
217        Ok(EspIdentifiedEvidenceRecord::with_occurrence(record, 0))
218    }
219
220    fn try_synchronize_identified(
221        &mut self,
222        identified: &EspIdentifiedEvidenceRecord,
223    ) -> Result<bool, EspEvidenceIdentityError> {
224        let source = record_identity_source(identified.record());
225        let identity = record_identity_key(identified.record(), identified.occurrence_ordinal());
226        if self.accepted_identities.contains(&identity) {
227            return Ok(false);
228        }
229        // Structured evidence is exempt from the ledger caps for the same reason as in
230        // `try_identify`: only the unbounded stream tail is capped, so re-synchronizing
231        // an already-admitted structured record can never be rejected here.
232        let enforce_caps = is_stream_evidence(identified.record());
233        if enforce_caps
234            && !self.last_by_source.contains_key(&source)
235            && self.last_by_source.len() >= self.max_sources
236        {
237            return Err(EspEvidenceIdentityError::SourceLimit);
238        }
239        if enforce_caps && self.accepted_identities.len() >= self.max_identities {
240            return Err(EspEvidenceIdentityError::IdentityLimit);
241        }
242        self.accepted_identities.insert(identity);
243        self.last_by_source
244            .entry(source)
245            .and_modify(|last| *last = (*last).max(identified.occurrence_ordinal()))
246            .or_insert(identified.occurrence_ordinal());
247        Ok(true)
248    }
249
250    #[doc(hidden)]
251    pub fn tracked_source_count(&self) -> usize {
252        self.last_by_source.len()
253    }
254
255    #[doc(hidden)]
256    pub fn tracked_identity_count(&self) -> usize {
257        self.accepted_identities.len()
258    }
259}
260
261#[derive(Debug, Clone)]
262pub struct EspDiagnosticsReducer {
263    generated_at_utc: String,
264    records: VecDeque<RetainedEvidenceRecord>,
265    retained_serialized_bytes: usize,
266    next_ordinal: usize,
267    next_occurrence_by_key: BTreeMap<(String, String), usize>,
268    retained_occurrence_counts: BTreeMap<(String, String), usize>,
269    discarded_records: usize,
270    discarded_serialized_bytes: usize,
271    max_retained_records: usize,
272    max_retained_serialized_bytes: usize,
273    identity_allocator: EspEvidenceIdentityAllocator,
274    identified_high_watermark_by_source: BTreeMap<String, usize>,
275    identity_rejections: EspEvidenceIdentityRejectionCounts,
276    captured_elevation: Option<EspElevationState>,
277    captured_app_names: BTreeMap<String, String>,
278}
279
280#[derive(Debug, Clone)]
281struct RetainedEvidenceRecord {
282    ordinal: usize,
283    occurrence_ordinal: usize,
284    occurrence_key: Option<(String, String)>,
285    serialized_bytes: usize,
286    record: EspEvidenceRecord,
287}
288
289impl EspDiagnosticsReducer {
290    pub fn new(generated_at_utc: String) -> Self {
291        Self::with_retention_limits(
292            generated_at_utc,
293            MAX_RETAINED_EVIDENCE_RECORDS,
294            MAX_RETAINED_EVIDENCE_SERIALIZED_BYTES,
295        )
296    }
297
298    #[doc(hidden)]
299    pub fn with_retention_limits(
300        generated_at_utc: String,
301        max_retained_records: usize,
302        max_retained_serialized_bytes: usize,
303    ) -> Self {
304        Self {
305            generated_at_utc,
306            records: VecDeque::new(),
307            retained_serialized_bytes: 0,
308            next_ordinal: 0,
309            next_occurrence_by_key: BTreeMap::new(),
310            retained_occurrence_counts: BTreeMap::new(),
311            discarded_records: 0,
312            discarded_serialized_bytes: 0,
313            max_retained_records: max_retained_records.max(1),
314            max_retained_serialized_bytes: max_retained_serialized_bytes.max(1),
315            identity_allocator: EspEvidenceIdentityAllocator::new(),
316            identified_high_watermark_by_source: BTreeMap::new(),
317            identity_rejections: EspEvidenceIdentityRejectionCounts::default(),
318            captured_elevation: None,
319            captured_app_names: BTreeMap::new(),
320        }
321    }
322
323    pub fn ingest(&mut self, record: EspEvidenceRecord) {
324        let ordinal = self.next_ordinal;
325        self.next_ordinal = self.next_ordinal.saturating_add(1);
326        let serialized_bytes = self.serialized_bytes(&record);
327        if serialized_bytes > self.max_retained_serialized_bytes {
328            self.note_discard(serialized_bytes);
329            return;
330        }
331
332        let source = record_identity_source(&record);
333        let occurrence_key = record_occurrence_key(&record);
334        let occurrence_ordinal = if let Some(key) = occurrence_key.as_ref() {
335            let key_next = self.next_occurrence_by_key.get(key).copied().unwrap_or(0);
336            let identified_floor = match self.identified_high_watermark_by_source.get(&source) {
337                Some(high_watermark) => match high_watermark.checked_add(1) {
338                    Some(next) => next,
339                    None => {
340                        self.note_identity_discard(EspEvidenceIdentityError::OccurrenceOverflow);
341                        return;
342                    }
343                },
344                None => 0,
345            };
346            key_next.max(identified_floor)
347        } else {
348            ordinal
349        };
350
351        let identified = EspIdentifiedEvidenceRecord::with_occurrence(record, occurrence_ordinal);
352        match self
353            .identity_allocator
354            .try_synchronize_identified(&identified)
355        {
356            Ok(true) => {}
357            Ok(false) => return,
358            Err(error) => {
359                self.note_identity_discard(error);
360                return;
361            }
362        }
363        if let Some(key) = occurrence_key.as_ref() {
364            self.next_occurrence_by_key
365                .insert(key.clone(), occurrence_ordinal.saturating_add(1));
366        }
367        self.retain_record(
368            identified.record,
369            ordinal,
370            occurrence_ordinal,
371            occurrence_key,
372            serialized_bytes,
373        );
374    }
375
376    fn serialized_bytes(&self, record: &EspEvidenceRecord) -> usize {
377        serde_json::to_vec(record)
378            .map(|serialized| serialized.len())
379            .unwrap_or_else(|_| self.max_retained_serialized_bytes.saturating_add(1))
380    }
381
382    fn retain_record(
383        &mut self,
384        record: EspEvidenceRecord,
385        ordinal: usize,
386        occurrence_ordinal: usize,
387        occurrence_key: Option<(String, String)>,
388        serialized_bytes: usize,
389    ) {
390        // The current-process elevation is a single authoritative fact, but it rides on
391        // a `System` record that `is_stream_evidence` treats as evictable. Under evidence
392        // pressure that record is dropped from the retained set before projection, which
393        // left `snapshot.elevation` at its default `false` and fired a spurious
394        // "not elevated" finding on an actually-elevated capture. Capture the elevation
395        // here, before any eviction, so the projection reflects the real elevation
396        // regardless of retention (covers live and imported evidence alike).
397        if let EspEvidenceRecord::System(observation) = &record {
398            if let EspSystemFact::Elevation(elevation) = &observation.fact {
399                self.captured_elevation = Some(elevation.clone());
400            }
401        }
402        // Capture the app's friendly name from IME evidence at ingest, before retention
403        // can evict the (high-volume, stream) IME line. The classic workload registry
404        // only knows GUIDs and Graph naming is usually unavailable during ESP, so this
405        // durable GUID -> name map is the only way to show real app names live even after
406        // the naming log lines are shed. Keyed by normalized GUID to match workload ids.
407        if let EspEvidenceRecord::Ime(observation) = &record {
408            if let Some(app_id) = observation
409                .app_id
410                .clone()
411                .or_else(|| extract_app_id(&observation.message))
412            {
413                if let Some(guid) = extract_guid(&app_id) {
414                    if let Some(name) = extract_app_name(&observation.message) {
415                        if !is_fallback_name(&name) {
416                            self.captured_app_names.insert(guid, name);
417                        }
418                    }
419                }
420            }
421        }
422        if let Some(key) = &occurrence_key {
423            let retained_count = self
424                .retained_occurrence_counts
425                .entry(key.clone())
426                .or_insert(0);
427            *retained_count = retained_count.saturating_add(1);
428        }
429        self.retained_serialized_bytes = self
430            .retained_serialized_bytes
431            .saturating_add(serialized_bytes);
432        self.records.push_back(RetainedEvidenceRecord {
433            ordinal,
434            occurrence_ordinal,
435            occurrence_key,
436            serialized_bytes,
437            record,
438        });
439        while self.records.len() > self.max_retained_records
440            || self.retained_serialized_bytes > self.max_retained_serialized_bytes
441        {
442            let eviction_index = self
443                .records
444                .iter()
445                .position(|retained| is_stream_evidence(&retained.record))
446                .unwrap_or(0);
447            let Some(discarded) = self.records.remove(eviction_index) else {
448                break;
449            };
450            self.retained_serialized_bytes = self
451                .retained_serialized_bytes
452                .saturating_sub(discarded.serialized_bytes);
453            self.release_identity(&discarded.record, discarded.occurrence_ordinal);
454            self.release_occurrence_key(discarded.occurrence_key.as_ref());
455            self.note_discard(discarded.serialized_bytes);
456        }
457    }
458
459    pub fn ingest_all<I: IntoIterator<Item = EspEvidenceRecord>>(&mut self, records: I) {
460        for record in records {
461            self.ingest(record);
462        }
463    }
464
465    #[doc(hidden)]
466    pub fn ingest_identified(&mut self, identified: EspIdentifiedEvidenceRecord) {
467        let serialized_bytes = self.serialized_bytes(identified.record());
468        if serialized_bytes > self.max_retained_serialized_bytes {
469            self.note_discard(serialized_bytes);
470            return;
471        }
472        match self
473            .identity_allocator
474            .try_synchronize_identified(&identified)
475        {
476            Ok(true) => {}
477            Ok(false) => return,
478            Err(error) => {
479                self.note_identity_discard(error);
480                return;
481            }
482        }
483        let source = record_identity_source(identified.record());
484        self.identified_high_watermark_by_source
485            .entry(source)
486            .and_modify(|watermark| *watermark = (*watermark).max(identified.occurrence_ordinal()))
487            .or_insert(identified.occurrence_ordinal());
488        let ordinal = self.next_ordinal;
489        self.next_ordinal = self.next_ordinal.saturating_add(1);
490        let occurrence_ordinal = identified.occurrence_ordinal();
491        let record = identified.record;
492        let occurrence_key = record_occurrence_key(&record);
493        if let Some(key) = &occurrence_key {
494            let next = self.next_occurrence_by_key.entry(key.clone()).or_insert(0);
495            *next = (*next).max(occurrence_ordinal.saturating_add(1));
496        }
497        self.retain_record(
498            record,
499            ordinal,
500            occurrence_ordinal,
501            occurrence_key,
502            serialized_bytes,
503        );
504    }
505
506    #[doc(hidden)]
507    pub fn ingest_identified_all<I: IntoIterator<Item = EspIdentifiedEvidenceRecord>>(
508        &mut self,
509        records: I,
510    ) {
511        for record in records {
512            self.ingest_identified(record);
513        }
514    }
515
516    #[doc(hidden)]
517    pub fn merge_identity_rejections(&mut self, rejections: EspEvidenceIdentityRejectionCounts) {
518        self.identity_rejections.merge(rejections);
519    }
520
521    pub fn snapshot(&self) -> EspDiagnosticsSnapshot {
522        let scenario = classify_scenario(self.records.iter().map(|retained| &retained.record));
523        let occurrence_ordinals = self
524            .records
525            .iter()
526            .map(|retained| (retained.ordinal, retained.occurrence_ordinal))
527            .collect();
528        let mut projection =
529            SnapshotProjection::new(self.generated_at_utc.clone(), scenario, occurrence_ordinals);
530        for retained in &self.records {
531            let ordinal = retained.ordinal;
532            let record = &retained.record;
533            if is_raw_hardware_hash_record(record) {
534                continue;
535            }
536            if let Some(raw) = raw_evidence_record(record, retained.occurrence_ordinal) {
537                projection.raw_evidence.push(raw);
538            }
539            if record_allowed_for_scenario(record, &projection.scenario) {
540                projection.process_record(ordinal, record);
541            }
542        }
543        if let Some(coverage) = self.retention_coverage() {
544            projection
545                .occurrence_ordinals
546                .insert(RETENTION_COVERAGE_ORDINAL, 0);
547            projection.process_coverage(RETENTION_COVERAGE_ORDINAL, coverage);
548        }
549        if let Some(coverage) = self.identity_limit_coverage() {
550            projection
551                .occurrence_ordinals
552                .insert(IDENTITY_COVERAGE_ORDINAL, 0);
553            projection.process_coverage(IDENTITY_COVERAGE_ORDINAL, coverage);
554        }
555        // Elevation is authoritative from ingest (see `retain_record`) and must not be
556        // lost when the underlying `System` record is evicted from the retained set.
557        if let Some(elevation) = &self.captured_elevation {
558            projection.elevation = elevation.clone();
559        }
560        let mut snapshot = projection.finish();
561        self.apply_captured_app_names(&mut snapshot);
562        snapshot
563    }
564
565    /// Fill any workload the registry/Graph left unnamed with the friendly name captured
566    /// from IME evidence at ingest (see `retain_record`). Graph names are applied in
567    /// `finish` and win; this only fills a still-`None` display name, and survives IME
568    /// eviction because the map is built at ingest rather than from retained records.
569    fn apply_captured_app_names(&self, snapshot: &mut EspDiagnosticsSnapshot) {
570        if self.captured_app_names.is_empty() {
571            return;
572        }
573        for workload in snapshot
574            .workloads
575            .iter_mut()
576            .filter(|workload| workload.display_name.is_none())
577        {
578            if let Some(guid) = extract_guid(&workload.raw_identifier) {
579                if let Some(name) = self.captured_app_names.get(&guid) {
580                    workload.display_name = Some(name.clone());
581                }
582            }
583        }
584    }
585
586    fn note_discard(&mut self, serialized_bytes: usize) {
587        self.discarded_records = self.discarded_records.saturating_add(1);
588        self.discarded_serialized_bytes = self
589            .discarded_serialized_bytes
590            .saturating_add(serialized_bytes);
591    }
592
593    fn release_occurrence_key(&mut self, key: Option<&(String, String)>) {
594        let Some(key) = key else {
595            return;
596        };
597        let remove_key = if let Some(count) = self.retained_occurrence_counts.get_mut(key) {
598            *count = count.saturating_sub(1);
599            *count == 0
600        } else {
601            false
602        };
603        if remove_key {
604            self.retained_occurrence_counts.remove(key);
605            self.next_occurrence_by_key.remove(key);
606        }
607    }
608
609    fn release_identity(&mut self, record: &EspEvidenceRecord, occurrence_ordinal: usize) {
610        let source = record_identity_source(record);
611        self.identity_allocator
612            .accepted_identities
613            .remove(&record_identity_key(record, occurrence_ordinal));
614        let source_is_retained = self
615            .identity_allocator
616            .accepted_identities
617            .iter()
618            .any(|identity| identity.source_artifact_id == source);
619        if !source_is_retained
620            && !self
621                .identified_high_watermark_by_source
622                .contains_key(&source)
623        {
624            self.identity_allocator.last_by_source.remove(&source);
625        }
626    }
627
628    fn retention_coverage(&self) -> Option<EspArtifactCoverage> {
629        (self.discarded_records != 0).then(|| EspArtifactCoverage {
630            artifact_id: RETENTION_COVERAGE_ARTIFACT_ID.to_string(),
631            family: RETENTION_COVERAGE_FAMILY.to_string(),
632            status: EspArtifactStatus::ParseFailed,
633            detail: Some(format!(
634                "Retained evidence is capped at {} records and {} serialized bytes; {} older or oversized {} totaling {} serialized bytes were discarded. Derived conclusions use retained evidence only.",
635                self.max_retained_records,
636                self.max_retained_serialized_bytes,
637                self.discarded_records,
638                if self.discarded_records == 1 {
639                    "record"
640                } else {
641                    "records"
642                },
643                self.discarded_serialized_bytes,
644            )),
645            observed_at_utc: self.generated_at_utc.clone(),
646            evidence: Vec::new(),
647        })
648    }
649
650    fn identity_limit_coverage(&self) -> Option<EspArtifactCoverage> {
651        let discarded = self.identity_rejections.total();
652        (discarded != 0).then(|| EspArtifactCoverage {
653            artifact_id: IDENTITY_COVERAGE_ARTIFACT_ID.to_string(),
654            family: IDENTITY_COVERAGE_FAMILY.to_string(),
655            status: EspArtifactStatus::ParseFailed,
656            detail: Some(format!(
657                "bounded evidence-identity tracking discarded {discarded} {} ({} source-limit; {} identity-limit; {} occurrence-overflow) with maxima of {} tracked sources and {} tracked identities",
658                if discarded == 1 { "record" } else { "records" },
659                self.identity_rejections.source_limit,
660                self.identity_rejections.identity_limit,
661                self.identity_rejections.occurrence_overflow,
662                MAX_EVIDENCE_IDENTITY_SOURCES,
663                MAX_TRACKED_EVIDENCE_IDENTITIES,
664            )),
665            observed_at_utc: self.generated_at_utc.clone(),
666            evidence: Vec::new(),
667        })
668    }
669
670    fn note_identity_discard(&mut self, error: EspEvidenceIdentityError) {
671        self.identity_rejections.record(error);
672    }
673}
674
675fn is_stream_evidence(record: &EspEvidenceRecord) -> bool {
676    // "Stream" evidence is the unbounded, high-volume material (log tails, event
677    // records, DO/Graph polling) that retention may evict first and that the
678    // per-session identity ledger caps. `System` facts are deliberately NOT here:
679    // there are only a handful per poll (hostname, serial, Entra device id, tenant,
680    // elevation, OS/hardware), they reconcile to stable identities across polls, and
681    // they are the ONLY source of the reduced device identity. Treating them as
682    // evictable stream let them be dropped under raw-log pressure, which emptied the
683    // identity and made Graph device correlation fail ("Blocked by deviceMatch") even
684    // once the device was managed. Keep them protected like registry/JSON evidence.
685    matches!(
686        record,
687        EspEvidenceRecord::EventLog(_)
688            | EspEvidenceRecord::Ime(_)
689            | EspEvidenceRecord::DeploymentLog(_)
690            | EspEvidenceRecord::Process(_)
691            | EspEvidenceRecord::DeliveryOptimization(_)
692            | EspEvidenceRecord::Graph(_)
693    )
694}
695
696struct SnapshotProjection {
697    generated_at_utc: String,
698    scenario: EspScenario,
699    occurrence_ordinals: BTreeMap<usize, usize>,
700    elevation: EspElevationState,
701    identity: EspIdentityEvidence,
702    profile: Option<EspProfileEvidence>,
703    enrollments: Vec<EspEnrollmentEvidence>,
704    sessions: Vec<EspSession>,
705    workloads: Vec<EspWorkload>,
706    node_cache: BTreeMap<u64, NodeCacheAccumulator>,
707    registration_events: Vec<EspRegistrationEvent>,
708    delivery_optimization: Option<EspDeliveryOptimizationEvidence>,
709    hardware: Option<EspHardwareEvidence>,
710    activity: Vec<(usize, EspTimelineEntry)>,
711    coverage: Vec<EspArtifactCoverage>,
712    raw_evidence: Vec<EspRawEvidenceRecord>,
713    process_observations: Vec<EspProcessObservation>,
714    deployment_log_observations: Vec<EspDeploymentLogObservation>,
715    ime_observations: Vec<EspImeObservation>,
716    v2_workloads: BTreeMap<(String, String, usize), V2WorkloadAccumulator>,
717    platform_scripts: BTreeMap<(String, String), PlatformScriptAccumulator>,
718    graph_observations: Vec<EspGraphObservation>,
719    office_details: Vec<OfficeDetailObservation>,
720    msi_details: Vec<MsiDetailObservation>,
721    enforcement_messages: BTreeMap<String, EnforcementMessageAccumulator>,
722    deferred_error_codes: Vec<DeferredErrorCode>,
723    sidecar_app_states: Vec<SidecarAppStateObservation>,
724}
725
726impl SnapshotProjection {
727    fn new(
728        generated_at_utc: String,
729        scenario: EspScenario,
730        occurrence_ordinals: BTreeMap<usize, usize>,
731    ) -> Self {
732        Self {
733            generated_at_utc,
734            scenario,
735            occurrence_ordinals,
736            elevation: EspElevationState {
737                is_elevated: false,
738                restart_supported: false,
739                restricted_sources: Vec::new(),
740            },
741            identity: EspIdentityEvidence {
742                device_name: None,
743                managed_device_id: None,
744                entra_device_id: None,
745                entdm_id: None,
746                tenant_id: None,
747                tenant_domain: None,
748                user_principal_name: None,
749                serial_number: None,
750                evidence: Vec::new(),
751            },
752            profile: None,
753            enrollments: Vec::new(),
754            sessions: Vec::new(),
755            workloads: Vec::new(),
756            node_cache: BTreeMap::new(),
757            registration_events: Vec::new(),
758            delivery_optimization: None,
759            hardware: None,
760            activity: Vec::new(),
761            coverage: Vec::new(),
762            raw_evidence: Vec::new(),
763            process_observations: Vec::new(),
764            deployment_log_observations: Vec::new(),
765            ime_observations: Vec::new(),
766            v2_workloads: BTreeMap::new(),
767            platform_scripts: BTreeMap::new(),
768            graph_observations: Vec::new(),
769            office_details: Vec::new(),
770            msi_details: Vec::new(),
771            enforcement_messages: BTreeMap::new(),
772            deferred_error_codes: Vec::new(),
773            sidecar_app_states: Vec::new(),
774        }
775    }
776
777    fn process_record(&mut self, ordinal: usize, record: &EspEvidenceRecord) {
778        match record {
779            EspEvidenceRecord::Coverage(coverage) => {
780                self.process_coverage(ordinal, coverage.clone());
781            }
782            _ if !record_is_usable(record) => {}
783            EspEvidenceRecord::Registry(observation) => self.process_registry(ordinal, observation),
784            EspEvidenceRecord::Json(observation) => self.process_json(ordinal, observation),
785            EspEvidenceRecord::EventLog(observation) => self.process_event(ordinal, observation),
786            EspEvidenceRecord::Ime(observation) => self.process_ime(ordinal, observation),
787            EspEvidenceRecord::DeploymentLog(observation) => {
788                self.process_deployment_log(ordinal, observation)
789            }
790            EspEvidenceRecord::Process(observation) => self.process_process(ordinal, observation),
791            EspEvidenceRecord::System(observation) => self.process_system(observation),
792            EspEvidenceRecord::DeliveryOptimizationSummary(summary) => {
793                self.process_delivery_optimization_summary(summary)
794            }
795            EspEvidenceRecord::DeliveryOptimization(observation) => {
796                self.process_delivery_optimization(ordinal, observation)
797            }
798            EspEvidenceRecord::Graph(observation) => {
799                self.graph_observations.push(observation.clone())
800            }
801        }
802    }
803
804    fn process_registry(&mut self, ordinal: usize, observation: &EspRegistryObservation) {
805        if self.process_node_cache(observation) {
806            return;
807        }
808        if self.process_enrollment(observation) {
809            return;
810        }
811        if is_platform_script_observation(observation) {
812            self.accumulate_platform_script(ordinal, observation);
813            return;
814        }
815        if let Some(detail) = msi_detail_observation(ordinal, observation) {
816            self.msi_details.push(detail);
817            return;
818        }
819        if let Some(detail) = office_detail_observation(ordinal, observation) {
820            self.office_details.push(detail);
821            return;
822        }
823        if let Some(deferred) = deferred_error_code_observation(ordinal, observation) {
824            self.deferred_error_codes.push(deferred);
825            return;
826        }
827        if let Some(sidecar) = sidecar_app_state_observation(ordinal, observation) {
828            self.sidecar_app_states.push(sidecar);
829            return;
830        }
831        if let Some(session_info) = classic_session_info(observation) {
832            self.process_classic_workload(ordinal, observation, session_info);
833            return;
834        }
835
836        let name = observation.value_name.to_ascii_lowercase();
837        match name.as_str() {
838            "deploymentprofilename" => {
839                if let Some(value) = observation_text(&observation.value) {
840                    self.ensure_profile().profile_name = Some(value);
841                    self.add_profile_evidence(&observation.context.evidence_ref);
842                }
843            }
844            "cloudassignedtenantdomain" => {
845                if let Some(value) = observation_text(&observation.value) {
846                    let classified = sensitive_string(value);
847                    self.ensure_profile().tenant_domain = Some(classified.clone());
848                    self.identity.tenant_domain = Some(classified);
849                    self.add_identity_and_profile_evidence(&observation.context.evidence_ref);
850                }
851            }
852            "cloudassignedtenantid" | "aadtenantid" => {
853                if let Some(value) = observation_text(&observation.value) {
854                    let classified = sensitive_string(value);
855                    self.ensure_profile().tenant_id = Some(classified.clone());
856                    self.identity.tenant_id = Some(classified);
857                    self.add_identity_and_profile_evidence(&observation.context.evidence_ref);
858                }
859            }
860            "ztdcorrelationid" => {
861                if let Some(value) = observation_text(&observation.value) {
862                    self.ensure_profile().correlation_id = Some(value);
863                    self.add_profile_evidence(&observation.context.evidence_ref);
864                }
865            }
866            "cloudassignedoobeconfig" => {
867                if let Some(value) = observation_u64(&observation.value) {
868                    self.ensure_profile().oobe_config = Some(decode_oobe_config(value));
869                    self.add_profile_evidence(&observation.context.evidence_ref);
870                }
871            }
872            "entdmid" => {
873                if let Some(value) = observation_text(&observation.value) {
874                    self.identity.entdm_id = Some(sensitive_string(value));
875                    self.identity
876                        .evidence
877                        .push(observation.context.evidence_ref.clone());
878                }
879            }
880            "upn" | "userprincipalname" => {
881                if let Some(value) = observation_text(&observation.value) {
882                    self.identity.user_principal_name = Some(sensitive_string(value));
883                    self.identity
884                        .evidence
885                        .push(observation.context.evidence_ref.clone());
886                }
887            }
888            "odjapplied" => {
889                if let Some(value) = observation_bool(&observation.value) {
890                    self.ensure_profile().odj_applied = Some(value);
891                    self.add_profile_evidence(&observation.context.evidence_ref);
892                    self.push_timeline(
893                        ordinal,
894                        &observation.context,
895                        EspTimelineKind::OfflineDomainJoin,
896                        "Offline Domain Join".to_string(),
897                        Some(
898                            if value {
899                                "ODJ applied"
900                            } else {
901                                "ODJ not applied"
902                            }
903                            .to_string(),
904                        ),
905                        Some(boolean_status(value)),
906                    );
907                }
908            }
909            "autopilotdeviceprephint" => {
910                self.ensure_device_preparation()
911                    .evidence
912                    .push(observation.context.evidence_ref.clone());
913                self.add_profile_evidence(&observation.context.evidence_ref);
914            }
915            _ => {}
916        }
917    }
918
919    fn process_json(&mut self, ordinal: usize, observation: &EspJsonObservation) {
920        if is_provisioning_progress(observation) {
921            self.accumulate_v2_workload(ordinal, observation);
922            return;
923        }
924        if is_enforcement_state_message(observation) {
925            self.accumulate_enforcement_message(ordinal, observation);
926            return;
927        }
928
929        let pointer = observation.json_pointer.to_ascii_lowercase();
930        if is_page_settings(observation) {
931            let evidence = observation.context.evidence_ref.clone();
932            let device_preparation = self.ensure_device_preparation();
933            match pointer.as_str() {
934                "/agentdownloadtimeoutseconds" => {
935                    device_preparation.agent_download_timeout_seconds =
936                        observation_u64(&observation.value)
937                }
938                "/pagetimeoutseconds" => {
939                    device_preparation.page_timeout_seconds = observation_u64(&observation.value)
940                }
941                "/allowskiponfailure" => {
942                    device_preparation.allow_skip_on_failure = observation_bool(&observation.value)
943                }
944                "/allowdiagnostics" => {
945                    device_preparation.allow_diagnostics = observation_bool(&observation.value)
946                }
947                _ if pointer.starts_with("/scripts/") => {
948                    if let Some(value) = observation_text(&observation.value) {
949                        device_preparation.script_ids.push(value);
950                    }
951                }
952                _ => return,
953            }
954            device_preparation.evidence.push(evidence.clone());
955            self.add_profile_evidence(&evidence);
956            return;
957        }
958
959        match pointer.as_str() {
960            "/policydownloaddate" => {
961                if let Some(value) = observation_text(&observation.value) {
962                    let normalized = normalize_timestamp(&value, None);
963                    self.ensure_profile().profile_download_time = Some(normalized.clone());
964                    self.add_profile_evidence(&observation.context.evidence_ref);
965                    self.activity.push((
966                        ordinal,
967                        EspTimelineEntry {
968                            entry_id: self.timeline_entry_id(&observation.context, ordinal),
969                            timestamp: normalized,
970                            kind: EspTimelineKind::ProfileDownload,
971                            title: "Autopilot profile".to_string(),
972                            detail: Some("Profile downloaded".to_string()),
973                            status: Some(text_status(
974                                "Profile downloaded",
975                                EspNormalizedStatus::Processed,
976                            )),
977                            evidence: vec![observation.context.evidence_ref.clone()],
978                        },
979                    ));
980                }
981            }
982            "/cloudassigneddomainjoinmethod" => {
983                if let Some(value) = observation_i64(&observation.value) {
984                    self.ensure_profile().join_mode = Some(if value == 1 {
985                        EspJoinMode::HybridEntra
986                    } else {
987                        EspJoinMode::Entra
988                    });
989                    self.add_profile_evidence(&observation.context.evidence_ref);
990                }
991            }
992            "/hybridjoinskipdcconnectivitycheck" => {
993                if let Some(value) = observation_bool(&observation.value) {
994                    self.ensure_profile().skip_domain_connectivity_check = Some(value);
995                    self.add_profile_evidence(&observation.context.evidence_ref);
996                }
997            }
998            "/deploymentprofilename" => {
999                if let Some(value) = observation_text(&observation.value) {
1000                    self.ensure_profile().profile_name = Some(value);
1001                    self.add_profile_evidence(&observation.context.evidence_ref);
1002                }
1003            }
1004            "/ztdcorrelationid" => {
1005                if let Some(value) = observation_text(&observation.value) {
1006                    self.ensure_profile().correlation_id = Some(value);
1007                    self.add_profile_evidence(&observation.context.evidence_ref);
1008                }
1009            }
1010            _ => {}
1011        }
1012    }
1013
1014    fn process_event(&mut self, ordinal: usize, observation: &EspEventLogObservation) {
1015        if !is_parity_event(observation.event_id) {
1016            return;
1017        }
1018        let odj_state = odj_state_details(observation);
1019        let message = odj_state
1020            .as_ref()
1021            .map(|(message, _)| (*message).to_string())
1022            .or_else(|| observation.message.clone())
1023            .unwrap_or_else(|| event_default_message(observation.event_id).to_string());
1024        let normalized = odj_state
1025            .map(|(_, normalized)| normalized)
1026            .unwrap_or_else(|| event_normalized_status(observation.event_id));
1027        let event_status = text_status(&message, normalized);
1028        let kind = event_timeline_kind(observation.event_id);
1029        let title = event_title(observation.event_id, &observation.named_data);
1030        self.push_timeline(
1031            ordinal,
1032            &observation.context,
1033            kind,
1034            title,
1035            Some(message.clone()),
1036            Some(event_status.clone()),
1037        );
1038
1039        if matches!(observation.event_id, 101 | 304 | 306) {
1040            self.registration_events.push(EspRegistrationEvent {
1041                event_id: observation.event_id,
1042                record_id: observation.record_id,
1043                status: event_status,
1044                message,
1045                timestamp: context_timestamp(&observation.context),
1046                named_data: observation.named_data.clone(),
1047                evidence: vec![observation.context.evidence_ref.clone()],
1048            });
1049        }
1050    }
1051
1052    fn process_ime(&mut self, ordinal: usize, observation: &EspImeObservation) {
1053        self.ime_observations.push(observation.clone());
1054        self.push_timeline(
1055            ordinal,
1056            &observation.context,
1057            EspTimelineKind::Workload,
1058            observation.message.clone(),
1059            observation.app_id.clone(),
1060            observation.status.clone(),
1061        );
1062    }
1063
1064    fn process_deployment_log(
1065        &mut self,
1066        ordinal: usize,
1067        observation: &EspDeploymentLogObservation,
1068    ) {
1069        self.deployment_log_observations.push(observation.clone());
1070        self.push_timeline(
1071            ordinal,
1072            &observation.context,
1073            EspTimelineKind::Workload,
1074            observation.message.clone(),
1075            observation.product_code.clone(),
1076            observation.status.clone(),
1077        );
1078    }
1079
1080    fn process_process(&mut self, ordinal: usize, observation: &EspProcessObservation) {
1081        self.process_observations.push(observation.clone());
1082        self.push_timeline(
1083            ordinal,
1084            &observation.context,
1085            EspTimelineKind::Process,
1086            observation.executable_name.clone(),
1087            Some(format!("PID {}", observation.pid)),
1088            None,
1089        );
1090    }
1091
1092    fn process_system(&mut self, observation: &EspSystemObservation) {
1093        match &observation.fact {
1094            EspSystemFact::Elevation(value) => self.elevation = value.clone(),
1095            EspSystemFact::Hostname(value) => {
1096                self.identity.device_name = Some(value.clone());
1097                self.identity
1098                    .evidence
1099                    .push(observation.context.evidence_ref.clone());
1100            }
1101            EspSystemFact::EntraDeviceId(value) => {
1102                self.identity.entra_device_id = Some(value.clone());
1103                self.identity
1104                    .evidence
1105                    .push(observation.context.evidence_ref.clone());
1106            }
1107            EspSystemFact::TenantId(value) => {
1108                let classified = sensitive_string(value.clone());
1109                self.identity.tenant_id = Some(classified.clone());
1110                self.ensure_profile().tenant_id = Some(classified);
1111                self.add_identity_and_profile_evidence(&observation.context.evidence_ref);
1112            }
1113            EspSystemFact::JoinMode(value) => {
1114                self.ensure_profile().join_mode = Some(value.clone());
1115                self.add_profile_evidence(&observation.context.evidence_ref);
1116            }
1117            EspSystemFact::OsVersion(value) => {
1118                self.ensure_hardware().os_version = Some(value.clone())
1119            }
1120            EspSystemFact::OsBuild(value) => self.ensure_hardware().os_build = Some(value.clone()),
1121            EspSystemFact::Manufacturer(value) => {
1122                self.ensure_hardware().manufacturer = Some(value.clone())
1123            }
1124            EspSystemFact::Model(value) => self.ensure_hardware().model = Some(value.clone()),
1125            EspSystemFact::SerialNumber(value) => {
1126                self.ensure_hardware().serial_number = Some(sensitive_string(value.clone()));
1127                self.identity.serial_number = Some(sensitive_string(value.clone()));
1128                self.identity
1129                    .evidence
1130                    .push(observation.context.evidence_ref.clone());
1131            }
1132            EspSystemFact::TpmVersion(value) => {
1133                self.ensure_hardware().tpm_version = Some(value.clone())
1134            }
1135        }
1136        if !matches!(observation.fact, EspSystemFact::Elevation(_)) {
1137            self.ensure_hardware()
1138                .evidence
1139                .push(observation.context.evidence_ref.clone());
1140        }
1141    }
1142
1143    fn process_delivery_optimization(
1144        &mut self,
1145        ordinal: usize,
1146        observation: &EspDeliveryOptimizationObservation,
1147    ) {
1148        let transfer = EspDeliveryOptimizationTransfer {
1149            transfer_id: self.record_id("transfer", &observation.context, ordinal),
1150            kind: observation.kind.clone(),
1151            content_id: observation.content_id.clone(),
1152            app_id: observation.app_id.clone(),
1153            timestamp: context_timestamp(&observation.context),
1154            evidence: vec![observation.context.evidence_ref.clone()],
1155        };
1156        let delivery = self
1157            .delivery_optimization
1158            .get_or_insert_with(empty_delivery_optimization);
1159        if let Some(value) = observation.http_bytes {
1160            delivery.download_http_bytes = value;
1161        }
1162        if let Some(value) = observation.lan_bytes {
1163            delivery.download_lan_bytes = value;
1164        }
1165        if let Some(value) = observation.cache_host_bytes {
1166            delivery.download_cache_host_bytes = value;
1167        }
1168        delivery.transfers.push(transfer);
1169        delivery
1170            .evidence
1171            .push(observation.context.evidence_ref.clone());
1172
1173        let (detail, status) = match observation.kind {
1174            EspDeliveryOptimizationEventKind::DownloadStarted => {
1175                ("Download started", EspNormalizedStatus::Downloading)
1176            }
1177            EspDeliveryOptimizationEventKind::DownloadCompleted => {
1178                ("Download completed", EspNormalizedStatus::Downloaded)
1179            }
1180        };
1181        self.push_timeline(
1182            ordinal,
1183            &observation.context,
1184            EspTimelineKind::DeliveryOptimization,
1185            observation
1186                .app_id
1187                .clone()
1188                .unwrap_or_else(|| "Delivery Optimization".to_string()),
1189            Some(detail.to_string()),
1190            Some(text_status(detail, status)),
1191        );
1192    }
1193
1194    fn process_delivery_optimization_summary(&mut self, summary: &EspDeliveryOptimizationEvidence) {
1195        let delivery = self
1196            .delivery_optimization
1197            .get_or_insert_with(empty_delivery_optimization);
1198        delivery.download_http_bytes = summary.download_http_bytes;
1199        delivery.download_lan_bytes = summary.download_lan_bytes;
1200        delivery.download_cache_host_bytes = summary.download_cache_host_bytes;
1201        delivery.peer_share_percent = summary.peer_share_percent;
1202        delivery.connected_cache_share_percent = summary.connected_cache_share_percent;
1203        delivery.transfers.extend(summary.transfers.iter().cloned());
1204        delivery.evidence.extend(summary.evidence.iter().cloned());
1205    }
1206
1207    fn process_coverage(&mut self, ordinal: usize, coverage: EspArtifactCoverage) {
1208        let source = coverage
1209            .evidence
1210            .first()
1211            .map(|evidence| evidence.source_artifact_id.clone())
1212            .unwrap_or_else(|| coverage.artifact_id.clone());
1213        let evidence_id = coverage
1214            .evidence
1215            .first()
1216            .map(|evidence| evidence.evidence_id.clone())
1217            .unwrap_or_else(|| coverage.artifact_id.clone());
1218        let context = synthetic_context(
1219            EspSourceKind::Coverage,
1220            &source,
1221            &evidence_id,
1222            &coverage.observed_at_utc,
1223        );
1224        self.push_timeline(
1225            ordinal,
1226            &context,
1227            EspTimelineKind::Coverage,
1228            coverage.family.clone(),
1229            coverage.detail.clone(),
1230            Some(text_status(
1231                &format!("{:?}", coverage.status),
1232                match coverage.status {
1233                    EspArtifactStatus::Available => EspNormalizedStatus::Processed,
1234                    EspArtifactStatus::Missing
1235                    | EspArtifactStatus::PermissionDenied
1236                    | EspArtifactStatus::ParseFailed
1237                    | EspArtifactStatus::Unsupported => EspNormalizedStatus::Unknown,
1238                },
1239            )),
1240        );
1241        self.coverage.push(coverage);
1242    }
1243
1244    fn process_node_cache(&mut self, observation: &EspRegistryObservation) -> bool {
1245        if !observation.key.to_ascii_lowercase().contains("nodecache") {
1246            return false;
1247        }
1248        let Some(index) =
1249            last_path_component(&observation.key).and_then(|value| value.parse::<u64>().ok())
1250        else {
1251            return false;
1252        };
1253        let value_name = observation.value_name.to_ascii_lowercase();
1254        if !matches!(value_name.as_str(), "nodeuri" | "expectedvalue") {
1255            return false;
1256        }
1257        let Some(value) = observation_text(&observation.value) else {
1258            return false;
1259        };
1260        let entry = self
1261            .node_cache
1262            .entry(index)
1263            .or_insert_with(|| NodeCacheAccumulator {
1264                node_uri: None,
1265                expected_value: None,
1266                sensitivity: observation.context.sensitivity.clone(),
1267                evidence: Vec::new(),
1268            });
1269        match value_name.as_str() {
1270            "nodeuri" => entry.node_uri = Some(value),
1271            "expectedvalue" => entry.expected_value = Some(value),
1272            _ => unreachable!("value name was validated above"),
1273        }
1274        entry.sensitivity =
1275            more_restrictive_sensitivity(&entry.sensitivity, &observation.context.sensitivity);
1276        entry
1277            .evidence
1278            .push(observation.context.evidence_ref.clone());
1279        true
1280    }
1281
1282    fn process_enrollment(&mut self, observation: &EspRegistryObservation) -> bool {
1283        let components = path_components(&observation.key);
1284        let Some(enrollments_index) = components
1285            .iter()
1286            .position(|part| part.eq_ignore_ascii_case("enrollments"))
1287        else {
1288            return false;
1289        };
1290        let Some(enrollment_id) = components.get(enrollments_index + 1) else {
1291            return false;
1292        };
1293        let Some(update) = enrollment_update(observation) else {
1294            return false;
1295        };
1296        let entry = if let Some(index) = self
1297            .enrollments
1298            .iter()
1299            .position(|entry| entry.enrollment_id == *enrollment_id)
1300        {
1301            &mut self.enrollments[index]
1302        } else {
1303            self.enrollments.push(EspEnrollmentEvidence {
1304                enrollment_id: enrollment_id.clone(),
1305                provider_id: None,
1306                tenant_id: None,
1307                user_principal_name: None,
1308                entdm_id: None,
1309                settings: EspEnrollmentSettings {
1310                    device_esp_enabled: None,
1311                    user_esp_enabled: None,
1312                    timeout_seconds: None,
1313                    blocking: None,
1314                    allow_reset: None,
1315                    allow_retry: None,
1316                    continue_anyway: None,
1317                },
1318                evidence: Vec::new(),
1319            });
1320            self.enrollments.last_mut().expect("just inserted")
1321        };
1322        match update {
1323            EnrollmentUpdate::ProviderId(value) => entry.provider_id = Some(value),
1324            EnrollmentUpdate::TenantId(value) => entry.tenant_id = Some(sensitive_string(value)),
1325            EnrollmentUpdate::UserPrincipalName(value) => {
1326                entry.user_principal_name = Some(sensitive_string(value))
1327            }
1328            EnrollmentUpdate::EntdmId(value) => entry.entdm_id = Some(sensitive_string(value)),
1329            EnrollmentUpdate::DeviceEspEnabled(value) => {
1330                entry.settings.device_esp_enabled = Some(value)
1331            }
1332            EnrollmentUpdate::UserEspEnabled(value) => {
1333                entry.settings.user_esp_enabled = Some(value)
1334            }
1335            EnrollmentUpdate::TimeoutSeconds(value) => entry.settings.timeout_seconds = Some(value),
1336            EnrollmentUpdate::Blocking(bits) => {
1337                entry.settings.blocking = Some(bits != 0);
1338                entry.settings.allow_reset = Some(bits & 1 != 0);
1339                entry.settings.allow_retry = Some(bits & 2 != 0);
1340                entry.settings.continue_anyway = Some(bits & 4 != 0);
1341            }
1342        }
1343        entry
1344            .evidence
1345            .push(observation.context.evidence_ref.clone());
1346        true
1347    }
1348
1349    fn process_classic_workload(
1350        &mut self,
1351        ordinal: usize,
1352        observation: &EspRegistryObservation,
1353        session_info: ClassicSessionInfo,
1354    ) {
1355        let Some(kind) = classic_workload_kind(&session_info.family, &observation.value_name)
1356        else {
1357            return;
1358        };
1359        let raw_identifier = classic_raw_identifier(&kind, &observation.value_name);
1360        let source = &observation.context.provenance.source_artifact_id;
1361        let session_identity = classic_session_identity(&session_info);
1362        let session_id = classic_session_id(source, &session_info);
1363        let started_at = normalize_timestamp(&session_info.raw_time, None);
1364        let user_sid = session_info
1365            .user_sid
1366            .as_ref()
1367            .map(|value| sensitive_string(value.clone()));
1368        if let Some(session) = self
1369            .sessions
1370            .iter_mut()
1371            .find(|session| session.session_id == session_id)
1372        {
1373            session
1374                .evidence
1375                .push(observation.context.evidence_ref.clone());
1376        } else {
1377            self.sessions.push(EspSession {
1378                session_id: session_id.clone(),
1379                kind: EspSessionKind::Classic,
1380                scope: session_info.scope.clone(),
1381                user_sid,
1382                started_at: Some(started_at),
1383                ended_at: None,
1384                phase: match session_info.scope {
1385                    EspScope::Device => EspPhase::DeviceSetup,
1386                    EspScope::User => EspPhase::AccountSetup,
1387                },
1388                is_latest: false,
1389                workload_ids: Vec::new(),
1390                evidence: vec![observation.context.evidence_ref.clone()],
1391            });
1392        }
1393
1394        let workload_identity = format!(
1395            "{}:{}:{}",
1396            session_identity,
1397            tracked_kind_name(&kind),
1398            raw_identifier
1399        );
1400        let workload_id = format!(
1401            "workload|{}|{}|0",
1402            escape_component(source),
1403            escape_component(&workload_identity)
1404        );
1405        let raw_status = if kind == EspTrackedKind::Msi {
1406            EspRawStatus::Number(0)
1407        } else {
1408            observation_raw_status(&observation.value)
1409        };
1410        let normalized_status = normalize_for_kind(&kind, raw_status);
1411        let observed = context_timestamp(&observation.context);
1412        let timestamps = workload_timestamps(observed.clone(), &normalized_status.normalized);
1413
1414        if let Some(workload) = self
1415            .workloads
1416            .iter_mut()
1417            .find(|workload| workload.workload_id == workload_id)
1418        {
1419            let replace_status = workload
1420                .timestamps
1421                .last_updated
1422                .as_ref()
1423                .map(|current| {
1424                    timestamp_chronology_key(&observed) >= timestamp_chronology_key(current)
1425                })
1426                .unwrap_or(true);
1427            merge_workload_timestamps(&mut workload.timestamps, timestamps);
1428            if replace_status {
1429                workload.status = normalized_status.clone();
1430            }
1431            workload
1432                .evidence
1433                .push(observation.context.evidence_ref.clone());
1434        } else {
1435            self.workloads.push(EspWorkload {
1436                workload_id: workload_id.clone(),
1437                session_id: session_id.clone(),
1438                kind: kind.clone(),
1439                scope: session_info.scope,
1440                raw_identifier: raw_identifier.clone(),
1441                display_name: None,
1442                status: normalized_status.clone(),
1443                timestamps,
1444                exit_code: None,
1445                enforcement_error_code: None,
1446                blocking: None,
1447                evidence: vec![observation.context.evidence_ref.clone()],
1448            });
1449        }
1450        if let Some(session) = self
1451            .sessions
1452            .iter_mut()
1453            .find(|session| session.session_id == session_id)
1454        {
1455            if !session.workload_ids.contains(&workload_id) {
1456                session.workload_ids.push(workload_id);
1457            }
1458        }
1459        self.push_timeline(
1460            ordinal,
1461            &observation.context,
1462            EspTimelineKind::Workload,
1463            raw_identifier,
1464            Some(tracked_kind_name(&kind).to_string()),
1465            Some(normalized_status),
1466        );
1467    }
1468
1469    fn accumulate_v2_workload(&mut self, ordinal: usize, observation: &EspJsonObservation) {
1470        let components: Vec<&str> = observation
1471            .json_pointer
1472            .split('/')
1473            .filter(|part| !part.is_empty())
1474            .collect();
1475        if components.len() < 3 || !components[0].eq_ignore_ascii_case("Workloads") {
1476            return;
1477        }
1478        let Ok(index) = components[1].parse::<usize>() else {
1479            return;
1480        };
1481        let field = components[2].to_ascii_lowercase();
1482        if !matches!(
1483            field.as_str(),
1484            "workloadid"
1485                | "friendlyname"
1486                | "workloadstate"
1487                | "starttime"
1488                | "endtime"
1489                | "errorcode"
1490                | "enforcementerrorcode"
1491        ) {
1492            return;
1493        }
1494        let key = (
1495            observation.context.provenance.source_artifact_id.clone(),
1496            v2_document_identity(observation),
1497            index,
1498        );
1499        let entry = self
1500            .v2_workloads
1501            .entry(key)
1502            .or_insert_with(|| V2WorkloadAccumulator {
1503                workload_id: None,
1504                friendly_name: None,
1505                started: Vec::new(),
1506                ended: Vec::new(),
1507                exit_codes: Vec::new(),
1508                enforcement_error_codes: Vec::new(),
1509                evidence: Vec::new(),
1510                observations: Vec::new(),
1511                states: Vec::new(),
1512            });
1513        match field.as_str() {
1514            "workloadid" => entry.workload_id = observation_text(&observation.value),
1515            "friendlyname" => entry.friendly_name = observation_text(&observation.value),
1516            "workloadstate" => {
1517                entry.states.push(V2StateOccurrence {
1518                    ordinal,
1519                    context: observation.context.clone(),
1520                    raw_status: observation_raw_status(&observation.value),
1521                });
1522            }
1523            "starttime" => {
1524                if let Some(timestamp) = timestamp_from_observation_value(&observation.value) {
1525                    entry.started.push(V2TimestampOccurrence {
1526                        ordinal,
1527                        context: observation.context.clone(),
1528                        timestamp,
1529                    });
1530                }
1531            }
1532            "endtime" => {
1533                if let Some(timestamp) = timestamp_from_observation_value(&observation.value) {
1534                    entry.ended.push(V2TimestampOccurrence {
1535                        ordinal,
1536                        context: observation.context.clone(),
1537                        timestamp,
1538                    });
1539                }
1540            }
1541            "errorcode" => {
1542                if let Some(raw) = observation_text(&observation.value) {
1543                    entry.exit_codes.push(DeferredCodeOccurrence {
1544                        ordinal,
1545                        code: error_code(&raw),
1546                        is_enforcement: false,
1547                        context: observation.context.clone(),
1548                    });
1549                }
1550            }
1551            "enforcementerrorcode" => {
1552                if let Some(raw) = observation_text(&observation.value) {
1553                    entry.enforcement_error_codes.push(DeferredCodeOccurrence {
1554                        ordinal,
1555                        code: error_code(&raw),
1556                        is_enforcement: true,
1557                        context: observation.context.clone(),
1558                    });
1559                }
1560            }
1561            _ => unreachable!("field was validated above"),
1562        }
1563        entry.observations.push(observation.context.clone());
1564        entry
1565            .evidence
1566            .push(observation.context.evidence_ref.clone());
1567    }
1568
1569    fn accumulate_platform_script(&mut self, ordinal: usize, observation: &EspRegistryObservation) {
1570        let raw_identifier =
1571            last_path_component(&observation.key).unwrap_or_else(|| observation.value_name.clone());
1572        let key = (
1573            observation.context.provenance.source_artifact_id.clone(),
1574            raw_identifier.clone(),
1575        );
1576        let entry = self
1577            .platform_scripts
1578            .entry(key)
1579            .or_insert_with(|| PlatformScriptAccumulator {
1580                raw_identifier,
1581                results: Vec::new(),
1582                last_updated: Vec::new(),
1583                evidence: Vec::new(),
1584            });
1585        entry
1586            .evidence
1587            .push(observation.context.evidence_ref.clone());
1588        if observation.value_name.eq_ignore_ascii_case("Result") {
1589            entry.results.push((ordinal, observation.clone()));
1590        } else if observation
1591            .value_name
1592            .eq_ignore_ascii_case("LastUpdatedTimeUtc")
1593        {
1594            if let Some(timestamp) = timestamp_from_observation_value(&observation.value) {
1595                entry.last_updated.push(V2TimestampOccurrence {
1596                    ordinal,
1597                    context: observation.context.clone(),
1598                    timestamp,
1599                });
1600            }
1601        }
1602    }
1603
1604    fn accumulate_enforcement_message(&mut self, ordinal: usize, observation: &EspJsonObservation) {
1605        let field = observation
1606            .json_pointer
1607            .split('/')
1608            .rfind(|part| !part.is_empty())
1609            .unwrap_or_default()
1610            .to_ascii_lowercase();
1611        let key = enforcement_message_group_key(observation);
1612        let entry =
1613            self.enforcement_messages
1614                .entry(key)
1615                .or_insert_with(|| EnforcementMessageAccumulator {
1616                    identifier: None,
1617                    scope: None,
1618                    user_sid: None,
1619                    codes: Vec::new(),
1620                });
1621        match field.as_str() {
1622            "applicationid" | "appid" | "workloadid" | "policyid" | "id" => {
1623                entry.identifier = observation_text(&observation.value)
1624            }
1625            "usersid" | "sid" => {
1626                entry.user_sid = observation_text(&observation.value);
1627                entry.scope = Some(EspScope::User);
1628            }
1629            "scope" => {
1630                entry.scope = observation_text(&observation.value).and_then(|value| {
1631                    if value.eq_ignore_ascii_case("device") {
1632                        Some(EspScope::Device)
1633                    } else if value.eq_ignore_ascii_case("user") {
1634                        Some(EspScope::User)
1635                    } else {
1636                        None
1637                    }
1638                });
1639            }
1640            "errorcode" | "enforcementerrorcode" | "exitcode" => {
1641                if let Some(raw) = observation_text(&observation.value) {
1642                    entry.codes.push(DeferredCodeOccurrence {
1643                        ordinal,
1644                        code: error_code(&raw),
1645                        is_enforcement: !field.eq_ignore_ascii_case("exitcode"),
1646                        context: observation.context.clone(),
1647                    });
1648                }
1649            }
1650            _ => {}
1651        }
1652    }
1653
1654    fn push_code_timeline(
1655        &mut self,
1656        ordinal: usize,
1657        context: &EspObservationContext,
1658        title: String,
1659        code: &EspErrorCode,
1660        is_enforcement: bool,
1661    ) {
1662        let code_kind = if is_enforcement {
1663            "Enforcement error code"
1664        } else {
1665            "Exit code"
1666        };
1667        let detail = format!("{code_kind} {}", code.raw);
1668        self.push_timeline(
1669            ordinal,
1670            context,
1671            EspTimelineKind::Workload,
1672            title,
1673            Some(detail.clone()),
1674            Some(text_status(&detail, error_code_normalized_status(code))),
1675        );
1676    }
1677
1678    fn push_timeline(
1679        &mut self,
1680        ordinal: usize,
1681        context: &EspObservationContext,
1682        kind: EspTimelineKind,
1683        title: String,
1684        detail: Option<String>,
1685        status: Option<EspStatus>,
1686    ) {
1687        self.activity.push((
1688            ordinal,
1689            EspTimelineEntry {
1690                entry_id: self.timeline_entry_id(context, ordinal),
1691                timestamp: context_timestamp(context),
1692                kind,
1693                title,
1694                detail,
1695                status,
1696                evidence: vec![context.evidence_ref.clone()],
1697            },
1698        ));
1699    }
1700
1701    fn push_workload_timeline_at(
1702        &mut self,
1703        ordinal: usize,
1704        context: &EspObservationContext,
1705        timestamp: EspTimestamp,
1706        title: String,
1707        detail: Option<String>,
1708        status: Option<EspStatus>,
1709    ) {
1710        self.activity.push((
1711            ordinal,
1712            EspTimelineEntry {
1713                entry_id: self.timeline_entry_id(context, ordinal),
1714                timestamp,
1715                kind: EspTimelineKind::Workload,
1716                title,
1717                detail,
1718                status,
1719                evidence: vec![context.evidence_ref.clone()],
1720            },
1721        ));
1722    }
1723
1724    fn ensure_profile(&mut self) -> &mut EspProfileEvidence {
1725        self.profile.get_or_insert_with(empty_profile)
1726    }
1727
1728    fn occurrence_ordinal(&self, ordinal: usize) -> usize {
1729        self.occurrence_ordinals
1730            .get(&ordinal)
1731            .copied()
1732            .unwrap_or(ordinal)
1733    }
1734
1735    fn timeline_entry_id(&self, context: &EspObservationContext, ordinal: usize) -> String {
1736        stable_timeline_entry_id(context, self.occurrence_ordinal(ordinal))
1737    }
1738
1739    fn record_id(&self, prefix: &str, context: &EspObservationContext, ordinal: usize) -> String {
1740        stable_record_id(prefix, context, self.occurrence_ordinal(ordinal))
1741    }
1742
1743    fn ensure_device_preparation(&mut self) -> &mut EspDevicePreparationEvidence {
1744        let profile = self.ensure_profile();
1745        profile
1746            .device_preparation
1747            .get_or_insert_with(empty_device_preparation)
1748    }
1749
1750    fn ensure_hardware(&mut self) -> &mut EspHardwareEvidence {
1751        self.hardware.get_or_insert_with(empty_hardware)
1752    }
1753
1754    fn add_profile_evidence(&mut self, evidence: &EspEvidenceRef) {
1755        self.ensure_profile().evidence.push(evidence.clone());
1756    }
1757
1758    fn add_identity_and_profile_evidence(&mut self, evidence: &EspEvidenceRef) {
1759        self.identity.evidence.push(evidence.clone());
1760        self.add_profile_evidence(evidence);
1761    }
1762
1763    fn finish(mut self) -> EspDiagnosticsSnapshot {
1764        self.finalize_v2_workloads();
1765        self.finalize_platform_scripts();
1766        self.apply_office_details();
1767        self.apply_msi_details();
1768        self.finalize_enforcement_messages();
1769        self.apply_graph_names();
1770        self.finalize_sessions();
1771        self.apply_deferred_error_codes();
1772        self.apply_sidecar_app_states();
1773        self.finalize_delivery_optimization();
1774
1775        let installer_correlations = correlate_installer_processes(
1776            &self.workloads,
1777            &self.process_observations,
1778            &self.deployment_log_observations,
1779            &self.ime_observations,
1780        );
1781
1782        let phase = snapshot_phase(&self.scenario, &self.sessions);
1783        let node_cache = self
1784            .node_cache
1785            .into_iter()
1786            .map(|(index, entry)| EspNodeCacheEntry {
1787                index,
1788                node_uri: entry.node_uri.unwrap_or_default(),
1789                expected_value: entry.expected_value,
1790                sensitivity: entry.sensitivity,
1791                evidence: entry.evidence,
1792            })
1793            .collect();
1794        let mut snapshot = EspDiagnosticsSnapshot {
1795            schema_version: ESP_DIAGNOSTICS_SCHEMA_VERSION,
1796            scenario: self.scenario,
1797            phase,
1798            generated_at_utc: self.generated_at_utc,
1799            elevation: self.elevation,
1800            identity: self.identity,
1801            profile: self.profile,
1802            enrollments: self.enrollments,
1803            sessions: self.sessions,
1804            workloads: self.workloads,
1805            installer_correlations,
1806            node_cache,
1807            registration_events: self.registration_events,
1808            delivery_optimization: self.delivery_optimization,
1809            hardware: self.hardware,
1810            activity: sort_timeline_entries(self.activity),
1811            findings: Vec::new(),
1812            coverage: self.coverage,
1813            raw_evidence: self.raw_evidence,
1814            graph: None,
1815        };
1816        snapshot.findings = derive_findings(&snapshot);
1817        snapshot
1818    }
1819
1820    fn finalize_v2_workloads(&mut self) {
1821        let accumulators = std::mem::take(&mut self.v2_workloads);
1822        for ((source, _document, index), entry) in accumulators {
1823            let raw_identifier = entry
1824                .workload_id
1825                .clone()
1826                .unwrap_or_else(|| format!("unknown-{index}"));
1827            let latest_state = entry.states.iter().max_by(|left, right| {
1828                context_chronology_key(&left.context)
1829                    .cmp(&context_chronology_key(&right.context))
1830                    .then_with(|| left.ordinal.cmp(&right.ordinal))
1831            });
1832            let raw_status = latest_state
1833                .map(|state| state.raw_status.clone())
1834                .unwrap_or_else(|| EspRawStatus::Text("missing".to_string()));
1835            let normalized_status = normalize_v2_status(raw_status);
1836            let session_identity = "devicePreparationV2:device:ProvisioningProgress";
1837            let session_id = format!(
1838                "session|{}|{}|0",
1839                escape_component(&source),
1840                session_identity
1841            );
1842            let first_observed = entry
1843                .observations
1844                .iter()
1845                .min_by(|left, right| {
1846                    context_chronology_key(left).cmp(&context_chronology_key(right))
1847                })
1848                .map(context_timestamp)
1849                .unwrap_or_else(|| normalize_timestamp(&self.generated_at_utc, None));
1850            let started = entry
1851                .started
1852                .iter()
1853                .min_by(|left, right| {
1854                    timestamp_chronology_key(&left.timestamp)
1855                        .cmp(timestamp_chronology_key(&right.timestamp))
1856                        .then_with(|| left.ordinal.cmp(&right.ordinal))
1857                })
1858                .map(|occurrence| occurrence.timestamp.clone());
1859            let ended = entry
1860                .ended
1861                .iter()
1862                .max_by(|left, right| {
1863                    timestamp_chronology_key(&left.timestamp)
1864                        .cmp(timestamp_chronology_key(&right.timestamp))
1865                        .then_with(|| left.ordinal.cmp(&right.ordinal))
1866                })
1867                .map(|occurrence| occurrence.timestamp.clone());
1868            let session_started = started.clone().unwrap_or_else(|| first_observed.clone());
1869            if let Some(session) = self
1870                .sessions
1871                .iter_mut()
1872                .find(|session| session.session_id == session_id)
1873            {
1874                merge_earliest_timestamp(&mut session.started_at, Some(session_started));
1875                merge_latest_timestamp(&mut session.ended_at, ended.clone());
1876                session.evidence.extend(entry.evidence.clone());
1877            } else {
1878                self.sessions.push(EspSession {
1879                    session_id: session_id.clone(),
1880                    kind: EspSessionKind::DevicePreparationV2,
1881                    scope: EspScope::Device,
1882                    user_sid: None,
1883                    started_at: Some(session_started),
1884                    ended_at: ended.clone(),
1885                    phase: EspPhase::DevicePreparation,
1886                    is_latest: false,
1887                    workload_ids: Vec::new(),
1888                    evidence: entry.evidence.clone(),
1889                });
1890            }
1891            let identity = format!("devicePreparationV2:device:{index}:{raw_identifier}");
1892            let workload_id = format!(
1893                "workload|{}|{}|0",
1894                escape_component(&source),
1895                escape_component(&identity)
1896            );
1897            let timestamps = EspWorkloadTimestamps {
1898                first_observed,
1899                started,
1900                ended,
1901                last_updated: latest_state.map(|state| context_timestamp(&state.context)),
1902            };
1903            let title = entry
1904                .friendly_name
1905                .clone()
1906                .unwrap_or_else(|| raw_identifier.clone());
1907            let exit_code =
1908                latest_code_occurrence(&entry.exit_codes).map(|occurrence| occurrence.code.clone());
1909            let enforcement_error_code = latest_code_occurrence(&entry.enforcement_error_codes)
1910                .map(|occurrence| occurrence.code.clone());
1911            self.workloads.push(EspWorkload {
1912                workload_id: workload_id.clone(),
1913                session_id: session_id.clone(),
1914                kind: EspTrackedKind::DevicePreparationWorkload,
1915                scope: EspScope::Device,
1916                raw_identifier: raw_identifier.clone(),
1917                display_name: entry.friendly_name.clone(),
1918                status: normalized_status.clone(),
1919                timestamps,
1920                exit_code,
1921                enforcement_error_code,
1922                blocking: None,
1923                evidence: entry.evidence.clone(),
1924            });
1925            if let Some(session) = self
1926                .sessions
1927                .iter_mut()
1928                .find(|session| session.session_id == session_id)
1929            {
1930                session.workload_ids.push(workload_id);
1931            }
1932            for state in entry.states {
1933                self.push_timeline(
1934                    state.ordinal,
1935                    &state.context,
1936                    EspTimelineKind::Workload,
1937                    title.clone(),
1938                    Some("devicePreparationWorkload".to_string()),
1939                    Some(normalize_v2_status(state.raw_status)),
1940                );
1941            }
1942            for occurrence in entry.started {
1943                self.push_workload_timeline_at(
1944                    occurrence.ordinal,
1945                    &occurrence.context,
1946                    occurrence.timestamp,
1947                    title.clone(),
1948                    Some("Installation started".to_string()),
1949                    Some(text_status(
1950                        "Installation started",
1951                        EspNormalizedStatus::Installing,
1952                    )),
1953                );
1954            }
1955            for occurrence in entry.ended {
1956                self.push_workload_timeline_at(
1957                    occurrence.ordinal,
1958                    &occurrence.context,
1959                    occurrence.timestamp,
1960                    title.clone(),
1961                    Some("Installation ended".to_string()),
1962                    Some(normalized_status.clone()),
1963                );
1964            }
1965            for occurrence in entry
1966                .exit_codes
1967                .into_iter()
1968                .chain(entry.enforcement_error_codes)
1969            {
1970                self.push_code_timeline(
1971                    occurrence.ordinal,
1972                    &occurrence.context,
1973                    title.clone(),
1974                    &occurrence.code,
1975                    occurrence.is_enforcement,
1976                );
1977            }
1978        }
1979    }
1980
1981    fn finalize_platform_scripts(&mut self) {
1982        let scripts = std::mem::take(&mut self.platform_scripts);
1983        for ((source, _), script) in scripts {
1984            let Some((_, latest_result)) = script.results.iter().max_by(|left, right| {
1985                context_chronology_key(&left.1.context)
1986                    .cmp(&context_chronology_key(&right.1.context))
1987                    .then_with(|| left.0.cmp(&right.0))
1988            }) else {
1989                continue;
1990            };
1991            let status = text_status_from_observation(&latest_result.value);
1992            let first_observed = script
1993                .results
1994                .iter()
1995                .map(|(_, observation)| context_timestamp(&observation.context))
1996                .chain(
1997                    script
1998                        .last_updated
1999                        .iter()
2000                        .map(|occurrence| context_timestamp(&occurrence.context)),
2001                )
2002                .min_by(|left, right| {
2003                    timestamp_chronology_key(left).cmp(timestamp_chronology_key(right))
2004                })
2005                .unwrap_or_else(|| normalize_timestamp(&self.generated_at_utc, None));
2006            let mut timestamps = workload_timestamps(
2007                context_timestamp(&script.results[0].1.context),
2008                &text_status_from_observation(&script.results[0].1.value).normalized,
2009            );
2010            for (_, observation) in script.results.iter().skip(1) {
2011                let occurrence_status = text_status_from_observation(&observation.value);
2012                merge_workload_timestamps(
2013                    &mut timestamps,
2014                    workload_timestamps(
2015                        context_timestamp(&observation.context),
2016                        &occurrence_status.normalized,
2017                    ),
2018                );
2019            }
2020            timestamps.first_observed = first_observed.clone();
2021            for occurrence in &script.last_updated {
2022                merge_latest_timestamp(
2023                    &mut timestamps.last_updated,
2024                    Some(occurrence.timestamp.clone()),
2025                );
2026            }
2027
2028            let candidate_sessions = self
2029                .sessions
2030                .iter()
2031                .filter(|session| session.kind == EspSessionKind::DevicePreparationV2)
2032                .collect::<Vec<_>>();
2033            let session_id = candidate_sessions
2034                .iter()
2035                .copied()
2036                .find(|session| {
2037                    session
2038                        .evidence
2039                        .iter()
2040                        .any(|evidence| evidence.source_artifact_id == source)
2041                })
2042                .or_else(|| (candidate_sessions.len() == 1).then(|| candidate_sessions[0]))
2043                .map(|session| session.session_id.clone())
2044                .unwrap_or_else(|| {
2045                    format!(
2046                        "session|{}|devicePreparationV2:device:Policies|0",
2047                        escape_component(&source)
2048                    )
2049                });
2050            if !self
2051                .sessions
2052                .iter()
2053                .any(|session| session.session_id == session_id)
2054            {
2055                self.sessions.push(EspSession {
2056                    session_id: session_id.clone(),
2057                    kind: EspSessionKind::DevicePreparationV2,
2058                    scope: EspScope::Device,
2059                    user_sid: None,
2060                    started_at: Some(first_observed.clone()),
2061                    ended_at: None,
2062                    phase: EspPhase::DevicePreparation,
2063                    is_latest: false,
2064                    workload_ids: Vec::new(),
2065                    evidence: script.evidence.clone(),
2066                });
2067            } else if let Some(session) = self
2068                .sessions
2069                .iter_mut()
2070                .find(|session| session.session_id == session_id)
2071            {
2072                merge_earliest_timestamp(&mut session.started_at, Some(first_observed));
2073                session.evidence.extend(script.evidence.clone());
2074            }
2075            let workload_id = format!(
2076                "workload|{}|{}|0",
2077                escape_component(&source),
2078                escape_component(&format!("platformScript:{}", script.raw_identifier))
2079            );
2080            self.workloads.push(EspWorkload {
2081                workload_id: workload_id.clone(),
2082                session_id: session_id.clone(),
2083                kind: EspTrackedKind::PlatformScript,
2084                scope: EspScope::Device,
2085                raw_identifier: script.raw_identifier.clone(),
2086                display_name: None,
2087                status: status.clone(),
2088                timestamps,
2089                exit_code: None,
2090                enforcement_error_code: None,
2091                blocking: None,
2092                evidence: script.evidence.clone(),
2093            });
2094            if let Some(session) = self
2095                .sessions
2096                .iter_mut()
2097                .find(|session| session.session_id == session_id)
2098            {
2099                if !session.workload_ids.contains(&workload_id) {
2100                    session.workload_ids.push(workload_id);
2101                }
2102            }
2103            for (ordinal, observation) in script.results {
2104                self.push_timeline(
2105                    ordinal,
2106                    &observation.context,
2107                    EspTimelineKind::Workload,
2108                    script.raw_identifier.clone(),
2109                    Some("platformScript".to_string()),
2110                    Some(text_status_from_observation(&observation.value)),
2111                );
2112            }
2113        }
2114    }
2115
2116    fn finalize_enforcement_messages(&mut self) {
2117        let messages = std::mem::take(&mut self.enforcement_messages);
2118        for (_, message) in messages {
2119            for occurrence in message.codes {
2120                self.deferred_error_codes.push(DeferredErrorCode {
2121                    identifier: message.identifier.clone(),
2122                    explicit_session: None,
2123                    scope: message.scope.clone(),
2124                    user_sid: message.user_sid.clone(),
2125                    kind: Some(EspTrackedKind::DevicePreparationWorkload),
2126                    code: occurrence.code,
2127                    is_enforcement: occurrence.is_enforcement,
2128                    ordinal: occurrence.ordinal,
2129                    context: occurrence.context,
2130                });
2131            }
2132        }
2133    }
2134
2135    fn apply_deferred_error_codes(&mut self) {
2136        for deferred in std::mem::take(&mut self.deferred_error_codes) {
2137            let title = deferred
2138                .identifier
2139                .clone()
2140                .unwrap_or_else(|| "Uncorrelated workload".to_string());
2141            self.push_code_timeline(
2142                deferred.ordinal,
2143                &deferred.context,
2144                title,
2145                &deferred.code,
2146                deferred.is_enforcement,
2147            );
2148            let Some(identifier) = deferred.identifier.as_deref() else {
2149                continue;
2150            };
2151            let candidates = self
2152                .workloads
2153                .iter()
2154                .enumerate()
2155                .filter_map(|(index, workload)| {
2156                    if !identifiers_match(&workload.raw_identifier, identifier)
2157                        || deferred
2158                            .kind
2159                            .as_ref()
2160                            .map(|kind| kind != &workload.kind)
2161                            .unwrap_or(false)
2162                    {
2163                        return None;
2164                    }
2165                    let session = self
2166                        .sessions
2167                        .iter()
2168                        .find(|session| session.session_id == workload.session_id)?;
2169                    if let Some(explicit_session) = &deferred.explicit_session {
2170                        return deferred_session_matches(session, explicit_session)
2171                            .then_some(index);
2172                    }
2173                    if !session.is_latest {
2174                        return None;
2175                    }
2176                    if deferred
2177                        .scope
2178                        .as_ref()
2179                        .map(|scope| scope != &session.scope)
2180                        .unwrap_or(false)
2181                    {
2182                        return None;
2183                    }
2184                    if let Some(user_sid) = &deferred.user_sid {
2185                        if session.user_sid.as_ref().map(|sid| sid.value.as_str())
2186                            != Some(user_sid.as_str())
2187                        {
2188                            return None;
2189                        }
2190                    }
2191                    Some(index)
2192                })
2193                .collect::<Vec<_>>();
2194            if let [index] = candidates.as_slice() {
2195                let workload = &mut self.workloads[*index];
2196                if deferred.is_enforcement {
2197                    workload.enforcement_error_code = Some(deferred.code);
2198                } else {
2199                    workload.exit_code = Some(deferred.code);
2200                }
2201                workload
2202                    .evidence
2203                    .push(deferred.context.evidence_ref.clone());
2204            }
2205        }
2206    }
2207
2208    /// Overrides a Win32 app workload to `Failed` when the per-app Sidecar
2209    /// tracking node reports a terminal failure. The workload status is built
2210    /// from an OMA-DM diagnostics node (a mid-state number), which can lag the
2211    /// authoritative per-app `InstallationState`/`ErrorHresult` under
2212    /// `...\Setup\Apps\Tracking\Sidecar\<app>`. Without this, a device whose ESP
2213    /// has errored still shows the app as installing.
2214    fn apply_sidecar_app_states(&mut self) {
2215        struct GroupedSidecarState {
2216            identifier: String,
2217            installation_state: Option<u64>,
2218            error_hresult: Option<u32>,
2219            // Context of the observation that best evidences the failure: the
2220            // `ErrorHresult` reading when present (it carries the code we
2221            // surface), otherwise the `InstallationState` reading.
2222            error_evidence: Option<(usize, EspObservationContext)>,
2223            state_evidence: Option<(usize, EspObservationContext)>,
2224        }
2225
2226        let mut grouped: BTreeMap<String, GroupedSidecarState> = BTreeMap::new();
2227        for observation in std::mem::take(&mut self.sidecar_app_states) {
2228            let entry = grouped
2229                .entry(identifier_match_key(&observation.identifier))
2230                .or_insert_with(|| GroupedSidecarState {
2231                    identifier: observation.identifier.clone(),
2232                    installation_state: None,
2233                    error_hresult: None,
2234                    error_evidence: None,
2235                    state_evidence: None,
2236                });
2237            match observation.field {
2238                SidecarAppField::InstallationState => {
2239                    entry.installation_state = Some(observation.value);
2240                    entry.state_evidence = Some((observation.ordinal, observation.context));
2241                }
2242                SidecarAppField::ErrorHresult => {
2243                    entry.error_hresult = Some(observation.value as u32);
2244                    entry.error_evidence = Some((observation.ordinal, observation.context));
2245                }
2246            }
2247        }
2248
2249        for group in grouped.into_values() {
2250            // `InstallationState == 4` is FAILED; a present, non-zero
2251            // `ErrorHresult` is also a failure even if the state lags behind.
2252            let failed = group.installation_state == Some(4)
2253                || group.error_hresult.is_some_and(|code| code != 0);
2254            if !failed {
2255                continue;
2256            }
2257            let Some((_ordinal, context)) = group.error_evidence.or(group.state_evidence) else {
2258                continue;
2259            };
2260            let candidates = self
2261                .workloads
2262                .iter()
2263                .enumerate()
2264                .filter_map(|(index, workload)| {
2265                    if !identifiers_match(&workload.raw_identifier, &group.identifier) {
2266                        return None;
2267                    }
2268                    let session = self
2269                        .sessions
2270                        .iter()
2271                        .find(|session| session.session_id == workload.session_id)?;
2272                    session.is_latest.then_some(index)
2273                })
2274                .collect::<Vec<_>>();
2275            let [index] = candidates.as_slice() else {
2276                continue;
2277            };
2278            let index = *index;
2279            // Recency guard, mirroring `process_classic_workload`: only override
2280            // when the failure observation is at least as recent as the
2281            // workload's last update, so a genuinely-later success is preserved.
2282            let observed = context_timestamp(&context);
2283            let replace_status = self.workloads[index]
2284                .timestamps
2285                .last_updated
2286                .as_ref()
2287                .map(|current| {
2288                    timestamp_chronology_key(&observed) >= timestamp_chronology_key(current)
2289                })
2290                .unwrap_or(true);
2291            if !replace_status {
2292                continue;
2293            }
2294            let session_id = self.workloads[index].session_id.clone();
2295            let workload = &mut self.workloads[index];
2296            workload.status = EspStatus {
2297                raw: group
2298                    .installation_state
2299                    .map(|state| EspRawStatus::Number(state as i64))
2300                    .unwrap_or_else(|| EspRawStatus::Text("Failed".to_string())),
2301                normalized: EspNormalizedStatus::Failed,
2302                display: "Failed".to_string(),
2303                detail: None,
2304            };
2305            if let Some(code) = group.error_hresult.filter(|code| *code != 0) {
2306                workload.enforcement_error_code = Some(error_code(&code.to_string()));
2307            }
2308            workload.evidence.push(context.evidence_ref.clone());
2309            // finalize_sessions computed the session (and thus overall) phase
2310            // before this override ran, so mark the owning latest session failed
2311            // to match its own rule: any failed workload -> session failed ->
2312            // snapshot phase Failed.
2313            if let Some(session) = self
2314                .sessions
2315                .iter_mut()
2316                .find(|session| session.session_id == session_id)
2317            {
2318                session.phase = EspPhase::Failed;
2319            }
2320        }
2321    }
2322
2323    fn apply_office_details(&mut self) {
2324        let mut details_by_identifier: BTreeMap<String, Vec<OfficeDetailObservation>> =
2325            BTreeMap::new();
2326        for detail in std::mem::take(&mut self.office_details) {
2327            details_by_identifier
2328                .entry(identifier_match_key(&detail.identifier))
2329                .or_default()
2330                .push(detail);
2331        }
2332        // Index activity entries by title so the per-group status back-patch no
2333        // longer rescans the whole (growing) `activity` vector for every Office
2334        // identifier group — previously O(groups * activity_len). The map is
2335        // maintained incrementally as `push_timeline` appends entries, so a
2336        // later group still observes the entries pushed by earlier groups,
2337        // preserving the original full-scan semantics byte-for-byte.
2338        let mut activity_indices_by_title: BTreeMap<String, Vec<usize>> = BTreeMap::new();
2339        for (position, (_, activity)) in self.activity.iter().enumerate() {
2340            activity_indices_by_title
2341                .entry(activity.title.clone())
2342                .or_default()
2343                .push(position);
2344        }
2345        for details in details_by_identifier.into_values() {
2346            let Some(best) = details.into_iter().max_by(|left, right| {
2347                left.is_final
2348                    .cmp(&right.is_final)
2349                    .then_with(|| {
2350                        context_chronology_key(&left.context)
2351                            .cmp(&context_chronology_key(&right.context))
2352                    })
2353                    .then_with(|| left.ordinal.cmp(&right.ordinal))
2354            }) else {
2355                continue;
2356            };
2357            let matching = self
2358                .workloads
2359                .iter()
2360                .enumerate()
2361                .filter_map(|(index, workload)| {
2362                    if workload.kind != EspTrackedKind::Office
2363                        || !identifiers_match(&workload.raw_identifier, &best.identifier)
2364                    {
2365                        return None;
2366                    }
2367                    let session = self
2368                        .sessions
2369                        .iter()
2370                        .find(|session| session.session_id == workload.session_id)?;
2371                    Some((index, session_chronology(session).to_string()))
2372                })
2373                .collect::<Vec<_>>();
2374            let latest = matching.iter().map(|(_, key)| key).max().cloned();
2375            let candidates = matching
2376                .iter()
2377                .filter(|(_, key)| Some(key) == latest.as_ref())
2378                .map(|(index, _)| *index)
2379                .collect::<Vec<_>>();
2380            let [index] = candidates.as_slice() else {
2381                continue;
2382            };
2383            let identifier = self.workloads[*index].raw_identifier.clone();
2384            let status = normalize_office_status(
2385                self.workloads[*index].status.raw.clone(),
2386                Some(best.raw_status.clone()),
2387            );
2388            let workload_evidence = self.workloads[*index].evidence.clone();
2389            let observed = context_timestamp(&best.context);
2390            let timestamps = workload_timestamps(observed, &status.normalized);
2391            merge_workload_timestamps(&mut self.workloads[*index].timestamps, timestamps);
2392            self.workloads[*index]
2393                .evidence
2394                .push(best.context.evidence_ref.clone());
2395            self.workloads[*index].status = status.clone();
2396
2397            if let Some(activity_indices) = activity_indices_by_title.get(&identifier) {
2398                for &activity_index in activity_indices {
2399                    let (_, activity) = &mut self.activity[activity_index];
2400                    if activity
2401                        .evidence
2402                        .iter()
2403                        .any(|evidence| workload_evidence.contains(evidence))
2404                    {
2405                        activity.status = Some(status.clone());
2406                    }
2407                }
2408            }
2409            // `push_timeline` appends exactly one entry titled `identifier`;
2410            // register its index so subsequent groups can back-patch it just as
2411            // the previous whole-vector scan would have.
2412            let pushed_index = self.activity.len();
2413            self.push_timeline(
2414                best.ordinal,
2415                &best.context,
2416                EspTimelineKind::Workload,
2417                identifier.clone(),
2418                Some("Office detailed status".to_string()),
2419                Some(status),
2420            );
2421            activity_indices_by_title
2422                .entry(identifier)
2423                .or_default()
2424                .push(pushed_index);
2425        }
2426    }
2427
2428    fn apply_msi_details(&mut self) {
2429        for detail in std::mem::take(&mut self.msi_details) {
2430            let matching = self
2431                .workloads
2432                .iter()
2433                .enumerate()
2434                .filter_map(|(index, workload)| {
2435                    if workload.kind != EspTrackedKind::Msi
2436                        || !identifiers_match(&workload.raw_identifier, &detail.identifier)
2437                        || detail
2438                            .scope
2439                            .as_ref()
2440                            .map(|scope| scope != &workload.scope)
2441                            .unwrap_or(false)
2442                    {
2443                        return None;
2444                    }
2445                    let session = self
2446                        .sessions
2447                        .iter()
2448                        .find(|session| session.session_id == workload.session_id)?;
2449                    if let Some(user_sid) = &detail.user_sid {
2450                        if session.user_sid.as_ref().map(|sid| sid.value.as_str())
2451                            != Some(user_sid.as_str())
2452                        {
2453                            return None;
2454                        }
2455                    }
2456                    Some((index, session_chronology(session).to_string()))
2457                })
2458                .collect::<Vec<_>>();
2459            let latest = matching.iter().map(|(_, key)| key).max().cloned();
2460            let candidates = matching
2461                .iter()
2462                .filter(|(_, key)| Some(key) == latest.as_ref())
2463                .map(|(index, _)| *index)
2464                .collect::<Vec<_>>();
2465            let status = normalize_office_detail_status(detail.raw_status.clone());
2466            if let [index] = candidates.as_slice() {
2467                let workload = &mut self.workloads[*index];
2468                merge_workload_timestamps(
2469                    &mut workload.timestamps,
2470                    workload_timestamps(context_timestamp(&detail.context), &status.normalized),
2471                );
2472                workload.status = status.clone();
2473                workload.evidence.push(detail.context.evidence_ref.clone());
2474            }
2475            self.push_timeline(
2476                detail.ordinal,
2477                &detail.context,
2478                EspTimelineKind::Workload,
2479                detail.identifier,
2480                Some("MSI detailed status".to_string()),
2481                Some(status),
2482            );
2483        }
2484    }
2485
2486    fn apply_graph_names(&mut self) {
2487        for graph in &self.graph_observations {
2488            for workload in self.workloads.iter_mut().filter(|workload| {
2489                graph_section_matches_workload(&graph.section, &workload.kind)
2490                    && identifiers_match(&workload.raw_identifier, &graph.record_id)
2491            }) {
2492                if let Some(display_name) = &graph.display_name {
2493                    workload.display_name = Some(display_name.clone());
2494                }
2495                workload.evidence.push(graph.context.evidence_ref.clone());
2496            }
2497        }
2498    }
2499
2500    fn finalize_sessions(&mut self) {
2501        for session in &mut self.sessions {
2502            let session_workloads = session
2503                .workload_ids
2504                .iter()
2505                .filter_map(|id| {
2506                    self.workloads
2507                        .iter()
2508                        .find(|workload| &workload.workload_id == id)
2509                })
2510                .collect::<Vec<_>>();
2511            let statuses = session_workloads
2512                .iter()
2513                .map(|workload| &workload.status.normalized)
2514                .collect::<Vec<_>>();
2515            if statuses
2516                .iter()
2517                .any(|status| matches!(status, EspNormalizedStatus::Failed))
2518            {
2519                session.phase = EspPhase::Failed;
2520            } else if !statuses.is_empty()
2521                && statuses.iter().all(|status| {
2522                    matches!(
2523                        status,
2524                        EspNormalizedStatus::Succeeded | EspNormalizedStatus::Processed
2525                    )
2526                })
2527            {
2528                session.phase = EspPhase::Completed;
2529            }
2530            if matches!(session.phase, EspPhase::Completed | EspPhase::Failed) {
2531                let latest_end = session_workloads
2532                    .iter()
2533                    .filter_map(|workload| workload.timestamps.ended.as_ref())
2534                    .max_by(|left, right| {
2535                        timestamp_chronology_key(left).cmp(timestamp_chronology_key(right))
2536                    })
2537                    .cloned();
2538                merge_latest_timestamp(&mut session.ended_at, latest_end);
2539            }
2540        }
2541
2542        self.sessions.sort_by(|left, right| {
2543            session_group_sort_key(left)
2544                .cmp(&session_group_sort_key(right))
2545                .then_with(|| session_chronology(left).cmp(session_chronology(right)))
2546                .then_with(|| left.session_id.cmp(&right.session_id))
2547        });
2548        let mut latest_by_group: BTreeMap<String, usize> = BTreeMap::new();
2549        for (index, session) in self.sessions.iter().enumerate() {
2550            latest_by_group.insert(session_group_identity(session), index);
2551        }
2552        for index in latest_by_group.into_values() {
2553            self.sessions[index].is_latest = true;
2554        }
2555    }
2556
2557    fn finalize_delivery_optimization(&mut self) {
2558        if let Some(delivery) = &mut self.delivery_optimization {
2559            if delivery.download_http_bytes == 0 {
2560                delivery.peer_share_percent = None;
2561                delivery.connected_cache_share_percent = None;
2562            } else {
2563                let download_http_bytes = delivery.download_http_bytes as f64;
2564                delivery.peer_share_percent =
2565                    Some(delivery.download_lan_bytes as f64 / download_http_bytes * 100.0);
2566                delivery.connected_cache_share_percent =
2567                    Some(delivery.download_cache_host_bytes as f64 / download_http_bytes * 100.0);
2568            }
2569        }
2570    }
2571}
2572
2573#[derive(Debug)]
2574struct NodeCacheAccumulator {
2575    node_uri: Option<String>,
2576    expected_value: Option<String>,
2577    sensitivity: EspSensitivity,
2578    evidence: Vec<EspEvidenceRef>,
2579}
2580
2581#[derive(Debug)]
2582struct V2WorkloadAccumulator {
2583    workload_id: Option<String>,
2584    friendly_name: Option<String>,
2585    started: Vec<V2TimestampOccurrence>,
2586    ended: Vec<V2TimestampOccurrence>,
2587    exit_codes: Vec<DeferredCodeOccurrence>,
2588    enforcement_error_codes: Vec<DeferredCodeOccurrence>,
2589    evidence: Vec<EspEvidenceRef>,
2590    observations: Vec<EspObservationContext>,
2591    states: Vec<V2StateOccurrence>,
2592}
2593
2594#[derive(Debug)]
2595struct V2StateOccurrence {
2596    ordinal: usize,
2597    context: EspObservationContext,
2598    raw_status: EspRawStatus,
2599}
2600
2601#[derive(Debug, Clone)]
2602struct V2TimestampOccurrence {
2603    ordinal: usize,
2604    context: EspObservationContext,
2605    timestamp: EspTimestamp,
2606}
2607
2608#[derive(Debug)]
2609struct PlatformScriptAccumulator {
2610    raw_identifier: String,
2611    results: Vec<(usize, EspRegistryObservation)>,
2612    last_updated: Vec<V2TimestampOccurrence>,
2613    evidence: Vec<EspEvidenceRef>,
2614}
2615
2616#[derive(Debug, Clone)]
2617struct OfficeDetailObservation {
2618    ordinal: usize,
2619    identifier: String,
2620    raw_status: EspRawStatus,
2621    is_final: bool,
2622    context: EspObservationContext,
2623}
2624
2625#[derive(Debug, Clone)]
2626struct MsiDetailObservation {
2627    ordinal: usize,
2628    identifier: String,
2629    scope: Option<EspScope>,
2630    user_sid: Option<String>,
2631    raw_status: EspRawStatus,
2632    context: EspObservationContext,
2633}
2634
2635#[derive(Debug)]
2636struct EnforcementMessageAccumulator {
2637    identifier: Option<String>,
2638    scope: Option<EspScope>,
2639    user_sid: Option<String>,
2640    codes: Vec<DeferredCodeOccurrence>,
2641}
2642
2643#[derive(Debug)]
2644struct DeferredCodeOccurrence {
2645    ordinal: usize,
2646    code: EspErrorCode,
2647    is_enforcement: bool,
2648    context: EspObservationContext,
2649}
2650
2651#[derive(Debug)]
2652struct DeferredSessionIdentity {
2653    scope: EspScope,
2654    user_sid: Option<String>,
2655    raw_time: String,
2656}
2657
2658#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2659enum SidecarAppField {
2660    InstallationState,
2661    ErrorHresult,
2662}
2663
2664/// A single per-app Sidecar tracking reading (`InstallationState` or
2665/// `ErrorHresult`) buffered from a `...\Setup\Apps\Tracking\Sidecar\<app>`
2666/// registry node, to be applied against the matched workload after workloads
2667/// are built.
2668#[derive(Debug)]
2669struct SidecarAppStateObservation {
2670    identifier: String,
2671    field: SidecarAppField,
2672    value: u64,
2673    ordinal: usize,
2674    context: EspObservationContext,
2675}
2676
2677#[derive(Debug)]
2678struct DeferredErrorCode {
2679    identifier: Option<String>,
2680    explicit_session: Option<DeferredSessionIdentity>,
2681    scope: Option<EspScope>,
2682    user_sid: Option<String>,
2683    kind: Option<EspTrackedKind>,
2684    code: EspErrorCode,
2685    is_enforcement: bool,
2686    ordinal: usize,
2687    context: EspObservationContext,
2688}
2689
2690enum EnrollmentUpdate {
2691    ProviderId(String),
2692    TenantId(String),
2693    UserPrincipalName(String),
2694    EntdmId(String),
2695    DeviceEspEnabled(bool),
2696    UserEspEnabled(bool),
2697    TimeoutSeconds(u64),
2698    Blocking(u64),
2699}
2700
2701struct ClassicSessionInfo {
2702    scope: EspScope,
2703    user_sid: Option<String>,
2704    family: String,
2705    raw_time: String,
2706}
2707
2708fn classify_scenario<'a>(records: impl IntoIterator<Item = &'a EspEvidenceRecord>) -> EspScenario {
2709    let mut has_v2 = false;
2710    let mut has_profile_name = false;
2711    let mut has_existing_device_identity = false;
2712    let mut has_esp = false;
2713    for record in records {
2714        if !record_is_usable(record) {
2715            continue;
2716        }
2717        match record {
2718            EspEvidenceRecord::Registry(observation) => {
2719                let name = observation.value_name.to_ascii_lowercase();
2720                if name == "autopilotdeviceprephint" {
2721                    has_v2 = true;
2722                } else if name == "deploymentprofilename"
2723                    && observation_text(&observation.value)
2724                        .map(|value| !value.is_empty())
2725                        .unwrap_or(false)
2726                {
2727                    has_profile_name = true;
2728                } else if matches!(name.as_str(), "ztdcorrelationid" | "cloudassignedtenantid") {
2729                    has_existing_device_identity = true;
2730                }
2731                if observation.key.to_ascii_lowercase().contains("firstsync")
2732                    || classic_session_info(observation).is_some()
2733                {
2734                    has_esp = true;
2735                }
2736            }
2737            EspEvidenceRecord::Json(observation) => {
2738                has_v2 |= is_provisioning_progress(observation) || is_page_settings(observation);
2739                let has_value = observation_text(&observation.value)
2740                    .map(|value| !value.trim().is_empty())
2741                    .unwrap_or(false);
2742                if observation
2743                    .json_pointer
2744                    .eq_ignore_ascii_case("/DeploymentProfileName")
2745                    && has_value
2746                {
2747                    has_profile_name = true;
2748                }
2749                if observation
2750                    .json_pointer
2751                    .eq_ignore_ascii_case("/ZtdCorrelationId")
2752                    && has_value
2753                {
2754                    has_existing_device_identity = true;
2755                }
2756            }
2757            _ => {}
2758        }
2759    }
2760    if has_v2 {
2761        EspScenario::AutopilotDevicePreparationV2
2762    } else if has_profile_name {
2763        EspScenario::AutopilotV1
2764    } else if has_existing_device_identity {
2765        EspScenario::ExistingDeviceJson
2766    } else if has_esp {
2767        EspScenario::EspOnly
2768    } else {
2769        EspScenario::Unknown
2770    }
2771}
2772
2773fn record_allowed_for_scenario(record: &EspEvidenceRecord, scenario: &EspScenario) -> bool {
2774    if !record_is_usable(record) {
2775        return true;
2776    }
2777    if *scenario == EspScenario::AutopilotDevicePreparationV2 {
2778        !matches!(record, EspEvidenceRecord::Registry(observation) if classic_session_info(observation).is_some())
2779    } else {
2780        !matches!(record, EspEvidenceRecord::Json(observation) if is_provisioning_progress(observation) || is_page_settings(observation))
2781            && !matches!(record, EspEvidenceRecord::Registry(observation) if is_platform_script_observation(observation))
2782    }
2783}
2784
2785fn record_is_usable(record: &EspEvidenceRecord) -> bool {
2786    let Some(context) = record_context(record) else {
2787        return true;
2788    };
2789    context.access_state == EspSourceAccessState::Available
2790        && matches!(
2791            context.parse_state,
2792            EspParseState::Parsed | EspParseState::Raw
2793        )
2794}
2795
2796fn record_context(record: &EspEvidenceRecord) -> Option<&EspObservationContext> {
2797    match record {
2798        EspEvidenceRecord::Registry(value) => Some(&value.context),
2799        EspEvidenceRecord::Json(value) => Some(&value.context),
2800        EspEvidenceRecord::EventLog(value) => Some(&value.context),
2801        EspEvidenceRecord::Ime(value) => Some(&value.context),
2802        EspEvidenceRecord::DeploymentLog(value) => Some(&value.context),
2803        EspEvidenceRecord::Process(value) => Some(&value.context),
2804        EspEvidenceRecord::System(value) => Some(&value.context),
2805        EspEvidenceRecord::DeliveryOptimizationSummary(_) => None,
2806        EspEvidenceRecord::DeliveryOptimization(value) => Some(&value.context),
2807        EspEvidenceRecord::Graph(value) => Some(&value.context),
2808        EspEvidenceRecord::Coverage(_) => None,
2809    }
2810}
2811
2812fn record_occurrence_key(record: &EspEvidenceRecord) -> Option<(String, String)> {
2813    if let Some(context) = record_context(record) {
2814        return Some((
2815            context.provenance.source_artifact_id.clone(),
2816            context.evidence_ref.evidence_id.clone(),
2817        ));
2818    }
2819    let EspEvidenceRecord::Coverage(coverage) = record else {
2820        return None;
2821    };
2822    let evidence = coverage.evidence.first();
2823    Some((
2824        evidence
2825            .map(|value| value.source_artifact_id.clone())
2826            .unwrap_or_else(|| coverage.artifact_id.clone()),
2827        evidence
2828            .map(|value| value.evidence_id.clone())
2829            .unwrap_or_else(|| coverage.artifact_id.clone()),
2830    ))
2831}
2832
2833fn record_identity_source(record: &EspEvidenceRecord) -> String {
2834    if let Some(context) = record_context(record) {
2835        return context.provenance.source_artifact_id.clone();
2836    }
2837    match record {
2838        EspEvidenceRecord::Coverage(coverage) => coverage
2839            .evidence
2840            .first()
2841            .map(|evidence| evidence.source_artifact_id.clone())
2842            .unwrap_or_else(|| coverage.artifact_id.clone()),
2843        EspEvidenceRecord::DeliveryOptimizationSummary(_) => {
2844            "delivery-optimization.summary".to_string()
2845        }
2846        _ => "source-neutral".to_string(),
2847    }
2848}
2849
2850fn record_identity_key(
2851    record: &EspEvidenceRecord,
2852    occurrence_ordinal: usize,
2853) -> EspEvidenceIdentityKey {
2854    let evidence_id = if let Some(context) = record_context(record) {
2855        context.evidence_ref.evidence_id.clone()
2856    } else {
2857        match record {
2858            EspEvidenceRecord::Coverage(coverage) => coverage
2859                .evidence
2860                .first()
2861                .map(|evidence| evidence.evidence_id.clone())
2862                .unwrap_or_else(|| coverage.artifact_id.clone()),
2863            EspEvidenceRecord::DeliveryOptimizationSummary(_) => {
2864                "delivery-optimization.summary".to_string()
2865            }
2866            _ => "source-neutral".to_string(),
2867        }
2868    };
2869    EspEvidenceIdentityKey {
2870        source_artifact_id: record_identity_source(record),
2871        evidence_id,
2872        occurrence_ordinal,
2873    }
2874}
2875
2876fn raw_evidence_record(record: &EspEvidenceRecord, ordinal: usize) -> Option<EspRawEvidenceRecord> {
2877    let context = record_context(record)?;
2878    Some(EspRawEvidenceRecord {
2879        record_id: stable_record_id("raw", context, ordinal),
2880        provenance: context.provenance.clone(),
2881        source_timestamp: context.source_timestamp.clone(),
2882        observed_at_utc: context.observed_at_utc.clone(),
2883        raw_value: raw_observation_value(record),
2884        sensitivity: context.sensitivity.clone(),
2885        parse_state: context.parse_state.clone(),
2886        access_state: context.access_state.clone(),
2887        evidence: vec![context.evidence_ref.clone()],
2888    })
2889}
2890
2891fn raw_observation_value(record: &EspEvidenceRecord) -> EspObservationValue {
2892    match record {
2893        EspEvidenceRecord::Registry(value) => value.value.clone(),
2894        EspEvidenceRecord::Json(value) => value.value.clone(),
2895        EspEvidenceRecord::EventLog(value) => value
2896            .message
2897            .clone()
2898            .map(EspObservationValue::Text)
2899            .unwrap_or_else(|| {
2900                EspObservationValue::StringList(
2901                    value
2902                        .named_data
2903                        .iter()
2904                        .map(|item| format!("{}={}", item.name, item.value))
2905                        .collect(),
2906                )
2907            }),
2908        EspEvidenceRecord::Ime(value) => EspObservationValue::Text(value.message.clone()),
2909        EspEvidenceRecord::DeploymentLog(value) => EspObservationValue::Text(value.message.clone()),
2910        EspEvidenceRecord::Process(value) => EspObservationValue::StringList(vec![
2911            value.pid.to_string(),
2912            value.executable_name.clone(),
2913            value.sanitized_command_line.clone().unwrap_or_default(),
2914        ]),
2915        EspEvidenceRecord::System(value) => system_fact_value(&value.fact),
2916        EspEvidenceRecord::DeliveryOptimizationSummary(value) => {
2917            EspObservationValue::StringList(vec![
2918                format!("httpBytes={}", value.download_http_bytes),
2919                format!("lanBytes={}", value.download_lan_bytes),
2920                format!("cacheHostBytes={}", value.download_cache_host_bytes),
2921            ])
2922        }
2923        EspEvidenceRecord::DeliveryOptimization(value) => EspObservationValue::StringList(vec![
2924            format!("kind={:?}", value.kind),
2925            format!("contentId={}", value.content_id.clone().unwrap_or_default()),
2926            format!("appId={}", value.app_id.clone().unwrap_or_default()),
2927            format!("httpBytes={}", value.http_bytes.unwrap_or_default()),
2928            format!("lanBytes={}", value.lan_bytes.unwrap_or_default()),
2929            format!(
2930                "cacheHostBytes={}",
2931                value.cache_host_bytes.unwrap_or_default()
2932            ),
2933        ]),
2934        EspEvidenceRecord::Graph(value) => {
2935            let mut fields = vec![
2936                format!("section={}", serialized_wire_value(&value.section)),
2937                format!("apiVersion={}", serialized_wire_value(&value.api_version)),
2938                format!("recordId={}", value.record_id),
2939                format!(
2940                    "displayName={}",
2941                    value.display_name.clone().unwrap_or_default()
2942                ),
2943            ];
2944            if let Some(status) = &value.status {
2945                fields.extend([
2946                    format!("remoteStatus.raw={}", serialized_wire_value(&status.raw)),
2947                    format!(
2948                        "remoteStatus.normalized={}",
2949                        serialized_wire_value(&status.normalized)
2950                    ),
2951                    format!("remoteStatus.display={}", status.display),
2952                ]);
2953            }
2954            EspObservationValue::StringList(fields)
2955        }
2956        EspEvidenceRecord::Coverage(_) => EspObservationValue::Text(String::new()),
2957    }
2958}
2959
2960fn is_raw_hardware_hash_record(record: &EspEvidenceRecord) -> bool {
2961    match record {
2962        EspEvidenceRecord::Registry(value) => {
2963            contains_hardware_hash(&value.key)
2964                || contains_hardware_hash(&value.value_name)
2965                || observation_value_contains_hardware_hash(&value.value)
2966        }
2967        EspEvidenceRecord::Json(value) => {
2968            contains_hardware_hash(&value.document_type)
2969                || contains_hardware_hash(&value.json_pointer)
2970        }
2971        _ => false,
2972    }
2973}
2974
2975fn observation_value_contains_hardware_hash(value: &EspObservationValue) -> bool {
2976    match value {
2977        EspObservationValue::Text(value) => contains_hardware_hash(value),
2978        EspObservationValue::StringList(values) => {
2979            values.iter().any(|value| contains_hardware_hash(value))
2980        }
2981        EspObservationValue::Integer(_)
2982        | EspObservationValue::Unsigned(_)
2983        | EspObservationValue::Boolean(_) => false,
2984    }
2985}
2986
2987fn contains_hardware_hash(value: &str) -> bool {
2988    let value = value.to_ascii_lowercase().replace([' ', '-', '_'], "");
2989    value.contains("hardwarehash") || value.contains("devicehardwaredata")
2990}
2991
2992fn serialized_wire_value<T: Serialize>(value: &T) -> String {
2993    match serde_json::to_value(value) {
2994        Ok(serde_json::Value::String(value)) => value,
2995        Ok(value) => value.to_string(),
2996        Err(_) => String::new(),
2997    }
2998}
2999
3000fn graph_section_matches_workload(
3001    section: &EspGraphObservationSection,
3002    kind: &EspTrackedKind,
3003) -> bool {
3004    match section {
3005        EspGraphObservationSection::App => matches!(
3006            kind,
3007            EspTrackedKind::ModernApp
3008                | EspTrackedKind::Win32App
3009                | EspTrackedKind::DevicePreparationWorkload
3010        ),
3011        EspGraphObservationSection::Policy => {
3012            matches!(
3013                kind,
3014                EspTrackedKind::Policy | EspTrackedKind::ScepCertificate
3015            )
3016        }
3017        EspGraphObservationSection::Script => matches!(kind, EspTrackedKind::PlatformScript),
3018        EspGraphObservationSection::ManagedDevice
3019        | EspGraphObservationSection::AutopilotIdentity
3020        | EspGraphObservationSection::DeploymentProfile
3021        | EspGraphObservationSection::EnrollmentConfiguration
3022        | EspGraphObservationSection::Unknown(_) => false,
3023    }
3024}
3025
3026fn classic_session_info(observation: &EspRegistryObservation) -> Option<ClassicSessionInfo> {
3027    let components = path_components(&observation.key);
3028    let diagnostics = components
3029        .iter()
3030        .position(|part| part.eq_ignore_ascii_case("Diagnostics"))?;
3031    let mut cursor = diagnostics + 1;
3032    let mut user_sid = None;
3033    let scope = if components
3034        .get(cursor)
3035        .map(|part| part.to_ascii_uppercase().starts_with("S-"))
3036        .unwrap_or(false)
3037    {
3038        user_sid = components.get(cursor).cloned();
3039        cursor += 1;
3040        EspScope::User
3041    } else {
3042        EspScope::Device
3043    };
3044    let family = components.get(cursor)?.clone();
3045    if !is_classic_family(&family) {
3046        return None;
3047    }
3048    let raw_time = components.get(cursor + 1)?.clone();
3049    Some(ClassicSessionInfo {
3050        scope,
3051        user_sid,
3052        family,
3053        raw_time,
3054    })
3055}
3056
3057fn classic_session_identity(info: &ClassicSessionInfo) -> String {
3058    match (&info.scope, &info.user_sid) {
3059        (EspScope::Device, _) => format!("classic:device:{}", info.raw_time),
3060        (EspScope::User, Some(sid)) => format!("classic:user:{sid}:{}", info.raw_time),
3061        (EspScope::User, None) => format!("classic:user:unknown:{}", info.raw_time),
3062    }
3063}
3064
3065fn classic_session_id(source_artifact_id: &str, info: &ClassicSessionInfo) -> String {
3066    format!(
3067        "session|{}|{}|0",
3068        escape_component(source_artifact_id),
3069        escape_component(&classic_session_identity(info))
3070    )
3071}
3072
3073fn deferred_error_code_observation(
3074    ordinal: usize,
3075    observation: &EspRegistryObservation,
3076) -> Option<DeferredErrorCode> {
3077    let is_enforcement = if observation
3078        .value_name
3079        .eq_ignore_ascii_case("EnforcementErrorCode")
3080    {
3081        true
3082    } else if observation.value_name.eq_ignore_ascii_case("ExitCode") {
3083        false
3084    } else {
3085        return None;
3086    };
3087    let raw = observation_text(&observation.value)?;
3088    let identifier = last_path_component(&observation.key)?;
3089    let explicit_session = classic_session_info(observation);
3090    let (scope, user_sid) = if let Some(info) = &explicit_session {
3091        (Some(info.scope.clone()), info.user_sid.clone())
3092    } else if let Some(sid) = path_components(&observation.key)
3093        .into_iter()
3094        .find(|part| part.to_ascii_uppercase().starts_with("S-"))
3095    {
3096        if is_device_scope_sid(&sid) {
3097            (Some(EspScope::Device), None)
3098        } else {
3099            (Some(EspScope::User), Some(sid))
3100        }
3101    } else {
3102        (None, None)
3103    };
3104    let key = observation.key.to_ascii_lowercase();
3105    let kind = if key.contains("win32apps")
3106        || explicit_session
3107            .as_ref()
3108            .map(|info| info.family.eq_ignore_ascii_case("Sidecar"))
3109            .unwrap_or(false)
3110    {
3111        Some(EspTrackedKind::Win32App)
3112    } else {
3113        None
3114    };
3115    Some(DeferredErrorCode {
3116        identifier: Some(identifier),
3117        explicit_session: explicit_session.map(|info| DeferredSessionIdentity {
3118            scope: info.scope,
3119            user_sid: info.user_sid,
3120            raw_time: info.raw_time,
3121        }),
3122        scope,
3123        user_sid,
3124        kind,
3125        code: error_code(&raw),
3126        is_enforcement,
3127        ordinal,
3128        context: observation.context.clone(),
3129    })
3130}
3131
3132/// Recognizes a per-app Sidecar tracking reading under an app-tracking node
3133/// (`...\Setup\Apps\Tracking\Sidecar\<app>`). These carry the authoritative
3134/// terminal `InstallationState` (4 = FAILED) and `ErrorHresult`, which the
3135/// OMA-DM diagnostics status node does not reflect.
3136fn sidecar_app_state_observation(
3137    ordinal: usize,
3138    observation: &EspRegistryObservation,
3139) -> Option<SidecarAppStateObservation> {
3140    let field = if observation.value_name.eq_ignore_ascii_case("InstallationState") {
3141        SidecarAppField::InstallationState
3142    } else if observation.value_name.eq_ignore_ascii_case("ErrorHresult") {
3143        SidecarAppField::ErrorHresult
3144    } else {
3145        return None;
3146    };
3147    let components = path_components(&observation.key);
3148    let is_app_tracking_node = components
3149        .iter()
3150        .any(|part| part.eq_ignore_ascii_case("Apps"))
3151        && components
3152            .iter()
3153            .any(|part| part.eq_ignore_ascii_case("Tracking"));
3154    if !is_app_tracking_node {
3155        return None;
3156    }
3157    let identifier = last_path_component(&observation.key)?;
3158    let value = observation_u64(&observation.value)?;
3159    Some(SidecarAppStateObservation {
3160        identifier,
3161        field,
3162        value,
3163        ordinal,
3164        context: observation.context.clone(),
3165    })
3166}
3167
3168fn is_device_scope_sid(value: &str) -> bool {
3169    value.to_ascii_uppercase().starts_with("S-0-")
3170}
3171
3172fn enrollment_update(observation: &EspRegistryObservation) -> Option<EnrollmentUpdate> {
3173    match observation.value_name.to_ascii_lowercase().as_str() {
3174        "providerid" => observation_text(&observation.value).map(EnrollmentUpdate::ProviderId),
3175        "aadtenantid" | "tenantid" => {
3176            observation_text(&observation.value).map(EnrollmentUpdate::TenantId)
3177        }
3178        "upn" | "userprincipalname" => {
3179            observation_text(&observation.value).map(EnrollmentUpdate::UserPrincipalName)
3180        }
3181        "entdmid" => observation_text(&observation.value).map(EnrollmentUpdate::EntdmId),
3182        "skipdevicestatuspage" => observation_i64(&observation.value)
3183            .map(|value| EnrollmentUpdate::DeviceEspEnabled(value == 0)),
3184        "skipuserstatuspage" => observation_i64(&observation.value)
3185            .map(|value| EnrollmentUpdate::UserEspEnabled(value == 0)),
3186        "syncfailuretimeout" => {
3187            observation_u64(&observation.value).map(EnrollmentUpdate::TimeoutSeconds)
3188        }
3189        "blockinstatuspage" => observation_u64(&observation.value).map(EnrollmentUpdate::Blocking),
3190        _ => None,
3191    }
3192}
3193
3194fn office_detail_observation(
3195    ordinal: usize,
3196    observation: &EspRegistryObservation,
3197) -> Option<OfficeDetailObservation> {
3198    let components = path_components(&observation.key);
3199    let office = components
3200        .iter()
3201        .position(|part| part.eq_ignore_ascii_case("OfficeCSP"))?;
3202    let identifier = components.get(office + 1)?;
3203    let is_final = if observation.value_name.eq_ignore_ascii_case("FinalStatus") {
3204        true
3205    } else if observation.value_name.eq_ignore_ascii_case("Status") {
3206        false
3207    } else {
3208        return None;
3209    };
3210    Some(OfficeDetailObservation {
3211        ordinal,
3212        identifier: percent_decode_bounded(identifier).unwrap_or_else(|_| identifier.clone()),
3213        raw_status: observation_raw_status(&observation.value),
3214        is_final,
3215        context: observation.context.clone(),
3216    })
3217}
3218
3219fn msi_detail_observation(
3220    ordinal: usize,
3221    observation: &EspRegistryObservation,
3222) -> Option<MsiDetailObservation> {
3223    if !observation.value_name.eq_ignore_ascii_case("Status") {
3224        return None;
3225    }
3226    let components = path_components(&observation.key);
3227    let root = components
3228        .iter()
3229        .position(|part| part.eq_ignore_ascii_case("EnterpriseDesktopAppManagement"))?;
3230    let msi = components
3231        .iter()
3232        .enumerate()
3233        .skip(root + 1)
3234        .find(|(_, part)| part.eq_ignore_ascii_case("MSI"))?
3235        .0;
3236    let identifier = components.get(msi + 1)?.clone();
3237    let sid = components
3238        .get(msi.saturating_sub(1))
3239        .filter(|part| part.to_ascii_uppercase().starts_with("S-"))
3240        .cloned();
3241    let (scope, user_sid) = match sid {
3242        Some(sid) if is_device_scope_sid(&sid) => (Some(EspScope::Device), None),
3243        Some(sid) => (Some(EspScope::User), Some(sid)),
3244        None => (None, None),
3245    };
3246    Some(MsiDetailObservation {
3247        ordinal,
3248        identifier: percent_decode_bounded(&identifier).unwrap_or(identifier),
3249        scope,
3250        user_sid,
3251        raw_status: observation_raw_status(&observation.value),
3252        context: observation.context.clone(),
3253    })
3254}
3255
3256fn is_classic_family(value: &str) -> bool {
3257    matches!(
3258        value.to_ascii_lowercase().as_str(),
3259        "expectedpolicies"
3260            | "expectedmsiapppackages"
3261            | "expectedmodernapppackages"
3262            | "sidecar"
3263            | "expectedscepcerts"
3264    )
3265}
3266
3267fn classic_workload_kind(family: &str, value_name: &str) -> Option<EspTrackedKind> {
3268    let decoded = percent_decode_bounded(value_name).unwrap_or_else(|_| value_name.to_string());
3269    let decoded = decoded.to_ascii_lowercase();
3270    match family.to_ascii_lowercase().as_str() {
3271        "expectedpolicies" => Some(EspTrackedKind::Policy),
3272        "expectedmsiapppackages" if decoded.contains("/office/installation/") => {
3273            Some(EspTrackedKind::Office)
3274        }
3275        "expectedmsiapppackages" => Some(EspTrackedKind::Msi),
3276        "expectedmodernapppackages" => Some(EspTrackedKind::ModernApp),
3277        "sidecar" => Some(EspTrackedKind::Win32App),
3278        "expectedscepcerts" => Some(EspTrackedKind::ScepCertificate),
3279        _ => None,
3280    }
3281}
3282
3283fn classic_raw_identifier(kind: &EspTrackedKind, value_name: &str) -> String {
3284    let decoded = percent_decode_bounded(value_name).unwrap_or_else(|_| value_name.to_string());
3285    let components: Vec<&str> = decoded.split('/').filter(|part| !part.is_empty()).collect();
3286    let after = match kind {
3287        EspTrackedKind::Msi => "MSI",
3288        EspTrackedKind::Office => "Installation",
3289        EspTrackedKind::ModernApp => "AppManagement",
3290        EspTrackedKind::ScepCertificate => "SCEP",
3291        _ => "",
3292    };
3293    if !after.is_empty() {
3294        if let Some(index) = components
3295            .iter()
3296            .position(|part| part.eq_ignore_ascii_case(after))
3297        {
3298            if let Some(identifier) = components.get(index + 1) {
3299                return (*identifier).to_string();
3300            }
3301        }
3302    }
3303    components
3304        .last()
3305        .map(|value| (*value).to_string())
3306        .unwrap_or(decoded)
3307}
3308
3309fn normalize_for_kind(kind: &EspTrackedKind, raw: EspRawStatus) -> EspStatus {
3310    match kind {
3311        EspTrackedKind::Msi => normalize_office_detail_status(raw),
3312        EspTrackedKind::Office => normalize_office_status(raw, None),
3313        EspTrackedKind::ModernApp | EspTrackedKind::Policy | EspTrackedKind::ScepCertificate => {
3314            normalize_policy_status(raw)
3315        }
3316        EspTrackedKind::Win32App => normalize_classic_esp_status(raw),
3317        EspTrackedKind::PlatformScript | EspTrackedKind::DevicePreparationWorkload => {
3318            normalize_v2_status(raw)
3319        }
3320    }
3321}
3322
3323fn is_provisioning_progress(observation: &EspJsonObservation) -> bool {
3324    observation
3325        .document_type
3326        .eq_ignore_ascii_case("ProvisioningProgress")
3327}
3328
3329fn v2_document_identity(observation: &EspJsonObservation) -> String {
3330    let provenance = &observation.context.provenance;
3331    if let Some(registry) = &provenance.registry {
3332        return format!(
3333            "registry|{}|{}|{}",
3334            registry.hive,
3335            registry.key,
3336            registry.value_name.as_deref().unwrap_or_default()
3337        )
3338        .to_ascii_lowercase();
3339    }
3340    if let Some(event) = &provenance.event {
3341        return format!(
3342            "event|{}|{}|{}",
3343            event.channel,
3344            event.event_id,
3345            event
3346                .record_id
3347                .map(|record_id| record_id.to_string())
3348                .unwrap_or_default()
3349        )
3350        .to_ascii_lowercase();
3351    }
3352    if let Some(file_path) = &provenance.file_path {
3353        return format!("file|{file_path}|{}", observation.document_type).to_ascii_lowercase();
3354    }
3355    format!("document|{}", observation.document_type).to_ascii_lowercase()
3356}
3357
3358fn is_enforcement_state_message(observation: &EspJsonObservation) -> bool {
3359    observation
3360        .document_type
3361        .eq_ignore_ascii_case("EnforcementStateMessage")
3362}
3363
3364fn enforcement_message_group_key(observation: &EspJsonObservation) -> String {
3365    let provenance_key = observation
3366        .context
3367        .provenance
3368        .registry
3369        .as_ref()
3370        .map(|registry| registry.key.as_str())
3371        .or(observation.context.provenance.file_path.as_deref())
3372        .unwrap_or_default();
3373    let components = observation
3374        .json_pointer
3375        .split('/')
3376        .filter(|part| !part.is_empty())
3377        .collect::<Vec<_>>();
3378    let semantic_field = components
3379        .iter()
3380        .position(|part| part.eq_ignore_ascii_case("EnforcementStateMessage"))
3381        .unwrap_or_else(|| components.len().saturating_sub(1));
3382    let record_path = components[..semantic_field].join("/");
3383    format!(
3384        "{}|{}|{}",
3385        observation.context.provenance.source_artifact_id, provenance_key, record_path
3386    )
3387}
3388
3389fn is_page_settings(observation: &EspJsonObservation) -> bool {
3390    observation
3391        .document_type
3392        .eq_ignore_ascii_case("PageSettings")
3393        || observation
3394            .document_type
3395            .eq_ignore_ascii_case("DevicePreparationPageSettings")
3396}
3397
3398fn is_platform_script_observation(observation: &EspRegistryObservation) -> bool {
3399    let key = observation.key.to_ascii_lowercase();
3400    key.contains("intunemanagementextension")
3401        && key.contains("policies")
3402        && (observation.value_name.eq_ignore_ascii_case("Result")
3403            || observation
3404                .value_name
3405                .eq_ignore_ascii_case("LastUpdatedTimeUtc"))
3406}
3407
3408fn is_parity_event(event_id: u32) -> bool {
3409    matches!(
3410        event_id,
3411        72 | 100 | 101 | 107 | 109 | 110 | 111 | 304 | 306 | 1905 | 1906 | 1920 | 1922 | 1924
3412    )
3413}
3414
3415fn event_timeline_kind(event_id: u32) -> EspTimelineKind {
3416    match event_id {
3417        100 | 107 | 109 | 110 | 111 => EspTimelineKind::OfflineDomainJoin,
3418        72 | 101 | 304 | 306 => EspTimelineKind::Registration,
3419        1905 | 1906 | 1920 | 1922 | 1924 => EspTimelineKind::Workload,
3420        _ => EspTimelineKind::Other,
3421    }
3422}
3423
3424fn event_normalized_status(event_id: u32) -> EspNormalizedStatus {
3425    match event_id {
3426        100 | 304 | 1924 => EspNormalizedStatus::Failed,
3427        107 | 306 | 1922 => EspNormalizedStatus::Succeeded,
3428        101 | 72 => EspNormalizedStatus::Processed,
3429        1905 => EspNormalizedStatus::Downloading,
3430        1906 => EspNormalizedStatus::Downloaded,
3431        109 | 110 | 111 | 1920 => EspNormalizedStatus::InProgress,
3432        _ => EspNormalizedStatus::Unknown,
3433    }
3434}
3435
3436fn odj_state_details(
3437    observation: &EspEventLogObservation,
3438) -> Option<(&'static str, EspNormalizedStatus)> {
3439    if !matches!(observation.event_id, 109 | 110) {
3440        return None;
3441    }
3442    let named_data = if observation.named_data.is_empty() {
3443        observation
3444            .context
3445            .provenance
3446            .event
3447            .as_ref()
3448            .map(|event| event.named_data.as_slice())
3449            .unwrap_or_default()
3450    } else {
3451        observation.named_data.as_slice()
3452    };
3453    let state = named_data
3454        .iter()
3455        .find(|value| {
3456            matches!(
3457                value.name.to_ascii_lowercase().as_str(),
3458                "state" | "odjstate" | "status"
3459            )
3460        })
3461        .or_else(|| named_data.first())
3462        .and_then(|value| value.value.trim().parse::<u8>().ok())
3463        .or_else(|| {
3464            let message = observation.message.as_deref()?.to_ascii_lowercase();
3465            if message.contains("timed out") {
3466                Some(3)
3467            } else if message.contains("not configured") {
3468                Some(0)
3469            } else if message.contains("waiting") {
3470                Some(1)
3471            } else if message.contains("processed") {
3472                Some(2)
3473            } else {
3474                None
3475            }
3476        })?;
3477    match state {
3478        0 => Some((
3479            "Offline domain join not configured",
3480            EspNormalizedStatus::NotStarted,
3481        )),
3482        1 => Some(("Waiting for ODJ blob", EspNormalizedStatus::InProgress)),
3483        2 => Some(("Processed ODJ blob", EspNormalizedStatus::Processed)),
3484        3 => Some((
3485            "Timed out waiting for ODJ blob or connectivity",
3486            EspNormalizedStatus::Failed,
3487        )),
3488        _ => None,
3489    }
3490}
3491
3492fn event_default_message(event_id: u32) -> &'static str {
3493    match event_id {
3494        72 => "MDM enrollment",
3495        100 => "Could not establish connectivity",
3496        101 => "SCP discovery successful",
3497        107 => "Successfully applied ODJ blob",
3498        109 | 110 => "Offline domain join state changed",
3499        111 => "Starting wait for ODJ blob",
3500        304 => "Hybrid AADJ device registration failed",
3501        306 => "Hybrid AADJ device registration succeeded",
3502        1905 => "Download started",
3503        1906 => "Download finished",
3504        1920 => "Installation started",
3505        1922 => "Installation finished",
3506        1924 => "Installation failed",
3507        _ => "Unknown event",
3508    }
3509}
3510
3511fn event_title(event_id: u32, named_data: &[EspNamedValue]) -> String {
3512    match event_timeline_kind(event_id) {
3513        EspTimelineKind::OfflineDomainJoin => "Offline Domain Join".to_string(),
3514        EspTimelineKind::Registration => {
3515            if event_id == 72 {
3516                "MDM Enrollment".to_string()
3517            } else {
3518                "Device Registration".to_string()
3519            }
3520        }
3521        EspTimelineKind::Workload => named_data
3522            .iter()
3523            .find(|item| item.name.eq_ignore_ascii_case("ProductCode"))
3524            .map(|item| item.value.clone())
3525            .unwrap_or_else(|| "MSI".to_string()),
3526        _ => format!("Event {event_id}"),
3527    }
3528}
3529
3530fn empty_profile() -> EspProfileEvidence {
3531    EspProfileEvidence {
3532        profile_name: None,
3533        deployment_profile_id: None,
3534        correlation_id: None,
3535        tenant_domain: None,
3536        tenant_id: None,
3537        oobe_config: None,
3538        profile_download_time: None,
3539        join_mode: None,
3540        odj_applied: None,
3541        skip_domain_connectivity_check: None,
3542        device_preparation: None,
3543        evidence: Vec::new(),
3544    }
3545}
3546
3547fn empty_device_preparation() -> EspDevicePreparationEvidence {
3548    EspDevicePreparationEvidence {
3549        agent_download_timeout_seconds: None,
3550        page_timeout_seconds: None,
3551        allow_skip_on_failure: None,
3552        allow_diagnostics: None,
3553        script_ids: Vec::new(),
3554        evidence: Vec::new(),
3555    }
3556}
3557
3558fn empty_hardware() -> EspHardwareEvidence {
3559    EspHardwareEvidence {
3560        os_version: None,
3561        os_build: None,
3562        manufacturer: None,
3563        model: None,
3564        serial_number: None,
3565        tpm_version: None,
3566        evidence: Vec::new(),
3567    }
3568}
3569
3570fn empty_delivery_optimization() -> EspDeliveryOptimizationEvidence {
3571    EspDeliveryOptimizationEvidence {
3572        download_http_bytes: 0,
3573        download_lan_bytes: 0,
3574        download_cache_host_bytes: 0,
3575        peer_share_percent: None,
3576        connected_cache_share_percent: None,
3577        transfers: Vec::new(),
3578        evidence: Vec::new(),
3579    }
3580}
3581
3582fn context_timestamp(context: &EspObservationContext) -> EspTimestamp {
3583    context
3584        .source_timestamp
3585        .clone()
3586        .unwrap_or_else(|| normalize_timestamp(&context.observed_at_utc, None))
3587}
3588
3589fn timestamp_chronology_key(timestamp: &EspTimestamp) -> &str {
3590    timestamp
3591        .normalized_utc
3592        .as_deref()
3593        .unwrap_or(&timestamp.raw_text)
3594}
3595
3596fn context_chronology_key(context: &EspObservationContext) -> String {
3597    timestamp_chronology_key(&context_timestamp(context)).to_string()
3598}
3599
3600fn merge_earliest_timestamp(target: &mut Option<EspTimestamp>, incoming: Option<EspTimestamp>) {
3601    let Some(incoming) = incoming else {
3602        return;
3603    };
3604    if target
3605        .as_ref()
3606        .map(|current| timestamp_chronology_key(&incoming) < timestamp_chronology_key(current))
3607        .unwrap_or(true)
3608    {
3609        *target = Some(incoming);
3610    }
3611}
3612
3613fn merge_latest_timestamp(target: &mut Option<EspTimestamp>, incoming: Option<EspTimestamp>) {
3614    let Some(incoming) = incoming else {
3615        return;
3616    };
3617    if target
3618        .as_ref()
3619        .map(|current| timestamp_chronology_key(&incoming) >= timestamp_chronology_key(current))
3620        .unwrap_or(true)
3621    {
3622        *target = Some(incoming);
3623    }
3624}
3625
3626fn workload_timestamps(
3627    observed: EspTimestamp,
3628    status: &EspNormalizedStatus,
3629) -> EspWorkloadTimestamps {
3630    let started = if matches!(
3631        status,
3632        EspNormalizedStatus::Downloading
3633            | EspNormalizedStatus::Installing
3634            | EspNormalizedStatus::InProgress
3635    ) {
3636        Some(observed.clone())
3637    } else {
3638        None
3639    };
3640    let ended = if matches!(
3641        status,
3642        EspNormalizedStatus::Succeeded | EspNormalizedStatus::Failed
3643    ) {
3644        Some(observed.clone())
3645    } else {
3646        None
3647    };
3648    EspWorkloadTimestamps {
3649        first_observed: observed.clone(),
3650        started,
3651        ended,
3652        last_updated: Some(observed),
3653    }
3654}
3655
3656fn merge_workload_timestamps(current: &mut EspWorkloadTimestamps, incoming: EspWorkloadTimestamps) {
3657    if timestamp_chronology_key(&incoming.first_observed)
3658        < timestamp_chronology_key(&current.first_observed)
3659    {
3660        current.first_observed = incoming.first_observed;
3661    }
3662    merge_earliest_timestamp(&mut current.started, incoming.started);
3663    merge_latest_timestamp(&mut current.ended, incoming.ended);
3664    merge_latest_timestamp(&mut current.last_updated, incoming.last_updated);
3665}
3666
3667fn text_status(raw: &str, normalized: EspNormalizedStatus) -> EspStatus {
3668    EspStatus {
3669        raw: EspRawStatus::Text(raw.to_string()),
3670        normalized,
3671        display: raw.to_string(),
3672        detail: None,
3673    }
3674}
3675
3676fn text_status_from_observation(value: &EspObservationValue) -> EspStatus {
3677    let raw = observation_text(value).unwrap_or_default();
3678    let normalized = if raw.eq_ignore_ascii_case("success") || raw.eq_ignore_ascii_case("completed")
3679    {
3680        EspNormalizedStatus::Succeeded
3681    } else if raw.eq_ignore_ascii_case("failed") {
3682        EspNormalizedStatus::Failed
3683    } else if raw.eq_ignore_ascii_case("inprogress") {
3684        EspNormalizedStatus::InProgress
3685    } else {
3686        EspNormalizedStatus::Unknown
3687    };
3688    text_status(&raw, normalized)
3689}
3690
3691fn boolean_status(value: bool) -> EspStatus {
3692    text_status(
3693        if value { "true" } else { "false" },
3694        if value {
3695            EspNormalizedStatus::Processed
3696        } else {
3697            EspNormalizedStatus::NotStarted
3698        },
3699    )
3700}
3701
3702fn observation_raw_status(value: &EspObservationValue) -> EspRawStatus {
3703    match value {
3704        EspObservationValue::Integer(value) => EspRawStatus::Number(*value),
3705        EspObservationValue::Unsigned(value) => i64::try_from(*value)
3706            .map(EspRawStatus::Number)
3707            .unwrap_or_else(|_| EspRawStatus::Text(value.to_string())),
3708        EspObservationValue::Text(value) => EspRawStatus::Text(value.clone()),
3709        EspObservationValue::Boolean(value) => EspRawStatus::Text(value.to_string()),
3710        EspObservationValue::StringList(value) => EspRawStatus::Text(value.join(",")),
3711    }
3712}
3713
3714fn observation_text(value: &EspObservationValue) -> Option<String> {
3715    match value {
3716        EspObservationValue::Text(value) => Some(value.clone()),
3717        EspObservationValue::Integer(value) => Some(value.to_string()),
3718        EspObservationValue::Unsigned(value) => Some(value.to_string()),
3719        EspObservationValue::Boolean(value) => Some(value.to_string()),
3720        EspObservationValue::StringList(value) => Some(value.join(",")),
3721    }
3722}
3723
3724fn observation_i64(value: &EspObservationValue) -> Option<i64> {
3725    match value {
3726        EspObservationValue::Integer(value) => Some(*value),
3727        EspObservationValue::Unsigned(value) => i64::try_from(*value).ok(),
3728        EspObservationValue::Text(value) => value.parse().ok(),
3729        _ => None,
3730    }
3731}
3732
3733fn observation_u64(value: &EspObservationValue) -> Option<u64> {
3734    match value {
3735        EspObservationValue::Unsigned(value) => Some(*value),
3736        EspObservationValue::Integer(value) => u64::try_from(*value).ok(),
3737        EspObservationValue::Text(value) => value.parse().ok(),
3738        _ => None,
3739    }
3740}
3741
3742fn observation_bool(value: &EspObservationValue) -> Option<bool> {
3743    match value {
3744        EspObservationValue::Boolean(value) => Some(*value),
3745        EspObservationValue::Integer(value) => Some(*value != 0),
3746        EspObservationValue::Unsigned(value) => Some(*value != 0),
3747        EspObservationValue::Text(value) if value.eq_ignore_ascii_case("true") => Some(true),
3748        EspObservationValue::Text(value) if value.eq_ignore_ascii_case("false") => Some(false),
3749        _ => None,
3750    }
3751}
3752
3753fn timestamp_from_observation_value(value: &EspObservationValue) -> Option<EspTimestamp> {
3754    observation_text(value).map(|value| normalize_embedded_timestamp(&value))
3755}
3756
3757fn normalize_embedded_timestamp(raw: &str) -> EspTimestamp {
3758    let unescaped = raw.replace(r"\/", "/");
3759    if let Some(inner) = unescaped
3760        .strip_prefix("/Date(")
3761        .and_then(|value| value.strip_suffix(")/"))
3762    {
3763        let millis_text = inner
3764            .chars()
3765            .enumerate()
3766            .take_while(|(index, character)| {
3767                character.is_ascii_digit() || (*index == 0 && *character == '-')
3768            })
3769            .map(|(_, character)| character)
3770            .collect::<String>();
3771        if let Ok(milliseconds) = millis_text.parse::<i64>() {
3772            if let Some(timestamp) = Utc.timestamp_millis_opt(milliseconds).single() {
3773                return EspTimestamp {
3774                    raw_text: raw.to_string(),
3775                    original_offset: Some("Z".to_string()),
3776                    // Standardize on AutoSi like every other `normalized_utc`
3777                    // producer (see normalize.rs::format_utc) so equal instants
3778                    // serialize to identical strings regardless of source.
3779                    normalized_utc: Some(timestamp.to_rfc3339_opts(SecondsFormat::AutoSi, true)),
3780                    kind: EspTimestampKind::Utc,
3781                };
3782            }
3783        }
3784    }
3785    normalize_timestamp(raw, None)
3786}
3787
3788fn error_code(raw: &str) -> EspErrorCode {
3789    if raw.starts_with("0x") || raw.starts_with("0X") {
3790        let digits = &raw[2..];
3791        EspErrorCode {
3792            raw: raw.to_string(),
3793            decimal: None,
3794            hex: u64::from_str_radix(digits, 16)
3795                .ok()
3796                .map(|_| format!("0x{}", digits.to_ascii_uppercase())),
3797        }
3798    } else {
3799        let decimal = raw.parse::<i64>().ok();
3800        let hex = decimal.map(|value| format!("0x{:08X}", value as i32 as u32));
3801        EspErrorCode {
3802            raw: raw.to_string(),
3803            decimal,
3804            hex,
3805        }
3806    }
3807}
3808
3809fn error_code_normalized_status(code: &EspErrorCode) -> EspNormalizedStatus {
3810    if code.decimal == Some(0)
3811        || code
3812            .raw
3813            .strip_prefix("0x")
3814            .or_else(|| code.raw.strip_prefix("0X"))
3815            .and_then(|value| u64::from_str_radix(value, 16).ok())
3816            == Some(0)
3817    {
3818        EspNormalizedStatus::Succeeded
3819    } else if code.decimal.is_some() || code.hex.is_some() {
3820        EspNormalizedStatus::Failed
3821    } else {
3822        EspNormalizedStatus::Unknown
3823    }
3824}
3825
3826fn sensitive_string(value: String) -> EspClassifiedString {
3827    EspClassifiedString {
3828        value,
3829        sensitivity: EspSensitivity::Sensitive,
3830    }
3831}
3832
3833fn more_restrictive_sensitivity(
3834    current: &EspSensitivity,
3835    incoming: &EspSensitivity,
3836) -> EspSensitivity {
3837    let rank = |value: &EspSensitivity| match value {
3838        EspSensitivity::Public => 0,
3839        EspSensitivity::Sensitive => 1,
3840        EspSensitivity::Restricted => 2,
3841    };
3842    if rank(incoming) > rank(current) {
3843        incoming.clone()
3844    } else {
3845        current.clone()
3846    }
3847}
3848
3849fn tracked_kind_name(kind: &EspTrackedKind) -> &'static str {
3850    match kind {
3851        EspTrackedKind::Msi => "msi",
3852        EspTrackedKind::Office => "office",
3853        EspTrackedKind::ModernApp => "modernApp",
3854        EspTrackedKind::Win32App => "win32App",
3855        EspTrackedKind::Policy => "policy",
3856        EspTrackedKind::ScepCertificate => "scepCertificate",
3857        EspTrackedKind::PlatformScript => "platformScript",
3858        EspTrackedKind::DevicePreparationWorkload => "devicePreparationWorkload",
3859    }
3860}
3861
3862fn path_components(value: &str) -> Vec<String> {
3863    value
3864        .split(['\\', '/'])
3865        .filter(|part| !part.is_empty())
3866        .map(str::to_string)
3867        .collect()
3868}
3869
3870fn last_path_component(value: &str) -> Option<String> {
3871    path_components(value).into_iter().last()
3872}
3873
3874fn escape_component(value: &str) -> String {
3875    value.replace('%', "%25").replace('|', "%7C")
3876}
3877
3878fn session_group_sort_key(session: &EspSession) -> (u8, u8, String) {
3879    (
3880        match session.kind {
3881            EspSessionKind::Classic => 0,
3882            EspSessionKind::DevicePreparationV2 => 1,
3883        },
3884        match session.scope {
3885            EspScope::Device => 0,
3886            EspScope::User => 1,
3887        },
3888        session
3889            .user_sid
3890            .as_ref()
3891            .map(|sid| sid.value.clone())
3892            .unwrap_or_default(),
3893    )
3894}
3895
3896fn session_group_identity(session: &EspSession) -> String {
3897    format!(
3898        "{:?}:{:?}:{}:{}",
3899        session.kind,
3900        session.scope,
3901        session
3902            .user_sid
3903            .as_ref()
3904            .map(|sid| sid.value.as_str())
3905            .unwrap_or(""),
3906        session.session_id.split('|').nth(1).unwrap_or_default()
3907    )
3908}
3909
3910fn session_chronology(session: &EspSession) -> &str {
3911    session
3912        .started_at
3913        .as_ref()
3914        .and_then(|timestamp| timestamp.normalized_utc.as_deref())
3915        .or_else(|| {
3916            session
3917                .started_at
3918                .as_ref()
3919                .map(|timestamp| timestamp.raw_text.as_str())
3920        })
3921        .unwrap_or("")
3922}
3923
3924fn snapshot_phase(scenario: &EspScenario, sessions: &[EspSession]) -> EspPhase {
3925    let latest = sessions
3926        .iter()
3927        .filter(|session| session.is_latest)
3928        .collect::<Vec<_>>();
3929    if latest
3930        .iter()
3931        .any(|session| session.phase == EspPhase::Failed)
3932    {
3933        EspPhase::Failed
3934    } else if !latest.is_empty()
3935        && latest
3936            .iter()
3937            .all(|session| session.phase == EspPhase::Completed)
3938    {
3939        EspPhase::Completed
3940    } else if *scenario == EspScenario::AutopilotDevicePreparationV2 {
3941        EspPhase::DevicePreparation
3942    } else if latest
3943        .iter()
3944        .any(|session| session.scope == EspScope::User && session.phase != EspPhase::Completed)
3945    {
3946        EspPhase::AccountSetup
3947    } else if latest
3948        .iter()
3949        .any(|session| session.scope == EspScope::Device)
3950    {
3951        EspPhase::DeviceSetup
3952    } else {
3953        EspPhase::NotStarted
3954    }
3955}
3956
3957fn identifiers_match(left: &str, right: &str) -> bool {
3958    left.eq_ignore_ascii_case(right)
3959        || match (extract_guid(left), extract_guid(right)) {
3960            (Some(left), Some(right)) => left == right,
3961            _ => false,
3962        }
3963}
3964
3965fn identifier_match_key(value: &str) -> String {
3966    extract_guid(value).unwrap_or_else(|| value.trim().to_ascii_lowercase())
3967}
3968
3969fn latest_code_occurrence(codes: &[DeferredCodeOccurrence]) -> Option<&DeferredCodeOccurrence> {
3970    codes.iter().max_by(|left, right| {
3971        context_chronology_key(&left.context)
3972            .cmp(&context_chronology_key(&right.context))
3973            .then_with(|| left.ordinal.cmp(&right.ordinal))
3974    })
3975}
3976
3977fn deferred_session_matches(session: &EspSession, identity: &DeferredSessionIdentity) -> bool {
3978    if session.kind != EspSessionKind::Classic || session.scope != identity.scope {
3979        return false;
3980    }
3981    if identity.user_sid.as_deref() != session.user_sid.as_ref().map(|sid| sid.value.as_str()) {
3982        return false;
3983    }
3984    let expected = normalize_timestamp(&identity.raw_time, None);
3985    session.started_at.as_ref().is_some_and(|started| {
3986        started.raw_text.eq_ignore_ascii_case(&identity.raw_time)
3987            || (started.normalized_utc.is_some()
3988                && started.normalized_utc == expected.normalized_utc)
3989    })
3990}
3991
3992fn synthetic_context(
3993    source_kind: EspSourceKind,
3994    source_artifact_id: &str,
3995    evidence_id: &str,
3996    timestamp: &str,
3997) -> EspObservationContext {
3998    EspObservationContext {
3999        evidence_ref: EspEvidenceRef {
4000            evidence_id: evidence_id.to_string(),
4001            source_artifact_id: source_artifact_id.to_string(),
4002        },
4003        provenance: EspEvidenceProvenance {
4004            source_kind,
4005            source_artifact_id: source_artifact_id.to_string(),
4006            file_path: None,
4007            line_number: None,
4008            record_number: None,
4009            registry: None,
4010            event: None,
4011        },
4012        source_timestamp: Some(normalize_timestamp(timestamp, None)),
4013        observed_at_utc: timestamp.to_string(),
4014        sensitivity: EspSensitivity::Public,
4015        parse_state: EspParseState::Parsed,
4016        access_state: EspSourceAccessState::Available,
4017    }
4018}
4019
4020fn system_fact_value(fact: &EspSystemFact) -> EspObservationValue {
4021    match fact {
4022        EspSystemFact::OsVersion(value)
4023        | EspSystemFact::OsBuild(value)
4024        | EspSystemFact::Manufacturer(value)
4025        | EspSystemFact::Model(value)
4026        | EspSystemFact::SerialNumber(value)
4027        | EspSystemFact::TpmVersion(value)
4028        | EspSystemFact::Hostname(value)
4029        | EspSystemFact::EntraDeviceId(value)
4030        | EspSystemFact::TenantId(value) => EspObservationValue::Text(value.clone()),
4031        EspSystemFact::JoinMode(value) => EspObservationValue::Text(match value {
4032            EspJoinMode::Entra => "entra".to_string(),
4033            EspJoinMode::HybridEntra => "hybridEntra".to_string(),
4034            EspJoinMode::Unknown(value) => value.clone(),
4035        }),
4036        EspSystemFact::Elevation(value) => EspObservationValue::Boolean(value.is_elevated),
4037    }
4038}