icydb-core 0.212.1

IcyDB — A schema-first typed query engine and persistence runtime for Internet Computer canisters
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
//! Module: db::integrity::job
//! Responsibility: invariant-bearing Deep job, checkpoint, and receipt vocabulary.
//! Does not own: stable storage, physical traversal, authorization, or time.
//! Boundary: Deep controller <-> current-form progress record codec.

use crate::db::{
    codec::hex::encode_hex_lower,
    integrity::{
        DatabaseIncarnationId, IntegrityAuthorityDiagnostic, IntegrityEntityIdentity,
        IntegrityFinding, IntegrityPhase, IntegrityProofVector, IntegrityResourceDiagnostic,
        IntegrityVerifierFamily, MAX_INTEGRITY_PATH_BYTES, PhysicalUnitCheckpoint,
        StorageTraversalCorruption,
    },
    journal::JournalInspectionCheckpoint,
};
use candid::CandidType;
use serde::Deserialize;

pub(in crate::db) const MAX_INTEGRITY_OWNER_BYTES: usize = 256;
pub(in crate::db) const MAX_INTEGRITY_SUBMISSION_KEY_BYTES: usize = 256;
const MAX_INTEGRITY_RECEIPT_FINDINGS: usize = 64;
pub(in crate::db) const MAX_INTEGRITY_IN_PROGRESS_PAGES: u64 = u64::MAX - 1;

/// Opaque lookup identity for one retained Deep inspection job.

#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct IntegrityJobId([u8; 32]);

impl IntegrityJobId {
    /// Admit one nonzero current-form lookup identity.
    ///
    /// # Errors
    ///
    /// Returns [`IntegrityJobError::CorruptProgressRecord`] when `bytes` is
    /// the reserved all-zero identity.
    pub fn try_from_bytes(bytes: [u8; 32]) -> Result<Self, IntegrityJobError> {
        if bytes == [0; 32] {
            return Err(IntegrityJobError::CorruptProgressRecord);
        }
        Ok(Self(bytes))
    }

    /// Decode one exact lowercase or uppercase hexadecimal job identity.
    ///
    /// # Errors
    ///
    /// Returns [`IntegrityJobError::InvalidJobId`] unless `value` contains
    /// exactly 64 hexadecimal characters and decodes to a nonzero identity.
    pub fn try_from_hex(value: &str) -> Result<Self, IntegrityJobError> {
        if value.len() != 64 {
            return Err(IntegrityJobError::InvalidJobId);
        }

        let mut bytes = [0_u8; 32];
        for (index, pair) in value.as_bytes().chunks_exact(2).enumerate() {
            let high = decode_hex_nibble(pair[0]).ok_or(IntegrityJobError::InvalidJobId)?;
            let low = decode_hex_nibble(pair[1]).ok_or(IntegrityJobError::InvalidJobId)?;
            bytes[index] = (high << 4) | low;
        }
        if bytes == [0; 32] {
            return Err(IntegrityJobError::InvalidJobId);
        }

        Ok(Self(bytes))
    }

    /// Return the canonical lookup bytes.
    #[must_use]
    pub const fn to_bytes(self) -> [u8; 32] {
        self.0
    }

    /// Render the canonical lowercase hexadecimal SQL/shell identity.
    #[must_use]
    pub fn to_hex(self) -> String {
        encode_hex_lower(&self.0)
    }

    /// Revalidate a possibly deserialized job identity before lookup.
    pub(in crate::db) fn validate(self) -> Result<(), IntegrityJobError> {
        if self.0 == [0; 32] {
            return Err(IntegrityJobError::InvalidJobId);
        }
        Ok(())
    }
}

const fn decode_hex_nibble(value: u8) -> Option<u8> {
    match value {
        b'0'..=b'9' => Some(value - b'0'),
        b'a'..=b'f' => Some(value - b'a' + 10),
        b'A'..=b'F' => Some(value - b'A' + 10),
        _ => None,
    }
}

