supercov-engine 0.0.47

Rust instrumentation, evidence, attribution, and query engine for Supercov
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! Validation boundary between language-specific producers and shared Rust analysis.
//!
//! Frontends contribute facts, never verdicts. This module enforces the frozen
//! per-run declaration against the normalized manifest/evidence request before
//! the language-neutral analyzer sees it.

use std::collections::{BTreeMap, BTreeSet};

use supercov_contracts::{
    AttributionPrecision, FrontendDeclarationError, FrontendRunDeclaration,
    FrontendRunnerDeclaration, validate_frontend_run_declaration,
};

use crate::coverage_report::{
    CoverageReport, CoverageReportRequest, RawTestResult, ReportError, analyze_coverage_results,
};

#[derive(Debug)]
pub enum FrontendProtocolError {
    Declaration(FrontendDeclarationError),
    InvalidManifestLimitation,
    DuplicateManifestLimitation(String),
    StructuralLimitationMismatch {
        declared: Vec<String>,
        manifest: Vec<String>,
    },
    UndeclaredRunner(String),
    UnobservedRunner(String),
    MissingExactIdentity {
        runner: String,
        axis: &'static str,
    },
    ScopeRunMismatch {
        expected: String,
        actual: String,
    },
    RetryMismatch {
        runner: String,
        result: usize,
        scope: usize,
    },
    InvalidUnstartedResult(String),
    InvalidPhaseKind(String),
    DuplicatePhase(String),
    UnknownPhaseReference(String),
    CyclicPhaseReference(String),
    Analysis(ReportError),
}

impl std::fmt::Display for FrontendProtocolError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Declaration(error) => write!(formatter, "{error}"),
            Self::InvalidManifestLimitation => {
                write!(
                    formatter,
                    "frontend manifest contains a limitation without an ID"
                )
            }
            Self::DuplicateManifestLimitation(id) => {
                write!(formatter, "duplicate frontend manifest limitation: {id}")
            }
            Self::StructuralLimitationMismatch { declared, manifest } => write!(
                formatter,
                "frontend structural limitation references differ: declared={declared:?} manifest={manifest:?}"
            ),
            Self::UndeclaredRunner(runner) => {
                write!(
                    formatter,
                    "frontend evidence uses undeclared runner: {runner}"
                )
            }
            Self::UnobservedRunner(runner) => {
                write!(
                    formatter,
                    "frontend declares an unobserved runner: {runner}"
                )
            }
            Self::MissingExactIdentity { runner, axis } => {
                write!(
                    formatter,
                    "frontend runner {runner} is missing exact {axis} identity"
                )
            }
            Self::ScopeRunMismatch { expected, actual } => write!(
                formatter,
                "frontend evidence run identity differs: expected={expected} actual={actual}"
            ),
            Self::RetryMismatch {
                runner,
                result,
                scope,
            } => write!(
                formatter,
                "frontend runner {runner} retry identity differs: result={result} scope={scope}"
            ),
            Self::InvalidUnstartedResult(reason) => {
                write!(
                    formatter,
                    "invalid selected-but-unstarted test record: {reason}"
                )
            }
            Self::InvalidPhaseKind(kind) => {
                write!(formatter, "unsupported frontend phase kind: {kind}")
            }
            Self::DuplicatePhase(id) => write!(formatter, "duplicate frontend phase ID: {id}"),
            Self::UnknownPhaseReference(id) => {
                write!(formatter, "unknown frontend phase reference: {id}")
            }
            Self::CyclicPhaseReference(id) => {
                write!(formatter, "cyclic frontend phase causality at: {id}")
            }
            Self::Analysis(error) => write!(formatter, "{error:?}"),
        }
    }
}

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

impl From<FrontendDeclarationError> for FrontendProtocolError {
    fn from(error: FrontendDeclarationError) -> Self {
        Self::Declaration(error)
    }
}

fn manifest_limitation_ids(
    request: &CoverageReportRequest,
) -> Result<BTreeSet<String>, FrontendProtocolError> {
    let mut ids = BTreeSet::new();
    for limitation in &request.manifest.limitations {
        let id = limitation
            .get("id")
            .and_then(serde_json::Value::as_str)
            .filter(|id| !id.is_empty())
            .ok_or(FrontendProtocolError::InvalidManifestLimitation)?;
        if !ids.insert(id.to_owned()) {
            return Err(FrontendProtocolError::DuplicateManifestLimitation(
                id.to_owned(),
            ));
        }
    }
    Ok(ids)
}

fn present(value: &str) -> bool {
    !value.trim().is_empty() && !value.chars().any(char::is_control)
}

