delivery_state.rs (12055B)
1 //! Bounded stock delivery projections; original outcomes remain separately paged. 2 use super::*; 3 use crate::command::{DeliveryIntent, DeliveryObservationKind}; 4 use core::{fmt, num::NonZeroU64}; 5 use radroots_domain::Sequence; 6 use radroots_protocol::intent::ArtifactRole; 7 use radroots_transport::{ 8 outcome::DeliveryOutcome, 9 policy::{SatisfactionState, evaluate_satisfaction}, 10 target::TargetFingerprint, 11 }; 12 13 /// One original endpoint's bounded progress. Counts are potential invocations; 14 /// acceptance is a relay observation, never physical receipt or business state. 15 #[derive(Clone, Eq, PartialEq)] 16 pub struct StockDeliveryTargetProgress { 17 target: TargetFingerprint, 18 attempts: u8, 19 accepted: bool, 20 last: Option<DeliveryObservationKind>, 21 observed: Option<NonZeroU64>, 22 } 23 impl StockDeliveryTargetProgress { 24 pub fn from_store_report( 25 target: TargetFingerprint, 26 attempts: u8, 27 accepted: bool, 28 last: Option<DeliveryObservationKind>, 29 observed: Option<NonZeroU64>, 30 ) -> Result<Self, OperationError> { 31 if attempts > 12 32 || (attempts == 0 && (accepted || last.is_some())) 33 || (accepted && last.is_none()) 34 || (last == Some(DeliveryObservationKind::Accepted) && !accepted) 35 || (last.is_none() && observed.is_some()) 36 || observed.is_some_and(|time| time.get() > 253_402_300_799_999) 37 { 38 return Err(OperationError::RecoveryRequired); 39 } 40 Ok(Self { 41 target, 42 attempts, 43 accepted, 44 last, 45 observed, 46 }) 47 } 48 pub const fn target(&self) -> &TargetFingerprint { 49 &self.target 50 } 51 pub const fn potential_attempts(&self) -> u8 { 52 self.attempts 53 } 54 pub const fn accepted(&self) -> bool { 55 self.accepted 56 } 57 pub const fn last_observation_kind(&self) -> Option<DeliveryObservationKind> { 58 self.last 59 } 60 pub const fn last_observed_at_unix_ms(&self) -> Option<NonZeroU64> { 61 self.observed 62 } 63 } 64 impl fmt::Debug for StockDeliveryTargetProgress { 65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 66 f.write_str("StockDeliveryTargetProgress(<redacted>)") 67 } 68 } 69 70 /// At most sixteen endpoint summaries and one original publication policy. 71 /// No collection duplicates the signed outer for every retained attempt. 72 #[derive(Clone, Eq, PartialEq)] 73 pub struct StockDeliverySummary { 74 source: Option<StockEnvelopeReceipt>, 75 first: Option<DeliveryIntent>, 76 policy_source: Option<StockEnvelopeReceipt>, 77 attempts: u16, 78 selected_attempts: u16, 79 targets: Box<[StockDeliveryTargetProgress]>, 80 checkpoint: InventoryCheckpointRef, 81 } 82 impl StockDeliverySummary { 83 /// Validates report shape only; current access and original protected history 84 /// must be authenticated by the concrete owner in the actual read transaction. 85 pub fn from_store_report( 86 request: &StockEnvelopeRequest, 87 source: Option<StockEnvelopeReceipt>, 88 first: Option<DeliveryIntent>, 89 attempts: u16, 90 targets: Vec<StockDeliveryTargetProgress>, 91 checkpoint: InventoryCheckpointRef, 92 ) -> Result<Self, OperationError> { 93 let policy_source = first.as_ref().and(source); 94 Self::from_store_report_with_policy_source( 95 request, 96 source, 97 policy_source, 98 first, 99 attempts, 100 attempts, 101 targets, 102 checkpoint, 103 ) 104 } 105 /// Validates an envelope-scoped projection while retaining the actual first 106 /// publication policy's source. A new carrier cannot renew its target set, 107 /// criterion or cumulative invocation budget. Reports grant no dispatch. 108 #[allow(clippy::too_many_arguments)] 109 pub fn from_store_report_with_policy_source( 110 request: &StockEnvelopeRequest, 111 source: Option<StockEnvelopeReceipt>, 112 policy_source: Option<StockEnvelopeReceipt>, 113 first: Option<DeliveryIntent>, 114 attempts: u16, 115 selected_attempts: u16, 116 targets: Vec<StockDeliveryTargetProgress>, 117 checkpoint: InventoryCheckpointRef, 118 ) -> Result<Self, OperationError> { 119 if checkpoint.authority() != request.binding().input().authority() 120 || checkpoint.revision().get() == 0 121 || attempts > 16 * 12 122 || selected_attempts > attempts 123 || (selected_attempts == 0 124 && targets 125 .iter() 126 .any(|target| target.accepted || target.last.is_some())) 127 || (source.is_none() && selected_attempts != 0) 128 || targets.len() > 16 129 { 130 return Err(OperationError::RecoveryRequired); 131 } 132 if let Some(source) = source { 133 let original = source.attempt(); 134 if original.workspace_id() != request.binding().input().workspace_id() 135 || original.operation_id() != request.operation_id() 136 || original.command_digest() != request.command_digest() 137 || source.recipient() != request.recipient() 138 || source.checkpoint().authority() != checkpoint.authority() 139 || source.checkpoint().revision() > checkpoint.revision() 140 || request 141 .envelope_event_id() 142 .is_some_and(|event| event != source.event_id()) 143 { 144 return Err(OperationError::RecoveryRequired); 145 } 146 } 147 if let Some(policy_source) = policy_source { 148 let selected = source.ok_or(OperationError::RecoveryRequired)?; 149 let policy_attempt = policy_source.attempt(); 150 let selected_attempt = selected.attempt(); 151 if policy_source.evidence() != selected.evidence() 152 || policy_source.installation_transaction_id() 153 != selected.installation_transaction_id() 154 || policy_attempt.workspace_id() != selected_attempt.workspace_id() 155 || policy_attempt.operation_id() != selected_attempt.operation_id() 156 || policy_attempt.command_digest() != selected_attempt.command_digest() 157 || policy_source.checkpoint().authority() != checkpoint.authority() 158 || policy_source.checkpoint().revision() > checkpoint.revision() 159 || first.is_none() 160 { 161 return Err(OperationError::RecoveryRequired); 162 } 163 } 164 match &first { 165 None if attempts != 0 || !targets.is_empty() => { 166 return Err(OperationError::RecoveryRequired); 167 } 168 Some(intent) 169 if attempts == 0 170 || intent.role() != ArtifactRole::GiftWrap 171 || policy_source 172 .is_none_or(|source| source.event_id() != intent.event_id()) 173 || intent 174 .targets() 175 .targets() 176 .iter() 177 .map(|target| target.fingerprint()) 178 .ne(targets.iter().map(|target| target.target())) 179 || targets 180 .iter() 181 .any(|target| u16::from(target.attempts) > attempts) 182 || targets 183 .iter() 184 .map(|target| u16::from(target.attempts)) 185 .sum::<u16>() 186 < attempts => 187 { 188 return Err(OperationError::RecoveryRequired); 189 } 190 _ => {} 191 } 192 Ok(Self { 193 source, 194 first, 195 policy_source, 196 attempts, 197 selected_attempts, 198 targets: targets.into_boxed_slice(), 199 checkpoint, 200 }) 201 } 202 pub const fn source(&self) -> Option<StockEnvelopeReceipt> { 203 self.source 204 } 205 pub const fn original_intent(&self) -> Option<&DeliveryIntent> { 206 self.first.as_ref() 207 } 208 /// Source of the first immutable target policy, which may differ from the 209 /// explicitly selected rewrapped envelope. 210 pub const fn publication_policy_source(&self) -> Option<StockEnvelopeReceipt> { 211 self.policy_source 212 } 213 pub const fn attempt_count(&self) -> u16 { 214 self.attempts 215 } 216 /// Potential invocations for the selected exact outer, independent of the 217 /// cumulative count used to enforce the shared recipient/endpoint quotas. 218 pub const fn selected_attempt_count(&self) -> u16 { 219 self.selected_attempts 220 } 221 pub fn targets(&self) -> &[StockDeliveryTargetProgress] { 222 &self.targets 223 } 224 pub const fn checkpoint(&self) -> InventoryCheckpointRef { 225 self.checkpoint 226 } 227 /// Applies the shared transport-owned law to sticky original acceptances. 228 pub fn acceptance_satisfied(&self) -> Result<bool, OperationError> { 229 let Some(intent) = &self.first else { 230 return Ok(false); 231 }; 232 let accepted = DeliveryOutcome::accepted(); 233 evaluate_satisfaction( 234 intent.policy(), 235 intent.targets(), 236 self.targets 237 .iter() 238 .filter(|target| target.accepted) 239 .map(|target| (target.target(), &accepted)), 240 ) 241 .map(|state| state == SatisfactionState::Satisfied) 242 .map_err(|_| OperationError::RecoveryRequired) 243 } 244 } 245 impl fmt::Debug for StockDeliverySummary { 246 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 247 f.write_str("StockDeliverySummary(<redacted>)") 248 } 249 } 250 251 /// A bounded original-outcome page, ordered by protected stock revision. 252 pub struct StockDeliveryObservationPage { 253 entries: Vec<StockDeliveryObservationReceipt>, 254 next: Option<Sequence>, 255 checkpoint: InventoryCheckpointRef, 256 } 257 impl StockDeliveryObservationPage { 258 pub fn from_store_report( 259 request: &StockEnvelopeRequest, 260 entries: Vec<StockDeliveryObservationReceipt>, 261 more: bool, 262 checkpoint: InventoryCheckpointRef, 263 ) -> Result<Self, OperationError> { 264 if entries.len() > 16 265 || (entries.is_empty() && more) 266 || checkpoint.authority() != request.binding().input().authority() 267 || entries 268 .windows(2) 269 .any(|pair| pair[0].checkpoint().revision() >= pair[1].checkpoint().revision()) 270 { 271 return Err(OperationError::RecoveryRequired); 272 } 273 for entry in &entries { 274 let original = entry.observation().source(); 275 if original.attempt().workspace_id() != request.binding().input().workspace_id() 276 || original.attempt().operation_id() != request.operation_id() 277 || original.attempt().command_digest() != request.command_digest() 278 || original.recipient() != request.recipient() 279 || entry.checkpoint().authority() != checkpoint.authority() 280 || entry.checkpoint().revision() > checkpoint.revision() 281 || request 282 .envelope_event_id() 283 .is_some_and(|event| event != original.event_id()) 284 { 285 return Err(OperationError::RecoveryRequired); 286 } 287 } 288 let next = if more { 289 entries.last().map(|entry| entry.checkpoint().revision()) 290 } else { 291 None 292 }; 293 Ok(Self { 294 entries, 295 next, 296 checkpoint, 297 }) 298 } 299 pub fn entries(&self) -> &[StockDeliveryObservationReceipt] { 300 &self.entries 301 } 302 pub const fn next_after(&self) -> Option<Sequence> { 303 self.next 304 } 305 pub const fn checkpoint(&self) -> InventoryCheckpointRef { 306 self.checkpoint 307 } 308 } 309 impl fmt::Debug for StockDeliveryObservationPage { 310 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 311 f.write_str("StockDeliveryObservationPage(<redacted>)") 312 } 313 }