cf-integration 0.2.0

Integration and conformance harness for ContextForge control-plane and data-plane services
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
//! Official conformance artifact paths, loading, and report generation.

use super::*;

const CONFORMANCE_COMPLETION_MARKER: &[u8] = b"complete\n";

impl<R: ProcessRunner> RuntimeContext<R> {
    pub(super) fn regenerate_conformance_report(
        &self,
        results_dir: Option<&Path>,
        output_dir: Option<&Path>,
    ) -> AppResult<()> {
        let artifact_root = results_dir.unwrap_or_else(|| self.config.integration_dir());
        let report_root = output_dir
            .map(Path::to_owned)
            .unwrap_or_else(|| self.config.root().join("reports"));
        let runs = discover_conformance_runs(artifact_root, &report_root)?;
        let mut failures = Vec::new();
        for paths in runs {
            match self.write_comparison_from_artifacts(&paths, None) {
                Ok(comparison) => println!(
                    "{} {}",
                    OutputStyle::stdout().info("Conformance comparison:"),
                    comparison.display()
                ),
                Err(error) => failures.push(format!("{}: {error}", paths.identity())),
            }
        }
        if failures.is_empty() {
            Ok(())
        } else {
            Err(AppFailure::from(anyhow!(
                "conformance report regeneration completed with failures:\n- {}",
                failures.join("\n- ")
            )))
        }
    }

    pub(super) fn write_comparison_from_artifacts(
        &self,
        paths: &ConformancePaths,
        expected_run: Option<(&str, ConformanceServerEra, &str)>,
    ) -> AppResult<PathBuf> {
        let fixture = self.load_conformance_artifact(paths, SemanticLane::FixtureDirect)?;
        let built_in = self.load_conformance_artifact(paths, SemanticLane::BuiltInDataPlane)?;
        let external = self.load_conformance_artifact(paths, SemanticLane::ExternalDataPlane)?;
        if fixture.is_none() && built_in.is_none() && external.is_none() {
            return Err(AppFailure::from(anyhow!(
                "no official conformance artifacts found beneath {}",
                paths.conformance_root.display()
            )));
        }
        let missing = [
            (SemanticLane::FixtureDirect, fixture.is_none()),
            (SemanticLane::BuiltInDataPlane, built_in.is_none()),
            (SemanticLane::ExternalDataPlane, external.is_none()),
        ]
        .into_iter()
        .filter_map(|(lane, missing)| missing.then_some(lane.slug()))
        .collect::<Vec<_>>();
        if !missing.is_empty() {
            return Err(AppFailure::from(anyhow!(
                "missing conformance lanes for {}: {}",
                paths.identity(),
                missing.join(", ")
            )));
        }

        let fixture = fixture.ok_or_else(|| {
            AppFailure::from(anyhow!("missing fixture-direct conformance artifact"))
        })?;
        let built_in = built_in.ok_or_else(|| {
            AppFailure::from(anyhow!("missing built-in dataplane conformance artifact"))
        })?;
        let external = external.ok_or_else(|| {
            AppFailure::from(anyhow!("missing external dataplane conformance artifact"))
        })?;
        let metadata = compatible_metadata(
            Some(&fixture.metadata),
            Some(&built_in.metadata),
            Some(&external.metadata),
            expected_run,
        )?;
        let scenarios = compare_result_sets(&fixture.results, &built_in.results, &external.results);
        let output = paths.report_output.join("mcp-conformance-comparison.md");
        write_comparison_report(
            &output,
            &ComparisonReport {
                client_version: metadata.client_version.clone(),
                server_era: metadata.server_era,
                suite: metadata.suite.clone(),
                fixture: metadata.fixture.clone(),
                scenarios,
            },
        )
        .map_err(AppFailure::from)?;
        Ok(output)
    }