/// Bounded authorization identity persisted with one job.

#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
pub struct IntegrityJobOwner(String);

impl IntegrityJobOwner {
    /// Admit one nonempty bounded owner identity.
    ///
    /// # Errors
    ///
    /// Returns [`IntegrityJobError::InvalidOwner`] when the identity is empty
    /// or exceeds the protocol bound.
    pub fn new(value: impl Into<String>) -> Result<Self, IntegrityJobError> {
        let value = value.into();
        if value.is_empty() || value.len() > MAX_INTEGRITY_OWNER_BYTES {
            return Err(IntegrityJobError::InvalidOwner);
        }
        Ok(Self(value))
    }

    /// Borrow the canonical owner identity.
    #[must_use]
    pub const fn as_str(&self) -> &str {
        self.0.as_str()
    }

    /// Revalidate a possibly deserialized owner before authorization.
    pub(in crate::db) const fn validate(&self) -> Result<(), IntegrityJobError> {
        if self.0.is_empty() || self.0.len() > MAX_INTEGRITY_OWNER_BYTES {
            return Err(IntegrityJobError::InvalidOwner);
        }
        Ok(())
    }
}

/// Bounded client idempotency identity for Deep start.

#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
pub struct IntegritySubmissionKey(String);

impl IntegritySubmissionKey {
    /// Admit one nonempty bounded submission key.
    ///
    /// # Errors
    ///
    /// Returns [`IntegrityJobError::InvalidSubmissionKey`] when the key is
    /// empty or exceeds the protocol bound.
    pub fn new(value: impl Into<String>) -> Result<Self, IntegrityJobError> {
        let value = value.into();
        if value.is_empty() || value.len() > MAX_INTEGRITY_SUBMISSION_KEY_BYTES {
            return Err(IntegrityJobError::InvalidSubmissionKey);
        }
        Ok(Self(value))
    }

    /// Borrow the canonical submission key.
    #[must_use]
    pub const fn as_str(&self) -> &str {
        self.0.as_str()
    }

    /// Revalidate a possibly deserialized key before job identity derivation.
    pub(in crate::db) const fn validate(&self) -> Result<(), IntegrityJobError> {
        if self.0.is_empty() || self.0.len() > MAX_INTEGRITY_SUBMISSION_KEY_BYTES {
            return Err(IntegrityJobError::InvalidSubmissionKey);
        }
        Ok(())
    }
}

/// Exact private continuation for the current Deep phase.
#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
pub(in crate::db) enum IntegrityCheckpoint {
    /// Bounded accepted metadata and control closure.
    QuickMetadata,
    /// Canonical source-row interval.
    Rows(PhysicalUnitCheckpoint),
    /// One active forward-index domain in accepted plan order.
    Index {
        ordinal: u32,
        checkpoint: PhysicalUnitCheckpoint,
    },
    /// One active source-owned reverse domain in accepted plan order.
    ReverseRelation {
        ordinal: u32,
        checkpoint: PhysicalUnitCheckpoint,
    },
    /// One participating journal tail in canonical store order.
    Journal {
        store_ordinal: u32,
        checkpoint: JournalInspectionCheckpoint,
    },
    /// No physical traversal remains; only final proof equality may complete.
    FinalProof,
}

impl IntegrityCheckpoint {
    /// Return the canonical phase implied by this checkpoint.
    #[must_use]
    pub(in crate::db) const fn phase(&self) -> IntegrityPhase {
        match self {
            Self::QuickMetadata => IntegrityPhase::QuickMetadata,
            Self::Rows(_) => IntegrityPhase::Rows,
            Self::Index { .. } => IntegrityPhase::IndexEntries,
            Self::ReverseRelation { .. } => IntegrityPhase::ReverseRelations,
            Self::Journal { .. } => IntegrityPhase::JournalTails,
            Self::FinalProof => IntegrityPhase::FinalProofVectorCheck,
        }
    }
}

