commit e6561a0cc7a8f6fde416d37dd15c9612ce312770 parent babe4622ec60f85167845bba8d58d3da58b03227 Author: triesap <tyson@radroots.org> Date: Sat, 10 Oct 2026 03:37:31 +0000 storage: add atomic canonical event mutation fences - Expose typed generation and epoch tokens with unsupported-provider refusal. - Advance memory and SQLite epochs only for committed semantic mutations. - Add forward schema bootstrap and overflow, replay, rollback and reopen controls. - Refresh API baselines after complete workspace and coverage qualification. Diffstat:
17 files changed, 719 insertions(+), 11 deletions(-)
diff --git a/contracts/api_baselines/radroots_storage.txt b/contracts/api_baselines/radroots_storage.txt @@ -813,6 +813,11 @@ pub struct radroots_storage::event::EventSequence(_) impl radroots_storage::event::EventSequence pub const fn radroots_storage::event::EventSequence::get(self) -> u64 pub const fn radroots_storage::event::EventSequence::new(u64) -> core::result::Result<Self, radroots_storage::Error> +pub struct radroots_storage::event::EventSourceToken +impl radroots_storage::event::EventSourceToken +pub const fn radroots_storage::event::EventSourceToken::epoch(self) -> u64 +pub const fn radroots_storage::event::EventSourceToken::generation(self) -> radroots_storage::event::SourceGeneration +pub const fn radroots_storage::event::EventSourceToken::new(radroots_storage::event::SourceGeneration, u64) -> Self pub struct radroots_storage::event::SourceGeneration(_) impl radroots_storage::event::SourceGeneration pub const fn radroots_storage::event::SourceGeneration::as_bytes(&self) -> &[u8; 32] @@ -860,6 +865,7 @@ pub fn radroots_storage::event::EventStore::query_raw(&self, radroots_storage::e pub fn radroots_storage::event::EventStore::query_verified(&self, radroots_storage::event::EventQuery) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::EventPage<radroots_storage::event::StoredVerifiedEvent>, radroots_storage::Error>> pub fn radroots_storage::event::EventStore::query_visible(&self, radroots_storage::event::EventQuery) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::EventPage<radroots_storage::event::StoredVisibleEvent>, radroots_storage::Error>> pub fn radroots_storage::event::EventStore::rebuild_visibility(&self) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::VisibilitySnapshot, radroots_storage::Error>> +pub fn radroots_storage::event::EventStore::source_token(&self) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::EventSourceToken, radroots_storage::Error>> pub fn radroots_storage::event::EventStore::status(&self) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::status::EventStoreStatus, radroots_storage::Error>> impl radroots_storage::event::EventStore for radroots_storage::memory::MemoryStorage pub fn radroots_storage::memory::MemoryStorage::admit(&self, radroots_storage::event::EventAdmission) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::AdmissionReceipt, radroots_storage::Error>> @@ -868,6 +874,7 @@ pub fn radroots_storage::memory::MemoryStorage::query_raw(&self, radroots_storag pub fn radroots_storage::memory::MemoryStorage::query_verified(&self, radroots_storage::event::EventQuery) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::EventPage<radroots_storage::event::StoredVerifiedEvent>, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::query_visible(&self, radroots_storage::event::EventQuery) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::EventPage<radroots_storage::event::StoredVisibleEvent>, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::rebuild_visibility(&self) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::VisibilitySnapshot, radroots_storage::Error>> +pub fn radroots_storage::memory::MemoryStorage::source_token(&self) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::EventSourceToken, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::status(&self) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::status::EventStoreStatus, radroots_storage::Error>> pub type radroots_storage::event::EventCursor = radroots_storage::event::EventPosition pub mod radroots_storage::journal @@ -1029,6 +1036,7 @@ pub fn radroots_storage::memory::MemoryStorage::query_raw(&self, radroots_storag pub fn radroots_storage::memory::MemoryStorage::query_verified(&self, radroots_storage::event::EventQuery) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::EventPage<radroots_storage::event::StoredVerifiedEvent>, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::query_visible(&self, radroots_storage::event::EventQuery) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::EventPage<radroots_storage::event::StoredVisibleEvent>, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::rebuild_visibility(&self) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::VisibilitySnapshot, radroots_storage::Error>> +pub fn radroots_storage::memory::MemoryStorage::source_token(&self) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::EventSourceToken, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::status(&self) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::status::EventStoreStatus, radroots_storage::Error>> impl radroots_storage::journal::Journal for radroots_storage::memory::MemoryStorage pub fn radroots_storage::memory::MemoryStorage::by_idempotency_key(&self, radroots_protocol::runtime::v1::OperationId, radroots_storage::journal::IdempotencyKey) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::journal::OperationRecord>, radroots_storage::Error>> @@ -1742,6 +1750,7 @@ pub radroots_storage::Error::DuplicateEventIndexShard pub radroots_storage::Error::DuplicateEventQueryId pub radroots_storage::Error::EmptyEventQueryIds pub radroots_storage::Error::EventConflict +pub radroots_storage::Error::EventMutationOverflow pub radroots_storage::Error::EventNotFound pub radroots_storage::Error::EventPageLimitExceeded pub radroots_storage::Error::IdempotencyConflict @@ -1891,6 +1900,7 @@ pub fn radroots_storage::EventStore::query_raw(&self, radroots_storage::event::E pub fn radroots_storage::EventStore::query_verified(&self, radroots_storage::event::EventQuery) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::EventPage<radroots_storage::event::StoredVerifiedEvent>, radroots_storage::Error>> pub fn radroots_storage::EventStore::query_visible(&self, radroots_storage::event::EventQuery) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::EventPage<radroots_storage::event::StoredVisibleEvent>, radroots_storage::Error>> pub fn radroots_storage::EventStore::rebuild_visibility(&self) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::VisibilitySnapshot, radroots_storage::Error>> +pub fn radroots_storage::EventStore::source_token(&self) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::EventSourceToken, radroots_storage::Error>> pub fn radroots_storage::EventStore::status(&self) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::status::EventStoreStatus, radroots_storage::Error>> impl radroots_storage::event::EventStore for radroots_storage::memory::MemoryStorage pub fn radroots_storage::memory::MemoryStorage::admit(&self, radroots_storage::event::EventAdmission) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::AdmissionReceipt, radroots_storage::Error>> @@ -1899,6 +1909,7 @@ pub fn radroots_storage::memory::MemoryStorage::query_raw(&self, radroots_storag pub fn radroots_storage::memory::MemoryStorage::query_verified(&self, radroots_storage::event::EventQuery) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::EventPage<radroots_storage::event::StoredVerifiedEvent>, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::query_visible(&self, radroots_storage::event::EventQuery) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::EventPage<radroots_storage::event::StoredVisibleEvent>, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::rebuild_visibility(&self) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::VisibilitySnapshot, radroots_storage::Error>> +pub fn radroots_storage::memory::MemoryStorage::source_token(&self) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::EventSourceToken, radroots_storage::Error>> pub fn radroots_storage::memory::MemoryStorage::status(&self) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::status::EventStoreStatus, radroots_storage::Error>> pub trait radroots_storage::Journal: core::marker::Send + core::marker::Sync pub fn radroots_storage::Journal::by_idempotency_key(&self, radroots_protocol::runtime::v1::OperationId, radroots_storage::journal::IdempotencyKey) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::journal::OperationRecord>, radroots_storage::Error>> diff --git a/contracts/api_baselines/radroots_storage_sqlite.txt b/contracts/api_baselines/radroots_storage_sqlite.txt @@ -586,6 +586,7 @@ pub fn radroots_storage_sqlite::SqliteStorage::query_raw(&self, radroots_storage pub fn radroots_storage_sqlite::SqliteStorage::query_verified(&self, radroots_storage::event::EventQuery) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::EventPage<radroots_storage::event::StoredVerifiedEvent>, radroots_storage::error::Error>> pub fn radroots_storage_sqlite::SqliteStorage::query_visible(&self, radroots_storage::event::EventQuery) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::EventPage<radroots_storage::event::StoredVisibleEvent>, radroots_storage::error::Error>> pub fn radroots_storage_sqlite::SqliteStorage::rebuild_visibility(&self) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::VisibilitySnapshot, radroots_storage::error::Error>> +pub fn radroots_storage_sqlite::SqliteStorage::source_token(&self) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::event::EventSourceToken, radroots_storage::error::Error>> pub fn radroots_storage_sqlite::SqliteStorage::status(&self) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::status::EventStoreStatus, radroots_storage::error::Error>> impl radroots_storage::journal::Journal for radroots_storage_sqlite::SqliteStorage pub fn radroots_storage_sqlite::SqliteStorage::by_idempotency_key(&self, radroots_protocol::runtime::v1::OperationId, radroots_storage::journal::IdempotencyKey) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::journal::OperationRecord>, radroots_storage::error::Error>> diff --git a/contracts/architecture/decisions/event_mutation_epoch.v1.json b/contracts/architecture/decisions/event_mutation_epoch.v1.json @@ -0,0 +1,19 @@ +{ + "schema": "radroots.event-mutation-epoch.v1", + "status": "approved", + "owners": ["radroots_storage", "radroots_storage_sqlite"], + "token": "event::EventSourceToken binds the canonical SourceGeneration and a u64 semantic admission epoch. EventStore::source_token reads current owning state without decoding retained history. Unsupported adapters return BackendUnavailable. Zero represents an empty source.", + "advance": "Insertion and actual admission-stage or contract-eligibility metadata changes advance the epoch atomically. The raw sequence alone is insufficient for same-count admission promotion. Exact duplicate admission, same-value SQL updates and provenance-only observations do not advance. Invalid/conflicting/regressing admissions have no effect.", + "atomicity": "Memory validates overflow before changing the event or provenance and publishes validated atomic candidates. SQLite insertion and eligibility triggers advance the owning generation in the same canonical transaction, covering direct admission and AtomicStorage ingestion. Event, source sequence, epoch, provenance and command receipt roll back together. Lost-receipt replay returns the durable receipt without another advancement.", + "writers": "Current production canonical writes are event admission and authored-v10 eligibility migration. Atomic ingest shares admit_transaction; Sync and local authored admission use that owner. Legacy import remains quarantine and does not itself admit canonical events. Forward v18 bootstrap fences earlier migration eligibility without rewriting any prior SQL or checksum. SQL insertion and actual eligibility updates cannot omit the epoch trigger.", + "bounds": {"memory_epoch_max": 18446744073709551615, "sqlite_epoch_max": 9223372036854775807, "token_read_rows_max": 1, "token_event_decode_count": 0}, + "overflow": "EventMutationOverflow is redacted and refuses semantic mutation rather than wrapping. Exact duplicate and provenance-only observations remain legal at the ceiling. SQLite uses the exact private trigger message plus SQLITE_CONSTRAINT_TRIGGER code; other errors retain existing classification.", + "migration": { + "version": 18, + "name": "event_mutation_epoch", + "sha256": "44e8f43722bb869f824dbfa28197d17c1e8f58c6d62b8219cdd19d452eb47c3c", + "bootstrap": "Existing nonempty generations start at epoch1; empty generations start at0. No signed-event decode is required. Subsequent eligible changes advance monotonically. Earlier runtime schema readers reject version18; an adapter without the column cannot supply a token. Preserve prior migrations, descriptors, snapshots, receipts and canonical identity/delete guards." + }, + "restore": "A verified whole-database restore preserves the backed-up generation and epoch together with its exact source bytes. Restore closes the previous provider handles and requires reopening. A token is not portable permission to continue a read across explicit history replacement: consumers must discard prior runtime scopes, derived presentation and cursors across restore/reopen. This does not claim a monotonic epoch across rollback to another complete database snapshot; that boundary includes runtime identity and is separately qualified by owning consumers.", + "non_goals": ["application policy", "indexed visibility implementation", "projection promotion", "new database", "public SQL", "new dependency", "artifact publication", "consumer adoption"] +} diff --git a/crates/storage/src/error.rs b/crates/storage/src/error.rs @@ -28,6 +28,8 @@ pub enum Error { AdmissionEventMismatch, AdmissionRegression, EventConflict, + /// The canonical semantic epoch cannot advance; the mutation is rolled back. + EventMutationOverflow, EventPageLimitExceeded, CursorGenerationMismatch, SourceGenerationChanged, @@ -154,6 +156,7 @@ impl fmt::Display for Error { Self::InvalidRetrySchedule => "storage authored retry schedule is invalid", Self::InvalidSourceGeneration => "storage source generation is invalid", Self::InvalidEventSequence => "storage event sequence is invalid", + Self::EventMutationOverflow => "storage event mutation epoch overflowed", Self::InvalidEventQueryLimit => "storage event query limit is invalid", Self::EmptyEventQueryIds => "storage event id query is empty", Self::TooManyEventQueryIds => "storage event id query exceeds its limit", diff --git a/crates/storage/src/event.rs b/crates/storage/src/event.rs @@ -51,6 +51,36 @@ const fn is_all_zero(bytes: &[u8; 32]) -> bool { true } +/// Canonical source identity and its semantic admission epoch. +/// +/// The epoch advances atomically on insertion or admission eligibility changes, +/// including changes that leave the event count unchanged. Duplicate admission +/// and provenance-only observations do not advance it. Zero denotes an empty +/// source. Tokens belong to their source generation; they are not timestamps. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +pub struct EventSourceToken { + generation: SourceGeneration, + epoch: u64, +} + +impl EventSourceToken { + /// Creates a source token from an owning provider's durable state. + pub const fn new(generation: SourceGeneration, epoch: u64) -> Self { + Self { generation, epoch } + } + + /// Returns the source generation. + pub const fn generation(self) -> SourceGeneration { + self.generation + } + + /// Returns the generation-local semantic mutation epoch. + pub const fn epoch(self) -> u64 { + self.epoch + } +} + /// Non-zero sequence within one source generation. #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] @@ -596,6 +626,14 @@ impl StoredEventProvenance { /// idempotent for an identical event and provenance observation; a stage may /// advance but never regress. Query cursors are bound to one source generation. pub trait EventStore: Send + Sync { + /// Reads the current source token without decoding retained event history. + /// + /// Providers that cannot supply this fence fail closed. SQLite epochs have + /// an `i64::MAX` storage ceiling; the memory provider uses `u64::MAX`. + fn source_token(&self) -> BoxFuture<'_, Result<EventSourceToken, Error>> { + Box::pin(async { Err(Error::BackendUnavailable) }) + } + /// Returns passive event-store status without initiating maintenance. fn status(&self) -> BoxFuture<'_, Result<EventStoreStatus, Error>>; diff --git a/crates/storage/src/memory.rs b/crates/storage/src/memory.rs @@ -3,6 +3,10 @@ #[path = "memory_delivery_history.rs"] mod delivery_history; +#[cfg(test)] +#[path = "memory_mutation_tests.rs"] +mod mutation_tests; + use radroots_event::EventId; use radroots_protocol::runtime::v1::OperationId; use radroots_transport::{BoxFuture, source::EventProvenance}; @@ -30,9 +34,10 @@ use crate::{ }, event::{ AdmissionDisposition, AdmissionReceipt, AdmissionStage, EventAdmission, EventPage, - EventPosition, EventQuery, EventQueryBounds, EventSequence, SourceGeneration, - StoredEventProvenance, StoredRawEvent, StoredVerifiedEvent, StoredVisibleEvent, - VisibilityEvaluation, VisibilityInput, VisibilitySnapshot, evaluate_visibility, + EventPosition, EventQuery, EventQueryBounds, EventSequence, EventSourceToken, + SourceGeneration, StoredEventProvenance, StoredRawEvent, StoredVerifiedEvent, + StoredVisibleEvent, VisibilityEvaluation, VisibilityInput, VisibilitySnapshot, + evaluate_visibility, }, journal::{ IdempotencyKey, JournalStage, JournalTransition, OperationInstanceId, OperationRecord, @@ -71,6 +76,7 @@ struct EventEntry { #[derive(Clone, Default)] struct State { events: Vec<EventEntry>, + event_mutation_epoch: u64, journal: Vec<OperationRecord>, outbox: Vec<OutboxRecord>, projections: Vec<ProjectionStatus>, @@ -109,6 +115,7 @@ impl MemoryStorage { generation, state: Mutex::new(State { events: Vec::new(), + event_mutation_epoch: 0, journal: Vec::new(), outbox: Vec::new(), projections: Vec::new(), @@ -219,6 +226,10 @@ impl MemoryStorage { let disposition = if admission.stage() == entry.admission.stage() { AdmissionDisposition::Duplicate } else { + state.event_mutation_epoch = state + .event_mutation_epoch + .checked_add(1) + .ok_or(Error::EventMutationOverflow)?; AdmissionDisposition::Advanced }; if !entry.provenance.contains(admission.provenance()) { @@ -237,6 +248,10 @@ impl MemoryStorage { .checked_add(1) .ok_or(Error::CorruptStoredEvent)?; let position = EventPosition::new(self.generation, EventSequence::new(next)?); + let epoch = state + .event_mutation_epoch + .checked_add(1) + .ok_or(Error::EventMutationOverflow)?; let receipt = AdmissionReceipt::new( *admission.event_id(), position, @@ -249,6 +264,7 @@ impl MemoryStorage { admission, provenance, }); + state.event_mutation_epoch = epoch; Ok(receipt) } @@ -408,6 +424,16 @@ impl Default for MemoryStorage { } impl EventStore for MemoryStorage { + fn source_token(&self) -> BoxFuture<'_, Result<EventSourceToken, Error>> { + Box::pin(async move { + let state = self.state()?; + Ok(EventSourceToken::new( + self.generation, + state.event_mutation_epoch, + )) + }) + } + fn status(&self) -> BoxFuture<'_, Result<EventStoreStatus, Error>> { Box::pin(async move { let state = self.state()?; diff --git a/crates/storage/src/memory_mutation_tests.rs b/crates/storage/src/memory_mutation_tests.rs @@ -0,0 +1,65 @@ +use super::*; +use futures_executor::block_on; +use radroots_event::{ + SignedEvent, + admission::{RawEvent, SignatureVerifier}, + wire::Nip01EventWire, +}; +use radroots_transport::{Target, TransportId, source::ObservedEvent}; + +fn admission() -> EventAdmission { + let raw = r#"{"id":"56bfc78223bb2221bad82b539efdec1ade0f56d0eb0e1f592fd387df4b2ceee0","pubkey":"585591529da0bab31b3b1b1f986611cf5f435dca84f978c89ee8a40cca7103df","created_at":1700000001,"kind":0,"tags":[],"content":"{}","sig":"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"}"#; + let event = + SignedEvent::from_wire_verified_id(Nip01EventWire::parse_json(raw).unwrap(), raw).unwrap(); + let target = Target::nostr_relay("wss://relay.example").unwrap(); + let provenance = + EventProvenance::new(TransportId::NOSTR, target.fingerprint().clone(), 100).unwrap(); + EventAdmission::raw(ObservedEvent::new(event, provenance)) +} + +struct Allow; +impl SignatureVerifier for Allow { + fn verify_signature(&self, _: &radroots_event::Event) -> Result<(), radroots_event::Error> { + Ok(()) + } +} + +#[test] +fn u64_epoch_maximum_rejects_semantic_mutation_without_partial_effects() { + let generation = SourceGeneration::new([78; 32]).unwrap(); + let storage = MemoryStorage::new(generation); + storage.state().unwrap().event_mutation_epoch = u64::MAX - 1; + let original = admission(); + block_on(storage.admit(original.clone())).unwrap(); + let maximum = EventSourceToken::new(generation, u64::MAX); + assert_eq!(block_on(storage.source_token()).unwrap(), maximum); + block_on(storage.admit(original.clone())).unwrap(); + let verified = RawEvent::new(original.event().envelope().clone()) + .verify_id() + .unwrap() + .verify_signature(&Allow) + .unwrap(); + let advanced = EventAdmission::verified( + ObservedEvent::new(original.event().clone(), original.provenance().clone()), + verified, + ) + .unwrap(); + assert_eq!( + block_on(storage.admit(advanced)), + Err(Error::EventMutationOverflow) + ); + assert_eq!(block_on(storage.source_token()).unwrap(), maximum); + let state = storage.state().unwrap(); + assert_eq!(state.events.len(), 1); + assert_eq!(state.events[0].admission.stage(), AdmissionStage::Raw); + assert_eq!(state.events[0].provenance.len(), 1); + let mut candidate = State { + event_mutation_epoch: u64::MAX, + ..State::default() + }; + assert_eq!( + storage.admit_locked(&mut candidate, original), + Err(Error::EventMutationOverflow) + ); + assert!(candidate.events.is_empty()); +} diff --git a/crates/storage/tests/event_store.rs b/crates/storage/tests/event_store.rs @@ -64,6 +64,14 @@ impl MemoryEventStore { } } +#[test] +fn unsupported_adapter_cannot_invent_a_source_fence() { + assert_eq!( + block_on(MemoryEventStore::new().source_token()), + Err(Error::BackendUnavailable) + ); +} + impl EventStore for MemoryEventStore { fn status(&self) -> BoxFuture<'_, Result<EventStoreStatus, Error>> { Box::pin(async move { diff --git a/crates/storage/tests/memory.rs b/crates/storage/tests/memory.rs @@ -158,6 +158,8 @@ fn verified_replacement_changes_visibility_without_increasing_raw_count() { block_on(store.admit(visible_admission(old.clone(), 10))).unwrap(); let raw = admission(newer.clone(), 20); block_on(store.admit(raw.clone())).unwrap(); + let token_before = block_on(store.source_token()).unwrap(); + assert_eq!(token_before.epoch(), 2); let before = block_on(store.rebuild_visibility()).unwrap(); assert_eq!(before.visible_event_ids(), &[*old.id()]); let verified = RawEvent::new(newer.envelope().clone()) @@ -179,6 +181,10 @@ fn verified_replacement_changes_visibility_without_increasing_raw_count() { advanced.disposition(), radroots_storage::event::AdmissionDisposition::Advanced ); + assert_eq!( + block_on(store.source_token()).unwrap().epoch(), + token_before.epoch() + 1 + ); let after = block_on(store.rebuild_visibility()).unwrap(); assert_ne!(before.digest(), after.digest()); assert_eq!(after.current_heads()[0].event_id, *newer.id()); diff --git a/crates/storage_sqlite/src/backend.rs b/crates/storage_sqlite/src/backend.rs @@ -5,6 +5,11 @@ use radroots_storage::Error; pub(crate) fn map_backend(source: sqlx::Error) -> Error { if is_capacity(&source) { Error::SpaceInsufficient + } else if source.as_database_error().is_some_and(|error| { + error.code().as_deref() == Some("1811") + && error.message() == "runtime event mutation epoch overflow" + }) { + Error::EventMutationOverflow } else { Error::BackendUnavailable } @@ -88,6 +93,7 @@ mod tests { Some("11"), Some("5"), Some("10"), + Some("1811"), ] { assert_eq!( map_backend(sqlx::Error::Database(Box::new(Coded(code)))), diff --git a/crates/storage_sqlite/src/backup/restore_capability_tests.rs b/crates/storage_sqlite/src/backup/restore_capability_tests.rs @@ -1,6 +1,7 @@ use super::*; use crate::OpenOptions; use radroots_storage::{ + EventStore, authored_draft::{AuthoredDraft, AuthoredDraftId, AuthoredDraftStage, AuthoredDraftStore}, event::SourceGeneration, status::ShutdownState, @@ -54,6 +55,7 @@ async fn restore_spi_stages_without_live_changes_then_closes_and_reopens_exact_h let root = tempfile::tempdir().unwrap(); let backup = tempfile::tempdir().unwrap(); let (store, restore) = fixture(root.path(), backup.path()).await; + let source_token = store.source_token().await.unwrap(); let owner: &dyn StorageReliability = &store; let paths = crate::Paths::from_directory(root.path()).unwrap(); let staging = RestoreStaging::new(&paths, restore.manifest()).unwrap(); @@ -84,6 +86,10 @@ async fn restore_spi_stages_without_live_changes_then_closes_and_reopens_exact_h ); owner.finalize_restore(restore.clone()).await.unwrap(); assert_eq!( + store.source_token().await, + Err(radroots_storage::Error::BackendUnavailable) + ); + assert_eq!( store.storage_status().await.unwrap().shutdown(), ShutdownState::Closed ); @@ -98,6 +104,7 @@ async fn restore_spi_stages_without_live_changes_then_closes_and_reopens_exact_h let reopened = SqliteStorage::open(OpenOptions::new(paths, OpenMode::ReadWriteExisting)) .await .unwrap(); + assert_eq!(reopened.source_token().await.unwrap(), source_token); assert_eq!( reopened .authored_draft_head(draft(1).draft_id()) diff --git a/crates/storage_sqlite/src/event/mod.rs b/crates/storage_sqlite/src/event/mod.rs @@ -6,7 +6,7 @@ use radroots_storage::{ event::{ AdmissionDisposition, AdmissionReceipt, AdmissionStage, BoxFuture, EventAdmission, EventCursor, EventId, EventPage, EventPosition, EventQuery, EventQueryBounds, - EventSequence, SourceGeneration, StoredEventProvenance, StoredRawEvent, + EventSequence, EventSourceToken, SourceGeneration, StoredEventProvenance, StoredRawEvent, StoredVerifiedEvent, StoredVisibleEvent, VisibilityEvaluation, VisibilityInput, VisibilitySnapshot, evaluate_visibility, }, @@ -397,6 +397,24 @@ fn admission_contract_metadata(admission: &EventAdmission) -> Option<(&'static s #[cfg_attr(coverage_nightly, coverage(off))] impl EventStore for SqliteStorage { + fn source_token(&self) -> BoxFuture<'_, Result<EventSourceToken, Error>> { + Box::pin(async move { + let epoch = sqlx::query_scalar::<_, i64>( + "SELECT mutation_epoch FROM radroots_runtime_source_generations + WHERE generation = ? AND state = 'active'", + ) + .bind(self.generation.as_bytes().as_slice()) + .fetch_optional(&self.pool) + .await + .map_err(map_backend)? + .ok_or(Error::SourceGenerationChanged)?; + Ok(EventSourceToken::new( + self.generation, + u64::try_from(epoch).map_err(|_| Error::CorruptStoredEvent)?, + )) + }) + } + fn status(&self) -> BoxFuture<'_, Result<EventStoreStatus, Error>> { Box::pin(async move { let sql = "SELECT @@ -628,6 +646,8 @@ fn map_corrupt(_: sqlx::Error) -> Error { } #[cfg(test)] +mod mutation_tests; +#[cfg(test)] mod work; #[cfg(test)] mod work_tests; @@ -998,6 +1018,14 @@ mod tests { ); let reopened = SqliteStorage::new(store.pool.clone(), generation, EventStoreMode::ReadWrite); + assert_eq!( + reopened.source_token().await.unwrap(), + memory.source_token().await.unwrap() + ); + assert_eq!( + reopened.source_token().await.unwrap().epoch(), + index as u64 + 1 + ); let snapshot = reopened.rebuild_visibility().await.unwrap(); assert_eq!(snapshot, memory.rebuild_visibility().await.unwrap()); let query = EventQuery::all(EventQueryBounds::first(10).unwrap()); diff --git a/crates/storage_sqlite/src/event/mutation_tests.rs b/crates/storage_sqlite/src/event/mutation_tests.rs @@ -0,0 +1,327 @@ +use super::tests::{observed, signed_event_with, store, verified, visible}; +use super::*; +use radroots_storage::atomic::{ + AtomicCommit, AtomicCommitDigest, AtomicCommitDisposition, AtomicCommitId, AtomicStorage, + AtomicWorkflow, CommitIngested, +}; + +fn event(content: &str) -> SignedEvent { + signed_event_with(content, false, 1_800_000_100, 0, vec![]) +} + +fn generation() -> SourceGeneration { + SourceGeneration::new([77; 32]).unwrap() +} + +fn atomic_request(event: SignedEvent, id: u8) -> AtomicCommit { + AtomicCommit::new( + AtomicCommitId::new([id; 16]).unwrap(), + AtomicCommitDigest::new([id; 32]), + 100, + AtomicWorkflow::Ingested(Box::new(CommitIngested::new( + EventAdmission::raw(observed(event, 100, None)), + None, + ))), + ) + .unwrap() +} + +#[tokio::test] +async fn epoch_tracks_semantic_changes_without_duplicate_or_provenance_churn() { + let storage = store(generation()).await; + assert_eq!( + storage.source_token().await.unwrap(), + EventSourceToken::new(generation(), 0) + ); + let event = event(r#"{"name":"Farm"}"#); + let receipt = storage + .admit(EventAdmission::raw(observed(event.clone(), 10, None))) + .await + .unwrap(); + assert_eq!(storage.source_token().await.unwrap().epoch(), 1); + for at in [10, 20] { + assert_eq!( + storage + .admit(EventAdmission::raw(observed(event.clone(), at, None))) + .await + .unwrap() + .disposition(), + AdmissionDisposition::Duplicate + ); + assert_eq!(storage.source_token().await.unwrap().epoch(), 1); + } + let advanced = storage + .admit( + EventAdmission::verified(observed(event.clone(), 30, None), verified(&event)).unwrap(), + ) + .await + .unwrap(); + assert_eq!(advanced.position(), receipt.position()); + assert_eq!(storage.source_token().await.unwrap().epoch(), 2); + storage + .admit(EventAdmission::visible(observed(event.clone(), 40, None), visible(&event)).unwrap()) + .await + .unwrap(); + assert_eq!(storage.source_token().await.unwrap().epoch(), 3); + storage + .admit(EventAdmission::visible(observed(event.clone(), 50, None), visible(&event)).unwrap()) + .await + .unwrap(); + assert_eq!(storage.source_token().await.unwrap().epoch(), 3); + assert_eq!(storage.status().await.unwrap().raw_events(), 1); + assert_eq!( + storage + .admit(EventAdmission::raw(observed(event, 60, None))) + .await, + Err(Error::AdmissionRegression) + ); + assert_eq!(storage.source_token().await.unwrap().epoch(), 3); +} + +#[tokio::test] +async fn sqlite_epoch_maximum_allows_replay_but_rolls_back_insert_and_promotion() { + let storage = store(generation()).await; + sqlx::query("UPDATE radroots_runtime_source_generations SET mutation_epoch = ?") + .bind(i64::MAX - 1) + .execute(&storage.pool) + .await + .unwrap(); + let original = event(r#"{"name":"Original"}"#); + storage + .admit(EventAdmission::raw(observed(original.clone(), 10, None))) + .await + .unwrap(); + let maximum = EventSourceToken::new(generation(), i64::MAX as u64); + assert_eq!(storage.source_token().await.unwrap(), maximum); + storage + .admit(EventAdmission::raw(observed(original.clone(), 20, None))) + .await + .unwrap(); + let before_provenance = storage + .query_provenance(*original.id(), EventQueryBounds::first(10).unwrap()) + .await + .unwrap(); + assert_eq!( + storage + .admit( + EventAdmission::visible(observed(original.clone(), 30, None), visible(&original)) + .unwrap() + ) + .await, + Err(Error::EventMutationOverflow) + ); + assert_eq!( + storage + .admit(EventAdmission::raw(observed( + event(r#"{"name":"New"}"#), + 40, + None + ))) + .await, + Err(Error::EventMutationOverflow) + ); + assert_eq!(storage.source_token().await.unwrap(), maximum); + let sequence: i64 = + sqlx::query_scalar("SELECT sequence_head FROM radroots_runtime_source_generations") + .fetch_one(&storage.pool) + .await + .unwrap(); + assert_eq!(sequence, 1); + assert_eq!( + storage + .query_provenance(*original.id(), EventQueryBounds::first(10).unwrap()) + .await + .unwrap(), + before_provenance + ); + let status = storage.status().await.unwrap(); + assert_eq!(status.raw_events(), 1); + assert_eq!(status.verified_events(), 0); + let error = Error::EventMutationOverflow; + assert_eq!(error.to_string(), "storage event mutation epoch overflowed"); +} + +#[tokio::test] +async fn actual_sql_writer_and_same_value_updates_obey_the_epoch_trigger() { + let storage = store(generation()).await; + let event = event(r#"{"name":"Farm"}"#); + storage + .admit(EventAdmission::raw(observed(event, 10, None))) + .await + .unwrap(); + sqlx::query("UPDATE radroots_runtime_events SET admission_stage = 'verified'") + .execute(&storage.pool) + .await + .unwrap(); + assert_eq!(storage.source_token().await.unwrap().epoch(), 2); + sqlx::query("UPDATE radroots_runtime_events SET admission_stage = 'visible', admitted_contract_id = 'radroots.profile.metadata.v1', admitted_registry_version = 7").execute(&storage.pool).await.unwrap(); + assert_eq!(storage.source_token().await.unwrap().epoch(), 3); + sqlx::query("UPDATE radroots_runtime_events SET admission_stage = admission_stage, admitted_contract_id = admitted_contract_id, admitted_registry_version = admitted_registry_version, updated_at_unix_ms = 20").execute(&storage.pool).await.unwrap(); + assert_eq!(storage.source_token().await.unwrap().epoch(), 3); + assert!( + sqlx::query("UPDATE radroots_runtime_source_generations SET mutation_epoch = 1") + .execute(&storage.pool) + .await + .is_err() + ); + assert!( + sqlx::query("DELETE FROM radroots_runtime_events") + .execute(&storage.pool) + .await + .is_err() + ); + assert_eq!(storage.source_token().await.unwrap().epoch(), 3); +} + +#[tokio::test] +async fn dropped_transaction_rolls_back_event_sequence_and_epoch_together() { + let storage = store(generation()).await; + let mut transaction = storage.pool.begin_with("BEGIN IMMEDIATE").await.unwrap(); + storage + .admit_transaction( + &mut transaction, + EventAdmission::raw(observed(event("{}"), 10, None)), + ) + .await + .unwrap(); + let epoch: i64 = + sqlx::query_scalar("SELECT mutation_epoch FROM radroots_runtime_source_generations") + .fetch_one(&mut *transaction) + .await + .unwrap(); + assert_eq!(epoch, 1); + drop(transaction); + assert_eq!(storage.source_token().await.unwrap().epoch(), 0); + assert_eq!(storage.status().await.unwrap().raw_events(), 0); + let sequence: i64 = + sqlx::query_scalar("SELECT sequence_head FROM radroots_runtime_source_generations") + .fetch_one(&storage.pool) + .await + .unwrap(); + assert_eq!(sequence, 0); +} + +#[tokio::test] +async fn lost_atomic_receipt_replay_does_not_advance_the_committed_epoch() { + let storage = store(generation()).await; + let request = atomic_request(event("{}"), 1); + let commit_id = request.commit_id(); + drop(storage.commit(request.clone()).await.unwrap()); + let token = storage.source_token().await.unwrap(); + assert_eq!(token.epoch(), 1); + assert!(storage.receipt(commit_id).await.unwrap().is_some()); + assert_eq!( + storage.commit(request).await.unwrap().disposition(), + AtomicCommitDisposition::Replay + ); + assert_eq!(storage.source_token().await.unwrap(), token); +} + +#[tokio::test] +async fn failure_after_event_write_rolls_back_epoch_and_atomic_receipt() { + let storage = store(generation()).await; + sqlx::raw_sql("CREATE TEMP TRIGGER test_receipt_fault BEFORE INSERT ON radroots_runtime_atomic_commits BEGIN SELECT RAISE(ABORT, 'injected receipt failure'); END;") + .execute(&storage.pool).await.unwrap(); + let request = atomic_request(event("{}"), 2); + let id = request.commit_id(); + assert!(storage.commit(request.clone()).await.is_err()); + assert_eq!(storage.source_token().await.unwrap().epoch(), 0); + assert_eq!(storage.status().await.unwrap().raw_events(), 0); + assert_eq!(storage.receipt(id).await.unwrap(), None); + sqlx::query("DROP TRIGGER test_receipt_fault") + .execute(&storage.pool) + .await + .unwrap(); + storage.commit(request).await.unwrap(); + assert_eq!(storage.source_token().await.unwrap().epoch(), 1); +} + +#[tokio::test] +async fn token_read_never_decodes_event_history_and_refuses_retired_sources() { + let storage = store(generation()).await; + storage + .admit(EventAdmission::raw(observed(event("{}"), 10, None))) + .await + .unwrap(); + storage.work.take(); + sqlx::query("DROP TRIGGER radroots_runtime_events_raw_update_guard") + .execute(&storage.pool) + .await + .unwrap(); + sqlx::query("UPDATE radroots_runtime_events SET signed_event = X'7b'") + .execute(&storage.pool) + .await + .unwrap(); + assert_eq!(storage.source_token().await.unwrap().epoch(), 1); + let counts = storage.work.take(); + assert_eq!(counts.event_rows_returned, 0); + assert_eq!(counts.codec_inputs, 0); + assert!( + storage + .query_raw(EventQuery::all(EventQueryBounds::first(1).unwrap())) + .await + .is_err() + ); + sqlx::query( + "UPDATE radroots_runtime_source_generations SET state = 'retired', retired_at_unix_ms = 20", + ) + .execute(&storage.pool) + .await + .unwrap(); + assert_eq!( + storage.source_token().await, + Err(Error::SourceGenerationChanged) + ); +} + +#[tokio::test] +async fn forward_migration_bootstraps_retained_and_empty_sources_without_decoding_history() { + use crate::migration::runtime::{MIGRATIONS, migration_sql}; + use sqlx::sqlite::SqlitePoolOptions; + for retained in [false, true] { + let pool = SqlitePoolOptions::new() + .max_connections(1) + .connect("sqlite::memory:") + .await + .unwrap(); + for migration in &MIGRATIONS[..17] { + sqlx::raw_sql(migration_sql(migration.version()).unwrap()) + .execute(&pool) + .await + .unwrap(); + } + sqlx::query("INSERT INTO radroots_runtime_source_generations (generation, sequence_head, state, created_at_unix_ms) VALUES (?, 0, 'active', 1)").bind(generation().as_bytes().as_slice()).execute(&pool).await.unwrap(); + if retained { + let event = event("{}"); + sqlx::query("UPDATE radroots_runtime_source_generations SET sequence_head = 1") + .execute(&pool) + .await + .unwrap(); + sqlx::query("INSERT INTO radroots_runtime_events (source_generation, source_sequence, event_id, admission_stage, signed_event, admitted_at_unix_ms, updated_at_unix_ms) VALUES (?, 1, ?, 'raw', X'7b', 10, 10)") + .bind(generation().as_bytes().as_slice()).bind(event.id().as_bytes().as_slice()).execute(&pool).await.unwrap(); + } + let adapter = SqliteStorage::new(pool.clone(), generation(), EventStoreMode::ReadWrite); + assert_eq!(adapter.source_token().await, Err(Error::BackendUnavailable)); + sqlx::raw_sql(migration_sql(18).unwrap()) + .execute(&pool) + .await + .unwrap(); + assert_eq!( + adapter.source_token().await.unwrap().epoch(), + u64::from(retained) + ); + let reopened = SqliteStorage::new(pool, generation(), EventStoreMode::ReadOnly); + assert_eq!( + reopened.source_token().await.unwrap(), + adapter.source_token().await.unwrap() + ); + if retained { + assert!( + reopened + .query_raw(EventQuery::all(EventQueryBounds::first(1).unwrap())) + .await + .is_err() + ); + } + } +} diff --git a/crates/storage_sqlite/src/migration.rs b/crates/storage_sqlite/src/migration.rs @@ -469,7 +469,7 @@ fn schema_metadata_error(source: &sqlx::Error, database: &'static str) -> Error fn validate_plan(plan: &MigrationPlan) -> Result<(), Error> { let valid = plan.minimum_version > 0 && plan.minimum_version <= plan.current_version - && plan.current_version <= 17 + && plan.current_version <= 18 && plan.steps.len() == usize::try_from(plan.current_version).unwrap_or(usize::MAX) && plan .steps @@ -593,6 +593,7 @@ const fn set_user_version_sql(version: u32) -> Option<&'static str> { 15 => Some("PRAGMA user_version = 15"), 16 => Some("PRAGMA user_version = 16"), 17 => Some("PRAGMA user_version = 17"), + 18 => Some("PRAGMA user_version = 18"), _ => None, } } @@ -876,9 +877,9 @@ mod tests { .execute(&mut newer) .await .expect("application id"); - let newer_version = 18; - assert_eq!(newer_version, runtime::CURRENT_VERSION + 1); - sqlx::raw_sql("PRAGMA user_version = 18") + let newer_version = runtime::CURRENT_VERSION + 1; + assert_eq!(newer_version, 19); + sqlx::raw_sql("PRAGMA user_version = 19") .execute(&mut newer) .await .expect("newer version"); diff --git a/crates/storage_sqlite/src/migration/runtime/0018_event_mutation_epoch.up.sql b/crates/storage_sqlite/src/migration/runtime/0018_event_mutation_epoch.up.sql @@ -0,0 +1,46 @@ +ALTER TABLE radroots_runtime_source_generations +ADD COLUMN mutation_epoch INTEGER NOT NULL DEFAULT 0 CHECK (mutation_epoch >= 0); + +UPDATE radroots_runtime_source_generations +SET mutation_epoch = 1 +WHERE EXISTS ( + SELECT 1 FROM radroots_runtime_events + WHERE source_generation = generation +); + +CREATE TRIGGER radroots_runtime_source_generations_mutation_guard +BEFORE UPDATE OF mutation_epoch ON radroots_runtime_source_generations +WHEN NEW.mutation_epoch < OLD.mutation_epoch +BEGIN + SELECT RAISE(ABORT, 'runtime source mutation epoch cannot regress'); +END; + +CREATE TRIGGER radroots_runtime_events_mutation_insert +AFTER INSERT ON radroots_runtime_events +BEGIN + SELECT CASE WHEN ( + SELECT mutation_epoch FROM radroots_runtime_source_generations + WHERE generation = NEW.source_generation + ) = 9223372036854775807 + THEN RAISE(ABORT, 'runtime event mutation epoch overflow') END; + UPDATE radroots_runtime_source_generations + SET mutation_epoch = mutation_epoch + 1 + WHERE generation = NEW.source_generation; +END; + +CREATE TRIGGER radroots_runtime_events_mutation_update +AFTER UPDATE OF admission_stage, admitted_contract_id, admitted_registry_version +ON radroots_runtime_events +WHEN NEW.admission_stage IS NOT OLD.admission_stage + OR NEW.admitted_contract_id IS NOT OLD.admitted_contract_id + OR NEW.admitted_registry_version IS NOT OLD.admitted_registry_version +BEGIN + SELECT CASE WHEN ( + SELECT mutation_epoch FROM radroots_runtime_source_generations + WHERE generation = NEW.source_generation + ) = 9223372036854775807 + THEN RAISE(ABORT, 'runtime event mutation epoch overflow') END; + UPDATE radroots_runtime_source_generations + SET mutation_epoch = mutation_epoch + 1 + WHERE generation = NEW.source_generation; +END; diff --git a/crates/storage_sqlite/src/migration/runtime/mod.rs b/crates/storage_sqlite/src/migration/runtime/mod.rs @@ -6,7 +6,7 @@ /// Lowest runtime schema version this package can recognize. pub const MINIMUM_VERSION: u32 = 1; /// Current runtime schema version created by this package. -pub const CURRENT_VERSION: u32 = 17; +pub const CURRENT_VERSION: u32 = 18; const RUNTIME_V1_SQL: &str = include_str!("0001_runtime.up.sql"); const CANONICAL_EVENT_STORAGE_V2_SQL: &str = include_str!("0002_canonical_event_storage.up.sql"); @@ -33,6 +33,8 @@ const AUTHORED_DELIVERY_FACTS_V16_SQL: &str = include_str!("0016_authored_delive const AUTHORED_DELIVERY_RECONCILIATION_V17_SQL: &str = include_str!("0017_authored_delivery_reconciliation.up.sql"); +const EVENT_MUTATION_EPOCH_V18_SQL: &str = include_str!("0018_event_mutation_epoch.up.sql"); + /// Stable, non-SQL description of one forward runtime migration. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct MigrationDescriptor { @@ -894,6 +896,99 @@ const RUNTIME_V17_OBJECTS: &[&str] = &[ "radroots_runtime_source_generations_sequence_guard", ]; +const RUNTIME_V18_OBJECTS: &[&str] = &[ + "radroots_runtime_atomic_commits", + "radroots_runtime_authored_artifacts", + "radroots_runtime_authored_artifacts_admission_ready_idx", + "radroots_runtime_authored_artifacts_signed_fact_guard", + "radroots_runtime_authored_artifacts_signing_ready_idx", + "radroots_runtime_authored_atomic_commits", + "radroots_runtime_authored_atomic_commits_delete_guard", + "radroots_runtime_authored_atomic_commits_update_guard", + "radroots_runtime_authored_atomic_target_phase_idx", + "radroots_runtime_authored_delivery_attempts", + "radroots_runtime_authored_delivery_claims", + "radroots_runtime_authored_delivery_claims_delete_guard", + "radroots_runtime_authored_delivery_claims_update_guard", + "radroots_runtime_authored_delivery_facts", + "radroots_runtime_authored_delivery_facts_delete_guard", + "radroots_runtime_authored_delivery_facts_update_guard", + "radroots_runtime_authored_delivery_plans", + "radroots_runtime_authored_delivery_ready_idx", + "radroots_runtime_authored_delivery_reconciliations", + "radroots_runtime_authored_delivery_reconciliations_delete_guard", + "radroots_runtime_authored_delivery_reconciliations_update_guard", + "radroots_runtime_authored_delivery_stop_guard", + "radroots_runtime_authored_delivery_targets", + "radroots_runtime_authored_draft_author_head_idx", + "radroots_runtime_authored_draft_revisions", + "radroots_runtime_authored_draft_revisions_delete_guard", + "radroots_runtime_authored_draft_revisions_update_guard", + "radroots_runtime_authored_draft_scope_head_idx", + "radroots_runtime_authored_migration_evidence", + "radroots_runtime_authored_migration_evidence_delete_guard", + "radroots_runtime_authored_migration_evidence_update_guard", + "radroots_runtime_authored_operations", + "radroots_runtime_delivery_evidence", + "radroots_runtime_delivery_evidence_item_idx", + "radroots_runtime_event_index_checkpoints", + "radroots_runtime_event_index_manifests", + "radroots_runtime_event_index_shards", + "radroots_runtime_event_provenance", + "radroots_runtime_event_provenance_observed_idx", + "radroots_runtime_events", + "radroots_runtime_events_admission_idx", + "radroots_runtime_events_contract_metadata_guard", + "radroots_runtime_events_contract_metadata_insert_guard", + "radroots_runtime_events_delete_guard", + "radroots_runtime_events_event_id_idx", + "radroots_runtime_events_mutation_insert", + "radroots_runtime_events_mutation_update", + "radroots_runtime_events_raw_update_guard", + "radroots_runtime_journal_idempotency_idx", + "radroots_runtime_journal_operations", + "radroots_runtime_journal_recovery_idx", + "radroots_runtime_legacy_event_staging", + "radroots_runtime_legacy_event_staging_delete_guard", + "radroots_runtime_legacy_event_staging_insert_guard", + "radroots_runtime_legacy_event_staging_update_guard", + "radroots_runtime_legacy_import_commit_delete_guard", + "radroots_runtime_legacy_import_commit_update_guard", + "radroots_runtime_legacy_import_commits", + "radroots_runtime_legacy_import_delete_guard", + "radroots_runtime_legacy_import_identity_guard", + "radroots_runtime_legacy_import_member_delete_guard", + "radroots_runtime_legacy_import_member_identity_guard", + "radroots_runtime_legacy_import_member_state_guard", + "radroots_runtime_legacy_import_members", + "radroots_runtime_legacy_import_state_guard", + "radroots_runtime_legacy_import_state_idx", + "radroots_runtime_legacy_imports", + "radroots_runtime_legacy_outbox_staging", + "radroots_runtime_legacy_outbox_staging_delete_guard", + "radroots_runtime_legacy_outbox_staging_insert_guard", + "radroots_runtime_legacy_outbox_staging_parent_idx", + "radroots_runtime_legacy_outbox_staging_update_guard", + "radroots_runtime_outbox_items", + "radroots_runtime_outbox_operation_idx", + "radroots_runtime_outbox_ready_idx", + "radroots_runtime_outbox_targets", + "radroots_runtime_projection_checkpoints", + "radroots_runtime_projection_documents", + "radroots_runtime_projection_invalidations", + "radroots_runtime_projection_rebuilds", + "radroots_runtime_projection_rebuilds_stage_idx", + "radroots_runtime_projection_snapshots", + "radroots_runtime_projection_snapshots_created_idx", + "radroots_runtime_projection_snapshots_update_guard", + "radroots_runtime_source_generations", + "radroots_runtime_source_generations_active_idx", + "radroots_runtime_source_generations_delete_guard", + "radroots_runtime_source_generations_identity_guard", + "radroots_runtime_source_generations_mutation_guard", + "radroots_runtime_source_generations_sequence_guard", +]; + /// Ordered, immutable runtime migration plan. pub const MIGRATIONS: &[MigrationDescriptor] = &[ MigrationDescriptor { @@ -998,6 +1093,12 @@ pub const MIGRATIONS: &[MigrationDescriptor] = &[ up_sha256: "01463e7effeb368dd0577bfed79f566f7bb2af76d03e8e76898cc53332e2e573", owned_objects: RUNTIME_V17_OBJECTS, }, + MigrationDescriptor { + version: 18, + name: "event_mutation_epoch", + up_sha256: "44e8f43722bb869f824dbfa28197d17c1e8f58c6d62b8219cdd19d452eb47c3c", + owned_objects: RUNTIME_V18_OBJECTS, + }, ]; pub(crate) const fn migration_sql(version: u32) -> Option<&'static str> { @@ -1019,6 +1120,7 @@ pub(crate) const fn migration_sql(version: u32) -> Option<&'static str> { 15 => Some(AUTHORED_SIGNED_FACTS_V15_SQL), 16 => Some(AUTHORED_DELIVERY_FACTS_V16_SQL), 17 => Some(AUTHORED_DELIVERY_RECONCILIATION_V17_SQL), + 18 => Some(EVENT_MUTATION_EPOCH_V18_SQL), _ => None, } } @@ -1062,8 +1164,8 @@ mod tests { fn migration_plan_matches_governed_snapshot() { let snapshot = toml::from_str::<PlanSnapshot>(PLAN_SNAPSHOT).expect("valid snapshot"); assert_eq!(MINIMUM_VERSION, 1); - assert_eq!(CURRENT_VERSION, 17); - assert_eq!(MIGRATIONS.len(), 17); + assert_eq!(CURRENT_VERSION, 18); + assert_eq!(MIGRATIONS.len(), 18); let migration = MIGRATIONS[8]; assert_eq!(snapshot.schema_version, 1); assert_eq!(snapshot.database, "runtime.sqlite"); diff --git a/crates/storage_sqlite/tests/open_lifecycle.rs b/crates/storage_sqlite/tests/open_lifecycle.rs @@ -24,6 +24,9 @@ async fn public_open_creates_both_databases_and_reuses_the_durable_generation() assert!(paths.runtime().is_file()); assert!(paths.private().is_file()); let status = EventStore::status(&store).await.expect("event status"); + let token = store.source_token().await.expect("source token"); + assert_eq!(token.generation(), expected); + assert_eq!(token.epoch(), 0); assert_eq!(status.generation(), expected); assert_eq!(status.mode(), EventStoreMode::ReadWrite); @@ -31,6 +34,10 @@ async fn public_open_creates_both_databases_and_reuses_the_durable_generation() .await .expect("concurrent reader"); let reader_status = EventStore::status(&reader).await.expect("reader status"); + assert_eq!( + reader.source_token().await.expect("reader source token"), + token + ); assert_eq!(reader_status.generation(), expected); assert_eq!(reader_status.mode(), EventStoreMode::ReadOnly); drop(reader); @@ -51,6 +58,13 @@ async fn public_open_creates_both_databases_and_reuses_the_durable_generation() .await .expect("reopen after guard release"); assert_eq!( + reopened + .source_token() + .await + .expect("reopened source token"), + token + ); + assert_eq!( EventStore::status(&reopened) .await .expect("reopened status")