    fn load_conformance_artifact(
        &self,
        paths: &ConformancePaths,
        target: SemanticLane,
    ) -> AppResult<Option<LoadedConformanceArtifact>> {
        let artifact = paths.conformance_lane(target);
        if !artifact.metadata.is_file()
            && !artifact.official_results.is_dir()
            && !artifact.completion.is_file()
        {
            return Ok(None);
        }
        if !artifact.metadata.is_file()
            || !artifact.official_results.is_dir()
            || !artifact.completion.is_file()
        {
            return Err(AppFailure::from(anyhow!(
                "incomplete conformance artifacts for {target} beneath {}",
                artifact.root.display()
            )));
        }
        verify_completion_marker(&artifact.completion)?;
        let metadata = read_run_metadata(&artifact.metadata)?;
        if metadata.direction != ConformanceDirection::Server {
            return Err(AppFailure::from(anyhow!(
                "conformance metadata direction {} does not match server",
                metadata.direction
            )));
        }
        if metadata.target != target.label() {
            return Err(AppFailure::from(anyhow!(
                "conformance metadata target {:?} does not match {target}",
                metadata.target
            )));
        }
        if metadata.oracle != crate::conformance::results::OFFICIAL_CONFORMANCE_PACKAGE {
            return Err(AppFailure::from(anyhow!(
                "conformance artifacts used oracle {:?}, expected {:?}",
                metadata.oracle,
                crate::conformance::results::OFFICIAL_CONFORMANCE_PACKAGE
            )));
        }
        if !is_trusted_official_fixture(&metadata.fixture) {
            return Err(AppFailure::from(anyhow!(
                "conformance artifacts do not identify the pinned official fixture"
            )));
        }
        let results = load_server_results(&artifact.official_results).map_err(AppFailure::from)?;
        validate_server_scenario_set(&results, &metadata.suite, &metadata.client_version)
            .map_err(AppFailure::from)?;
        validate_scored_results(&results).map_err(AppFailure::from)?;
        Ok(Some(LoadedConformanceArtifact { results, metadata }))
    }

    pub(super) fn load_selected_conformance_results(
        &self,
        paths: &ConformancePaths,
        lanes: &[SemanticLane],
    ) -> AppResult<BTreeMap<SemanticLane, ConformanceResults>> {
        let results = self.load_completed_conformance_results(paths, lanes)?;
        for lane in conformance_evidence_lanes(lanes) {
            if !results.contains_key(&lane) {
                return Err(AppFailure::from(anyhow!(
                    "missing required conformance lane {} for {}",
                    lane.slug(),
                    paths.identity()
                )));
            }
        }
        Ok(results)
    }

    pub(super) fn load_completed_conformance_results(
        &self,
        paths: &ConformancePaths,
        lanes: &[SemanticLane],
    ) -> AppResult<BTreeMap<SemanticLane, ConformanceResults>> {
        let mut results = BTreeMap::new();
        for lane in conformance_evidence_lanes(lanes) {
            if let Some(artifact) = self.load_conformance_artifact(paths, lane)? {
                results.insert(lane, artifact.results);
            }
        }
        Ok(results)
    }

    pub(super) fn load_completed_client_conformance_results(
        &self,
        paths: &ConformancePaths,
    ) -> AppResult<BTreeMap<SemanticLane, ConformanceResults>> {
        let lane = SemanticLane::ExternalDataPlane;
        let artifact = paths.client_conformance_lane(lane);
        if !artifact.metadata.is_file()
            && !artifact.official_results.is_dir()
            && !artifact.completion.is_file()
        {
            return Ok(BTreeMap::new());
        }
        if !artifact.metadata.is_file()
            || !artifact.official_results.is_dir()
            || !artifact.completion.is_file()
        {
            return Err(AppFailure::from(anyhow!(
                "incomplete client-conformance artifacts for {lane} beneath {}",
                artifact.root.display()
            )));
        }
        verify_completion_marker(&artifact.completion)?;
        let metadata = read_run_metadata(&artifact.metadata)?;
        if metadata.direction != ConformanceDirection::Client || metadata.target != lane.label() {
            return Err(AppFailure::from(anyhow!(
                "client-conformance metadata does not match external dataplane"
            )));
        }
        if metadata.oracle != crate::conformance::results::OFFICIAL_CONFORMANCE_PACKAGE
            || !is_trusted_official_fixture(&metadata.fixture)
        {
            return Err(AppFailure::from(anyhow!(
                "client-conformance artifacts do not identify the pinned official runner"
            )));
        }
        let results = load_client_results(&artifact.official_results).map_err(AppFailure::from)?;
        validate_client_scenario_set(&results, &metadata.client_version)
            .map_err(AppFailure::from)?;
        validate_scored_results(&results).map_err(AppFailure::from)?;
        Ok(BTreeMap::from([(lane, results)]))
    }
}