/// Frozen intent that supersedes advancement after the outstanding page is acknowledged.

#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
pub enum IntegrityPendingTerminal {
    /// The inactivity lease elapsed.
    Expired,
    /// The authorized owner requested abort.
    Aborted,
}

/// Stable terminal meaning of a completed Deep job.

#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
pub enum IntegrityTerminalOutcome {
    /// Every phase exhausted cleanly under one unchanged proof.
    DeepCompleteClean,
    /// Every phase exhausted with one or more definite findings.
    DeepCompleteWithFindings,
    /// One proof component changed before completion.
    Invalidated,
    /// Accepted authority could not be inspected.
    Uninspectable(IntegrityAuthorityDiagnostic),
    /// A load-bearing physical traversal could not prove progress.
    UninspectableStorage(StorageTraversalCorruption),
    /// One frozen bounded resource was insufficient.
    ResourceLimited(IntegrityResourceDiagnostic),
    /// The inactivity lease expired.
    Expired,
    /// The authorized owner aborted the job.
    Aborted,
}

/// Durable job lifecycle state.
#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
pub(in crate::db) enum IntegrityJobState {
    /// Physical advancement remains permitted.
    InProgress,
    /// Advancement is frozen while the last page remains unacknowledged.
    TerminalPending(IntegrityPendingTerminal),
    /// One final receipt is retained for replay and acknowledgement.
    Terminal {
        outcome: IntegrityTerminalOutcome,
        receipt_acknowledged: bool,
    },
}

/// Semantic status carried by one bounded Deep page.

#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
pub enum DeepIntegrityPageStatus {
    /// More physical work or the final proof check remains.
    InProgress,
    /// This receipt records the stable terminal result.
    Terminal(IntegrityTerminalOutcome),
}

/// One bounded replayable Deep result page.

#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
pub struct DeepIntegrityPage {
    pub(super) job_id: IntegrityJobId,
    pub(super) page_sequence: u64,
    pub(super) phase: IntegrityPhase,
    pub(super) status: DeepIntegrityPageStatus,
    pub(super) pages_completed: u64,
    pub(super) findings_seen: u64,
    pub(super) findings: Vec<IntegrityFinding>,
    pub(super) blocked_verifier_families: Vec<IntegrityVerifierFamily>,
}

impl DeepIntegrityPage {
    /// Return the opaque job lookup identity.
    #[must_use]
    pub const fn job_id(&self) -> IntegrityJobId {
        self.job_id
    }

    /// Return the monotonically increasing receipt sequence.
    #[must_use]
    pub const fn page_sequence(&self) -> u64 {
        self.page_sequence
    }

    /// Return the phase represented by this receipt.
    #[must_use]
    pub const fn phase(&self) -> IntegrityPhase {
        self.phase
    }

    /// Borrow the current or terminal status.
    #[must_use]
    pub const fn status(&self) -> &DeepIntegrityPageStatus {
        &self.status
    }

    /// Return cumulative successfully persisted page count.
    #[must_use]
    pub const fn pages_completed(&self) -> u64 {
        self.pages_completed
    }

    /// Return cumulative definite findings.
    #[must_use]
    pub const fn findings_seen(&self) -> u64 {
        self.findings_seen
    }

    /// Borrow findings produced only by this page.
    #[must_use]
    pub const fn findings(&self) -> &[IntegrityFinding] {
        self.findings.as_slice()
    }

    /// Borrow the cumulative canonical blocked-family set.
    #[must_use]
    pub const fn blocked_verifier_families(&self) -> &[IntegrityVerifierFamily] {
        self.blocked_verifier_families.as_slice()
    }
}

/// Abort receipt status.

#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
pub enum IntegrityAbortStatus {
    /// Abort is frozen but cannot replace the outstanding page yet.
    TerminationPending(IntegrityPendingTerminal),
    /// The terminal abort result is replayable.
    Terminal(IntegrityTerminalOutcome),
}

