lib

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

commit 14c5cd539d388fa3a835449679cb8ccbed8942fe
parent 225b1774b018b280593076ec46581c846c55a46d
Author: triesap <tyson@radroots.org>
Date:   Sun, 18 Oct 2026 08:08:24 +0000

storage: install native chat messages and recipient work

- retain one logical message and recipient publication work atomically
- preserve original provenance across late, partial and uncertain effects
- expose bounded private views and independent installation receipts
- verify native effects, portable APIs and unchanged coverage gates

Diffstat:
MCargo.lock | 1+
Mcontracts/api_baselines/radroots.txt | 5+++++
Mcontracts/api_baselines/radroots_domain.txt | 25+++++++++++++++++++++++++
Mcontracts/api_baselines/radroots_execution.txt | 7+++++--
Mcontracts/api_baselines/radroots_protocol.txt | 2+-
Mcontracts/api_baselines/radroots_storage.txt | 45+++++++++++++++++++++++++++++++++++++++++++++
Mcontracts/api_baselines/radroots_storage_sqlite.txt | 1+
Mcontracts/protocol/chat.v1.json | 39+++++++++++++++++++++++++++++++--------
Mcrates/domain/Cargo.toml | 1+
Mcrates/domain/README.md | 7+++++++
Mcrates/domain/src/lib.rs | 2++
Acrates/domain/src/message.rs | 116+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/domain/tests/message.rs | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/execution/README.md | 9+++++++--
Mcrates/execution/src/receipt.rs | 63+++++++++++++++++++++++++++++++++++++++++++++++++++------------
Mcrates/protocol/src/command.rs | 4++--
Mcrates/radroots/README.md | 9++++++---
Mcrates/radroots/src/domain.rs | 4++--
Acrates/radroots/tests/support/receipt_installation.rs | 449+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/radroots/tests/support/receipt_installation_refusal.rs | 261+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/radroots/tests/support/receipt_installation_reports.rs | 211+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/radroots/tests/support/receipt_plans.rs | 2++
Mcrates/storage/README.md | 11++++++++++-
Mcrates/storage/src/command.rs | 12++++++++++++
Acrates/storage/src/command/installation.rs | 254+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/storage/src/command/snapshot.rs | 126++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Mcrates/storage_sqlite/README.md | 25++++++++++++++++++++++---
Mcrates/storage_sqlite/src/workspace/crypto.rs | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/storage_sqlite/src/workspace/installation.rs | 499+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/storage_sqlite/src/workspace/mod.rs | 14++++++++++++++
Mcrates/storage_sqlite/src/workspace/snapshot.rs | 210+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
Mcrates/storage_sqlite/src/workspace/tests/mod.rs | 4++--
Mcrates/storage_sqlite/src/workspace/worker.rs | 52++++++++++++++++++++++++++++++++++++++++++++--------
Mcrates/storage_sqlite/tests/workspace_admission.rs | 2+-
34 files changed, 2472 insertions(+), 110 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -3103,6 +3103,7 @@ dependencies = [ "radroots_identity", "serde", "serde_json", + "zeroize", ] [[package]] diff --git a/contracts/api_baselines/radroots.txt b/contracts/api_baselines/radroots.txt @@ -44,9 +44,14 @@ pub const fn radroots::client::knowledge_enabled() -> bool pub const fn radroots::client::local_only() -> radroots_sdk::transport::Profile pub mod radroots::domain pub use radroots::domain::AmountError +pub use radroots::domain::CHAT_MAX_BYTES +pub use radroots::domain::ChatMessage +pub use radroots::domain::ChatRelation pub use radroots::domain::Decimal pub use radroots::domain::ExecutorId pub use radroots::domain::IdentifierError +pub use radroots::domain::MessageError +pub use radroots::domain::MessageId pub use radroots::domain::OperationId pub use radroots::domain::OrderId pub use radroots::domain::PositiveDecimal diff --git a/contracts/api_baselines/radroots_domain.txt b/contracts/api_baselines/radroots_domain.txt @@ -14,6 +14,13 @@ pub radroots_domain::AmountError::ScaleOutOfRange impl core::error::Error for radroots_domain::AmountError impl core::fmt::Display for radroots_domain::AmountError pub fn radroots_domain::AmountError::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result +#[non_exhaustive] pub enum radroots_domain::ChatRelation +pub radroots_domain::ChatRelation::Inquire +pub radroots_domain::ChatRelation::Inquire::source: core::option::Option<radroots_domain::SourceId> +pub radroots_domain::ChatRelation::Reply +pub radroots_domain::ChatRelation::Reply::parent: radroots_domain::MessageId +impl core::fmt::Debug for radroots_domain::ChatRelation +pub fn radroots_domain::ChatRelation::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result #[non_exhaustive] pub enum radroots_domain::ContextError pub radroots_domain::ContextError::InconsistentCoverage pub radroots_domain::ContextError::InvalidInteger @@ -32,6 +39,11 @@ pub radroots_domain::IdentifierError::InvalidLength impl core::error::Error for radroots_domain::IdentifierError impl core::fmt::Display for radroots_domain::IdentifierError pub fn radroots_domain::IdentifierError::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result +pub enum radroots_domain::MessageError +pub radroots_domain::MessageError::LimitExceeded +impl core::error::Error for radroots_domain::MessageError +impl core::fmt::Display for radroots_domain::MessageError +pub fn radroots_domain::MessageError::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result pub struct radroots_domain::ActorRef impl radroots_domain::ActorRef pub const fn radroots_domain::ActorRef::grant_id(self) -> core::option::Option<radroots_domain::GrantId> @@ -70,6 +82,18 @@ impl radroots_domain::AuthorityRef pub const fn radroots_domain::AuthorityRef::executor_id(self) -> radroots_domain::ExecutorId pub const fn radroots_domain::AuthorityRef::generation(self) -> radroots_domain::Generation pub const fn radroots_domain::AuthorityRef::new(radroots_domain::ExecutorId, radroots_domain::Generation) -> Self +pub struct radroots_domain::ChatMessage +impl radroots_domain::ChatMessage +pub const fn radroots_domain::ChatMessage::author(&self) -> radroots_identity::key::PublicKey +pub const fn radroots_domain::ChatMessage::created_at(&self) -> radroots_domain::UnixSeconds +pub const fn radroots_domain::ChatMessage::id(&self) -> radroots_domain::MessageId +pub fn radroots_domain::ChatMessage::new(radroots_domain::MessageId, radroots_domain::ThreadId, radroots_identity::key::PublicKey, radroots_identity::key::PublicKey, radroots_domain::UnixSeconds, radroots_domain::ChatRelation, &str) -> core::result::Result<Self, radroots_domain::MessageError> +pub const fn radroots_domain::ChatMessage::peer(&self) -> radroots_identity::key::PublicKey +pub const fn radroots_domain::ChatMessage::relation(&self) -> radroots_domain::ChatRelation +pub fn radroots_domain::ChatMessage::text(&self) -> &str +pub const fn radroots_domain::ChatMessage::thread_id(&self) -> radroots_domain::ThreadId +impl core::fmt::Debug for radroots_domain::ChatMessage +pub fn radroots_domain::ChatMessage::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result pub struct radroots_domain::CheckpointDigest(_) impl radroots_domain::CheckpointDigest pub const fn radroots_domain::CheckpointDigest::as_bytes(&self) -> &[u8; 32] @@ -420,4 +444,5 @@ impl serde_core::ser::Serialize for radroots_domain::WorkspaceId pub fn radroots_domain::WorkspaceId::serialize<S: serde_core::ser::Serializer>(&self, S) -> core::result::Result<<S as serde_core::ser::Serializer>::Ok, <S as serde_core::ser::Serializer>::Error> impl<'de> serde_core::de::Deserialize<'de> for radroots_domain::WorkspaceId pub fn radroots_domain::WorkspaceId::deserialize<D: serde_core::de::Deserializer<'de>>(D) -> core::result::Result<Self, <D as serde_core::de::Deserializer>::Error> +pub const radroots_domain::CHAT_MAX_BYTES: usize pub const radroots_domain::FIRST_ISSUANCE_MAX_SECONDS: u64 diff --git a/contracts/api_baselines/radroots_execution.txt b/contracts/api_baselines/radroots_execution.txt @@ -195,6 +195,7 @@ pub const fn radroots_execution::ArtifactObservation::intent_digest(&self) -> co pub const fn radroots_execution::ArtifactObservation::issuance(&self) -> radroots_execution::Issuance pub const fn radroots_execution::ArtifactObservation::observed_at_unix_ms(&self) -> core::option::Option<core::num::nonzero::NonZeroU64> pub const fn radroots_execution::ArtifactObservation::obtained_digest(&self) -> core::option::Option<[u8; 32]> +pub const fn radroots_execution::ArtifactObservation::publication_transaction_id(&self) -> core::option::Option<radroots_domain::identifier::TransactionId> pub const fn radroots_execution::ArtifactObservation::recipient(&self) -> core::option::Option<radroots_identity::key::PublicKey> pub const fn radroots_execution::ArtifactObservation::role(&self) -> radroots_protocol::intent::ArtifactRole impl core::fmt::Debug for radroots_execution::ArtifactObservation @@ -231,8 +232,8 @@ pub const fn radroots_execution::OperationContext::operation_id(self) -> core::o pub const fn radroots_execution::OperationContext::proposed(core::option::Option<radroots_domain::identifier::OperationId>) -> Self pub const fn radroots_execution::OperationContext::retained(radroots_domain::identifier::OperationId) -> Self pub struct radroots_execution::OperationReceipt<T> -impl radroots_execution::OperationReceipt<()> -pub fn radroots_execution::OperationReceipt<()>::from_snapshot(&radroots_storage::command::snapshot::OperationSnapshot) -> Self +impl radroots_execution::OperationReceipt<radroots_domain::message::ChatMessage> +pub fn radroots_execution::OperationReceipt<radroots_domain::message::ChatMessage>::from_snapshot(&radroots_storage::command::snapshot::OperationSnapshot) -> Self impl<T> radroots_execution::OperationReceipt<T> pub const fn radroots_execution::OperationReceipt<T>::admission(&self) -> radroots_execution::Admission pub const fn radroots_execution::OperationReceipt<T>::admission_transaction_id(&self) -> radroots_domain::identifier::TransactionId @@ -248,6 +249,8 @@ pub const fn radroots_execution::OperationReceipt<T>::delivery(&self) -> radroot pub fn radroots_execution::OperationReceipt<T>::delivery_observations(&self) -> &[radroots_execution::DeliveryObservation] pub const fn radroots_execution::OperationReceipt<T>::durable(&self) -> bool pub const fn radroots_execution::OperationReceipt<T>::error(&self) -> core::option::Option<radroots_execution::Error> +pub const fn radroots_execution::OperationReceipt<T>::installation_transaction_id(&self) -> core::option::Option<radroots_domain::identifier::TransactionId> +pub const fn radroots_execution::OperationReceipt<T>::installed_at(&self) -> core::option::Option<radroots_domain::context::UnixSeconds> pub const fn radroots_execution::OperationReceipt<T>::issuance(&self) -> radroots_execution::Issuance pub const fn radroots_execution::OperationReceipt<T>::issuance_window(&self) -> radroots_domain::issuance::IssuanceWindow pub const fn radroots_execution::OperationReceipt<T>::message_id(&self) -> core::option::Option<radroots_domain::identifier::MessageId> diff --git a/contracts/api_baselines/radroots_protocol.txt b/contracts/api_baselines/radroots_protocol.txt @@ -133,6 +133,7 @@ pub const radroots_protocol::chat::RUMOR_MAX_BYTES: usize pub const radroots_protocol::chat::SIGNED_MAX_BYTES: usize pub fn radroots_protocol::chat::nip44_v2_payload_len(usize) -> core::result::Result<usize, radroots_protocol::nip01::WireError> pub mod radroots_protocol::command +pub use radroots_protocol::command::CHAT_MAX_BYTES pub enum radroots_protocol::command::ChatKind pub radroots_protocol::command::ChatKind::Inquire pub radroots_protocol::command::ChatKind::Reply @@ -190,7 +191,6 @@ pub fn radroots_protocol::command::ReplyInput::text(&self) -> &str pub const fn radroots_protocol::command::ReplyInput::thread_id(&self) -> radroots_domain::identifier::ThreadId impl core::fmt::Debug for radroots_protocol::command::ReplyInput pub fn radroots_protocol::command::ReplyInput::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result -pub const radroots_protocol::command::CHAT_MAX_BYTES: usize pub const radroots_protocol::command::COMMAND_MAX_BYTES: usize pub mod radroots_protocol::error pub mod radroots_protocol::error::v1 diff --git a/contracts/api_baselines/radroots_storage.txt b/contracts/api_baselines/radroots_storage.txt @@ -772,6 +772,11 @@ pub fn radroots_storage::command::ClaimAttemptOutcome::fmt(&self, &mut core::fmt pub enum radroots_storage::command::EvidenceCaptureOutcome pub radroots_storage::command::EvidenceCaptureOutcome::Committed(radroots_storage::command::EvidenceReceipt) pub radroots_storage::command::EvidenceCaptureOutcome::Unknown(radroots_storage::command::CommitAttempt) +pub enum radroots_storage::command::InstallationOutcome +pub radroots_storage::command::InstallationOutcome::Committed(alloc::boxed::Box<radroots_storage::command::ChatInstallation>) +pub radroots_storage::command::InstallationOutcome::Unknown(radroots_storage::command::CommitAttempt) +impl core::fmt::Debug for radroots_storage::command::InstallationOutcome +pub fn radroots_storage::command::InstallationOutcome::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result #[non_exhaustive] pub enum radroots_storage::command::OperationError pub radroots_storage::command::OperationError::DeadlineExceeded pub radroots_storage::command::OperationError::IdempotencyConflict @@ -830,6 +835,21 @@ pub fn radroots_storage::command::CapturedSeal::from_committed_store(radroots_st pub const fn radroots_storage::command::CapturedSeal::receipt(&self) -> radroots_storage::command::EvidenceReceipt impl core::fmt::Debug for radroots_storage::command::CapturedSeal pub fn radroots_storage::command::CapturedSeal::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result +pub struct radroots_storage::command::ChatInstallation +impl radroots_storage::command::ChatInstallation +pub const fn radroots_storage::command::ChatInstallation::basis(&self) -> radroots_storage::command::EvidenceReceipt +pub const fn radroots_storage::command::ChatInstallation::command_digest(&self) -> radroots_domain::identifier::CommandDigest +pub fn radroots_storage::command::ChatInstallation::from_committed_store(&radroots_storage::command::BoundSigningPlan, radroots_storage::command::EvidenceReceipt, radroots_domain::identifier::TransactionId, radroots_domain::context::UnixSeconds) -> core::result::Result<Self, radroots_storage::command::OperationError> +pub const fn radroots_storage::command::ChatInstallation::installed_at(&self) -> radroots_domain::context::UnixSeconds +pub const fn radroots_storage::command::ChatInstallation::message_id(&self) -> radroots_domain::identifier::MessageId +pub const fn radroots_storage::command::ChatInstallation::operation_id(&self) -> radroots_domain::identifier::OperationId +pub const fn radroots_storage::command::ChatInstallation::plan_digest(&self) -> &[u8; 32] +pub const fn radroots_storage::command::ChatInstallation::plan_transaction_id(&self) -> radroots_domain::identifier::TransactionId +pub const fn radroots_storage::command::ChatInstallation::preparation_transaction_id(&self) -> radroots_domain::identifier::TransactionId +pub const fn radroots_storage::command::ChatInstallation::transaction_id(&self) -> radroots_domain::identifier::TransactionId +pub const fn radroots_storage::command::ChatInstallation::workspace_id(&self) -> radroots_domain::identifier::WorkspaceId +impl core::fmt::Debug for radroots_storage::command::ChatInstallation +pub fn radroots_storage::command::ChatInstallation::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result pub struct radroots_storage::command::ClaimAttemptRequest impl radroots_storage::command::ClaimAttemptRequest pub const fn radroots_storage::command::ClaimAttemptRequest::binding(&self) -> &radroots_storage::workspace::WorkspaceBinding @@ -875,15 +895,29 @@ pub const fn radroots_storage::command::EvidenceReceipt::operation_id(&self) -> pub const fn radroots_storage::command::EvidenceReceipt::recipient(&self) -> radroots_identity::key::PublicKey pub const fn radroots_storage::command::EvidenceReceipt::transaction_id(&self) -> radroots_domain::identifier::TransactionId pub const fn radroots_storage::command::EvidenceReceipt::workspace_id(&self) -> radroots_domain::identifier::WorkspaceId +pub struct radroots_storage::command::InstallationRequest +impl radroots_storage::command::InstallationRequest +pub const fn radroots_storage::command::InstallationRequest::binding(&self) -> &radroots_storage::workspace::WorkspaceBinding +pub const fn radroots_storage::command::InstallationRequest::command_digest(&self) -> radroots_domain::identifier::CommandDigest +pub fn radroots_storage::command::InstallationRequest::identity(&self) -> radroots_storage::command::CommitAttempt +pub const fn radroots_storage::command::InstallationRequest::new(radroots_storage::workspace::WorkspaceBinding, radroots_domain::identifier::OperationId, radroots_domain::identifier::CommandDigest, [u8; 32]) -> Self +pub const fn radroots_storage::command::InstallationRequest::operation_id(&self) -> radroots_domain::identifier::OperationId +pub const fn radroots_storage::command::InstallationRequest::plan_digest(&self) -> &[u8; 32] +impl core::fmt::Debug for radroots_storage::command::InstallationRequest +pub fn radroots_storage::command::InstallationRequest::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result pub struct radroots_storage::command::OperationSnapshot impl radroots_storage::command::OperationSnapshot pub fn radroots_storage::command::OperationSnapshot::bound_plan(&self) -> core::option::Option<&radroots_storage::command::BoundSigningPlan> pub fn radroots_storage::command::OperationSnapshot::checkpoint_digest(&self) -> radroots_domain::identifier::CheckpointDigest pub fn radroots_storage::command::OperationSnapshot::from_store_report(radroots_storage::command::RetainedOperation, alloc::vec::Vec<radroots_storage::command::RecipientSnapshot>, radroots_domain::context::UnixSeconds) -> core::result::Result<Self, radroots_storage::command::OperationError> +pub const fn radroots_storage::command::OperationSnapshot::installation(&self) -> core::option::Option<radroots_storage::command::ChatInstallation> +pub fn radroots_storage::command::OperationSnapshot::installed_message(&self) -> core::option::Option<&radroots_domain::message::ChatMessage> pub fn radroots_storage::command::OperationSnapshot::message_id(&self) -> core::option::Option<radroots_domain::identifier::MessageId> pub const fn radroots_storage::command::OperationSnapshot::observed_at(&self) -> radroots_domain::context::UnixSeconds pub fn radroots_storage::command::OperationSnapshot::prepared(&self) -> &radroots_storage::command::PreparedOperation +pub fn radroots_storage::command::OperationSnapshot::publications(&self) -> &[radroots_storage::command::PublicationWork] pub fn radroots_storage::command::OperationSnapshot::recipients(&self) -> &[radroots_storage::command::RecipientSnapshot] +pub fn radroots_storage::command::OperationSnapshot::with_installation(self, core::option::Option<radroots_storage::command::ChatInstallation>, alloc::vec::Vec<radroots_storage::command::PublicationWork>) -> core::result::Result<Self, radroots_storage::command::OperationError> impl core::fmt::Debug for radroots_storage::command::OperationSnapshot pub fn radroots_storage::command::OperationSnapshot::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result pub struct radroots_storage::command::PreparedOperation @@ -896,6 +930,16 @@ pub const fn radroots_storage::command::PreparedOperation::revision(&self) -> ra pub const fn radroots_storage::command::PreparedOperation::transaction_id(&self) -> radroots_domain::identifier::TransactionId impl core::fmt::Debug for radroots_storage::command::PreparedOperation pub fn radroots_storage::command::PreparedOperation::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result +pub struct radroots_storage::command::PublicationWork +impl radroots_storage::command::PublicationWork +pub const fn radroots_storage::command::PublicationWork::evidence(&self) -> radroots_storage::command::EvidenceReceipt +pub fn radroots_storage::command::PublicationWork::from_committed_store(&radroots_storage::command::BoundSigningPlan, radroots_storage::command::ChatInstallation, radroots_storage::command::EvidenceReceipt, radroots_domain::identifier::TransactionId, radroots_domain::context::UnixSeconds) -> core::result::Result<Self, radroots_storage::command::OperationError> +pub const fn radroots_storage::command::PublicationWork::installation_transaction_id(&self) -> radroots_domain::identifier::TransactionId +pub const fn radroots_storage::command::PublicationWork::plan_digest(&self) -> &[u8; 32] +pub const fn radroots_storage::command::PublicationWork::queued_at(&self) -> radroots_domain::context::UnixSeconds +pub const fn radroots_storage::command::PublicationWork::transaction_id(&self) -> radroots_domain::identifier::TransactionId +impl core::fmt::Debug for radroots_storage::command::PublicationWork +pub fn radroots_storage::command::PublicationWork::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result pub struct radroots_storage::command::RecipientSnapshot impl radroots_storage::command::RecipientSnapshot pub const fn radroots_storage::command::RecipientSnapshot::claim(self) -> core::option::Option<radroots_storage::command::ClaimSnapshot> @@ -942,6 +986,7 @@ pub fn radroots_storage::command::OperationJournal::admit_command_and_hold(&self pub fn radroots_storage::command::OperationJournal::bind_signing_intents(&self, radroots_storage::workspace::WorkspaceBinding, radroots_protocol::chat::plan::ChatSigningPlan, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::command::PlanBindingOutcome, radroots_storage::command::OperationError>> pub fn radroots_storage::command::OperationJournal::capture_evidence<'a>(&'a self, radroots_storage::workspace::AttachmentInput, &'a radroots_storage::workspace::AttachmentProof, &'a radroots_storage::command::SealEvidence) -> radroots_transport::source::BoxFuture<'a, core::result::Result<radroots_storage::command::EvidenceCaptureOutcome, radroots_storage::command::OperationError>> pub fn radroots_storage::command::OperationJournal::claim_attempt(&self, radroots_storage::command::ClaimAttemptRequest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::command::ClaimAttemptOutcome, radroots_storage::command::OperationError>> +pub fn radroots_storage::command::OperationJournal::install_transition_and_outbox(&self, radroots_storage::command::InstallationRequest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::command::InstallationOutcome, radroots_storage::command::OperationError>> pub fn radroots_storage::command::OperationJournal::obtained_evidence(&self, radroots_storage::command::ClaimAttemptRequest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::command::CapturedSeal>, radroots_storage::command::OperationError>> pub fn radroots_storage::command::OperationJournal::operation(&self, radroots_storage::workspace::WorkspaceBinding, radroots_domain::identifier::OperationId, core::option::Option<radroots_domain::identifier::CommandDigest>, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::command::PreparedOperation>, radroots_storage::command::OperationError>> pub fn radroots_storage::command::OperationJournal::operation_snapshot(&self, radroots_storage::workspace::WorkspaceBinding, radroots_domain::identifier::OperationId, core::option::Option<radroots_domain::identifier::CommandDigest>, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::command::OperationSnapshot>, radroots_storage::command::OperationError>> diff --git a/contracts/api_baselines/radroots_storage_sqlite.txt b/contracts/api_baselines/radroots_storage_sqlite.txt @@ -384,6 +384,7 @@ pub fn radroots_storage_sqlite::workspace::NativeWorkspace::admit_command_and_ho pub fn radroots_storage_sqlite::workspace::NativeWorkspace::bind_signing_intents(&self, radroots_storage::workspace::WorkspaceBinding, radroots_protocol::chat::plan::ChatSigningPlan, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::command::PlanBindingOutcome, radroots_storage::command::OperationError>> pub fn radroots_storage_sqlite::workspace::NativeWorkspace::capture_evidence<'a>(&'a self, radroots_storage::workspace::AttachmentInput, &'a radroots_storage::workspace::AttachmentProof, &'a radroots_storage::command::issuance::SealEvidence) -> radroots_transport::source::BoxFuture<'a, core::result::Result<radroots_storage::command::issuance::EvidenceCaptureOutcome, radroots_storage::command::OperationError>> pub fn radroots_storage_sqlite::workspace::NativeWorkspace::claim_attempt(&self, radroots_storage::command::issuance::ClaimAttemptRequest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::command::issuance::ClaimAttemptOutcome, radroots_storage::command::OperationError>> +pub fn radroots_storage_sqlite::workspace::NativeWorkspace::install_transition_and_outbox(&self, radroots_storage::command::installation::InstallationRequest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::command::installation::InstallationOutcome, radroots_storage::command::OperationError>> pub fn radroots_storage_sqlite::workspace::NativeWorkspace::obtained_evidence(&self, radroots_storage::command::issuance::ClaimAttemptRequest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::command::issuance::CapturedSeal>, radroots_storage::command::OperationError>> pub fn radroots_storage_sqlite::workspace::NativeWorkspace::operation(&self, radroots_storage::workspace::WorkspaceBinding, radroots_domain::identifier::OperationId, core::option::Option<radroots_domain::identifier::CommandDigest>, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::command::PreparedOperation>, radroots_storage::command::OperationError>> pub fn radroots_storage_sqlite::workspace::NativeWorkspace::operation_snapshot(&self, radroots_storage::workspace::WorkspaceBinding, radroots_domain::identifier::OperationId, core::option::Option<radroots_domain::identifier::CommandDigest>, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::command::snapshot::OperationSnapshot>, radroots_storage::command::OperationError>> diff --git a/contracts/protocol/chat.v1.json b/contracts/protocol/chat.v1.json @@ -95,7 +95,7 @@ "protected_data_part_max_bytes": 16384, "max_data_parts": 24, "protected_total_max_bytes": 2097152, - "native_workspace_schema": 5, + "native_workspace_schema": 6, "predecessors": "Preserve and refuse; no inferred timestamps or automatic namespace rewrite", "queue": "32 slots plus conservative queued and in-flight reservations under 4194304 bytes", "transaction": "Current protected grant, original preparation, all indexed facts and fixed first-issue window rechecked under BEGIN IMMEDIATE; crypto work before transaction", @@ -116,8 +116,8 @@ "max_data_parts": 4, "protected_total_max_bytes": 1048576, "capture_queue_reservation_bytes": 262144, - "schema": 5, - "schema_objects": 11, + "schema": 6, + "schema_objects": 16, "remaining": "Ordinary client-owned orchestration and full operation receipts, route/provider approval, installation/outbox/delivery, target graph retirement and all consumer/final gates" }, "native_operation_snapshot": { @@ -125,11 +125,11 @@ "snapshot_owner": "radroots_storage::command::OperationSnapshot", "query": "OperationJournal::operation_snapshot on the existing native owner", "receipt_owner": "radroots_execution::OperationReceipt; ordinary facade reexports", - "native_schema": 5, - "transaction": "Authenticate complete original preparation/plan/expected claims and obtained evidence outside transaction; recheck current grant, all retained and absent facts and complete graph counts under BEGIN IMMEDIATE; confirmed rollback before reporting", + "native_schema": 6, + "transaction": "Authenticate complete original T0/T1/S and protected T2 records outside transaction; recheck current grant, all retained and absent facts and complete bounded graph counts under BEGIN IMMEDIATE; confirmed rollback before reporting", "extra_or_orphan_rows": "Recovery required; never silently omitted from a complete report", "memory": "One complete owned plan and at most two sorted unique boxed recipient reports; no private raw bytes or automatic payload serialization in receipt diagnostics", - "progress_digest": "SHA-256 with radroots.operation.snapshot.v1 NUL separator; workspace/operation/command, T0 transaction/revision/window, authority checkpoint digest, optional T1 plan/transaction/time, each recipient claim/intent/time and obtained transaction/event/raw digest/optional time; excludes query time and diagnostic correlation", + "progress_digest": "SHA-256 with radroots.operation.snapshot.v2 NUL separator; original v1 fact dimensions plus optional installation transaction/time/basis capture and each recipient publication-work transaction/time; excludes query time and diagnostic correlation", "dimensions": [ "durable_admission", "business_installation", @@ -141,7 +141,30 @@ "first_issue_expiry": "Remaining unrequested work is blocked; obtained and unknown classifications are preserved. Expiry proves no nonissuance.", "scope": "Original actor/workspace/authority/route, command/transactions/window, authority evidence checkpoint, logical-message and structured-record namespaces, protected artifact references and nullable original observations", "construction_authority": "Pure values do not authenticate a principal, qualify a custom adapter, permit dispatch or prove installation/delivery", - "remaining": "Native T2 installation/publication work, exact wrapping/delivery, library-owned ordinary inbox jobs, complete browser/binding/consumer closure and final acceptance", - "count_bounds": "Whole-operation extra-recipient count checks stop at excess sentinels 3 claims, 3 obtained manifests and 9 parts; absent-operation checks use EXISTS. Existing protected per-recipient part bounds and authentication remain mandatory." + "remaining": "Exact outer wrapping/delivery, library-owned ordinary inbox jobs, complete browser/binding/consumer closure and final acceptance", + "count_bounds": "Whole-operation extra-recipient count checks stop at excess sentinels 3 claims, 3 obtained manifests and 9 parts; absent-operation checks use EXISTS. Existing protected per-recipient part bounds and authentication remain mandatory. T2 sentinels stop at 2 messages and 3 publication-work rows." + }, + "native_installation": { + "state": "native_t2_component_implemented", + "transaction_owner": "Existing bounded native workspace connection worker; no second engine", + "operation": "OperationJournal::install_transition_and_outbox", + "business_view": "radroots_domain::ChatMessage; original captured text/relations and original logical message time, independently owned and diagnostically redacted", + "plain_text_max_bytes": 4096, + "body_retention": "Original authenticated T0 command; fixed protected T2 records reference it and the exact complete T1 plan without duplicate private text", + "atomic_effect": "One logical chat_messages installation plus eligible actual-recipient chat_publication_work in one IMMEDIATE transaction; complete current grant and all retained/absent T0/T1/S/T2 facts compared before writes", + "partial_and_late": "Any actual matching obtained recipient artifact may establish the original installation. Later capture adds only its missing publication work; original installation transaction/time and earlier links stay unchanged.", + "window": "Original first-issue window constrains new signatures, not installation of matching already obtained evidence. Current access still gates T2.", + "outcomes": "Confirmed commit, known rollback and original anchored unknown; unknown latches until safe reopen. Claims/evidence are never released by T2 failures or observer loss.", + "replay": "Compare original retained graph before fresh work; no replacement installation, new entropy/protection or signature on exact replay", + "protected_plaintext_max_bytes": 16384, + "protected_row_max_bytes": 262144, + "protected_aggregate_upper_bound_bytes": 786432, + "live_queue": "Existing 32 slots and 4194304 accepted/in-flight input bytes; 16384-byte installation input reservation", + "schema": 6, + "schema_objects": 16, + "cutover": "Preserve and refuse all predecessor namespaces, including actual schema5 claims/evidence; no automatic upgrade or state deletion", + "reporting": "Owned message only with scoped actual installation report; independent original installation and recipient work transactions; delivery pending and zero inferred relay observations", + "qualification": "Separate exact-source executed evidence; declarations and data constructors are not qualification", + "remaining": "Exact outer-envelope retention and per-target D observations, ordinary library-owned inbox jobs, browser/bindings, supported consumer adoption and all final gates" } } diff --git a/crates/domain/Cargo.toml b/crates/domain/Cargo.toml @@ -20,6 +20,7 @@ serde = ["dep:serde", "radroots_identity/serde"] [dependencies] radroots_identity = { workspace = true, default-features = false } serde = { workspace = true, optional = true } +zeroize = { workspace = true, features = ["alloc"] } [dev-dependencies] serde_json = { workspace = true } diff --git a/crates/domain/README.md b/crates/domain/README.md @@ -44,4 +44,11 @@ sources retain full digests independently of structured-record identity. `CommandContextData` is untrusted data; only an authenticated execution session can construct the opaque executable context. +`ChatMessage` owns a bounded logical participant view, with separate message, +thread, author, peer, creation time and inquiry/source or reply relation. Plain +text has the shared 4,096 UTF-8 byte limit before JSON/encryption expansion; +empty text and Unicode contents are preserved exactly. Private text uses +zeroizing owned storage and redacted diagnostics, with no automatic payload +serialization. Constructing a value proves no signature, history or installation. + Licensed under MIT OR Apache-2.0; see LICENSE-MIT and LICENSE-APACHE. diff --git a/crates/domain/src/lib.rs b/crates/domain/src/lib.rs @@ -9,6 +9,7 @@ mod amount; mod context; mod identifier; mod issuance; +mod message; /// The identity-owned validated participant key; no second key type is defined. pub use radroots_identity::PublicKey; @@ -24,3 +25,4 @@ pub use identifier::{ SourceId, ThreadId, TransactionId, WorkspaceId, }; pub use issuance::{FIRST_ISSUANCE_MAX_SECONDS, IssuanceWindow}; +pub use message::{CHAT_MAX_BYTES, ChatMessage, ChatRelation, MessageError}; diff --git a/crates/domain/src/message.rs b/crates/domain/src/message.rs @@ -0,0 +1,116 @@ +//! Bounded logical private chat data, without authenticated installation facts. +use crate::{MessageId, PublicKey, SourceId, ThreadId, UnixSeconds}; +use alloc::string::String; +use core::fmt; +use zeroize::Zeroizing; + +/// Maximum plain UTF-8 chat bytes before JSON or encryption expansion. +pub const CHAT_MAX_BYTES: usize = 4096; + +/// Input-free logical message failures. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum MessageError { + /// The plain text budget was exceeded. + LimitExceeded, +} +impl fmt::Display for MessageError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("chat text limit exceeded") + } +} +impl core::error::Error for MessageError {} + +/// A logical relation. Selectors alone authenticate no source or parent history. +#[derive(Clone, Copy, Eq, PartialEq)] +#[non_exhaustive] +pub enum ChatRelation { + /// An inquiry, optionally referring to the exact public source event. + Inquire { + /// Exact referenced public event, when supplied. + source: Option<SourceId>, + }, + /// A reply to an exact logical message, never an outer envelope ID. + Reply { + /// Original logical parent, whose history requires authentication. + parent: MessageId, + }, +} +impl fmt::Debug for ChatRelation { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("ChatRelation(<redacted>)") + } +} + +/// Owned logical participant view. Construction checks bounds only; it supplies +/// no signature, authenticated history, durable admission or installation proof. +/// Private text is explicitly borrowed and is never included in Debug or an +/// automatic serialization implementation. +#[derive(Clone, Eq, PartialEq)] +pub struct ChatMessage { + id: MessageId, + thread: ThreadId, + author: PublicKey, + peer: PublicKey, + created: UnixSeconds, + relation: ChatRelation, + text: Zeroizing<String>, +} +impl ChatMessage { + /// Checks the UTF-8 budget before copying. Empty text and Unicode contents + /// remain representable without trimming or normalization. + pub fn new( + id: MessageId, + thread: ThreadId, + author: PublicKey, + peer: PublicKey, + created: UnixSeconds, + relation: ChatRelation, + text: &str, + ) -> Result<Self, MessageError> { + if text.len() > CHAT_MAX_BYTES { + return Err(MessageError::LimitExceeded); + } + Ok(Self { + id, + thread, + author, + peer, + created, + relation, + text: Zeroizing::new(String::from(text)), + }) + } + /// Logical rumor identity, separate from seals and outer envelopes. + pub const fn id(&self) -> MessageId { + self.id + } + /// Exact application thread. + pub const fn thread_id(&self) -> ThreadId { + self.thread + } + /// Original participant author. + pub const fn author(&self) -> PublicKey { + self.author + } + /// Intended peer; a self copy does not replace the peer's outcome. + pub const fn peer(&self) -> PublicKey { + self.peer + } + /// Original logical creation time, not a later installation observation. + pub const fn created_at(&self) -> UnixSeconds { + self.created + } + /// Original inquiry/source or logical reply relation. + pub const fn relation(&self) -> ChatRelation { + self.relation + } + /// Borrows private text for an authorized participant view. + pub fn text(&self) -> &str { + &self.text + } +} +impl fmt::Debug for ChatMessage { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("ChatMessage(<redacted>)") + } +} diff --git a/crates/domain/tests/message.rs b/crates/domain/tests/message.rs @@ -0,0 +1,54 @@ +use radroots_domain::{ + CHAT_MAX_BYTES, ChatMessage, ChatRelation, MessageError, MessageId, PublicKey, SourceId, + ThreadId, UnixSeconds, +}; + +fn message(text: &str, relation: ChatRelation) -> Result<ChatMessage, MessageError> { + ChatMessage::new( + MessageId::from_bytes([0x11; 32]), + ThreadId::from_bytes([0x22; 32]), + PublicKey::from_hex("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798") + .unwrap(), + PublicKey::from_hex("c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5") + .unwrap(), + UnixSeconds::new(100).unwrap(), + relation, + text, + ) +} + +#[test] +fn private_logical_views_preserve_exact_relations_and_utf8_bounds_without_diagnostics() { + let relation = ChatRelation::Inquire { + source: Some(SourceId::from_bytes([0x33; 32])), + }; + let text = "🥕".repeat(CHAT_MAX_BYTES / 4); + let value = message(&text, relation).unwrap(); + let owned = value.clone(); + drop(value); + assert_eq!(owned.text(), text); + assert_eq!(owned.id(), MessageId::from_bytes([0x11; 32])); + assert_eq!(owned.thread_id(), ThreadId::from_bytes([0x22; 32])); + assert_ne!(owned.author(), owned.peer()); + assert_eq!(owned.created_at().get(), 100); + assert_eq!(owned.relation(), relation); + assert_eq!(format!("{owned:?}"), "ChatMessage(<redacted>)"); + assert_eq!(format!("{relation:?}"), "ChatRelation(<redacted>)"); + assert!(matches!( + message(&(text + "x"), relation), + Err(MessageError::LimitExceeded) + )); + assert_eq!( + MessageError::LimitExceeded.to_string(), + "chat text limit exceeded" + ); + + let parent = MessageId::from_bytes([0x44; 32]); + let reply = message("", ChatRelation::Reply { parent }).unwrap(); + assert_eq!(reply.text(), ""); + assert_eq!(reply.relation(), ChatRelation::Reply { parent }); + assert_eq!( + message("e\u{301}\0\n", relation).unwrap().text(), + "e\u{301}\0\n" + ); +} diff --git a/crates/execution/README.md b/crates/execution/README.md @@ -94,7 +94,7 @@ Closing the attachment invalidates its contexts. A custom adapter is an explicit host trust boundary and requires its own qualification; its observation cannot establish native SQL authority. An approval descriptor is not consent proof. -Package upload is not authorized. Actual durable command transactions, outbox +Package upload is not authorized. Ordinary durable command orchestration, outbox workers, inventory authority, public listing/private chat execution, browser attachment and new bindings remain separate requirements. Revalidating a context is not durable admission; the authoritative transaction must recheck its scope. @@ -106,7 +106,12 @@ It separates installation, per-recipient issuance, delivery and caller waiting. Its diagnostics contain no private command text or ciphertext. The optional `receipts` feature projects a Storage operation snapshot without I/O; the concrete adapter must qualify the underlying authenticated coherent query. -The current native T0/T1/S profile reports no business installation or delivery. +The native T2 profile projects an owned bounded `ChatMessage` when the concrete +owner reports a matching installed result. It preserves the original installation +transaction/time separately from later recipient publication-work transactions. +Pending wrapping/publication work does not establish transport acknowledgement. +Newly captured recipient evidence may require T2 continuation while the original +message remains installed; unknown issuance remains independently unknown. Expired remaining first issuance is blocked while obtained and unknown facts remain retained. Changing a wait observation cannot change those facts. Default receipt values remain usable without an async runtime on noStd targets; diff --git a/crates/execution/src/receipt.rs b/crates/execution/src/receipt.rs @@ -4,9 +4,9 @@ use crate::{Admission, Error}; use alloc::boxed::Box; use core::{fmt, num::NonZeroU64}; use radroots_domain::{ - CheckpointDigest, CheckpointRef, CommandContextData, CommandDigest, EnvelopeEventId, - IssuanceWindow, MessageId, PublicKey, RecordId, SigningAttemptId, SourceId, TransactionId, - UnixSeconds, + ChatMessage, CheckpointDigest, CheckpointRef, CommandContextData, CommandDigest, + EnvelopeEventId, IssuanceWindow, MessageId, PublicKey, RecordId, SigningAttemptId, SourceId, + TransactionId, UnixSeconds, }; use radroots_protocol::intent::ArtifactRole; @@ -115,6 +115,7 @@ pub struct ArtifactObservation { claim_transaction: Option<TransactionId>, intent_digest: Option<[u8; 32]>, capture_transaction: Option<TransactionId>, + publication_transaction: Option<TransactionId>, obtained_digest: Option<[u8; 32]>, observed_at: Option<NonZeroU64>, } @@ -174,6 +175,12 @@ impl ArtifactObservation { "Digest of the exact original obtained representation." ); copied!( + publication_transaction_id, + Option<TransactionId>, + publication_transaction, + "Original transaction retaining this recipient's exact publication work." + ); + copied!( observed_at_unix_ms, Option<NonZeroU64>, observed_at, @@ -239,6 +246,8 @@ pub struct OperationReceipt<T> { admission: Admission, admission_transaction: TransactionId, plan_transaction: Option<TransactionId>, + installation_transaction: Option<TransactionId>, + installed_at: Option<UnixSeconds>, issuance_window: IssuanceWindow, authority_checkpoint: CheckpointRef, operation_checkpoint: CheckpointDigest, @@ -296,6 +305,18 @@ impl<T> OperationReceipt<T> { issuance_window, "Original first-issue bounds; never restarted by a receipt." ); + copied!( + installation_transaction_id, + Option<TransactionId>, + installation_transaction, + "Original exactly-once logical installation transaction." + ); + copied!( + installed_at, + Option<UnixSeconds>, + installed_at, + "Actual original installation observation, separate from message creation." + ); /// Authority/evidence checkpoint originally captured by the operation. pub const fn authority_checkpoint(&self) -> &CheckpointRef { &self.authority_checkpoint @@ -396,8 +417,8 @@ impl<T> fmt::Debug for OperationReceipt<T> { } #[cfg(feature = "receipts")] -impl OperationReceipt<()> { - /// Projects a structurally validated T0/T1/S report. Only the concrete +impl OperationReceipt<ChatMessage> { + /// Projects a structurally validated retained operation report. Only the concrete /// adapter's actual authenticated coherent query qualifies the facts. This /// conversion performs no I/O, issuance, installation or publication. pub fn from_snapshot(snapshot: &radroots_storage::command::OperationSnapshot) -> Self { @@ -425,6 +446,11 @@ impl OperationReceipt<()> { claim_transaction: claim.map(|value| value.transaction_id()), intent_digest: claim.map(|value| *value.intent().digest()), capture_transaction: obtained.map(|value| value.transaction_id()), + publication_transaction: snapshot + .publications() + .iter() + .find(|value| Some(value.evidence()) == obtained) + .map(|value| value.transaction_id()), obtained_digest: obtained.map(|value| *value.digest()), observed_at: obtained.and_then(|value| value.observed_at_unix_ms()), }); @@ -432,9 +458,9 @@ impl OperationReceipt<()> { let unknown = artifacts .iter() .any(|value| value.issuance == Issuance::Unknown); - let obtained = artifacts - .iter() - .any(|value| value.issuance == Issuance::Obtained); + let install_pending = artifacts.iter().any(|value| { + value.issuance == Issuance::Obtained && value.publication_transaction.is_none() + }); let issuance = if unknown { Issuance::Unknown } else if artifacts @@ -454,9 +480,12 @@ impl OperationReceipt<()> { if unknown { recovery.push(RecoveryRequirement::ReconcileIssuance); } - if obtained { + if install_pending { recovery.push(RecoveryRequirement::InstallObtainedEvidence); } + if !snapshot.publications().is_empty() { + recovery.push(RecoveryRequirement::ResumeDelivery); + } if expired { recovery.push(RecoveryRequirement::ManualReconciliation); } @@ -469,19 +498,29 @@ impl OperationReceipt<()> { admission: Admission::Confirmed, admission_transaction: prepared.transaction_id(), plan_transaction: snapshot.bound_plan().map(|value| value.transaction_id()), + installation_transaction: snapshot.installation().map(|value| value.transaction_id()), + installed_at: snapshot.installation().map(|value| value.installed_at()), issuance_window: prepared.issuance_window(), authority_checkpoint: prepared.request().binding().input().checkpoint().clone(), operation_checkpoint: snapshot.checkpoint_digest(), conflicting_records: vec![].into_boxed_slice(), conflicting_sources: vec![].into_boxed_slice(), observed_at: snapshot.observed_at(), - business_installation: BusinessInstallation::NotInstalled, - business_result: None, + business_installation: if snapshot.installation().is_some() { + BusinessInstallation::Installed + } else { + BusinessInstallation::NotInstalled + }, + business_result: snapshot.installed_message().cloned(), message_id: snapshot.message_id(), record_ids: vec![].into_boxed_slice(), artifacts: artifacts.into_boxed_slice(), issuance, - delivery: Delivery::NotStarted, + delivery: if snapshot.publications().is_empty() { + Delivery::NotStarted + } else { + Delivery::Pending + }, delivery_observations: vec![].into_boxed_slice(), wait_termination: WaitTermination::Returned, recovery: recovery.into_boxed_slice(), diff --git a/crates/protocol/src/command.rs b/crates/protocol/src/command.rs @@ -18,8 +18,8 @@ use zeroize::Zeroizing; /// Maximum raw or canonical command bytes accepted by this closed boundary. pub const COMMAND_MAX_BYTES: usize = 65_536; -/// Maximum plain chat text in UTF-8 bytes, before JSON/encryption expansion. -pub const CHAT_MAX_BYTES: usize = 4096; +/// Shared plain chat text budget, before JSON/encryption expansion. +pub use radroots_domain::CHAT_MAX_BYTES; const JSON_MAX_DEPTH: usize = 16; /// Fixed input-free command data failures. diff --git a/crates/radroots/README.md b/crates/radroots/README.md @@ -86,6 +86,9 @@ The approved successor ownership and host contracts live in The common `OperationReceipt<T>` types are exported by this facade. Their owned observations preserve original identities and independent issuance, installation, delivery and caller-wait dimensions. The advanced native journal can supply a -coherent T0/T1/S snapshot for this projection. Ordinary inbox job orchestration, -T2 business installation/outbox and exact delivery remain required before the -complete native submission workflow is available. +coherent retained snapshot for this projection. Native T2 can install a bounded +logical chat result and retain independent recipient publication work; its +original installation time/transaction remain separate from later work and +unknown issuance. `domain::ChatMessage` is the owned private business view. +Ordinary inbox job orchestration and exact wrapping/delivery remain required +before the complete native submission workflow is available. diff --git a/crates/radroots/src/domain.rs b/crates/radroots/src/domain.rs @@ -4,6 +4,6 @@ //! participant, select an executor or prove durable command admission. pub use radroots_domain::{ - AmountError, Decimal, ExecutorId, IdentifierError, OperationId, OrderId, PositiveDecimal, - ThreadId, WorkspaceId, + AmountError, CHAT_MAX_BYTES, ChatMessage, ChatRelation, Decimal, ExecutorId, IdentifierError, + MessageError, MessageId, OperationId, OrderId, PositiveDecimal, ThreadId, WorkspaceId, }; diff --git a/crates/radroots/tests/support/receipt_installation.rs b/crates/radroots/tests/support/receipt_installation.rs @@ -0,0 +1,449 @@ +use super::issuance::{claim, obtain, prepare, selection}; +use super::*; +use radroots_execution::{ + BusinessInstallation, Delivery, Issuance, OperationReceipt, RecoveryRequirement, +}; +use radroots_storage::command::{ + ChatInstallation, ClaimAttemptOutcome, EvidenceCaptureOutcome, EvidenceReceipt, + InstallationOutcome, InstallationRequest, OperationSnapshot, SigningAttempt, +}; +#[path = "receipt_installation_refusal.rs"] +mod refusal; +#[path = "receipt_installation_reports.rs"] +mod reports; + +fn request_install( + binding: &WorkspaceBinding, + prepared: &PreparedOperation, + plan: [u8; 32], +) -> InstallationRequest { + InstallationRequest::new( + binding.clone(), + prepared.identity().operation_id(), + prepared.identity().command_digest(), + plan, + ) +} +fn installed(outcome: InstallationOutcome) -> ChatInstallation { + match outcome { + InstallationOutcome::Committed(value) => *value, + InstallationOutcome::Unknown(_) => panic!("fixture must observe confirmed T2"), + } +} +async fn capture( + store: &NativeWorkspace, + input: &AttachmentInput, + proof: &AttachmentProof, + attempt: &SigningAttempt, +) -> EvidenceReceipt { + let evidence = obtain(attempt, true).await; + match store + .capture_evidence(input.clone(), proof, &evidence) + .await + .unwrap() + { + EvidenceCaptureOutcome::Committed(value) => value, + EvidenceCaptureOutcome::Unknown(_) => panic!("fixture must confirm original capture"), + } +} +async fn snapshot( + store: &NativeWorkspace, + binding: &WorkspaceBinding, + prepared: &PreparedOperation, + proof: &AttachmentProof, +) -> OperationSnapshot { + store + .operation_snapshot( + binding.clone(), + prepared.identity().operation_id(), + Some(prepared.identity().command_digest()), + copy(proof), + ) + .await + .unwrap() + .unwrap() +} + +#[tokio::test] +async fn actual_peer_installation_and_late_self_capture_keep_one_original_message_and_distinct_work() + { + let fixture = Fixture::new(); + let (store, input, proof, binding) = fixture.create().await; + let (prepared, digest, recipients) = + prepare(&fixture, &store, &binding, &proof, 220, true).await; + assert!(matches!( + store + .install_transition_and_outbox( + request_install(&binding, &prepared, digest), + copy(&proof) + ) + .await, + Err(OperationError::InvalidEvidence) + )); + let peer = *recipients + .iter() + .find(|recipient| **recipient != key()) + .unwrap(); + let peer_claim = claim( + store + .claim_attempt(selection(&binding, &prepared, digest, peer), copy(&proof)) + .await + .unwrap(), + ); + let self_claim = claim( + store + .claim_attempt(selection(&binding, &prepared, digest, key()), copy(&proof)) + .await + .unwrap(), + ); + let peer_capture = capture(&store, &input, &proof, &peer_claim).await; + // Obtain within the original first-issue window, then delay only persistence. + let self_bytes = obtain(&self_claim, true).await; + let before = snapshot(&store, &binding, &prepared, &proof).await; + assert!(before.installed_message().is_none()); + fixture.clock.now.store(110, Ordering::SeqCst); + let first = installed( + store + .install_transition_and_outbox( + request_install(&binding, &prepared, digest), + copy(&proof), + ) + .await + .unwrap(), + ); + assert_eq!(first.basis(), peer_capture); + assert_eq!(first.installed_at().get(), 110); + let state = snapshot(&store, &binding, &prepared, &proof).await; + assert_eq!(state.installation(), Some(first)); + assert_eq!(state.publications().len(), 1); + assert_eq!(state.publications()[0].evidence(), peer_capture); + assert_ne!(before.checkpoint_digest(), state.checkpoint_digest()); + let view = OperationReceipt::from_snapshot(&state); + assert_eq!( + view.business_installation(), + BusinessInstallation::Installed + ); + assert_eq!(view.issuance(), Issuance::Unknown); + assert_eq!(view.delivery(), Delivery::Pending); + assert!(view.delivery_observations().is_empty()); + assert_eq!( + view.installation_transaction_id(), + Some(first.transaction_id()) + ); + let message = view.business_result().unwrap().clone(); + assert_eq!(message.id(), first.message_id()); + assert_eq!(message.created_at().get(), 100); + assert_eq!(message.text(), "\0".repeat(3500)); + assert_eq!(message.peer(), peer); + assert_eq!( + view.artifacts() + .iter() + .filter(|value| value.publication_transaction_id().is_some()) + .count(), + 1 + ); + store.close().await.unwrap(); + fixture.clock.now.store(151, Ordering::SeqCst); + let reopened = fixture.reopen(input.clone()).await; + let replay = installed( + reopened + .install_transition_and_outbox( + request_install(&binding, &prepared, digest), + copy(&proof), + ) + .await + .unwrap(), + ); + assert_eq!(replay, first); + assert_eq!( + snapshot(&reopened, &binding, &prepared, &proof) + .await + .installed_message(), + Some(&message) + ); + let captured = reopened + .capture_evidence(input, &proof, &self_bytes) + .await + .unwrap(); + assert!(matches!(captured, EvidenceCaptureOutcome::Committed(_))); + let late = snapshot(&reopened, &binding, &prepared, &proof).await; + let late = OperationReceipt::from_snapshot(&late); + assert_eq!( + late.business_installation(), + BusinessInstallation::Installed + ); + assert_eq!(late.issuance(), Issuance::Obtained); + assert!( + late.recovery() + .contains(&RecoveryRequirement::InstallObtainedEvidence) + ); + assert!( + late.recovery() + .contains(&RecoveryRequirement::ResumeDelivery) + ); + assert!(late.error().is_none()); + let extended = installed( + reopened + .install_transition_and_outbox( + request_install(&binding, &prepared, digest), + copy(&proof), + ) + .await + .unwrap(), + ); + assert_eq!(extended, first); + let complete = snapshot(&reopened, &binding, &prepared, &proof).await; + assert_eq!(complete.publications().len(), 2); + assert_eq!(complete.installed_message(), Some(&message)); + let peer_work = complete + .publications() + .iter() + .find(|value| value.evidence().recipient() == peer) + .unwrap(); + let self_work = complete + .publications() + .iter() + .find(|value| value.evidence().recipient() == key()) + .unwrap(); + assert_eq!(peer_work.transaction_id(), first.transaction_id()); + assert_ne!(self_work.transaction_id(), first.transaction_id()); + assert_eq!(self_work.queued_at().get(), 151); + let receipt = OperationReceipt::from_snapshot(&complete); + assert_eq!(receipt.installed_at(), Some(first.installed_at())); + assert_eq!(receipt.business_result(), Some(&message)); + assert_eq!(receipt.recovery(), &[RecoveryRequirement::ResumeDelivery]); + assert!(matches!( + reopened + .claim_attempt(selection(&binding, &prepared, digest, peer), copy(&proof)) + .await + .unwrap(), + ClaimAttemptOutcome::Retained(_) + )); + reopened.close().await.unwrap(); +} + +#[tokio::test] +async fn actual_work_insert_failure_rolls_back_the_message_but_keeps_original_claim_and_evidence() { + let fixture = Fixture::new(); + let (store, input, proof, binding) = fixture.create().await; + let (prepared, digest, recipients) = + prepare(&fixture, &store, &binding, &proof, 221, false).await; + let attempt = claim( + store + .claim_attempt( + selection(&binding, &prepared, digest, recipients[0]), + copy(&proof), + ) + .await + .unwrap(), + ); + let original = capture(&store, &input, &proof, &attempt).await; + let mut database = fixture.database().await; + sqlx::raw_sql("CREATE TRIGGER refuse_chat_work BEFORE INSERT ON chat_publication_work BEGIN SELECT RAISE(ABORT,'synthetic insert failure'); END").execute(&mut database).await.unwrap(); + assert!(matches!( + store + .install_transition_and_outbox( + request_install(&binding, &prepared, digest), + copy(&proof) + ) + .await, + Err(OperationError::StorageFailure) + )); + let counts: (i64, i64) = sqlx::query_as( + "SELECT (SELECT count(*) FROM chat_messages),(SELECT count(*) FROM chat_publication_work)", + ) + .fetch_one(&mut database) + .await + .unwrap(); + assert_eq!(counts, (0, 0)); + let state = snapshot(&store, &binding, &prepared, &proof).await; + assert!(state.installation().is_none()); + assert_eq!( + state + .recipients() + .iter() + .filter_map(|value| value.obtained()) + .collect::<Vec<_>>(), + vec![original] + ); + sqlx::query("DROP TRIGGER refuse_chat_work") + .execute(&mut database) + .await + .unwrap(); + database.close().await.unwrap(); + installed( + store + .install_transition_and_outbox( + request_install(&binding, &prepared, digest), + copy(&proof), + ) + .await + .unwrap(), + ); + assert!( + snapshot(&store, &binding, &prepared, &proof) + .await + .installed_message() + .is_some() + ); + store.close().await.unwrap(); +} + +#[tokio::test] +async fn actual_t2_commit_uncertainty_latches_and_reopen_reconciles_original_obtained_facts() { + let fixture = Fixture::new(); + let (store, input, proof, binding) = fixture.create().await; + let (prepared, digest, recipients) = + prepare(&fixture, &store, &binding, &proof, 222, false).await; + let attempt = claim( + store + .claim_attempt( + selection(&binding, &prepared, digest, recipients[0]), + copy(&proof), + ) + .await + .unwrap(), + ); + let original = capture(&store, &input, &proof, &attempt).await; + let mut database = fixture.database().await; + sqlx::raw_sql("CREATE TABLE chat_commit_fault (parent INTEGER REFERENCES workspace_grant(singleton) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER chat_commit_fault_trigger AFTER INSERT ON chat_publication_work BEGIN INSERT INTO chat_commit_fault VALUES(2); END").execute(&mut database).await.unwrap(); + database.close().await.unwrap(); + let InstallationOutcome::Unknown(anchor) = store + .install_transition_and_outbox(request_install(&binding, &prepared, digest), copy(&proof)) + .await + .unwrap() + else { + panic!("actual failed commit must remain anchored unknown") + }; + assert_eq!(anchor.operation_id(), prepared.identity().operation_id()); + assert_eq!( + anchor.command_digest(), + prepared.identity().command_digest() + ); + assert!(anchor.transaction_id().is_some()); + assert!(matches!( + store + .operation_snapshot( + binding.clone(), + prepared.identity().operation_id(), + None, + copy(&proof) + ) + .await, + Err(OperationError::RecoveryRequired) + )); + store.close().await.unwrap(); + let mut database = fixture.database().await; + sqlx::raw_sql("DROP TRIGGER chat_commit_fault_trigger; DROP TABLE chat_commit_fault") + .execute(&mut database) + .await + .unwrap(); + database.close().await.unwrap(); + let reopened = fixture.reopen(input).await; + let state = snapshot(&reopened, &binding, &prepared, &proof).await; + assert!(state.installation().is_none()); + assert_eq!( + state + .recipients() + .iter() + .filter_map(|value| value.obtained()) + .collect::<Vec<_>>(), + vec![original] + ); + installed( + reopened + .install_transition_and_outbox( + request_install(&binding, &prepared, digest), + copy(&proof), + ) + .await + .unwrap(), + ); + assert!( + snapshot(&reopened, &binding, &prepared, &proof) + .await + .installed_message() + .is_some() + ); + reopened.close().await.unwrap(); +} + +#[tokio::test] +async fn accepted_installation_survives_lost_observer_and_safe_close_drains_the_original_work() { + let fixture = Fixture::new(); + let (store, input, proof, binding) = fixture.create().await; + let (prepared, digest, recipients) = + prepare(&fixture, &store, &binding, &proof, 223, false).await; + let attempt = claim( + store + .claim_attempt( + selection(&binding, &prepared, digest, recipients[0]), + copy(&proof), + ) + .await + .unwrap(), + ); + capture(&store, &input, &proof, &attempt).await; + let (arrived, resume) = fixture.clock.arm(1); + let owner = store.clone(); + let selected = request_install(&binding, &prepared, digest); + let secret = copy(&proof); + let observer = + tokio::spawn(async move { owner.install_transition_and_outbox(selected, secret).await }); + reached(arrived).await; + observer.abort(); + assert!(observer.await.unwrap_err().is_cancelled()); + resume.send(()).unwrap(); + store.close().await.unwrap(); + let reopened = fixture.reopen(input).await; + let state = snapshot(&reopened, &binding, &prepared, &proof).await; + assert!(state.installed_message().is_some()); + assert_eq!(state.publications().len(), 1); + reopened.close().await.unwrap(); +} + +#[tokio::test] +async fn predecessor_schema_five_retains_actual_claims_and_obtained_bytes_without_automatic_upgrade() + { + let fixture = Fixture::new(); + let (store, input, proof, binding) = fixture.create().await; + let (prepared, digest, recipients) = + prepare(&fixture, &store, &binding, &proof, 224, false).await; + let attempt = claim( + store + .claim_attempt( + selection(&binding, &prepared, digest, recipients[0]), + copy(&proof), + ) + .await + .unwrap(), + ); + capture(&store, &input, &proof, &attempt).await; + store.close().await.unwrap(); + let mut database = fixture.database().await; + sqlx::raw_sql( + "DROP TABLE chat_publication_work; DROP TABLE chat_messages; PRAGMA user_version=5", + ) + .execute(&mut database) + .await + .unwrap(); + let count: i64 = sqlx::query_scalar("SELECT count(*) FROM sqlite_schema") + .fetch_one(&mut database) + .await + .unwrap(); + assert_eq!(count, 11); + database.close().await.unwrap(); + let path = fixture.path.join("execution_v1.sqlite"); + let original = std::fs::read(&path).unwrap(); + assert!(matches!( + NativeWorkspace::open_existing( + &fixture.path, + input, + fixture.crypto(), + fixture.clock.clone() + ) + .await, + Err(WorkspaceError::RecoveryRequired) + )); + assert_eq!(std::fs::read(path).unwrap(), original); +} diff --git a/crates/radroots/tests/support/receipt_installation_refusal.rs b/crates/radroots/tests/support/receipt_installation_refusal.rs @@ -0,0 +1,261 @@ +use super::*; +use sqlx::Row; + +async fn message_row(database: &mut SqliteConnection) -> Vec<Vec<u8>> { + let row = sqlx::query( + "SELECT message_id,thread_id,plan_digest,transaction_id,protected FROM chat_messages", + ) + .fetch_one(database) + .await + .unwrap(); + (0..5).map(|index| row.try_get(index).unwrap()).collect() +} +async fn work_row(database: &mut SqliteConnection) -> Vec<Vec<u8>> { + let row=sqlx::query("SELECT recipient,installation_transaction,capture_transaction,event_id,evidence_digest,transaction_id,protected FROM chat_publication_work").fetch_one(database).await.unwrap(); + (0..7).map(|index| row.try_get(index).unwrap()).collect() +} + +#[tokio::test] +async fn protected_message_and_work_projection_corruption_is_refused_without_repair() { + let fixture = Fixture::new(); + let (store, input, proof, binding) = fixture.create().await; + let (prepared, digest, recipients) = + prepare(&fixture, &store, &binding, &proof, 225, false).await; + let attempt = claim( + store + .claim_attempt( + selection(&binding, &prepared, digest, recipients[0]), + copy(&proof), + ) + .await + .unwrap(), + ); + capture(&store, &input, &proof, &attempt).await; + installed( + store + .install_transition_and_outbox( + request_install(&binding, &prepared, digest), + copy(&proof), + ) + .await + .unwrap(), + ); + let mut database = fixture.database().await; + sqlx::query("PRAGMA foreign_keys=OFF") + .execute(&mut database) + .await + .unwrap(); + sqlx::query("PRAGMA ignore_check_constraints=ON") + .execute(&mut database) + .await + .unwrap(); + let original = message_row(&mut database).await; + for statement in [ + "UPDATE chat_messages SET message_id=zeroblob(32)", + "UPDATE chat_messages SET thread_id=zeroblob(32)", + "UPDATE chat_messages SET plan_digest=zeroblob(32)", + "UPDATE chat_messages SET transaction_id=zeroblob(32)", + "UPDATE chat_messages SET protected=zeroblob(0)", + "UPDATE chat_messages SET protected=zeroblob(262145)", + "UPDATE chat_messages SET protected=zeroblob(262144)", + "UPDATE chat_messages SET message_id=zeroblob(31)", + "UPDATE chat_messages SET thread_id=zeroblob(31)", + "UPDATE chat_messages SET plan_digest=zeroblob(31)", + "UPDATE chat_messages SET transaction_id=zeroblob(31)", + ] { + sqlx::query(statement).execute(&mut database).await.unwrap(); + let corrupt = message_row(&mut database).await; + assert!( + store + .operation_snapshot( + binding.clone(), + prepared.identity().operation_id(), + None, + copy(&proof) + ) + .await + .is_err(), + "{statement}" + ); + assert_eq!(message_row(&mut database).await, corrupt); + sqlx::query("UPDATE chat_messages SET message_id=?,thread_id=?,plan_digest=?,transaction_id=?,protected=?") + .bind(&original[0]).bind(&original[1]).bind(&original[2]).bind(&original[3]).bind(&original[4]).execute(&mut database).await.unwrap(); + } + let original = work_row(&mut database).await; + for statement in [ + "UPDATE chat_publication_work SET recipient=zeroblob(32)", + "UPDATE chat_publication_work SET installation_transaction=zeroblob(32)", + "UPDATE chat_publication_work SET capture_transaction=zeroblob(32)", + "UPDATE chat_publication_work SET event_id=zeroblob(32)", + "UPDATE chat_publication_work SET evidence_digest=zeroblob(32)", + "UPDATE chat_publication_work SET transaction_id=zeroblob(32)", + "UPDATE chat_publication_work SET protected=zeroblob(0)", + "UPDATE chat_publication_work SET protected=zeroblob(262145)", + "UPDATE chat_publication_work SET protected=zeroblob(262144)", + "UPDATE chat_publication_work SET installation_transaction=zeroblob(31)", + "UPDATE chat_publication_work SET capture_transaction=zeroblob(31)", + "UPDATE chat_publication_work SET event_id=zeroblob(31)", + "UPDATE chat_publication_work SET evidence_digest=zeroblob(31)", + "UPDATE chat_publication_work SET transaction_id=zeroblob(31)", + ] { + sqlx::query(statement).execute(&mut database).await.unwrap(); + let corrupt = work_row(&mut database).await; + assert!( + store + .operation_snapshot( + binding.clone(), + prepared.identity().operation_id(), + None, + copy(&proof) + ) + .await + .is_err(), + "{statement}" + ); + assert_eq!(work_row(&mut database).await, corrupt); + sqlx::query("UPDATE chat_publication_work SET recipient=?,installation_transaction=?,capture_transaction=?,event_id=?,evidence_digest=?,transaction_id=?,protected=?") + .bind(&original[0]).bind(&original[1]).bind(&original[2]).bind(&original[3]).bind(&original[4]).bind(&original[5]).bind(&original[6]).execute(&mut database).await.unwrap(); + } + // A retained installation cannot silently recreate its missing atomic basis. + sqlx::query("DELETE FROM chat_publication_work") + .execute(&mut database) + .await + .unwrap(); + assert!(matches!( + store + .install_transition_and_outbox( + request_install(&binding, &prepared, digest), + copy(&proof) + ) + .await, + Err(OperationError::RecoveryRequired) + )); + let count: i64 = sqlx::query_scalar("SELECT count(*) FROM chat_publication_work") + .fetch_one(&mut database) + .await + .unwrap(); + assert_eq!(count, 0); + sqlx::query("INSERT INTO chat_publication_work(workspace,operation_id,recipient,installation_transaction,capture_transaction,event_id,evidence_digest,transaction_id,protected) VALUES(?,?,?,?,?,?,?,?,?)") + .bind(binding.input().workspace_id().as_bytes().as_slice()).bind(prepared.identity().operation_id().as_bytes().as_slice()).bind(&original[0]).bind(&original[1]).bind(&original[2]).bind(&original[3]).bind(&original[4]).bind(&original[5]).bind(&original[6]).execute(&mut database).await.unwrap(); + for number in 0..10u8 { + sqlx::query("INSERT INTO chat_publication_work SELECT workspace,operation_id,?1,installation_transaction,capture_transaction,event_id,evidence_digest,transaction_id,protected FROM chat_publication_work WHERE recipient=?2") + .bind([0x80+number;32].as_slice()).bind(&original[0]).execute(&mut database).await.unwrap(); + } + assert!( + store + .operation_snapshot( + binding.clone(), + prepared.identity().operation_id(), + None, + copy(&proof) + ) + .await + .is_err() + ); + assert!( + store + .install_transition_and_outbox( + request_install(&binding, &prepared, digest), + copy(&proof) + ) + .await + .is_err() + ); + let count: i64 = sqlx::query_scalar("SELECT count(*) FROM chat_publication_work") + .fetch_one(&mut database) + .await + .unwrap(); + assert_eq!(count, 11); + sqlx::query("DELETE FROM chat_publication_work WHERE recipient!=?") + .bind(&original[0]) + .execute(&mut database) + .await + .unwrap(); + database.close().await.unwrap(); + assert!( + snapshot(&store, &binding, &prepared, &proof) + .await + .installed_message() + .is_some() + ); + store.close().await.unwrap(); +} + +#[tokio::test] +async fn installation_checks_original_scope_current_grant_and_transaction_clock_without_releasing_evidence() + { + let fixture = Fixture::new(); + let (store, input, proof, binding) = fixture.create().await; + let (prepared, digest, recipients) = + prepare(&fixture, &store, &binding, &proof, 226, false).await; + let attempt = claim( + store + .claim_attempt( + selection(&binding, &prepared, digest, recipients[0]), + copy(&proof), + ) + .await + .unwrap(), + ); + let original = capture(&store, &input, &proof, &attempt).await; + let wrong = InstallationRequest::new( + binding.clone(), + prepared.identity().operation_id(), + CommandDigest::from_bytes([0x91; 32]), + digest, + ); + assert!(matches!( + store + .install_transition_and_outbox(wrong, copy(&proof)) + .await, + Err(OperationError::IdempotencyConflict) + )); + assert!(matches!( + store + .install_transition_and_outbox( + request_install(&binding, &prepared, [0x92; 32]), + copy(&proof) + ) + .await, + Err(OperationError::IdempotencyConflict) + )); + fixture.clock.now.store(110, Ordering::SeqCst); + let (arrived, resume) = fixture.clock.arm(1); + let owner = store.clone(); + let selected = request_install(&binding, &prepared, digest); + let secret = copy(&proof); + let observer = + tokio::spawn(async move { owner.install_transition_and_outbox(selected, secret).await }); + reached(arrived).await; + fixture.clock.now.store(105, Ordering::SeqCst); + resume.send(()).unwrap(); + assert!(matches!( + observer.await.unwrap(), + Err(OperationError::Workspace(WorkspaceError::Unavailable)) + )); + let state = snapshot(&store, &binding, &prepared, &proof).await; + assert!(state.installation().is_none()); + assert_eq!( + state + .recipients() + .iter() + .filter_map(|value| value.obtained()) + .collect::<Vec<_>>(), + vec![original] + ); + store.revoke(input, &proof).await.unwrap(); + assert!( + store + .install_transition_and_outbox( + request_install(&binding, &prepared, digest), + copy(&proof) + ) + .await + .is_err() + ); + let mut database = fixture.database().await; + let counts:(i64,i64,i64)=sqlx::query_as("SELECT (SELECT count(*) FROM chat_messages),(SELECT count(*) FROM chat_publication_work),(SELECT count(*) FROM seal_evidence)").fetch_one(&mut database).await.unwrap(); + assert_eq!(counts, (0, 0, 1)); + database.close().await.unwrap(); + store.close().await.unwrap(); +} diff --git a/crates/radroots/tests/support/receipt_installation_reports.rs b/crates/radroots/tests/support/receipt_installation_reports.rs @@ -0,0 +1,211 @@ +//! Structural report validation is separate from the actual native T2 tests. +use super::*; +use radroots_domain::TransactionId; +use radroots_storage::command::PublicationWork; + +fn restamp_plan(source: &BoundSigningPlan, transaction: TransactionId) -> BoundSigningPlan { + let original = source.prepared(); + let request = AdmissionRequest::from_protected_wire( + original.request().binding().clone(), + original.request().command().protected_wire(), + ) + .unwrap(); + let prepared = PreparedOperation::from_committed_store( + request, + original.transaction_id(), + original.revision(), + original.issuance_window(), + ) + .unwrap(); + let plan = ChatSigningPlan::parse(prepared.request().command(), source.plan().protected_wire()) + .unwrap(); + BoundSigningPlan::from_committed_store(prepared, plan, transaction, source.bound_at()).unwrap() +} + +#[tokio::test] +async fn structural_installation_reports_bind_original_plan_capture_and_complete_sorted_work_scope() +{ + let fixture = Fixture::new(); + let (store, input, proof, binding) = fixture.create().await; + let (prepared, digest, recipients) = + prepare(&fixture, &store, &binding, &proof, 227, false).await; + let mut captures = Vec::new(); + for recipient in recipients { + let attempt = claim( + store + .claim_attempt( + selection(&binding, &prepared, digest, recipient), + copy(&proof), + ) + .await + .unwrap(), + ); + captures.push(capture(&store, &input, &proof, &attempt).await); + } + let state = snapshot(&store, &binding, &prepared, &proof).await; + let bound = state.bound_plan().unwrap(); + let transaction = TransactionId::from_bytes([0xa1; 32]); + let time = UnixSeconds::new(110).unwrap(); + let installation = + ChatInstallation::from_committed_store(bound, captures[0], transaction, time).unwrap(); + let first = + PublicationWork::from_committed_store(bound, installation, captures[0], transaction, time) + .unwrap(); + let second = + PublicationWork::from_committed_store(bound, installation, captures[1], transaction, time) + .unwrap(); + assert!(matches!( + ChatInstallation::from_committed_store( + bound, + captures[0], + transaction, + UnixSeconds::new(99).unwrap() + ), + Err(OperationError::RecoveryRequired) + )); + assert!(matches!( + PublicationWork::from_committed_store( + bound, + installation, + captures[0], + transaction, + UnixSeconds::new(109).unwrap() + ), + Err(OperationError::RecoveryRequired) + )); + for (installed, work) in [ + (None, vec![first]), + (Some(installation), vec![]), + (Some(installation), vec![first, second, first]), + (Some(installation), vec![second, first]), + (Some(installation), vec![first, first]), + (Some(installation), vec![second]), + ] { + assert!(matches!( + snapshot(&store, &binding, &prepared, &proof) + .await + .with_installation(installed, work), + Err(OperationError::RecoveryRequired) + )); + } + let reported = snapshot(&store, &binding, &prepared, &proof) + .await + .with_installation(Some(installation), vec![first, second]) + .unwrap(); + assert_eq!(reported.installation(), Some(installation)); + assert_eq!( + reported.installed_message().unwrap().text(), + prepared.request().command().text() + ); + // No actual installation was performed by constructing any of these values. + assert!( + snapshot(&store, &binding, &prepared, &proof) + .await + .installation() + .is_none() + ); + + let restamped = restamp_plan(bound, TransactionId::from_bytes([0xa2; 32])); + let changed = + ChatInstallation::from_committed_store(&restamped, captures[0], transaction, time).unwrap(); + let changed_work = + PublicationWork::from_committed_store(&restamped, changed, captures[0], transaction, time) + .unwrap(); + assert!(matches!( + snapshot(&store, &binding, &prepared, &proof) + .await + .with_installation(Some(changed), vec![changed_work]), + Err(OperationError::RecoveryRequired) + )); + assert!(matches!( + PublicationWork::from_committed_store( + &restamped, + installation, + captures[0], + transaction, + time + ), + Err(OperationError::RecoveryRequired) + )); + let wrong_installation = ChatInstallation::from_committed_store( + bound, + captures[0], + TransactionId::from_bytes([0xa3; 32]), + time, + ) + .unwrap(); + assert!(matches!( + snapshot(&store, &binding, &prepared, &proof) + .await + .with_installation(Some(wrong_installation), vec![first]), + Err(OperationError::RecoveryRequired) + )); + + let (foreign, foreign_digest, foreign_recipients) = + prepare(&fixture, &store, &binding, &proof, 228, false).await; + let attempt = claim( + store + .claim_attempt( + selection(&binding, &foreign, foreign_digest, foreign_recipients[0]), + copy(&proof), + ) + .await + .unwrap(), + ); + let foreign_capture = capture(&store, &input, &proof, &attempt).await; + let foreign_state = snapshot(&store, &binding, &foreign, &proof).await; + let foreign_installation = ChatInstallation::from_committed_store( + foreign_state.bound_plan().unwrap(), + foreign_capture, + transaction, + time, + ) + .unwrap(); + assert!(matches!( + ChatInstallation::from_committed_store(bound, foreign_capture, transaction, time), + Err(OperationError::RecoveryRequired) + )); + assert!(matches!( + PublicationWork::from_committed_store( + bound, + installation, + foreign_capture, + transaction, + time + ), + Err(OperationError::RecoveryRequired) + )); + assert!(matches!( + PublicationWork::from_committed_store( + bound, + foreign_installation, + captures[0], + transaction, + time + ), + Err(OperationError::RecoveryRequired) + )); + assert!(matches!( + snapshot(&store, &binding, &prepared, &proof) + .await + .with_installation(Some(foreign_installation), vec![first]), + Err(OperationError::RecoveryRequired) + )); + + let unbound = committed( + store + .admit_command_and_hold( + request(&binding, 229, "original unbound", "trace"), + copy(&proof), + ) + .await + .unwrap(), + ); + assert!(matches!( + snapshot(&store, &binding, &unbound, &proof) + .await + .with_installation(Some(installation), vec![first]), + Err(OperationError::RecoveryRequired) + )); + store.close().await.unwrap(); +} diff --git a/crates/radroots/tests/support/receipt_plans.rs b/crates/radroots/tests/support/receipt_plans.rs @@ -1,4 +1,6 @@ //! Native storage tests use real synthetic NIP-44 data, never host custody. +#[path = "receipt_installation.rs"] +mod installation; #[path = "receipt_issuance.rs"] mod issuance; #[path = "receipt_snapshots.rs"] diff --git a/crates/storage/README.md b/crates/storage/README.md @@ -313,4 +313,13 @@ obtained artifact, then compares both presence and absence together before returning. Per-recipient reports share one owned plan; bounded boxed collections discard spare caller capacity. The scoped progress digest excludes query time and diagnostic correlation. Structural snapshot construction supplies no -backend qualification, dispatch permission or installed business result. +proof of backend qualification, dispatch permission or business installation. + +`install_transition_and_outbox` requires original operation/command/plan +selectors. Concrete owners atomically install one logical chat result and retain +separate publication work for actual obtained recipient artifacts. Original +installation and later work transactions remain distinct. A coherent snapshot +may carry the owned bounded message reconstructed from the original protected +command and plan, with validated installation/basis/work reports. Structural +constructors supply no authentication or persistence guarantee. Uncertain T2 +completion never releases claims or obtained evidence. diff --git a/crates/storage/src/command.rs b/crates/storage/src/command.rs @@ -11,8 +11,12 @@ pub use radroots_protocol::chat::ChatSigningPlan; use radroots_protocol::command::CapturedChatCommand; use radroots_transport::BoxFuture; +mod installation; mod issuance; mod snapshot; +pub use installation::{ + ChatInstallation, InstallationOutcome, InstallationRequest, PublicationWork, +}; pub use issuance::{ CapturedSeal, ClaimAttemptOutcome, ClaimAttemptRequest, EvidenceCaptureOutcome, EvidenceReceipt, SealEvidence, SigningAttempt, @@ -256,6 +260,14 @@ impl fmt::Debug for AdmissionOutcome { /// Typed transaction port, implemented by actual shared storage adapters. /// Applications do not implement a second orchestration or stock engine here. pub trait OperationJournal: WorkspaceStore { + /// Atomically installs the original logical chat result and retains distinct + /// publication work for actual obtained recipients. No signing or transport + /// occurs here; unknown completion retains all original claims and evidence. + fn install_transition_and_outbox( + &self, + request: InstallationRequest, + proof: AttachmentProof, + ) -> BoxFuture<'_, Result<InstallationOutcome, OperationError>>; /// Queries one coherent original preparation/plan/per-recipient evidence /// graph under current access. Reported absence grants no safe reissue. fn operation_snapshot( diff --git a/crates/storage/src/command/installation.rs b/crates/storage/src/command/installation.rs @@ -0,0 +1,254 @@ +//! Structural T2 reports. Only an actual qualified transaction establishes +//! installation; constructing these values never grants authority or delivery. +use super::{BoundSigningPlan, CommitAttempt, EvidenceReceipt, OperationError}; +use crate::workspace::WorkspaceBinding; +use core::fmt; +use radroots_domain::{ + CommandDigest, EnvelopeEventId, MessageId, OperationId, TransactionId, UnixSeconds, WorkspaceId, +}; +use std::boxed::Box; + +/// Original-operation selectors for native installation. The concrete owner +/// independently loads and authenticates the complete retained graph. +pub struct InstallationRequest { + binding: WorkspaceBinding, + operation: OperationId, + command: CommandDigest, + plan: [u8; 32], +} +impl InstallationRequest { + /// Captures exact selectors, without authorizing installation. + pub const fn new( + binding: WorkspaceBinding, + operation: OperationId, + command: CommandDigest, + plan: [u8; 32], + ) -> Self { + Self { + binding, + operation, + command, + plan, + } + } + /// Original selected workspace scope. + pub const fn binding(&self) -> &WorkspaceBinding { + &self.binding + } + /// Original full operation identity. + pub const fn operation_id(&self) -> OperationId { + self.operation + } + /// Original semantic command commitment. + pub const fn command_digest(&self) -> CommandDigest { + self.command + } + /// Exact retained complete signing plan commitment. + pub const fn plan_digest(&self) -> &[u8; 32] { + &self.plan + } + /// Stable original anchor for an unknown transaction result. + pub fn identity(&self) -> CommitAttempt { + CommitAttempt::new( + self.binding.input().workspace_id(), + self.operation, + self.command, + None, + ) + } +} +impl fmt::Debug for InstallationRequest { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("InstallationRequest(<redacted>)") + } +} + +/// Original confirmed logical installation and its actual obtained basis. +/// The body remains in the protected original command; no ciphertext or text is +/// copied into this fixed reference. This data is not backend qualification. +#[derive(Clone, Copy, Eq, PartialEq)] +pub struct ChatInstallation { + plan: [u8; 32], + message: MessageId, + preparation_transaction: TransactionId, + plan_transaction: TransactionId, + transaction: TransactionId, + installed_at: UnixSeconds, + basis: EvidenceReceipt, +} +impl ChatInstallation { + /// Checks a structural store report against the original complete plan. + /// The backend must also authenticate the referenced actual obtained bytes + /// and compare every retained fact in the installation transaction. + pub fn from_committed_store( + bound: &BoundSigningPlan, + basis: EvidenceReceipt, + transaction: TransactionId, + installed_at: UnixSeconds, + ) -> Result<Self, OperationError> { + check_evidence(bound, basis)?; + if installed_at < bound.bound_at() { + return Err(OperationError::RecoveryRequired); + } + Ok(Self { + plan: *bound.plan().digest(), + message: bound.plan().message_id(), + preparation_transaction: bound.prepared().transaction_id(), + plan_transaction: bound.transaction_id(), + transaction, + installed_at, + basis, + }) + } + /// Original workspace. + pub const fn workspace_id(&self) -> WorkspaceId { + self.basis.workspace_id() + } + /// Original operation. + pub const fn operation_id(&self) -> OperationId { + self.basis.operation_id() + } + /// Original semantic command commitment. + pub const fn command_digest(&self) -> CommandDigest { + self.basis.command_digest() + } + /// Exact retained plan commitment. + pub const fn plan_digest(&self) -> &[u8; 32] { + &self.plan + } + /// Original logical message, distinct from seal/envelope IDs. + pub const fn message_id(&self) -> MessageId { + self.message + } + /// Original T0 transaction. + pub const fn preparation_transaction_id(&self) -> TransactionId { + self.preparation_transaction + } + /// Original T1 transaction. + pub const fn plan_transaction_id(&self) -> TransactionId { + self.plan_transaction + } + /// Original exactly-once logical installation transaction. + pub const fn transaction_id(&self) -> TransactionId { + self.transaction + } + /// Actual installation clock observation, not the original message time. + pub const fn installed_at(&self) -> UnixSeconds { + self.installed_at + } + /// Exact original capture that established the installation basis. + pub const fn basis(&self) -> EvidenceReceipt { + self.basis + } +} +impl fmt::Debug for ChatInstallation { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("ChatInstallation(<redacted>)") + } +} + +/// Per-recipient pending wrapping/publication work, referencing an actual +/// retained exact seal and the original plan's selected routes. This is neither +/// a signed outer envelope nor a transport acknowledgement. +#[derive(Clone, Copy, Eq, PartialEq)] +pub struct PublicationWork { + installation: TransactionId, + plan: [u8; 32], + evidence: EvidenceReceipt, + transaction: TransactionId, + queued_at: UnixSeconds, +} +impl PublicationWork { + /// Checks scoped data only. An actual adapter must authenticate this work + /// and its evidence and atomically link it to the retained installation. + pub fn from_committed_store( + bound: &BoundSigningPlan, + installation: ChatInstallation, + evidence: EvidenceReceipt, + transaction: TransactionId, + queued_at: UnixSeconds, + ) -> Result<Self, OperationError> { + check_evidence(bound, evidence)?; + if installation.workspace_id() != evidence.workspace_id() + || installation.operation_id() != evidence.operation_id() + || installation.command_digest() != evidence.command_digest() + || installation.plan_digest() != bound.plan().digest() + || installation.message_id() != bound.plan().message_id() + || installation.preparation_transaction_id() != bound.prepared().transaction_id() + || installation.plan_transaction_id() != bound.transaction_id() + || queued_at < installation.installed_at() + { + return Err(OperationError::RecoveryRequired); + } + Ok(Self { + installation: installation.transaction_id(), + plan: *bound.plan().digest(), + evidence, + transaction, + queued_at, + }) + } + /// Original installation this work extends without replacing it. + pub const fn installation_transaction_id(&self) -> TransactionId { + self.installation + } + /// Original immutable plan holding the exact selected routes. + pub const fn plan_digest(&self) -> &[u8; 32] { + &self.plan + } + /// Exact recipient, seal and protected capture reference. + pub const fn evidence(&self) -> EvidenceReceipt { + self.evidence + } + /// Confirmed transaction that first retained this recipient's work. + pub const fn transaction_id(&self) -> TransactionId { + self.transaction + } + /// Actual original work-retention observation. + pub const fn queued_at(&self) -> UnixSeconds { + self.queued_at + } +} +impl fmt::Debug for PublicationWork { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("PublicationWork(<redacted>)") + } +} + +/// Actual T2 completion or an original retained uncertainty anchor. Applications +/// must reconcile unknown completion before any further effects. +pub enum InstallationOutcome { + /// The logical installation and eligible publication links are confirmed. + Committed(Box<ChatInstallation>), + /// Completion is unknown; retained claims/evidence are never discarded. + Unknown(CommitAttempt), +} +impl fmt::Debug for InstallationOutcome { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(match self { + Self::Committed(_) => "InstallationOutcome::Committed(<redacted>)", + Self::Unknown(_) => "InstallationOutcome::Unknown(<retained>)", + }) + } +} + +fn check_evidence( + bound: &BoundSigningPlan, + evidence: EvidenceReceipt, +) -> Result<(), OperationError> { + let identity = bound.prepared().identity(); + let seal = bound + .plan() + .seals() + .iter() + .find(|seal| seal.recipient() == evidence.recipient()) + .ok_or(OperationError::RecoveryRequired)?; + if evidence.workspace_id() != identity.workspace_id() + || evidence.operation_id() != identity.operation_id() + || evidence.command_digest() != identity.command_digest() + || evidence.event_id() != EnvelopeEventId::from_bytes(seal.event_id()) + { + return Err(OperationError::RecoveryRequired); + } + Ok(()) +} diff --git a/crates/storage/src/command/snapshot.rs b/crates/storage/src/command/snapshot.rs @@ -1,10 +1,13 @@ //! Coherent retained graph reports. Values are not persistence qualification or //! dispatch permission; concrete owners must authenticate and compare all facts. -use super::{BoundSigningPlan, EvidenceReceipt, OperationError, PreparedOperation, SigningAttempt}; +use super::{ + BoundSigningPlan, ChatInstallation, EvidenceReceipt, OperationError, PreparedOperation, + PublicationWork, SigningAttempt, +}; use core::fmt; use radroots_domain::{ - CommandDigest, EnvelopeEventId, MessageId, OperationId, PublicKey, SigningAttemptId, - TransactionId, UnixSeconds, WorkspaceId, + ChatMessage, ChatRelation, CommandDigest, EnvelopeEventId, MessageId, OperationId, PublicKey, + SigningAttemptId, TransactionId, UnixSeconds, WorkspaceId, }; use radroots_protocol::intent::IntentBinding; use sha2::{Digest, Sha256}; @@ -172,6 +175,9 @@ pub struct OperationSnapshot { retained: RetainedOperation, recipients: Box<[RecipientSnapshot]>, observed_at: UnixSeconds, + installation: Option<ChatInstallation>, + message: Option<ChatMessage>, + publications: Box<[PublicationWork]>, } impl OperationSnapshot { /// Stable scoped progress identity over original transaction, command, @@ -181,7 +187,7 @@ impl OperationSnapshot { let prepared = self.prepared(); let identity = prepared.identity(); let mut digest = Sha256::new(); - digest.update(b"radroots.operation.snapshot.v1\0"); + digest.update(b"radroots.operation.snapshot.v2\0"); digest.update(identity.workspace_id().as_bytes()); digest.update(identity.operation_id().as_bytes()); digest.update(identity.command_digest().as_bytes()); @@ -242,6 +248,20 @@ impl OperationSnapshot { None => digest.update([0]), } } + match self.installation() { + Some(installed) => { + digest.update([1]); + digest.update(installed.transaction_id().as_bytes()); + digest.update(installed.installed_at().get().to_be_bytes()); + digest.update(installed.basis().transaction_id().as_bytes()); + } + None => digest.update([0]), + } + for work in self.publications() { + digest.update(work.evidence().recipient().as_bytes()); + digest.update(work.transaction_id().as_bytes()); + digest.update(work.queued_at().get().to_be_bytes()); + } radroots_domain::CheckpointDigest::from_bytes(digest.finalize().into()) } /// Checks the exact complete recipient set, original claim scope and obtained @@ -319,8 +339,106 @@ impl OperationSnapshot { retained, recipients: recipients.into_boxed_slice(), observed_at, + installation: None, + message: None, + publications: Vec::new().into_boxed_slice(), }) } + /// Adds structurally checked T2 reports to this data snapshot. The concrete + /// owner must authenticate and atomically compare these records with the + /// complete original graph. This transformation performs no installation. + pub fn with_installation( + mut self, + installation: Option<ChatInstallation>, + publications: Vec<PublicationWork>, + ) -> Result<Self, OperationError> { + let Some(installed) = installation else { + if !publications.is_empty() { + return Err(OperationError::RecoveryRequired); + } + self.installation = None; + self.message = None; + self.publications = Vec::new().into_boxed_slice(); + return Ok(self); + }; + let bound = self.bound_plan().ok_or(OperationError::RecoveryRequired)?; + let checked = ChatInstallation::from_committed_store( + bound, + installed.basis(), + installed.transaction_id(), + installed.installed_at(), + )?; + if checked != installed + || !self + .recipients() + .iter() + .any(|recipient| recipient.obtained() == Some(installed.basis())) + || publications.is_empty() + || publications.len() > self.recipients().len() + { + return Err(OperationError::RecoveryRequired); + } + let mut previous = None; + let mut basis_linked = false; + for work in &publications { + let recipient = work.evidence().recipient(); + if previous.is_some_and(|key| key >= recipient) + || PublicationWork::from_committed_store( + bound, + installed, + work.evidence(), + work.transaction_id(), + work.queued_at(), + )? != *work + || !self + .recipients() + .iter() + .any(|value| value.obtained() == Some(work.evidence())) + { + return Err(OperationError::RecoveryRequired); + } + previous = Some(recipient); + basis_linked |= work.evidence() == installed.basis(); + } + if !basis_linked { + return Err(OperationError::RecoveryRequired); + } + let command = bound.prepared().request().command(); + let relation = match command.parent() { + Some(parent) => ChatRelation::Reply { parent }, + None => ChatRelation::Inquire { + source: command.source().map(|source| source.event_id()), + }, + }; + let message = ChatMessage::new( + installed.message_id(), + command.thread_id(), + command.context().actor().participant(), + command.peer(), + bound.plan().created_at(), + relation, + command.text(), + ) + .map_err(|_| OperationError::RecoveryRequired)?; + self.installation = Some(installed); + self.message = Some(message); + self.publications = publications.into_boxed_slice(); + Ok(self) + } + /// Actual retained installation report, when supplied by the concrete owner. + pub const fn installation(&self) -> Option<ChatInstallation> { + self.installation + } + /// Owned bounded logical business result, reconstructed from the original + /// protected command and complete plan, never a caller's replacement body. + pub fn installed_message(&self) -> Option<&ChatMessage> { + self.message.as_ref() + } + /// Independent retained recipient publication work. No outer envelope or + /// relay observation is inferred from these references. + pub fn publications(&self) -> &[PublicationWork] { + &self.publications + } /// Original preparation, without a fabricated installed business result. pub fn prepared(&self) -> &PreparedOperation { self.retained.prepared() diff --git a/crates/storage_sqlite/README.md b/crates/storage_sqlite/README.md @@ -214,14 +214,14 @@ under `BEGIN IMMEDIATE`, with provider work outside that transaction. A fixed first-issue window never restarts on replay. Already-bound bytes remain queryable and replayable after expiry under current access; this grants no new issuance. -The current workspace schema is version 5. Older namespaces are preserved and +The current workspace schema is version 6. Older namespaces are preserved and refused for explicit cutover, without an automatic state rewrite. Complete plans use a protected manifest and at most twenty-four 16 KiB protected data parts, with context binding the full workspace/operation/plan identity and part index. Existing secret-material/envelope bounds remain intact. Queued and in-flight request reservations share a 4 MiB byte cap alongside the 32-slot queue limit. Known rollback, confirmed commit and unknown completion remain distinct. -This component performs no signer dispatch, business installation or delivery. +Plan binding performs no signer dispatch, business installation or delivery. Native first claims retain a full-width attempt identity and exact command, @@ -244,4 +244,23 @@ lock. It rechecks current access, all retained and absent facts and whole-graph counts under one `BEGIN IMMEDIATE`, rejecting extra or orphan rows. It performs no signing or graph mutation and confirms rollback before returning. Window expiry does not erase retained facts. The existing owner drains accepted reads -after a lost observer and closes explicitly. Workspace schema 5 stays unchanged. +after a lost observer and closes explicitly. Schema 6 has sixteen exact objects +and preserves/refuses predecessors without automatic rewriting. + +Native T2 uses that same connection owner. It authenticates the complete original +graph, protects fixed installation/work records before the transaction, then +rechecks current access and every retained/absent fact under `BEGIN IMMEDIATE`. +It atomically retains one `chat_messages` entry and distinct recipient work in +`chat_publication_work`, referencing exact retained seals and original routes. +The original private body stays protected in T0 instead of being duplicated. +Later recipient capture may append missing work without replacing the original +message installation. Already obtained evidence may install after the original +first-issue window, under current access; no new signature is requested. + +Each T2 protected plaintext is at most 16 KiB. One message plus at most two work +records cap protected materialization below 1 MiB; excess sentinels reject more +than one message or two work rows without unbounded scans. Exact replay preserves +original ciphertext and transaction identities. Known rollback, confirmed commit +and anchored unknown completion remain distinct, and accepted work survives +observer loss. Outer wrapping, transport observations and ordinary owned inbox +orchestration remain separate requirements. diff --git a/crates/storage_sqlite/src/workspace/crypto.rs b/crates/storage_sqlite/src/workspace/crypto.rs @@ -24,6 +24,7 @@ pub(super) struct IssuanceScope { pub(super) enum IssuancePayload { Attempt, Evidence, + Publication, } /// Explicit host-owned encryption material. This is a data-encryption key, @@ -36,6 +37,34 @@ pub struct WorkspaceCrypto { key: SecretMaterial, } impl WorkspaceCrypto { + pub(super) async fn seal_message( + &self, + workspace: WorkspaceId, + operation: OperationId, + plan: &[u8; 32], + plaintext: &[u8], + ) -> Result<Vec<u8>, WorkspaceError> { + self.seal_bound( + message_context(workspace, operation, plan)?, + PRIVATE_PAYLOAD_MAX_BYTES, + plaintext, + ) + .await + } + pub(super) async fn open_message( + &self, + workspace: WorkspaceId, + operation: OperationId, + plan: &[u8; 32], + protected: &[u8], + ) -> Result<SecretMaterial, WorkspaceError> { + self.open_bound( + message_context(workspace, operation, plan)?, + PRIVATE_PAYLOAD_MAX_BYTES, + protected, + ) + .await + } /// Binds protected host input to one exact persistent provider/ref. Actual /// provider access and context authentication still execute at seal/open. pub fn new( @@ -305,6 +334,11 @@ fn issuance_context( "radroots.workspace.seal_evidence", "radroots.workspace.seal_evidence.v1", ), + IssuancePayload::Publication => ( + 2u8, + "radroots.workspace.chat_publication_work", + "radroots.workspace.chat_publication_work.v1", + ), }; let mut hash = Sha256::new(); hash.update(b"radroots.workspace.issuance.context.v1\0"); @@ -322,3 +356,25 @@ fn issuance_context( PayloadSchemaId::parse(schema).map_err(|_| WorkspaceError::InvalidInput)?, )) } + +fn message_context( + workspace: WorkspaceId, + operation: OperationId, + plan: &[u8; 32], +) -> Result<EnvelopeContext, WorkspaceError> { + use sha2::{Digest, Sha256}; + let mut hash = Sha256::new(); + hash.update(b"radroots.workspace.chat_message.context.v1\0"); + hash.update(workspace.as_bytes()); + hash.update(operation.as_bytes()); + hash.update(plan); + let subject = radroots_domain::CommandDigest::from_bytes(hash.finalize().into()); + Ok(EnvelopeContext::new( + EnvelopePurpose::parse("radroots.workspace.chat_message") + .map_err(|_| WorkspaceError::InvalidInput)?, + EnvelopeSubject::parse("workspace_chat_message", subject.to_string()) + .map_err(|_| WorkspaceError::InvalidInput)?, + PayloadSchemaId::parse("radroots.workspace.chat_message.v1") + .map_err(|_| WorkspaceError::InvalidInput)?, + )) +} diff --git a/crates/storage_sqlite/src/workspace/installation.rs b/crates/storage_sqlite/src/workspace/installation.rs @@ -0,0 +1,499 @@ +//! Protected logical chat installation and original recipient publication links. +//! Bodies remain in the authenticated captured command; no private text is +//! duplicated or reserialized into these fixed bounded transaction records. +use super::{ + AttachmentInput, AttachmentProof, + admission::{Journal, recheck}, + crypto::{IssuancePayload, IssuanceScope, WorkspaceCrypto}, + snapshot::RetainedGraph, +}; +use core::num::NonZeroU64; +use radroots_domain::{ + CommandDigest, EnvelopeEventId, MessageId, OperationId, PublicKey, SigningAttemptId, ThreadId, + TransactionId, UnixSeconds, WorkspaceId, +}; +use radroots_storage::command::{ + BoundSigningPlan, ChatInstallation, CommitAttempt, EvidenceReceipt, InstallationOutcome, + InstallationRequest, OperationError, PublicationWork, +}; +use serde::{Deserialize, Serialize}; +use sqlx::{Connection, Row, SqliteConnection}; +use zeroize::Zeroizing; + +pub(super) const TABLE: &str = "CREATE TABLE chat_messages (workspace BLOB NOT NULL, message_id BLOB NOT NULL CHECK(length(message_id)=32), operation_id BLOB NOT NULL, thread_id BLOB NOT NULL CHECK(length(thread_id)=32), plan_digest BLOB NOT NULL CHECK(length(plan_digest)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), PRIMARY KEY(workspace,message_id), FOREIGN KEY(workspace,operation_id) REFERENCES signing_plans(workspace,operation_id)) STRICT, WITHOUT ROWID"; +pub(super) const OP_INDEX: &str = + "CREATE UNIQUE INDEX chat_message_operation_identity ON chat_messages(workspace,operation_id)"; +pub(super) const TX_INDEX: &str = "CREATE UNIQUE INDEX chat_message_transaction_identity ON chat_messages(workspace,transaction_id)"; +pub(super) const THREAD_INDEX: &str = + "CREATE INDEX chat_message_thread ON chat_messages(workspace,thread_id,message_id)"; +pub(super) const WORK: &str = "CREATE TABLE chat_publication_work (workspace BLOB NOT NULL, operation_id BLOB NOT NULL, recipient BLOB NOT NULL CHECK(length(recipient)=32), installation_transaction BLOB NOT NULL CHECK(length(installation_transaction)=32), capture_transaction BLOB NOT NULL CHECK(length(capture_transaction)=32), event_id BLOB NOT NULL CHECK(length(event_id)=32), evidence_digest BLOB NOT NULL CHECK(length(evidence_digest)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), PRIMARY KEY(workspace,operation_id,recipient), FOREIGN KEY(workspace,operation_id) REFERENCES chat_messages(workspace,operation_id), FOREIGN KEY(workspace,operation_id,recipient) REFERENCES seal_evidence(workspace,operation_id,recipient)) STRICT, WITHOUT ROWID"; + +#[derive(Serialize, Deserialize, Eq, PartialEq)] +#[serde(deny_unknown_fields)] +struct EvidenceRef { + workspace: WorkspaceId, + operation: OperationId, + command: CommandDigest, + recipient: PublicKey, + attempt: SigningAttemptId, + event: EnvelopeEventId, + digest: [u8; 32], + transaction: TransactionId, + observed_at: Option<u64>, +} +impl EvidenceRef { + fn new(value: EvidenceReceipt) -> Self { + Self { + workspace: value.workspace_id(), + operation: value.operation_id(), + command: value.command_digest(), + recipient: value.recipient(), + attempt: value.attempt_id(), + event: value.event_id(), + digest: *value.digest(), + transaction: value.transaction_id(), + observed_at: value.observed_at_unix_ms().map(NonZeroU64::get), + } + } +} +#[derive(Serialize, Deserialize, Eq, PartialEq)] +#[serde(deny_unknown_fields)] +struct MessageRecord { + version: u8, + plan_digest: [u8; 32], + message: MessageId, + thread: ThreadId, + preparation_transaction: TransactionId, + plan_transaction: TransactionId, + transaction: TransactionId, + installed_at: UnixSeconds, + basis: EvidenceRef, +} +impl MessageRecord { + fn new(bound: &BoundSigningPlan, value: ChatInstallation) -> Self { + Self { + version: 1, + plan_digest: *value.plan_digest(), + message: value.message_id(), + thread: bound.prepared().request().command().thread_id(), + preparation_transaction: value.preparation_transaction_id(), + plan_transaction: value.plan_transaction_id(), + transaction: value.transaction_id(), + installed_at: value.installed_at(), + basis: EvidenceRef::new(value.basis()), + } + } +} +#[derive(Serialize, Deserialize, Eq, PartialEq)] +#[serde(deny_unknown_fields)] +struct WorkRecord { + version: u8, + plan_digest: [u8; 32], + message: MessageId, + installation_transaction: TransactionId, + preparation_transaction: TransactionId, + plan_transaction: TransactionId, + transaction: TransactionId, + queued_at: UnixSeconds, + evidence: EvidenceRef, +} +impl WorkRecord { + fn new(installed: ChatInstallation, value: PublicationWork) -> Self { + Self { + version: 1, + plan_digest: *value.plan_digest(), + message: installed.message_id(), + installation_transaction: value.installation_transaction_id(), + preparation_transaction: installed.preparation_transaction_id(), + plan_transaction: installed.plan_transaction_id(), + transaction: value.transaction_id(), + queued_at: value.queued_at(), + evidence: EvidenceRef::new(value.evidence()), + } + } +} +pub(super) struct ProtectedMessage { + pub report: ChatInstallation, + pub thread: ThreadId, + pub protected: Vec<u8>, +} +pub(super) struct ProtectedWork { + pub report: PublicationWork, + pub protected: Vec<u8>, +} +pub(super) struct Records { + pub message: Option<ProtectedMessage>, + pub work: Vec<ProtectedWork>, +} +fn scope(bound: &BoundSigningPlan, receipt: EvidenceReceipt) -> IssuanceScope { + IssuanceScope { + workspace: receipt.workspace_id(), + operation: receipt.operation_id(), + plan_digest: *bound.plan().digest(), + attempt: receipt.attempt_id(), + recipient: receipt.recipient(), + } +} +fn bytes(row: &sqlx::sqlite::SqliteRow, index: usize) -> Result<Vec<u8>, OperationError> { + row.try_get::<Option<Vec<u8>>, _>(index) + .map_err(|_| OperationError::RecoveryRequired)? + .ok_or(OperationError::RecoveryRequired) +} +fn obtained( + graph: &RetainedGraph, + recipient: PublicKey, +) -> Result<EvidenceReceipt, OperationError> { + graph + .reports() + .find(|value| value.recipient() == recipient) + .and_then(|value| value.obtained()) + .ok_or(OperationError::RecoveryRequired) +} +impl Records { + pub(super) async fn read( + connection: &mut SqliteConnection, + selected: &AttachmentInput, + operation: OperationId, + graph: &RetainedGraph, + crypto: &WorkspaceCrypto, + ) -> Result<Self, OperationError> { + // At most one message and two recipient work rows can be materialized; + // fixed 262144-byte protected bounds cap their total below 1 MiB. + // Excess or orphan rows are rejected by bounded transaction sentinels. + let row=sqlx::query("SELECT CASE WHEN length(message_id)=32 THEN message_id END,CASE WHEN length(thread_id)=32 THEN thread_id END,CASE WHEN length(plan_digest)=32 THEN plan_digest END,CASE WHEN length(transaction_id)=32 THEN transaction_id END,CASE WHEN length(protected)>0 AND length(protected)<=262144 THEN protected END FROM chat_messages WHERE workspace=? AND operation_id=? LIMIT 1") + .bind(selected.workspace_id().as_bytes().as_slice()).bind(operation.as_bytes().as_slice()).fetch_optional(&mut *connection).await.map_err(|_|OperationError::RecoveryRequired)?; + let message = if let Some(row) = row { + let bound = graph.bound_plan().ok_or(OperationError::RecoveryRequired)?; + let protected = bytes(&row, 4)?; + let plaintext = crypto + .open_message( + selected.workspace_id(), + operation, + bound.plan().digest(), + &protected, + ) + .await?; + let encoded: MessageRecord = plaintext + .expose_secret(|raw| serde_json::from_slice(raw)) + .map_err(|_| OperationError::RecoveryRequired)?; + let basis = obtained(graph, encoded.basis.recipient)?; + let report = ChatInstallation::from_committed_store( + bound, + basis, + encoded.transaction, + encoded.installed_at, + )?; + if encoded != MessageRecord::new(bound, report) + || bytes(&row, 0)? != report.message_id().as_bytes() + || bytes(&row, 1)? != bound.prepared().request().command().thread_id().as_bytes() + || bytes(&row, 2)? != report.plan_digest() + || bytes(&row, 3)? != report.transaction_id().as_bytes() + { + return Err(OperationError::RecoveryRequired); + } + Some(ProtectedMessage { + report, + thread: encoded.thread, + protected, + }) + } else { + None + }; + let mut work = Vec::new(); + if let Some(bound) = graph.bound_plan() { + for recipient in graph.reports().map(|value| value.recipient()) { + let row=sqlx::query("SELECT CASE WHEN length(installation_transaction)=32 THEN installation_transaction END,CASE WHEN length(capture_transaction)=32 THEN capture_transaction END,CASE WHEN length(event_id)=32 THEN event_id END,CASE WHEN length(evidence_digest)=32 THEN evidence_digest END,CASE WHEN length(transaction_id)=32 THEN transaction_id END,CASE WHEN length(protected)>0 AND length(protected)<=262144 THEN protected END FROM chat_publication_work WHERE workspace=? AND operation_id=? AND recipient=?") + .bind(selected.workspace_id().as_bytes().as_slice()).bind(operation.as_bytes().as_slice()).bind(recipient.as_bytes().as_slice()).fetch_optional(&mut *connection).await.map_err(|_|OperationError::RecoveryRequired)?; + if let Some(row) = row { + let installed = message + .as_ref() + .ok_or(OperationError::RecoveryRequired)? + .report; + let evidence = obtained(graph, recipient)?; + let protected = bytes(&row, 5)?; + let plaintext = crypto + .open_issuance( + scope(bound, evidence), + IssuancePayload::Publication, + 0, + &protected, + ) + .await?; + let encoded: WorkRecord = plaintext + .expose_secret(|raw| serde_json::from_slice(raw)) + .map_err(|_| OperationError::RecoveryRequired)?; + let report = PublicationWork::from_committed_store( + bound, + installed, + evidence, + encoded.transaction, + encoded.queued_at, + )?; + if encoded != WorkRecord::new(installed, report) + || bytes(&row, 0)? != installed.transaction_id().as_bytes() + || bytes(&row, 1)? != evidence.transaction_id().as_bytes() + || bytes(&row, 2)? != evidence.event_id().as_bytes() + || bytes(&row, 3)? != evidence.digest() + || bytes(&row, 4)? != report.transaction_id().as_bytes() + { + return Err(OperationError::RecoveryRequired); + } + work.push(ProtectedWork { report, protected }); + } + } + } + if let Some(message) = &message + && !work + .iter() + .any(|value| value.report.evidence() == message.report.basis()) + { + return Err(OperationError::RecoveryRequired); + } + Ok(Self { message, work }) + } + pub(super) async fn unchanged( + &self, + connection: &mut SqliteConnection, + selected: &AttachmentInput, + operation: OperationId, + ) -> Result<(), OperationError> { + let counts:(i64,i64)=sqlx::query_as("SELECT (SELECT count(*) FROM (SELECT 1 FROM chat_messages WHERE workspace=?1 AND operation_id=?2 LIMIT 2)),(SELECT count(*) FROM (SELECT 1 FROM chat_publication_work WHERE workspace=?1 AND operation_id=?2 LIMIT 3))") + .bind(selected.workspace_id().as_bytes().as_slice()).bind(operation.as_bytes().as_slice()).fetch_one(&mut *connection).await.map_err(|_|OperationError::RecoveryRequired)?; + if counts != (i64::from(self.message.is_some()), self.work.len() as i64) { + return Err(OperationError::RecoveryRequired); + } + if let Some(message) = &self.message { + let installed = message.report; + let count:i64=sqlx::query_scalar("SELECT count(*) FROM chat_messages WHERE workspace=? AND operation_id=? AND message_id=? AND thread_id=? AND plan_digest=? AND transaction_id=? AND protected=?") + .bind(selected.workspace_id().as_bytes().as_slice()).bind(operation.as_bytes().as_slice()).bind(installed.message_id().as_bytes().as_slice()).bind(message.thread.as_bytes().as_slice()).bind(installed.plan_digest().as_slice()).bind(installed.transaction_id().as_bytes().as_slice()).bind(&message.protected).fetch_one(&mut *connection).await.map_err(|_|OperationError::RecoveryRequired)?; + if count != 1 { + return Err(OperationError::RecoveryRequired); + } + } + for value in &self.work { + let work = value.report; + let evidence = work.evidence(); + let count:i64=sqlx::query_scalar("SELECT count(*) FROM chat_publication_work WHERE workspace=? AND operation_id=? AND recipient=? AND installation_transaction=? AND capture_transaction=? AND event_id=? AND evidence_digest=? AND transaction_id=? AND protected=?") + .bind(selected.workspace_id().as_bytes().as_slice()).bind(operation.as_bytes().as_slice()).bind(evidence.recipient().as_bytes().as_slice()).bind(work.installation_transaction_id().as_bytes().as_slice()).bind(evidence.transaction_id().as_bytes().as_slice()).bind(evidence.event_id().as_bytes().as_slice()).bind(evidence.digest().as_slice()).bind(work.transaction_id().as_bytes().as_slice()).bind(&value.protected).fetch_one(&mut *connection).await.map_err(|_|OperationError::RecoveryRequired)?; + if count != 1 { + return Err(OperationError::RecoveryRequired); + } + } + Ok(()) + } +} +pub(super) async fn protect_message( + bound: &BoundSigningPlan, + report: ChatInstallation, + crypto: &WorkspaceCrypto, +) -> Result<ProtectedMessage, OperationError> { + let plaintext = Zeroizing::new( + serde_json::to_vec(&MessageRecord::new(bound, report)) + .map_err(|_| OperationError::InvalidCommand)?, + ); + let protected = crypto + .seal_message( + report.workspace_id(), + report.operation_id(), + report.plan_digest(), + &plaintext, + ) + .await?; + Ok(ProtectedMessage { + report, + thread: bound.prepared().request().command().thread_id(), + protected, + }) +} +pub(super) async fn protect_work( + bound: &BoundSigningPlan, + installed: ChatInstallation, + report: PublicationWork, + crypto: &WorkspaceCrypto, +) -> Result<ProtectedWork, OperationError> { + let plaintext = Zeroizing::new( + serde_json::to_vec(&WorkRecord::new(installed, report)) + .map_err(|_| OperationError::InvalidCommand)?, + ); + let protected = crypto + .seal_issuance( + scope(bound, report.evidence()), + IssuancePayload::Publication, + 0, + &plaintext, + ) + .await?; + Ok(ProtectedWork { report, protected }) +} +pub(super) async fn insert_message( + connection: &mut SqliteConnection, + bound: &BoundSigningPlan, + value: &ProtectedMessage, +) -> Result<(), OperationError> { + let report = value.report; + sqlx::query("INSERT INTO chat_messages(workspace,message_id,operation_id,thread_id,plan_digest,transaction_id,protected) VALUES(?,?,?,?,?,?,?)") + .bind(report.workspace_id().as_bytes().as_slice()).bind(report.message_id().as_bytes().as_slice()).bind(report.operation_id().as_bytes().as_slice()).bind(bound.prepared().request().command().thread_id().as_bytes().as_slice()).bind(report.plan_digest().as_slice()).bind(report.transaction_id().as_bytes().as_slice()).bind(&value.protected).execute(connection).await.map_err(write_error)?; + Ok(()) +} +pub(super) async fn insert_work( + connection: &mut SqliteConnection, + value: &ProtectedWork, +) -> Result<(), OperationError> { + let work = value.report; + let evidence = work.evidence(); + sqlx::query("INSERT INTO chat_publication_work(workspace,operation_id,recipient,installation_transaction,capture_transaction,event_id,evidence_digest,transaction_id,protected) VALUES(?,?,?,?,?,?,?,?,?)") + .bind(evidence.workspace_id().as_bytes().as_slice()).bind(evidence.operation_id().as_bytes().as_slice()).bind(evidence.recipient().as_bytes().as_slice()).bind(work.installation_transaction_id().as_bytes().as_slice()).bind(evidence.transaction_id().as_bytes().as_slice()).bind(evidence.event_id().as_bytes().as_slice()).bind(evidence.digest().as_slice()).bind(work.transaction_id().as_bytes().as_slice()).bind(&value.protected).execute(connection).await.map_err(write_error)?; + Ok(()) +} +fn write_error(error: sqlx::Error) -> OperationError { + if error + .as_database_error() + .is_some_and(|value| value.is_unique_violation()) + { + OperationError::IdempotencyConflict + } else { + OperationError::StorageFailure + } +} + +impl Journal<'_> { + pub(super) async fn install_chat( + &mut self, + request: InstallationRequest, + proof: AttachmentProof, + ) -> Result<InstallationOutcome, OperationError> { + let (record, grant, current) = self.access(request.binding(), &proof).await?; + let graph = RetainedGraph::read( + self.connection, + self.selected, + request.binding(), + request.operation_id(), + Some(request.command_digest()), + self.crypto, + ) + .await?; + let bound = graph.bound_plan().ok_or(OperationError::RecoveryRequired)?; + if bound.plan().digest() != request.plan_digest() { + return Err(OperationError::IdempotencyConflict); + } + let records = Records::read( + self.connection, + self.selected, + request.operation_id(), + &graph, + self.crypto, + ) + .await?; + let missing: Vec<EvidenceReceipt> = graph + .reports() + .filter_map(|value| value.obtained()) + .filter(|evidence| { + !records + .work + .iter() + .any(|value| value.report.evidence().recipient() == evidence.recipient()) + }) + .collect(); + let fresh = records.message.is_none() || !missing.is_empty(); + if records.message.is_none() && missing.is_empty() { + return Err(OperationError::InvalidEvidence); + } + // Exact replay never replaces the installation, reruns allocation, + // re-signs, consumes new entropy or prepares replacement ciphertext. + let transaction_id = if fresh { + TransactionId::from_bytes(super::random()?) + } else { + records + .message + .as_ref() + .ok_or(OperationError::RecoveryRequired)? + .report + .transaction_id() + }; + let installed = match &records.message { + Some(value) => value.report, + None => ChatInstallation::from_committed_store( + bound, + *missing.first().ok_or(OperationError::InvalidEvidence)?, + transaction_id, + current.observed_at(), + )?, + }; + let prepared_message = if records.message.is_none() { + Some(protect_message(bound, installed, self.crypto).await?) + } else { + None + }; + let mut prepared_work = Vec::with_capacity(missing.len()); + for evidence in missing { + let work = PublicationWork::from_committed_store( + bound, + installed, + evidence, + transaction_id, + current.observed_at(), + )?; + prepared_work.push(protect_work(bound, installed, work, self.crypto).await?); + } + let anchor = CommitAttempt::new( + installed.workspace_id(), + installed.operation_id(), + installed.command_digest(), + Some(transaction_id), + ); + let mut transaction = self + .connection + .begin_with("BEGIN IMMEDIATE") + .await + .map_err(|_| OperationError::StorageFailure)?; + let changed = async { + let now = recheck( + &mut transaction, + self.namespace, + self.selected, + &grant, + &record, + request.binding(), + &proof, + self.clock, + ) + .await?; + graph + .unchanged(&mut transaction, self.selected, request.operation_id()) + .await?; + records + .unchanged(&mut transaction, self.selected, request.operation_id()) + .await?; + if fresh && now.observed_at() < current.observed_at() { + return Err(super::WorkspaceError::Unavailable.into()); + } + // The original window constrains first issuance, not installation + // of independently authenticated already obtained original bytes. + if let Some(message) = &prepared_message { + insert_message(&mut transaction, bound, message).await?; + } + for work in &prepared_work { + insert_work(&mut transaction, work).await?; + } + Ok::<_, OperationError>(()) + } + .await; + if let Err(error) = changed { + if transaction.rollback().await.is_err() { + *self.uncertain = true; + return Ok(InstallationOutcome::Unknown(anchor)); + } + return Err(error); + } + let completed = if fresh { + transaction.commit().await + } else { + transaction.rollback().await + }; + if completed.is_err() { + *self.uncertain = true; + return Ok(InstallationOutcome::Unknown(anchor)); + } + Ok(InstallationOutcome::Committed(Box::new(installed))) + } +} diff --git a/crates/storage_sqlite/src/workspace/mod.rs b/crates/storage_sqlite/src/workspace/mod.rs @@ -10,6 +10,7 @@ mod attempt; mod crypto; mod evidence; mod fs; +mod installation; mod issuance; mod operation; mod plan; @@ -218,6 +219,19 @@ impl WorkspaceStore for NativeWorkspace { } } impl radroots_storage::command::OperationJournal for NativeWorkspace { + fn install_transition_and_outbox( + &self, + request: radroots_storage::command::InstallationRequest, + proof: AttachmentProof, + ) -> BoxFuture< + '_, + Result< + radroots_storage::command::InstallationOutcome, + radroots_storage::command::OperationError, + >, + > { + Box::pin(self.worker.install_chat(request, proof)) + } fn operation_snapshot( &self, binding: WorkspaceBinding, diff --git a/crates/storage_sqlite/src/workspace/snapshot.rs b/crates/storage_sqlite/src/workspace/snapshot.rs @@ -1,14 +1,16 @@ //! One authenticated coherent observation of the complete retained T0/T1/S graph. use super::{ - AttachmentProof, WorkspaceBinding, + AttachmentInput, AttachmentProof, WorkspaceBinding, WorkspaceCrypto, admission::{Journal, recheck, same_binding}, attempt, crypto::IssuanceScope, evidence::{self, ProtectedEvidence}, + installation::Records, issuance::absent_claim, operation, plan, + plan::ProtectedPlan, }; -use radroots_domain::{CommandDigest, EnvelopeEventId, OperationId, PublicKey}; +use radroots_domain::{CommandDigest, EnvelopeEventId, OperationId, PublicKey, UnixSeconds}; use radroots_storage::command::{ BoundSigningPlan, ClaimSnapshot, OperationError, OperationSnapshot, RecipientSnapshot, RetainedOperation, SigningAttempt, @@ -20,24 +22,35 @@ struct SeenRecipient { claim: Option<Vec<u8>>, evidence: Option<ProtectedEvidence>, } -impl Journal<'_> { - pub(super) async fn query_snapshot( - &mut self, - binding: WorkspaceBinding, +pub(super) struct RetainedGraph { + retained: Option<(RetainedOperation, Vec<u8>)>, + protected_plan: Option<ProtectedPlan>, + recipients: Vec<SeenRecipient>, +} +impl RetainedGraph { + pub(super) fn bound_plan(&self) -> Option<&BoundSigningPlan> { + self.retained + .as_ref() + .and_then(|(value, _)| value.bound_plan()) + } + pub(super) fn reports(&self) -> impl Iterator<Item = RecipientSnapshot> + '_ { + self.recipients.iter().map(|value| value.report) + } + pub(super) async fn read( + connection: &mut SqliteConnection, + selected: &AttachmentInput, + binding: &WorkspaceBinding, operation_id: OperationId, expected_digest: Option<CommandDigest>, - proof: AttachmentProof, - ) -> Result<Option<OperationSnapshot>, OperationError> { - let (record, grant, _) = self.access(&binding, &proof).await?; - let original = - operation::read(self.connection, self.selected, operation_id, self.crypto).await?; + crypto: &WorkspaceCrypto, + ) -> Result<Self, OperationError> { + let original = operation::read(connection, selected, operation_id, crypto).await?; let mut recipients = Vec::new(); let mut protected_plan = None; let retained = if let Some((prepared, protected)) = original { operation::check_digest(&prepared, binding.actor(), expected_digest)?; - same_binding(prepared.request().binding(), &binding)?; - let (prepared, bound) = - plan::read(self.connection, self.selected, prepared, self.crypto).await?; + same_binding(prepared.request().binding(), binding)?; + let (prepared, bound) = plan::read(connection, selected, prepared, crypto).await?; let source = if let Some((plan, transaction, time, protected)) = bound { protected_plan = Some(protected); let mut bound = @@ -49,14 +62,8 @@ impl Journal<'_> { .map(|seal| seal.recipient()) .collect(); for recipient in keys { - let (returned, claim) = attempt::read( - self.connection, - self.selected, - bound, - recipient, - self.crypto, - ) - .await?; + let (returned, claim) = + attempt::read(connection, selected, bound, recipient, crypto).await?; let expected = returned .plan() .seals() @@ -72,8 +79,7 @@ impl Journal<'_> { transaction, time, )?; - let captured = - evidence::read(self.connection, &attempt, self.crypto).await?; + let captured = evidence::read(connection, &attempt, crypto).await?; let claim = ClaimSnapshot::from_store_report(&attempt); let (obtained, evidence) = match captured { Some((_, protected)) => (Some(protected.receipt), Some(protected)), @@ -123,46 +129,64 @@ impl Journal<'_> { } else { None }; - // No provider or signature verification work occurs under this lock. - let mut transaction = self - .connection - .begin_with("BEGIN IMMEDIATE") - .await - .map_err(|_| OperationError::StorageFailure)?; - let checked = async { - let observed = recheck(&mut transaction, self.namespace, self.selected, &grant, &record, &binding, &proof, self.clock).await?; - if let Some((source, protected)) = &retained { - operation::unchanged(&mut transaction, source.prepared(), protected).await?; - plan::unchanged(&mut transaction, source.prepared(), protected_plan.as_ref()).await?; - for seen in &recipients { - if let (Some(claim), Some(protected)) = (seen.report.claim(), &seen.claim) { - attempt::unchanged_snapshot(&mut transaction, claim, protected).await?; - let scope = IssuanceScope { - workspace: claim.workspace_id(), operation: claim.operation_id(), - plan_digest: *claim.plan_digest(), attempt: claim.attempt_id(), recipient: claim.recipient(), - }; - evidence::unchanged_scope(&mut transaction, scope, seen.evidence.as_ref()).await?; - } else { - absent_claim(&mut transaction, self.selected, operation_id, seen.report.recipient()).await?; - } + Ok(Self { + retained, + protected_plan, + recipients, + }) + } + pub(super) async fn unchanged( + &self, + connection: &mut SqliteConnection, + selected: &AttachmentInput, + operation_id: OperationId, + ) -> Result<(), OperationError> { + if let Some((source, protected)) = &self.retained { + operation::unchanged(connection, source.prepared(), protected).await?; + plan::unchanged(connection, source.prepared(), self.protected_plan.as_ref()).await?; + for seen in &self.recipients { + if let (Some(claim), Some(protected)) = (seen.report.claim(), &seen.claim) { + attempt::unchanged_snapshot(connection, claim, protected).await?; + let scope = IssuanceScope { + workspace: claim.workspace_id(), + operation: claim.operation_id(), + plan_digest: *claim.plan_digest(), + attempt: claim.attempt_id(), + recipient: claim.recipient(), + }; + evidence::unchanged_scope(connection, scope, seen.evidence.as_ref()).await?; + } else { + absent_claim(connection, selected, operation_id, seen.report.recipient()) + .await?; } - graph_counts(&mut transaction, self.selected.workspace_id().as_bytes(), operation_id.as_bytes(), &recipients).await?; - } else { - let count:i64=sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM operations WHERE workspace=?1 AND operation_id=?2)+EXISTS(SELECT 1 FROM signing_plans WHERE workspace=?1 AND operation_id=?2)+EXISTS(SELECT 1 FROM signing_plan_parts WHERE workspace=?1 AND operation_id=?2)+EXISTS(SELECT 1 FROM signing_attempts WHERE workspace=?1 AND operation_id=?2)+EXISTS(SELECT 1 FROM seal_evidence WHERE workspace=?1 AND operation_id=?2)+EXISTS(SELECT 1 FROM seal_evidence_parts WHERE workspace=?1 AND operation_id=?2)") - .bind(self.selected.workspace_id().as_bytes().as_slice()).bind(operation_id.as_bytes().as_slice()).fetch_one(&mut *transaction).await.map_err(|_|OperationError::RecoveryRequired)?; - if count != 0 { return Err(OperationError::RecoveryRequired); } } - Ok::<_,OperationError>(observed.observed_at()) - }.await; - if transaction.rollback().await.is_err() { - *self.uncertain = true; - return Err(OperationError::RecoveryRequired); + graph_counts( + connection, + selected.workspace_id().as_bytes(), + operation_id.as_bytes(), + &self.recipients, + ) + .await?; + } else { + let count:i64=sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM operations WHERE workspace=?1 AND operation_id=?2)+EXISTS(SELECT 1 FROM signing_plans WHERE workspace=?1 AND operation_id=?2)+EXISTS(SELECT 1 FROM signing_plan_parts WHERE workspace=?1 AND operation_id=?2)+EXISTS(SELECT 1 FROM signing_attempts WHERE workspace=?1 AND operation_id=?2)+EXISTS(SELECT 1 FROM seal_evidence WHERE workspace=?1 AND operation_id=?2)+EXISTS(SELECT 1 FROM seal_evidence_parts WHERE workspace=?1 AND operation_id=?2)") + .bind(selected.workspace_id().as_bytes().as_slice()).bind(operation_id.as_bytes().as_slice()).fetch_one(connection).await.map_err(|_|OperationError::RecoveryRequired)?; + if count != 0 { + return Err(OperationError::RecoveryRequired); + } } - let observed = checked?; - match retained { + Ok(()) + } + pub(super) fn into_snapshot( + self, + observed: UnixSeconds, + ) -> Result<Option<OperationSnapshot>, OperationError> { + match self.retained { Some((source, _)) => OperationSnapshot::from_store_report( source, - recipients.into_iter().map(|seen| seen.report).collect(), + self.recipients + .into_iter() + .map(|seen| seen.report) + .collect(), observed, ) .map(Some), @@ -170,6 +194,74 @@ impl Journal<'_> { } } } +impl Journal<'_> { + pub(super) async fn query_snapshot( + &mut self, + binding: WorkspaceBinding, + operation_id: OperationId, + expected_digest: Option<CommandDigest>, + proof: AttachmentProof, + ) -> Result<Option<OperationSnapshot>, OperationError> { + let (record, grant, _) = self.access(&binding, &proof).await?; + let graph = RetainedGraph::read( + self.connection, + self.selected, + &binding, + operation_id, + expected_digest, + self.crypto, + ) + .await?; + let records = Records::read( + self.connection, + self.selected, + operation_id, + &graph, + self.crypto, + ) + .await?; + // Provider and signature verification work precedes the transaction. + let mut transaction = self + .connection + .begin_with("BEGIN IMMEDIATE") + .await + .map_err(|_| OperationError::StorageFailure)?; + let checked = async { + let observed = recheck( + &mut transaction, + self.namespace, + self.selected, + &grant, + &record, + &binding, + &proof, + self.clock, + ) + .await?; + graph + .unchanged(&mut transaction, self.selected, operation_id) + .await?; + records + .unchanged(&mut transaction, self.selected, operation_id) + .await?; + Ok::<_, OperationError>(observed.observed_at()) + } + .await; + if transaction.rollback().await.is_err() { + *self.uncertain = true; + return Err(OperationError::RecoveryRequired); + } + graph + .into_snapshot(checked?)? + .map(|snapshot| { + snapshot.with_installation( + records.message.map(|value| value.report), + records.work.into_iter().map(|value| value.report).collect(), + ) + }) + .transpose() + } +} async fn graph_counts( connection: &mut SqliteConnection, workspace: &[u8; 32], diff --git a/crates/storage_sqlite/src/workspace/tests/mod.rs b/crates/storage_sqlite/src/workspace/tests/mod.rs @@ -558,7 +558,7 @@ async fn existing_database_rejects_corrupt_version_missing_grant_and_oversized_r .await .unwrap(); let statement = match fault { - 0 => "PRAGMA user_version=6", + 0 => "PRAGMA user_version=7", 1 => "DELETE FROM workspace_grant", 2 => "UPDATE workspace_grant SET grant_id=zeroblob(31)", 3 => "UPDATE workspace_grant SET protected=zeroblob(0)", @@ -604,7 +604,7 @@ async fn existing_database_rejects_corrupt_version_missing_grant_and_oversized_r .fetch_one(&mut connection) .await .unwrap(); - assert_eq!(version, if fault == 0 { 6 } else { 5 }); + assert_eq!(version, if fault == 0 { 7 } else { 6 }); assert_eq!(rows, if fault == 1 { 0 } else { 1 }); if (3..=5).contains(&fault) { let size: i64 = sqlx::query_scalar("SELECT length(protected) FROM workspace_grant") diff --git a/crates/storage_sqlite/src/workspace/worker.rs b/crates/storage_sqlite/src/workspace/worker.rs @@ -1,14 +1,14 @@ use super::{ AttachmentInput, AttachmentProof, WorkspaceBinding, WorkspaceClock, WorkspaceCrypto, - WorkspaceError, admission::Journal, attempt, evidence, fs::Namespace, operation, plan, - record::GrantRecord, + WorkspaceError, admission::Journal, attempt, evidence, fs::Namespace, installation, operation, + plan, record::GrantRecord, }; use radroots_domain::{CommandDigest, OperationId}; use radroots_storage::command::{ AdmissionOutcome, AdmissionRequest, BoundSigningPlan, CapturedSeal, ChatSigningPlan, ClaimAttemptOutcome, ClaimAttemptRequest, CommitAttempt, EvidenceCaptureOutcome, - OperationError, OperationSnapshot, PlanBindingOutcome, PreparedOperation, SealEvidence, - SigningAttempt, + InstallationOutcome, InstallationRequest, OperationError, OperationSnapshot, + PlanBindingOutcome, PreparedOperation, SealEvidence, SigningAttempt, }; use sqlx::{ Connection, Row, SqliteConnection, @@ -33,6 +33,11 @@ const QUEUE_CAPACITY: usize = 32; const TABLE: &str = "CREATE TABLE workspace_grant (singleton INTEGER PRIMARY KEY CHECK(singleton=1), workspace BLOB NOT NULL CHECK(length(workspace)=32), grant_id BLOB NOT NULL CHECK(length(grant_id)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144)) STRICT"; type Reply<T, E = WorkspaceError> = oneshot::Sender<Result<T, E>>; enum Job { + Install( + Box<InstallationRequest>, + AttachmentProof, + Reply<InstallationOutcome, OperationError>, + ), Snapshot( Box<WorkspaceBinding>, OperationId, @@ -179,6 +184,19 @@ impl Handle { if ready.send(Ok(())).is_ok() { while let Ok(Queued { job, _budget }) = requests.recv() { match job { + Job::Install(request, proof, reply) => { + let result = Journal { + connection: &mut connection, + namespace: &namespace, + selected: &input, + crypto: &crypto, + clock: clock.as_ref(), + uncertain: &mut uncertain, + } + .install_chat(*request, proof) + .await; + let _ = reply.send(result); + } Job::Snapshot(binding, operation, digest, proof, reply) => { let result = Journal { connection: &mut connection, @@ -410,6 +428,19 @@ impl Handle { ))?; result.await.map_err(|_| OperationError::RecoveryRequired)? } + pub(super) async fn install_chat( + &self, + request: InstallationRequest, + proof: AttachmentProof, + ) -> Result<InstallationOutcome, OperationError> { + let anchor = request.identity(); + let (reply, result) = oneshot::channel(); + self.send(Job::Install(Box::new(request), proof, reply))?; + match result.await { + Ok(result) => result, + Err(_) => Ok(InstallationOutcome::Unknown(anchor)), + } + } pub(super) async fn claim_attempt( &self, request: ClaimAttemptRequest, @@ -652,13 +683,18 @@ async fn initialize( evidence::TABLE, evidence::PARTS, evidence::TX_INDEX, + installation::TABLE, + installation::OP_INDEX, + installation::TX_INDEX, + installation::THREAD_INDEX, + installation::WORK, ] { sqlx::raw_sql(definition) .execute(&mut *transaction) .await .map_err(|_| WorkspaceError::StorageFailure)?; } - sqlx::query("PRAGMA user_version=5") + sqlx::query("PRAGMA user_version=6") .execute(&mut *transaction) .await .map_err(|_| WorkspaceError::StorageFailure)?; @@ -680,11 +716,11 @@ async fn initialize( .fetch_one(&mut *connection) .await .map_err(|_| WorkspaceError::RecoveryRequired)?; - if version != 5 { + if version != 6 { return Err(WorkspaceError::RecoveryRequired); } - let catalog_matches: i64 = sqlx::query_scalar("SELECT CASE WHEN count(*)=11 AND sum(CASE WHEN type='table' AND name='workspace_grant' AND tbl_name='workspace_grant' AND sql=? THEN 1 WHEN type='table' AND name='operations' AND tbl_name='operations' AND sql=? THEN 1 WHEN type='table' AND name='signing_plans' AND tbl_name='signing_plans' AND sql=? THEN 1 WHEN type='table' AND name='signing_plan_parts' AND tbl_name='signing_plan_parts' AND sql=? THEN 1 WHEN type='table' AND name='signing_attempts' AND tbl_name='signing_attempts' AND sql=? THEN 1 WHEN type='index' AND name='signing_attempt_identity' AND tbl_name='signing_attempts' AND sql=? THEN 1 WHEN type='index' AND name='signing_attempt_transaction_identity' AND tbl_name='signing_attempts' AND sql=? THEN 1 WHEN type='table' AND name='seal_evidence' AND tbl_name='seal_evidence' AND sql=? THEN 1 WHEN type='table' AND name='seal_evidence_parts' AND tbl_name='seal_evidence_parts' AND sql=? THEN 1 WHEN type='index' AND name='seal_evidence_transaction_identity' AND tbl_name='seal_evidence' AND sql=? THEN 1 WHEN type='index' AND name='operation_transaction_identity' AND tbl_name='operations' AND sql=? THEN 1 ELSE 0 END)=11 THEN 1 ELSE 0 END FROM sqlite_schema") - .bind(TABLE).bind(operation::TABLE).bind(plan::TABLE).bind(plan::PARTS).bind(attempt::TABLE).bind(attempt::ID_INDEX).bind(attempt::TX_INDEX).bind(evidence::TABLE).bind(evidence::PARTS).bind(evidence::TX_INDEX).bind(operation::INDEX).fetch_one(&mut *connection).await.map_err(|_| WorkspaceError::RecoveryRequired)?; + let catalog_matches: i64 = sqlx::query_scalar("SELECT CASE WHEN count(*)=16 AND sum(CASE WHEN type='table' AND name='workspace_grant' AND tbl_name='workspace_grant' AND sql=? THEN 1 WHEN type='table' AND name='operations' AND tbl_name='operations' AND sql=? THEN 1 WHEN type='table' AND name='signing_plans' AND tbl_name='signing_plans' AND sql=? THEN 1 WHEN type='table' AND name='signing_plan_parts' AND tbl_name='signing_plan_parts' AND sql=? THEN 1 WHEN type='table' AND name='signing_attempts' AND tbl_name='signing_attempts' AND sql=? THEN 1 WHEN type='index' AND name='signing_attempt_identity' AND tbl_name='signing_attempts' AND sql=? THEN 1 WHEN type='index' AND name='signing_attempt_transaction_identity' AND tbl_name='signing_attempts' AND sql=? THEN 1 WHEN type='table' AND name='seal_evidence' AND tbl_name='seal_evidence' AND sql=? THEN 1 WHEN type='table' AND name='seal_evidence_parts' AND tbl_name='seal_evidence_parts' AND sql=? THEN 1 WHEN type='index' AND name='seal_evidence_transaction_identity' AND tbl_name='seal_evidence' AND sql=? THEN 1 WHEN type='index' AND name='operation_transaction_identity' AND tbl_name='operations' AND sql=? THEN 1 WHEN type='table' AND name='chat_messages' AND tbl_name='chat_messages' AND sql=? THEN 1 WHEN type='index' AND name='chat_message_operation_identity' AND tbl_name='chat_messages' AND sql=? THEN 1 WHEN type='index' AND name='chat_message_transaction_identity' AND tbl_name='chat_messages' AND sql=? THEN 1 WHEN type='index' AND name='chat_message_thread' AND tbl_name='chat_messages' AND sql=? THEN 1 WHEN type='table' AND name='chat_publication_work' AND tbl_name='chat_publication_work' AND sql=? THEN 1 ELSE 0 END)=16 THEN 1 ELSE 0 END FROM sqlite_schema") + .bind(TABLE).bind(operation::TABLE).bind(plan::TABLE).bind(plan::PARTS).bind(attempt::TABLE).bind(attempt::ID_INDEX).bind(attempt::TX_INDEX).bind(evidence::TABLE).bind(evidence::PARTS).bind(evidence::TX_INDEX).bind(operation::INDEX).bind(installation::TABLE).bind(installation::OP_INDEX).bind(installation::TX_INDEX).bind(installation::THREAD_INDEX).bind(installation::WORK).fetch_one(&mut *connection).await.map_err(|_| WorkspaceError::RecoveryRequired)?; if catalog_matches != 1 { return Err(WorkspaceError::RecoveryRequired); } diff --git a/crates/storage_sqlite/tests/workspace_admission.rs b/crates/storage_sqlite/tests/workspace_admission.rs @@ -763,7 +763,7 @@ async fn predecessor_grant_only_schema_is_preserved_for_explicit_cutover() { store.close().await.unwrap(); let mut database = fixture.database().await; sqlx::raw_sql( - "DROP TABLE seal_evidence_parts; DROP TABLE seal_evidence; DROP TABLE signing_attempts; DROP TABLE signing_plan_parts; DROP TABLE signing_plans; DROP INDEX operation_transaction_identity; DROP TABLE operations; PRAGMA user_version=1", + "DROP TABLE chat_publication_work; DROP TABLE chat_messages; DROP TABLE seal_evidence_parts; DROP TABLE seal_evidence; DROP TABLE signing_attempts; DROP TABLE signing_plan_parts; DROP TABLE signing_plans; DROP INDEX operation_transaction_identity; DROP TABLE operations; PRAGMA user_version=1", ) .execute(&mut database) .await