fn conformance_evidence_lanes(selected: &[SemanticLane]) -> Vec<SemanticLane> {
    let routed = selected.iter().any(|lane| {
        matches!(
            lane,
            SemanticLane::BuiltInDataPlane | SemanticLane::ExternalDataPlane
        )
    });
    [
        SemanticLane::FixtureDirect,
        SemanticLane::BuiltInDataPlane,
        SemanticLane::ExternalDataPlane,
    ]
    .into_iter()
    .filter(|lane| selected.contains(lane) || (routed && *lane == SemanticLane::FixtureDirect))
    .collect()
}

#[derive(Debug, Clone)]
pub(super) struct ConformancePaths {
    pub(super) conformance_root: PathBuf,
    pub(super) report_output: PathBuf,
    client_version: String,
    server_era: ConformanceServerEra,
}

impl ConformancePaths {
    pub(super) fn new(
        artifact_root: &Path,
        report_root: PathBuf,
        client_version: &str,
        server_era: ConformanceServerEra,
    ) -> Self {
        Self {
            conformance_root: artifact_root
                .join("conformance")
                .join(client_version)
                .join(server_era.label()),
            report_output: report_root
                .join("conformance")
                .join(client_version)
                .join(server_era.label()),
            client_version: client_version.to_owned(),
            server_era,
        }
    }

    pub(super) fn identity(&self) -> String {
        format!(
            "client {} [{}], server {} [{}]",
            client_era_for_version(&self.client_version),
            self.client_version,
            self.server_era.label(),
            self.server_era.protocol_versions_label()
        )
    }

    pub(super) fn setup_log(&self) -> PathBuf {
        self.conformance_root.join("setup.log")
    }

    pub(super) fn baseline_report(&self, target: SemanticLane) -> PathBuf {
        self.report_output
            .join(target.slug())
            .join("baseline-comparison.yml")
    }

    pub(super) fn client_baseline_report(&self, target: SemanticLane) -> PathBuf {
        self.report_output
            .join("client")
            .join(target.slug())
            .join("baseline-comparison.yml")
    }

    pub(super) fn conformance_lane(&self, target: SemanticLane) -> ConformanceLanePaths {
        let root = self.conformance_root.join(target.slug());
        ConformanceLanePaths {
            official_results: root.join("official"),
            runner_log: root.join("runner.log"),
            expected_failures: root.join("expected-failures.yml"),
            metadata: root.join("metadata.json"),
            completion: root.join("complete"),
            root,
        }
    }

    pub(super) fn client_conformance_lane(&self, target: SemanticLane) -> ConformanceLanePaths {
        let root = self.conformance_root.join("client").join(target.slug());
        ConformanceLanePaths {
            official_results: root.join("official"),
            runner_log: root.join("runner.log"),
            expected_failures: root.join("expected-failures.yml"),
            metadata: root.join("metadata.json"),
            completion: root.join("complete"),
            root,
        }
    }

    pub(super) fn clear_conformance(&self) -> AppResult<()> {
        for target in [
            SemanticLane::FixtureDirect,
            SemanticLane::BuiltInDataPlane,
            SemanticLane::ExternalDataPlane,
        ] {
            remove_artifact_directory(&self.conformance_lane(target).root)?;
        }
        remove_artifact_directory(
            &self
                .client_conformance_lane(SemanticLane::ExternalDataPlane)
                .root,
        )?;
        Ok(())
    }
}

#[derive(Debug, Clone)]
pub(super) struct ConformanceLanePaths {
    pub(super) root: PathBuf,
    pub(super) official_results: PathBuf,
    pub(super) runner_log: PathBuf,
    pub(super) expected_failures: PathBuf,
    pub(super) metadata: PathBuf,
    pub(super) completion: PathBuf,
}

struct LoadedConformanceArtifact {
    results: ConformanceResults,
    metadata: ConformanceRunMetadata,
}

