Skip to main content

ic_backup/policy/execution_settlement/
mod.rs

1//! Exact complete Applied local journal admission; no terminal or release authority.
2
3use crate::{
4    model::{
5        attempt_journal::AttemptJournalRecord, execution_settlement::ExecutionSettlementRecord,
6        operation_plan::OperationPlanRecord,
7    },
8    policy::execution_progress::{
9        self, ExecutionProgressError, ExecutionProgressView, OperationProgressState,
10    },
11};
12use thiserror::Error;
13
14/// Admit the original complete causal journal set and match every retained history.
15///
16/// The canonical progress owner checks exact coverage/context/operation/limits and
17/// retained prerequisite evidence; no missing input means unused allowance. Every
18/// operation must be Applied, even when remaining allowances are nonzero. Exact
19/// full history includes negative/uncertain receipts and consumed attempts, not
20/// just the final progress shape. This performs no IO/serialization/transitions or
21/// remote observations. Receipt authentication, dispatch chronology, artifacts,
22/// actual application safety and command quiescence remain separately qualified.
23/// # Errors
24/// Rejects changed plan, incomplete/invalid/unsettled journals or different exact histories.
25pub fn validate(
26    plan: &OperationPlanRecord,
27    journals: &[&AttemptJournalRecord],
28    settlement: &ExecutionSettlementRecord,
29) -> Result<ExecutionProgressView, ExecutionSettlementPolicyError> {
30    validate_journals(plan, journals.iter().copied(), settlement)
31}
32
33pub(crate) fn validate_journals<'a>(
34    plan: &OperationPlanRecord,
35    journals: impl ExactSizeIterator<Item = &'a AttemptJournalRecord>,
36    settlement: &ExecutionSettlementRecord,
37) -> Result<ExecutionProgressView, ExecutionSettlementPolicyError> {
38    if &plan.digest() != settlement.plan_intent() {
39        return Err(ExecutionSettlementPolicyError::IntentMismatch);
40    }
41    let (view, actual) = execution_progress::progress_with_journals(plan, journals)?;
42    if let Some(operation) = view
43        .operations
44        .iter()
45        .find(|operation| operation.state != OperationProgressState::Applied)
46    {
47        return Err(ExecutionSettlementPolicyError::UnsettledOperation(
48            operation.operation_sequence,
49        ));
50    }
51    if settlement.journals().len() != plan.operations().len() {
52        return Err(ExecutionSettlementPolicyError::JournalSetMismatch);
53    }
54    for (row, operation) in settlement.journals().iter().zip(plan.operations()) {
55        if row.operation_sequence() != operation.operation_sequence() {
56            return Err(ExecutionSettlementPolicyError::JournalSetMismatch);
57        }
58        let journal = actual
59            .get(&row.operation_sequence())
60            .ok_or(ExecutionSettlementPolicyError::JournalSetMismatch)?;
61        if &journal.digest() != row.history() {
62            return Err(ExecutionSettlementPolicyError::HistoryMismatch(
63                row.operation_sequence(),
64            ));
65        }
66    }
67    Ok(view)
68}
69/// Typed local settlement denial; no error dispatches, refunds or releases evidence.
70#[derive(Debug, Eq, Error, PartialEq)]
71pub enum ExecutionSettlementPolicyError {
72    /// Checkpoint names another full original plan.
73    #[error("execution settlement original plan mismatch")]
74    IntentMismatch,
75    /// Original complete evidence/causality failed canonical admission.
76    #[error(transparent)]
77    Progress(#[from] ExecutionProgressError),
78    /// Operation has no retained Applied outcome.
79    #[error("execution settlement operation {0} is not applied")]
80    UnsettledOperation(u64),
81    /// Checkpoint rows do not cover exactly the original operation set.
82    #[error("execution settlement journal set mismatch")]
83    JournalSetMismatch,
84    /// Same final progress hides changed exact reservations/receipt evidence.
85    #[error("execution settlement journal {0} history mismatch")]
86    HistoryMismatch(u64),
87}
88#[cfg(test)]
89mod tests;