fn has_attributable_observations(raw: &RawTestResult) -> bool {
    !raw.phases.is_empty()
        || !raw.server.is_empty()
        || raw.runtime.iter().chain(&raw.browser).any(|snapshot| {
            !snapshot.decisions.is_empty()
                || !snapshot.hits.is_empty()
                || !snapshot.events.is_empty()
        })
}

fn require_exact_identities(
    runner: &FrontendRunnerDeclaration,
    raw: &RawTestResult,
    run_id: &str,
    global_phase_ids: &mut BTreeSet<String>,
) -> Result<(), FrontendProtocolError> {
    let missing = |axis| FrontendProtocolError::MissingExactIdentity {
        runner: runner.runner.clone(),
        axis,
    };
    if runner.attribution.test == AttributionPrecision::Exact
        && !raw.test_id.as_deref().is_some_and(present)
    {
        return Err(missing("test"));
    }
    let has_observations = has_attributable_observations(raw);
    let unstarted = raw.status.as_deref() == Some("unstarted");
    if unstarted
        && (raw.role != "test"
            || raw.scope.is_some()
            || raw.retry.is_some()
            || raw.flaky
            || has_observations)
    {
        return Err(FrontendProtocolError::InvalidUnstartedResult(
            "it must be a test with no scope, retry, flaky verdict, phase, or observation".into(),
        ));
    }
    if let Some(scope) = &raw.scope {
        if scope.run_id != run_id {
            return Err(FrontendProtocolError::ScopeRunMismatch {
                expected: run_id.to_owned(),
                actual: scope.run_id.clone(),
            });
        }
        if runner.attribution.worker == AttributionPrecision::Exact && !present(&scope.worker_id) {
            return Err(missing("worker"));
        }
        if runner.attribution.test == AttributionPrecision::Exact
            && (!present(&scope.test_id) || raw.test_id.as_deref() != Some(scope.test_id.as_str()))
        {
            return Err(missing("test"));
        }
        if runner.attribution.retry == AttributionPrecision::Exact {
            let result_retry = raw.retry.ok_or_else(|| missing("retry"))?;
            if result_retry != scope.retry {
                return Err(FrontendProtocolError::RetryMismatch {
                    runner: runner.runner.clone(),
                    result: result_retry,
                    scope: scope.retry,
                });
            }
        }
    } else if runner.attribution.worker == AttributionPrecision::Exact && has_observations {
        return Err(missing("worker"));
    } else if runner.attribution.retry == AttributionPrecision::Exact
        && raw.retry.is_none()
        && !unstarted
    {
        return Err(missing("retry"));
    }

    let mut phase_ids = BTreeSet::new();
    for phase in &raw.phases {
        if !matches!(
            phase.kind.as_str(),
            "setup" | "test" | "action" | "assertion" | "teardown" | "background"
        ) {
            return Err(FrontendProtocolError::InvalidPhaseKind(phase.kind.clone()));
        }
        if runner.attribution.phase == AttributionPrecision::Exact && !present(&phase.id) {
            return Err(missing("phase"));
        }
        if !phase_ids.insert(phase.id.clone()) {
            return Err(FrontendProtocolError::DuplicatePhase(phase.id.clone()));
        }
        if !global_phase_ids.insert(phase.id.clone()) {
            return Err(FrontendProtocolError::DuplicatePhase(phase.id.clone()));
        }
    }
    let phase_reference = |id: &str| {
        if present(id) && phase_ids.contains(id) {
            Ok(())
        } else {
            Err(FrontendProtocolError::UnknownPhaseReference(id.to_owned()))
        }
    };
    for phase in &raw.phases {
        if let Some(cause) = &phase.caused_by_phase_id {
            phase_reference(cause)?;
        }
    }
    let causes = raw
        .phases
        .iter()
        .filter_map(|phase| {
            phase
                .caused_by_phase_id
                .as_ref()
                .map(|cause| (phase.id.as_str(), cause.as_str()))
        })
        .collect::<BTreeMap<_, _>>();
    for start in causes.keys() {
        let mut visited = BTreeSet::new();
        let mut current = *start;
        while let Some(next) = causes.get(current) {
            if !visited.insert(current) {
                return Err(FrontendProtocolError::CyclicPhaseReference(
                    (*start).to_owned(),
                ));
            }
            current = next;
        }
    }
    for snapshot in raw.runtime.iter().chain(&raw.browser) {
        for event in &snapshot.events {
            if let Some(phase) = &event.phase_id {
                phase_reference(phase)?;
            }
        }
    }
    for record in &raw.server {
        if let Some(phase) = &record.phase_id {
            phase_reference(phase)?;
        }
    }
    Ok(())
}