fn discover_conformance_runs(
    artifact_root: &Path,
    report_root: &Path,
) -> AppResult<Vec<ConformancePaths>> {
    let root = artifact_root.join("conformance");
    let version_entries = strict_directories(&root, "client-version")?;
    let mut runs = Vec::new();
    for version_entry in version_entries {
        let client_version = version_entry
            .file_name()
            .into_string()
            .map_err(|_| AppFailure::from(anyhow!("client-version directory is not UTF-8")))?;
        ProtocolVersion::from_str(&client_version).map_err(|error| {
            AppFailure::from(anyhow!(
                "invalid conformance client-version directory {client_version:?}: {error}"
            ))
        })?;
        for era_entry in strict_directories(&version_entry.path(), "server-era")? {
            let label = era_entry
                .file_name()
                .into_string()
                .map_err(|_| AppFailure::from(anyhow!("server-era directory is not UTF-8")))?;
            let server_era = ConformanceServerEra::from_label(&label).ok_or_else(|| {
                AppFailure::from(anyhow!(
                    "unknown conformance server-era directory {label:?}"
                ))
            })?;
            runs.push(ConformancePaths::new(
                artifact_root,
                report_root.to_owned(),
                &client_version,
                server_era,
            ));
        }
    }
    if runs.is_empty() {
        return Err(AppFailure::from(anyhow!(
            "no partitioned official conformance artifacts found beneath {}",
            root.display()
        )));
    }
    Ok(runs)
}

fn strict_directories(path: &Path, dimension: &str) -> AppResult<Vec<fs::DirEntry>> {
    let mut entries = fs::read_dir(path)
        .with_context(|| format!("failed to read conformance {dimension} directory {path:?}"))
        .map_err(AppFailure::from)?
        .collect::<std::io::Result<Vec<_>>>()
        .with_context(|| format!("failed to enumerate conformance directory {path:?}"))
        .map_err(AppFailure::from)?;
    entries.sort_by_key(fs::DirEntry::file_name);
    for entry in &entries {
        let file_type = entry
            .file_type()
            .with_context(|| format!("failed to inspect conformance entry {:?}", entry.path()))
            .map_err(AppFailure::from)?;
        if !file_type.is_dir() || file_type.is_symlink() {
            return Err(AppFailure::from(anyhow!(
                "unexpected non-directory entry in conformance {dimension} directory: {}",
                entry.path().display()
            )));
        }
    }
    Ok(entries)
}

pub(super) fn recreate_directory(path: &Path) -> AppResult<()> {
    if path.exists() {
        fs::remove_dir_all(path)
            .with_context(|| format!("failed to clear result directory {path:?}"))
            .map_err(AppFailure::from)?;
    }
    fs::create_dir_all(path)
        .with_context(|| format!("failed to create result directory {path:?}"))
        .map_err(AppFailure::from)
}

pub(super) fn remove_file_if_exists(path: &Path) -> AppResult<()> {
    match fs::remove_file(path) {
        Ok(()) => Ok(()),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(AppFailure::from(
            anyhow!(error).context(format!("failed to clear completion marker {path:?}")),
        )),
    }
}

fn write_completion_marker(path: &Path) -> AppResult<()> {
    fs::write(path, CONFORMANCE_COMPLETION_MARKER)
        .with_context(|| format!("failed to write conformance completion marker {path:?}"))
        .map_err(AppFailure::from)
}

pub(super) fn conformance_process_completed(process_result: &AppResult<()>) -> bool {
    match process_result {
        Ok(()) => true,
        Err(AppFailure::Infrastructure(InfrastructureError::ChildExit { status, .. })) => {
            status.code().is_some()
        }
        Err(AppFailure::Infrastructure(InfrastructureError::Native(_)))
        | Err(AppFailure::Native(_))
        | Err(AppFailure::Reported) => false,
    }
}

pub(super) fn mark_conformance_complete(
    process_result: &AppResult<()>,
    results: &ConformanceResults,
    target: SemanticLane,
    suite: &str,
    spec_version: &str,
    path: &Path,
) -> AppResult<bool> {
    if !conformance_process_completed(process_result) {
        return Ok(false);
    }
    validate_server_scenario_set(results, suite, spec_version)
        .with_context(|| format!("official conformance did not complete for {target}"))
        .map_err(AppFailure::from)?;
    write_completion_marker(path)?;
    Ok(true)
}

pub(super) fn mark_client_conformance_complete(
    results: &ConformanceResults,
    target: SemanticLane,
    spec_version: &str,
    path: &Path,
) -> AppResult<()> {
    validate_client_scenario_set(results, spec_version)
        .with_context(|| format!("official client conformance did not complete for {target}"))
        .map_err(AppFailure::from)?;
    write_completion_marker(path)
}

