umadb-tools 0.6.5

Command-line tools for UmaDB databases (integrity checks, utilities).
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
use std::collections::{BTreeMap, HashMap};
use std::env;
use std::os::unix::fs::FileExt;
use std::path::PathBuf;
use umadb_core::common::{PageID, Position};
use umadb_core::db::{read_conditional, tag_to_hash};
use umadb_core::mvcc::{DEFAULT_PAGE_SIZE, Mvcc, StorageOptions};
use umadb_core::node::Node;
use umadb_core::page::Page;
use umadb_core::tags_tree_nodes::{
    TAG_HASH_LEN, TagsLeafNode, get_tag_key_width, normalize_tag_hash_for_current_width,
    set_tag_key_width,
};
use umadb_dcb::{DcbError, DcbEvent, DcbQuery, DcbResult};

fn type_name_from_byte(b: u8) -> Option<&'static str> {
    match b {
        b'1' => Some("Header"),
        b'2' => Some("FreeListLeaf"),
        b'3' => Some("FreeListInternal"),
        b'4' => Some("EventLeaf"),
        b'5' => Some("EventInternal"),
        b'6' => Some("TagsLeaf"),
        b'7' => Some("TagsInternal"),
        b'8' => Some("TagLeaf"),
        b'9' => Some("TagInternal"),
        b'a' => Some("EventOverflow"),
        b'b' => Some("FreeListTsnLeaf"),
        b'c' => Some("FreeListTsnInternal"),
        b'd' => Some("TrackingLeaf"),
        _ => None,
    }
}

fn print_usage(program: &str) {
    eprintln!(
        "Usage: {program} [--page-size <bytes>] [--verbose] [--print-events] <path-to-db-or-directory>\n\n\
        Options:\n  --page-size <bytes>   Page size in bytes (defaults to UmaDB default).\n  --verbose             Print per-page details.\n  --print-events        Print all events found during Stage 3 with one attribute per line, including data.\n\n\
        The tool scans the database file page-by-page and reports any CRC/parse errors,\n        along with a summary of page counts by node type. It never modifies the file."
    );
}

#[derive(Default)]
struct Args {
    page_size: Option<usize>,
    verbose: bool,
    print_events: bool,
    path: Option<PathBuf>,
}

fn parse_args() -> Result<Args, String> {
    let mut args = env::args().skip(1);
    let mut res = Args::default();
    while let Some(arg) = args.next() {
        match arg.as_str() {
            "--page-size" => {
                let v = args
                    .next()
                    .ok_or_else(|| "--page-size requires a value".to_string())?;
                let ps: usize = v.parse().map_err(|_| format!("Invalid page size: {v}"))?;
                if ps == 0 || !ps.is_multiple_of(512) {
                    return Err("Page size must be a positive multiple of 512".to_string());
                }
                res.page_size = Some(ps);
            }
            "--verbose" => res.verbose = true,
            "--print-events" | "--events" => res.print_events = true,
            "-h" | "--help" => {
                print_usage(&env::args().next().unwrap_or_else(|| "umadb-check".into()));
                std::process::exit(0);
            }
            other => {
                if res.path.is_none() {
                    res.path = Some(PathBuf::from(other));
                } else {
                    return Err(format!("Unexpected argument: {other}"));
                }
            }
        }
    }
    if res.path.is_none() {
        return Err("Missing path".to_string());
    }
    Ok(res)
}

fn main() {
    if let Err(e) = real_main() {
        match e {
            DcbError::InternalError(s) => eprintln!("Error: {}", s),
            other => eprintln!("Error: {}", other),
        }
        std::process::exit(2);
    }
}

