Skip to main content

icydb_core/db/diagnostics/
mod.rs

1//! Module: diagnostics
2//! Responsibility: read-only storage footprint and integrity snapshots.
3//! Does not own: recovery, write-path mutation, or query planning semantics.
4//! Boundary: consumes `Db`/store read APIs and returns DTO snapshots.
5
6mod execution_trace;
7#[cfg(test)]
8mod tests;
9
10use crate::{
11    db::{
12        Db, EntityRuntimeHooks,
13        commit::CommitRowOp,
14        data::{DataKey, StorageKey, decode_structural_row_cbor},
15        index::IndexKey,
16        registry::StoreHandle,
17    },
18    error::{ErrorClass, InternalError},
19    traits::CanisterKind,
20    types::EntityTag,
21};
22use candid::CandidType;
23use serde::Deserialize;
24use std::collections::{BTreeMap, BTreeSet};
25
26pub use execution_trace::{
27    ExecutionAccessPathVariant, ExecutionMetrics, ExecutionOptimization, ExecutionTrace,
28};
29
30#[cfg_attr(doc, doc = "StorageReport\n\nLive storage snapshot payload.")]
31#[derive(CandidType, Clone, Debug, Default, Deserialize)]
32pub struct StorageReport {
33    pub(crate) storage_data: Vec<DataStoreSnapshot>,
34    pub(crate) storage_index: Vec<IndexStoreSnapshot>,
35    pub(crate) entity_storage: Vec<EntitySnapshot>,
36    pub(crate) corrupted_keys: u64,
37    pub(crate) corrupted_entries: u64,
38}
39
40#[cfg_attr(
41    doc,
42    doc = "IntegrityTotals\n\nAggregated integrity-scan counters across all stores."
43)]
44#[derive(CandidType, Clone, Debug, Default, Deserialize)]
45pub struct IntegrityTotals {
46    pub(crate) data_rows_scanned: u64,
47    pub(crate) index_entries_scanned: u64,
48    pub(crate) corrupted_data_keys: u64,
49    pub(crate) corrupted_data_rows: u64,
50    pub(crate) corrupted_index_keys: u64,
51    pub(crate) corrupted_index_entries: u64,
52    pub(crate) missing_index_entries: u64,
53    pub(crate) divergent_index_entries: u64,
54    pub(crate) orphan_index_references: u64,
55    pub(crate) compatibility_findings: u64,
56    pub(crate) misuse_findings: u64,
57}
58
59impl IntegrityTotals {
60    const fn add_store_snapshot(&mut self, store: &IntegrityStoreSnapshot) {
61        self.data_rows_scanned = self
62            .data_rows_scanned
63            .saturating_add(store.data_rows_scanned);
64        self.index_entries_scanned = self
65            .index_entries_scanned
66            .saturating_add(store.index_entries_scanned);
67        self.corrupted_data_keys = self
68            .corrupted_data_keys
69            .saturating_add(store.corrupted_data_keys);
70        self.corrupted_data_rows = self
71            .corrupted_data_rows
72            .saturating_add(store.corrupted_data_rows);
73        self.corrupted_index_keys = self
74            .corrupted_index_keys
75            .saturating_add(store.corrupted_index_keys);
76        self.corrupted_index_entries = self
77            .corrupted_index_entries
78            .saturating_add(store.corrupted_index_entries);
79        self.missing_index_entries = self
80            .missing_index_entries
81            .saturating_add(store.missing_index_entries);
82        self.divergent_index_entries = self
83            .divergent_index_entries
84            .saturating_add(store.divergent_index_entries);
85        self.orphan_index_references = self
86            .orphan_index_references
87            .saturating_add(store.orphan_index_references);
88        self.compatibility_findings = self
89            .compatibility_findings
90            .saturating_add(store.compatibility_findings);
91        self.misuse_findings = self.misuse_findings.saturating_add(store.misuse_findings);
92    }
93
94    /// Return total number of data rows scanned.
95    #[must_use]
96    pub const fn data_rows_scanned(&self) -> u64 {
97        self.data_rows_scanned
98    }
99
100    /// Return total number of index entries scanned.
101    #[must_use]
102    pub const fn index_entries_scanned(&self) -> u64 {
103        self.index_entries_scanned
104    }
105
106    /// Return total number of corrupted data-key findings.
107    #[must_use]
108    pub const fn corrupted_data_keys(&self) -> u64 {
109        self.corrupted_data_keys
110    }
111
112    /// Return total number of corrupted data-row findings.
113    #[must_use]
114    pub const fn corrupted_data_rows(&self) -> u64 {
115        self.corrupted_data_rows
116    }
117
118    /// Return total number of corrupted index-key findings.
119    #[must_use]
120    pub const fn corrupted_index_keys(&self) -> u64 {
121        self.corrupted_index_keys
122    }
123
124    /// Return total number of corrupted index-entry findings.
125    #[must_use]
126    pub const fn corrupted_index_entries(&self) -> u64 {
127        self.corrupted_index_entries
128    }
129
130    /// Return total number of missing index-entry findings.
131    #[must_use]
132    pub const fn missing_index_entries(&self) -> u64 {
133        self.missing_index_entries
134    }
135
136    /// Return total number of divergent index-entry findings.
137    #[must_use]
138    pub const fn divergent_index_entries(&self) -> u64 {
139        self.divergent_index_entries
140    }
141
142    /// Return total number of orphan index-reference findings.
143    #[must_use]
144    pub const fn orphan_index_references(&self) -> u64 {
145        self.orphan_index_references
146    }
147
148    /// Return total number of compatibility findings.
149    #[must_use]
150    pub const fn compatibility_findings(&self) -> u64 {
151        self.compatibility_findings
152    }
153
154    /// Return total number of misuse findings.
155    #[must_use]
156    pub const fn misuse_findings(&self) -> u64 {
157        self.misuse_findings
158    }
159}
160
161#[cfg_attr(
162    doc,
163    doc = "IntegrityStoreSnapshot\n\nPer-store integrity findings and scan counters."
164)]
165#[derive(CandidType, Clone, Debug, Default, Deserialize)]
166pub struct IntegrityStoreSnapshot {
167    pub(crate) path: String,
168    pub(crate) data_rows_scanned: u64,
169    pub(crate) index_entries_scanned: u64,
170    pub(crate) corrupted_data_keys: u64,
171    pub(crate) corrupted_data_rows: u64,
172    pub(crate) corrupted_index_keys: u64,
173    pub(crate) corrupted_index_entries: u64,
174    pub(crate) missing_index_entries: u64,
175    pub(crate) divergent_index_entries: u64,
176    pub(crate) orphan_index_references: u64,
177    pub(crate) compatibility_findings: u64,
178    pub(crate) misuse_findings: u64,
179}
180
181impl IntegrityStoreSnapshot {
182    /// Construct one empty store-level integrity snapshot.
183    #[must_use]
184    pub fn new(path: String) -> Self {
185        Self {
186            path,
187            ..Self::default()
188        }
189    }
190
191    /// Borrow store path.
192    #[must_use]
193    pub const fn path(&self) -> &str {
194        self.path.as_str()
195    }
196
197    /// Return number of scanned data rows.
198    #[must_use]
199    pub const fn data_rows_scanned(&self) -> u64 {
200        self.data_rows_scanned
201    }
202
203    /// Return number of scanned index entries.
204    #[must_use]
205    pub const fn index_entries_scanned(&self) -> u64 {
206        self.index_entries_scanned
207    }
208
209    /// Return number of corrupted data-key findings.
210    #[must_use]
211    pub const fn corrupted_data_keys(&self) -> u64 {
212        self.corrupted_data_keys
213    }
214
215    /// Return number of corrupted data-row findings.
216    #[must_use]
217    pub const fn corrupted_data_rows(&self) -> u64 {
218        self.corrupted_data_rows
219    }
220
221    /// Return number of corrupted index-key findings.
222    #[must_use]
223    pub const fn corrupted_index_keys(&self) -> u64 {
224        self.corrupted_index_keys
225    }
226
227    /// Return number of corrupted index-entry findings.
228    #[must_use]
229    pub const fn corrupted_index_entries(&self) -> u64 {
230        self.corrupted_index_entries
231    }
232
233    /// Return number of missing index-entry findings.
234    #[must_use]
235    pub const fn missing_index_entries(&self) -> u64 {
236        self.missing_index_entries
237    }
238
239    /// Return number of divergent index-entry findings.
240    #[must_use]
241    pub const fn divergent_index_entries(&self) -> u64 {
242        self.divergent_index_entries
243    }
244
245    /// Return number of orphan index-reference findings.
246    #[must_use]
247    pub const fn orphan_index_references(&self) -> u64 {
248        self.orphan_index_references
249    }
250
251    /// Return number of compatibility findings.
252    #[must_use]
253    pub const fn compatibility_findings(&self) -> u64 {
254        self.compatibility_findings
255    }
256
257    /// Return number of misuse findings.
258    #[must_use]
259    pub const fn misuse_findings(&self) -> u64 {
260        self.misuse_findings
261    }
262}
263
264#[cfg_attr(
265    doc,
266    doc = "IntegrityReport\n\nFull integrity-scan output across all registered stores."
267)]
268#[derive(CandidType, Clone, Debug, Default, Deserialize)]
269pub struct IntegrityReport {
270    pub(crate) stores: Vec<IntegrityStoreSnapshot>,
271    pub(crate) totals: IntegrityTotals,
272}
273
274impl IntegrityReport {
275    /// Construct one integrity report payload.
276    #[must_use]
277    pub const fn new(stores: Vec<IntegrityStoreSnapshot>, totals: IntegrityTotals) -> Self {
278        Self { stores, totals }
279    }
280
281    /// Borrow per-store integrity snapshots.
282    #[must_use]
283    pub const fn stores(&self) -> &[IntegrityStoreSnapshot] {
284        self.stores.as_slice()
285    }
286
287    /// Borrow aggregated integrity totals.
288    #[must_use]
289    pub const fn totals(&self) -> &IntegrityTotals {
290        &self.totals
291    }
292}
293
294impl StorageReport {
295    /// Construct one storage report payload.
296    #[must_use]
297    pub const fn new(
298        storage_data: Vec<DataStoreSnapshot>,
299        storage_index: Vec<IndexStoreSnapshot>,
300        entity_storage: Vec<EntitySnapshot>,
301        corrupted_keys: u64,
302        corrupted_entries: u64,
303    ) -> Self {
304        Self {
305            storage_data,
306            storage_index,
307            entity_storage,
308            corrupted_keys,
309            corrupted_entries,
310        }
311    }
312
313    /// Borrow data-store snapshots.
314    #[must_use]
315    pub const fn storage_data(&self) -> &[DataStoreSnapshot] {
316        self.storage_data.as_slice()
317    }
318
319    /// Borrow index-store snapshots.
320    #[must_use]
321    pub const fn storage_index(&self) -> &[IndexStoreSnapshot] {
322        self.storage_index.as_slice()
323    }
324
325    /// Borrow entity-level storage snapshots.
326    #[must_use]
327    pub const fn entity_storage(&self) -> &[EntitySnapshot] {
328        self.entity_storage.as_slice()
329    }
330
331    /// Return count of corrupted decoded data keys.
332    #[must_use]
333    pub const fn corrupted_keys(&self) -> u64 {
334        self.corrupted_keys
335    }
336
337    /// Return count of corrupted index entries.
338    #[must_use]
339    pub const fn corrupted_entries(&self) -> u64 {
340        self.corrupted_entries
341    }
342}
343
344#[cfg_attr(doc, doc = "DataStoreSnapshot\n\nData-store snapshot row.")]
345#[derive(CandidType, Clone, Debug, Default, Deserialize)]
346pub struct DataStoreSnapshot {
347    pub(crate) path: String,
348    pub(crate) entries: u64,
349    pub(crate) memory_bytes: u64,
350}
351
352impl DataStoreSnapshot {
353    /// Construct one data-store snapshot row.
354    #[must_use]
355    pub const fn new(path: String, entries: u64, memory_bytes: u64) -> Self {
356        Self {
357            path,
358            entries,
359            memory_bytes,
360        }
361    }
362
363    /// Borrow store path.
364    #[must_use]
365    pub const fn path(&self) -> &str {
366        self.path.as_str()
367    }
368
369    /// Return row count.
370    #[must_use]
371    pub const fn entries(&self) -> u64 {
372        self.entries
373    }
374
375    /// Return memory usage in bytes.
376    #[must_use]
377    pub const fn memory_bytes(&self) -> u64 {
378        self.memory_bytes
379    }
380}
381
382#[cfg_attr(doc, doc = "IndexStoreSnapshot\n\nIndex-store snapshot row.")]
383#[derive(CandidType, Clone, Debug, Default, Deserialize)]
384pub struct IndexStoreSnapshot {
385    pub(crate) path: String,
386    pub(crate) entries: u64,
387    pub(crate) user_entries: u64,
388    pub(crate) system_entries: u64,
389    pub(crate) memory_bytes: u64,
390}
391
392impl IndexStoreSnapshot {
393    /// Construct one index-store snapshot row.
394    #[must_use]
395    pub const fn new(
396        path: String,
397        entries: u64,
398        user_entries: u64,
399        system_entries: u64,
400        memory_bytes: u64,
401    ) -> Self {
402        Self {
403            path,
404            entries,
405            user_entries,
406            system_entries,
407            memory_bytes,
408        }
409    }
410
411    /// Borrow store path.
412    #[must_use]
413    pub const fn path(&self) -> &str {
414        self.path.as_str()
415    }
416
417    /// Return total entry count.
418    #[must_use]
419    pub const fn entries(&self) -> u64 {
420        self.entries
421    }
422
423    /// Return user-namespace entry count.
424    #[must_use]
425    pub const fn user_entries(&self) -> u64 {
426        self.user_entries
427    }
428
429    /// Return system-namespace entry count.
430    #[must_use]
431    pub const fn system_entries(&self) -> u64 {
432        self.system_entries
433    }
434
435    /// Return memory usage in bytes.
436    #[must_use]
437    pub const fn memory_bytes(&self) -> u64 {
438        self.memory_bytes
439    }
440}
441
442#[cfg_attr(doc, doc = "EntitySnapshot\n\nPer-entity storage snapshot row.")]
443#[derive(CandidType, Clone, Debug, Default, Deserialize)]
444pub struct EntitySnapshot {
445    pub(crate) store: String,
446
447    pub(crate) path: String,
448
449    pub(crate) entries: u64,
450
451    pub(crate) memory_bytes: u64,
452}
453
454impl EntitySnapshot {
455    /// Construct one entity-storage snapshot row.
456    #[must_use]
457    pub const fn new(store: String, path: String, entries: u64, memory_bytes: u64) -> Self {
458        Self {
459            store,
460            path,
461            entries,
462            memory_bytes,
463        }
464    }
465
466    /// Borrow store path.
467    #[must_use]
468    pub const fn store(&self) -> &str {
469        self.store.as_str()
470    }
471
472    /// Borrow entity path.
473    #[must_use]
474    pub const fn path(&self) -> &str {
475        self.path.as_str()
476    }
477
478    /// Return row count.
479    #[must_use]
480    pub const fn entries(&self) -> u64 {
481        self.entries
482    }
483
484    /// Return memory usage in bytes.
485    #[must_use]
486    pub const fn memory_bytes(&self) -> u64 {
487        self.memory_bytes
488    }
489}
490
491#[cfg_attr(
492    doc,
493    doc = "EntityStats\n\nInternal struct for building per-entity stats before snapshotting."
494)]
495#[derive(Default)]
496struct EntityStats {
497    entries: u64,
498    memory_bytes: u64,
499}
500
501impl EntityStats {
502    // Accumulate per-entity entry count and byte footprint for snapshot output.
503    const fn update(&mut self, value_len: u64) {
504        self.entries = self.entries.saturating_add(1);
505        self.memory_bytes = self
506            .memory_bytes
507            .saturating_add(DataKey::entry_size_bytes(value_len));
508    }
509}
510
511// Update one small per-store entity-stat accumulator without pulling ordered
512// map machinery into the default snapshot path. Final output ordering is still
513// enforced later on the emitted snapshot rows.
514fn update_default_entity_stats(
515    entity_stats: &mut Vec<(EntityTag, EntityStats)>,
516    entity_tag: EntityTag,
517    value_len: u64,
518) {
519    if let Some((_, stats)) = entity_stats
520        .iter_mut()
521        .find(|(existing_tag, _)| *existing_tag == entity_tag)
522    {
523        stats.update(value_len);
524        return;
525    }
526
527    let mut stats = EntityStats::default();
528    stats.update(value_len);
529    entity_stats.push((entity_tag, stats));
530}
531
532fn storage_report_name_for_hook<'a, C: CanisterKind>(
533    name_map: &BTreeMap<&'static str, &'a str>,
534    hooks: &EntityRuntimeHooks<C>,
535) -> &'a str {
536    name_map
537        .get(hooks.entity_path)
538        .copied()
539        .or_else(|| name_map.get(hooks.model.name()).copied())
540        .unwrap_or(hooks.entity_path)
541}
542
543// Resolve one default entity path label for storage snapshots without pulling
544// alias/path remapping support into the caller.
545fn storage_report_default_name_for_entity_tag<C: CanisterKind>(
546    db: &Db<C>,
547    entity_tag: EntityTag,
548) -> String {
549    db.runtime_hook_for_entity_tag(entity_tag).ok().map_or_else(
550        || format!("#{}", entity_tag.value()),
551        |hooks| hooks.entity_path.to_string(),
552    )
553}
554
555#[cfg_attr(
556    doc,
557    doc = "Build one deterministic storage snapshot with default entity-path names.\n\nThis variant is used by generated snapshot endpoints that never pass alias remapping, so it keeps the snapshot root independent from optional alias-resolution machinery."
558)]
559pub(crate) fn storage_report_default<C: CanisterKind>(
560    db: &Db<C>,
561) -> Result<StorageReport, InternalError> {
562    db.ensure_recovered_state()?;
563    let mut data = Vec::new();
564    let mut index = Vec::new();
565    let mut entity_storage: Vec<EntitySnapshot> = Vec::new();
566    let mut corrupted_keys = 0u64;
567    let mut corrupted_entries = 0u64;
568
569    db.with_store_registry(|reg| {
570        // Keep diagnostics snapshots deterministic by traversing stores in path order.
571        let mut stores = reg.iter().collect::<Vec<_>>();
572        stores.sort_by_key(|(path, _)| *path);
573
574        for (path, store_handle) in stores {
575            // Phase 1: collect data-store snapshots and per-entity stats.
576            store_handle.with_data(|store| {
577                data.push(DataStoreSnapshot::new(
578                    path.to_string(),
579                    store.len(),
580                    store.memory_bytes(),
581                ));
582
583                // Track per-entity counts and byte footprint for snapshot output.
584                let mut by_entity = Vec::<(EntityTag, EntityStats)>::new();
585
586                for entry in store.iter() {
587                    let Ok(dk) = DataKey::try_from_raw(entry.key()) else {
588                        corrupted_keys = corrupted_keys.saturating_add(1);
589                        continue;
590                    };
591
592                    let value_len = entry.value().len() as u64;
593
594                    update_default_entity_stats(&mut by_entity, dk.entity_tag(), value_len);
595                }
596
597                for (entity_tag, stats) in by_entity {
598                    entity_storage.push(EntitySnapshot::new(
599                        path.to_string(),
600                        storage_report_default_name_for_entity_tag(db, entity_tag),
601                        stats.entries,
602                        stats.memory_bytes,
603                    ));
604                }
605            });
606
607            // Phase 2: collect index-store snapshots and integrity counters.
608            store_handle.with_index(|store| {
609                let mut user_entries = 0u64;
610                let mut system_entries = 0u64;
611
612                for (key, value) in store.entries() {
613                    let Ok(decoded_key) = IndexKey::try_from_raw(&key) else {
614                        corrupted_entries = corrupted_entries.saturating_add(1);
615                        continue;
616                    };
617
618                    if decoded_key.uses_system_namespace() {
619                        system_entries = system_entries.saturating_add(1);
620                    } else {
621                        user_entries = user_entries.saturating_add(1);
622                    }
623
624                    if value.validate().is_err() {
625                        corrupted_entries = corrupted_entries.saturating_add(1);
626                    }
627                }
628
629                index.push(IndexStoreSnapshot::new(
630                    path.to_string(),
631                    store.len(),
632                    user_entries,
633                    system_entries,
634                    store.memory_bytes(),
635                ));
636            });
637        }
638    });
639
640    // Phase 3: enforce deterministic entity snapshot emission order.
641    // This remains stable even if outer store traversal internals change.
642    entity_storage
643        .sort_by(|left, right| (left.store(), left.path()).cmp(&(right.store(), right.path())));
644
645    Ok(StorageReport::new(
646        data,
647        index,
648        entity_storage,
649        corrupted_keys,
650        corrupted_entries,
651    ))
652}
653
654#[cfg_attr(
655    doc,
656    doc = "Build one deterministic storage snapshot with per-entity rollups.\n\nThis path is read-only and fail-closed on decode/validation errors by counting corrupted keys/entries instead of panicking."
657)]
658pub(crate) fn storage_report<C: CanisterKind>(
659    db: &Db<C>,
660    name_to_path: &[(&'static str, &'static str)],
661) -> Result<StorageReport, InternalError> {
662    db.ensure_recovered_state()?;
663    // Build one optional alias map once, then resolve report names from the
664    // runtime hook table so entity tags keep distinct path identity even when
665    // multiple hooks intentionally share the same model name.
666    let name_map: BTreeMap<&'static str, &str> = name_to_path.iter().copied().collect();
667    let mut tag_name_map = BTreeMap::<EntityTag, &str>::new();
668    for hooks in db.entity_runtime_hooks {
669        tag_name_map
670            .entry(hooks.entity_tag)
671            .or_insert_with(|| storage_report_name_for_hook(&name_map, hooks));
672    }
673    let mut data = Vec::new();
674    let mut index = Vec::new();
675    let mut entity_storage: Vec<EntitySnapshot> = Vec::new();
676    let mut corrupted_keys = 0u64;
677    let mut corrupted_entries = 0u64;
678
679    db.with_store_registry(|reg| {
680        // Keep diagnostics snapshots deterministic by traversing stores in path order.
681        let mut stores = reg.iter().collect::<Vec<_>>();
682        stores.sort_by_key(|(path, _)| *path);
683
684        for (path, store_handle) in stores {
685            // Phase 1: collect data-store snapshots and per-entity stats.
686            store_handle.with_data(|store| {
687                data.push(DataStoreSnapshot::new(
688                    path.to_string(),
689                    store.len(),
690                    store.memory_bytes(),
691                ));
692
693                // Track per-entity counts and byte footprint for snapshot output.
694                let mut by_entity: BTreeMap<EntityTag, EntityStats> = BTreeMap::new();
695
696                for entry in store.iter() {
697                    let Ok(dk) = DataKey::try_from_raw(entry.key()) else {
698                        corrupted_keys = corrupted_keys.saturating_add(1);
699                        continue;
700                    };
701
702                    let value_len = entry.value().len() as u64;
703
704                    by_entity
705                        .entry(dk.entity_tag())
706                        .or_default()
707                        .update(value_len);
708                }
709
710                for (entity_tag, stats) in by_entity {
711                    let path_name = tag_name_map
712                        .get(&entity_tag)
713                        .copied()
714                        .map(str::to_string)
715                        .or_else(|| {
716                            db.runtime_hook_for_entity_tag(entity_tag)
717                                .ok()
718                                .map(|hooks| {
719                                    storage_report_name_for_hook(&name_map, hooks).to_string()
720                                })
721                        })
722                        .unwrap_or_else(|| format!("#{}", entity_tag.value()));
723                    entity_storage.push(EntitySnapshot::new(
724                        path.to_string(),
725                        path_name,
726                        stats.entries,
727                        stats.memory_bytes,
728                    ));
729                }
730            });
731
732            // Phase 2: collect index-store snapshots and integrity counters.
733            store_handle.with_index(|store| {
734                let mut user_entries = 0u64;
735                let mut system_entries = 0u64;
736
737                for (key, value) in store.entries() {
738                    let Ok(decoded_key) = IndexKey::try_from_raw(&key) else {
739                        corrupted_entries = corrupted_entries.saturating_add(1);
740                        continue;
741                    };
742
743                    if decoded_key.uses_system_namespace() {
744                        system_entries = system_entries.saturating_add(1);
745                    } else {
746                        user_entries = user_entries.saturating_add(1);
747                    }
748
749                    if value.validate().is_err() {
750                        corrupted_entries = corrupted_entries.saturating_add(1);
751                    }
752                }
753
754                index.push(IndexStoreSnapshot::new(
755                    path.to_string(),
756                    store.len(),
757                    user_entries,
758                    system_entries,
759                    store.memory_bytes(),
760                ));
761            });
762        }
763    });
764
765    // Phase 3: enforce deterministic entity snapshot emission order.
766    // This remains stable even if outer store traversal internals change.
767    entity_storage
768        .sort_by(|left, right| (left.store(), left.path()).cmp(&(right.store(), right.path())));
769
770    Ok(StorageReport::new(
771        data,
772        index,
773        entity_storage,
774        corrupted_keys,
775        corrupted_entries,
776    ))
777}
778
779#[cfg_attr(
780    doc,
781    doc = "Build one deterministic integrity scan over all registered stores.\n\nThis scan is read-only and classifies findings as:\n- corruption: malformed persisted bytes or inconsistent structural links\n- compatibility: persisted payloads outside decode compatibility windows\n- misuse: unsupported runtime wiring (for example missing entity hooks)"
782)]
783pub(crate) fn integrity_report<C: CanisterKind>(
784    db: &Db<C>,
785) -> Result<IntegrityReport, InternalError> {
786    db.ensure_recovered_state()?;
787
788    integrity_report_after_recovery(db)
789}
790
791#[cfg_attr(
792    doc,
793    doc = "Build one deterministic integrity scan after recovery has already completed.\n\nCallers running inside recovery flow should use this variant to avoid recursive recovery gating."
794)]
795pub(in crate::db) fn integrity_report_after_recovery<C: CanisterKind>(
796    db: &Db<C>,
797) -> Result<IntegrityReport, InternalError> {
798    build_integrity_report(db)
799}
800
801fn build_integrity_report<C: CanisterKind>(db: &Db<C>) -> Result<IntegrityReport, InternalError> {
802    let mut stores = Vec::new();
803    let mut totals = IntegrityTotals::default();
804    let global_live_keys_by_entity = collect_global_live_keys_by_entity(db)?;
805
806    db.with_store_registry(|reg| {
807        // Keep deterministic output order across registry traversal implementations.
808        let mut store_entries = reg.iter().collect::<Vec<_>>();
809        store_entries.sort_by_key(|(path, _)| *path);
810
811        for (path, store_handle) in store_entries {
812            let mut snapshot = IntegrityStoreSnapshot::new(path.to_string());
813            scan_store_forward_integrity(db, store_handle, &mut snapshot)?;
814            scan_store_reverse_integrity(store_handle, &global_live_keys_by_entity, &mut snapshot);
815
816            totals.add_store_snapshot(&snapshot);
817            stores.push(snapshot);
818        }
819
820        Ok::<(), InternalError>(())
821    })?;
822
823    Ok(IntegrityReport::new(stores, totals))
824}
825
826// Build one global map of live data keys grouped by entity across all stores.
827fn collect_global_live_keys_by_entity<C: CanisterKind>(
828    db: &Db<C>,
829) -> Result<BTreeMap<EntityTag, BTreeSet<StorageKey>>, InternalError> {
830    let mut keys = BTreeMap::<EntityTag, BTreeSet<StorageKey>>::new();
831
832    db.with_store_registry(|reg| {
833        for (_, store_handle) in reg.iter() {
834            store_handle.with_data(|data_store| {
835                for entry in data_store.iter() {
836                    if let Ok(data_key) = DataKey::try_from_raw(entry.key()) {
837                        keys.entry(data_key.entity_tag())
838                            .or_default()
839                            .insert(data_key.storage_key());
840                    }
841                }
842            });
843        }
844
845        Ok::<(), InternalError>(())
846    })?;
847
848    Ok(keys)
849}
850
851// Run forward (data -> index) integrity checks for one store.
852fn scan_store_forward_integrity<C: CanisterKind>(
853    db: &Db<C>,
854    store_handle: StoreHandle,
855    snapshot: &mut IntegrityStoreSnapshot,
856) -> Result<(), InternalError> {
857    store_handle.with_data(|data_store| {
858        for entry in data_store.iter() {
859            snapshot.data_rows_scanned = snapshot.data_rows_scanned.saturating_add(1);
860
861            let raw_key = *entry.key();
862
863            let Ok(data_key) = DataKey::try_from_raw(&raw_key) else {
864                snapshot.corrupted_data_keys = snapshot.corrupted_data_keys.saturating_add(1);
865                continue;
866            };
867
868            let hooks = match db.runtime_hook_for_entity_tag(data_key.entity_tag()) {
869                Ok(hooks) => hooks,
870                Err(err) => {
871                    classify_scan_error(err, snapshot)?;
872                    continue;
873                }
874            };
875
876            let marker_row = CommitRowOp::new(
877                hooks.entity_path,
878                raw_key,
879                None,
880                Some(entry.value().as_bytes().to_vec()),
881                crate::db::schema::commit_schema_fingerprint_for_model(
882                    hooks.entity_path,
883                    hooks.model,
884                ),
885            );
886
887            // Validate envelope compatibility before typed preparation so
888            // incompatible persisted formats remain compatibility-classified.
889            if let Err(err) = decode_structural_row_cbor(&entry.value()) {
890                classify_scan_error(err, snapshot)?;
891                continue;
892            }
893
894            let prepared = match db.prepare_row_commit_op(&marker_row) {
895                Ok(prepared) => prepared,
896                Err(err) => {
897                    classify_scan_error(err, snapshot)?;
898                    continue;
899                }
900            };
901
902            for index_op in prepared.index_ops {
903                let Some(expected_value) = index_op.value else {
904                    continue;
905                };
906
907                let actual = index_op
908                    .store
909                    .with_borrow(|index_store| index_store.get(&index_op.key));
910                match actual {
911                    Some(actual_value) if actual_value == expected_value => {}
912                    Some(_) => {
913                        snapshot.divergent_index_entries =
914                            snapshot.divergent_index_entries.saturating_add(1);
915                    }
916                    None => {
917                        snapshot.missing_index_entries =
918                            snapshot.missing_index_entries.saturating_add(1);
919                    }
920                }
921            }
922        }
923
924        Ok::<(), InternalError>(())
925    })
926}
927
928// Run reverse (index -> data) integrity checks for one store.
929fn scan_store_reverse_integrity(
930    store_handle: StoreHandle,
931    live_keys_by_entity: &BTreeMap<EntityTag, BTreeSet<StorageKey>>,
932    snapshot: &mut IntegrityStoreSnapshot,
933) {
934    store_handle.with_index(|index_store| {
935        for (raw_index_key, raw_index_entry) in index_store.entries() {
936            snapshot.index_entries_scanned = snapshot.index_entries_scanned.saturating_add(1);
937
938            let Ok(decoded_index_key) = IndexKey::try_from_raw(&raw_index_key) else {
939                snapshot.corrupted_index_keys = snapshot.corrupted_index_keys.saturating_add(1);
940                continue;
941            };
942
943            let index_entity_tag = data_entity_tag_for_index_key(&decoded_index_key);
944
945            let Ok(indexed_primary_keys) = raw_index_entry.decode_keys() else {
946                snapshot.corrupted_index_entries =
947                    snapshot.corrupted_index_entries.saturating_add(1);
948                continue;
949            };
950
951            for primary_key in indexed_primary_keys {
952                let exists = live_keys_by_entity
953                    .get(&index_entity_tag)
954                    .is_some_and(|entity_keys| entity_keys.contains(&primary_key));
955                if !exists {
956                    snapshot.orphan_index_references =
957                        snapshot.orphan_index_references.saturating_add(1);
958                }
959            }
960        }
961    });
962}
963
964// Map scan-time errors into explicit integrity classification buckets.
965fn classify_scan_error(
966    err: InternalError,
967    snapshot: &mut IntegrityStoreSnapshot,
968) -> Result<(), InternalError> {
969    match err.class() {
970        ErrorClass::Corruption => {
971            snapshot.corrupted_data_rows = snapshot.corrupted_data_rows.saturating_add(1);
972            Ok(())
973        }
974        ErrorClass::IncompatiblePersistedFormat => {
975            snapshot.compatibility_findings = snapshot.compatibility_findings.saturating_add(1);
976            Ok(())
977        }
978        ErrorClass::Unsupported | ErrorClass::NotFound | ErrorClass::Conflict => {
979            snapshot.misuse_findings = snapshot.misuse_findings.saturating_add(1);
980            Ok(())
981        }
982        ErrorClass::Internal | ErrorClass::InvariantViolation => Err(err),
983    }
984}
985
986// Parse the data-entity identity from one decoded index key.
987const fn data_entity_tag_for_index_key(index_key: &IndexKey) -> EntityTag {
988    index_key.index_id().entity_tag
989}