fn verify_completion_marker(path: &Path) -> AppResult<()> {
    let marker = fs::read(path)
        .with_context(|| format!("failed to read conformance completion marker {path:?}"))
        .map_err(AppFailure::from)?;
    if marker != CONFORMANCE_COMPLETION_MARKER {
        return Err(AppFailure::from(anyhow!(
            "invalid conformance completion marker {path:?}"
        )));
    }
    Ok(())
}

pub(super) fn write_run_metadata(path: &Path, metadata: &ConformanceRunMetadata) -> AppResult<()> {
    let serialized = serde_json::to_vec_pretty(metadata)
        .context("failed to serialize conformance run metadata")
        .map_err(AppFailure::from)?;
    fs::write(path, serialized)
        .with_context(|| format!("failed to write conformance run metadata {path:?}"))
        .map_err(AppFailure::from)
}

fn read_run_metadata(path: &Path) -> AppResult<ConformanceRunMetadata> {
    let source = fs::read(path)
        .with_context(|| format!("failed to read conformance run metadata {path:?}"))
        .map_err(AppFailure::from)?;
    serde_json::from_slice(&source)
        .with_context(|| format!("failed to parse conformance run metadata {path:?}"))
        .map_err(AppFailure::from)
}

fn compatible_metadata<'a>(
    fixture: Option<&'a ConformanceRunMetadata>,
    built_in: Option<&'a ConformanceRunMetadata>,
    external: Option<&'a ConformanceRunMetadata>,
    expected_run: Option<(&str, ConformanceServerEra, &str)>,
) -> AppResult<&'a ConformanceRunMetadata> {
    let metadata = fixture.or(built_in).or(external).ok_or_else(|| {
        AppFailure::from(anyhow!(
            "no conformance metadata is available for reporting"
        ))
    })?;
    for candidate in [fixture, built_in, external].into_iter().flatten() {
        if candidate.fixture != metadata.fixture {
            return Err(AppFailure::from(anyhow!(
                "direct fixture, built-in dataplane, and external dataplane conformance fixture provenance mismatch"
            )));
        }
        if candidate.client_version != metadata.client_version
            || candidate.server_era != metadata.server_era
            || candidate.suite != metadata.suite
            || candidate.oracle != metadata.oracle
        {
            return Err(AppFailure::from(anyhow!(
                "direct fixture, built-in dataplane, and external dataplane conformance artifacts were produced by incompatible runs"
            )));
        }
    }
    if let Some((spec_version, server_era, suite)) = expected_run
        && (metadata.client_version != spec_version
            || metadata.server_era != server_era
            || metadata.suite != suite)
    {
        return Err(AppFailure::from(anyhow!(
            "conformance artifacts do not match requested client spec version {spec_version:?}, server era {server_era:?}, and suite {suite:?}"
        )));
    }
    Ok(metadata)
}

pub(super) const fn conformance_target(topology: StackMode) -> SemanticLane {
    match topology {
        StackMode::Controlplane => SemanticLane::BuiltInDataPlane,
        StackMode::Dataplane => SemanticLane::ExternalDataPlane,
    }
}