fn real_main() -> DcbResult<()> {
    let args = parse_args().map_err(DcbError::InternalError)?;

    let db_path = args.path.unwrap();

    // Quick existence check so we can fail fast with a friendly message
    if !db_path.exists() {
        return Err(DcbError::InternalError(format!(
            "Database file not found: {}",
            db_path.display()
        )));
    }

    let page_size = args.page_size.unwrap_or(DEFAULT_PAGE_SIZE);

    // Open MVCC in normal mode; the library controls file access and reads.
    // No writes are performed by this tool.
    let mvcc = Mvcc::new(
        args.verbose,
        StorageOptions::default()
            .db_path(&db_path)
            .page_size(page_size),
    )?;

    // Progressive report prelude
    println!("UmaDB file: {}", db_path.display());
    println!("Page size: {} bytes", page_size);
    println!("Stage 1: Checking headers...");

    // Prepare counters and buckets before any page reads
    let mut counts: BTreeMap<&'static str, u64> = BTreeMap::new();
    let mut event_leaf_page_ids: Vec<PageID> = Vec::new();
    let mut deser_errors: Vec<String> = Vec::new();
    let mut event_errors: Vec<String> = Vec::new();
    let mut header_errors: Vec<String> = Vec::new();
    let mut header_infos: Vec<String> = Vec::new();
    // Collect raw bytes of TagsLeaf pages that fail to deserialize for Stage 4 forensic analysis
    struct ForensicPage {
        page_id: u64,
        bytes: Vec<u8>,
        source: &'static str,
    }
    let mut forensic_tags_pages: Vec<ForensicPage> = Vec::new();

    // Bootstrap: attempt to deserialize header pages 0 and 1 before reading the latest header
    for pid in 0..=1u64 {
        let page_id = PageID(pid);
        match mvcc.pager.read_page_mmap_slice(page_id) {
            Ok(mapped) => {
                let bytes = mapped.as_slice();
                match Page::deserialize(page_id, bytes) {
                    Ok(page) => {
                        let t = page.node.type_name();
                        *counts.entry(t).or_insert(0) += 1;
                        match &page.node {
                            Node::EventLeaf(_) => {
                                event_leaf_page_ids.push(page_id);
                            }
                            Node::Header(h) => {
                                let flags_str = if h.tracking_root_page_id.0 != 0 {
                                    "HAS_TRACKING_ROOT_ID"
                                } else {
                                    "none"
                                };
                                header_infos.push(format!(
                                    "Header page {}: tsn={} next_page_id={} next_position={} schema_version={} flags={}",
                                    pid, h.tsn.0, h.next_page_id.0, h.next_position.0, h.schema_version, flags_str
                                ));
                            }
                            _ => {}
                        }
                        if args.verbose {
                            println!("ok page_id={} type={} (bootstrap)", pid, t);
                        }
                    }
                    Err(err) => {
                        let type_str = type_name_from_byte(bytes[0]).unwrap_or("unknown");
                        *counts.entry(type_str).or_insert(0) += 1;
                        let msg = match &err {
                            DcbError::DeserializationError(_) => {
                                format!("Page {}: {} {:?}", pid, type_str, err)
                            }
                            DcbError::DatabaseCorrupted(s) => {
                                format!("Page {}: {} {}", pid, type_str, s)
                            }
                            other => format!("Page {}: {} {:?}", pid, type_str, other),
                        };
                        if args.verbose {
                            eprintln!("{}", msg);
                        }
                        header_errors.push(msg);
                    }
                }
            }
            Err(e) => {
                let msg = format!(
                    "Page {}: unknown I/O error while reading header page: {}",
                    pid, e
                );
                if args.verbose {
                    eprintln!("{}", msg);
                }
                deser_errors.push(msg);
            }
        }
    }

    // Now read the latest header as usual
    let header_page = mvcc.get_latest_header_page()?;
    let header_page_id = header_page.page_id;
    let header = header_page.as_header_node()?;

    let total_pages = header.next_page_id.0;
    if args.verbose {
        eprintln!(
            "Header at page {:?}: next_page_id = {}, next_position = {}",
            header_page_id, header.next_page_id.0, header.next_position.0
        );
        eprintln!(
            "Root pages: free_lists = {:?}, events = {:?}, tags = {:?}, tracking = {:?}",
            header.free_lists_tree_root_id,
            header.events_tree_root_id,
            header.tags_tree_root_id,
            header.tracking_root_page_id
        );
    }
    // Stage 1 results
    if !header_infos.is_empty() {
        for info in &header_infos {
            println!("{}", info);
        }
    }
    println!(
        "Latest header: page={} tsn={} next_page_id={} next_position={} schema_version={} flags={}",
        header_page_id.0,
        header.tsn.0,
        header.next_page_id.0,
        header.next_position.0,
        header.schema_version,
        if header.tracking_root_page_id.0 != 0 {
            "HAS_TRACKING_ROOT_ID"
        } else {
            "none"
        }
    );
    if header_errors.is_empty() {
        println!("Headers: OK");
    } else {
        println!("Header errors:");
        for e in &header_errors {
            println!("  - {}", e);
        }
    }

    // Track actual last scanned page id for reporting (will be updated if we read beyond header)
    let mut last_scanned_pid: u64 = total_pages.saturating_sub(1);

    // Stage 2 intention
    println!("Stage 2: Checking page deserialisation...");

    // Main scan: process allocated pages starting from 2 to avoid double-processing headers
    if total_pages > 2 {
        for pid in 2..total_pages {
            let page_id = PageID(pid);
            match mvcc.read_page(page_id) {
                Ok(page) => {
                    let t = page.node.type_name();
                    *counts.entry(t).or_insert(0) += 1;
                    // Track EventLeaf page IDs for later probing if events are missing
                    if let Node::EventLeaf(_) = &page.node {
                        event_leaf_page_ids.push(page_id);
                    }
                    if args.verbose {
                        println!("ok page_id={} type={}", pid, t);
                    }
                }
                Err(err) => {
                    // Try to peek the page header to guess the node type
                    let mapped_opt = mvcc.pager.read_page_mmap_slice(page_id).ok();
                    let type_hint = mapped_opt
                        .as_ref()
                        .and_then(|m| type_name_from_byte(m.as_slice()[0]));
                    let type_str = type_hint.unwrap_or("unknown");
                    // Include failed pages in counts under their hinted type (or "unknown")
                    *counts.entry(type_str).or_insert(0) += 1;
                    // If this looks like a TagsLeaf, retain the raw page bytes for Stage 4 forensic analysis
                    if type_str == "TagsLeaf"
                        && let Some(mapped) = mapped_opt
                    {
                        forensic_tags_pages.push(ForensicPage {
                            page_id: pid,
                            bytes: mapped.as_slice().to_vec(),
                            source: "within-header",
                        });
                    }

                    let msg = match &err {
                        DcbError::DeserializationError(_) => {
                            // Show the variant and message using Debug formatting
                            format!("Page {}: {} {:?}", pid, type_str, err)
                        }
                        DcbError::DatabaseCorrupted(s) => {
                            // Keep only the human-readable message for corruption
                            format!("Page {}: {} {}", pid, type_str, s)
                        }
                        other => format!("Page {}: {} {:?}", pid, type_str, other),
                    };
                    if args.verbose {
                        eprintln!("{}", msg);
                    }
                    deser_errors.push(msg);
                }
            }
        }
    }

    // Extended scan: continue reading pages beyond header.next_page_id up to actual file length
    // Stop when a full page of zeros is encountered or when the file ends.
    let file_len = mvcc
        .pager
        .file
        .metadata()
        .map_err(|e| DcbError::InternalError(format!("Failed to read file metadata: {}", e)))?
        .len();
    let page_size_u64 = page_size as u64;
    let mut pid = total_pages; // start at next_page_id
    let mut buf = vec![0u8; page_size];

    // Track next-page status and number of contiguous data pages beyond header before first zero page
    let mut next_page_status: Option<String> = None;
    let mut data_pages_beyond: u64 = 0;

    loop {
        let offset = pid.saturating_mul(page_size_u64);
        let end = offset + page_size_u64;
        if end > file_len {
            if pid == total_pages {
                next_page_status = Some("not present (EOF)".to_string());
            }
            break;
        }
        // Read page content safely without expanding mappings or the file
        match mvcc.pager.file.read_at(&mut buf[..], offset as u64) {
            Ok(n) => {
                if n < page_size {
                    if pid == total_pages {
                        next_page_status = Some("not present (short read at EOF)".to_string());
                    }
                    break; // Short read at end-of-file; stop scanning
                }
            }
            Err(e) => {
                if pid == total_pages {
                    next_page_status = Some(format!("I/O error: {}", e));
                }
                deser_errors.push(format!(
                    "Page {}: unknown I/O error while reading beyond header: {}",
                    pid, e
                ));
                break;
            }
        }
        // If the page is all zeros, it hasn't been written; stop here
        if buf.iter().all(|&b| b == 0) {
            if pid == total_pages {
                next_page_status = Some("zero-filled (unused)".to_string());
            }
            break;
        }
        // Non-zero page: mark next-page status if this is the first beyond-header page
        if pid == total_pages && next_page_status.is_none() {
            next_page_status = Some("data present".to_string());
        }
        // Try to deserialize this page
        let page_id = PageID(pid);
        match Page::deserialize(page_id, &buf[..]) {
            Ok(page) => {
                let t = page.node.type_name();
                *counts.entry(t).or_insert(0) += 1;
                if let Node::EventLeaf(_) = &page.node {
                    event_leaf_page_ids.push(page_id);
                }
                if args.verbose {
                    println!("ok page_id={} (beyond header) type={}", pid, t);
                }
            }
            Err(err) => {
                let type_hint = type_name_from_byte(buf[0]);
                let type_str = type_hint.unwrap_or("unknown");
                *counts.entry(type_str).or_insert(0) += 1;
                if type_str == "TagsLeaf" {
                    forensic_tags_pages.push(ForensicPage {
                        page_id: pid,
                        bytes: buf.clone(),
                        source: "beyond-header",
                    });
                }
                let msg = match &err {
                    DcbError::DeserializationError(_) => {
                        format!("Page {}: {} {:?}", pid, type_str, err)
                    }
                    DcbError::DatabaseCorrupted(s) => format!("Page {}: {} {}", pid, type_str, s),
                    other => format!("Page {}: {} {:?}", pid, type_str, other),
                };
                if args.verbose {
                    eprintln!("{}", msg);
                }
                deser_errors.push(msg);
            }
        }
        last_scanned_pid = pid;
        data_pages_beyond += 1;
        pid += 1;
    }

    // Deserialisation section (Stage 2 findings)
    println!(
        "Checking page deserialisation across pages 0..{}...",
        last_scanned_pid
    );
    // Report next-page status and how many contiguous data pages exist before first zero page
    let next_page_id_val = total_pages;
    let next_status = next_page_status.unwrap_or_else(|| "unknown".to_string());
    println!("Next page (page {}): {}", next_page_id_val, next_status);
    println!(
        "Data pages beyond header.next_page_id before first zero page: {}",
        data_pages_beyond
    );
    println!("Counts by node type:");
    for (k, v) in counts.iter() {
        println!("  {:>22}: {}", k, v);
    }
    if deser_errors.is_empty() {
        println!("Deserialisation: OK");
    } else {
        println!("Deserialisation errors:");
        for e in &deser_errors {
            println!("  - {}", e);
        }
    }

    // Stage 3 intention
    println!("Stage 3: Checking events...");
    if args.print_events {
        println!("Printing events found (position-ordered):");
    }

    // After page scan, attempt higher-level integrity: event sequence vs next_position
    let mut last_event_pos: u64 = 0;
    let mut start = Some(Position(1));
    let mut total_events_read: u64 = 0;
    // Cache of events by position for Stage 4 analysis
    let mut events_by_position: HashMap<u64, DcbEvent> = HashMap::new();
    let empty_dirty: HashMap<PageID, Page> = HashMap::new();
    loop {
        // Read a batch of events sequentially using empty query (all events)
        match read_conditional(
            &mvcc,
            &empty_dirty,
            header.events_tree_root_id,
            header.tags_tree_root_id,
            DcbQuery { items: vec![] },
            start,
            false,
            Some(1024),
            false,
            None,
        ) {
            Ok(batch) => {
                if batch.is_empty() {
                    break;
                }
                if args.print_events {
                    for ev in &batch {
                        // Build tags list string
                        let tags_str = if ev.event.tags.is_empty() {
                            String::from("[]")
                        } else {
                            let mut s = String::from("[");
                            for (i, t) in ev.event.tags.iter().enumerate() {
                                if i > 0 {
                                    s.push_str(", ");
                                }
                                s.push_str(t);
                            }
                            s.push(']');
                            s
                        };
                        // Build UUID string (if any)
                        let uuid_str = ev
                            .event
                            .uuid
                            .as_ref()
                            .map(|u| u.to_string())
                            .unwrap_or_else(|| "none".to_string());
                        // Hex-encode data
                        let mut hex = String::with_capacity(ev.event.data.len() * 2);
                        for b in &ev.event.data {
                            hex.push_str(&format!("{:02x}", b));
                        }
                        println!("Event {}:", ev.position);
                        println!("  type: {}", ev.event.event_type);
                        println!("  tags: {}", tags_str);
                        println!("  uuid: {}", uuid_str);
                        println!("  data_len: {}", ev.event.data.len());
                        println!("  data: {}", hex);
                    }
                }
                total_events_read += batch.len() as u64;
                for ev in &batch {
                    // Cache event by position for Stage 4
                    events_by_position.insert(
                        ev.position,
                        DcbEvent {
                            event_type: ev.event.event_type.clone(),
                            data: ev.event.data.clone(),
                            tags: ev.event.tags.clone(),
                            uuid: ev.event.uuid,
                            metadata: ev.event.metadata.clone(),
                        },
                    );
                    last_event_pos = ev.position;
                }
                // next start is last + 1
                start = Some(Position(last_event_pos + 1));
            }
            Err(e) => {
                event_errors.push(format!("Event sequence traversal failed: {:?}", e));
                break;
            }
        }
    }

    // Compare last event position to header.next_position - 1
    let header_last = header.next_position.0.saturating_sub(1);

    // New: validate we can get the last event directly using the events tree (backwards read)
    let mut last_by_tree: Option<u64> = None;
    match read_conditional(
        &mvcc,
        &empty_dirty,
        header.events_tree_root_id,
        header.tags_tree_root_id,
        DcbQuery { items: vec![] },
        None,    // no start -> let iterator pick end when backwards
        true,    // backwards: start from the end
        Some(1), // only need the last event
        false,
        None,
    ) {
        Ok(mut v) => {
            if let Some(ev) = v.pop() {
                last_by_tree = Some(ev.position);
            }
        }
        Err(e) => {
            event_errors.push(format!(
                "Direct last-event lookup via events tree failed: {:?}",
                e
            ));
        }
    }

    // events_ok1: sequential scan vs header
    let events_ok1 = last_event_pos == header_last;
    if !events_ok1 {
        event_errors.push(format!(
            "Event sequence mismatch: header.next_position={} implies last={}, but events end at {} (events read: {})",
            header.next_position.0,
            header_last,
            last_event_pos,
            total_events_read
        ));
    } else if args.verbose {
        eprintln!(
            "Event sequence check OK: last_event_pos={} matches header.next_position-1",
            last_event_pos
        );
    }

    // events_ok2: direct-tree last vs header
    let events_ok2 = match last_by_tree {
        Some(p) => p == header_last,
        None => false,
    };
    if !events_ok2 {
        match last_by_tree {
            Some(p) => event_errors.push(format!(
                "Events tree last-event mismatch: header.next_position={} implies last={}, but direct-tree read returned {}",
                header.next_position.0,
                header_last,
                p
            )),
            None => event_errors.push("Events tree last-event lookup returned no events while header indicates there should be some".to_string()),
        }
    } else if args.verbose {
        eprintln!("Direct events-tree last-event check OK: {}", header_last);
    }

    // Also compare sequential scan vs direct-tree result if both present
    if let Some(p) = last_by_tree
        && p != last_event_pos
    {
        event_errors.push(format!(
            "Mismatch between sequential scan last ({}) and direct-tree last ({})",
            last_event_pos, p
        ));
    }

    let events_ok =
        events_ok1 && events_ok2 && last_by_tree.map(|p| p == last_event_pos).unwrap_or(false);

    // If there are missing events, probe EventLeaf pages to see if the missing positions are present there
    let mut probe_findings: Vec<String> = Vec::new();
    if !events_ok {
        let missing_start = last_event_pos.saturating_add(1);
        let missing_end = header_last; // inclusive
        if missing_start <= missing_end {
            if args.verbose {
                eprintln!(
                    "Probing EventLeaf pages for missing positions in range {}..={} ({} positions) ...",
                    missing_start,
                    missing_end,
                    missing_end - missing_start + 1
                );
            }
            // Iterate EventLeaf page IDs that successfully deserialized earlier
            for pid in &event_leaf_page_ids {
                match mvcc.read_page(*pid) {
                    Ok(page) => {
                        if let Node::EventLeaf(node) = &page.node {
                            let mut matches: Vec<u64> = Vec::new();
                            for k in node.keys.iter() {
                                let pos = k.0;
                                if pos >= missing_start && pos <= missing_end {
                                    matches.push(pos);
                                }
                            }
                            if !matches.is_empty() {
                                matches.sort_unstable();
                                let total = matches.len();
                                let show = usize::min(total, 10);
                                let sample: Vec<String> =
                                    matches.iter().take(show).map(|p| p.to_string()).collect();
                                let suffix = if total > show {
                                    format!(" (showing first {} of {})", show, total)
                                } else {
                                    String::new()
                                };
                                probe_findings.push(format!(
                                    "Page {}: contains {} missing positions{}: {}",
                                    pid.0,
                                    total,
                                    if suffix.is_empty() { "" } else { &suffix },
                                    sample.join(", ")
                                ));
                            }
                        }
                    }
                    Err(e) => {
                        if args.verbose {
                            eprintln!(
                                "Skipping EventLeaf probe for page {} due to error: {:?}",
                                pid.0, e
                            );
                        }
                    }
                }
            }
            if probe_findings.is_empty() {
                probe_findings.push(format!(
                    "No EventLeaf pages were found containing positions in the missing range {}..={}.",
                    missing_start, missing_end
                ));
            }
        }
    }

    // Event sequence section
    println!("Checking event sequence vs header.next_position...");
    if events_ok && event_errors.is_empty() {
        println!(
            "Events: {} (last position {}) checks out against header.next_position={}",
            total_events_read, last_event_pos, header.next_position.0
        );
    } else {
        println!("Event sequence errors:");
        for e in &event_errors {
            println!("  - {}", e);
        }
        // If we probed EventLeaf pages, report findings
        if !probe_findings.is_empty() {
            println!("Looking for missing events inside EventLeaf pages:");
            for f in &probe_findings {
                println!("  - {}", f);
            }
        }
    }

    // Stage 4 intention and findings: Forensic analysis of failed TagsLeaf pages
    println!("Stage 4: Forensic analysis of failed TagsLeaf pages...");
    if forensic_tags_pages.is_empty() {
        println!("No failed TagsLeaf pages to analyze.");
    } else {
        // Helper to parse a TagsLeaf body tolerantly with a given key width
        fn forensic_parse_tags_leaf(
            body: &[u8],
            keyw: usize,
        ) -> (usize, Vec<([u8; TAG_HASH_LEN], Vec<u64>)>, String) {
            let mut notes = String::new();
            if body.len() < 2 {
                notes.push_str("too short (<2 bytes) for keys_len; ");
                return (0, vec![], notes);
            }
            let keys_len = u16::from_le_bytes([body[0], body[1]]) as usize;
            let mut offset = 2usize;
            let keys_bytes = keys_len.saturating_mul(keyw);
            if body.len() < offset + keys_bytes {
                notes.push_str("not enough bytes for keys; ");
                return (0, vec![], notes);
            }
            let mut keys: Vec<[u8; TAG_HASH_LEN]> = Vec::with_capacity(keys_len);
            for i in 0..keys_len {
                let start = offset + i * keyw;
                let mut k = [0u8; TAG_HASH_LEN];
                let end = start + keyw;
                if end <= body.len() {
                    k[..keyw].copy_from_slice(&body[start..end]);
                } else {
                    notes.push_str(&format!("key {} truncated; ", i));
                    let avail = body.len().saturating_sub(start);
                    k[..avail].copy_from_slice(&body[start..start + avail]);
                }
                keys.push(k);
            }
            offset += keys_bytes;
            let mut out: Vec<([u8; TAG_HASH_LEN], Vec<u64>)> = Vec::new();
            for (i, kh) in keys.into_iter().enumerate() {
                if offset + 10 > body.len() {
                    notes.push_str(&format!("value header for key {} truncated; ", i));
                    break;
                }
                let _root = u64::from_le_bytes(body[offset..offset + 8].try_into().unwrap());
                offset += 8;
                let plen =
                    u16::from_le_bytes(body[offset..offset + 2].try_into().unwrap()) as usize;
                offset += 2;
                let need = plen.saturating_mul(8);
                if offset + need > body.len() {
                    notes.push_str(&format!("positions for key {} truncated; ", i));
                    // read as many complete u64s as we have
                    let avail = (body.len().saturating_sub(offset)) / 8;
                    let mut pos: Vec<u64> = Vec::with_capacity(avail);
                    for j in 0..avail {
                        let p = offset + j * 8;
                        pos.push(u64::from_le_bytes(body[p..p + 8].try_into().unwrap()));
                    }
                    out.push((kh, pos));
                    break;
                } else {
                    let mut pos: Vec<u64> = Vec::with_capacity(usize::min(plen, 4096));
                    for j in 0..plen {
                        let p = offset + j * 8;
                        pos.push(u64::from_le_bytes(body[p..p + 8].try_into().unwrap()));
                    }
                    offset += need;
                    out.push((kh, pos));
                }
            }
            (out.len(), out, notes)
        }

        // Helper to analyze recovered entries against cached events using a specific hash width
        fn analyze_entries(
            entries: &Vec<([u8; TAG_HASH_LEN], Vec<u64>)>,
            used_w: usize,
            events_by_position: &HashMap<u64, DcbEvent>,
        ) -> (usize, usize, usize, Vec<String>) {
            let mut total_positions = 0usize;
            let mut pos_with_event = 0usize;
            let mut hash_matches = 0usize;
            let mut mismatches: Vec<String> = Vec::new();

            for (kh, positions) in entries.iter() {
                total_positions += positions.len();
                for pos in positions {
                    if let Some(ev) = events_by_position.get(pos) {
                        pos_with_event += 1;
                        let mut matched = false;
                        for t in &ev.tags {
                            if used_w == 8 {
                                let h = umadb_core::db::tag_to_hash_crc64(t);
                                if h[..8] == kh[..8] {
                                    matched = true;
                                    break;
                                }
                            } else {
                                let h = normalize_tag_hash_for_current_width(tag_to_hash(t));
                                if &h == kh {
                                    matched = true;
                                    break;
                                }
                            }
                        }
                        if matched {
                            hash_matches += 1;
                        } else if mismatches.len() < 5 {
                            mismatches.push(format!(
                                "pos {} has event with tags {:?} but none hash to key {:02x?}",
                                pos,
                                ev.tags,
                                &kh[..used_w]
                            ));
                        }
                    }
                }
            }
            (total_positions, pos_with_event, hash_matches, mismatches)
        }

        // Analyze each forensic page
        for fp in &forensic_tags_pages {
            // Extract page body from full page bytes (strip 9-byte page header)
            let bytes = &fp.bytes;
            let body = if bytes.len() >= 9 {
                &bytes[9..]
            } else {
                bytes.as_slice()
            };

            // Parse with both widths independently
            let cur_w = get_tag_key_width();
            let (_n_cur, cur_entries, cur_notes) = forensic_parse_tags_leaf(body, cur_w);
            let (_n_legacy, legacy_entries, legacy_notes) = forensic_parse_tags_leaf(body, 8);

            println!("Forensic TagsLeaf page {} ({}):", fp.page_id, fp.source);

            // Report current width analysis
            let (tpos_cur, pe_cur, hm_cur, mm_cur) =
                analyze_entries(&cur_entries, cur_w, &events_by_position);
            println!(
                "  parse width={}: recovered keys={} total_positions={}",
                cur_w,
                cur_entries.len(),
                tpos_cur
            );
            if !cur_notes.is_empty() {
                println!("  notes[width={}]: {}", cur_w, cur_notes);
            }
            println!("  positions with readable events: {}", pe_cur);
            println!(
                "  events where some tag hashes matched recovered key: {}",
                hm_cur
            );
            if !mm_cur.is_empty() {
                println!("  sample mismatches (up to 5) [width={}]:", cur_w);
                for m in mm_cur {
                    println!("    - {}", m);
                }
            }

            // Report legacy width=8 analysis
            let (tpos_legacy, pe_legacy, hm_legacy, mm_legacy) =
                analyze_entries(&legacy_entries, 8, &events_by_position);
            println!(
                "  parse width=8: recovered keys={} total_positions={}",
                legacy_entries.len(),
                tpos_legacy
            );
            if !legacy_notes.is_empty() {
                println!("  notes[width=8]: {}", legacy_notes);
            }
            println!("  positions with readable events: {}", pe_legacy);
            println!(
                "  events where some tag hashes matched recovered key: {}",
                hm_legacy
            );
            if !mm_legacy.is_empty() {
                println!("  sample mismatches (up to 5) [width=8]:");
                for m in mm_legacy {
                    println!("    - {}", m);
                }
            }

            // Additionally, attempt core deserialization using schema 0 key width (8 bytes)
            let prev_w = get_tag_key_width();
            set_tag_key_width(8);
            let core_res = TagsLeafNode::from_slice(body);
            // Restore width immediately
            set_tag_key_width(prev_w);
            match core_res {
                Ok(node) => {
                    let keys = node.keys.len();
                    let total_positions: usize =
                        node.values.iter().map(|v| v.positions.len()).sum();
                    println!(
                        "  core deserialise with schema0 key width=8: OK (keys={} total_positions={})",
                        keys, total_positions
                    );
                }
                Err(e) => {
                    println!(
                        "  core deserialise with schema0 key width=8: failed: {:?}",
                        e
                    );
                }
            }
        }
    }

    // Final outcome
    if deser_errors.is_empty() && event_errors.is_empty() {
        println!("Integrity: OK");
        Ok(())
    } else {
        Err(DcbError::InternalError(
            "Integrity check failed".to_string(),
        ))
    }
}