/// One bounded abort/expiry receipt.

#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
pub struct IntegrityAbortReceipt {
    pub(super) job_id: IntegrityJobId,
    pub(super) page_sequence: u64,
    pub(super) status: IntegrityAbortStatus,
}

impl IntegrityAbortReceipt {
    /// Return the opaque job identity.
    #[must_use]
    pub const fn job_id(&self) -> IntegrityJobId {
        self.job_id
    }

    /// Return the outstanding or terminal receipt sequence.
    #[must_use]
    pub const fn page_sequence(&self) -> u64 {
        self.page_sequence
    }

    /// Borrow the pending or terminal abort status.
    #[must_use]
    pub const fn status(&self) -> &IntegrityAbortStatus {
        &self.status
    }
}

/// Persisted receipt body.

#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
pub enum IntegrityJobReceipt {
    /// Normal start, advancement, or completion page.
    Page(DeepIntegrityPage),
    /// Abort/expiry terminal or pending acknowledgement.
    Abort(IntegrityAbortReceipt),
}

impl IntegrityJobReceipt {
    /// Return the job identity carried by this receipt.
    #[must_use]
    pub const fn job_id(&self) -> IntegrityJobId {
        match self {
            Self::Page(page) => page.job_id,
            Self::Abort(receipt) => receipt.job_id,
        }
    }

    /// Return the sequence carried by this receipt.
    #[must_use]
    pub const fn page_sequence(&self) -> u64 {
        match self {
            Self::Page(page) => page.page_sequence,
            Self::Abort(receipt) => receipt.page_sequence,
        }
    }
}

/// Request identity that produced the cached receipt.
#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
pub(in crate::db) enum IntegrityReceiptReplayKey {
    /// Initial Deep start.
    Start,
    /// Continue request acknowledging the named prior sequence.
    Continue { acknowledged_sequence: u64 },
}

/// One cached bounded receipt and its exact replay request.
#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
pub(in crate::db) struct IntegrityReceiptEnvelope {
    pub(super) replay_key: IntegrityReceiptReplayKey,
    pub(super) receipt: IntegrityJobReceipt,
}

/// Current invariant-bearing durable Deep record.
#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
pub(in crate::db) struct IntegrityJob {
    pub(super) id: IntegrityJobId,
    pub(super) database_incarnation_id: DatabaseIncarnationId,
    pub(super) owner: IntegrityJobOwner,
    pub(super) submission_key: IntegritySubmissionKey,
    pub(super) entity: IntegrityEntityIdentity,
    pub(super) accepted_schema_version: u32,
    pub(super) accepted_schema_fingerprint: [u8; 16],
    pub(super) inspection_plan_fingerprint: [u8; 32],
    pub(super) checkpoint: IntegrityCheckpoint,
    pub(super) captured_proof_vector: IntegrityProofVector,
    pub(super) state: IntegrityJobState,
    pub(super) lease_deadline_nanos: u64,
    pub(super) findings_seen: u64,
    pub(super) pages_completed: u64,
    pub(super) blocked_verifier_families: Vec<IntegrityVerifierFamily>,
    pub(super) last_receipt: IntegrityReceiptEnvelope,
}

