lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

memory.rs (95782B)


      1 //! Deterministic in-memory reference storage backend.
      2 
      3 #[path = "memory_delivery_history.rs"]
      4 mod delivery_history;
      5 #[path = "memory_visibility_index.rs"]
      6 mod visibility_index;
      7 
      8 #[cfg(test)]
      9 #[path = "memory_mutation_tests.rs"]
     10 mod mutation_tests;
     11 
     12 use radroots_event::EventId;
     13 use radroots_protocol::runtime::v1::OperationId;
     14 use radroots_transport::{BoxFuture, source::EventProvenance};
     15 use std::sync::{Mutex, MutexGuard};
     16 
     17 use crate::{
     18     Error, EventStore, Journal, Outbox, ProjectionStore,
     19     atomic::{
     20         AtomicCommit, AtomicCommitDisposition, AtomicCommitId, AtomicCommitOutcome,
     21         AtomicCommitReceipt, AtomicStorage, AtomicWorkflow,
     22     },
     23     authored::{AdmissionState, FailureClass, WorkFailure, WorkPhase},
     24     authored_atomic::{
     25         AuthoredAtomicCommand, AuthoredAtomicOutcome, AuthoredAtomicReceipt, AuthoredAtomicStorage,
     26         AuthoredWorkTarget, CancelAuthoredTarget, ClaimAuthoredTarget,
     27     },
     28     authored_delivery::DeliveryAttemptOutcome,
     29     authored_draft::{
     30         AUTHORED_DRAFT_QUERY_LIMIT_MAX, AuthoredDraft, AuthoredDraftId, AuthoredDraftRevision,
     31         AuthoredDraftStore, DraftAppendDisposition, DraftAppendReceipt,
     32     },
     33     backup::{
     34         BackupId, BackupOperation, BackupPlan, BackupTransition, ReliabilityRevision,
     35         RestoreOperation, RestorePlan, RestoreTransition, StorageReliability,
     36     },
     37     event::{
     38         AdmissionDisposition, AdmissionReceipt, AdmissionStage, EventAdmission, EventPage,
     39         EventPosition, EventQuery, EventQueryBounds, EventSequence, EventSourceToken,
     40         SourceGeneration, StoredEventProvenance, StoredRawEvent, StoredVerifiedEvent,
     41         StoredVisibleEvent, VisibilityEvaluation, VisibilityInput, VisibilitySnapshot,
     42         evaluate_visibility,
     43     },
     44     journal::{
     45         IdempotencyKey, JournalStage, JournalTransition, OperationInstanceId, OperationRecord,
     46         PrepareDisposition, PrepareOperation, PrepareReceipt, RECOVERABLE_QUERY_LIMIT_MAX,
     47     },
     48     outbox::{
     49         ClaimOutboxItems, ClaimedOutboxItem, DeliveryAttemptEvidence, EnqueueDisposition,
     50         EnqueueOutboxItem, EnqueueReceipt, LeaseId, OutboxItemId, OutboxLease, OutboxRecord,
     51         OutboxRevision, OutboxStage, OutboxStatus,
     52     },
     53     private_artifact::{
     54         DeletionReason, EXPIRED_ARTIFACT_QUERY_LIMIT_MAX, PrivateArtifactId,
     55         PrivateArtifactMetadata, PrivateArtifactResealReceipt, PrivateArtifactResealRequest,
     56         PrivateArtifactRevision, PrivateArtifactStage, PrivateArtifactStatus, PrivateArtifactStore,
     57     },
     58     projection::{
     59         EventIndexCheckpoint, EventIndexManifest, ProjectionCheckpoint, ProjectionDocument,
     60         ProjectionGeneration, ProjectionHealth, ProjectionId, ProjectionInvalidation,
     61         ProjectionSnapshot, ProjectionStatus, RebuildStage, RebuildTicket, RebuildTicketId,
     62         RebuildTransition,
     63     },
     64     status::{
     65         EventStoreHealth, EventStoreMode, EventStoreStatus, IntegrityHealth, IntegrityStatus,
     66         ShutdownState, StorageBackend, StorageOpenMode, StorageStatus, StorageStatusProvider,
     67         WriterPolicy,
     68     },
     69 };
     70 
     71 #[path = "memory_projection_batch.rs"]
     72 mod projection_batch;
     73 #[path = "memory_projection_cache.rs"]
     74 mod projection_cache;
     75 #[path = "memory_projection_cache_retired.rs"]
     76 mod projection_cache_retired;
     77 
     78 #[derive(Clone)]
     79 struct EventEntry {
     80     position: EventPosition,
     81     admission: EventAdmission,
     82     provenance: Vec<EventProvenance>,
     83     last_mutation_epoch: u64,
     84 }
     85 
     86 #[derive(Clone, Default)]
     87 struct State {
     88     events: Vec<EventEntry>,
     89     event_mutation_epoch: u64,
     90     event_changes: std::collections::BTreeMap<u64, u64>,
     91     visibility_index: visibility_index::Index,
     92     journal: Vec<OperationRecord>,
     93     outbox: Vec<OutboxRecord>,
     94     projections: Vec<ProjectionStatus>,
     95     projection_invalidations: Vec<ProjectionInvalidation>,
     96     rebuilds: Vec<RebuildTicket>,
     97     event_index_manifests: Vec<EventIndexManifest>,
     98     event_index_checkpoints: Vec<EventIndexCheckpoint>,
     99     projection_documents: projection_batch::Documents,
    100     projection_generations: std::collections::BTreeMap<
    101         (ProjectionId, ProjectionGeneration),
    102         projection_batch::Generation,
    103     >,
    104     projection_snapshots: projection_cache::Snapshots,
    105     projection_cache: projection_cache::Cache,
    106     private_artifacts: Vec<PrivateArtifactMetadata>,
    107     private_artifact_reseals: Vec<PrivateArtifactResealReceipt>,
    108     backups: Vec<BackupOperation>,
    109     restores: Vec<RestoreOperation>,
    110     atomic_receipts: Vec<AtomicCommitReceipt>,
    111     authored_operations: Vec<crate::authored::AuthoredOperation>,
    112     authored_artifacts: Vec<crate::authored::AuthoredArtifact>,
    113     authored_delivery_plans: Vec<crate::authored_delivery::AuthoredDeliveryPlan>,
    114     authored_atomic_receipts: Vec<AuthoredAtomicReceipt>,
    115     authored_delivery_history: std::collections::BTreeMap<
    116         crate::authored_delivery::AuthoredDeliveryPlanId,
    117         delivery_history::Entry,
    118     >,
    119     authored_drafts: Vec<AuthoredDraft>,
    120     closed: bool,
    121 }
    122 
    123 /// Bounded deterministic reference backend with no hidden tasks or globals.
    124 pub struct MemoryStorage {
    125     generation: SourceGeneration,
    126     state: Mutex<State>,
    127 }
    128 
    129 impl MemoryStorage {
    130     pub const fn new(generation: SourceGeneration) -> Self {
    131         Self {
    132             generation,
    133             state: Mutex::new(State {
    134                 events: Vec::new(),
    135                 event_mutation_epoch: 0,
    136                 event_changes: std::collections::BTreeMap::new(),
    137                 visibility_index: visibility_index::Index::new(),
    138                 journal: Vec::new(),
    139                 outbox: Vec::new(),
    140                 projections: Vec::new(),
    141                 projection_invalidations: Vec::new(),
    142                 rebuilds: Vec::new(),
    143                 event_index_manifests: Vec::new(),
    144                 event_index_checkpoints: Vec::new(),
    145                 projection_documents: std::collections::BTreeMap::new(),
    146                 projection_generations: std::collections::BTreeMap::new(),
    147                 projection_snapshots: projection_cache::Snapshots::new(),
    148                 projection_cache: projection_cache::Cache::new(),
    149                 private_artifacts: Vec::new(),
    150                 private_artifact_reseals: Vec::new(),
    151                 backups: Vec::new(),
    152                 restores: Vec::new(),
    153                 atomic_receipts: Vec::new(),
    154                 authored_operations: Vec::new(),
    155                 authored_artifacts: Vec::new(),
    156                 authored_delivery_plans: Vec::new(),
    157                 authored_atomic_receipts: Vec::new(),
    158                 authored_delivery_history: std::collections::BTreeMap::new(),
    159                 authored_drafts: Vec::new(),
    160                 closed: false,
    161             }),
    162         }
    163     }
    164 
    165     pub const fn generation(&self) -> SourceGeneration {
    166         self.generation
    167     }
    168 
    169     fn state(&self) -> Result<MutexGuard<'_, State>, Error> {
    170         let state = self.state_any()?;
    171         if state.closed {
    172             return Err(Error::BackendUnavailable);
    173         }
    174         Ok(state)
    175     }
    176 
    177     fn state_any(&self) -> Result<MutexGuard<'_, State>, Error> {
    178         self.state.lock().map_err(|_| Error::BackendUnavailable)
    179     }
    180 
    181     fn selected(
    182         &self,
    183         state: &State,
    184         query: &EventQuery,
    185         mut eligible: impl FnMut(&EventEntry) -> bool,
    186     ) -> Result<(Vec<EventEntry>, Option<EventPosition>), Error> {
    187         if query
    188             .bounds()
    189             .cursor()
    190             .is_some_and(|cursor| cursor.generation() != self.generation)
    191         {
    192             return Err(Error::SourceGenerationChanged);
    193         }
    194         let after = query
    195             .bounds()
    196             .cursor()
    197             .map_or(0, |cursor| cursor.sequence().get());
    198         let mut selected = state
    199             .events
    200             .iter()
    201             .filter(|entry| {
    202                 entry.position.sequence().get() > after
    203                     && query.selects(entry.admission.event_id())
    204                     && eligible(entry)
    205             })
    206             .take(usize::from(query.bounds().limit()) + 1)
    207             .cloned()
    208             .collect::<Vec<_>>();
    209         let next = if selected.len() > usize::from(query.bounds().limit()) {
    210             selected.truncate(usize::from(query.bounds().limit()));
    211             selected.last().map(|entry| entry.position)
    212         } else {
    213             None
    214         };
    215         Ok((selected, next))
    216     }
    217 
    218     fn visible_page_locked(
    219         &self,
    220         state: &State,
    221         query: EventQuery,
    222         expected: Option<EventSourceToken>,
    223     ) -> Result<EventPage<StoredVisibleEvent>, Error> {
    224         if query
    225             .bounds()
    226             .cursor()
    227             .is_some_and(|cursor| cursor.generation() != self.generation)
    228         {
    229             return Err(Error::SourceGenerationChanged);
    230         }
    231         if expected.is_some_and(|token| {
    232             token != EventSourceToken::new(self.generation, state.event_mutation_epoch)
    233         }) {
    234             return Err(Error::SourceEpochChanged);
    235         }
    236         if !state.visibility_index.ready {
    237             return Err(Error::VisibilityRebuilding);
    238         }
    239         let after = query
    240             .bounds()
    241             .cursor()
    242             .map_or(0, |cursor| cursor.sequence().get());
    243         let maximum = usize::from(query.bounds().limit());
    244         let positions = if query.event_ids().is_empty() {
    245             state
    246                 .visibility_index
    247                 .visible
    248                 .range((std::ops::Bound::Excluded(after), std::ops::Bound::Unbounded))
    249                 .take(maximum + 1)
    250                 .copied()
    251                 .collect::<Vec<_>>()
    252         } else {
    253             let positions = query
    254                 .event_ids()
    255                 .iter()
    256                 .filter_map(|id| state.visibility_index.ids.get(id).copied())
    257                 .filter(|position| {
    258                     *position > after && state.visibility_index.visible.contains(position)
    259                 })
    260                 .collect::<std::collections::BTreeSet<_>>();
    261             positions.into_iter().take(maximum + 1).collect()
    262         };
    263         let mut charged = 4096 + (u64::from(query.bounds().limit()) + 1) * 128;
    264         let mut items = Vec::new();
    265         let mut more = false;
    266         for position in positions {
    267             if items.len() == maximum {
    268                 more = true;
    269                 break;
    270             }
    271             let entry = state
    272                 .events
    273                 .get(usize::try_from(position - 1).map_err(|_| Error::CorruptStoredEvent)?)
    274                 .ok_or(Error::CorruptStoredEvent)?;
    275             let charge = charged
    276                 .checked_add(crate::event::VisibilityIndexBounds::charge(
    277                     entry.admission.event().raw_json().len() as u64,
    278                 )?)
    279                 .ok_or(Error::SpaceInsufficient)?;
    280             if charge > 16 * 1024 * 1024 {
    281                 if items.is_empty() {
    282                     return Err(Error::SpaceInsufficient);
    283                 }
    284                 more = true;
    285                 break;
    286             }
    287             items.push(StoredVisibleEvent::new(
    288                 entry.position,
    289                 entry.admission.event().clone(),
    290             ));
    291             charged = charge;
    292         }
    293         let next = more
    294             .then(|| items.last().map(StoredVisibleEvent::position))
    295             .flatten();
    296         EventPage::new(self.generation, items, next, query.bounds())
    297     }
    298 
    299     fn visibility_locked(&self, state: &State) -> Result<VisibilityEvaluation, Error> {
    300         evaluate_visibility(
    301             self.generation,
    302             state.events.iter().map(|entry| {
    303                 VisibilityInput::new(
    304                     entry.position,
    305                     entry.admission.event(),
    306                     entry.admission.stage(),
    307                 )
    308             }),
    309         )
    310     }
    311 
    312     fn admit_locked(
    313         &self,
    314         state: &mut State,
    315         admission: EventAdmission,
    316     ) -> Result<AdmissionReceipt, Error> {
    317         let was_ready = state.visibility_index.ready;
    318         crate::event::VisibilityEventMetadata::from_event(admission.event(), admission.stage())?;
    319         let bytes = u64::try_from(admission.event().raw_json().len())
    320             .map_err(|_| Error::SpaceInsufficient)?;
    321         let fits = crate::event::VisibilityIndexBounds::charge(bytes)?
    322             <= 16 * 1024 * 1024 - 4096 - 64 * 128;
    323         let receipt = self.admit_source_locked(state, admission)?;
    324         if was_ready && receipt.disposition() != AdmissionDisposition::Duplicate && fits {
    325             visibility_index::prepare(
    326                 self.generation,
    327                 state,
    328                 crate::event::VisibilityIndexBounds::new(64, 16 * 1024 * 1024)?,
    329             )?;
    330         }
    331         Ok(receipt)
    332     }
    333 
    334     fn admit_source_locked(
    335         &self,
    336         state: &mut State,
    337         admission: EventAdmission,
    338     ) -> Result<AdmissionReceipt, Error> {
    339         if let Some(entry) = state
    340             .events
    341             .iter_mut()
    342             .find(|entry| entry.admission.event_id() == admission.event_id())
    343         {
    344             if entry.admission.event() != admission.event() {
    345                 return Err(Error::EventConflict);
    346             }
    347             if admission.stage() < entry.admission.stage() {
    348                 return Err(Error::AdmissionRegression);
    349             }
    350             let disposition = if admission.stage() == entry.admission.stage() {
    351                 AdmissionDisposition::Duplicate
    352             } else {
    353                 state.event_mutation_epoch = state
    354                     .event_mutation_epoch
    355                     .checked_add(1)
    356                     .ok_or(Error::EventMutationOverflow)?;
    357                 state.event_changes.remove(&entry.last_mutation_epoch);
    358                 entry.last_mutation_epoch = state.event_mutation_epoch;
    359                 state
    360                     .event_changes
    361                     .insert(entry.last_mutation_epoch, entry.position.sequence().get());
    362                 state.visibility_index.ready = false;
    363                 AdmissionDisposition::Advanced
    364             };
    365             if !entry.provenance.contains(admission.provenance()) {
    366                 entry.provenance.push(admission.provenance().clone());
    367             }
    368             entry.admission = admission;
    369             return Ok(AdmissionReceipt::new(
    370                 *entry.admission.event_id(),
    371                 entry.position,
    372                 entry.admission.stage(),
    373                 disposition,
    374             ));
    375         }
    376         let next = u64::try_from(state.events.len())
    377             .map_err(|_| Error::CorruptStoredEvent)?
    378             .checked_add(1)
    379             .ok_or(Error::CorruptStoredEvent)?;
    380         let position = EventPosition::new(self.generation, EventSequence::new(next)?);
    381         let epoch = state
    382             .event_mutation_epoch
    383             .checked_add(1)
    384             .ok_or(Error::EventMutationOverflow)?;
    385         let receipt = AdmissionReceipt::new(
    386             *admission.event_id(),
    387             position,
    388             admission.stage(),
    389             AdmissionDisposition::Inserted,
    390         );
    391         let provenance = vec![admission.provenance().clone()];
    392         state.events.push(EventEntry {
    393             position,
    394             admission,
    395             provenance,
    396             last_mutation_epoch: epoch,
    397         });
    398         state.event_mutation_epoch = epoch;
    399         state.event_changes.insert(epoch, next);
    400         state.visibility_index.ready = false;
    401         Ok(receipt)
    402     }
    403 
    404     fn prepare_locked(
    405         state: &mut State,
    406         operation: PrepareOperation,
    407     ) -> Result<PrepareReceipt, Error> {
    408         if let Some(record) = state
    409             .journal
    410             .iter()
    411             .find(|record| record.idempotency_key() == operation.idempotency_key())
    412         {
    413             if record.operation_id() != operation.operation_id()
    414                 || record.input_digest() != operation.input_digest()
    415                 || record.instance_id() != operation.instance_id()
    416             {
    417                 return Err(Error::IdempotencyConflict);
    418             }
    419             return Ok(PrepareReceipt::new(
    420                 PrepareDisposition::Replay,
    421                 record.clone(),
    422             ));
    423         }
    424         if state
    425             .journal
    426             .iter()
    427             .any(|record| record.instance_id() == operation.instance_id())
    428         {
    429             return Err(Error::OperationIdentityMismatch);
    430         }
    431         let record = operation.into_record()?;
    432         state.journal.push(record.clone());
    433         Ok(PrepareReceipt::new(PrepareDisposition::Created, record))
    434     }
    435 
    436     fn transition_locked(
    437         state: &mut State,
    438         transition: JournalTransition,
    439     ) -> Result<OperationRecord, Error> {
    440         let record = state
    441             .journal
    442             .iter_mut()
    443             .find(|record| record.instance_id() == transition.instance_id())
    444             .ok_or(Error::OperationNotFound)?;
    445         let next = record.transition(&transition)?;
    446         *record = next.clone();
    447         Ok(next)
    448     }
    449 
    450     fn enqueue_locked(state: &mut State, item: EnqueueOutboxItem) -> Result<EnqueueReceipt, Error> {
    451         if let Some(record) = state
    452             .outbox
    453             .iter()
    454             .find(|record| record.item_id() == item.item_id())
    455         {
    456             let candidate = item.into_record();
    457             if record.operation_instance_id() != candidate.operation_instance_id()
    458                 || record.plan_digest() != candidate.plan_digest()
    459                 || record.request() != candidate.request()
    460                 || record.created_at_unix_ms() != candidate.created_at_unix_ms()
    461             {
    462                 return Err(Error::OutboxPlanConflict);
    463             }
    464             return Ok(EnqueueReceipt::new(
    465                 EnqueueDisposition::Replay,
    466                 record.clone(),
    467             ));
    468         }
    469         if state.outbox.iter().any(|record| {
    470             record.operation_instance_id() == item.operation_instance_id()
    471                 && record.item_id() != item.item_id()
    472         }) {
    473             return Err(Error::OutboxPlanConflict);
    474         }
    475         let record = item.into_record();
    476         state.outbox.push(record.clone());
    477         Ok(EnqueueReceipt::new(EnqueueDisposition::Created, record))
    478     }
    479 
    480     fn checkpoint_locked(
    481         state: &mut State,
    482         checkpoint: ProjectionCheckpoint,
    483     ) -> Result<ProjectionStatus, Error> {
    484         if state
    485             .projection_generations
    486             .contains_key(&(checkpoint.projection_id().clone(), checkpoint.generation()))
    487         {
    488             return Err(Error::ProjectionRevisionConflict);
    489         }
    490         let identity = (checkpoint.projection_id().clone(), checkpoint.generation());
    491         state.projection_cache.check_protected(&identity)?;
    492         if let Some(status) = state
    493             .projections
    494             .iter_mut()
    495             .find(|status| status.projection_id() == checkpoint.projection_id())
    496         {
    497             if status.generation() != checkpoint.generation() {
    498                 return Err(Error::ProjectionCheckpointMismatch);
    499             }
    500             if status
    501                 .checkpoint()
    502                 .is_some_and(|prior| !checkpoint.advances(prior))
    503             {
    504                 return Err(Error::ProjectionCheckpointRegression);
    505             }
    506             let next = ProjectionStatus::new(
    507                 checkpoint.projection_id().clone(),
    508                 checkpoint.generation(),
    509                 ProjectionHealth::Ready,
    510                 Some(checkpoint),
    511                 None,
    512             )?;
    513             *status = next.clone();
    514             return Ok(next);
    515         }
    516         let status = ProjectionStatus::new(
    517             checkpoint.projection_id().clone(),
    518             checkpoint.generation(),
    519             ProjectionHealth::Ready,
    520             Some(checkpoint),
    521             None,
    522         )?;
    523         state.projection_cache.protected.insert(identity);
    524         state.projections.push(status.clone());
    525         Ok(status)
    526     }
    527 
    528     fn integrity_locked(state: &State) -> Result<IntegrityStatus, Error> {
    529         let members = state
    530             .events
    531             .len()
    532             .checked_add(state.journal.len())
    533             .and_then(|count| count.checked_add(state.outbox.len()))
    534             .and_then(|count| count.checked_add(state.projections.len()))
    535             .and_then(|count| count.checked_add(state.private_artifacts.len()))
    536             .ok_or(Error::InvalidIntegrityStatus)?;
    537         IntegrityStatus::new(
    538             IntegrityHealth::Healthy,
    539             None,
    540             u32::try_from(members).map_err(|_| Error::InvalidIntegrityStatus)?,
    541             0,
    542         )
    543     }
    544 
    545     fn status_locked(state: &State) -> Result<StorageStatus, Error> {
    546         StorageStatus::new(
    547             StorageBackend::Memory,
    548             StorageOpenMode::Create,
    549             WriterPolicy::NoWriter,
    550             if state.closed {
    551                 ShutdownState::Closed
    552             } else {
    553                 ShutdownState::Open
    554             },
    555             Self::integrity_locked(state)?,
    556             false,
    557             0,
    558         )
    559     }
    560 }
    561 
    562 impl Default for MemoryStorage {
    563     fn default() -> Self {
    564         Self::new(SourceGeneration::new([1; 32]).expect("fixed non-zero memory generation"))
    565     }
    566 }
    567 
    568 impl EventStore for MemoryStorage {
    569     fn maintain_visibility(
    570         &self,
    571         bounds: crate::event::VisibilityIndexBounds,
    572     ) -> BoxFuture<'_, Result<crate::event::VisibilityIndexProgress, Error>> {
    573         Box::pin(async move {
    574             let mut state = self.state()?;
    575             visibility_index::prepare(self.generation, &mut state, bounds)
    576         })
    577     }
    578 
    579     fn source_token(&self) -> BoxFuture<'_, Result<EventSourceToken, Error>> {
    580         Box::pin(async move {
    581             let state = self.state()?;
    582             Ok(EventSourceToken::new(
    583                 self.generation,
    584                 state.event_mutation_epoch,
    585             ))
    586         })
    587     }
    588 
    589     fn status(&self) -> BoxFuture<'_, Result<EventStoreStatus, Error>> {
    590         Box::pin(async move {
    591             let state = self.state()?;
    592             if !state.visibility_index.ready {
    593                 return Err(Error::VisibilityRebuilding);
    594             }
    595             EventStoreStatus::new(
    596                 self.generation,
    597                 EventStoreMode::ReadWrite,
    598                 EventStoreHealth::Available,
    599                 u64::try_from(state.events.len()).map_err(|_| Error::CorruptStoredEvent)?,
    600                 state.visibility_index.verified,
    601                 u64::try_from(state.visibility_index.visible.len())
    602                     .map_err(|_| Error::CorruptStoredEvent)?,
    603             )
    604         })
    605     }
    606 
    607     fn admit(&self, admission: EventAdmission) -> BoxFuture<'_, Result<AdmissionReceipt, Error>> {
    608         Box::pin(async move {
    609             let mut state = self.state()?;
    610             self.admit_locked(&mut state, admission)
    611         })
    612     }
    613 
    614     fn query_raw(
    615         &self,
    616         query: EventQuery,
    617     ) -> BoxFuture<'_, Result<EventPage<StoredRawEvent>, Error>> {
    618         Box::pin(async move {
    619             let state = self.state()?;
    620             let (entries, next) = self.selected(&state, &query, |_| true)?;
    621             let items = entries
    622                 .into_iter()
    623                 .map(|entry| {
    624                     StoredRawEvent::new(
    625                         entry.position,
    626                         entry.admission.event().clone(),
    627                         entry.admission.stage(),
    628                     )
    629                 })
    630                 .collect();
    631             EventPage::new(self.generation, items, next, query.bounds())
    632         })
    633     }
    634 
    635     fn query_verified(
    636         &self,
    637         query: EventQuery,
    638     ) -> BoxFuture<'_, Result<EventPage<StoredVerifiedEvent>, Error>> {
    639         Box::pin(async move {
    640             let state = self.state()?;
    641             let (entries, next) = self.selected(&state, &query, |entry| {
    642                 entry.admission.stage() >= AdmissionStage::Verified
    643             })?;
    644             let items = entries
    645                 .into_iter()
    646                 .map(|entry| {
    647                     StoredVerifiedEvent::new(entry.position, entry.admission.event().clone())
    648                 })
    649                 .collect();
    650             EventPage::new(self.generation, items, next, query.bounds())
    651         })
    652     }
    653 
    654     fn query_visible(
    655         &self,
    656         query: EventQuery,
    657     ) -> BoxFuture<'_, Result<EventPage<StoredVisibleEvent>, Error>> {
    658         Box::pin(async move {
    659             let state = self.state()?;
    660             self.visible_page_locked(&state, query, None)
    661         })
    662     }
    663 
    664     fn query_visible_at(
    665         &self,
    666         query: EventQuery,
    667         expected: EventSourceToken,
    668     ) -> BoxFuture<'_, Result<EventPage<StoredVisibleEvent>, Error>> {
    669         Box::pin(async move {
    670             let state = self.state()?;
    671             self.visible_page_locked(&state, query, Some(expected))
    672         })
    673     }
    674 
    675     fn rebuild_visibility(&self) -> BoxFuture<'_, Result<VisibilitySnapshot, Error>> {
    676         Box::pin(async move {
    677             let state = self.state()?;
    678             Ok(self.visibility_locked(&state)?.into_snapshot())
    679         })
    680     }
    681 
    682     fn query_provenance(
    683         &self,
    684         event_id: EventId,
    685         bounds: EventQueryBounds,
    686     ) -> BoxFuture<'_, Result<EventPage<StoredEventProvenance>, Error>> {
    687         Box::pin(async move {
    688             if bounds
    689                 .cursor()
    690                 .is_some_and(|cursor| cursor.generation() != self.generation)
    691             {
    692                 return Err(Error::SourceGenerationChanged);
    693             }
    694             let state = self.state()?;
    695             let entry = state
    696                 .events
    697                 .iter()
    698                 .find(|entry| entry.admission.event_id() == &event_id)
    699                 .ok_or(Error::EventNotFound)?;
    700             let after = bounds.cursor().map_or(0, |cursor| cursor.sequence().get());
    701             let items = if entry.position.sequence().get() > after {
    702                 entry
    703                     .provenance
    704                     .iter()
    705                     .take(usize::from(bounds.limit()))
    706                     .cloned()
    707                     .map(|provenance| StoredEventProvenance::new(entry.position, provenance))
    708                     .collect()
    709             } else {
    710                 Vec::new()
    711             };
    712             EventPage::new(self.generation, items, None, bounds)
    713         })
    714     }
    715 }
    716 
    717 impl Journal for MemoryStorage {
    718     fn prepare(&self, operation: PrepareOperation) -> BoxFuture<'_, Result<PrepareReceipt, Error>> {
    719         Box::pin(async move {
    720             let mut state = self.state()?;
    721             Self::prepare_locked(&mut state, operation)
    722         })
    723     }
    724 
    725     fn operation(
    726         &self,
    727         instance_id: OperationInstanceId,
    728     ) -> BoxFuture<'_, Result<Option<OperationRecord>, Error>> {
    729         Box::pin(async move {
    730             Ok(self
    731                 .state()?
    732                 .journal
    733                 .iter()
    734                 .find(|record| record.instance_id() == instance_id)
    735                 .cloned())
    736         })
    737     }
    738 
    739     fn by_idempotency_key(
    740         &self,
    741         operation_id: OperationId,
    742         idempotency_key: IdempotencyKey,
    743     ) -> BoxFuture<'_, Result<Option<OperationRecord>, Error>> {
    744         Box::pin(async move {
    745             Ok(self
    746                 .state()?
    747                 .journal
    748                 .iter()
    749                 .find(|record| {
    750                     record.operation_id() == operation_id
    751                         && record.idempotency_key() == &idempotency_key
    752                 })
    753                 .cloned())
    754         })
    755     }
    756 
    757     fn transition(
    758         &self,
    759         transition: JournalTransition,
    760     ) -> BoxFuture<'_, Result<OperationRecord, Error>> {
    761         Box::pin(async move {
    762             let mut state = self.state()?;
    763             Self::transition_locked(&mut state, transition)
    764         })
    765     }
    766 
    767     fn recoverable(&self, limit: u16) -> BoxFuture<'_, Result<Vec<OperationRecord>, Error>> {
    768         Box::pin(async move {
    769             if limit == 0 || limit > RECOVERABLE_QUERY_LIMIT_MAX {
    770                 return Err(Error::InvalidJournalQueryLimit);
    771             }
    772             Ok(self
    773                 .state()?
    774                 .journal
    775                 .iter()
    776                 .filter(|record| record.state().stage() == JournalStage::Recoverable)
    777                 .take(usize::from(limit))
    778                 .cloned()
    779                 .collect())
    780         })
    781     }
    782 }
    783 
    784 impl Outbox for MemoryStorage {
    785     fn enqueue(&self, item: EnqueueOutboxItem) -> BoxFuture<'_, Result<EnqueueReceipt, Error>> {
    786         Box::pin(async move {
    787             let mut state = self.state()?;
    788             Self::enqueue_locked(&mut state, item)
    789         })
    790     }
    791 
    792     fn item(&self, item_id: OutboxItemId) -> BoxFuture<'_, Result<Option<OutboxRecord>, Error>> {
    793         Box::pin(async move {
    794             Ok(self
    795                 .state()?
    796                 .outbox
    797                 .iter()
    798                 .find(|record| record.item_id() == item_id)
    799                 .cloned())
    800         })
    801     }
    802 
    803     fn claim(
    804         &self,
    805         request: ClaimOutboxItems,
    806     ) -> BoxFuture<'_, Result<Vec<ClaimedOutboxItem>, Error>> {
    807         Box::pin(async move {
    808             let mut state = self.state()?;
    809             let mut claimed = Vec::new();
    810             for record in &mut state.outbox {
    811                 if claimed.len() >= usize::from(request.limit()) || record.stage().is_terminal() {
    812                     continue;
    813                 }
    814                 if record
    815                     .retry_not_before_unix_ms()
    816                     .is_some_and(|at| request.now_unix_ms() < at)
    817                     || record
    818                         .lease()
    819                         .is_some_and(|lease| lease.is_active_at(request.now_unix_ms()))
    820                 {
    821                     continue;
    822                 }
    823                 let lease = OutboxLease::new(
    824                     request.lease_id_for(record.item_id()),
    825                     request.owner().clone(),
    826                     request.now_unix_ms(),
    827                     request.lease_expires_at_unix_ms(),
    828                 )?;
    829                 record.claim(lease.clone())?;
    830                 claimed.push(ClaimedOutboxItem::new(record.clone(), lease));
    831             }
    832             Ok(claimed)
    833         })
    834     }
    835 
    836     fn record_attempt(
    837         &self,
    838         evidence: DeliveryAttemptEvidence,
    839     ) -> BoxFuture<'_, Result<OutboxRecord, Error>> {
    840         Box::pin(async move {
    841             let mut state = self.state()?;
    842             let record = state
    843                 .outbox
    844                 .iter_mut()
    845                 .find(|record| record.item_id() == evidence.item_id())
    846                 .ok_or(Error::OutboxItemNotFound)?;
    847             record.record_attempt(evidence)?;
    848             Ok(record.clone())
    849         })
    850     }
    851 
    852     fn release(
    853         &self,
    854         item_id: OutboxItemId,
    855         lease_id: LeaseId,
    856         expected_revision: OutboxRevision,
    857         released_at_unix_ms: u64,
    858         retry_not_before_unix_ms: Option<u64>,
    859     ) -> BoxFuture<'_, Result<OutboxRecord, Error>> {
    860         Box::pin(async move {
    861             let mut state = self.state()?;
    862             let record = state
    863                 .outbox
    864                 .iter_mut()
    865                 .find(|record| record.item_id() == item_id)
    866                 .ok_or(Error::OutboxItemNotFound)?;
    867             record.release(
    868                 lease_id,
    869                 expected_revision,
    870                 released_at_unix_ms,
    871                 retry_not_before_unix_ms,
    872             )?;
    873             Ok(record.clone())
    874         })
    875     }
    876 
    877     fn status(&self) -> BoxFuture<'_, Result<OutboxStatus, Error>> {
    878         Box::pin(async move {
    879             let state = self.state()?;
    880             let mut status = OutboxStatus {
    881                 pending: 0,
    882                 leased: 0,
    883                 retryable: 0,
    884                 satisfied: 0,
    885                 exhausted: 0,
    886             };
    887             for record in &state.outbox {
    888                 let count = match record.stage() {
    889                     OutboxStage::Pending => &mut status.pending,
    890                     OutboxStage::Leased => &mut status.leased,
    891                     OutboxStage::Retryable => &mut status.retryable,
    892                     OutboxStage::Satisfied => &mut status.satisfied,
    893                     OutboxStage::Exhausted => &mut status.exhausted,
    894                 };
    895                 *count = count.checked_add(1).ok_or(Error::CorruptOutboxRecord)?;
    896             }
    897             Ok(status)
    898         })
    899     }
    900 }
    901 
    902 impl ProjectionStore for MemoryStorage {
    903     fn configure_projection_cache(
    904         &self,
    905         id: ProjectionId,
    906         limits: crate::projection::cache::ProjectionCacheLimits,
    907     ) -> BoxFuture<'_, Result<crate::projection::cache::ProjectionCacheUsage, Error>> {
    908         Box::pin(async move {
    909             projection_cache::configure(&mut *self.state()?, id, limits, projection_cache::now()?)
    910         })
    911     }
    912     fn retire_projection_generation(
    913         &self,
    914         scope: crate::projection::cache::ProjectionCacheScope,
    915     ) -> BoxFuture<'_, Result<crate::projection::cache::ProjectionCacheUsage, Error>> {
    916         Box::pin(
    917             async move { projection_cache::retire(&mut *self.state()?, self.generation, scope) },
    918         )
    919     }
    920     fn collect_projection_cache(
    921         &self,
    922         scope: crate::projection::cache::ProjectionCacheScope,
    923     ) -> BoxFuture<'_, Result<crate::projection::cache::ProjectionCacheCollection, Error>> {
    924         Box::pin(async move {
    925             projection_cache::collect(
    926                 &mut *self.state()?,
    927                 self.generation,
    928                 scope,
    929                 projection_cache::now()?,
    930             )
    931         })
    932     }
    933     fn collect_retired_projection_cache(
    934         &self,
    935         id: ProjectionId,
    936         source: EventSourceToken,
    937         expected_active: Option<ProjectionGeneration>,
    938     ) -> BoxFuture<'_, Result<crate::projection::cache::ProjectionCacheCollection, Error>> {
    939         Box::pin(async move {
    940             projection_cache_retired::collect(
    941                 &mut *self.state()?,
    942                 self.generation,
    943                 id,
    944                 source,
    945                 expected_active,
    946                 projection_cache::now()?,
    947             )
    948         })
    949     }
    950     fn collect_obsolete_projection_snapshots(
    951         &self,
    952         scope: crate::projection::cache::ProjectionCacheScope,
    953     ) -> BoxFuture<'_, Result<crate::projection::cache::ProjectionCacheCollection, Error>> {
    954         Box::pin(async move {
    955             projection_cache::collect_kind(
    956                 &mut *self.state()?,
    957                 self.generation,
    958                 scope,
    959                 projection_cache::now()?,
    960                 projection_cache::CollectionKind::ObsoleteSnapshots,
    961             )
    962         })
    963     }
    964     fn status(
    965         &self,
    966         projection_id: ProjectionId,
    967     ) -> BoxFuture<'_, Result<Option<ProjectionStatus>, Error>> {
    968         Box::pin(async move {
    969             Ok(self
    970                 .state()?
    971                 .projections
    972                 .iter()
    973                 .find(|status| status.projection_id() == &projection_id)
    974                 .cloned())
    975         })
    976     }
    977 
    978     fn checkpoint(
    979         &self,
    980         checkpoint: ProjectionCheckpoint,
    981     ) -> BoxFuture<'_, Result<ProjectionStatus, Error>> {
    982         Box::pin(async move {
    983             let mut state = self.state()?;
    984             Self::checkpoint_locked(&mut state, checkpoint)
    985         })
    986     }
    987 
    988     fn invalidate(
    989         &self,
    990         invalidation: ProjectionInvalidation,
    991     ) -> BoxFuture<'_, Result<ProjectionStatus, Error>> {
    992         Box::pin(async move {
    993             let mut state = self.state()?;
    994             let status_index = state
    995                 .projections
    996                 .iter()
    997                 .position(|status| status.projection_id() == invalidation.projection_id())
    998                 .ok_or(Error::ProjectionCheckpointMismatch)?;
    999             if state.projections[status_index].generation() != invalidation.invalid_generation() {
   1000                 return Err(Error::ProjectionCheckpointMismatch);
   1001             }
   1002             if let Some(existing) = state.projection_invalidations.iter().find(|existing| {
   1003                 existing.projection_id() == invalidation.projection_id()
   1004                     && existing.invalid_generation() == invalidation.invalid_generation()
   1005             }) && existing != &invalidation
   1006             {
   1007                 return Err(Error::ProjectionRevisionConflict);
   1008             }
   1009             let next = ProjectionStatus::new(
   1010                 invalidation.projection_id().clone(),
   1011                 state.projections[status_index].generation(),
   1012                 ProjectionHealth::Invalidated,
   1013                 state.projections[status_index].checkpoint().cloned(),
   1014                 None,
   1015             )?;
   1016             if !state
   1017                 .projection_invalidations
   1018                 .iter()
   1019                 .any(|existing| existing == &invalidation)
   1020             {
   1021                 state.projection_invalidations.push(invalidation);
   1022             }
   1023             state.projections[status_index] = next.clone();
   1024             Ok(next)
   1025         })
   1026     }
   1027 
   1028     fn invalidation(
   1029         &self,
   1030         projection_id: ProjectionId,
   1031         replacement_generation: ProjectionGeneration,
   1032     ) -> BoxFuture<'_, Result<Option<ProjectionInvalidation>, Error>> {
   1033         Box::pin(async move {
   1034             Ok(self
   1035                 .state()?
   1036                 .projection_invalidations
   1037                 .iter()
   1038                 .rev()
   1039                 .find(|invalidation| {
   1040                     invalidation.projection_id() == &projection_id
   1041                         && invalidation.replacement_generation() == replacement_generation
   1042                 })
   1043                 .cloned())
   1044         })
   1045     }
   1046 
   1047     fn request_rebuild(
   1048         &self,
   1049         ticket: RebuildTicket,
   1050     ) -> BoxFuture<'_, Result<RebuildTicket, Error>> {
   1051         Box::pin(async move {
   1052             let mut state = self.state()?;
   1053             if let Some(existing) = state
   1054                 .rebuilds
   1055                 .iter()
   1056                 .find(|existing| existing.ticket_id() == ticket.ticket_id())
   1057             {
   1058                 return if existing == &ticket {
   1059                     Ok(existing.clone())
   1060                 } else {
   1061                     Err(Error::ProjectionRevisionConflict)
   1062                 };
   1063             }
   1064             let projection_id = ticket.invalidation().projection_id();
   1065             let has_invalidation = state
   1066                 .projection_invalidations
   1067                 .iter()
   1068                 .any(|invalidation| invalidation == ticket.invalidation());
   1069             let status = state
   1070                 .projections
   1071                 .iter_mut()
   1072                 .find(|status| status.projection_id() == projection_id)
   1073                 .ok_or(Error::ProjectionCheckpointMismatch)?;
   1074             if status.generation() != ticket.invalidation().invalid_generation()
   1075                 || status.health() != ProjectionHealth::Invalidated
   1076                 || !has_invalidation
   1077             {
   1078                 return Err(Error::ProjectionCheckpointMismatch);
   1079             }
   1080             *status = ProjectionStatus::new(
   1081                 projection_id.clone(),
   1082                 status.generation(),
   1083                 ProjectionHealth::Rebuilding,
   1084                 status.checkpoint().cloned(),
   1085                 Some(ticket.ticket_id()),
   1086             )?;
   1087             state.rebuilds.push(ticket.clone());
   1088             Ok(ticket)
   1089         })
   1090     }
   1091 
   1092     fn rebuild(
   1093         &self,
   1094         ticket_id: RebuildTicketId,
   1095     ) -> BoxFuture<'_, Result<Option<RebuildTicket>, Error>> {
   1096         Box::pin(async move {
   1097             Ok(self
   1098                 .state()?
   1099                 .rebuilds
   1100                 .iter()
   1101                 .find(|ticket| ticket.ticket_id() == ticket_id)
   1102                 .cloned())
   1103         })
   1104     }
   1105 
   1106     fn transition_rebuild(
   1107         &self,
   1108         transition: RebuildTransition,
   1109     ) -> BoxFuture<'_, Result<RebuildTicket, Error>> {
   1110         Box::pin(async move {
   1111             let mut state = self.state()?;
   1112             let index = state
   1113                 .rebuilds
   1114                 .iter()
   1115                 .position(|ticket| ticket.ticket_id() == transition.ticket_id())
   1116                 .ok_or(Error::ProjectionRevisionConflict)?;
   1117             let next = state.rebuilds[index].transition(transition)?;
   1118             if next.stage() == RebuildStage::Completed
   1119                 && state.projection_generations.contains_key(&(
   1120                     next.invalidation().projection_id().clone(),
   1121                     next.invalidation().replacement_generation(),
   1122                 ))
   1123             {
   1124                 return Err(Error::ProjectionRevisionConflict);
   1125             }
   1126             if next.stage() == RebuildStage::Completed
   1127                 && (next.source_generation() != self.generation
   1128                     || next
   1129                         .source_high_water()
   1130                         .map_or(0, |position| position.sequence().get())
   1131                         != u64::try_from(state.events.len())
   1132                             .map_err(|_| Error::CorruptProjectionRecord)?)
   1133             {
   1134                 return Err(Error::SourceGenerationChanged);
   1135             }
   1136             let projection_id = next.invalidation().projection_id();
   1137             let old_identity = (
   1138                 projection_id.clone(),
   1139                 next.invalidation().invalid_generation(),
   1140             );
   1141             let new_identity = (
   1142                 projection_id.clone(),
   1143                 next.invalidation().replacement_generation(),
   1144             );
   1145             let old_registered = state.projection_generations.contains_key(&old_identity);
   1146             if next.stage() == RebuildStage::Completed
   1147                 && (old_registered || !state.projection_cache.protected.contains(&old_identity))
   1148             {
   1149                 // A live registration retains its reservation until explicit retirement.
   1150                 // Only an unregistered legacy checkpoint transfers its existing slot.
   1151                 state.projection_cache.check_protected(&new_identity)?;
   1152             }
   1153             let status = state
   1154                 .projections
   1155                 .iter_mut()
   1156                 .find(|status| status.projection_id() == projection_id)
   1157                 .ok_or(Error::CorruptProjectionRecord)?;
   1158             if status.generation() != next.invalidation().invalid_generation()
   1159                 || status.health() != ProjectionHealth::Rebuilding
   1160                 || status.active_rebuild() != Some(next.ticket_id())
   1161             {
   1162                 return Err(Error::CorruptProjectionRecord);
   1163             }
   1164             let (generation, health, checkpoint, active_rebuild) = match next.stage() {
   1165                 RebuildStage::Requested | RebuildStage::Running => (
   1166                     status.generation(),
   1167                     ProjectionHealth::Rebuilding,
   1168                     status.checkpoint().cloned(),
   1169                     Some(next.ticket_id()),
   1170                 ),
   1171                 RebuildStage::Completed => (
   1172                     next.invalidation().replacement_generation(),
   1173                     ProjectionHealth::Ready,
   1174                     next.checkpoint().cloned(),
   1175                     None,
   1176                 ),
   1177                 RebuildStage::Failed => (
   1178                     status.generation(),
   1179                     ProjectionHealth::Ready,
   1180                     status.checkpoint().cloned(),
   1181                     None,
   1182                 ),
   1183             };
   1184             *status = ProjectionStatus::new(
   1185                 projection_id.clone(),
   1186                 generation,
   1187                 health,
   1188                 checkpoint,
   1189                 active_rebuild,
   1190             )?;
   1191             if next.stage() == RebuildStage::Completed {
   1192                 if !old_registered {
   1193                     state.projection_cache.protected.remove(&old_identity);
   1194                 }
   1195                 state.projection_cache.protected.insert(new_identity);
   1196             }
   1197             state.rebuilds[index] = next.clone();
   1198             Ok(next)
   1199         })
   1200     }
   1201 
   1202     fn event_index_manifest(
   1203         &self,
   1204         generation: ProjectionGeneration,
   1205     ) -> BoxFuture<'_, Result<Option<EventIndexManifest>, Error>> {
   1206         Box::pin(async move {
   1207             Ok(self
   1208                 .state()?
   1209                 .event_index_manifests
   1210                 .iter()
   1211                 .find(|manifest| manifest.generation() == generation)
   1212                 .cloned())
   1213         })
   1214     }
   1215 
   1216     fn put_event_index_manifest(
   1217         &self,
   1218         manifest: EventIndexManifest,
   1219     ) -> BoxFuture<'_, Result<(), Error>> {
   1220         Box::pin(async move {
   1221             let mut state = self.state()?;
   1222             if let Some(existing) = state
   1223                 .event_index_manifests
   1224                 .iter()
   1225                 .find(|existing| existing.generation() == manifest.generation())
   1226             {
   1227                 return if existing == &manifest {
   1228                     Ok(())
   1229                 } else {
   1230                     Err(Error::CorruptProjectionRecord)
   1231                 };
   1232             }
   1233             state.event_index_manifests.push(manifest);
   1234             Ok(())
   1235         })
   1236     }
   1237 
   1238     fn event_index_checkpoint(
   1239         &self,
   1240         generation: ProjectionGeneration,
   1241     ) -> BoxFuture<'_, Result<Option<EventIndexCheckpoint>, Error>> {
   1242         Box::pin(async move {
   1243             Ok(self
   1244                 .state()?
   1245                 .event_index_checkpoints
   1246                 .iter()
   1247                 .find(|checkpoint| checkpoint.generation() == generation)
   1248                 .cloned())
   1249         })
   1250     }
   1251 
   1252     fn put_event_index_checkpoint(
   1253         &self,
   1254         checkpoint: EventIndexCheckpoint,
   1255     ) -> BoxFuture<'_, Result<(), Error>> {
   1256         Box::pin(async move {
   1257             let mut state = self.state()?;
   1258             if let Some(existing) = state
   1259                 .event_index_checkpoints
   1260                 .iter_mut()
   1261                 .find(|existing| existing.generation() == checkpoint.generation())
   1262             {
   1263                 if checkpoint.generated_at_unix_ms() < existing.generated_at_unix_ms() {
   1264                     return Err(Error::InvalidEventIndexCheckpoint);
   1265                 }
   1266                 *existing = checkpoint;
   1267             } else {
   1268                 state.event_index_checkpoints.push(checkpoint);
   1269             }
   1270             Ok(())
   1271         })
   1272     }
   1273 
   1274     fn put_projection_batch(
   1275         &self,
   1276         batch: crate::projection::batch::ProjectionDocumentBatch,
   1277     ) -> BoxFuture<'_, Result<(), Error>> {
   1278         Box::pin(async move {
   1279             let mut state = self.state()?;
   1280             projection_batch::write(&mut state, self.generation, &batch, None)?;
   1281             Ok(())
   1282         })
   1283     }
   1284     fn promote_projection_batch(
   1285         &self,
   1286         batch: crate::projection::batch::ProjectionDocumentBatch,
   1287         checkpoint: ProjectionCheckpoint,
   1288     ) -> BoxFuture<'_, Result<ProjectionStatus, Error>> {
   1289         Box::pin(async move {
   1290             let mut state = self.state()?;
   1291             projection_batch::write(&mut state, self.generation, &batch, Some(checkpoint))?
   1292                 .ok_or(Error::CorruptProjectionRecord)
   1293         })
   1294     }
   1295     fn query_projection_documents_at(
   1296         &self,
   1297         query: crate::projection::document_query::ProjectionDocumentQuery,
   1298         source: EventSourceToken,
   1299         active: ProjectionGeneration,
   1300     ) -> BoxFuture<'_, Result<crate::projection::document_query::ProjectionDocumentPage, Error>>
   1301     {
   1302         Box::pin(async move {
   1303             if query.generations()
   1304                 != crate::projection::document_query::ProjectionDocumentGenerations::Exact(active)
   1305             {
   1306                 return Err(Error::InvalidProjectionDocument);
   1307             }
   1308             let state = self.state()?;
   1309             projection_batch::read_fence(
   1310                 &state,
   1311                 self.generation,
   1312                 source,
   1313                 query.projection_id(),
   1314                 active,
   1315             )?;
   1316             projection_batch::page(&state, &query)
   1317         })
   1318     }
   1319     fn put_projection_document(
   1320         &self,
   1321         projection_id: ProjectionId,
   1322         generation: ProjectionGeneration,
   1323         document: ProjectionDocument,
   1324     ) -> BoxFuture<'_, Result<(), Error>> {
   1325         Box::pin(async move {
   1326             let mut state = self.state()?;
   1327             if state.projection_cache.owners.contains(&projection_id) {
   1328                 return Err(Error::ProjectionRevisionConflict);
   1329             }
   1330             if state
   1331                 .projection_generations
   1332                 .contains_key(&(projection_id.clone(), generation))
   1333             {
   1334                 return Err(Error::ProjectionRevisionConflict);
   1335             }
   1336             let key = (projection_id, generation, document.key().to_owned());
   1337             let bytes = state.projection_cache.check_bytes(
   1338                 state
   1339                     .projection_documents
   1340                     .get(&key)
   1341                     .map_or(0, |value| value.value().len() as u64),
   1342                 document.value().len() as u64,
   1343             )?;
   1344             state.projection_cache.value_bytes = bytes;
   1345             state.projection_documents.insert(key, document);
   1346             Ok(())
   1347         })
   1348     }
   1349     fn query_projection_documents(
   1350         &self,
   1351         query: crate::projection::document_query::ProjectionDocumentQuery,
   1352     ) -> BoxFuture<'_, Result<crate::projection::document_query::ProjectionDocumentPage, Error>>
   1353     {
   1354         Box::pin(async move {
   1355             let state = self.state()?;
   1356             projection_batch::page(&state, &query)
   1357         })
   1358     }
   1359     fn projection_document(
   1360         &self,
   1361         projection_id: ProjectionId,
   1362         generation: ProjectionGeneration,
   1363         key: String,
   1364     ) -> BoxFuture<'_, Result<Option<ProjectionDocument>, Error>> {
   1365         Box::pin(async move {
   1366             projection_batch::validate_key(&key)?;
   1367             Ok(self
   1368                 .state()?
   1369                 .projection_documents
   1370                 .get(&(projection_id, generation, key))
   1371                 .cloned())
   1372         })
   1373     }
   1374 
   1375     fn put_projection_snapshot(
   1376         &self,
   1377         snapshot: ProjectionSnapshot,
   1378     ) -> BoxFuture<'_, Result<(), Error>> {
   1379         Box::pin(async move {
   1380             let mut state = self.state()?;
   1381             let key = (snapshot.projection_id().clone(), *snapshot.snapshot_id());
   1382             if let Some(existing) = state.projection_snapshots.get(&key) {
   1383                 return if existing == &snapshot {
   1384                     Ok(())
   1385                 } else {
   1386                     Err(Error::CorruptProjectionDocument)
   1387                 };
   1388             }
   1389             if state
   1390                 .projection_cache
   1391                 .owners
   1392                 .contains(snapshot.projection_id())
   1393             {
   1394                 return Err(Error::ProjectionRevisionConflict);
   1395             }
   1396             if state.projection_cache.limits.is_some_and(|limit| {
   1397                 state.projection_snapshots.len() as u64 >= limit.legacy_snapshots()
   1398             }) {
   1399                 return Err(Error::SpaceInsufficient);
   1400             }
   1401             let bytes = state
   1402                 .projection_cache
   1403                 .check_bytes(0, snapshot.value().len() as u64)?;
   1404             state.projection_cache.value_bytes = bytes;
   1405             state.projection_cache.snapshot_order.insert((
   1406                 key.0.clone(),
   1407                 snapshot.generation(),
   1408                 snapshot.created_at_unix_ms(),
   1409                 key.1,
   1410             ));
   1411             state.projection_snapshots.insert(key, snapshot);
   1412             Ok(())
   1413         })
   1414     }
   1415     fn projection_snapshot(
   1416         &self,
   1417         id: ProjectionId,
   1418         snapshot: [u8; 32],
   1419     ) -> BoxFuture<'_, Result<Option<ProjectionSnapshot>, Error>> {
   1420         Box::pin(async move {
   1421             let mut state = self.state()?;
   1422             if state.projection_cache.owners.contains(&id) {
   1423                 let now = state.projection_cache.time(projection_cache::now()?);
   1424                 if state
   1425                     .projection_snapshots
   1426                     .get(&(id.clone(), snapshot))
   1427                     .is_some_and(|value| {
   1428                         state
   1429                             .projection_cache
   1430                             .expired(value.created_at_unix_ms(), now)
   1431                     })
   1432                 {
   1433                     return Ok(None);
   1434                 }
   1435             }
   1436             Ok(state.projection_snapshots.get(&(id, snapshot)).cloned())
   1437         })
   1438     }
   1439 }
   1440 
   1441 impl PrivateArtifactStore for MemoryStorage {
   1442     fn put_metadata(
   1443         &self,
   1444         metadata: PrivateArtifactMetadata,
   1445     ) -> BoxFuture<'_, Result<PrivateArtifactMetadata, Error>> {
   1446         Box::pin(async move {
   1447             let mut state = self.state()?;
   1448             if let Some(existing) = state
   1449                 .private_artifacts
   1450                 .iter()
   1451                 .find(|existing| existing.artifact_id() == metadata.artifact_id())
   1452             {
   1453                 return if existing == &metadata {
   1454                     Ok(existing.clone())
   1455                 } else {
   1456                     Err(Error::PrivateArtifactConflict)
   1457                 };
   1458             }
   1459             state.private_artifacts.push(metadata.clone());
   1460             Ok(metadata)
   1461         })
   1462     }
   1463 
   1464     fn metadata(
   1465         &self,
   1466         artifact_id: PrivateArtifactId,
   1467     ) -> BoxFuture<'_, Result<Option<PrivateArtifactMetadata>, Error>> {
   1468         Box::pin(async move {
   1469             Ok(self
   1470                 .state()?
   1471                 .private_artifacts
   1472                 .iter()
   1473                 .find(|metadata| metadata.artifact_id() == artifact_id)
   1474                 .cloned())
   1475         })
   1476     }
   1477 
   1478     fn reseal_metadata(
   1479         &self,
   1480         request: PrivateArtifactResealRequest,
   1481     ) -> BoxFuture<'_, Result<PrivateArtifactResealReceipt, Error>> {
   1482         Box::pin(async move {
   1483             let mut state = self.state()?;
   1484             if let Some(receipt) = state
   1485                 .private_artifact_reseals
   1486                 .iter()
   1487                 .find(|receipt| receipt.reseal_id() == request.reseal_id())
   1488             {
   1489                 return receipt.replay(&request);
   1490             }
   1491             let metadata = state
   1492                 .private_artifacts
   1493                 .iter_mut()
   1494                 .find(|metadata| metadata.artifact_id() == request.artifact_id())
   1495                 .ok_or(Error::PrivateArtifactNotFound)?;
   1496             let next = metadata.resealed(&request)?;
   1497             let receipt = PrivateArtifactResealReceipt::committed(&request, next.revision());
   1498             *metadata = next;
   1499             state.private_artifact_reseals.push(receipt);
   1500             Ok(receipt)
   1501         })
   1502     }
   1503 
   1504     fn mark_expired(
   1505         &self,
   1506         artifact_id: PrivateArtifactId,
   1507         expected_revision: PrivateArtifactRevision,
   1508         at_unix_ms: u64,
   1509     ) -> BoxFuture<'_, Result<PrivateArtifactMetadata, Error>> {
   1510         Box::pin(async move {
   1511             let mut state = self.state()?;
   1512             let metadata = state
   1513                 .private_artifacts
   1514                 .iter_mut()
   1515                 .find(|metadata| metadata.artifact_id() == artifact_id)
   1516                 .ok_or(Error::PrivateArtifactNotFound)?;
   1517             let next = metadata.mark_expired(expected_revision, at_unix_ms)?;
   1518             *metadata = next.clone();
   1519             Ok(next)
   1520         })
   1521     }
   1522 
   1523     fn tombstone(
   1524         &self,
   1525         artifact_id: PrivateArtifactId,
   1526         expected_revision: PrivateArtifactRevision,
   1527         at_unix_ms: u64,
   1528         reason: DeletionReason,
   1529     ) -> BoxFuture<'_, Result<PrivateArtifactMetadata, Error>> {
   1530         Box::pin(async move {
   1531             let mut state = self.state()?;
   1532             let metadata = state
   1533                 .private_artifacts
   1534                 .iter_mut()
   1535                 .find(|metadata| metadata.artifact_id() == artifact_id)
   1536                 .ok_or(Error::PrivateArtifactNotFound)?;
   1537             let next = metadata.tombstone(expected_revision, at_unix_ms, reason)?;
   1538             *metadata = next.clone();
   1539             Ok(next)
   1540         })
   1541     }
   1542 
   1543     fn expired(
   1544         &self,
   1545         at_unix_ms: u64,
   1546         limit: u16,
   1547     ) -> BoxFuture<'_, Result<Vec<PrivateArtifactMetadata>, Error>> {
   1548         Box::pin(async move {
   1549             if at_unix_ms == 0 || limit == 0 || limit > EXPIRED_ARTIFACT_QUERY_LIMIT_MAX {
   1550                 return Err(Error::InvalidExpiredArtifactQueryLimit);
   1551             }
   1552             Ok(self
   1553                 .state()?
   1554                 .private_artifacts
   1555                 .iter()
   1556                 .filter(|metadata| {
   1557                     metadata.stage() == PrivateArtifactStage::Active
   1558                         && metadata.retention().is_expired_at(at_unix_ms)
   1559                 })
   1560                 .take(usize::from(limit))
   1561                 .cloned()
   1562                 .collect())
   1563         })
   1564     }
   1565 
   1566     fn status(&self) -> BoxFuture<'_, Result<PrivateArtifactStatus, Error>> {
   1567         Box::pin(async move {
   1568             let state = self.state()?;
   1569             let mut status = PrivateArtifactStatus {
   1570                 active: 0,
   1571                 expired: 0,
   1572                 tombstoned: 0,
   1573             };
   1574             for metadata in &state.private_artifacts {
   1575                 let count = match metadata.stage() {
   1576                     PrivateArtifactStage::Active => &mut status.active,
   1577                     PrivateArtifactStage::Expired => &mut status.expired,
   1578                     PrivateArtifactStage::Tombstoned => &mut status.tombstoned,
   1579                 };
   1580                 *count = count
   1581                     .checked_add(1)
   1582                     .ok_or(Error::CorruptPrivateArtifactMetadata)?;
   1583             }
   1584             Ok(status)
   1585         })
   1586     }
   1587 }
   1588 
   1589 impl StorageReliability for MemoryStorage {
   1590     fn begin_backup(&self, plan: BackupPlan) -> BoxFuture<'_, Result<BackupOperation, Error>> {
   1591         Box::pin(async move {
   1592             let mut state = self.state()?;
   1593             if let Some(existing) = state
   1594                 .backups
   1595                 .iter()
   1596                 .find(|operation| operation.plan().backup_id() == plan.backup_id())
   1597             {
   1598                 return if existing.plan() == &plan {
   1599                     Ok(existing.clone())
   1600                 } else {
   1601                     Err(Error::ReliabilityRevisionConflict)
   1602                 };
   1603             }
   1604             let operation = BackupOperation::planned(plan);
   1605             state.backups.push(operation.clone());
   1606             Ok(operation)
   1607         })
   1608     }
   1609 
   1610     fn transition_backup(
   1611         &self,
   1612         backup_id: BackupId,
   1613         expected_revision: ReliabilityRevision,
   1614         transition: BackupTransition,
   1615         at_unix_ms: u64,
   1616     ) -> BoxFuture<'_, Result<BackupOperation, Error>> {
   1617         Box::pin(async move {
   1618             let mut state = self.state()?;
   1619             let operation = state
   1620                 .backups
   1621                 .iter_mut()
   1622                 .find(|operation| operation.plan().backup_id() == backup_id)
   1623                 .ok_or(Error::CorruptReliabilityOperation)?;
   1624             let next = operation.transition(expected_revision, transition, at_unix_ms)?;
   1625             *operation = next.clone();
   1626             Ok(next)
   1627         })
   1628     }
   1629 
   1630     fn begin_restore(&self, plan: RestorePlan) -> BoxFuture<'_, Result<RestoreOperation, Error>> {
   1631         Box::pin(async move {
   1632             let mut state = self.state()?;
   1633             let backup_id = plan.manifest().backup_id();
   1634             if let Some(existing) = state
   1635                 .restores
   1636                 .iter()
   1637                 .find(|operation| operation.plan().manifest().backup_id() == backup_id)
   1638             {
   1639                 return if existing.plan() == &plan {
   1640                     Ok(existing.clone())
   1641                 } else {
   1642                     Err(Error::ReliabilityRevisionConflict)
   1643                 };
   1644             }
   1645             let operation = RestoreOperation::staging(plan);
   1646             state.restores.push(operation.clone());
   1647             Ok(operation)
   1648         })
   1649     }
   1650 
   1651     fn transition_restore(
   1652         &self,
   1653         backup_id: BackupId,
   1654         expected_revision: ReliabilityRevision,
   1655         transition: RestoreTransition,
   1656         at_unix_ms: u64,
   1657     ) -> BoxFuture<'_, Result<RestoreOperation, Error>> {
   1658         Box::pin(async move {
   1659             let mut state = self.state()?;
   1660             let operation = state
   1661                 .restores
   1662                 .iter_mut()
   1663                 .find(|operation| operation.plan().manifest().backup_id() == backup_id)
   1664                 .ok_or(Error::CorruptReliabilityOperation)?;
   1665             let next = operation.transition(expected_revision, transition, at_unix_ms)?;
   1666             *operation = next.clone();
   1667             Ok(next)
   1668         })
   1669     }
   1670 
   1671     fn integrity(&self) -> BoxFuture<'_, Result<IntegrityStatus, Error>> {
   1672         Box::pin(async move {
   1673             let state = self.state_any()?;
   1674             Self::integrity_locked(&state)
   1675         })
   1676     }
   1677 
   1678     fn status(&self) -> BoxFuture<'_, Result<StorageStatus, Error>> {
   1679         Box::pin(async move {
   1680             let state = self.state_any()?;
   1681             Self::status_locked(&state)
   1682         })
   1683     }
   1684 
   1685     fn close(&self) -> BoxFuture<'_, Result<StorageStatus, Error>> {
   1686         Box::pin(async move {
   1687             let mut state = self.state_any()?;
   1688             state.closed = true;
   1689             Self::status_locked(&state)
   1690         })
   1691     }
   1692 }
   1693 
   1694 impl StorageStatusProvider for MemoryStorage {
   1695     fn storage_status(&self) -> BoxFuture<'_, Result<StorageStatus, Error>> {
   1696         Box::pin(async move {
   1697             let state = self.state_any()?;
   1698             Self::status_locked(&state)
   1699         })
   1700     }
   1701 }
   1702 
   1703 impl AtomicStorage for MemoryStorage {
   1704     fn commit(&self, request: AtomicCommit) -> BoxFuture<'_, Result<AtomicCommitReceipt, Error>> {
   1705         Box::pin(async move {
   1706             let mut state = self.state()?;
   1707             if let Some(existing) = state
   1708                 .atomic_receipts
   1709                 .iter()
   1710                 .find(|receipt| receipt.commit_id() == request.commit_id())
   1711             {
   1712                 if existing.digest() != request.digest()
   1713                     || existing.outcome().kind() != request.workflow().kind()
   1714                 {
   1715                     return Err(Error::AtomicCommitConflict);
   1716                 }
   1717                 return AtomicCommitReceipt::new(
   1718                     &request,
   1719                     AtomicCommitDisposition::Replay,
   1720                     existing.committed_at_unix_ms(),
   1721                     existing.outcome().clone(),
   1722                 );
   1723             }
   1724             let mut candidate = state.clone();
   1725             let outcome = match request.workflow().clone() {
   1726                 AtomicWorkflow::Ingested(ingested) => {
   1727                     let admission =
   1728                         self.admit_locked(&mut candidate, ingested.admission().clone())?;
   1729                     let projection = ingested
   1730                         .projection()
   1731                         .cloned()
   1732                         .map(|checkpoint| Self::checkpoint_locked(&mut candidate, checkpoint))
   1733                         .transpose()?
   1734                         .map(Box::new);
   1735                     AtomicCommitOutcome::Ingested {
   1736                         admission,
   1737                         projection,
   1738                     }
   1739                 }
   1740             };
   1741             let receipt = AtomicCommitReceipt::new(
   1742                 &request,
   1743                 AtomicCommitDisposition::Committed,
   1744                 request.requested_at_unix_ms(),
   1745                 outcome,
   1746             )?;
   1747             candidate.atomic_receipts.push(receipt.clone());
   1748             *state = candidate;
   1749             Ok(receipt)
   1750         })
   1751     }
   1752 
   1753     fn receipt(
   1754         &self,
   1755         commit_id: AtomicCommitId,
   1756     ) -> BoxFuture<'_, Result<Option<AtomicCommitReceipt>, Error>> {
   1757         Box::pin(async move {
   1758             Ok(self
   1759                 .state()?
   1760                 .atomic_receipts
   1761                 .iter()
   1762                 .find(|receipt| receipt.commit_id() == commit_id)
   1763                 .cloned())
   1764         })
   1765     }
   1766 }
   1767 
   1768 fn prepare_authored_memory(
   1769     candidate: &mut State,
   1770     value: crate::authored_atomic::PrepareAuthoredOperation,
   1771 ) -> Result<AuthoredAtomicOutcome, Error> {
   1772     if candidate
   1773         .authored_operations
   1774         .iter()
   1775         .any(|operation| operation.operation_id() == value.operation().operation_id())
   1776         || value.artifacts().iter().any(|artifact| {
   1777             candidate
   1778                 .authored_artifacts
   1779                 .iter()
   1780                 .any(|existing| existing.artifact_id() == artifact.artifact_id())
   1781         })
   1782         || value.delivery_plans().iter().any(|plan| {
   1783             candidate
   1784                 .authored_delivery_plans
   1785                 .iter()
   1786                 .any(|existing| existing.plan_id() == plan.plan_id())
   1787         })
   1788     {
   1789         return Err(Error::AtomicCommitConflict);
   1790     }
   1791     candidate
   1792         .authored_operations
   1793         .push(value.operation().clone());
   1794     candidate
   1795         .authored_artifacts
   1796         .extend(value.artifacts().iter().cloned());
   1797     candidate
   1798         .authored_delivery_plans
   1799         .extend(value.delivery_plans().iter().cloned());
   1800     Ok(AuthoredAtomicOutcome::Prepared {
   1801         operation: value.operation().clone(),
   1802         artifacts: value.artifacts().to_vec(),
   1803         delivery_plans: value.delivery_plans().to_vec(),
   1804     })
   1805 }
   1806 
   1807 impl AuthoredAtomicStorage for MemoryStorage {
   1808     fn authored_delivery_history(
   1809         &self,
   1810         plan_id: crate::authored_delivery::AuthoredDeliveryPlanId,
   1811     ) -> BoxFuture<'_, Result<Option<crate::authored_delivery::AuthoredDeliveryHistory>, Error>>
   1812     {
   1813         Box::pin(async move {
   1814             let state = self.state()?;
   1815             delivery_history::history(&state, plan_id)
   1816         })
   1817     }
   1818     fn execute_authored(
   1819         &self,
   1820         command: AuthoredAtomicCommand,
   1821     ) -> BoxFuture<'_, Result<AuthoredAtomicReceipt, Error>> {
   1822         Box::pin(async move {
   1823             let mut state = self.state()?;
   1824             if let Some(existing) = state
   1825                 .authored_atomic_receipts
   1826                 .iter()
   1827                 .find(|receipt| receipt.commit_id() == command.commit_id())
   1828             {
   1829                 if !existing.matches_command(&command) {
   1830                     return Err(Error::AtomicCommitConflict);
   1831                 }
   1832                 return AuthoredAtomicReceipt::from_durable_parts(
   1833                     existing.commit_id(),
   1834                     existing.digest(),
   1835                     AtomicCommitDisposition::Replay,
   1836                     existing.committed_at_unix_ms(),
   1837                     existing.outcome().clone(),
   1838                 );
   1839             }
   1840 
   1841             let mut candidate = state.clone();
   1842             let outcome = match command.clone() {
   1843                 AuthoredAtomicCommand::Prepare(value) => {
   1844                     prepare_authored_memory(&mut candidate, value)?
   1845                 }
   1846                 AuthoredAtomicCommand::PrepareFromDraft(value) => {
   1847                     value.validate()?;
   1848                     let head = candidate
   1849                         .authored_drafts
   1850                         .iter()
   1851                         .filter(|draft| draft.draft_id() == value.source().draft_id())
   1852                         .max_by_key(|draft| draft.revision());
   1853                     if !head.is_some_and(|head| value.source().matches(head)) {
   1854                         return Err(Error::DraftRevisionConflict);
   1855                     }
   1856                     if candidate
   1857                         .authored_drafts
   1858                         .iter()
   1859                         .any(|draft| draft.draft_id() == value.intent().draft_id())
   1860                     {
   1861                         return Err(Error::DraftRevisionConflict);
   1862                     }
   1863                     let ordinary = AuthoredAtomicCommand::Prepare(value.preparation().clone());
   1864                     if candidate
   1865                         .authored_atomic_receipts
   1866                         .iter()
   1867                         .any(|receipt| receipt.commit_id() == ordinary.commit_id())
   1868                     {
   1869                         return Err(Error::AtomicCommitConflict);
   1870                     }
   1871                     let prepared =
   1872                         prepare_authored_memory(&mut candidate, value.preparation().clone())?;
   1873                     candidate.authored_drafts.push(value.intent().clone());
   1874                     let ordinary_index = candidate.authored_atomic_receipts.len();
   1875                     delivery_history::register(&mut candidate, &ordinary, ordinary_index)?;
   1876                     candidate
   1877                         .authored_atomic_receipts
   1878                         .push(AuthoredAtomicReceipt::new(
   1879                             &ordinary,
   1880                             AtomicCommitDisposition::Committed,
   1881                             ordinary.requested_at_unix_ms(),
   1882                             prepared,
   1883                         )?);
   1884                     AuthoredAtomicOutcome::Submitted(value)
   1885                 }
   1886                 AuthoredAtomicCommand::Claim(value) => match value.target() {
   1887                     ClaimAuthoredTarget::ArtifactSigning(artifact_id) => {
   1888                         let artifact = candidate
   1889                             .authored_artifacts
   1890                             .iter_mut()
   1891                             .find(|artifact| artifact.artifact_id() == *artifact_id)
   1892                             .ok_or(Error::InvalidAuthoredArtifact)?;
   1893                         artifact.set_signing_claim(
   1894                             value.claim().clone(),
   1895                             value.claim().acquired_at_unix_ms(),
   1896                         )?;
   1897                         AuthoredAtomicOutcome::Artifact(artifact.clone())
   1898                     }
   1899                     ClaimAuthoredTarget::ArtifactAdmission(artifact_id) => {
   1900                         let artifact = candidate
   1901                             .authored_artifacts
   1902                             .iter_mut()
   1903                             .find(|artifact| artifact.artifact_id() == *artifact_id)
   1904                             .ok_or(Error::InvalidAuthoredArtifact)?;
   1905                         artifact.set_admission_claim(
   1906                             value.claim().clone(),
   1907                             value.claim().acquired_at_unix_ms(),
   1908                         )?;
   1909                         AuthoredAtomicOutcome::Artifact(artifact.clone())
   1910                     }
   1911                     ClaimAuthoredTarget::DeliveryPlan(plan_id) => {
   1912                         let artifact_id = candidate
   1913                             .authored_delivery_plans
   1914                             .iter()
   1915                             .find(|plan| plan.plan_id() == *plan_id)
   1916                             .ok_or(Error::InvalidAuthoredDeliveryPlan)?
   1917                             .artifact_id();
   1918                         let artifact = candidate
   1919                             .authored_artifacts
   1920                             .iter()
   1921                             .find(|artifact| artifact.artifact_id() == artifact_id)
   1922                             .ok_or(Error::InvalidAuthoredArtifact)?;
   1923                         if artifact.signing_state() != crate::authored::SigningState::Signed {
   1924                             return Err(Error::InvalidAuthoredTransition);
   1925                         }
   1926                         let plan = candidate
   1927                             .authored_delivery_plans
   1928                             .iter_mut()
   1929                             .find(|plan| plan.plan_id() == *plan_id)
   1930                             .ok_or(Error::InvalidAuthoredDeliveryPlan)?;
   1931                         plan.claim(value.claim().clone(), value.claim().acquired_at_unix_ms())?;
   1932                         AuthoredAtomicOutcome::DeliveryPlan(plan.clone())
   1933                     }
   1934                 },
   1935                 AuthoredAtomicCommand::ApplySigned(value) => {
   1936                     let artifact = candidate
   1937                         .authored_artifacts
   1938                         .iter_mut()
   1939                         .find(|artifact| artifact.artifact_id() == value.artifact_id())
   1940                         .ok_or(Error::InvalidAuthoredArtifact)?;
   1941                     require_artifact_claim(
   1942                         artifact.signing_claim(),
   1943                         value.fence(),
   1944                         value.applied_at_unix_ms(),
   1945                     )?;
   1946                     artifact.record_signed(value.event().clone(), value.applied_at_unix_ms())?;
   1947                     let artifact = artifact.clone();
   1948                     for plan in candidate
   1949                         .authored_delivery_plans
   1950                         .iter_mut()
   1951                         .filter(|plan| plan.artifact_id() == value.artifact_id())
   1952                     {
   1953                         plan.bind_signed_event(value.event().clone(), value.applied_at_unix_ms())?;
   1954                     }
   1955                     AuthoredAtomicOutcome::Artifact(artifact)
   1956                 }
   1957                 AuthoredAtomicCommand::ApplyAdmission(value) => {
   1958                     let artifact = candidate
   1959                         .authored_artifacts
   1960                         .iter_mut()
   1961                         .find(|artifact| artifact.artifact_id() == value.artifact_id())
   1962                         .ok_or(Error::InvalidAuthoredArtifact)?;
   1963                     require_artifact_claim(
   1964                         artifact.admission_claim(),
   1965                         value.fence(),
   1966                         value.applied_at_unix_ms(),
   1967                     )?;
   1968                     artifact.record_admission(
   1969                         value.state(),
   1970                         value.failure().cloned(),
   1971                         value.retry().cloned(),
   1972                         value.applied_at_unix_ms(),
   1973                     )?;
   1974                     AuthoredAtomicOutcome::Artifact(artifact.clone())
   1975                 }
   1976                 AuthoredAtomicCommand::RecordSigned(value) => {
   1977                     let claim_id = value.claim_command().commit_id();
   1978                     let original = candidate
   1979                         .authored_atomic_receipts
   1980                         .iter()
   1981                         .find(|receipt| receipt.commit_id() == claim_id)
   1982                         .ok_or(Error::AtomicWorkflowMismatch)?;
   1983                     let artifact = candidate
   1984                         .authored_artifacts
   1985                         .iter_mut()
   1986                         .find(|artifact| artifact.artifact_id() == value.artifact_id())
   1987                         .ok_or(Error::InvalidAuthoredArtifact)?;
   1988                     let already_signed = artifact.signed().is_some();
   1989                     value.apply_to(artifact, original)?;
   1990                     let artifact = artifact.clone();
   1991                     if !already_signed
   1992                         && artifact.signing_state() == crate::authored::SigningState::Signed
   1993                     {
   1994                         for plan in candidate.authored_delivery_plans.iter_mut().filter(|plan| {
   1995                             plan.artifact_id() == value.artifact_id() && !plan.state().is_terminal()
   1996                         }) {
   1997                             plan.bind_signed_event(
   1998                                 value.event().clone(),
   1999                                 value.observed_at_unix_ms().max(plan.updated_at_unix_ms()),
   2000                             )?;
   2001                         }
   2002                     }
   2003                     AuthoredAtomicOutcome::Artifact(artifact)
   2004                 }
   2005                 AuthoredAtomicCommand::RecordDelivery(value) => {
   2006                     let claim_id = value.claim_command().commit_id();
   2007                     let original = candidate
   2008                         .authored_atomic_receipts
   2009                         .iter()
   2010                         .find(|receipt| receipt.commit_id() == claim_id)
   2011                         .ok_or(Error::AtomicWorkflowMismatch)?;
   2012                     let plan = candidate
   2013                         .authored_delivery_plans
   2014                         .iter_mut()
   2015                         .find(|plan| plan.plan_id() == value.plan_id())
   2016                         .ok_or(Error::InvalidAuthoredDeliveryPlan)?;
   2017                     value.apply_to(plan, original)?;
   2018                     AuthoredAtomicOutcome::DeliveryPlan(plan.clone())
   2019                 }
   2020                 AuthoredAtomicCommand::ReconcileDelivery(value) => {
   2021                     let plan = delivery_history::reconcile(&mut candidate, &value)?;
   2022                     AuthoredAtomicOutcome::DeliveryPlan(plan)
   2023                 }
   2024                 AuthoredAtomicCommand::ApplyDelivery(value) => {
   2025                     let plan = candidate
   2026                         .authored_delivery_plans
   2027                         .iter_mut()
   2028                         .find(|plan| plan.plan_id() == value.plan_id())
   2029                         .ok_or(Error::InvalidAuthoredDeliveryPlan)?;
   2030                     match value.outcome().clone() {
   2031                         DeliveryAttemptOutcome::Receipt(receipt) => plan.apply_receipt(
   2032                             value.fence().token(),
   2033                             value.fence().generation(),
   2034                             value.fence().row_revision(),
   2035                             receipt,
   2036                             value.retry().cloned(),
   2037                             value.applied_at_unix_ms(),
   2038                         )?,
   2039                         DeliveryAttemptOutcome::SinkFailure(failure) => plan.apply_sink_failure(
   2040                             value.fence().token(),
   2041                             value.fence().generation(),
   2042                             value.fence().row_revision(),
   2043                             failure,
   2044                             value.retry().cloned(),
   2045                             value.applied_at_unix_ms(),
   2046                         )?,
   2047                     }
   2048                     AuthoredAtomicOutcome::DeliveryPlan(plan.clone())
   2049                 }
   2050                 AuthoredAtomicCommand::ApplyFailure(value) => match value.target() {
   2051                     AuthoredWorkTarget::Artifact(artifact_id) => {
   2052                         let artifact = candidate
   2053                             .authored_artifacts
   2054                             .iter_mut()
   2055                             .find(|artifact| artifact.artifact_id() == *artifact_id)
   2056                             .ok_or(Error::InvalidAuthoredArtifact)?;
   2057                         match value.failure().phase() {
   2058                             WorkPhase::Signing => {
   2059                                 require_artifact_claim(
   2060                                     artifact.signing_claim(),
   2061                                     value.fence(),
   2062                                     value.applied_at_unix_ms(),
   2063                                 )?;
   2064                                 artifact.record_signing_failure(
   2065                                     value.failure().clone(),
   2066                                     value.retry().cloned(),
   2067                                     value.applied_at_unix_ms(),
   2068                                 )?;
   2069                             }
   2070                             WorkPhase::Admission => {
   2071                                 require_artifact_claim(
   2072                                     artifact.admission_claim(),
   2073                                     value.fence(),
   2074                                     value.applied_at_unix_ms(),
   2075                                 )?;
   2076                                 let state = match value.failure().class() {
   2077                                     FailureClass::Retryable => AdmissionState::Retryable,
   2078                                     FailureClass::Terminal => AdmissionState::Rejected,
   2079                                     FailureClass::Indeterminate => {
   2080                                         return Err(Error::InvalidAuthoredTransition);
   2081                                     }
   2082                                 };
   2083                                 artifact.record_admission(
   2084                                     state,
   2085                                     Some(value.failure().clone()),
   2086                                     value.retry().cloned(),
   2087                                     value.applied_at_unix_ms(),
   2088                                 )?;
   2089                             }
   2090                             WorkPhase::Delivery => {
   2091                                 return Err(Error::AtomicWorkflowMismatch);
   2092                             }
   2093                         }
   2094                         AuthoredAtomicOutcome::Artifact(artifact.clone())
   2095                     }
   2096                     AuthoredWorkTarget::DeliveryPlan(plan_id) => {
   2097                         let plan = candidate
   2098                             .authored_delivery_plans
   2099                             .iter_mut()
   2100                             .find(|plan| plan.plan_id() == *plan_id)
   2101                             .ok_or(Error::InvalidAuthoredDeliveryPlan)?;
   2102                         if value.failure().phase() != WorkPhase::Delivery
   2103                             || value.failure().class() == FailureClass::Indeterminate
   2104                         {
   2105                             return Err(Error::AtomicWorkflowMismatch);
   2106                         }
   2107                         let retryability = match value.failure().class() {
   2108                             FailureClass::Retryable => {
   2109                                 radroots_transport::outcome::Retryability::Retryable
   2110                             }
   2111                             FailureClass::Terminal => {
   2112                                 radroots_transport::outcome::Retryability::Terminal
   2113                             }
   2114                             FailureClass::Indeterminate => unreachable!(),
   2115                         };
   2116                         let failure = radroots_transport::SinkFailure::for_request(
   2117                             plan.request().ok_or(Error::InvalidAuthoredDeliveryPlan)?,
   2118                             value.failure().code(),
   2119                             retryability,
   2120                             value.failure().retry_after_unix_ms(),
   2121                             value.failure().diagnostic().map(str::to_owned),
   2122                             Vec::new(),
   2123                         )
   2124                         .map_err(|_| Error::AtomicWorkflowMismatch)?;
   2125                         plan.apply_sink_failure(
   2126                             value.fence().token(),
   2127                             value.fence().generation(),
   2128                             value.fence().row_revision(),
   2129                             failure,
   2130                             value.retry().cloned(),
   2131                             value.applied_at_unix_ms(),
   2132                         )?;
   2133                         AuthoredAtomicOutcome::DeliveryPlan(plan.clone())
   2134                     }
   2135                 },
   2136                 AuthoredAtomicCommand::Cancel(value) => match value.target() {
   2137                     CancelAuthoredTarget::ArtifactSigning(artifact_id) => {
   2138                         let artifact = candidate
   2139                             .authored_artifacts
   2140                             .iter_mut()
   2141                             .find(|artifact| artifact.artifact_id() == *artifact_id)
   2142                             .ok_or(Error::InvalidAuthoredArtifact)?;
   2143                         if artifact.revision() != value.expected_revision() {
   2144                             return Err(Error::InvalidAuthoredTransition);
   2145                         }
   2146                         artifact.cancel_signing(value.cancelled_at_unix_ms())?;
   2147                         AuthoredAtomicOutcome::Artifact(artifact.clone())
   2148                     }
   2149                     CancelAuthoredTarget::ArtifactAdmission(artifact_id) => {
   2150                         let artifact = candidate
   2151                             .authored_artifacts
   2152                             .iter_mut()
   2153                             .find(|artifact| artifact.artifact_id() == *artifact_id)
   2154                             .ok_or(Error::InvalidAuthoredArtifact)?;
   2155                         if artifact.revision() != value.expected_revision() {
   2156                             return Err(Error::InvalidAuthoredTransition);
   2157                         }
   2158                         let failure = WorkFailure::new(
   2159                             "cancelled",
   2160                             WorkPhase::Admission,
   2161                             FailureClass::Terminal,
   2162                             None,
   2163                             None,
   2164                         )?;
   2165                         artifact.record_admission(
   2166                             AdmissionState::Cancelled,
   2167                             Some(failure),
   2168                             None,
   2169                             value.cancelled_at_unix_ms(),
   2170                         )?;
   2171                         AuthoredAtomicOutcome::Artifact(artifact.clone())
   2172                     }
   2173                     CancelAuthoredTarget::DeliveryPlan(plan_id) => {
   2174                         let plan = candidate
   2175                             .authored_delivery_plans
   2176                             .iter_mut()
   2177                             .find(|plan| plan.plan_id() == *plan_id)
   2178                             .ok_or(Error::InvalidAuthoredDeliveryPlan)?;
   2179                         if plan.revision() != value.expected_revision() {
   2180                             return Err(Error::InvalidAuthoredDeliveryPlan);
   2181                         }
   2182                         plan.request_stop(value.cancelled_at_unix_ms())?;
   2183                         AuthoredAtomicOutcome::DeliveryPlan(plan.clone())
   2184                     }
   2185                 },
   2186             };
   2187             let committed_at = match (&command, &outcome) {
   2188                 (
   2189                     AuthoredAtomicCommand::RecordDelivery(_),
   2190                     AuthoredAtomicOutcome::DeliveryPlan(plan),
   2191                 ) => command
   2192                     .requested_at_unix_ms()
   2193                     .max(plan.updated_at_unix_ms()),
   2194                 (
   2195                     AuthoredAtomicCommand::RecordSigned(_),
   2196                     AuthoredAtomicOutcome::Artifact(artifact),
   2197                 ) => command
   2198                     .requested_at_unix_ms()
   2199                     .max(artifact.updated_at_unix_ms()),
   2200                 _ => command.requested_at_unix_ms(),
   2201             };
   2202             let receipt = AuthoredAtomicReceipt::new(
   2203                 &command,
   2204                 AtomicCommitDisposition::Committed,
   2205                 committed_at,
   2206                 outcome,
   2207             )?;
   2208             let receipt_index = candidate.authored_atomic_receipts.len();
   2209             delivery_history::register(&mut candidate, &command, receipt_index)?;
   2210             candidate.authored_atomic_receipts.push(receipt.clone());
   2211             *state = candidate;
   2212             Ok(receipt)
   2213         })
   2214     }
   2215 
   2216     fn authored_receipt(
   2217         &self,
   2218         commit_id: AtomicCommitId,
   2219     ) -> BoxFuture<'_, Result<Option<AuthoredAtomicReceipt>, Error>> {
   2220         Box::pin(async move {
   2221             Ok(self
   2222                 .state()?
   2223                 .authored_atomic_receipts
   2224                 .iter()
   2225                 .find(|receipt| receipt.commit_id() == commit_id)
   2226                 .cloned())
   2227         })
   2228     }
   2229 
   2230     fn authored_operation(
   2231         &self,
   2232         operation_id: OperationInstanceId,
   2233     ) -> BoxFuture<'_, Result<Option<crate::authored::AuthoredOperation>, Error>> {
   2234         Box::pin(async move {
   2235             Ok(self
   2236                 .state()?
   2237                 .authored_operations
   2238                 .iter()
   2239                 .find(|operation| operation.operation_id() == operation_id)
   2240                 .cloned())
   2241         })
   2242     }
   2243 
   2244     fn authored_artifact(
   2245         &self,
   2246         artifact_id: crate::authored::AuthoredArtifactId,
   2247     ) -> BoxFuture<'_, Result<Option<crate::authored::AuthoredArtifact>, Error>> {
   2248         Box::pin(async move {
   2249             Ok(self
   2250                 .state()?
   2251                 .authored_artifacts
   2252                 .iter()
   2253                 .find(|artifact| artifact.artifact_id() == artifact_id)
   2254                 .cloned())
   2255         })
   2256     }
   2257 
   2258     fn authored_delivery_plan(
   2259         &self,
   2260         plan_id: crate::authored_delivery::AuthoredDeliveryPlanId,
   2261     ) -> BoxFuture<'_, Result<Option<crate::authored_delivery::AuthoredDeliveryPlan>, Error>> {
   2262         Box::pin(async move {
   2263             Ok(self
   2264                 .state()?
   2265                 .authored_delivery_plans
   2266                 .iter()
   2267                 .find(|plan| plan.plan_id() == plan_id)
   2268                 .cloned())
   2269         })
   2270     }
   2271 }
   2272 
   2273 impl AuthoredDraftStore for MemoryStorage {
   2274     fn append_authored_draft_pair(
   2275         &self,
   2276         pair: crate::authored_draft_pair::AuthoredDraftPair,
   2277     ) -> BoxFuture<'_, Result<[DraftAppendReceipt; 2], Error>> {
   2278         Box::pin(async move {
   2279             let mut state = self.state()?;
   2280             let [first, second] = pair.drafts();
   2281             let [first_expected, second_expected] = *pair.expected_heads();
   2282             let a = draft_append_disposition(&state.authored_drafts, first, first_expected)?;
   2283             let b = draft_append_disposition(&state.authored_drafts, second, second_expected)?;
   2284             if a != b {
   2285                 return Err(Error::DraftRevisionConflict);
   2286             }
   2287             if a == DraftAppendDisposition::Inserted {
   2288                 state
   2289                     .authored_drafts
   2290                     .extend([first.clone(), second.clone()]);
   2291             }
   2292             Ok([
   2293                 DraftAppendReceipt::new(first.clone(), a),
   2294                 DraftAppendReceipt::new(second.clone(), b),
   2295             ])
   2296         })
   2297     }
   2298 
   2299     fn query_authored_drafts(
   2300         &self,
   2301         query: crate::authored_draft_query::AuthoredDraftQuery,
   2302     ) -> BoxFuture<'_, Result<crate::authored_draft_query::AuthoredDraftPage, Error>> {
   2303         Box::pin(async move {
   2304             use crate::authored_draft_query::{
   2305                 AUTHORED_DRAFT_PAGE_PAYLOAD_MAX_BYTES, AuthoredDraftPage, AuthoredDraftQueryRecord,
   2306             };
   2307             let state = self.state()?;
   2308             let mut heads: std::collections::BTreeMap<AuthoredDraftId, &AuthoredDraft> =
   2309                 std::collections::BTreeMap::new();
   2310             let capacity = usize::from(query.limit()) + 1;
   2311             for draft in &state.authored_drafts {
   2312                 if !query.matches(draft)
   2313                     || query
   2314                         .after()
   2315                         .is_some_and(|after| *draft.draft_id().as_bytes() <= after)
   2316                 {
   2317                     continue;
   2318                 }
   2319                 if let Some(head) = heads.get_mut(&draft.draft_id()) {
   2320                     if draft.revision() > head.revision() {
   2321                         *head = draft;
   2322                     }
   2323                     continue;
   2324                 }
   2325                 // Retain only the smallest requested IDs and one lookahead.
   2326                 // Draft identity metadata is immutable across revisions.
   2327                 if heads.len() == capacity {
   2328                     if heads
   2329                         .last_key_value()
   2330                         .is_some_and(|(last, _)| draft.draft_id() >= *last)
   2331                     {
   2332                         continue;
   2333                     }
   2334                     heads.pop_last();
   2335                 }
   2336                 heads.insert(draft.draft_id(), draft);
   2337             }
   2338             let mut records = Vec::new();
   2339             let mut bytes = 0usize;
   2340             let mut has_more = false;
   2341             for draft in heads.values() {
   2342                 if records.len() == usize::from(query.limit())
   2343                     || bytes + draft.payload().len() > AUTHORED_DRAFT_PAGE_PAYLOAD_MAX_BYTES
   2344                 {
   2345                     has_more = true;
   2346                     break;
   2347                 }
   2348                 bytes += draft.payload().len();
   2349                 records.push(AuthoredDraftQueryRecord::Draft((*draft).clone()));
   2350             }
   2351             AuthoredDraftPage::new(&query, records, has_more)
   2352         })
   2353     }
   2354 
   2355     fn append_authored_draft(
   2356         &self,
   2357         draft: AuthoredDraft,
   2358         expected_head: Option<AuthoredDraftRevision>,
   2359     ) -> BoxFuture<'_, Result<DraftAppendReceipt, Error>> {
   2360         Box::pin(async move {
   2361             draft.validate()?;
   2362             let mut state = self.state()?;
   2363             let disposition =
   2364                 draft_append_disposition(&state.authored_drafts, &draft, expected_head)?;
   2365             if disposition == DraftAppendDisposition::Replay {
   2366                 return Ok(DraftAppendReceipt::new(draft, disposition));
   2367             }
   2368             state.authored_drafts.push(draft.clone());
   2369             Ok(DraftAppendReceipt::new(
   2370                 draft,
   2371                 DraftAppendDisposition::Inserted,
   2372             ))
   2373         })
   2374     }
   2375 
   2376     fn authored_draft_head(
   2377         &self,
   2378         draft_id: AuthoredDraftId,
   2379     ) -> BoxFuture<'_, Result<Option<AuthoredDraft>, Error>> {
   2380         Box::pin(async move {
   2381             Ok(self
   2382                 .state()?
   2383                 .authored_drafts
   2384                 .iter()
   2385                 .filter(|draft| draft.draft_id() == draft_id)
   2386                 .max_by_key(|draft| draft.revision())
   2387                 .cloned())
   2388         })
   2389     }
   2390 
   2391     fn authored_draft_revision(
   2392         &self,
   2393         draft_id: AuthoredDraftId,
   2394         revision: AuthoredDraftRevision,
   2395     ) -> BoxFuture<'_, Result<Option<AuthoredDraft>, Error>> {
   2396         Box::pin(async move {
   2397             Ok(self
   2398                 .state()?
   2399                 .authored_drafts
   2400                 .iter()
   2401                 .find(|draft| draft.draft_id() == draft_id && draft.revision() == revision)
   2402                 .cloned())
   2403         })
   2404     }
   2405 
   2406     fn authored_draft_heads(
   2407         &self,
   2408         author: [u8; 32],
   2409         limit: u16,
   2410     ) -> BoxFuture<'_, Result<Vec<AuthoredDraft>, Error>> {
   2411         Box::pin(async move {
   2412             if author.iter().all(|byte| *byte == 0)
   2413                 || limit == 0
   2414                 || limit > AUTHORED_DRAFT_QUERY_LIMIT_MAX
   2415             {
   2416                 return Err(Error::InvalidAuthoredDraft);
   2417             }
   2418             let state = self.state()?;
   2419             let mut heads = state
   2420                 .authored_drafts
   2421                 .iter()
   2422                 .filter(|draft| draft.author() == &author)
   2423                 .fold(Vec::<AuthoredDraft>::new(), |mut heads, draft| {
   2424                     match heads
   2425                         .iter_mut()
   2426                         .find(|head| head.draft_id() == draft.draft_id())
   2427                     {
   2428                         Some(head) if draft.revision() > head.revision() => *head = draft.clone(),
   2429                         None => heads.push(draft.clone()),
   2430                         Some(_) => {}
   2431                     }
   2432                     heads
   2433                 });
   2434             heads.sort_by(|left, right| {
   2435                 right
   2436                     .updated_at_unix_ms()
   2437                     .cmp(&left.updated_at_unix_ms())
   2438                     .then_with(|| left.draft_id().cmp(&right.draft_id()))
   2439             });
   2440             heads.truncate(usize::from(limit));
   2441             Ok(heads)
   2442         })
   2443     }
   2444 }
   2445 
   2446 fn require_artifact_claim(
   2447     claim: Option<&crate::authored::WorkClaim>,
   2448     fence: &crate::authored_atomic::WorkFence,
   2449     now_unix_ms: u64,
   2450 ) -> Result<(), Error> {
   2451     if !claim.is_some_and(|claim| {
   2452         claim.matches_fence(
   2453             fence.token(),
   2454             fence.generation(),
   2455             fence.row_revision(),
   2456             now_unix_ms,
   2457         )
   2458     }) {
   2459         return Err(Error::DeliveryPlanClaimConflict);
   2460     }
   2461     Ok(())
   2462 }
   2463 
   2464 fn draft_append_disposition(
   2465     drafts: &[AuthoredDraft],
   2466     draft: &AuthoredDraft,
   2467     expected_head: Option<AuthoredDraftRevision>,
   2468 ) -> Result<DraftAppendDisposition, Error> {
   2469     if let Some(existing) = drafts.iter().find(|existing| {
   2470         existing.draft_id() == draft.draft_id() && existing.revision() == draft.revision()
   2471     }) {
   2472         return if existing == draft {
   2473             Ok(DraftAppendDisposition::Replay)
   2474         } else {
   2475             Err(Error::DraftRevisionConflict)
   2476         };
   2477     }
   2478     let head = drafts
   2479         .iter()
   2480         .filter(|existing| existing.draft_id() == draft.draft_id())
   2481         .max_by_key(|existing| existing.revision());
   2482     match (head, expected_head) {
   2483         (None, None) if draft.revision() == AuthoredDraftRevision::INITIAL => {}
   2484         (Some(previous), Some(expected)) if previous.revision() == expected => {
   2485             draft.validate_successor_of(previous)?;
   2486         }
   2487         _ => return Err(Error::DraftRevisionConflict),
   2488     }
   2489     Ok(DraftAppendDisposition::Inserted)
   2490 }