fn remove_artifact_directory(path: &Path) -> AppResult<()> {
    match fs::remove_dir_all(path) {
        Ok(()) => Ok(()),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(AppFailure::from(
            anyhow!(error).context(format!("failed to clear conformance artifacts {path:?}")),
        )),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::conformance::fixture::{
        OFFICIAL_CONFORMANCE_REPOSITORY, OFFICIAL_CONFORMANCE_REVISION,
        OFFICIAL_CONFORMANCE_SERVER_ID,
    };
    use crate::conformance::results::OFFICIAL_CONFORMANCE_PACKAGE;

    fn metadata(target: SemanticLane) -> ConformanceRunMetadata {
        ConformanceRunMetadata {
            oracle: OFFICIAL_CONFORMANCE_PACKAGE.to_owned(),
            target: target.label().to_owned(),
            direction: ConformanceDirection::Server,
            client_version: "2026-07-28".to_owned(),
            server_era: ConformanceServerEra::Dual,
            suite: "all".to_owned(),
            fixture: ConformanceFixtureMetadata {
                repository: OFFICIAL_CONFORMANCE_REPOSITORY.to_owned(),
                revision: OFFICIAL_CONFORMANCE_REVISION.to_owned(),
                server_id: OFFICIAL_CONFORMANCE_SERVER_ID.to_owned(),
            },
        }
    }

    #[test]
    fn conformance_paths_partition_all_three_lanes() {
        let paths = ConformancePaths::new(
            Path::new("artifacts"),
            PathBuf::from("reports"),
            "2026-07-28",
            ConformanceServerEra::Modern,
        );

        assert_eq!(
            paths.conformance_lane(SemanticLane::FixtureDirect).root,
            PathBuf::from("artifacts/conformance/2026-07-28/modern/fixture-direct")
        );
        assert_eq!(
            paths.conformance_lane(SemanticLane::BuiltInDataPlane).root,
            PathBuf::from("artifacts/conformance/2026-07-28/modern/built-in-data-plane")
        );
        assert_eq!(
            paths.conformance_lane(SemanticLane::ExternalDataPlane).root,
            PathBuf::from("artifacts/conformance/2026-07-28/modern/external-data-plane")
        );
        assert_eq!(
            paths.baseline_report(SemanticLane::BuiltInDataPlane),
            PathBuf::from(
                "reports/conformance/2026-07-28/modern/built-in-data-plane/baseline-comparison.yml"
            )
        );
        assert_eq!(
            paths.identity(),
            "client modern [2026-07-28], server modern [2026-07-28]"
        );
    }

    #[test]
    fn routed_selection_loads_direct_fixture_evidence_without_selecting_its_baseline() {
        assert_eq!(
            conformance_evidence_lanes(&[SemanticLane::BuiltInDataPlane]),
            [SemanticLane::FixtureDirect, SemanticLane::BuiltInDataPlane]
        );
        assert_eq!(
            conformance_evidence_lanes(&[SemanticLane::ExternalDataPlane]),
            [SemanticLane::FixtureDirect, SemanticLane::ExternalDataPlane]
        );
        assert_eq!(
            conformance_evidence_lanes(&[SemanticLane::FixtureDirect]),
            [SemanticLane::FixtureDirect]
        );
    }

    #[test]
    fn clearing_a_run_removes_every_lane_to_prevent_stale_comparisons() {
        let directory = tempfile::tempdir().expect("temporary artifact root");
        let paths = ConformancePaths::new(
            directory.path(),
            PathBuf::from("reports"),
            "2026-07-28",
            ConformanceServerEra::Dual,
        );
        for target in [
            SemanticLane::FixtureDirect,
            SemanticLane::BuiltInDataPlane,
            SemanticLane::ExternalDataPlane,
        ] {
            fs::create_dir_all(paths.conformance_lane(target).root)
                .expect("lane directory should be created");
        }

        paths
            .clear_conformance()
            .expect("all old lanes should be removed");

        for target in [
            SemanticLane::FixtureDirect,
            SemanticLane::BuiltInDataPlane,
            SemanticLane::ExternalDataPlane,
        ] {
            assert!(!paths.conformance_lane(target).root.exists());
        }
    }

    #[test]
    fn partial_lane_metadata_is_reportable_when_provenance_matches() {
        let fixture = metadata(SemanticLane::FixtureDirect);
        let dataplane = metadata(SemanticLane::ExternalDataPlane);

        let selected = compatible_metadata(
            Some(&fixture),
            None,
            Some(&dataplane),
            Some(("2026-07-28", ConformanceServerEra::Dual, "all")),
        )
        .expect("selected lanes should be compatible");

        assert_eq!(selected.client_version, "2026-07-28");
    }

    #[test]
    fn mismatched_fixture_provenance_prevents_cross_lane_comparison() {
        let fixture = metadata(SemanticLane::FixtureDirect);
        let mut dataplane = metadata(SemanticLane::ExternalDataPlane);
        dataplane.fixture.revision = "different".to_owned();

        let error = compatible_metadata(Some(&fixture), None, Some(&dataplane), None)
            .expect_err("mismatched provenance must fail")
            .to_string();

        assert!(error.contains("provenance mismatch"));
        assert!(!error.contains("different"));
    }

    #[test]
    fn mismatched_server_eras_prevent_cross_lane_comparison() {
        let fixture = metadata(SemanticLane::FixtureDirect);
        let mut dataplane = metadata(SemanticLane::ExternalDataPlane);
        dataplane.server_era = ConformanceServerEra::Legacy;

        let error = compatible_metadata(Some(&fixture), None, Some(&dataplane), None)
            .expect_err("different server eras must not be compared")
            .to_string();

        assert!(error.contains("incompatible runs"));
    }
}