hawdb-bootstrap 0.21.0

An embedded Rust graph database for local data-plane workloads
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
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Developer-only verification for portable bootstrap staging artifacts.
//!
//! This module validates only staged files and typed bootstrap contracts. It
//! never opens an embedded database or lets a staged artifact relax a host
//! resource limit.

use crate::{
    hawdb_lightning_artifact_summary, hawdb_lightning_graph_stream_validation_json,
    hawdb_lightning_relational_stream_validation_json, validate_hawdb_lightning_graph_stream,
    validate_hawdb_lightning_relational_stream, HAWDB_LIGHTNING_BOOTSTRAP_PROTOCOL_VERSION,
    HAWDB_LIGHTNING_RELATIONAL_STREAM_FORMAT_VERSION,
    HAWDB_LIGHTNING_STAGING_CATALOG_PROTOCOL_VERSION,
};
use hawdb_core::{HawDBError, Result};
use hawdb_integrity::checksum_u64;
use std::fs;
use std::path::Path;

pub fn verify_hawdb_lightning_staging_catalog(
    staging_dir: impl AsRef<Path>,
) -> Result<serde_json::Value> {
    let staging_dir = staging_dir.as_ref();
    let catalog_path = staging_dir.join("hawdb_lightning_staging_catalog.json");
    let catalog = read_json_file(&catalog_path)?;
    let mut errors = Vec::new();
    let mut artifact_errors = Vec::new();
    let mut manifest_errors = Vec::new();
    let mut graph_stream_errors = Vec::new();
    let mut relational_stream_errors = Vec::new();
    let mut bundle_errors = Vec::new();
    let mut catalog_errors = Vec::new();
    let mut artifact_reports = Vec::new();
    let mut manifest = None;
    let mut graph_stream = None;
    let mut relational_stream = None;
    let mut bundle = None;

    let catalog_protocol_matches = catalog.get("protocol").and_then(serde_json::Value::as_str)
        == Some("hawdb-lightning-staging-catalog");
    if !catalog_protocol_matches {
        record_error(
            &mut errors,
            &mut catalog_errors,
            "staging catalog protocol mismatch",
        );
    }
    let catalog_protocol_version_matches = catalog
        .get("protocol_version")
        .and_then(serde_json::Value::as_u64)
        == Some(HAWDB_LIGHTNING_STAGING_CATALOG_PROTOCOL_VERSION);
    if !catalog_protocol_version_matches {
        record_error(
            &mut errors,
            &mut catalog_errors,
            "staging catalog protocol version mismatch",
        );
    }

    let artifacts = catalog
        .get("artifacts")
        .and_then(serde_json::Value::as_array)
        .cloned()
        .unwrap_or_else(|| {
            record_error(
                &mut errors,
                &mut catalog_errors,
                "staging catalog missing artifacts array",
            );
            Vec::new()
        });
    for artifact in &artifacts {
        let kind = artifact
            .get("kind")
            .and_then(serde_json::Value::as_str)
            .unwrap_or("unknown");
        let Some(path) = artifact.get("path").and_then(serde_json::Value::as_str) else {
            record_error(
                &mut errors,
                &mut artifact_errors,
                format!("staging artifact {kind} missing path"),
            );
            continue;
        };
        if path.contains('/') || path.contains('\\') {
            record_error(
                &mut errors,
                &mut artifact_errors,
                format!("staging artifact {kind} uses non-local path {path}"),
            );
            continue;
        }
        let artifact_path = staging_dir.join(path);
        let expected_byte_len = artifact.get("byte_len").and_then(serde_json::Value::as_u64);
        let expected_checksum = artifact.get("checksum").and_then(serde_json::Value::as_u64);
        match fs::read(&artifact_path) {
            Ok(bytes) => {
                let actual_byte_len = bytes.len() as u64;
                let actual_checksum = checksum_u64(&bytes);
                let byte_len_matches = expected_byte_len == Some(actual_byte_len);
                let checksum_matches = expected_checksum == Some(actual_checksum);
                if !byte_len_matches {
                    record_error(
                        &mut errors,
                        &mut artifact_errors,
                        format!("staging artifact {kind} byte length mismatch"),
                    );
                }
                if !checksum_matches {
                    record_error(
                        &mut errors,
                        &mut artifact_errors,
                        format!("staging artifact {kind} checksum mismatch"),
                    );
                }
                match kind {
                    "manifest" => match serde_json::from_slice::<serde_json::Value>(&bytes) {
                        Ok(value) => manifest = Some(value),
                        Err(error) => record_error(
                            &mut errors,
                            &mut manifest_errors,
                            format!("invalid manifest artifact JSON: {error}"),
                        ),
                    },
                    "graph_stream" => match String::from_utf8(bytes.clone()) {
                        Ok(value) => graph_stream = Some(value),
                        Err(error) => record_error(
                            &mut errors,
                            &mut graph_stream_errors,
                            format!("invalid GraphStream UTF-8: {error}"),
                        ),
                    },
                    "relational_stream" => relational_stream = Some(bytes),
                    "bundle" => match serde_json::from_slice::<serde_json::Value>(&bytes) {
                        Ok(value) => bundle = Some(value),
                        Err(error) => record_error(
                            &mut errors,
                            &mut bundle_errors,
                            format!("invalid bundle artifact JSON: {error}"),
                        ),
                    },
                    _ => record_error(
                        &mut errors,
                        &mut artifact_errors,
                        format!("unknown staging artifact kind {kind}"),
                    ),
                }
                artifact_reports.push(serde_json::json!({
                    "kind": kind,
                    "path": path,
                    "expected_byte_len": expected_byte_len,
                    "actual_byte_len": actual_byte_len,
                    "byte_len_matches": byte_len_matches,
                    "expected_checksum": expected_checksum,
                    "actual_checksum": actual_checksum,
                    "checksum_matches": checksum_matches,
                }));
            }
            Err(error) => {
                record_error(
                    &mut errors,
                    &mut artifact_errors,
                    format!("missing staging artifact {kind} at {path}: {error}"),
                );
                artifact_reports.push(serde_json::json!({
                    "kind": kind,
                    "path": path,
                    "expected_byte_len": expected_byte_len,
                    "actual_byte_len": serde_json::Value::Null,
                    "byte_len_matches": false,
                    "expected_checksum": expected_checksum,
                    "actual_checksum": serde_json::Value::Null,
                    "checksum_matches": false,
                }));
            }
        }
    }

    let manifest_protocol_version_matches = manifest
        .as_ref()
        .and_then(|manifest| manifest.get("protocol_version"))
        .and_then(serde_json::Value::as_u64)
        == Some(HAWDB_LIGHTNING_BOOTSTRAP_PROTOCOL_VERSION);
    if !manifest_protocol_version_matches {
        record_error(
            &mut errors,
            &mut manifest_errors,
            "manifest protocol version mismatch",
        );
    }

    let graph_stream_validation = graph_stream
        .as_ref()
        .map(|encoded| validate_hawdb_lightning_graph_stream(encoded, None));
    let graph_stream_validation_json = graph_stream_validation
        .as_ref()
        .map(hawdb_lightning_graph_stream_validation_json);
    let relational_stream_validation = relational_stream
        .as_ref()
        .map(|encoded| validate_hawdb_lightning_relational_stream(encoded, None));
    let mut relational_stream_validation_json = relational_stream_validation
        .as_ref()
        .map(hawdb_lightning_relational_stream_validation_json);
    if let (Some(validation), Some(manifest)) = (
        relational_stream_validation_json.as_mut(),
        manifest.as_ref(),
    ) {
        validation["expected_stream_checksum"] = manifest["relational_stream_checksum"].clone();
    }
    let manifest_matches_graph_stream = match (&manifest, &graph_stream, &graph_stream_validation) {
        (Some(manifest), Some(graph_stream), Some(validation)) => {
            let matches = manifest
                .get("graph_stream_checksum")
                .and_then(serde_json::Value::as_u64)
                == validation.expected_stream_checksum
                && manifest
                    .get("graph_stream_byte_len")
                    .and_then(serde_json::Value::as_u64)
                    == Some(graph_stream.len() as u64)
                && manifest
                    .get("graph_commit_epoch")
                    .and_then(serde_json::Value::as_u64)
                    == validation.graph_commit_epoch
                && manifest
                    .get("logical_checksum")
                    .and_then(serde_json::Value::as_u64)
                    == validation.logical_checksum
                && manifest
                    .get("node_count")
                    .and_then(serde_json::Value::as_u64)
                    == Some(validation.node_count as u64)
                && manifest
                    .get("relationship_count")
                    .and_then(serde_json::Value::as_u64)
                    == Some(validation.relationship_count as u64);
            if !matches {
                record_error(
                    &mut errors,
                    &mut manifest_errors,
                    "manifest does not match GraphStream artifact",
                );
            }
            matches
        }
        _ => {
            record_error(
                &mut errors,
                &mut manifest_errors,
                "manifest or GraphStream artifact missing",
            );
            false
        }
    };
    let manifest_matches_relational_stream =
        match (&manifest, &relational_stream, &relational_stream_validation) {
            (Some(manifest), Some(relational_stream), Some(validation)) => {
                let matches = manifest
                    .get("relational_stream_format_version")
                    .and_then(serde_json::Value::as_u64)
                    == Some(HAWDB_LIGHTNING_RELATIONAL_STREAM_FORMAT_VERSION)
                    && manifest
                        .get("relational_stream_checksum")
                        .and_then(serde_json::Value::as_u64)
                        == Some(validation.actual_stream_checksum)
                    && manifest
                        .get("relational_stream_byte_len")
                        .and_then(serde_json::Value::as_u64)
                        == Some(relational_stream.len() as u64)
                    && manifest
                        .get("database_commit_epoch")
                        .and_then(serde_json::Value::as_u64)
                        == validation.database_commit_epoch
                    && manifest
                        .get("relational_table_count")
                        .and_then(serde_json::Value::as_u64)
                        == Some(validation.table_count as u64)
                    && manifest
                        .get("relational_row_count")
                        .and_then(serde_json::Value::as_u64)
                        == Some(validation.row_count as u64)
                    && manifest
                        .get("relational_overflow_segment_count")
                        .and_then(serde_json::Value::as_u64)
                        == Some(validation.overflow_segment_count as u64);
                if !matches {
                    record_error(
                        &mut errors,
                        &mut manifest_errors,
                        "manifest does not match relational stream artifact",
                    );
                }
                matches
            }
            _ => {
                record_error(
                    &mut errors,
                    &mut manifest_errors,
                    "manifest or relational stream artifact missing",
                );
                false
            }
        };
    let bundle_matches_artifacts = match (
        &bundle,
        &manifest,
        &graph_stream_validation_json,
        &relational_stream_validation_json,
    ) {
        (Some(bundle), Some(manifest), Some(graph_validation), Some(relational_validation)) => {
            let matches = bundle.get("manifest") == Some(manifest)
                && bundle.get("graph_stream_validation") == Some(graph_validation)
                && bundle.get("relational_stream_validation") == Some(relational_validation)
                && bundle.get("export_gate") == catalog.get("export_gate");
            if !matches {
                record_error(
                    &mut errors,
                    &mut bundle_errors,
                    "bundle does not match staged manifest, stream validations, or catalog gate",
                );
            }
            matches
        }
        _ => {
            record_error(&mut errors, &mut bundle_errors, "bundle artifact missing");
            false
        }
    };
    let storage_recovery_evidence = match (&bundle, &manifest) {
        (Some(bundle), Some(manifest)) => verify_bundle_storage_recovery_evidence(
            bundle,
            manifest,
            &mut errors,
            &mut bundle_errors,
        ),
        _ => StorageRecoveryEvidenceVerification::default(),
    };
    let artifact_integrity = artifact_reports.iter().all(|report| {
        report
            .get("byte_len_matches")
            .and_then(serde_json::Value::as_bool)
            == Some(true)
            && report
                .get("checksum_matches")
                .and_then(serde_json::Value::as_bool)
                == Some(true)
    });
    let artifact_summary = hawdb_lightning_artifact_summary(&artifact_reports, "actual_byte_len");
    let catalog_state_ready = catalog
        .get("stage_state")
        .and_then(serde_json::Value::as_str)
        == Some("READY")
        && catalog
            .get("export_gate")
            .and_then(|gate| gate.get("decision"))
            .and_then(serde_json::Value::as_str)
            == Some("ready");
    if !catalog_state_ready {
        record_error(
            &mut errors,
            &mut catalog_errors,
            "staging catalog is not READY",
        );
    }
    let graph_stream_valid = graph_stream_validation
        .as_ref()
        .is_some_and(|validation| validation.is_valid);
    if !graph_stream_valid {
        record_error(
            &mut errors,
            &mut graph_stream_errors,
            "GraphStream validation failed",
        );
    }
    let relational_stream_valid = relational_stream_validation
        .as_ref()
        .is_some_and(|validation| validation.is_valid);
    if !relational_stream_valid {
        record_error(
            &mut errors,
            &mut relational_stream_errors,
            "relational stream validation failed",
        );
    }
    let decision = if errors.is_empty()
        && artifact_integrity
        && catalog_protocol_matches
        && catalog_protocol_version_matches
        && manifest_protocol_version_matches
        && manifest_matches_graph_stream
        && manifest_matches_relational_stream
        && bundle_matches_artifacts
        && storage_recovery_evidence.valid
        && catalog_state_ready
        && graph_stream_valid
        && relational_stream_valid
    {
        "ready"
    } else {
        "blocked"
    };
    Ok(serde_json::json!({
        "protocol": "hawdb-lightning-staging-verification",
        "protocol_version": HAWDB_LIGHTNING_STAGING_CATALOG_PROTOCOL_VERSION,
        "catalog_path": "hawdb_lightning_staging_catalog.json",
        "artifact_integrity": artifact_integrity,
        "catalog_protocol_matches": catalog_protocol_matches,
        "catalog_protocol_version_matches": catalog_protocol_version_matches,
        "manifest_protocol_version_matches": manifest_protocol_version_matches,
        "manifest_matches_graph_stream": manifest_matches_graph_stream,
        "manifest_matches_relational_stream": manifest_matches_relational_stream,
        "bundle_matches_artifacts": bundle_matches_artifacts,
        "storage_recovery_evidence": {
            "present": storage_recovery_evidence.present,
            "valid": storage_recovery_evidence.valid,
            "protocol_matches": storage_recovery_evidence.protocol_matches,
            "storage_version_present": storage_recovery_evidence.storage_version_present,
            "recovered_commit_epoch_matches_manifest": storage_recovery_evidence.recovered_commit_epoch_matches_manifest,
        },
        "catalog_state_ready": catalog_state_ready,
        "graph_stream_validation": graph_stream_validation_json,
        "relational_stream_validation": relational_stream_validation_json,
        "artifact_summary": artifact_summary,
        "artifacts": artifact_reports,
        "validation_gate": {
            "decision": decision,
            "artifact_errors": artifact_errors.len(),
            "manifest_errors": manifest_errors.len(),
            "graph_stream_errors": graph_stream_errors.len(),
            "relational_stream_errors": relational_stream_errors.len(),
            "bundle_errors": bundle_errors.len(),
            "catalog_errors": catalog_errors.len(),
            "artifact_error_messages": artifact_errors,
            "manifest_error_messages": manifest_errors,
            "graph_stream_error_messages": graph_stream_errors,
            "relational_stream_error_messages": relational_stream_errors,
            "bundle_error_messages": bundle_errors,
            "catalog_error_messages": catalog_errors,
            "errors": errors,
        },
    }))
}

