exochain-dag-db-lab 0.2.0-beta

EXOCHAIN DAG DB diagnostics, graph explorer, benchmarks, and lab tools
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
//! Deterministic benchmark database silo isolation contracts.

use std::{
    collections::BTreeMap,
    fs,
    path::{Path, PathBuf},
};

use exo_core::Hash256;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use thiserror::Error;

pub const MAIN_DATABASE_URL_ENV: &str = "EXO_DAGDB_MAIN_DATABASE_URL";
pub const NEUTRAL_BENCH_DATABASE_URL_ENV: &str = "EXO_DAGDB_NEUTRAL_BENCH_DATABASE_URL";
pub const CONTROL_BENCH_DATABASE_URL_ALIAS_ENV: &str = "EXO_DAGDB_CONTROL_BENCH_DATABASE_URL";
pub const GOVERNED_BENCH_DATABASE_URL_ENV: &str = "EXO_DAGDB_GOVERNED_BENCH_DATABASE_URL";

pub const BENCHMARK_ISOLATION_DIR: &str = "target/dagdb/benchmark_isolation";
pub const SILO_CONFIG_SUMMARY_JSON: &str =
    "target/dagdb/benchmark_isolation/silo_config_summary.json";
pub const SILO_VALIDATION_REPORT_JSON: &str =
    "target/dagdb/benchmark_isolation/silo_validation_report.json";
pub const SILO_CONTAMINATION_REPORT_JSON: &str =
    "target/dagdb/benchmark_isolation/silo_contamination_report.json";
pub const RUNNER_SILO_ASSIGNMENT_JSON: &str =
    "target/dagdb/benchmark_isolation/runner_silo_assignment.json";
pub const BENCHMARK_ISOLATION_SUMMARY_MD: &str =
    "target/dagdb/benchmark_isolation/benchmark_isolation_summary.md";