pub fn validate_frontend_report_request(
    declaration: &FrontendRunDeclaration,
    request: &CoverageReportRequest,
) -> Result<(), FrontendProtocolError> {
    validate_frontend_run_declaration(declaration)?;
    let manifest = manifest_limitation_ids(request)?;
    let declared = declaration
        .structural_limitations
        .iter()
        .cloned()
        .collect::<BTreeSet<_>>();
    if declared != manifest {
        return Err(FrontendProtocolError::StructuralLimitationMismatch {
            declared: declared.into_iter().collect(),
            manifest: manifest.into_iter().collect(),
        });
    }

    let runners = declaration
        .runners
        .iter()
        .map(|runner| (runner.runner.as_str(), runner))
        .collect::<BTreeMap<_, _>>();
    let mut observed = BTreeSet::new();
    let mut phase_ids = BTreeSet::new();
    for raw in &request.raw_results {
        let name = raw.provenance.runner.as_str();
        let runner = runners
            .get(name)
            .ok_or_else(|| FrontendProtocolError::UndeclaredRunner(name.to_owned()))?;
        observed.insert(name);
        require_exact_identities(runner, raw, &request.run_id, &mut phase_ids)?;
    }
    for runner in runners.keys() {
        if !observed.contains(runner) {
            return Err(FrontendProtocolError::UnobservedRunner(
                (*runner).to_owned(),
            ));
        }
    }
    Ok(())
}