fn read_json_file(path: &Path) -> Result<serde_json::Value> {
    let bytes = fs::read(path)?;
    serde_json::from_slice(&bytes)
        .map_err(|_| HawDBError::Execution("invalid JSON file: invalid_json".to_string()))
}

fn record_error(errors: &mut Vec<String>, group: &mut Vec<String>, message: impl Into<String>) {
    let message = message.into();
    errors.push(message.clone());
    group.push(message);
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
struct StorageRecoveryEvidenceVerification {
    present: bool,
    valid: bool,
    protocol_matches: bool,
    storage_version_present: bool,
    recovered_commit_epoch_matches_manifest: bool,
}

fn verify_bundle_storage_recovery_evidence(
    bundle: &serde_json::Value,
    manifest: &serde_json::Value,
    errors: &mut Vec<String>,
    bundle_errors: &mut Vec<String>,
) -> StorageRecoveryEvidenceVerification {
    let Some(storage_recovery) = bundle.get("storage_recovery") else {
        return StorageRecoveryEvidenceVerification {
            present: false,
            valid: true,
            protocol_matches: false,
            storage_version_present: false,
            recovered_commit_epoch_matches_manifest: false,
        };
    };
    let protocol_matches = storage_recovery
        .get("protocol")
        .and_then(serde_json::Value::as_str)
        == Some("hawdb-storage-recovery-report");
    if !protocol_matches {
        record_error(
            errors,
            bundle_errors,
            "bundle storage_recovery protocol mismatch",
        );
    }
    let storage_version_present = storage_recovery
        .get("storage_version")
        .and_then(serde_json::Value::as_str)
        .is_some_and(|version| !version.is_empty());
    if !storage_version_present {
        record_error(
            errors,
            bundle_errors,
            "bundle storage_recovery missing storage_version",
        );
    }
    let recovered_commit_epoch_matches_manifest = storage_recovery
        .get("recovered_commit_epoch")
        .and_then(serde_json::Value::as_u64)
        == manifest
            .get("database_commit_epoch")
            .and_then(serde_json::Value::as_u64);
    if !recovered_commit_epoch_matches_manifest {
        record_error(
            errors,
            bundle_errors,
            "bundle storage_recovery recovered commit epoch does not match manifest database epoch",
        );
    }
    StorageRecoveryEvidenceVerification {
        present: true,
        valid: protocol_matches
            && storage_version_present
            && recovered_commit_epoch_matches_manifest,
        protocol_matches,
        storage_version_present,
        recovered_commit_epoch_matches_manifest,
    }
}

pub fn verify_hawdb_lightning_published_manifest(
    staging_dir: impl AsRef<Path>,
    publish_dir: impl AsRef<Path>,
) -> Result<serde_json::Value> {
    let staging_dir = staging_dir.as_ref();
    let publish_dir = publish_dir.as_ref();
    let published_path = publish_dir.join("hawdb_lightning_published_manifest.json");
    let published = read_json_file(&published_path)?;
    let staging_verification = verify_hawdb_lightning_staging_catalog(staging_dir)?;
    let catalog_path = staging_dir.join("hawdb_lightning_staging_catalog.json");
    let catalog_bytes = fs::read(&catalog_path)?;
    let actual_catalog_checksum = checksum_u64(&catalog_bytes);
    let actual_catalog_byte_len = catalog_bytes.len() as u64;
    let expected_catalog_checksum = published
        .get("staging_catalog")
        .and_then(|catalog| catalog.get("checksum"))
        .and_then(serde_json::Value::as_u64);
    let expected_catalog_byte_len = published
        .get("staging_catalog")
        .and_then(|catalog| catalog.get("byte_len"))
        .and_then(serde_json::Value::as_u64);
    let catalog_checksum_matches = expected_catalog_checksum == Some(actual_catalog_checksum);
    let catalog_byte_len_matches = expected_catalog_byte_len == Some(actual_catalog_byte_len);
    let pointer_state_published =
        published.get("state").and_then(serde_json::Value::as_str) == Some("PUBLISHED");
    let staging_ready = staging_verification
        .get("validation_gate")
        .and_then(|gate| gate.get("decision"))
        .and_then(serde_json::Value::as_str)
        == Some("ready");
    let storage_recovery_evidence = staging_verification
        .get("storage_recovery_evidence")
        .cloned()
        .unwrap_or_else(default_storage_recovery_evidence);
    let catalog = serde_json::from_slice::<serde_json::Value>(&catalog_bytes)
        .map_err(|_| HawDBError::Execution("invalid JSON file: invalid_json".to_string()))?;
    let manifest = read_staging_artifact_json(&catalog, staging_dir, "manifest")?;
    let pointer_matches_manifest = published.get("database_commit_epoch")
        == manifest.get("database_commit_epoch")
        && published.get("graph_commit_epoch") == manifest.get("graph_commit_epoch")
        && published.get("logical_checksum") == manifest.get("logical_checksum")
        && published.get("schema_checksum") == manifest.get("schema_checksum")
        && published.get("graph_stream_checksum") == manifest.get("graph_stream_checksum")
        && published.get("graph_stream_byte_len") == manifest.get("graph_stream_byte_len")
        && published.get("relational_stream_checksum")
            == manifest.get("relational_stream_checksum")
        && published.get("relational_stream_byte_len")
            == manifest.get("relational_stream_byte_len")
        && published.get("relational_table_count") == manifest.get("relational_table_count")
        && published.get("relational_row_count") == manifest.get("relational_row_count")
        && published.get("node_count") == manifest.get("node_count")
        && published.get("relationship_count") == manifest.get("relationship_count");
    let mut errors = Vec::new();
    let mut pointer_errors = Vec::new();
    let mut catalog_errors = Vec::new();
    let mut staging_errors = Vec::new();
    if !pointer_state_published {
        record_error(
            &mut errors,
            &mut pointer_errors,
            "published pointer is not PUBLISHED",
        );
    }
    if !catalog_checksum_matches {
        record_error(
            &mut errors,
            &mut catalog_errors,
            "published pointer staging catalog checksum mismatch",
        );
    }
    if !catalog_byte_len_matches {
        record_error(
            &mut errors,
            &mut catalog_errors,
            "published pointer staging catalog byte length mismatch",
        );
    }
    if !staging_ready {
        record_error(
            &mut errors,
            &mut staging_errors,
            "published staging catalog is not ready",
        );
    }
    if !pointer_matches_manifest {
        record_error(
            &mut errors,
            &mut pointer_errors,
            "published pointer does not match staged manifest",
        );
    }
    let decision = if errors.is_empty() {
        "ready"
    } else {
        "blocked"
    };
    Ok(serde_json::json!({
        "protocol": "hawdb-lightning-published-verification",
        "protocol_version": 1,
        "pointer_state_published": pointer_state_published,
        "catalog_checksum_matches": catalog_checksum_matches,
        "catalog_byte_len_matches": catalog_byte_len_matches,
        "staging_ready": staging_ready,
        "pointer_matches_manifest": pointer_matches_manifest,
        "storage_recovery_evidence": storage_recovery_evidence,
        "published_manifest": published,
        "staging_verification": staging_verification,
        "validation_gate": {
            "decision": decision,
            "pointer_errors": pointer_errors.len(),
            "catalog_errors": catalog_errors.len(),
            "staging_errors": staging_errors.len(),
            "pointer_error_messages": pointer_errors,
            "catalog_error_messages": catalog_errors,
            "staging_error_messages": staging_errors,
            "errors": errors,
        },
    }))
}

pub fn read_hawdb_lightning_staging_artifact_json(
    catalog: &serde_json::Value,
    staging_dir: &Path,
    kind: &str,
) -> Result<serde_json::Value> {
    read_staging_artifact_json(catalog, staging_dir, kind)
}

fn read_staging_artifact_json(
    catalog: &serde_json::Value,
    staging_dir: &Path,
    kind: &str,
) -> Result<serde_json::Value> {
    let artifacts = catalog
        .get("artifacts")
        .and_then(serde_json::Value::as_array)
        .ok_or_else(|| {
            HawDBError::Execution("staging catalog missing artifacts array".to_string())
        })?;
    let artifact = artifacts
        .iter()
        .find(|artifact| artifact.get("kind").and_then(serde_json::Value::as_str) == Some(kind))
        .ok_or_else(|| HawDBError::Execution(format!("staging catalog missing {kind} artifact")))?;
    let path = artifact
        .get("path")
        .and_then(serde_json::Value::as_str)
        .ok_or_else(|| HawDBError::Execution(format!("staging {kind} artifact missing path")))?;
    if path.contains('/') || path.contains('\\') {
        return Err(HawDBError::Execution(format!(
            "staging {kind} artifact uses non-local path {path}"
        )));
    }
    read_json_file(&staging_dir.join(path))
}

fn default_storage_recovery_evidence() -> serde_json::Value {
    serde_json::json!({
        "present": false,
        "valid": true,
        "protocol_matches": false,
        "storage_version_present": false,
        "recovered_commit_epoch_matches_manifest": false,
    })
}

#[cfg(test)]
mod tests {
    use super::verify_hawdb_lightning_staging_catalog;
    use crate::{
        stage_hawdb_lightning_bootstrap_export, CanonicalGraphSnapshotExport,
        HawDBLightningBootstrapExport, HawDBLightningRelationalStream,
    };
    use hawdb_storage::relational::RelationalState;

    #[test]
    fn verifies_a_staged_export_through_the_bootstrap_owner() {
        let snapshot = CanonicalGraphSnapshotExport::from_rows(7, Vec::new(), Vec::new());
        let relational_stream =
            HawDBLightningRelationalStream::from_state(7, &RelationalState::default()).unwrap();
        let manifest = snapshot.hawdb_lightning_bootstrap_manifest(&relational_stream);
        let graph_stream = snapshot.hawdb_lightning_graph_stream();
        let export = HawDBLightningBootstrapExport {
            snapshot,
            manifest,
            graph_stream,
            relational_stream,
        };
        let staging_dir = std::env::temp_dir().join(format!(
            "hawdb-bootstrap-staging-verification-{}",
            std::process::id()
        ));
        let _ = std::fs::remove_dir_all(&staging_dir);
        stage_hawdb_lightning_bootstrap_export(&export, &staging_dir).unwrap();

        let report = verify_hawdb_lightning_staging_catalog(&staging_dir).unwrap();

        assert_eq!(report["validation_gate"]["decision"], "ready");
        assert_eq!(report["artifact_integrity"], true);
        assert_eq!(report["manifest_matches_graph_stream"], true);
        assert_eq!(report["manifest_matches_relational_stream"], true);
        assert_eq!(report["bundle_matches_artifacts"], true);
        std::fs::remove_dir_all(staging_dir).unwrap();
    }
}