1use std::collections::{BTreeMap, BTreeSet};
5
6use serde::Serialize;
7
8use crate::{
9 coverage_analysis::McdcVector,
10 coverage_report::{CoveragePhase, DecisionSnapshot, RuntimeEvent, RuntimeSnapshot},
11 rust_compiler_manifest::NormalizedRustCompilerManifest,
12 rust_phase_projection::{RustPhaseProjection, project_rust_assertion_phases},
13 rust_probe_transport::{RustTransportError, RustTransportRead},
14 rust_runtime::RustProbeObservation,
15};
16
17#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
18#[serde(rename_all = "camelCase")]
19pub struct RustCompilerTransportHealth {
20 pub committed: u64,
21 pub incomplete: u64,
22 pub dropped: u64,
23 pub attachments: u64,
24}
25
26impl RustCompilerTransportHealth {
27 pub fn is_complete(&self) -> bool {
28 self.incomplete == 0 && self.dropped == 0
29 }
30}
31
32#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
33#[serde(rename_all = "camelCase")]
34pub struct RustCompilerEvidenceProjection {
35 pub assertion_phases: Vec<CoveragePhase>,
36 pub attributed: RuntimeSnapshot,
37 pub background: RuntimeSnapshot,
38 pub health: RustCompilerTransportHealth,
39}
40
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub enum RustCompilerEvidenceError {
43 Transport(RustTransportError),
44 UnknownProbe(String),
45 UnknownOrdinal(u64),
46 NonEvidenceOrdinal(u64),
47 InvalidVector {
48 id: String,
49 expected: usize,
50 actual: usize,
51 },
52}
53
54impl std::fmt::Display for RustCompilerEvidenceError {
55 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56 match self {
57 Self::Transport(error) => error.fmt(formatter),
58 Self::UnknownProbe(id) => write!(formatter, "unknown Rust compiler probe {id}"),
59 Self::UnknownOrdinal(ordinal) => {
60 write!(formatter, "unknown Rust compiler probe ordinal {ordinal}")
61 }
62 Self::NonEvidenceOrdinal(ordinal) => write!(
63 formatter,
64 "Rust compiler internal ordinal {ordinal} was emitted as coverage evidence"
65 ),
66 Self::InvalidVector {
67 id,
68 expected,
69 actual,
70 } => write!(
71 formatter,
72 "Rust compiler decision {id} expected {expected} conditions but observed {actual}"
73 ),
74 }
75 }
76}
77
78impl std::error::Error for RustCompilerEvidenceError {}
79
80impl From<RustTransportError> for RustCompilerEvidenceError {
81 fn from(error: RustTransportError) -> Self {
82 Self::Transport(error)
83 }
84}
85
86type DecisionVectorKey = (Vec<Option<bool>>, bool);
87
88#[derive(Default)]
89struct SnapshotBuilder {
90 hits: BTreeSet<String>,
91 decisions: BTreeMap<String, BTreeSet<DecisionVectorKey>>,
92 events: Vec<RuntimeEvent>,
93}
94
95impl SnapshotBuilder {
96 fn hit(&mut self, id: &str, phase_id: Option<&str>, timestamp_ms: i64) {
97 self.hits.insert(id.into());
98 self.events.push(RuntimeEvent {
99 event_type: "hit".into(),
100 id: id.into(),
101 vector: None,
102 timestamp_ms,
106 phase_id: phase_id.map(str::to_owned),
107 environment: "rust".into(),
108 });
109 }
110
111 fn decision(
112 &mut self,
113 id: &str,
114 vector: McdcVector,
115 phase_id: Option<&str>,
116 timestamp_ms: i64,
117 ) {
118 self.decisions
119 .entry(id.into())
120 .or_default()
121 .insert((vector.values.clone(), vector.outcome));
122 self.events.push(RuntimeEvent {
123 event_type: "decision".into(),
124 id: id.into(),
125 vector: Some(vector),
126 timestamp_ms,
127 phase_id: phase_id.map(str::to_owned),
128 environment: "rust".into(),
129 });
130 }
131
132 fn finish(
133 self,
134 decisions: &BTreeMap<&str, &crate::coverage_report::DecisionMeta>,
135 ) -> RuntimeSnapshot {
136 RuntimeSnapshot {
137 decisions: self
138 .decisions
139 .into_iter()
140 .map(|(id, vectors)| DecisionSnapshot {
141 meta: (*decisions[&id.as_str()]).clone(),
142 vectors: vectors
143 .into_iter()
144 .map(|(values, outcome)| McdcVector { values, outcome })
145 .collect(),
146 })
147 .collect(),
148 hits: self.hits.into_iter().collect(),
149 events: self.events,
150 }
151 }
152}
153
154fn builder_and_phase<'builder, 'phase>(
155 context_id: u64,
156 base_context_id: u64,
157 base_phase_id: &'phase str,
158 phases: &'phase RustPhaseProjection,
159 attributed: &'builder mut SnapshotBuilder,
160 background: &'builder mut SnapshotBuilder,
161) -> Result<(&'builder mut SnapshotBuilder, Option<&'phase str>), RustCompilerEvidenceError> {
162 if context_id == 0 {
163 return Ok((background, None));
164 }
165 let phase_id = phases.phase_id_for_context(base_context_id, base_phase_id, context_id)?;
166 Ok((attributed, phase_id))
167}
168
169pub fn project_rust_compiler_evidence(
175 base_context_id: u64,
176 base_phase: &CoveragePhase,
177 read: &RustTransportRead,
178 normalized: &NormalizedRustCompilerManifest,
179) -> Result<RustCompilerEvidenceProjection, RustCompilerEvidenceError> {
180 let phases =
181 project_rust_assertion_phases(base_context_id, base_phase, read, &normalized.manifest)?;
182 let points_and_alternatives = normalized
183 .manifest
184 .points
185 .iter()
186 .map(|point| point.id.as_str())
187 .chain(normalized.manifest.branches.iter().flat_map(|branch| {
188 branch
189 .alternatives
190 .iter()
191 .map(|alternative| alternative.id.as_str())
192 }))
193 .collect::<BTreeSet<_>>();
194 let decisions = normalized
195 .manifest
196 .decisions
197 .iter()
198 .map(|decision| (decision.id.as_str(), decision))
199 .collect::<BTreeMap<_, _>>();
200 let mut attributed = SnapshotBuilder::default();
201 let mut background = SnapshotBuilder::default();
202
203 for record in &read.observations {
204 let (builder, phase_id) = builder_and_phase(
205 record.context_id,
206 base_context_id,
207 &base_phase.id,
208 &phases,
209 &mut attributed,
210 &mut background,
211 )?;
212 match &record.observation {
213 RustProbeObservation::Hit { id } => {
214 if !points_and_alternatives.contains(id.as_str()) {
215 return Err(RustCompilerEvidenceError::UnknownProbe(id.clone()));
216 }
217 builder.hit(id, phase_id, base_phase.started_at_ms);
218 }
219 RustProbeObservation::Decision {
220 id,
221 values,
222 outcome,
223 } => {
224 let Some(meta) = decisions.get(id.as_str()) else {
225 return Err(RustCompilerEvidenceError::UnknownProbe(id.clone()));
226 };
227 if values.len() != meta.conditions.len() {
228 return Err(RustCompilerEvidenceError::InvalidVector {
229 id: id.clone(),
230 expected: meta.conditions.len(),
231 actual: values.len(),
232 });
233 }
234 if let Some(selections) = normalized.decision_logical_selection_obligations.get(id)
235 {
236 for selection in selections {
237 let alternative_id = if values[selection.right_condition_index].is_some() {
238 &selection.right_evaluated_id
239 } else {
240 &selection.short_circuited_id
241 };
242 builder.hit(alternative_id, phase_id, base_phase.started_at_ms);
243 }
244 }
245 builder.decision(
246 id,
247 McdcVector {
248 values: values.clone(),
249 outcome: *outcome,
250 },
251 phase_id,
252 base_phase.started_at_ms,
253 );
254 }
255 }
256 }
257 for record in &read.ordinal_hits {
258 let (builder, phase_id) = builder_and_phase(
259 record.context_id,
260 base_context_id,
261 &base_phase.id,
262 &phases,
263 &mut attributed,
264 &mut background,
265 )?;
266 if normalized.internal_ordinals.contains(&record.ordinal) {
267 return Err(RustCompilerEvidenceError::NonEvidenceOrdinal(
268 record.ordinal,
269 ));
270 }
271 let Some(ids) = normalized.hit_obligations_by_ordinal.get(&record.ordinal) else {
272 return Err(RustCompilerEvidenceError::UnknownOrdinal(record.ordinal));
273 };
274 for id in ids {
275 builder.hit(id, phase_id, base_phase.started_at_ms);
276 }
277 }
278
279 Ok(RustCompilerEvidenceProjection {
280 assertion_phases: phases.phases,
281 attributed: attributed.finish(&decisions),
282 background: background.finish(&decisions),
283 health: RustCompilerTransportHealth {
284 committed: read.committed,
285 incomplete: read.incomplete,
286 dropped: read.dropped,
287 attachments: read.attachments,
288 },
289 })
290}
291
292#[cfg(test)]
293mod tests {
294 use crate::{
295 coverage_analysis::PointKind,
296 coverage_report::{
297 BranchAlternativeMeta, BranchMeta, CoverageManifest, DecisionMeta, PointMeta,
298 },
299 rust_compiler_manifest::NormalizedRustCompilerManifest,
300 rust_probe_transport::{
301 RustOrdinalHit, RustPhaseContext, RustTransportObservation, RustTransportRead,
302 rust_assertion_context_id,
303 },
304 };
305
306 use super::*;
307
308 const BASE: u64 = 42;
309 const ASSERTION: &str = "rs:decision:0123456789abcdef01234567";
310
311 fn normalized() -> NormalizedRustCompilerManifest {
312 NormalizedRustCompilerManifest {
313 manifest: CoverageManifest {
314 unmeasured: Vec::new(),
315 decisions: vec![DecisionMeta {
316 id: ASSERTION.into(),
317 file: "src/lib.rs".into(),
318 line: 4,
319 column: 4,
320 source: "assert!(value)".into(),
321 conditions: vec!["value".into()],
322 kind: "assertion".into(),
323 }],
324 points: vec![PointMeta {
325 id: "rs:statement:111111111111111111111111".into(),
326 kind: PointKind::Statement,
327 file: "src/lib.rs".into(),
328 line: 2,
329 column: 4,
330 source: "work();".into(),
331 label: None,
332 }],
333 branches: vec![BranchMeta {
334 id: "rs:branch:222222222222222222222222".into(),
335 kind: "match-arm".into(),
336 file: "src/lib.rs".into(),
337 line: 3,
338 column: 4,
339 source: "first => work()".into(),
340 alternatives: vec![
341 BranchAlternativeMeta {
342 id: "rs:branch-alternative:333333333333333333333333".into(),
343 label: "selected".into(),
344 },
345 BranchAlternativeMeta {
346 id: "rs:branch-alternative:444444444444444444444444".into(),
347 label: "not selected".into(),
348 },
349 ],
350 }],
351 limitations: Vec::new(),
352 scope: None,
353 },
354 hit_obligations_by_ordinal: BTreeMap::from([
355 (10, vec!["rs:statement:111111111111111111111111".into()]),
356 (
357 20,
358 vec![
359 "rs:branch-alternative:333333333333333333333333".into(),
360 "rs:branch-alternative:444444444444444444444444".into(),
361 ],
362 ),
363 ]),
364 internal_ordinals: BTreeSet::from([100]),
365 decision_outcome_obligations: BTreeMap::new(),
366 decision_loop_obligations: BTreeMap::new(),
367 decision_logical_selection_obligations: BTreeMap::new(),
368 }
369 }
370
371 fn base_phase() -> CoveragePhase {
372 CoveragePhase {
373 id: "test-phase".into(),
374 kind: "test".into(),
375 operation: "libtest test".into(),
376 source: Some("src/lib.rs".into()),
377 caused_by_phase_id: None,
378 started_at_ms: 10,
379 ended_at_ms: Some(20),
380 status: Some("passed".into()),
381 error: None,
382 }
383 }
384
385 #[test]
386 fn projects_exact_contexts_ordinals_background_and_health() {
387 let assertion = rust_assertion_context_id(BASE, ASSERTION, 0).unwrap();
388 let read = RustTransportRead {
389 observations: vec![RustTransportObservation {
390 process_id: 1,
391 context_id: assertion,
392 observation: RustProbeObservation::Decision {
393 id: ASSERTION.into(),
394 values: vec![Some(true)],
395 outcome: true,
396 },
397 }],
398 ordinal_hits: vec![
399 RustOrdinalHit {
400 process_id: 1,
401 context_id: BASE,
402 ordinal: 10,
403 },
404 RustOrdinalHit {
405 process_id: 1,
406 context_id: assertion,
407 ordinal: 20,
408 },
409 RustOrdinalHit {
410 process_id: 1,
411 context_id: 0,
412 ordinal: 10,
413 },
414 ],
415 phases: vec![RustPhaseContext {
416 process_id: 1,
417 child_context_id: assertion,
418 parent_context_id: BASE,
419 invocation_nonce: 0,
420 decision_id: ASSERTION.into(),
421 }],
422 committed: 5,
423 incomplete: 1,
424 dropped: 2,
425 attachments: 1,
426 ..RustTransportRead::empty()
427 };
428 let projection =
429 project_rust_compiler_evidence(BASE, &base_phase(), &read, &normalized()).unwrap();
430 assert_eq!(projection.assertion_phases.len(), 1);
431 assert_eq!(
432 projection.assertion_phases[0].status.as_deref(),
433 Some("passed")
434 );
435 assert_eq!(projection.attributed.hits.len(), 3);
436 assert_eq!(projection.background.hits.len(), 1);
437 assert_eq!(projection.attributed.decisions.len(), 1);
438 assert!(
439 projection
440 .attributed
441 .events
442 .iter()
443 .filter(|event| event.id.contains("branch-alternative"))
444 .all(|event| event.phase_id == Some(projection.assertion_phases[0].id.clone()))
445 );
446 assert!(!projection.health.is_complete());
447 }
448
449 #[test]
450 fn rejects_unknown_ordinals_and_vector_widths() {
451 let mut read = RustTransportRead {
452 observations: Vec::new(),
453 ordinal_hits: vec![RustOrdinalHit {
454 process_id: 1,
455 context_id: BASE,
456 ordinal: 999,
457 }],
458 phases: Vec::new(),
459 committed: 1,
460 incomplete: 0,
461 dropped: 0,
462 attachments: 1,
463 ..RustTransportRead::empty()
464 };
465 assert!(matches!(
466 project_rust_compiler_evidence(BASE, &base_phase(), &read, &normalized()),
467 Err(RustCompilerEvidenceError::UnknownOrdinal(999))
468 ));
469 read.ordinal_hits[0].ordinal = 100;
470 assert!(matches!(
471 project_rust_compiler_evidence(BASE, &base_phase(), &read, &normalized()),
472 Err(RustCompilerEvidenceError::NonEvidenceOrdinal(100))
473 ));
474 read.ordinal_hits.clear();
475 read.observations.push(RustTransportObservation {
476 process_id: 1,
477 context_id: BASE,
478 observation: RustProbeObservation::Decision {
479 id: ASSERTION.into(),
480 values: vec![Some(true), Some(false)],
481 outcome: false,
482 },
483 });
484 assert!(matches!(
485 project_rust_compiler_evidence(BASE, &base_phase(), &read, &normalized()),
486 Err(RustCompilerEvidenceError::InvalidVector {
487 expected: 1,
488 actual: 2,
489 ..
490 })
491 ));
492 }
493
494 #[test]
495 fn projects_logical_selection_hits_from_ternary_vectors_without_ordinals() {
496 let mut normalized = normalized();
497 normalized.manifest.decisions[0].conditions = vec!["left".into(), "right".into()];
498 normalized.manifest.branches.push(BranchMeta {
499 id: "logical".into(),
500 kind: "logical-selection".into(),
501 file: "src/lib.rs".into(),
502 line: 4,
503 column: 4,
504 source: "left && right".into(),
505 alternatives: vec![
506 BranchAlternativeMeta {
507 id: "short".into(),
508 label: "short-circuited".into(),
509 },
510 BranchAlternativeMeta {
511 id: "evaluated".into(),
512 label: "right operand evaluated".into(),
513 },
514 ],
515 });
516 normalized.decision_logical_selection_obligations.insert(
517 ASSERTION.into(),
518 vec![
519 crate::rust_compiler_manifest::NormalizedRustLogicalSelection {
520 short_circuited_id: "short".into(),
521 right_evaluated_id: "evaluated".into(),
522 right_condition_index: 1,
523 },
524 ],
525 );
526 let read = RustTransportRead {
527 observations: vec![
528 RustTransportObservation {
529 process_id: 1,
530 context_id: BASE,
531 observation: RustProbeObservation::Decision {
532 id: ASSERTION.into(),
533 values: vec![Some(false), None],
534 outcome: false,
535 },
536 },
537 RustTransportObservation {
538 process_id: 1,
539 context_id: BASE,
540 observation: RustProbeObservation::Decision {
541 id: ASSERTION.into(),
542 values: vec![Some(true), Some(true)],
543 outcome: true,
544 },
545 },
546 ],
547 ordinal_hits: Vec::new(),
548 phases: Vec::new(),
549 committed: 2,
550 incomplete: 0,
551 dropped: 0,
552 attachments: 1,
553 ..RustTransportRead::empty()
554 };
555
556 let projection =
557 project_rust_compiler_evidence(BASE, &base_phase(), &read, &normalized).unwrap();
558 assert_eq!(
559 projection.attributed.hits,
560 vec!["evaluated".to_string(), "short".to_string()]
561 );
562 assert_eq!(
563 projection
564 .attributed
565 .events
566 .iter()
567 .filter(|event| event.event_type == "hit")
568 .map(|event| event.id.as_str())
569 .collect::<Vec<_>>(),
570 vec!["short", "evaluated"]
571 );
572 }
573}