impl IntegrityJob {
    /// Validate all persisted cross-field invariants before use.
    pub(super) fn validate(&self) -> Result<(), IntegrityJobError> {
        if self.id != self.last_receipt.receipt.job_id()
            || self.database_incarnation_id != self.captured_proof_vector.database_incarnation_id()
            || self.accepted_schema_version != self.captured_proof_vector.accepted_schema_version()
            || self.accepted_schema_fingerprint
                != self.captured_proof_vector.accepted_schema_fingerprint()
            || self.inspection_plan_fingerprint
                != self.captured_proof_vector.inspection_plan_fingerprint()
            || self.entity.entity_path().is_empty()
            || self.entity.entity_path().len() > MAX_INTEGRITY_PATH_BYTES
            || self.entity.store_path().is_empty()
            || self.entity.store_path().len() > MAX_INTEGRITY_PATH_BYTES
            || self.entity.entity_tag() == 0
            || self.owner.as_str().is_empty()
            || self.owner.as_str().len() > MAX_INTEGRITY_OWNER_BYTES
            || self.submission_key.as_str().is_empty()
            || self.submission_key.as_str().len() > MAX_INTEGRITY_SUBMISSION_KEY_BYTES
            || self.accepted_schema_version == 0
            || self.lease_deadline_nanos == 0
            || matches!(
                self.state,
                IntegrityJobState::InProgress | IntegrityJobState::TerminalPending(_)
            ) && self.pages_completed > MAX_INTEGRITY_IN_PROGRESS_PAGES
            || !strictly_sorted_unique(&self.blocked_verifier_families)
            || self.captured_proof_vector.validate().is_err()
            || !self.checkpoint_is_well_formed()
        {
            return Err(IntegrityJobError::CorruptProgressRecord);
        }

        let receipt_matches_state = match (&self.state, &self.last_receipt.receipt) {
            (
                IntegrityJobState::InProgress | IntegrityJobState::TerminalPending(_),
                IntegrityJobReceipt::Page(page),
            ) => {
                page.status == DeepIntegrityPageStatus::InProgress
                    && page.phase == self.checkpoint.phase()
                    && self.page_matches_counters(page)
            }
            (IntegrityJobState::Terminal { outcome, .. }, IntegrityJobReceipt::Page(page)) => {
                page.status == DeepIntegrityPageStatus::Terminal(outcome.clone())
                    && page.phase == self.checkpoint.phase()
                    && !matches!(
                        outcome,
                        IntegrityTerminalOutcome::Expired | IntegrityTerminalOutcome::Aborted
                    )
                    && self.page_matches_counters(page)
            }
            (IntegrityJobState::Terminal { outcome, .. }, IntegrityJobReceipt::Abort(receipt)) => {
                receipt.status == IntegrityAbortStatus::Terminal(outcome.clone())
                    && matches!(
                        outcome,
                        IntegrityTerminalOutcome::Expired | IntegrityTerminalOutcome::Aborted
                    )
            }
            _ => false,
        };
        if !receipt_matches_state
            || self.last_receipt.receipt.page_sequence() != self.pages_completed
            || !self.replay_key_matches_receipt()
            || !self.terminal_outcome_matches_counts()
        {
            return Err(IntegrityJobError::CorruptProgressRecord);
        }

        Ok(())
    }

    fn page_matches_counters(&self, page: &DeepIntegrityPage) -> bool {
        page.pages_completed == self.pages_completed
            && page.findings_seen == self.findings_seen
            && page.findings.len() <= MAX_INTEGRITY_RECEIPT_FINDINGS
            && u64::try_from(page.findings.len()).is_ok_and(|count| count <= self.findings_seen)
            && page.blocked_verifier_families == self.blocked_verifier_families
    }

    fn replay_key_matches_receipt(&self) -> bool {
        match self.last_receipt.replay_key {
            IntegrityReceiptReplayKey::Start => {
                self.pages_completed == 0
                    && self.last_receipt.receipt.page_sequence() == 0
                    && matches!(
                        &self.last_receipt.receipt,
                        IntegrityJobReceipt::Page(DeepIntegrityPage {
                            status: DeepIntegrityPageStatus::InProgress,
                            ..
                        })
                    )
            }
            IntegrityReceiptReplayKey::Continue {
                acknowledged_sequence,
            } => acknowledged_sequence
                .checked_add(1)
                .is_some_and(|sequence| sequence == self.last_receipt.receipt.page_sequence()),
        }
    }

