Skip to main content

ic_backup/model/attempt_journal/
mod.rs

1//! Durable finite local attempt accounting adapted from Canic's pending/receipt contracts.
2
3use crate::model::evidence_digest::EvidenceDigest;
4mod authority;
5mod history;
6pub use authority::{
7    AttemptAuthorityRecord, AttemptBudgetRecord, MAX_OPERATION_ATTEMPTS, OperationBindingRecord,
8    OperationBindingRequest,
9};
10
11use crate::model::{
12    artifacts::{ArtifactChecksumRecord, ChecksumError, canonical_hash},
13    bounded_sequence,
14};
15use history::{AttemptEventRecord, Projection};
16use serde::{Deserialize, Deserializer, Serialize};
17use thiserror::Error;
18
19/// Maximum retained reservation/receipt events in one operation journal.
20pub const MAX_ATTEMPT_EVENTS: usize = 2048;
21/// Maximum encoded input and canonical output bytes admitted by persistence ops.
22pub const MAX_ATTEMPT_JOURNAL_BYTES: u64 = 1024 * 1024;
23
24/// Integration-qualified direct mutation outcome; an invocation failure is insufficient.
25#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
26#[serde(rename_all = "snake_case")]
27pub enum MutationOutcomeRecord {
28    /// Exact requested effect is qualified as applied.
29    Applied,
30    /// Exact requested effect is qualified as not applied; consumed allowance remains spent.
31    NotApplied,
32}
33
34/// Integration-qualified observation outcome for its exact reserved mutation.
35#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
36#[serde(rename_all = "snake_case")]
37pub enum ObservationOutcomeRecord {
38    /// Observation qualifies exact requested effect as applied.
39    Applied,
40    /// Observation qualifies exact requested effect as not applied.
41    NotApplied,
42    /// Settled observation cannot resolve the mutation; no allowance is restored.
43    Uncertain,
44}
45
46/// Passive exact direct-reply evidence supplied by its qualified owner.
47#[derive(Clone, Debug)]
48pub struct MutationReceiptRequest {
49    /// Previously reserved mutation attempt number.
50    pub attempt: u32,
51    /// Exact mutating request digest, checked against immutable authority.
52    pub request: String,
53    /// Qualified effect outcome; local process exit alone cannot produce it.
54    pub outcome: MutationOutcomeRecord,
55    /// Canonical digest of retained owner evidence.
56    pub evidence: String,
57}
58
59/// Passive exact reconciliation-reply evidence supplied by its qualified owner.
60///
61/// The owner qualifies observation completion, custody and paid-effect settlement.
62/// A lost observation reply stays pending; it cannot be recorded as `Uncertain`
63/// merely because the local invocation failed or its response was lost.
64#[derive(Clone, Debug)]
65pub struct ObservationReceiptRequest {
66    /// Previously reserved observation attempt number.
67    pub attempt: u32,
68    /// Exact observation request digest, checked against its reservation.
69    pub request: String,
70    /// Qualified outcome, including explicit unresolved evidence.
71    pub outcome: ObservationOutcomeRecord,
72    /// Canonical digest of retained owner evidence.
73    pub evidence: String,
74}
75
76/// Read-only derived local accounting; does not schedule or authorize effects.
77#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
78pub struct AttemptJournalView {
79    /// Exact immutable authority digest including original budgets.
80    pub authority: ArtifactChecksumRecord,
81    /// Mutation attempts already consumed, without refunds.
82    pub mutations_used: u32,
83    /// Reconciliation observations already consumed, without refunds.
84    pub observations_used: u32,
85    /// Original mutation allowance still unconsumed.
86    pub mutations_remaining: u32,
87    /// Original observation allowance still unconsumed.
88    pub observations_remaining: u32,
89    /// Exact unresolved mutation attempt, if any.
90    pub pending_mutation: Option<u32>,
91    /// Exact unresolved observation attempt, if any.
92    pub pending_observation: Option<u32>,
93    /// Retained qualified application of this operation, not full run completion.
94    pub applied: bool,
95    /// Number of retained reservation and receipt events.
96    pub history_len: usize,
97}
98
99/// Exact locally retained evidence for an Applied operation.
100///
101/// The integration owns the evidence's meaning and qualification. In particular,
102/// observation evidence is not the original mutation reply or retry authority.
103#[derive(Clone, Copy, Debug, Eq, PartialEq)]
104pub enum AppliedReceiptEvidence<'a> {
105    /// The original mutation was settled by its direct qualified receipt.
106    Mutation {
107        /// Original mutation attempt number.
108        attempt: u32,
109        /// Digest of the retained integration-qualified direct evidence.
110        evidence: &'a str,
111    },
112    /// A separately budgeted observation settled the original mutation.
113    Observation {
114        /// Separately reserved observation attempt number.
115        attempt: u32,
116        /// Exact observation request digest from its reservation.
117        request: &'a str,
118        /// Digest of retained integration-qualified observation evidence.
119        evidence: &'a str,
120    },
121}
122
123/// Maintained v1 append-only local reservations and receipts for one exact operation.
124#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
125#[serde(try_from = "JournalFields")]
126pub struct AttemptJournalRecord {
127    version: u16,
128    authority: AttemptAuthorityRecord,
129    events: Vec<AttemptEventRecord>,
130    #[serde(skip)]
131    projection: Projection,
132}
133
134#[derive(Deserialize)]
135#[serde(deny_unknown_fields)]
136struct JournalFields {
137    version: u16,
138    authority: AttemptAuthorityRecord,
139    #[serde(deserialize_with = "bounded_events")]
140    events: Vec<AttemptEventRecord>,
141}
142
143impl TryFrom<JournalFields> for AttemptJournalRecord {
144    type Error = AttemptJournalRecordError;
145    fn try_from(mut fields: JournalFields) -> Result<Self, Self::Error> {
146        if fields.version != 1 {
147            return Err(AttemptJournalRecordError::UnsupportedVersion(
148                fields.version,
149            ));
150        }
151        let mut projection = Projection::empty();
152        for event in &mut fields.events {
153            event.normalize()?;
154            projection.apply(event, fields.authority.budget())?;
155        }
156        Ok(Self {
157            version: 1,
158            authority: fields.authority,
159            events: fields.events,
160            projection,
161        })
162    }
163}
164
165impl AttemptJournalRecord {
166    /// Retain an exact validated authority with no consumed attempts.
167    #[must_use]
168    pub const fn new(authority: AttemptAuthorityRecord) -> Self {
169        Self {
170            version: 1,
171            authority,
172            events: Vec::new(),
173            projection: Projection::empty(),
174        }
175    }
176    /// Read exact declared identity and immutable ceilings.
177    #[must_use]
178    pub const fn authority(&self) -> &AttemptAuthorityRecord {
179        &self.authority
180    }
181    // Keep passive request admission ordered: authority, mutation, then recovery.
182    // Callers retain their own public error types; this grants no dispatch authority.
183    pub(crate) fn validate_pending_mutation<E>(
184        &self,
185        authority: &AttemptAuthorityRecord,
186        mutation_attempt: u32,
187        authority_mismatch: E,
188        mutation_mismatch: E,
189        observation_pending: E,
190    ) -> Result<(), E> {
191        if self.authority() != authority {
192            return Err(authority_mismatch);
193        }
194        if self.pending_mutation() != Some(mutation_attempt) {
195            return Err(mutation_mismatch);
196        }
197        if self.pending_observation().is_some() {
198            return Err(observation_pending);
199        }
200        Ok(())
201    }
202    // Reservation admission reads the validated projection without building the
203    // public reporting view or hashing authority unrelated to these attempt IDs.
204    pub(crate) const fn pending_mutation(&self) -> Option<u32> {
205        self.projection.pending_mutation
206    }
207    pub(crate) const fn pending_observation(&self) -> Option<u32> {
208        match &self.projection.pending_observation {
209            Some(pending) => Some(pending.attempt),
210            None => None,
211        }
212    }
213    /// Read the exact canonical request of the unresolved observation, if any.
214    ///
215    /// This projects existing validated history. It creates no reservation or
216    /// repeat-dispatch permission and returns None after a settled observation.
217    #[must_use]
218    pub fn pending_observation_request(&self) -> Option<&str> {
219        self.projection
220            .pending_observation
221            .as_ref()
222            .map(|pending| pending.request.as_str())
223    }
224    /// Read the terminal Applied receipt from validated original history.
225    ///
226    /// This does not authenticate evidence or grant dispatch, retry or release.
227    #[must_use]
228    pub fn applied_receipt(&self) -> Option<AppliedReceiptEvidence<'_>> {
229        match self.events.last()? {
230            AttemptEventRecord::MutationResolved {
231                mutation,
232                outcome: MutationOutcomeRecord::Applied,
233                evidence,
234            } => Some(AppliedReceiptEvidence::Mutation {
235                attempt: *mutation,
236                evidence,
237            }),
238            AttemptEventRecord::ObservationRecorded {
239                observation,
240                request,
241                outcome: ObservationOutcomeRecord::Applied,
242                evidence,
243            } => Some(AppliedReceiptEvidence::Observation {
244                attempt: *observation,
245                request,
246                evidence,
247            }),
248            _ => None,
249        }
250    }
251    /// Project local evidence without IO or replenishing authority.
252    ///
253    /// Use `Self::applied_receipt` for the terminal receipt or `Self::digest`
254    /// for exact full-history identity.
255    #[must_use]
256    pub fn view(&self) -> AttemptJournalView {
257        AttemptJournalView {
258            authority: self.authority.digest(),
259            mutations_used: self.projection.mutations_used,
260            observations_used: self.projection.observations_used,
261            mutations_remaining: self.authority.budget().mutations()
262                - self.projection.mutations_used,
263            observations_remaining: self.authority.budget().observations()
264                - self.projection.observations_used,
265            pending_mutation: self.projection.pending_mutation,
266            pending_observation: self
267                .projection
268                .pending_observation
269                .as_ref()
270                .map(|pending| pending.attempt),
271            applied: self.projection.applied,
272            history_len: self.events.len(),
273        }
274    }
275    /// Hash full original authority and every canonical chronological reservation/receipt.
276    ///
277    /// The NUL-terminated v1 domain precedes 64 ASCII authority hash bytes, a
278    /// big-endian u64 event count and closed tagged event bytes. Derived projections,
279    /// JSON formatting and filesystem paths are excluded. This authenticates no receipt.
280    #[must_use]
281    pub fn digest(&self) -> ArtifactChecksumRecord {
282        let mut bytes = EvidenceDigest::new(b"ic-backup/attempt-journal-history/v1\0");
283        bytes.append(self.authority.digest().hash().as_bytes());
284        bytes.append(&(self.events.len() as u64).to_be_bytes());
285        for event in &self.events {
286            event.append_digest_bytes(&mut bytes);
287        }
288        bytes.finish()
289    }
290    pub(crate) fn reserve_mutation(&mut self) -> Result<u32, AttemptJournalRecordError> {
291        let attempt = self.projection.next_attempt;
292        self.append(AttemptEventRecord::MutationReserved { attempt })?;
293        Ok(attempt)
294    }
295    pub(crate) fn reserve_observation(
296        &mut self,
297        mutation: u32,
298        request: &str,
299    ) -> Result<u32, AttemptJournalRecordError> {
300        let attempt = self.projection.next_attempt;
301        self.append(AttemptEventRecord::ObservationReserved {
302            attempt,
303            mutation,
304            request: request.to_owned(),
305        })?;
306        Ok(attempt)
307    }
308    pub(crate) fn record_mutation(
309        &mut self,
310        receipt: MutationReceiptRequest,
311    ) -> Result<(), AttemptJournalRecordError> {
312        if canonical_hash(&receipt.request)? != self.authority.binding().request() {
313            return Err(AttemptJournalRecordError::RequestMismatch);
314        }
315        self.append(AttemptEventRecord::MutationResolved {
316            mutation: receipt.attempt,
317            outcome: receipt.outcome,
318            evidence: receipt.evidence,
319        })
320    }
321    pub(crate) fn record_observation(
322        &mut self,
323        receipt: ObservationReceiptRequest,
324    ) -> Result<(), AttemptJournalRecordError> {
325        self.append(AttemptEventRecord::ObservationRecorded {
326            observation: receipt.attempt,
327            request: receipt.request,
328            outcome: receipt.outcome,
329            evidence: receipt.evidence,
330        })
331    }
332    fn append(&mut self, mut event: AttemptEventRecord) -> Result<(), AttemptJournalRecordError> {
333        if self.events.len() == MAX_ATTEMPT_EVENTS {
334            return Err(AttemptJournalRecordError::HistoryTooLarge);
335        }
336        event.normalize()?;
337        let mut projection = self.projection.clone();
338        projection.apply(&event, self.authority.budget())?;
339        self.events.push(event);
340        self.projection = projection;
341        Ok(())
342    }
343}
344
345fn bounded_events<'de, D: Deserializer<'de>>(
346    deserializer: D,
347) -> Result<Vec<AttemptEventRecord>, D::Error> {
348    bounded_sequence::deserialize::<_, _, MAX_ATTEMPT_EVENTS>(
349        deserializer,
350        "a bounded chronological attempt event list",
351        AttemptJournalRecordError::HistoryTooLarge,
352    )
353}
354
355/// Typed exact identity, finite accounting or chronological state rejection.
356#[derive(Debug, Error)]
357pub enum AttemptJournalRecordError {
358    /// Other product generations are not maintained.
359    #[error("unsupported attempt journal version {0}")]
360    UnsupportedVersion(u16),
361    /// Original limits overflow or exceed the bounded total.
362    #[error("attempt allowance exceeds {MAX_OPERATION_ATTEMPTS}")]
363    BudgetTooLarge,
364    /// The chronological metadata count exceeds its finite bound.
365    #[error("attempt history exceeds {MAX_ATTEMPT_EVENTS} events")]
366    HistoryTooLarge,
367    /// A selected principal is not admitted by its owning boundary.
368    #[error("invalid attempt operation principal")]
369    InvalidPrincipal,
370    /// A digest fails exact canonical admission.
371    #[error(transparent)]
372    Checksum(#[from] ChecksumError),
373    /// An unresolved paid mutation blocks another mutation.
374    #[error("mutation attempt {attempt} remains unresolved")]
375    MutationPending {
376        /// Retained unresolved attempt number.
377        attempt: u32,
378    },
379    /// An observation is still unsettled locally.
380    #[error("observation attempt {attempt} remains unresolved")]
381    ObservationPending {
382        /// Retained unresolved attempt number.
383        attempt: u32,
384    },
385    /// A receipt or reconciliation has no corresponding unresolved mutation.
386    #[error("no pending mutation attempt")]
387    NoPendingMutation,
388    /// A reply has no corresponding unresolved observation.
389    #[error("no pending observation attempt")]
390    NoPendingObservation,
391    /// A receipt or sequence targets another attempt.
392    #[error("attempt identity mismatch: expected {expected}, actual {actual}")]
393    AttemptMismatch {
394        /// Expected exact attempt number.
395        expected: u32,
396        /// Rejected attempt number.
397        actual: u32,
398    },
399    /// Reply/request identity differs from its retained exact reservation.
400    #[error("attempt request digest mismatch")]
401    RequestMismatch,
402    /// Original mutation authority has been consumed.
403    #[error("mutation attempt allowance exhausted")]
404    MutationBudgetExhausted,
405    /// Original observation authority has been consumed.
406    #[error("observation attempt allowance exhausted")]
407    ObservationBudgetExhausted,
408    /// This exact operation is already qualified as applied.
409    #[error("operation application already retained")]
410    AlreadyApplied,
411}
412
413#[cfg(test)]
414mod tests;