const E2E_DIAGNOSTIC_DIR: &str = "target/dagdb/end_to_end_diagnostics";
pub const SUMMARY_JSON: &str = "summary.json";
pub const PER_TASK_RESULTS_JSON: &str = "per_task_results.json";
pub const COST_BREAKDOWN_JSON: &str = "cost_breakdown.json";
pub const QUALITY_BREAKDOWN_JSON: &str = "quality_breakdown.json";
pub const LATENCY_BREAKDOWN_JSON: &str = "latency_breakdown.json";
pub const RECOMMENDATIONS_MD: &str = "recommendations.md";
const REPORT_SCHEMA_VERSION: &str = "dagdb_benchmark_isolation_v1";

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DatabaseSiloRole {
    Main,
    NeutralBenchmark,
    GovernedBenchmark,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DatabaseSiloPurpose {
    ProductionMemory,
    NeutralBaseline,
    GovernedDagBenchmark,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DatabaseSiloConfig {
    pub role: DatabaseSiloRole,
    pub purpose: DatabaseSiloPurpose,
    #[serde(skip_serializing)]
    pub database_url_env: String,
    pub configured: bool,
    pub tenant_id: String,
    pub namespace: String,
    pub read_only: bool,
    pub allow_benchmark_writes: bool,
    pub allow_synthetic_data: bool,
    pub allow_graph_routing: bool,
    pub allow_route_memory: bool,
    pub allow_canonical_graph: bool,
    pub allow_context_packet_graph: bool,
    pub allow_private_payload_reads: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BenchmarkRunMode {
    NeutralLongContext,
    NeutralFlatRag,
    NoMemoryLowerBound,
    RawDagRouting,
    GovernedDagdb,
    GovernedDagdbOptimized,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BenchmarkSiloAssignment {
    pub run_mode: BenchmarkRunMode,
    pub required_silo_role: DatabaseSiloRole,
    pub graph_routing_allowed: bool,
    pub writeback_allowed: bool,
    pub synthetic_data_allowed: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BenchmarkIsolationValidation {
    pub valid: bool,
    pub errors: Vec<String>,
    pub warnings: Vec<String>,
    pub main_db_protected: bool,
    pub neutral_governed_separated: bool,
    pub runner_silo_assignments: Vec<BenchmarkSiloAssignment>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SiloContaminationStatus {
    Clean,
    Contaminated,
    UnknownNotObservable,
    NotConfiguredDemoMode,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SiloContaminationCheck {
    pub checked_at_commit_or_run_id: String,
    pub main_db_used_for_benchmark: SiloContaminationStatus,
    pub neutral_used_graph_routing: SiloContaminationStatus,
    pub governed_used_neutral_state: SiloContaminationStatus,
    pub cross_silo_route_memory_reuse: SiloContaminationStatus,
    pub cross_silo_context_packet_reuse: SiloContaminationStatus,
    pub cross_silo_receipt_reuse: SiloContaminationStatus,
    pub contamination_detected: bool,
    pub findings: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ArtifactHashStatus {
    pub path: String,
    pub status: String,
    pub hash: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BenchmarkIsolationReport {
    pub schema_version: String,
    pub report_id: String,
    pub fixture_id: String,
    pub neutral_silo_status: DatabaseSiloConfig,
    pub governed_silo_status: DatabaseSiloConfig,
    pub main_silo_status: DatabaseSiloConfig,
    pub validation: BenchmarkIsolationValidation,
    pub contamination_check: SiloContaminationCheck,
    pub diagnostic_report_ids: Vec<String>,
    pub generated_at_harness_run_id: String,
    pub diagnostic_summary_hash: ArtifactHashStatus,
    pub per_task_results_hash: ArtifactHashStatus,
    pub cost_breakdown_hash: ArtifactHashStatus,
    pub quality_breakdown_hash: ArtifactHashStatus,
    pub latency_breakdown_hash: ArtifactHashStatus,
    pub recommendations_hash: ArtifactHashStatus,
    pub source_artifact_paths: Vec<String>,
    pub source_commit_or_run_id: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContaminationEvidence {
    pub demo_mode: bool,
    pub main_db_used_for_benchmark: Option<bool>,
    pub neutral_used_graph_routing: Option<bool>,
    pub governed_used_neutral_state: Option<bool>,
    pub cross_silo_route_memory_reuse: Option<bool>,
    pub cross_silo_context_packet_reuse: Option<bool>,
    pub cross_silo_receipt_reuse: Option<bool>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BenchmarkIsolationArtifacts {
    pub silo_config_summary_json: String,
    pub silo_validation_report_json: String,
    pub silo_contamination_report_json: String,
    pub runner_silo_assignment_json: String,
    pub benchmark_isolation_summary_md: String,
}

#[derive(Debug, Error)]
pub enum BenchmarkIsolationError {
    #[error("benchmark_silo_assignment_invalid: {reason}")]
    InvalidAssignment { reason: String },
    #[error("benchmark_isolation_io_failed: {path}")]
    Io {
        path: String,
        #[source]
        source: std::io::Error,
    },
    #[error("benchmark_isolation_json_failed")]
    Json {
        #[source]
        source: serde_json::Error,
    },
}

#[must_use]
pub fn all_benchmark_run_modes() -> Vec<BenchmarkRunMode> {
    vec![
        BenchmarkRunMode::NeutralLongContext,
        BenchmarkRunMode::NeutralFlatRag,
        BenchmarkRunMode::NoMemoryLowerBound,
        BenchmarkRunMode::RawDagRouting,
        BenchmarkRunMode::GovernedDagdb,
        BenchmarkRunMode::GovernedDagdbOptimized,
    ]
}

#[must_use]
pub fn default_silo_configs_from_env<F>(env_lookup: F) -> Vec<DatabaseSiloConfig>
where
    F: Fn(&str) -> Option<String>,
{
    let neutral_env = if env_lookup(NEUTRAL_BENCH_DATABASE_URL_ENV).is_some() {
        NEUTRAL_BENCH_DATABASE_URL_ENV
    } else {
        CONTROL_BENCH_DATABASE_URL_ALIAS_ENV
    };
    vec![
        DatabaseSiloConfig {
            role: DatabaseSiloRole::Main,
            purpose: DatabaseSiloPurpose::ProductionMemory,
            database_url_env: MAIN_DATABASE_URL_ENV.into(),
            configured: env_lookup(MAIN_DATABASE_URL_ENV).is_some(),
            tenant_id: "main".into(),
            namespace: "production".into(),
            read_only: true,
            allow_benchmark_writes: false,
            allow_synthetic_data: false,
            allow_graph_routing: true,
            allow_route_memory: true,
            allow_canonical_graph: true,
            allow_context_packet_graph: true,
            allow_private_payload_reads: false,
        },
        DatabaseSiloConfig {
            role: DatabaseSiloRole::NeutralBenchmark,
            purpose: DatabaseSiloPurpose::NeutralBaseline,
            database_url_env: neutral_env.into(),
            configured: env_lookup(neutral_env).is_some(),
            tenant_id: "benchmark_neutral".into(),
            namespace: "neutral_benchmark".into(),
            read_only: false,
            allow_benchmark_writes: true,
            allow_synthetic_data: true,
            allow_graph_routing: false,
            allow_route_memory: false,
            allow_canonical_graph: false,
            allow_context_packet_graph: false,
            allow_private_payload_reads: false,
        },
        DatabaseSiloConfig {
            role: DatabaseSiloRole::GovernedBenchmark,
            purpose: DatabaseSiloPurpose::GovernedDagBenchmark,
            database_url_env: GOVERNED_BENCH_DATABASE_URL_ENV.into(),
            configured: env_lookup(GOVERNED_BENCH_DATABASE_URL_ENV).is_some(),
            tenant_id: "benchmark_governed".into(),
            namespace: "governed_benchmark".into(),
            read_only: false,
            allow_benchmark_writes: true,
            allow_synthetic_data: true,
            allow_graph_routing: true,
            allow_route_memory: true,
            allow_canonical_graph: true,
            allow_context_packet_graph: true,
            allow_private_payload_reads: false,
        },
    ]
}

#[must_use]
pub fn default_silo_configs() -> Vec<DatabaseSiloConfig> {
    default_silo_configs_from_env(|key| std::env::var(key).ok())
}

pub fn validate_benchmark_run_silo_assignment(
    run_mode: BenchmarkRunMode,
    silo_config: &DatabaseSiloConfig,
) -> std::result::Result<BenchmarkSiloAssignment, BenchmarkIsolationError> {
    let assignment = benchmark_silo_assignment(run_mode);
    if silo_config.role != assignment.required_silo_role {
        return Err(BenchmarkIsolationError::InvalidAssignment {
            reason: format!(
                "benchmark_runner_wrong_silo:{:?}:{:?}",
                run_mode, silo_config.role
            ),
        });
    }
    let neutral_graph_features_enabled = [
        silo_config.allow_graph_routing,
        silo_config.allow_route_memory,
        silo_config.allow_canonical_graph,
        silo_config.allow_context_packet_graph,
    ]
    .contains(&true);
    if assignment.required_silo_role == DatabaseSiloRole::NeutralBenchmark
        && neutral_graph_features_enabled
    {
        return Err(BenchmarkIsolationError::InvalidAssignment {
            reason: "neutral_runner_graph_features_enabled".into(),
        });
    }
    if assignment.required_silo_role == DatabaseSiloRole::GovernedBenchmark
        && !silo_config.allow_graph_routing
    {
        return Err(BenchmarkIsolationError::InvalidAssignment {
            reason: "governed_runner_graph_routing_disabled".into(),
        });
    }
    Ok(assignment)
}

#[must_use]
pub fn benchmark_silo_assignment(run_mode: BenchmarkRunMode) -> BenchmarkSiloAssignment {
    match run_mode {
        BenchmarkRunMode::NeutralLongContext
        | BenchmarkRunMode::NeutralFlatRag
        | BenchmarkRunMode::NoMemoryLowerBound => BenchmarkSiloAssignment {
            run_mode,
            required_silo_role: DatabaseSiloRole::NeutralBenchmark,
            graph_routing_allowed: false,
            writeback_allowed: false,
            synthetic_data_allowed: true,
        },
        BenchmarkRunMode::RawDagRouting
        | BenchmarkRunMode::GovernedDagdb
        | BenchmarkRunMode::GovernedDagdbOptimized => BenchmarkSiloAssignment {
            run_mode,
            required_silo_role: DatabaseSiloRole::GovernedBenchmark,
            graph_routing_allowed: true,
            writeback_allowed: true,
            synthetic_data_allowed: true,
        },
    }
}

#[must_use]
pub fn validate_benchmark_silo_config<F>(
    configs: &[DatabaseSiloConfig],
    run_plan: &[BenchmarkRunMode],
    env_lookup: F,
) -> BenchmarkIsolationValidation
where
    F: Fn(&str) -> Option<String>,
{
    let mut errors = Vec::new();
    let mut warnings = Vec::new();
    let mut by_role = BTreeMap::new();
    for config in configs {
        by_role.insert(config.role, config);
    }
    let main_db_protected = by_role
        .get(&DatabaseSiloRole::Main)
        .map(|config| !config.allow_benchmark_writes && !config.allow_synthetic_data)
        .unwrap_or(false);
    if !main_db_protected {
        errors.push("main_database_allows_benchmark_mutation".into());
    }

    for role in [
        DatabaseSiloRole::Main,
        DatabaseSiloRole::NeutralBenchmark,
        DatabaseSiloRole::GovernedBenchmark,
    ] {
        if !by_role.contains_key(&role) {
            errors.push(format!("missing_silo_role:{role:?}"));
        }
    }

    let mut runner_silo_assignments = Vec::new();
    for run_mode in run_plan {
        let assignment = benchmark_silo_assignment(*run_mode);
        if let Some(config) = by_role.get(&assignment.required_silo_role) {
            match validate_benchmark_run_silo_assignment(*run_mode, config) {
                Ok(guarded) => runner_silo_assignments.push(guarded),
                Err(error) => errors.push(error.to_string()),
            }
        } else {
            errors.push(format!("missing_assignment_silo:{:?}", run_mode));
        }
    }

    if let Some(neutral) = by_role.get(&DatabaseSiloRole::NeutralBenchmark) {
        if neutral.allow_graph_routing {
            errors.push("neutral_benchmark_graph_routing_enabled".into());
        }
        if neutral.allow_route_memory {
            errors.push("neutral_benchmark_route_memory_enabled".into());
        }
        if neutral.allow_canonical_graph {
            errors.push("neutral_benchmark_canonical_graph_enabled".into());
        }
        if neutral.allow_context_packet_graph {
            errors.push("neutral_benchmark_context_packet_graph_enabled".into());
        }
    }
    if let Some(governed) = by_role.get(&DatabaseSiloRole::GovernedBenchmark) {
        if !governed.allow_graph_routing {
            errors.push("governed_benchmark_graph_routing_disabled".into());
        }
    }

    for config in configs
        .iter()
        .filter(|config| config.configured && config.role != DatabaseSiloRole::Main)
    {
        if config.tenant_id.is_empty() {
            errors.push(format!(
                "configured_benchmark_silo_missing_tenant:{:?}",
                config.role
            ));
        }
        if config.namespace.is_empty() {
            errors.push(format!(
                "configured_benchmark_silo_missing_namespace:{:?}",
                config.role
            ));
        }
    }

    let configured_count = configs.iter().filter(|config| config.configured).count();
    if configured_count < 3 {
        warnings.push("deterministic_report_file_mode".into());
    }
    let url_values = configured_url_values(configs, &env_lookup);
    let neutral_governed_separated =
        validate_url_separation(&url_values, &mut errors, &mut warnings, configs);

    BenchmarkIsolationValidation {
        valid: errors.is_empty(),
        errors,
        warnings,
        main_db_protected,
        neutral_governed_separated,
        runner_silo_assignments,
    }
}

fn configured_url_values<F>(
    configs: &[DatabaseSiloConfig],
    env_lookup: &F,
) -> BTreeMap<DatabaseSiloRole, String>
where
    F: Fn(&str) -> Option<String>,
{
    configs
        .iter()
        .filter_map(|config| {
            env_lookup(config.database_url_env.as_str()).map(|url| (config.role, url))
        })
        .collect()
}

fn validate_url_separation(
    url_values: &BTreeMap<DatabaseSiloRole, String>,
    errors: &mut Vec<String>,
    warnings: &mut Vec<String>,
    configs: &[DatabaseSiloConfig],
) -> bool {
    // Compare canonicalized URLs, not raw strings: two URLs that differ only by
    // port, case, scheme alias, or query-param order point at the SAME database
    // and must be reported as NOT separated. An unparseable URL is a hard error,
    // never a soft warning, so a malformed silo URL cannot pass isolation.
    let normalize = |role: &DatabaseSiloRole, errors: &mut Vec<String>| -> Option<String> {
        let raw = url_values.get(role)?;
        match normalize_database_url(raw) {
            Some(canonical) => Some(canonical),
            None => {
                errors.push(format!("benchmark_silo_url_unparseable:{role:?}"));
                None
            }
        }
    };
    let neutral = normalize(&DatabaseSiloRole::NeutralBenchmark, errors);
    let governed = normalize(&DatabaseSiloRole::GovernedBenchmark, errors);
    let main = normalize(&DatabaseSiloRole::Main, errors);

    let mut separated = match (
        url_values.get(&DatabaseSiloRole::NeutralBenchmark),
        url_values.get(&DatabaseSiloRole::GovernedBenchmark),
    ) {
        (Some(_), Some(_)) => match (&neutral, &governed) {
            (Some(neutral_url), Some(governed_url)) => {
                if neutral_url == governed_url {
                    errors.push("neutral_and_governed_database_urls_match".into());
                    false
                } else {
                    true
                }
            }
            // At least one of the two configured URLs failed to parse; the hard
            // error above already records it, and separation is unproven.
            _ => false,
        },
        _ => {
            warnings.push("neutral_governed_separation_unobservable".into());
            false
        }
    };
    let benchmark_writes_enabled = configs
        .iter()
        .any(|config| config.role != DatabaseSiloRole::Main && config.allow_benchmark_writes);
    if benchmark_writes_enabled {
        match url_values.get(&DatabaseSiloRole::Main) {
            Some(_) => {
                if let Some(main_url) = &main {
                    if neutral
                        .as_ref()
                        .is_some_and(|neutral_url| neutral_url == main_url)
                        || governed
                            .as_ref()
                            .is_some_and(|governed_url| governed_url == main_url)
                    {
                        separated = false;
                        errors.push("main_and_benchmark_database_urls_match".into());
                    }
                }
            }
            None => warnings.push("main_benchmark_separation_unobservable".into()),
        }
    }
    separated
}

/// Canonicalize a database URL so equivalent URLs compare equal.
///
/// No `url` crate is available in this crate's dependencies, so this does a
/// careful manual canonicalization of the parts that determine which database an
/// URL points at: the scheme (with `postgres`/`postgresql` treated as aliases),
/// the lowercased host, the explicit port (default-filled for known schemes), the
/// path / database name, and the query parameters in sorted order. Returns `None`
/// when the URL cannot be parsed, which the caller treats as a hard error.
fn normalize_database_url(raw: &str) -> Option<String> {
    let trimmed = raw.trim();
    let (scheme_raw, rest) = trimmed.split_once("://")?;
    if scheme_raw.is_empty() || rest.is_empty() {
        return None;
    }
    let scheme = match scheme_raw.to_ascii_lowercase().as_str() {
        "postgres" | "postgresql" => "postgresql".to_owned(),
        other => other.to_owned(),
    };

    // Split authority (userinfo@host:port) from path?query.
    let (authority, path_and_query) = match rest.split_once('/') {
        Some((authority, tail)) => (authority, format!("/{tail}")),
        None => (rest, String::new()),
    };
    if authority.is_empty() {
        return None;
    }

    // Drop userinfo: credentials do not change which database the URL targets.
    let host_port = authority.rsplit_once('@').map_or(authority, |(_, hp)| hp);
    let (host, port) = match host_port.rsplit_once(':') {
        // A ':' inside brackets is an IPv6 host, not a port separator.
        Some((host, port)) if !host.ends_with(']') => {
            let port = port.parse::<u16>().ok()?;
            (host, Some(port))
        }
        _ => (host_port, None),
    };
    if host.is_empty() {
        return None;
    }
    let host = host.to_ascii_lowercase();
    let port = port.unwrap_or_else(|| default_port_for_scheme(&scheme));

    // Split path from query and canonicalize the query param order.
    let (path, query) = match path_and_query.split_once('?') {
        Some((path, query)) => (path.to_owned(), query.to_owned()),
        None => (path_and_query, String::new()),
    };
    let mut params: Vec<&str> = query.split('&').filter(|param| !param.is_empty()).collect();
    params.sort_unstable();
    let canonical_query = params.join("&");

    Some(format!("{scheme}://{host}:{port}{path}?{canonical_query}"))
}

fn default_port_for_scheme(scheme: &str) -> u16 {
    match scheme {
        "postgresql" => 5432,
        "mysql" => 3306,
        "redis" => 6379,
        "mongodb" => 27017,
        _ => 0,
    }
}

#[must_use]
pub fn build_contamination_check(
    checked_at_commit_or_run_id: String,
    evidence: &ContaminationEvidence,
) -> SiloContaminationCheck {
    let main_db_used_for_benchmark =
        contamination_status(evidence.demo_mode, evidence.main_db_used_for_benchmark);
    let neutral_used_graph_routing =
        contamination_status(evidence.demo_mode, evidence.neutral_used_graph_routing);
    let governed_used_neutral_state =
        contamination_status(evidence.demo_mode, evidence.governed_used_neutral_state);
    let cross_silo_route_memory_reuse =
        contamination_status(evidence.demo_mode, evidence.cross_silo_route_memory_reuse);
    let cross_silo_context_packet_reuse =
        contamination_status(evidence.demo_mode, evidence.cross_silo_context_packet_reuse);
    let cross_silo_receipt_reuse =
        contamination_status(evidence.demo_mode, evidence.cross_silo_receipt_reuse);
    let statuses = [
        main_db_used_for_benchmark,
        neutral_used_graph_routing,
        governed_used_neutral_state,
        cross_silo_route_memory_reuse,
        cross_silo_context_packet_reuse,
        cross_silo_receipt_reuse,
    ];
    let contamination_detected = statuses.contains(&SiloContaminationStatus::Contaminated);
    let findings = if evidence.demo_mode {
        vec!["not_configured_demo_mode".into()]
    } else {
        statuses
            .contains(&SiloContaminationStatus::UnknownNotObservable)
            .then(|| "one_or_more_fields_unknown_not_observable".into())
            .into_iter()
            .collect()
    };
    SiloContaminationCheck {
        checked_at_commit_or_run_id,
        main_db_used_for_benchmark,
        neutral_used_graph_routing,
        governed_used_neutral_state,
        cross_silo_route_memory_reuse,
        cross_silo_context_packet_reuse,
        cross_silo_receipt_reuse,
        contamination_detected,
        findings,
    }
}

fn contamination_status(demo_mode: bool, evidence: Option<bool>) -> SiloContaminationStatus {
    if demo_mode {
        return SiloContaminationStatus::NotConfiguredDemoMode;
    }
    match evidence {
        Some(false) => SiloContaminationStatus::Clean,
        Some(true) => SiloContaminationStatus::Contaminated,
        None => SiloContaminationStatus::UnknownNotObservable,
    }
}

#[must_use]
pub fn unobservable_contamination_evidence(demo_mode: bool) -> ContaminationEvidence {
    ContaminationEvidence {
        demo_mode,
        main_db_used_for_benchmark: None,
        neutral_used_graph_routing: None,
        governed_used_neutral_state: None,
        cross_silo_route_memory_reuse: None,
        cross_silo_context_packet_reuse: None,
        cross_silo_receipt_reuse: None,
    }
}

pub fn build_benchmark_isolation_report<F>(
    diagnostic_report_dir: &Path,
    source_commit_or_run_id: String,
    env_lookup: F,
) -> BenchmarkIsolationReport
where
    F: Fn(&str) -> Option<String>,
{
    let configs = default_silo_configs_from_env(&env_lookup);
    let validation =
        validate_benchmark_silo_config(&configs, &all_benchmark_run_modes(), &env_lookup);
    let demo_mode = configs.iter().any(|config| !config.configured);
    let contamination_check = build_contamination_check(
        source_commit_or_run_id.clone(),
        &unobservable_contamination_evidence(demo_mode),
    );
    let source_artifacts = source_artifact_paths();
    let hashes = artifact_hashes(diagnostic_report_dir, &source_artifacts);
    let fixture_id = fixture_id_from_summary(diagnostic_report_dir)
        .unwrap_or_else(|| "missing_diagnostic_fixture".into());
    let report_id_material =
        format!("{REPORT_SCHEMA_VERSION}:{source_commit_or_run_id}:{fixture_id}");
    let report_id = Hash256::digest(report_id_material.as_bytes()).to_string();
    BenchmarkIsolationReport {
        schema_version: REPORT_SCHEMA_VERSION.into(),
        report_id,
        fixture_id,
        main_silo_status: config_by_role(&configs, DatabaseSiloRole::Main),
        neutral_silo_status: config_by_role(&configs, DatabaseSiloRole::NeutralBenchmark),
        governed_silo_status: config_by_role(&configs, DatabaseSiloRole::GovernedBenchmark),
        validation,
        contamination_check,
        diagnostic_report_ids: hashes
            .values()
            .filter_map(|hash| hash.hash.clone())
            .collect(),
        generated_at_harness_run_id: source_commit_or_run_id.clone(),
        diagnostic_summary_hash: hash_by_name(&hashes, SUMMARY_JSON),
        per_task_results_hash: hash_by_name(&hashes, PER_TASK_RESULTS_JSON),
        cost_breakdown_hash: hash_by_name(&hashes, COST_BREAKDOWN_JSON),
        quality_breakdown_hash: hash_by_name(&hashes, QUALITY_BREAKDOWN_JSON),
        latency_breakdown_hash: hash_by_name(&hashes, LATENCY_BREAKDOWN_JSON),
        recommendations_hash: hash_by_name(&hashes, RECOMMENDATIONS_MD),
        source_artifact_paths: source_artifacts,
        source_commit_or_run_id,
    }
}

fn config_by_role(configs: &[DatabaseSiloConfig], role: DatabaseSiloRole) -> DatabaseSiloConfig {
    configs
        .iter()
        .find(|config| config.role == role)
        .cloned()
        .unwrap_or(DatabaseSiloConfig {
            role,
            purpose: match role {
                DatabaseSiloRole::Main => DatabaseSiloPurpose::ProductionMemory,
                DatabaseSiloRole::NeutralBenchmark => DatabaseSiloPurpose::NeutralBaseline,
                DatabaseSiloRole::GovernedBenchmark => DatabaseSiloPurpose::GovernedDagBenchmark,
            },
            database_url_env: String::new(),
            configured: false,
            tenant_id: String::new(),
            namespace: String::new(),
            read_only: true,
            allow_benchmark_writes: false,
            allow_synthetic_data: false,
            allow_graph_routing: false,
            allow_route_memory: false,
            allow_canonical_graph: false,
            allow_context_packet_graph: false,
            allow_private_payload_reads: false,
        })
}

fn source_artifact_paths() -> Vec<String> {
    [
        SUMMARY_JSON,
        PER_TASK_RESULTS_JSON,
        COST_BREAKDOWN_JSON,
        QUALITY_BREAKDOWN_JSON,
        LATENCY_BREAKDOWN_JSON,
        RECOMMENDATIONS_MD,
    ]
    .into_iter()
    .map(|artifact| format!("{E2E_DIAGNOSTIC_DIR}/{artifact}"))
    .collect()
}

fn artifact_hashes(
    diagnostic_report_dir: &Path,
    source_artifacts: &[String],
) -> BTreeMap<String, ArtifactHashStatus> {
    source_artifacts
        .iter()
        .map(|source_path| {
            let name = source_path
                .rsplit('/')
                .next()
                .unwrap_or(source_path.as_str());
            let path = diagnostic_report_dir.join(name);
            let status = match fs::read(&path) {
                Ok(bytes) => ArtifactHashStatus {
                    path: source_path.clone(),
                    status: "available".into(),
                    hash: Some(Hash256::digest(&bytes).to_string()),
                },
                Err(_) => ArtifactHashStatus {
                    path: source_path.clone(),
                    status: "missing".into(),
                    hash: None,
                },
            };
            (name.into(), status)
        })
        .collect()
}

fn hash_by_name(hashes: &BTreeMap<String, ArtifactHashStatus>, name: &str) -> ArtifactHashStatus {
    hashes.get(name).cloned().unwrap_or(ArtifactHashStatus {
        path: format!("{E2E_DIAGNOSTIC_DIR}/{name}"),
        status: "missing".into(),
        hash: None,
    })
}

fn fixture_id_from_summary(diagnostic_report_dir: &Path) -> Option<String> {
    let text = fs::read_to_string(diagnostic_report_dir.join(SUMMARY_JSON)).ok()?;
    let value = serde_json::from_str::<Value>(&text).ok()?;
    value
        .get("fixture_ids")?
        .as_array()?
        .first()?
        .as_str()
        .map(str::to_owned)
}

pub fn render_benchmark_isolation_artifacts(
    report: &BenchmarkIsolationReport,
) -> std::result::Result<BenchmarkIsolationArtifacts, BenchmarkIsolationError> {
    Ok(BenchmarkIsolationArtifacts {
        silo_config_summary_json: json_string(&vec![
            &report.main_silo_status,
            &report.neutral_silo_status,
            &report.governed_silo_status,
        ])?,
        silo_validation_report_json: json_string(&report.validation)?,
        silo_contamination_report_json: json_string(&report.contamination_check)?,
        runner_silo_assignment_json: json_string(&report.validation.runner_silo_assignments)?,
        benchmark_isolation_summary_md: benchmark_isolation_markdown(report),
    })
}

pub fn write_benchmark_isolation_artifacts_for_report_dir<F>(
    diagnostic_report_dir: &Path,
    source_commit_or_run_id: String,
    env_lookup: F,
) -> std::result::Result<BenchmarkIsolationArtifacts, BenchmarkIsolationError>
where
    F: Fn(&str) -> Option<String>,
{
    let report = build_benchmark_isolation_report(
        diagnostic_report_dir,
        source_commit_or_run_id,
        env_lookup,
    );
    let artifacts = render_benchmark_isolation_artifacts(&report)?;
    write_artifact(
        SILO_CONFIG_SUMMARY_JSON,
        &artifacts.silo_config_summary_json,
    )?;
    write_artifact(
        SILO_VALIDATION_REPORT_JSON,
        &artifacts.silo_validation_report_json,
    )?;
    write_artifact(
        SILO_CONTAMINATION_REPORT_JSON,
        &artifacts.silo_contamination_report_json,
    )?;
    write_artifact(
        RUNNER_SILO_ASSIGNMENT_JSON,
        &artifacts.runner_silo_assignment_json,
    )?;
    write_artifact(
        BENCHMARK_ISOLATION_SUMMARY_MD,
        &artifacts.benchmark_isolation_summary_md,
    )?;
    Ok(artifacts)
}

pub fn write_default_benchmark_isolation_artifacts()
-> std::result::Result<BenchmarkIsolationArtifacts, BenchmarkIsolationError> {
    let report_dir = workspace_artifact_path(E2E_DIAGNOSTIC_DIR);
    write_benchmark_isolation_artifacts_for_report_dir(
        &report_dir,
        "dagdb_benchmark_isolation_report_file_mode_v1".into(),
        |key| std::env::var(key).ok(),
    )
}

fn json_string<T: Serialize>(value: &T) -> std::result::Result<String, BenchmarkIsolationError> {
    let mut json = serde_json::to_string_pretty(value)
        .map_err(|source| BenchmarkIsolationError::Json { source })?;
    json.push('\n');
    Ok(json)
}

fn benchmark_isolation_markdown(report: &BenchmarkIsolationReport) -> String {
    let mut output = String::new();
    output.push_str("# EXOCHAIN DAG DB Benchmark Isolation Summary\n\n");
    output.push_str(&format!("- schema_version: {}\n", report.schema_version));
    output.push_str(&format!("- report_id: {}\n", report.report_id));
    output.push_str(&format!("- fixture_id: {}\n", report.fixture_id));
    output.push_str(&format!(
        "- source_commit_or_run_id: {}\n",
        report.source_commit_or_run_id
    ));
    output.push_str(&format!("- valid: {}\n", report.validation.valid));
    output.push_str(&format!(
        "- main_db_protected: {}\n",
        report.validation.main_db_protected
    ));
    output.push_str(&format!(
        "- neutral_governed_separated: {}\n",
        report.validation.neutral_governed_separated
    ));
    output.push_str(&format!(
        "- contamination_detected: {}\n\n",
        report.contamination_check.contamination_detected
    ));
    output.push_str("## Silo Roles\n\n");
    for config in [
        &report.main_silo_status,
        &report.neutral_silo_status,
        &report.governed_silo_status,
    ] {
        output.push_str(&format!(
            "- {:?}: purpose={:?}, configured={}, env_var=not_serialized, graph_routing={}, route_memory={}, canonical_graph={}, context_packet_graph={}, benchmark_writes={}, synthetic_data={}\n",
            config.role,
            config.purpose,
            config.configured,
            config.allow_graph_routing,
            config.allow_route_memory,
            config.allow_canonical_graph,
            config.allow_context_packet_graph,
            config.allow_benchmark_writes,
            config.allow_synthetic_data
        ));
    }
    output.push_str("\n## Validation Errors\n\n");
    if report.validation.errors.is_empty() {
        output.push_str("- none\n");
    } else {
        for error in &report.validation.errors {
            output.push_str(&format!("- {error}\n"));
        }
    }
    output.push_str("\n## Validation Warnings\n\n");
    if report.validation.warnings.is_empty() {
        output.push_str("- none\n");
    } else {
        for warning in &report.validation.warnings {
            output.push_str(&format!("- {warning}\n"));
        }
    }
    output.push_str("\n## Source Artifact Hashes\n\n");
    for artifact in [
        &report.diagnostic_summary_hash,
        &report.per_task_results_hash,
        &report.cost_breakdown_hash,
        &report.quality_breakdown_hash,
        &report.latency_breakdown_hash,
        &report.recommendations_hash,
    ] {
        output.push_str(&format!(
            "- {}: status={}, hash={}\n",
            artifact.path,
            artifact.status,
            artifact.hash.as_deref().unwrap_or("missing")
        ));
    }
    output
}

fn write_artifact(path: &str, contents: &str) -> std::result::Result<(), BenchmarkIsolationError> {
    let artifact_path = workspace_artifact_path(path);
    if let Some(parent) = artifact_path.parent() {
        fs::create_dir_all(parent).map_err(|source| BenchmarkIsolationError::Io {
            path: parent.display().to_string(),
            source,
        })?;
    }
    fs::write(&artifact_path, contents).map_err(|source| BenchmarkIsolationError::Io {
        path: path.into(),
        source,
    })
}

fn workspace_artifact_path(path: &str) -> PathBuf {
    let crate_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
    crate_dir
        .parent()
        .and_then(Path::parent)
        .unwrap_or(crate_dir)
        .join(path)
}