pub fn analyze_frontend_results(
    declaration: &FrontendRunDeclaration,
    request: &CoverageReportRequest,
) -> Result<CoverageReport, FrontendProtocolError> {
    validate_frontend_report_request(declaration, request)?;
    analyze_coverage_results(request).map_err(FrontendProtocolError::Analysis)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        coverage_analysis::PointKind,
        coverage_report::{
            CoverageManifest, CoveragePhase, ExecutionScope, ExitCodeInput, PointMeta,
            RuntimeEvent, RuntimeSnapshot, TestProvenance,
        },
    };
    use supercov_contracts::{
        ExecutionModel, FrontendAttribution, FrontendLimitation, FrontendLimitationScope,
        FrontendRunnerDeclaration, LANGUAGE_FRONTEND_PROTOCOL_VERSION, StructuralSource,
    };

    fn declaration() -> FrontendRunDeclaration {
        FrontendRunDeclaration {
            protocol_version: LANGUAGE_FRONTEND_PROTOCOL_VERSION,
            frontend_id: "fixture".into(),
            frontend_version: "fixture-v1".into(),
            language: "fixture".into(),
            structural_source: StructuralSource::NativeImport,
            runners: vec![FrontendRunnerDeclaration {
                runner: "fixture-runner".into(),
                execution_model: ExecutionModel::SerialInProcess,
                attribution: FrontendAttribution {
                    run: AttributionPrecision::Exact,
                    worker: AttributionPrecision::Exact,
                    test: AttributionPrecision::Exact,
                    retry: AttributionPrecision::Exact,
                    phase: AttributionPrecision::Exact,
                    action: AttributionPrecision::Unavailable,
                    assertion: AttributionPrecision::Exact,
                },
                limitations: vec![FrontendLimitation {
                    id: "no-action-hook".into(),
                    scopes: vec![FrontendLimitationScope::Action],
                    reason: "The fixture runner has no action lifecycle".into(),
                }],
            }],
            structural_limitations: vec!["dynamic-fixture".into()],
        }
    }

    fn request() -> CoverageReportRequest {
        CoverageReportRequest {
            run_id: "run".into(),
            manifest: CoverageManifest {
                unmeasured: Vec::new(),
                decisions: vec![],
                points: vec![PointMeta {
                    id: "point".into(),
                    kind: PointKind::Statement,
                    file: "src/example.py".into(),
                    line: 1,
                    column: 1,
                    source: "work()".into(),
                    label: None,
                }],
                branches: vec![],
                limitations: vec![serde_json::json!({
                    "id": "dynamic-fixture",
                    "kind": "dynamic-code",
                    "file": "src/example.py",
                    "line": 2,
                    "column": 1,
                    "source": "eval(source)",
                    "reason": "Runtime source has no stable denominator"
                })],
                scope: None,
            },
            raw_results: vec![RawTestResult {
                test_id: Some("test".into()),
                scope: Some(ExecutionScope {
                    version: 1,
                    run_id: "run".into(),
                    worker_id: "worker".into(),
                    test_id: "test".into(),
                    test_key: "test".into(),
                    retry: 0,
                    attempt_id: "attempt".into(),
                }),
                test: "test".into(),
                test_file: Some("tests/test_example.py".into()),
                title: Some("test".into()),
                retry: Some(0),
                status: Some("passed".into()),
                expected_status: Some("passed".into()),
                flaky: false,
                provenance: TestProvenance {
                    runner: "fixture-runner".into(),
                    kind: "integration".into(),
                    project: None,
                    source: "explicit".into(),
                },
                role: "test".into(),
                phases: vec![CoveragePhase {
                    id: "assertion".into(),
                    kind: "assertion".into(),
                    operation: "assert result".into(),
                    source: Some("tests/test_example.py:1".into()),
                    caused_by_phase_id: None,
                    started_at_ms: 1,
                    ended_at_ms: Some(2),
                    status: Some("passed".into()),
                    error: None,
                }],
                runtime: vec![RuntimeSnapshot {
                    decisions: vec![],
                    hits: vec!["point".into()],
                    events: vec![RuntimeEvent {
                        event_type: "hit".into(),
                        id: "point".into(),
                        vector: None,
                        timestamp_ms: 1,
                        phase_id: Some("assertion".into()),
                        statement_id: None,
                        environment: "fixture".into(),
                    }],
                    logicals: vec![],
                }],
                browser: vec![],
                server: vec![],
            }],
            generated_at: "2026-08-25T00:00:00.000Z".into(),
            coverage_model: None,
            integrity: None,
            test_exit_code: ExitCodeInput::Present(Some(0)),
        }
    }

    #[test]
    fn validates_a_declared_frontend_before_shared_analysis() {
        let report = analyze_frontend_results(&declaration(), &request()).unwrap();
        assert!(report.execution.unwrap().valid);
        assert_eq!(report.view.summary.statements.covered, 1);
        assert!(!report.view.summary.coverage_complete);
    }

    #[test]
    fn rejects_hidden_limitations_undeclared_runners_and_missing_exact_scope() {
        let mut hidden = declaration();
        hidden.structural_limitations.clear();
        assert!(matches!(
            validate_frontend_report_request(&hidden, &request()),
            Err(FrontendProtocolError::StructuralLimitationMismatch { .. })
        ));

        let mut undeclared = request();
        undeclared.raw_results[0].provenance.runner = "other".into();
        assert!(matches!(
            validate_frontend_report_request(&declaration(), &undeclared),
            Err(FrontendProtocolError::UndeclaredRunner(runner)) if runner == "other"
        ));

        let mut missing_scope = request();
        missing_scope.raw_results[0].scope = None;
        assert!(matches!(
            validate_frontend_report_request(&declaration(), &missing_scope),
            Err(FrontendProtocolError::MissingExactIdentity { axis: "worker", .. })
        ));

        let mut unknown_phase = request();
        unknown_phase.raw_results[0].runtime[0].events[0].phase_id = Some("other".into());
        assert!(matches!(
            validate_frontend_report_request(&declaration(), &unknown_phase),
            Err(FrontendProtocolError::UnknownPhaseReference(id)) if id == "other"
        ));

        let mut cyclic_phase = request();
        cyclic_phase.raw_results[0].phases[0].caused_by_phase_id = Some("assertion".into());
        assert!(matches!(
            validate_frontend_report_request(&declaration(), &cyclic_phase),
            Err(FrontendProtocolError::CyclicPhaseReference(id)) if id == "assertion"
        ));
    }

    #[test]
    fn selected_but_unstarted_tests_have_no_invented_attempt_identity() {
        let mut unstarted = request();
        let raw = &mut unstarted.raw_results[0];
        raw.scope = None;
        raw.retry = None;
        raw.status = Some("unstarted".into());
        raw.phases.clear();
        raw.runtime.clear();
        assert!(validate_frontend_report_request(&declaration(), &unstarted).is_ok());

        let mut false_attempt = unstarted.clone();
        false_attempt.raw_results[0].retry = Some(0);
        assert!(matches!(
            validate_frontend_report_request(&declaration(), &false_attempt),
            Err(FrontendProtocolError::InvalidUnstartedResult(_))
        ));

        let mut false_observation = unstarted;
        false_observation.raw_results[0].runtime = request().raw_results[0].runtime.clone();
        assert!(matches!(
            validate_frontend_report_request(&declaration(), &false_observation),
            Err(FrontendProtocolError::InvalidUnstartedResult(_))
        ));
    }
}