    const fn terminal_outcome_matches_counts(&self) -> bool {
        match &self.state {
            IntegrityJobState::Terminal {
                outcome: IntegrityTerminalOutcome::DeepCompleteClean,
                ..
            } => self.findings_seen == 0 && self.blocked_verifier_families.is_empty(),
            IntegrityJobState::Terminal {
                outcome: IntegrityTerminalOutcome::DeepCompleteWithFindings,
                ..
            } => self.findings_seen > 0 || !self.blocked_verifier_families.is_empty(),
            _ => true,
        }
    }

    fn checkpoint_is_well_formed(&self) -> bool {
        match &self.checkpoint {
            IntegrityCheckpoint::Rows(checkpoint) => row_checkpoint_is_well_formed(checkpoint),
            IntegrityCheckpoint::Index {
                ordinal,
                checkpoint,
            } => {
                usize::try_from(*ordinal).is_ok_and(|ordinal| {
                    ordinal < self.captured_proof_vector.index_generation_count()
                }) && index_checkpoint_is_well_formed(checkpoint)
            }
            IntegrityCheckpoint::ReverseRelation {
                ordinal,
                checkpoint,
            } => {
                usize::try_from(*ordinal).is_ok_and(|ordinal| {
                    ordinal < self.captured_proof_vector.relation_generation_count()
                }) && reverse_checkpoint_is_well_formed(checkpoint)
            }
            IntegrityCheckpoint::Journal {
                store_ordinal,
                checkpoint,
            } => usize::try_from(*store_ordinal)
                .ok()
                .and_then(|ordinal| self.captured_proof_vector.stores().get(ordinal))
                .is_some_and(|proof| {
                    let (fold_sequence, next_append_sequence) = proof.journal_interval();
                    journal_checkpoint_is_well_formed(
                        checkpoint,
                        fold_sequence,
                        next_append_sequence,
                    )
                }),
            IntegrityCheckpoint::QuickMetadata | IntegrityCheckpoint::FinalProof => true,
        }
    }
}

fn row_checkpoint_is_well_formed(checkpoint: &PhysicalUnitCheckpoint) -> bool {
    checkpoint.raw_data_key().is_ok()
        && !matches!(
            checkpoint,
            PhysicalUnitCheckpoint::Within {
                verifier_family: IntegrityVerifierFamily::IndexEntry
                    | IntegrityVerifierFamily::UniqueIndex
                    | IntegrityVerifierFamily::ReverseRelationEntry
                    | IntegrityVerifierFamily::JournalEnvelope
                    | IntegrityVerifierFamily::JournalBatchIdentity,
                ..
            }
        )
}

fn index_checkpoint_is_well_formed(checkpoint: &PhysicalUnitCheckpoint) -> bool {
    checkpoint.raw_index_key().is_ok()
        && !matches!(
            checkpoint,
            PhysicalUnitCheckpoint::Within {
                verifier_family: IntegrityVerifierFamily::DataKey
                    | IntegrityVerifierFamily::RowEnvelope
                    | IntegrityVerifierFamily::FieldValue
                    | IntegrityVerifierFamily::PrimaryKey
                    | IntegrityVerifierFamily::ValidatedConstraints
                    | IntegrityVerifierFamily::ForwardIndex
                    | IntegrityVerifierFamily::Relation
                    | IntegrityVerifierFamily::ReverseRelationEntry
                    | IntegrityVerifierFamily::JournalEnvelope
                    | IntegrityVerifierFamily::JournalBatchIdentity,
                ..
            }
        )
}

fn reverse_checkpoint_is_well_formed(checkpoint: &PhysicalUnitCheckpoint) -> bool {
    checkpoint.raw_index_key().is_ok()
        && !matches!(
            checkpoint,
            PhysicalUnitCheckpoint::Within {
                verifier_family: IntegrityVerifierFamily::DataKey
                    | IntegrityVerifierFamily::RowEnvelope
                    | IntegrityVerifierFamily::FieldValue
                    | IntegrityVerifierFamily::PrimaryKey
                    | IntegrityVerifierFamily::ValidatedConstraints
                    | IntegrityVerifierFamily::ForwardIndex
                    | IntegrityVerifierFamily::Relation
                    | IntegrityVerifierFamily::IndexEntry
                    | IntegrityVerifierFamily::UniqueIndex
                    | IntegrityVerifierFamily::JournalEnvelope
                    | IntegrityVerifierFamily::JournalBatchIdentity,
                ..
            }
        )
}

const fn journal_checkpoint_is_well_formed(
    checkpoint: &JournalInspectionCheckpoint,
    fold_sequence: u64,
    next_append_sequence: u64,
) -> bool {
    match checkpoint {
        JournalInspectionCheckpoint::BeforeFirst => true,
        JournalInspectionCheckpoint::BeforeBatch { sequence } => {
            *sequence > fold_sequence && *sequence < next_append_sequence
        }
        JournalInspectionCheckpoint::CheckingBatchIdentity {
            sequence,
            next_prior_sequence,
            ..
        } => {
            *sequence > fold_sequence
                && *sequence < next_append_sequence
                && *next_prior_sequence > fold_sequence
                && *next_prior_sequence < *sequence
        }
        JournalInspectionCheckpoint::AfterBatch { sequence } => {
            *sequence >= fold_sequence && *sequence < next_append_sequence
        }
    }
}

fn strictly_sorted_unique(values: &[IntegrityVerifierFamily]) -> bool {
    values.windows(2).all(|pair| pair[0] < pair[1])
}

/// Typed Deep protocol or progress-record failure.

#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
pub enum IntegrityJobError {
    /// The bounded progress store cannot admit another job.
    CapacityExceeded,

    /// The progress-store header is malformed.
    CorruptProgressHeader,

    /// The retained job record violates its current-form contract.
    CorruptProgressRecord,

    /// A checked protocol counter cannot advance.
    CounterExhausted,

    /// The accepted entity selector no longer matches runtime authority.
    EntityIdentityMismatch,

    /// The progress store uses an unsupported persisted format.
    IncompatibleProgressFormat,

    /// An internal Deep controller operation failed.
    Internal,

    /// The accepted entity selector is malformed.
    InvalidEntityIdentity,

    /// A caller-authored job identity is malformed or reserved.
    InvalidJobId,

    /// The authorization owner is empty or exceeds its bound.
    InvalidOwner,

    /// The idempotency key is empty or exceeds its bound.
    InvalidSubmissionKey,

    /// Another bounded advancement currently owns the job.
    JobBusy,

    /// The job belongs to another database incarnation.
    JobIncarnationMismatch,

    /// No retained job has the supplied identity.
    JobNotFound,

    /// The supplied authorization owner does not own the job.
    JobOwnerMismatch,

    /// The acknowledgement does not name the outstanding receipt.
    StaleAcknowledgement,

    /// A retried start names a job that already advanced.
    SubmissionAlreadyAdvanced,

    /// An owner/key pair was reused for a different target.
    SubmissionConflict,
}

/// Deep protocol failures keep persisted-protocol and engine causes distinct.

#[derive(Debug)]
pub enum IntegrityDeepError {
    /// Accepted authority or physical execution failed.
    Internal(crate::error::InternalError),

    /// Stable job/progress protocol rejected the request.
    Job(IntegrityJobError),
}

impl From<IntegrityJobError> for IntegrityDeepError {
    fn from(error: IntegrityJobError) -> Self {
        Self::Job(error)
    }
}

impl From<crate::error::InternalError> for IntegrityDeepError {
    fn from(error: crate::error::InternalError) -> Self {
        Self::Internal(error)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn public_job_inputs_revalidate_after_wire_decode() {
        let owner_bytes =
            candid::encode_one(IntegrityJobOwner(String::new())).expect("owner should encode");
        let owner: IntegrityJobOwner =
            candid::decode_one(&owner_bytes).expect("owner should decode");
        assert_eq!(owner.validate(), Err(IntegrityJobError::InvalidOwner));

        let submission_bytes = candid::encode_one(IntegritySubmissionKey(String::new()))
            .expect("submission key should encode");
        let submission: IntegritySubmissionKey =
            candid::decode_one(&submission_bytes).expect("submission key should decode");
        assert_eq!(
            submission.validate(),
            Err(IntegrityJobError::InvalidSubmissionKey),
        );

        let job_id_bytes =
            candid::encode_one(IntegrityJobId([0; 32])).expect("job id should encode");
        let job_id: IntegrityJobId =
            candid::decode_one(&job_id_bytes).expect("job id should decode");
        assert_eq!(job_id.validate(), Err(IntegrityJobError::InvalidJobId),);
    }

    #[test]
    fn public_job_id_hex_round_trip_is_exact_and_fail_closed() {
        let mut bytes = [0_u8; 32];
        bytes[0] = 0x01;
        bytes[31] = 0xfe;
        let job_id = IntegrityJobId::try_from_bytes(bytes).expect("job id should admit");
        let encoded = job_id.to_hex();

        assert_eq!(encoded.len(), 64);
        assert_eq!(IntegrityJobId::try_from_hex(encoded.as_str()), Ok(job_id),);
        assert_eq!(
            IntegrityJobId::try_from_hex(encoded.to_uppercase().as_str()),
            Ok(job_id),
        );
        for malformed in [
            "",
            "01",
            "0000000000000000000000000000000000000000000000000000000000000000",
            "g001000000000000000000000000000000000000000000000000000000000000",
        ] {
            assert_eq!(
                IntegrityJobId::try_from_hex(malformed),
                Err(IntegrityJobError::InvalidJobId),
            );
        }
    }

    #[test]
    fn persisted_checkpoint_families_stay_phase_owned() {
        let journal_in_row = PhysicalUnitCheckpoint::Within {
            physical_key: vec![1],
            verifier_family: IntegrityVerifierFamily::JournalEnvelope,
            ordinal: 0,
        };
        let row_in_index = PhysicalUnitCheckpoint::Within {
            physical_key: vec![1],
            verifier_family: IntegrityVerifierFamily::FieldValue,
            ordinal: 0,
        };
        let reverse_in_reverse = PhysicalUnitCheckpoint::Within {
            physical_key: vec![1],
            verifier_family: IntegrityVerifierFamily::ReverseRelationEntry,
            ordinal: 0,
        };

        assert!(!row_checkpoint_is_well_formed(&journal_in_row));
        assert!(!index_checkpoint_is_well_formed(&row_in_index));
        assert!(reverse_checkpoint_is_well_formed(&reverse_in_reverse));
    }

    #[test]
    fn persisted_journal_checkpoint_cannot_skip_the_captured_tail_interval() {
        assert!(journal_checkpoint_is_well_formed(
            &JournalInspectionCheckpoint::BeforeFirst,
            4,
            8,
        ));
        assert!(journal_checkpoint_is_well_formed(
            &JournalInspectionCheckpoint::BeforeBatch { sequence: 7 },
            4,
            8,
        ));
        assert!(journal_checkpoint_is_well_formed(
            &JournalInspectionCheckpoint::AfterBatch { sequence: 4 },
            4,
            8,
        ));
        assert!(!journal_checkpoint_is_well_formed(
            &JournalInspectionCheckpoint::BeforeBatch { sequence: 4 },
            4,
            8,
        ));
        assert!(!journal_checkpoint_is_well_formed(
            &JournalInspectionCheckpoint::AfterBatch { sequence: 8 },
            4,
            8,
        ));
        assert!(!journal_checkpoint_is_well_formed(
            &JournalInspectionCheckpoint::CheckingBatchIdentity {
                sequence: 7,
                batch_id: [1; 16],
                next_prior_sequence: 4,
            },
            4,
            8,
        ));
    }
}