lib

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

commit 9930e19cd4f125ac9eac489fc9f129e0ad0854ae
parent eebc57b3235bb80660d157129fdecae29ea9e951
Author: triesap <tyson@radroots.org>
Date:   Sun, 18 Oct 2026 15:20:56 +0000

workspace: retain exact native delivery attempts

- Introduce protected schema7 envelope intents and append-only target observations.
- Bind payload and logical-message identities to their original operations.
- Freeze publication policy, bound retries and preserve canonical route and temporary-key checks.
- Verify native loopback and recovery, reviewed APIs, portable builds and unchanged coverage gates.

Diffstat:
MCargo.lock | 2++
Mcontracts/api_baselines/radroots_execution.txt | 5+++++
Mcontracts/api_baselines/radroots_storage.txt | 109+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcontracts/api_baselines/radroots_storage_sqlite.txt | 4++++
Mcontracts/api_baselines/radroots_transport.txt | 4++++
Mcontracts/protocol/chat.v1.json | 77++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
Mcrates/execution/README.md | 10++++++++++
Mcrates/execution/src/receipt.rs | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Mcrates/protocol/README.md | 6++++++
Mcrates/protocol/src/chat.rs | 5+++++
Acrates/protocol/tests/chat_identity.rs | 23+++++++++++++++++++++++
Mcrates/protocol/tests/fixtures/chat_wire.v1.json | 56++++++++++++++++++++++++++++----------------------------
Mcrates/protocol/tests/support/chat_wire_oracle.mjs | 2+-
Mcrates/radroots/Cargo.toml | 4+++-
Acrates/radroots/tests/support/receipt_delivery.rs | 662+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/radroots/tests/support/receipt_delivery_admission.rs | 509+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/radroots/tests/support/receipt_delivery_faults.rs | 573+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/radroots/tests/support/receipt_delivery_validation.rs | 451+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/radroots/tests/support/receipt_installation.rs | 2+-
Mcrates/radroots/tests/support/receipt_plans.rs | 23++++++++++++++++++++---
Mcrates/storage/README.md | 23+++++++++++++++++++++++
Mcrates/storage/src/command.rs | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/storage/src/command/delivery.rs | 478+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/storage/src/command/delivery_summary.rs | 93+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/storage/src/command/observation.rs | 202+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/storage/src/command/snapshot.rs | 234+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Mcrates/storage_sqlite/Cargo.toml | 1+
Mcrates/storage_sqlite/README.md | 37+++++++++++++++++++++++++++++++++----
Mcrates/storage_sqlite/src/workspace/crypto.rs | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/storage_sqlite/src/workspace/delivery/codec.rs | 441+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/storage_sqlite/src/workspace/delivery/codec_tests.rs | 358+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/storage_sqlite/src/workspace/delivery/graph.rs | 271+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/storage_sqlite/src/workspace/delivery/graph_tests.rs | 372+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/storage_sqlite/src/workspace/delivery/json.rs | 172+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/storage_sqlite/src/workspace/delivery/mod.rs | 17+++++++++++++++++
Acrates/storage_sqlite/src/workspace/delivery/observation.rs | 241+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/storage_sqlite/src/workspace/delivery/observation_operations.rs | 237+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/storage_sqlite/src/workspace/delivery/observation_tests.rs | 89+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/storage_sqlite/src/workspace/delivery/operations.rs | 413+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/storage_sqlite/src/workspace/installation.rs | 4++--
Mcrates/storage_sqlite/src/workspace/issuance.rs | 2+-
Mcrates/storage_sqlite/src/workspace/mod.rs | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/storage_sqlite/src/workspace/snapshot.rs | 63++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
Mcrates/storage_sqlite/src/workspace/tests/issuance_records.rs | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/storage_sqlite/src/workspace/tests/mod.rs | 6+++---
Mcrates/storage_sqlite/src/workspace/worker.rs | 223+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Mcrates/storage_sqlite/tests/package_boundary.rs | 20++++++++++++++++++++
Mcrates/transport/README.md | 11+++++++++++
Mcrates/transport/src/error.rs | 4++++
Mcrates/transport/src/sink.rs | 33+++++++++++++++++++++++++++++----
Mcrates/transport/src/target.rs | 15+++++++++++++++
Mcrates/transport/tests/delivery_contract.rs | 106+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/transport/tests/target_contract.rs | 43+++++++++++++++++++++++++++++++++++++++++++
53 files changed, 7038 insertions(+), 88 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -3057,10 +3057,12 @@ dependencies = [ "radroots_storage_sqlite", "radroots_trade", "radroots_transport", + "radroots_transport_nostr", "serde_json", "sqlx", "tempfile", "tokio", + "tokio-tungstenite", "toml 0.8.23", ] diff --git a/contracts/api_baselines/radroots_execution.txt b/contracts/api_baselines/radroots_execution.txt @@ -177,6 +177,7 @@ pub const fn radroots_execution::RetryClass::as_str(self) -> &'static str #[non_exhaustive] pub enum radroots_execution::TargetObservation pub radroots_execution::TargetObservation::Accepted pub radroots_execution::TargetObservation::Disconnected +pub radroots_execution::TargetObservation::NotAttempted pub radroots_execution::TargetObservation::Rejected pub radroots_execution::TargetObservation::TimedOut pub radroots_execution::TargetObservation::Unknown @@ -206,7 +207,10 @@ pub const fn radroots_execution::DeliveryObservation::event_id(&self) -> radroot pub const fn radroots_execution::DeliveryObservation::observed_at_unix_ms(&self) -> core::option::Option<core::num::nonzero::NonZeroU64> pub const fn radroots_execution::DeliveryObservation::outcome(&self) -> radroots_execution::TargetObservation pub const fn radroots_execution::DeliveryObservation::recipient(&self) -> core::option::Option<radroots_identity::key::PublicKey> +pub fn radroots_execution::DeliveryObservation::request_id(&self) -> &str pub fn radroots_execution::DeliveryObservation::target(&self) -> &str +pub fn radroots_execution::DeliveryObservation::target_fingerprint(&self) -> &str +pub const fn radroots_execution::DeliveryObservation::target_ordinal(&self) -> u8 impl core::fmt::Debug for radroots_execution::DeliveryObservation pub fn radroots_execution::DeliveryObservation::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result pub struct radroots_execution::Detail @@ -246,6 +250,7 @@ pub fn radroots_execution::OperationReceipt<T>::conflicting_records(&self) -> &[ pub fn radroots_execution::OperationReceipt<T>::conflicting_sources(&self) -> &[radroots_domain::identifier::SourceId] pub const fn radroots_execution::OperationReceipt<T>::context(&self) -> &radroots_domain::context::CommandContextData pub const fn radroots_execution::OperationReceipt<T>::delivery(&self) -> radroots_execution::Delivery +pub fn radroots_execution::OperationReceipt<T>::delivery_attempts(&self) -> &[radroots_storage::command::delivery_summary::DeliveryAttemptSummary] 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> diff --git a/contracts/api_baselines/radroots_storage.txt b/contracts/api_baselines/radroots_storage.txt @@ -769,6 +769,28 @@ pub radroots_storage::command::ClaimAttemptOutcome::Retained(radroots_storage::c pub radroots_storage::command::ClaimAttemptOutcome::Unknown(radroots_storage::command::CommitAttempt) impl core::fmt::Debug for radroots_storage::command::ClaimAttemptOutcome pub fn radroots_storage::command::ClaimAttemptOutcome::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result +pub enum radroots_storage::command::DeliveryAttemptOutcome +pub radroots_storage::command::DeliveryAttemptOutcome::Claimed(alloc::boxed::Box<radroots_storage::command::DeliveryAttempt>) +pub radroots_storage::command::DeliveryAttemptOutcome::Retained(alloc::boxed::Box<radroots_storage::command::DeliveryAttempt>) +pub radroots_storage::command::DeliveryAttemptOutcome::Unknown +pub radroots_storage::command::DeliveryAttemptOutcome::Unknown::attempt: radroots_transport::sink::DeliveryRequestId +pub radroots_storage::command::DeliveryAttemptOutcome::Unknown::operation: radroots_storage::command::CommitAttempt +impl core::fmt::Debug for radroots_storage::command::DeliveryAttemptOutcome +pub fn radroots_storage::command::DeliveryAttemptOutcome::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result +pub enum radroots_storage::command::DeliveryObservationKind +pub radroots_storage::command::DeliveryObservationKind::Accepted +pub radroots_storage::command::DeliveryObservationKind::Disconnected +pub radroots_storage::command::DeliveryObservationKind::NotAttempted +pub radroots_storage::command::DeliveryObservationKind::Rejected +pub radroots_storage::command::DeliveryObservationKind::TimedOut +pub radroots_storage::command::DeliveryObservationKind::Unknown +impl radroots_storage::command::DeliveryObservationKind +pub const fn radroots_storage::command::DeliveryObservationKind::code(self) -> u8 +pub enum radroots_storage::command::DeliveryObservationOutcome +pub radroots_storage::command::DeliveryObservationOutcome::Committed(radroots_storage::command::DeliveryObservation) +pub radroots_storage::command::DeliveryObservationOutcome::Unknown(radroots_storage::command::DeliveryObservation) +impl core::fmt::Debug for radroots_storage::command::DeliveryObservationOutcome +pub fn radroots_storage::command::DeliveryObservationOutcome::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result 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) @@ -779,6 +801,7 @@ 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::DeliveryAttemptLimit pub radroots_storage::command::OperationError::IdempotencyConflict pub radroots_storage::command::OperationError::InvalidCommand pub radroots_storage::command::OperationError::InvalidEvidence @@ -883,6 +906,84 @@ pub const fn radroots_storage::command::CommitAttempt::new(radroots_domain::iden pub const fn radroots_storage::command::CommitAttempt::operation_id(self) -> radroots_domain::identifier::OperationId pub const fn radroots_storage::command::CommitAttempt::transaction_id(self) -> core::option::Option<radroots_domain::identifier::TransactionId> pub const fn radroots_storage::command::CommitAttempt::workspace_id(self) -> radroots_domain::identifier::WorkspaceId +pub struct radroots_storage::command::DeliveryAttempt +impl radroots_storage::command::DeliveryAttempt +pub const fn radroots_storage::command::DeliveryAttempt::envelope_digest(&self) -> &[u8; 32] +pub fn radroots_storage::command::DeliveryAttempt::from_store_report(radroots_storage::command::PublicationWork, radroots_storage::command::DeliveryIntent, &str, radroots_domain::identifier::TransactionId, radroots_domain::context::UnixSeconds, alloc::vec::Vec<u8>) -> core::result::Result<Self, radroots_storage::command::OperationError> +pub const fn radroots_storage::command::DeliveryAttempt::intent(&self) -> &radroots_storage::command::DeliveryIntent +pub fn radroots_storage::command::DeliveryAttempt::raw_json(&self) -> &str +pub const fn radroots_storage::command::DeliveryAttempt::retained_at(&self) -> radroots_domain::context::UnixSeconds +pub fn radroots_storage::command::DeliveryAttempt::target_ordinals(&self) -> &[u8] +pub const fn radroots_storage::command::DeliveryAttempt::transaction_id(&self) -> radroots_domain::identifier::TransactionId +pub fn radroots_storage::command::DeliveryAttempt::transport_request(&self) -> core::result::Result<radroots_transport::sink::DeliveryRequest, radroots_storage::command::OperationError> +pub const fn radroots_storage::command::DeliveryAttempt::work(&self) -> radroots_storage::command::PublicationWork +impl core::fmt::Debug for radroots_storage::command::DeliveryAttempt +pub fn radroots_storage::command::DeliveryAttempt::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result +pub struct radroots_storage::command::DeliveryAttemptRequest +impl radroots_storage::command::DeliveryAttemptRequest +pub const fn radroots_storage::command::DeliveryAttemptRequest::binding(&self) -> &radroots_storage::workspace::WorkspaceBinding +pub fn radroots_storage::command::DeliveryAttemptRequest::identity(&self) -> radroots_storage::command::CommitAttempt +pub const fn radroots_storage::command::DeliveryAttemptRequest::intent(&self) -> &radroots_storage::command::DeliveryIntent +pub fn radroots_storage::command::DeliveryAttemptRequest::new(radroots_storage::workspace::WorkspaceBinding, radroots_storage::command::PublicationWork, radroots_storage::command::DeliveryIntent) -> core::result::Result<Self, radroots_storage::command::OperationError> +pub const fn radroots_storage::command::DeliveryAttemptRequest::work(&self) -> radroots_storage::command::PublicationWork +impl core::fmt::Debug for radroots_storage::command::DeliveryAttemptRequest +pub fn radroots_storage::command::DeliveryAttemptRequest::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result +pub struct radroots_storage::command::DeliveryAttemptSummary +impl radroots_storage::command::DeliveryAttemptSummary +pub fn radroots_storage::command::DeliveryAttemptSummary::acceptance_satisfied(&self, &[radroots_storage::command::DeliveryObservation]) -> core::result::Result<bool, radroots_storage::command::OperationError> +pub const fn radroots_storage::command::DeliveryAttemptSummary::envelope_digest(&self) -> &[u8; 32] +pub fn radroots_storage::command::DeliveryAttemptSummary::from_attempt(&radroots_storage::command::DeliveryAttempt) -> Self +pub const fn radroots_storage::command::DeliveryAttemptSummary::intent(&self) -> &radroots_storage::command::DeliveryIntent +pub const fn radroots_storage::command::DeliveryAttemptSummary::retained_at(&self) -> radroots_domain::context::UnixSeconds +pub fn radroots_storage::command::DeliveryAttemptSummary::target_ordinals(&self) -> &[u8] +pub const fn radroots_storage::command::DeliveryAttemptSummary::transaction_id(&self) -> radroots_domain::identifier::TransactionId +pub const fn radroots_storage::command::DeliveryAttemptSummary::work(&self) -> radroots_storage::command::PublicationWork +impl core::fmt::Debug for radroots_storage::command::DeliveryAttemptSummary +pub fn radroots_storage::command::DeliveryAttemptSummary::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result +pub struct radroots_storage::command::DeliveryIntent +impl radroots_storage::command::DeliveryIntent +pub const fn radroots_storage::command::DeliveryIntent::deadline_unix_ms(&self) -> core::num::nonzero::NonZeroU64 +pub const fn radroots_storage::command::DeliveryIntent::digest(&self) -> &[u8; 32] +pub const fn radroots_storage::command::DeliveryIntent::event_id(&self) -> radroots_domain::identifier::EnvelopeEventId +pub fn radroots_storage::command::DeliveryIntent::new(&str, radroots_domain::identifier::EnvelopeEventId, &radroots_transport::target::TargetSet, &radroots_transport::policy::SatisfactionPolicy, core::num::nonzero::NonZeroU64) -> core::result::Result<Self, radroots_storage::command::OperationError> +pub const fn radroots_storage::command::DeliveryIntent::policy(&self) -> &radroots_transport::policy::SatisfactionPolicy +pub const fn radroots_storage::command::DeliveryIntent::publication_policy_digest(&self) -> &[u8; 32] +pub const fn radroots_storage::command::DeliveryIntent::request_id(&self) -> &radroots_transport::sink::DeliveryRequestId +pub fn radroots_storage::command::DeliveryIntent::selected_target_set(&self) -> core::result::Result<radroots_transport::target::TargetSet, radroots_storage::command::OperationError> +pub fn radroots_storage::command::DeliveryIntent::selected_targets(&self) -> impl core::iter::traits::iterator::Iterator<Item = &radroots_transport::target::Target> +pub fn radroots_storage::command::DeliveryIntent::selection(&self) -> &[radroots_transport::target::TargetFingerprint] +pub const fn radroots_storage::command::DeliveryIntent::targets(&self) -> &radroots_transport::target::TargetSet +pub fn radroots_storage::command::DeliveryIntent::with_selection(self, &radroots_transport::target::TargetSet) -> core::result::Result<Self, radroots_storage::command::OperationError> +impl core::fmt::Debug for radroots_storage::command::DeliveryIntent +pub fn radroots_storage::command::DeliveryIntent::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result +pub struct radroots_storage::command::DeliveryObservation +impl radroots_storage::command::DeliveryObservation +pub const fn radroots_storage::command::DeliveryObservation::envelope_digest(&self) -> &[u8; 32] +pub const fn radroots_storage::command::DeliveryObservation::event_id(&self) -> radroots_domain::identifier::EnvelopeEventId +pub fn radroots_storage::command::DeliveryObservation::from_failure(&radroots_storage::command::DeliveryAttempt, &radroots_transport::sink::SinkFailure, &radroots_transport::target::TargetFingerprint, core::option::Option<core::num::nonzero::NonZeroU64>) -> core::result::Result<Self, radroots_storage::command::OperationError> +pub fn radroots_storage::command::DeliveryObservation::from_receipt(&radroots_storage::command::DeliveryAttempt, &radroots_transport::sink::DeliveryReceipt, &radroots_transport::target::TargetFingerprint, core::option::Option<core::num::nonzero::NonZeroU64>) -> core::result::Result<Self, radroots_storage::command::OperationError> +pub fn radroots_storage::command::DeliveryObservation::from_store_report(&radroots_storage::command::DeliveryAttempt, &radroots_transport::sink::DeliveryTargetReceipt, core::option::Option<core::num::nonzero::NonZeroU64>) -> core::result::Result<Self, radroots_storage::command::OperationError> +pub const fn radroots_storage::command::DeliveryObservation::kind(&self) -> radroots_storage::command::DeliveryObservationKind +pub const fn radroots_storage::command::DeliveryObservation::observed_at_unix_ms(&self) -> core::option::Option<core::num::nonzero::NonZeroU64> +pub const fn radroots_storage::command::DeliveryObservation::request_id(&self) -> &radroots_transport::sink::DeliveryRequestId +pub const fn radroots_storage::command::DeliveryObservation::target_ordinal(&self) -> u8 +pub const fn radroots_storage::command::DeliveryObservation::target_receipt(&self) -> &radroots_transport::sink::DeliveryTargetReceipt +pub const fn radroots_storage::command::DeliveryObservation::work(&self) -> radroots_storage::command::PublicationWork +impl core::fmt::Debug for radroots_storage::command::DeliveryObservation +pub fn radroots_storage::command::DeliveryObservation::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result +pub struct radroots_storage::command::EnvelopeCandidate +impl radroots_storage::command::EnvelopeCandidate +pub const fn radroots_storage::command::EnvelopeCandidate::author(&self) -> radroots_identity::key::PublicKey +pub const fn radroots_storage::command::EnvelopeCandidate::created_at(&self) -> radroots_domain::context::UnixSeconds +pub const fn radroots_storage::command::EnvelopeCandidate::digest(&self) -> &[u8; 32] +pub const fn radroots_storage::command::EnvelopeCandidate::event_id(&self) -> radroots_domain::identifier::EnvelopeEventId +pub fn radroots_storage::command::EnvelopeCandidate::from_obtained(radroots_storage::command::PublicationWork, &str, &[u8; 32]) -> core::result::Result<Self, radroots_storage::command::OperationError> +pub fn radroots_storage::command::EnvelopeCandidate::raw_json(&self) -> &str +pub fn radroots_storage::command::EnvelopeCandidate::retained_copy(&self) -> Self +pub fn radroots_storage::command::EnvelopeCandidate::with_wrapping_key<T>(&self, impl core::ops::function::FnOnce(&[u8; 32]) -> T) -> T +pub const fn radroots_storage::command::EnvelopeCandidate::work(&self) -> radroots_storage::command::PublicationWork +impl core::fmt::Debug for radroots_storage::command::EnvelopeCandidate +pub fn radroots_storage::command::EnvelopeCandidate::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result pub struct radroots_storage::command::EvidenceReceipt impl radroots_storage::command::EvidenceReceipt pub const fn radroots_storage::command::EvidenceReceipt::attempt_id(&self) -> radroots_domain::identifier::SigningAttemptId @@ -909,14 +1010,18 @@ 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::delivery_attempts(&self) -> &[radroots_storage::command::DeliveryAttemptSummary] +pub fn radroots_storage::command::OperationSnapshot::delivery_observations(&self) -> &[radroots_storage::command::DeliveryObservation] 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::publication_accepted(&self, radroots_storage::command::PublicationWork) -> core::result::Result<bool, radroots_storage::command::OperationError> 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_delivery(self, alloc::vec::Vec<radroots_storage::command::DeliveryAttemptSummary>, alloc::vec::Vec<radroots_storage::command::DeliveryObservation>) -> core::result::Result<Self, radroots_storage::command::OperationError> 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 @@ -986,10 +1091,14 @@ 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::delivery_attempt(&self, radroots_storage::workspace::WorkspaceBinding, radroots_domain::identifier::OperationId, radroots_domain::identifier::CommandDigest, radroots_identity::key::PublicKey, radroots_transport::sink::DeliveryRequestId, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::command::DeliveryAttempt>, radroots_storage::command::OperationError>> +pub fn radroots_storage::command::OperationJournal::delivery_observations(&self, radroots_storage::workspace::WorkspaceBinding, radroots_domain::identifier::OperationId, radroots_domain::identifier::CommandDigest, radroots_identity::key::PublicKey, radroots_transport::sink::DeliveryRequestId, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<alloc::vec::Vec<radroots_storage::command::DeliveryObservation>, 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>> +pub fn radroots_storage::command::OperationJournal::prepare_delivery_attempt<'a>(&'a self, radroots_storage::command::DeliveryAttemptRequest, radroots_storage::workspace::AttachmentProof, core::option::Option<&'a radroots_storage::command::EnvelopeCandidate>) -> radroots_transport::source::BoxFuture<'a, core::result::Result<radroots_storage::command::DeliveryAttemptOutcome, radroots_storage::command::OperationError>> +pub fn radroots_storage::command::OperationJournal::record_delivery_observation(&self, radroots_storage::workspace::WorkspaceBinding, radroots_storage::workspace::AttachmentProof, radroots_storage::command::DeliveryObservation) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::command::DeliveryObservationOutcome, radroots_storage::command::OperationError>> pub fn radroots_storage::command::OperationJournal::signing_attempt(&self, radroots_storage::command::ClaimAttemptRequest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::command::SigningAttempt>, radroots_storage::command::OperationError>> pub fn radroots_storage::command::OperationJournal::signing_plan(&self, radroots_storage::workspace::WorkspaceBinding, radroots_domain::identifier::OperationId, radroots_domain::identifier::CommandDigest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::command::BoundSigningPlan>, radroots_storage::command::OperationError>> pub mod radroots_storage::event diff --git a/contracts/api_baselines/radroots_storage_sqlite.txt b/contracts/api_baselines/radroots_storage_sqlite.txt @@ -384,10 +384,14 @@ 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::delivery_attempt(&self, radroots_storage::workspace::WorkspaceBinding, radroots_domain::identifier::OperationId, radroots_domain::identifier::CommandDigest, radroots_identity::key::PublicKey, radroots_transport::sink::DeliveryRequestId, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::command::delivery::DeliveryAttempt>, radroots_storage::command::OperationError>> +pub fn radroots_storage_sqlite::workspace::NativeWorkspace::delivery_observations(&self, radroots_storage::workspace::WorkspaceBinding, radroots_domain::identifier::OperationId, radroots_domain::identifier::CommandDigest, radroots_identity::key::PublicKey, radroots_transport::sink::DeliveryRequestId, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<alloc::vec::Vec<radroots_storage::command::observation::DeliveryObservation>, 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>> +pub fn radroots_storage_sqlite::workspace::NativeWorkspace::prepare_delivery_attempt<'a>(&'a self, radroots_storage::command::delivery::DeliveryAttemptRequest, radroots_storage::workspace::AttachmentProof, core::option::Option<&'a radroots_storage::command::delivery::EnvelopeCandidate>) -> radroots_transport::source::BoxFuture<'a, core::result::Result<radroots_storage::command::delivery::DeliveryAttemptOutcome, radroots_storage::command::OperationError>> +pub fn radroots_storage_sqlite::workspace::NativeWorkspace::record_delivery_observation(&self, radroots_storage::workspace::WorkspaceBinding, radroots_storage::workspace::AttachmentProof, radroots_storage::command::observation::DeliveryObservation) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::command::observation::DeliveryObservationOutcome, radroots_storage::command::OperationError>> pub fn radroots_storage_sqlite::workspace::NativeWorkspace::signing_attempt(&self, radroots_storage::command::issuance::ClaimAttemptRequest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::command::issuance::SigningAttempt>, radroots_storage::command::OperationError>> pub fn radroots_storage_sqlite::workspace::NativeWorkspace::signing_plan(&self, radroots_storage::workspace::WorkspaceBinding, radroots_domain::identifier::OperationId, radroots_domain::identifier::CommandDigest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::command::BoundSigningPlan>, radroots_storage::command::OperationError>> impl radroots_storage::workspace::WorkspaceStore for radroots_storage_sqlite::workspace::NativeWorkspace diff --git a/contracts/api_baselines/radroots_transport.txt b/contracts/api_baselines/radroots_transport.txt @@ -90,6 +90,7 @@ pub const radroots_transport::endpoint::TARGET_LABEL_MAX_BYTES: usize pub const radroots_transport::endpoint::TARGET_SCOPE_MAX_BYTES: usize pub mod radroots_transport::error pub enum radroots_transport::error::Error +pub radroots_transport::error::Error::DeliveryReceiptPayloadMismatch pub radroots_transport::error::Error::DeliveryReceiptRequestIdMismatch pub radroots_transport::error::Error::DeliveryReceiptTargetSetMismatch pub radroots_transport::error::Error::DeliveryTargetReceiptAttemptMismatch @@ -525,6 +526,7 @@ pub fn radroots_transport::target::Target::kind(&self) -> &radroots_transport::T pub fn radroots_transport::target::Target::label(&self) -> core::option::Option<&radroots_transport::target::TargetLabel> pub fn radroots_transport::target::Target::local(impl core::convert::AsRef<str>) -> core::result::Result<Self, radroots_transport::error::Error> pub fn radroots_transport::target::Target::local_with_metadata(impl core::convert::AsRef<str>, core::option::Option<radroots_transport::target::TargetScope>, core::option::Option<radroots_transport::target::TargetLabel>) -> core::result::Result<Self, radroots_transport::error::Error> +pub fn radroots_transport::target::Target::matches_nostr_relay(&self, &str) -> core::result::Result<bool, radroots_transport::error::Error> pub fn radroots_transport::target::Target::new(radroots_transport::TransportId, impl core::convert::AsRef<str>) -> core::result::Result<Self, radroots_transport::error::Error> pub fn radroots_transport::target::Target::new_with_metadata(radroots_transport::TransportId, impl core::convert::AsRef<str>, core::option::Option<radroots_transport::target::TargetScope>, core::option::Option<radroots_transport::target::TargetLabel>) -> core::result::Result<Self, radroots_transport::error::Error> pub fn radroots_transport::target::Target::nostr_relay(impl core::convert::AsRef<str>) -> core::result::Result<Self, radroots_transport::error::Error> @@ -594,6 +596,7 @@ impl<'de> serde_core::de::Deserialize<'de> for radroots_transport::target::Targe pub fn radroots_transport::target::TargetSet::deserialize<D>(D) -> core::result::Result<Self, <D as serde_core::de::Deserializer>::Error> where D: serde_core::de::Deserializer<'de> pub const radroots_transport::target::TARGET_SET_MAX_ITEMS: usize pub enum radroots_transport::Error +pub radroots_transport::Error::DeliveryReceiptPayloadMismatch pub radroots_transport::Error::DeliveryReceiptRequestIdMismatch pub radroots_transport::Error::DeliveryReceiptTargetSetMismatch pub radroots_transport::Error::DeliveryTargetReceiptAttemptMismatch @@ -798,6 +801,7 @@ pub fn radroots_transport::target::Target::kind(&self) -> &radroots_transport::T pub fn radroots_transport::target::Target::label(&self) -> core::option::Option<&radroots_transport::target::TargetLabel> pub fn radroots_transport::target::Target::local(impl core::convert::AsRef<str>) -> core::result::Result<Self, radroots_transport::error::Error> pub fn radroots_transport::target::Target::local_with_metadata(impl core::convert::AsRef<str>, core::option::Option<radroots_transport::target::TargetScope>, core::option::Option<radroots_transport::target::TargetLabel>) -> core::result::Result<Self, radroots_transport::error::Error> +pub fn radroots_transport::target::Target::matches_nostr_relay(&self, &str) -> core::result::Result<bool, radroots_transport::error::Error> pub fn radroots_transport::target::Target::new(radroots_transport::TransportId, impl core::convert::AsRef<str>) -> core::result::Result<Self, radroots_transport::error::Error> pub fn radroots_transport::target::Target::new_with_metadata(radroots_transport::TransportId, impl core::convert::AsRef<str>, core::option::Option<radroots_transport::target::TargetScope>, core::option::Option<radroots_transport::target::TargetLabel>) -> core::result::Result<Self, radroots_transport::error::Error> pub fn radroots_transport::target::Target::nostr_relay(impl core::convert::AsRef<str>) -> core::result::Result<Self, radroots_transport::error::Error> diff --git a/contracts/protocol/chat.v1.json b/contracts/protocol/chat.v1.json @@ -21,6 +21,11 @@ "radroots", "thread", "thread_id" + ], + [ + "radroots", + "operation", + "operation_id" ] ], "optional_tags_in_order": [ @@ -38,6 +43,7 @@ ] ], "unsigned": true, + "logical_identity": "The exact captured full operation ID is an unsigned-rumor discriminator inside the private encrypted message. Distinct operations with identical author/text/thread/time remain distinct; replay uses the original operation and captured time. The tag grants no authority.", "complete_max_bytes": 24576, "plain_text_max_bytes": 4096, "recipient_set": "Sorted unique author and peer; one exact rumor for every per-recipient seal, including a deduplicated self-conversation", @@ -95,7 +101,7 @@ "protected_data_part_max_bytes": 16384, "max_data_parts": 24, "protected_total_max_bytes": 2097152, - "native_workspace_schema": 6, + "native_workspace_schema": 7, "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 +122,8 @@ "max_data_parts": 4, "protected_total_max_bytes": 1048576, "capture_queue_reservation_bytes": 262144, - "schema": 6, - "schema_objects": 16, + "schema": 7, + "schema_objects": 20, "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 +131,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": 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", + "native_schema": 7, + "transaction": "Authenticate complete original T0/T1/S/T2/D and protected target observations outside transaction; recheck current grant, retained and absent facts and every bounded graph count 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.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", + "memory": "One complete owned plan, at most two recipients, bounded original delivery summaries and target observations. Snapshot/receipt references omit duplicate raw envelope bytes; owning graph verification remains bounded. Diagnostics are redacted.", + "progress_digest": "SHA-256 with radroots.operation.snapshot.v3 NUL separator; original v2 dimensions plus complete original delivery intent, exact outer representation digest, transaction/time/target ordinals, and original target outcome/retry/detail/nullable time. Query time and diagnostic correlation remain excluded.", "dimensions": [ "durable_admission", "business_installation", @@ -141,8 +147,8 @@ "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": "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." + "remaining": "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. D excess sentinels are 385 attempts, 9217 frame parts and 2305 target observations; at most 384 actual target slots and 2304 independent observation classes." }, "native_installation": { "state": "native_t2_component_implemented", @@ -160,12 +166,12 @@ "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, + "schema": 7, + "schema_objects": 20, "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", + "reporting": "Owned message only with scoped actual installation report; original installation and independent recipient work transactions. Actual D observations remain separate; no relay fact is inferred from T2.", "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" + "remaining": "Ordinary library-owned inbox jobs, browser/bindings, supported consumer adoption and all final gates" }, "outer_wrapping": { "state": "retained_seal_crypto_component_implemented", @@ -183,5 +189,50 @@ }, "authority": "Construction authenticates a seal and creates an outer representation; it proves no grant, route, operation linkage, durable capture, scheduling lease or delivery", "remaining": "Protected native exact-envelope and target-intent retention before transport, bound observations, ordinary owned inbox jobs, browser/binding runtime and final acceptance" + }, + "native_delivery": { + "state": "native_retention_and_observation_component_implemented", + "owner": "Existing bounded native workspace connection worker; no additional engine", + "schema": 7, + "schema_objects": 20, + "retention": "Verify actual T0/T1/S/T2 and selected signed kind10050 provenance. Derive and verify temporary outer key, decrypt the signed kind1059 against exact original retained seal bytes, then atomically protect exact signed outer and complete target intent before any transport.", + "temporary_material": "Borrowed temporary wrapping key is used only for independent containment verification; it is never persisted or treated as participant commitment authority. Original actor, peer and selected-recipient keys are all refused as temporary outer authors, including for self-copy publication work.", + "route_identity": "Authenticate the original signed kind10050 source and exact selected strings without replacement. Compare actual retained Nostr targets with those captured strings through Target::matches_nostr_relay, using the target's original construction policy and shared canonical URI rules. Root slash, host case and default-port aliases preserve endpoint identity; matching grants no network permission or new route.", + "intent": "Full 256-bit delivery request ID, original actual publication/capture/plan linkage, exact outer ID and representation digest, full original canonical target set and Accepted-only satisfaction policy, original per-attempt deadline, independent nonempty exact target selection and selected-endpoint potential invocation ordinals.", + "attempts": "At most twelve potential invocations per recipient/canonical endpoint. Changing target scope does not reset this budget. Exact replay returns retained original data with no dispatch right or budget increment. A newly retained retry may reuse only the original exact bytes, with no new participant signature.", + "observation": "Original credential and actual attempt/target/event/representation linkage required. No fresh clock, deadline, revocation or scheduling lease admission. First original observation of each independent class is immutable, including null time; later classes append.", + "classes": [ + "accepted", + "rejected", + "timed_out", + "disconnected", + "unknown", + "not_attempted" + ], + "receipt_binding": "Generic successful and partial-failure receipts require SHA-256 of exact original signed JSON bytes in addition to original request ID and targets. Relay acceptance never proves recipient delivery.", + "bounds": { + "signed_outer_bytes": 65536, + "targets": 16, + "endpoint_bytes": 2048, + "attempts_per_endpoint": 12, + "operation_target_slots": 384, + "observation_classes_per_slot": 6, + "operation_observations": 2304, + "frame_bytes": 393216, + "frame_parts": 24, + "private_part_bytes": 16384, + "protected_row_bytes": 262144, + "protected_frame_aggregate_bytes": 2097152, + "live_queue_bytes": 4194304, + "prepare_input_reservation_bytes": 1048576, + "observation_input_reservation_bytes": 98304 + }, + "completion": "Confirmed commit, known rollback and original anchored unknown remain distinct. Accepted owner work survives lost observers and drains before close. Missing ACKs, transport errors and uncertain D completion never reverse T0/T2 or release S claims.", + "startup": "Current access is required for coherent queries. Reopen and restored projections never publish automatically or renew scheduling rights. Preserve and refuse predecessor schema6 namespaces without automatic upgrade.", + "qualification": "Exact-source executed owning checks are required separately; constructors, declarations, valid metadata and a custom adapter report confer no qualification.", + "remaining": "Ordinary library-owned inbox job orchestration, complete provider/route approval at dispatch, browser/bindings, supported consumer adoption and final acceptance", + "publication_policy": "Freeze the first full target identity set and satisfaction criterion per actual recipient work. A retry or rewrapping cannot narrow or replace it. Evaluate original full-target acceptance across all retained exact attempt observations; an earlier acceptance remains authoritative.", + "selection": "Only selected endpoints spend potential-invocation slots or produce bound target observations. The host uses EventSink::deliver_selected with the full original request and exact retained subset; unselected endpoints are not invoked. Exhausted optional endpoints do not starve required endpoints. Quorum success may accumulate across selected retries.", + "deadline": "A matching request ID retains its original deadline. A distinct current-access attempt may capture a new delivery deadline without altering the full publication criterion, original operation installation or participant issuance facts." } } diff --git a/crates/execution/README.md b/crates/execution/README.md @@ -116,3 +116,13 @@ 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; the Storage-backed projection is a separately selected host feature. + +The D projection retains original outer/target intent summaries and independent +target observations, including the full request ID, target fingerprint and +endpoint attempt number. Unobserved retained potential invocations remain +unknown; observed rejection or timeout does not become acceptance. Accepted +evidence is evaluated with the shared original target policy, independently for +each publication work item and across its independently selected retries, +without erasing another recipient's pending state or weakening the full policy. +These projections perform no transport or participant signer invocation and +never renew a scheduling lease. diff --git a/crates/execution/src/receipt.rs b/crates/execution/src/receipt.rs @@ -93,6 +93,8 @@ pub enum TargetObservation { Disconnected, /// Completion requires reconciliation. Unknown, + /// The adapter explicitly reports that it did not attempt this target. + NotAttempted, } macro_rules! copied { ($name:ident, $ty:ty, $field:ident, $doc:literal) => { @@ -198,6 +200,9 @@ pub struct DeliveryObservation { event: EnvelopeEventId, recipient: Option<PublicKey>, target: Box<str>, + request: Box<str>, + target_fingerprint: Box<str>, + ordinal: u8, outcome: TargetObservation, observed_at: Option<NonZeroU64>, } @@ -230,6 +235,18 @@ impl DeliveryObservation { pub fn target(&self) -> &str { &self.target } + /// Full original retained potential invocation identity. + pub fn request_id(&self) -> &str { + &self.request + } + /// Exact canonical target identity, independent of display labels. + pub fn target_fingerprint(&self) -> &str { + &self.target_fingerprint + } + /// Original endpoint potential invocation number, from one through twelve. + pub const fn target_ordinal(&self) -> u8 { + self.ordinal + } } impl fmt::Debug for DeliveryObservation { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -262,11 +279,19 @@ pub struct OperationReceipt<T> { issuance: Issuance, delivery: Delivery, delivery_observations: Box<[DeliveryObservation]>, + #[cfg(feature = "receipts")] + delivery_attempts: Box<[radroots_storage::command::DeliveryAttemptSummary]>, wait_termination: WaitTermination, recovery: Box<[RecoveryRequirement]>, error: Option<Error>, } impl<T> OperationReceipt<T> { + /// Complete original outer/target intent references. These contain no raw + /// payload and grant no dispatch permission or adapter qualification. + #[cfg(feature = "receipts")] + pub fn delivery_attempts(&self) -> &[radroots_storage::command::DeliveryAttemptSummary] { + &self.delivery_attempts + } /// Original captured data; this is not an authenticated executable context. pub const fn context(&self) -> &CommandContextData { &self.context @@ -477,19 +502,68 @@ impl OperationReceipt<ChatMessage> { && issuance != Issuance::Obtained && snapshot.observed_at() >= prepared.issuance_window().first_issue_not_after(); let mut recovery = Vec::new(); + let observations = snapshot.delivery_observations(); + let attempts = snapshot.delivery_attempts(); + let delivery_pending = snapshot + .publications() + .iter() + .any(|work| snapshot.publication_accepted(*work) != Ok(true)); + let delivery_unknown = attempts.iter().any(|attempt| { + attempt.intent().selected_targets().any(|target| { + !observations.iter().any(|value| { + value.work() == attempt.work() + && value.request_id() == attempt.intent().request_id() + && value.target_receipt().target() == target + && value.kind() + != radroots_storage::command::DeliveryObservationKind::Unknown + }) + }) + }); + let projected = observations + .iter() + .map(|value| { + use radroots_storage::command::DeliveryObservationKind as Kind; + DeliveryObservation { + event: value.event_id(), + recipient: Some(value.work().evidence().recipient()), + target: value.target_receipt().target().uri().as_str().into(), + request: value.request_id().as_str().into(), + target_fingerprint: value + .target_receipt() + .target() + .fingerprint() + .as_str() + .into(), + ordinal: value.target_ordinal(), + observed_at: value.observed_at_unix_ms(), + outcome: match value.kind() { + Kind::Accepted => TargetObservation::Accepted, + Kind::Rejected => TargetObservation::Rejected, + Kind::TimedOut => TargetObservation::TimedOut, + Kind::Disconnected => TargetObservation::Disconnected, + Kind::Unknown => TargetObservation::Unknown, + Kind::NotAttempted => TargetObservation::NotAttempted, + }, + } + }) + .collect::<Vec<_>>(); if unknown { recovery.push(RecoveryRequirement::ReconcileIssuance); } if install_pending { recovery.push(RecoveryRequirement::InstallObtainedEvidence); } - if !snapshot.publications().is_empty() { + if delivery_pending { recovery.push(RecoveryRequirement::ResumeDelivery); } if expired { recovery.push(RecoveryRequirement::ManualReconciliation); } - if recovery.is_empty() { + if recovery.is_empty() + && (snapshot.installation().is_none() + || issuance == Issuance::Pending + || issuance == Issuance::None) + { recovery.push(RecoveryRequirement::ContinueOriginalOperation); } Self { @@ -518,10 +592,20 @@ impl OperationReceipt<ChatMessage> { issuance, delivery: if snapshot.publications().is_empty() { Delivery::NotStarted - } else { + } else if delivery_unknown { + Delivery::Unknown + } else if attempts.is_empty() + || snapshot + .publications() + .iter() + .any(|work| !attempts.iter().any(|attempt| attempt.work() == *work)) + { Delivery::Pending + } else { + Delivery::Observed }, - delivery_observations: vec![].into_boxed_slice(), + delivery_observations: projected.into_boxed_slice(), + delivery_attempts: attempts.to_vec().into_boxed_slice(), wait_termination: WaitTermination::Returned, recovery: recovery.into_boxed_slice(), error: if unknown { diff --git a/crates/protocol/README.md b/crates/protocol/README.md @@ -5,6 +5,12 @@ for the exact already obtained seal representation. Whitespace in retained signed JSON still consumes encryption and event budget; canonical event identity does not permit replacing those bytes during recovery. +Authored chat rumors carry the original full operation ID in a private +`radroots/operation` tag. Identical messages from distinct operations in one +second therefore have distinct NIP-01 message identities. Replaying the +original operation preserves its captured time, exact rumor and message ID; +the discriminator supplies no authorization. + `radroots_protocol` defines the passive, versioned wire contracts shared by Radroots processes and language bindings. It is a `no_std + alloc` foundation crate: it validates schema identities and contract catalogs, but performs no diff --git a/crates/protocol/src/chat.rs b/crates/protocol/src/chat.rs @@ -145,6 +145,11 @@ impl ChatRumor { String::from("thread"), command.thread_id().to_string(), ], + vec![ + String::from("radroots"), + String::from("operation"), + command.context().operation_id().to_string(), + ], ]; if let Some(source) = command.source() { tags.push(vec![ diff --git a/crates/protocol/tests/chat_identity.rs b/crates/protocol/tests/chat_identity.rs @@ -0,0 +1,23 @@ +#![cfg(feature = "chat")] +use radroots_domain::UnixSeconds; +use radroots_protocol::{chat::ChatRumor, command::CapturedChatCommand}; +use serde_json::Value; + +#[test] +fn distinct_operations_with_identical_text_in_one_second_have_distinct_logical_messages() { + let vectors: Value = + serde_json::from_str(include_str!("fixtures/chat_commands.v1.json")).unwrap(); + let mut source: Value = + serde_json::from_str(vectors["vectors"][0]["wire"].as_str().unwrap()).unwrap(); + let first = CapturedChatCommand::parse(source.to_string().as_bytes()).unwrap(); + source["context"]["operation_id"] = "42".repeat(32).into(); + let second = CapturedChatCommand::parse(source.to_string().as_bytes()).unwrap(); + assert_ne!(first.digest(), second.digest()); + let captured = UnixSeconds::new(1_700_000_000).unwrap(); + let original = ChatRumor::from_command(&first, captured).unwrap(); + let replay = ChatRumor::from_command(&first, captured).unwrap(); + let distinct = ChatRumor::from_command(&second, captured).unwrap(); + assert_eq!(original.json(), replay.json()); + assert_eq!(original.message_id(), replay.message_id()); + assert_ne!(original.message_id(), distinct.message_id()); +} diff --git a/crates/protocol/tests/fixtures/chat_wire.v1.json b/crates/protocol/tests/fixtures/chat_wire.v1.json @@ -6,61 +6,61 @@ { "name": "inquiry_unicode", "wire": "{\"context\":{\"actor\":{\"grant_id\":\"0202020202020202020202020202020202020202020202020202020202020202\",\"organization\":null,\"participant\":\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",\"role\":\"buyer\"},\"approval\":null,\"authority\":{\"executor_id\":\"0404040404040404040404040404040404040404040404040404040404040404\",\"generation\":\"9007199254740993\"},\"correlation_id\":\"first-observer\",\"expected\":null,\"not_after\":\"200\",\"operation_id\":\"0101010101010101010101010101010101010101010101010101010101010101\",\"route_digest\":\"0505050505050505050505050505050505050505050505050505050505050505\",\"workspace_id\":\"0303030303030303030303030303030303030303030303030303030303030303\"},\"input\":{\"peer\":\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\",\"source\":null,\"text\":\"private 🥬 \\u000f \\n Å Å 
 🧺\",\"thread_id\":\"0606060606060606060606060606060606060606060606060606060606060606\"},\"type\":\"inbox.inquire\"}", - "preimage": "[0,\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",1700000000,14,[[\"p\",\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\"],[\"radroots\",\"thread\",\"0606060606060606060606060606060606060606060606060606060606060606\"]],\"private 🥬 \\u000f \\n Å Å 
 🧺\"]", - "rumor": "{\"id\":\"e5b879b60760f36c51395c24bddf454f508b81f51373de1dc75211cede535363\",\"pubkey\":\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",\"created_at\":1700000000,\"kind\":14,\"tags\":[[\"p\",\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\"],[\"radroots\",\"thread\",\"0606060606060606060606060606060606060606060606060606060606060606\"]],\"content\":\"private 🥬 \\u000f \\n Å Å 
 🧺\"}", - "message_id": "e5b879b60760f36c51395c24bddf454f508b81f51373de1dc75211cede535363", + "preimage": "[0,\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",1700000000,14,[[\"p\",\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\"],[\"radroots\",\"thread\",\"0606060606060606060606060606060606060606060606060606060606060606\"],[\"radroots\",\"operation\",\"0101010101010101010101010101010101010101010101010101010101010101\"]],\"private 🥬 \\u000f \\n Å Å 
 🧺\"]", + "rumor": "{\"id\":\"bdefd9afad055871a21f3c21b8f8bdae82afd2f06adee033c06aa8229b02e7e5\",\"pubkey\":\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",\"created_at\":1700000000,\"kind\":14,\"tags\":[[\"p\",\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\"],[\"radroots\",\"thread\",\"0606060606060606060606060606060606060606060606060606060606060606\"],[\"radroots\",\"operation\",\"0101010101010101010101010101010101010101010101010101010101010101\"]],\"content\":\"private 🥬 \\u000f \\n Å Å 
 🧺\"}", + "message_id": "bdefd9afad055871a21f3c21b8f8bdae82afd2f06adee033c06aa8229b02e7e5", "recipients": [ "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798", "c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5" ], - "structural_ciphertext": "AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=", - "seal_preimage": "[0,\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",1699999990,13,[],\"AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\"]", - "seal_id": "a69029d41dcd5a0ba1c44fd6ff4c5b9aa41988a4a3aa4b3ee542ff3ddffb8f1a", - "signed_seal_bytes": 1031 + "structural_ciphertext": "AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", + "seal_preimage": "[0,\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",1699999990,13,[],\"AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"]", + "seal_id": "6d06df3b5496dc1cfd8207590f8196911ef374a3f49d63238c2a4cb5901be1da", + "signed_seal_bytes": 1115 }, { "name": "inquiry_source_expected", "wire": "{\"context\":{\"actor\":{\"grant_id\":\"0202020202020202020202020202020202020202020202020202020202020202\",\"organization\":null,\"participant\":\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",\"role\":\"buyer\"},\"approval\":null,\"authority\":{\"executor_id\":\"0404040404040404040404040404040404040404040404040404040404040404\",\"generation\":\"9007199254740993\"},\"correlation_id\":\"first-observer\",\"expected\":{\"agreement\":null,\"buyer_head\":null,\"checkpoint\":{\"coverage\":\"partial\",\"digest\":\"0808080808080808080808080808080808080808080808080808080808080808\",\"missing\":[\"0909090909090909090909090909090909090909090909090909090909090909\"],\"sequence\":\"9007199254740993\"},\"order_id\":\"0707070707070707070707070707070707070707070707070707070707070707\",\"seller_sequence\":\"9007199254740993\"},\"not_after\":\"200\",\"operation_id\":\"0101010101010101010101010101010101010101010101010101010101010101\",\"route_digest\":\"0505050505050505050505050505050505050505050505050505050505050505\",\"workspace_id\":\"0303030303030303030303030303030303030303030303030303030303030303\"},\"input\":{\"peer\":\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\",\"source\":{\"coordinate\":{\"author\":\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\",\"d\":\"55555555555555555555555555555555\",\"kind\":30402},\"event_id\":\"0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a\"},\"text\":\"A public reference does not prove terms or stock.\",\"thread_id\":\"0606060606060606060606060606060606060606060606060606060606060606\"},\"type\":\"inbox.inquire\"}", - "preimage": "[0,\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",1700000000,14,[[\"p\",\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\"],[\"radroots\",\"thread\",\"0606060606060606060606060606060606060606060606060606060606060606\"],[\"radroots\",\"source\",\"30402\",\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\",\"55555555555555555555555555555555\",\"0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a\"]],\"A public reference does not prove terms or stock.\"]", - "rumor": "{\"id\":\"87ef1e8f05137848323d95ea44f9d12148eb36f94d7d08bec12a3c5757694bfd\",\"pubkey\":\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",\"created_at\":1700000000,\"kind\":14,\"tags\":[[\"p\",\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\"],[\"radroots\",\"thread\",\"0606060606060606060606060606060606060606060606060606060606060606\"],[\"radroots\",\"source\",\"30402\",\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\",\"55555555555555555555555555555555\",\"0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a\"]],\"content\":\"A public reference does not prove terms or stock.\"}", - "message_id": "87ef1e8f05137848323d95ea44f9d12148eb36f94d7d08bec12a3c5757694bfd", + "preimage": "[0,\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",1700000000,14,[[\"p\",\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\"],[\"radroots\",\"thread\",\"0606060606060606060606060606060606060606060606060606060606060606\"],[\"radroots\",\"operation\",\"0101010101010101010101010101010101010101010101010101010101010101\"],[\"radroots\",\"source\",\"30402\",\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\",\"55555555555555555555555555555555\",\"0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a\"]],\"A public reference does not prove terms or stock.\"]", + "rumor": "{\"id\":\"b5f99a9981140eeb742c8d3e4ebf9edb1e3dee5e28cddf17b06ad6dc6cf3f6b1\",\"pubkey\":\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",\"created_at\":1700000000,\"kind\":14,\"tags\":[[\"p\",\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\"],[\"radroots\",\"thread\",\"0606060606060606060606060606060606060606060606060606060606060606\"],[\"radroots\",\"operation\",\"0101010101010101010101010101010101010101010101010101010101010101\"],[\"radroots\",\"source\",\"30402\",\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\",\"55555555555555555555555555555555\",\"0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a\"]],\"content\":\"A public reference does not prove terms or stock.\"}", + "message_id": "b5f99a9981140eeb742c8d3e4ebf9edb1e3dee5e28cddf17b06ad6dc6cf3f6b1", "recipients": [ "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798", "c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5" ], - "structural_ciphertext": "AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=", - "seal_preimage": "[0,\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",1699999990,13,[],\"AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\"]", - "seal_id": "4d8a27e6d22027eed214055d931d58fcc03f31afdec002ed107172b12bcce96f", - "signed_seal_bytes": 1287 + "structural_ciphertext": "AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==", + "seal_preimage": "[0,\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",1699999990,13,[],\"AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==\"]", + "seal_id": "b13e6954d2acc59331c0084faaa9c76e2c2015300cd35e2476306040bbb92ef2", + "signed_seal_bytes": 1459 }, { "name": "reply", "wire": "{\"context\":{\"actor\":{\"grant_id\":\"0202020202020202020202020202020202020202020202020202020202020202\",\"organization\":null,\"participant\":\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",\"role\":\"buyer\"},\"approval\":null,\"authority\":{\"executor_id\":\"0404040404040404040404040404040404040404040404040404040404040404\",\"generation\":\"9007199254740993\"},\"correlation_id\":null,\"expected\":null,\"not_after\":\"200\",\"operation_id\":\"0101010101010101010101010101010101010101010101010101010101010101\",\"route_digest\":\"0505050505050505050505050505050505050505050505050505050505050505\",\"workspace_id\":\"0303030303030303030303030303030303030303030303030303030303030303\"},\"input\":{\"parent\":\"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b\",\"peer\":\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\",\"text\":\"reply\",\"thread_id\":\"0606060606060606060606060606060606060606060606060606060606060606\"},\"type\":\"inbox.reply\"}", - "preimage": "[0,\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",1700000000,14,[[\"p\",\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\"],[\"radroots\",\"thread\",\"0606060606060606060606060606060606060606060606060606060606060606\"],[\"e\",\"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b\"]],\"reply\"]", - "rumor": "{\"id\":\"4d1680b430b9efa51ed1dfc97783a4624f51f91a6ced924284b31a42e77b8213\",\"pubkey\":\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",\"created_at\":1700000000,\"kind\":14,\"tags\":[[\"p\",\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\"],[\"radroots\",\"thread\",\"0606060606060606060606060606060606060606060606060606060606060606\"],[\"e\",\"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b\"]],\"content\":\"reply\"}", - "message_id": "4d1680b430b9efa51ed1dfc97783a4624f51f91a6ced924284b31a42e77b8213", + "preimage": "[0,\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",1700000000,14,[[\"p\",\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\"],[\"radroots\",\"thread\",\"0606060606060606060606060606060606060606060606060606060606060606\"],[\"radroots\",\"operation\",\"0101010101010101010101010101010101010101010101010101010101010101\"],[\"e\",\"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b\"]],\"reply\"]", + "rumor": "{\"id\":\"e38a1b9d6a77387bb0933598fd77113387f92e9dd9e9b2f2dd95445bdf8207aa\",\"pubkey\":\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",\"created_at\":1700000000,\"kind\":14,\"tags\":[[\"p\",\"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5\"],[\"radroots\",\"thread\",\"0606060606060606060606060606060606060606060606060606060606060606\"],[\"radroots\",\"operation\",\"0101010101010101010101010101010101010101010101010101010101010101\"],[\"e\",\"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b\"]],\"content\":\"reply\"}", + "message_id": "e38a1b9d6a77387bb0933598fd77113387f92e9dd9e9b2f2dd95445bdf8207aa", "recipients": [ "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798", "c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5" ], - "structural_ciphertext": "AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=", - "seal_preimage": "[0,\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",1699999990,13,[],\"AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\"]", - "seal_id": "a69029d41dcd5a0ba1c44fd6ff4c5b9aa41988a4a3aa4b3ee542ff3ddffb8f1a", - "signed_seal_bytes": 1031 + "structural_ciphertext": "AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=", + "seal_preimage": "[0,\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",1699999990,13,[],\"AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\"]", + "seal_id": "4d8a27e6d22027eed214055d931d58fcc03f31afdec002ed107172b12bcce96f", + "signed_seal_bytes": 1287 }, { "name": "self_chat", "wire": "{\"context\":{\"actor\":{\"grant_id\":\"0202020202020202020202020202020202020202020202020202020202020202\",\"organization\":null,\"participant\":\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",\"role\":\"buyer\"},\"approval\":null,\"authority\":{\"executor_id\":\"0404040404040404040404040404040404040404040404040404040404040404\",\"generation\":\"9007199254740993\"},\"correlation_id\":\"first-observer\",\"expected\":null,\"not_after\":\"200\",\"operation_id\":\"0101010101010101010101010101010101010101010101010101010101010101\",\"route_digest\":\"0505050505050505050505050505050505050505050505050505050505050505\",\"workspace_id\":\"0303030303030303030303030303030303030303030303030303030303030303\"},\"input\":{\"peer\":\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",\"source\":null,\"text\":\"private 🥬 \\u000f \\n Å Å 
 🧺\",\"thread_id\":\"0606060606060606060606060606060606060606060606060606060606060606\"},\"type\":\"inbox.inquire\"}", - "preimage": "[0,\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",1700000000,14,[[\"p\",\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\"],[\"radroots\",\"thread\",\"0606060606060606060606060606060606060606060606060606060606060606\"]],\"private 🥬 \\u000f \\n Å Å 
 🧺\"]", - "rumor": "{\"id\":\"836309fc7a3b433934dac0391d97218ac7e8f6398c4cd4a5c44a41b456122e8d\",\"pubkey\":\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",\"created_at\":1700000000,\"kind\":14,\"tags\":[[\"p\",\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\"],[\"radroots\",\"thread\",\"0606060606060606060606060606060606060606060606060606060606060606\"]],\"content\":\"private 🥬 \\u000f \\n Å Å 
 🧺\"}", - "message_id": "836309fc7a3b433934dac0391d97218ac7e8f6398c4cd4a5c44a41b456122e8d", + "preimage": "[0,\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",1700000000,14,[[\"p\",\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\"],[\"radroots\",\"thread\",\"0606060606060606060606060606060606060606060606060606060606060606\"],[\"radroots\",\"operation\",\"0101010101010101010101010101010101010101010101010101010101010101\"]],\"private 🥬 \\u000f \\n Å Å 
 🧺\"]", + "rumor": "{\"id\":\"5d5d01b142ce09d92d3438e7a7af9ff53ce4e57f75144037aecf186f5f44977e\",\"pubkey\":\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",\"created_at\":1700000000,\"kind\":14,\"tags\":[[\"p\",\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\"],[\"radroots\",\"thread\",\"0606060606060606060606060606060606060606060606060606060606060606\"],[\"radroots\",\"operation\",\"0101010101010101010101010101010101010101010101010101010101010101\"]],\"content\":\"private 🥬 \\u000f \\n Å Å 
 🧺\"}", + "message_id": "5d5d01b142ce09d92d3438e7a7af9ff53ce4e57f75144037aecf186f5f44977e", "recipients": [ "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798" ], - "structural_ciphertext": "AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=", - "seal_preimage": "[0,\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",1699999990,13,[],\"AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\"]", - "seal_id": "a69029d41dcd5a0ba1c44fd6ff4c5b9aa41988a4a3aa4b3ee542ff3ddffb8f1a", - "signed_seal_bytes": 1031 + "structural_ciphertext": "AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", + "seal_preimage": "[0,\"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\",1699999990,13,[],\"AgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"]", + "seal_id": "6d06df3b5496dc1cfd8207590f8196911ef374a3f49d63238c2a4cb5901be1da", + "signed_seal_bytes": 1115 } ], "budgets": [ diff --git a/crates/protocol/tests/support/chat_wire_oracle.mjs b/crates/protocol/tests/support/chat_wire_oracle.mjs @@ -29,7 +29,7 @@ commands.push(self); const vectors = commands.map(({ name, command }) => { const input = command.input; const author = command.context.actor.participant; - const tags = [["p", input.peer], ["radroots", "thread", input.thread_id]]; + const tags = [["p", input.peer], ["radroots", "thread", input.thread_id], ["radroots", "operation", command.context.operation_id]]; if (input.source) tags.push(["radroots", "source", "30402", input.source.coordinate.author, input.source.coordinate.d, input.source.event_id]); if (input.parent) tags.push(["e", input.parent]); const event = { pubkey: author, created_at: 1700000000, kind: 14, tags, content: input.text }; diff --git a/crates/radroots/Cargo.toml b/crates/radroots/Cargo.toml @@ -63,12 +63,14 @@ full = ["native", "radroots_sdk/full"] [dev-dependencies] radroots_protocol = { workspace = true, default-features = false, features = ["chat"] } radroots_signing = { workspace = true, features = ["chat"] } -nostr = { workspace = true, default-features = false, features = ["std", "nip44"] } +nostr = { workspace = true, default-features = false, features = ["std", "nip44", "nip59"] } sqlx = { workspace = true, features = ["runtime-tokio", "sqlite-bundled"] } radroots_secrets = { workspace = true, features = ["file"] } futures = { workspace = true } radroots_event_codec = { workspace = true, default-features = false } radroots_nostr = { workspace = true, features = ["signing", "nip17"] } +radroots_transport_nostr = { workspace = true } +tokio-tungstenite = { workspace = true } serde_json = { workspace = true, features = ["std"] } tempfile = { workspace = true } tokio = { workspace = true, features = ["macros", "rt"] } diff --git a/crates/radroots/tests/support/receipt_delivery.rs b/crates/radroots/tests/support/receipt_delivery.rs @@ -0,0 +1,662 @@ +use super::*; +use futures::{SinkExt, StreamExt}; +use radroots_nostr::{ + key::parse_secret_key, + signing::{LocalSigner, OsNonceSource, gift_wrap::wrap_retained_seal}, +}; +use radroots_signing::{ + issuance::{OutcomeKind, SigningIntent}, + provider::{ParticipantProvider, ProviderCall}, + request::CancellationSignal, +}; +use radroots_storage::command::{ + DeliveryAttempt, DeliveryAttemptOutcome, DeliveryAttemptRequest, DeliveryIntent, + DeliveryObservation, DeliveryObservationKind, DeliveryObservationOutcome, EnvelopeCandidate, + EvidenceCaptureOutcome, InstallationOutcome, InstallationRequest, PublicationWork, + SealEvidence, +}; +use radroots_transport::{ + EventSink, TargetSet, + policy::{SatisfactionClass, SatisfactionPolicy, TargetPolicy}, +}; +use radroots_transport_nostr::{ + Config, NostrTransport, RelayAccess, RelayEndpoint, RelayProfile, RelayProfileKind, + RelayUrlPolicy, +}; +use std::{ + num::NonZeroU64, + time::{SystemTime, UNIX_EPOCH}, +}; +use tokio::net::TcpListener; +use tokio_tungstenite::{accept_async, tungstenite::Message}; +#[path = "receipt_delivery_admission.rs"] +mod admission_validation; +#[path = "receipt_delivery_faults.rs"] +mod faults; +#[path = "receipt_delivery_validation.rs"] +mod validation; + +fn now_ms() -> u64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_millis() + .try_into() + .unwrap() +} +fn signer_now() -> Result<u64, radroots_signing::Error> { + Ok(now_ms()) +} +struct Ready { + fixture: Fixture, + store: Arc<NativeWorkspace>, + input: AttachmentInput, + proof: AttachmentProof, + binding: WorkspaceBinding, + work: PublicationWork, + candidate: EnvelopeCandidate, + config: Config, +} +impl Ready { + async fn new(urls: &[String], id: u8) -> Self { + let fixture = Fixture::new(); + let time = now_ms() / 1000; + fixture.clock.now.store(time, Ordering::SeqCst); + let (store, input, proof, binding) = fixture.create_with_expiry(time + 600).await; + let mut value: serde_json::Value = serde_json::from_slice( + request_until( + &binding, + id, + "one exact native delivery", + "delivery", + time + 120, + ) + .command() + .protected_wire(), + ) + .unwrap(); + value["input"]["peer"] = keys(2).public_key().to_hex().into(); + let admitted = AdmissionRequest::from_protected_wire( + binding.clone(), + &serde_json::to_vec(&value).unwrap(), + ) + .unwrap(); + let prepared = committed( + store + .admit_command_and_hold(admitted, copy(&proof)) + .await + .unwrap(), + ); + let plan = plan_with_routes(prepared.request().command(), time, "delivery", Some(urls)); + let digest = *plan.digest(); + let recipient = prepared.request().command().peer(); + bound( + store + .bind_signing_intents(binding.clone(), plan, copy(&proof)) + .await + .unwrap(), + ); + let claim = issuance::claim( + store + .claim_attempt( + issuance::selection(&binding, &prepared, digest, recipient), + copy(&proof), + ) + .await + .unwrap(), + ); + let signer = LocalSigner::with_clock( + parse_secret_key("0000000000000000000000000000000000000000000000000000000000000001") + .unwrap(), + signer_now, + ) + .unwrap() + .with_nonce_source(Arc::new(OsNonceSource)); + let intent = SigningIntent::from_chat_seal( + claim.binding().operation_id(), + prepared.identity().command_digest(), + claim.seal(), + ProviderCall::new(key(), claim.deadline_unix_ms(), CancellationSignal::new()), + ) + .unwrap(); + let (_, result) = signer.sign_exact(intent).await.into_parts(); + let OutcomeKind::Obtained(artifact) = result else { + panic!("synthetic participant seal must be obtained") + }; + let seal = artifact.with_json(|raw| { + SealEvidence::verify(&claim, raw, artifact.observed_at_unix_ms()).unwrap() + }); + assert!(matches!( + store + .capture_evidence(input.clone(), &proof, &seal) + .await + .unwrap(), + EvidenceCaptureOutcome::Committed(_) + )); + assert!(matches!( + store + .install_transition_and_outbox( + InstallationRequest::new( + binding.clone(), + prepared.identity().operation_id(), + prepared.identity().command_digest(), + digest + ), + copy(&proof) + ) + .await + .unwrap(), + InstallationOutcome::Committed(_) + )); + let snapshot = store + .operation_snapshot( + binding.clone(), + prepared.identity().operation_id(), + None, + copy(&proof), + ) + .await + .unwrap() + .unwrap(); + let work = snapshot.publications()[0]; + let pending = wrap_retained_seal( + seal.raw_json(), + recipient, + NonZeroU64::new(time).unwrap(), + &OsNonceSource, + ) + .unwrap(); + let candidate = pending.with_wrapping_key(|key| { + EnvelopeCandidate::from_obtained(work, pending.signed_json(), key).unwrap() + }); + let config = Config::from_profile( + RelayProfile::explicit( + RelayProfileKind::Simulator, + urls.iter().map(|url| { + RelayEndpoint::new(url, RelayUrlPolicy::Local, RelayAccess::ReadWrite).unwrap() + }), + ) + .unwrap(), + ) + .with_timeouts(1000, 1000, 500) + .unwrap(); + Self { + fixture, + store, + input, + proof, + binding, + work, + candidate, + config, + } + } + fn request(&self, id: u8) -> DeliveryAttemptRequest { + let targets = TargetSet::new( + self.config + .relays() + .iter() + .map(|relay| relay.to_target().unwrap()) + .collect(), + ) + .unwrap(); + let intent = DeliveryIntent::new( + &format!("{id:02x}").repeat(32), + self.candidate.event_id(), + &targets, + &SatisfactionPolicy::new(SatisfactionClass::Accepted, TargetPolicy::all()), + NonZeroU64::new(self.work.queued_at().get() * 1000 + 60_000).unwrap(), + ) + .unwrap(); + DeliveryAttemptRequest::new(self.binding.clone(), self.work, intent).unwrap() + } + async fn retain(&self, id: u8) -> DeliveryAttempt { + match self + .store + .prepare_delivery_attempt(self.request(id), copy(&self.proof), Some(&self.candidate)) + .await + .unwrap() + { + DeliveryAttemptOutcome::Claimed(value) => *value, + _ => panic!("new actual retained delivery expected"), + } + } + async fn query( + &self, + store: &NativeWorkspace, + id: &radroots_transport::sink::DeliveryRequestId, + ) -> DeliveryAttempt { + let e = self.work.evidence(); + store + .delivery_attempt( + self.binding.clone(), + e.operation_id(), + e.command_digest(), + e.recipient(), + id.clone(), + copy(&self.proof), + ) + .await + .unwrap() + .unwrap() + } + async fn observations( + &self, + store: &NativeWorkspace, + id: &radroots_transport::sink::DeliveryRequestId, + ) -> Vec<DeliveryObservation> { + let e = self.work.evidence(); + store + .delivery_observations( + self.binding.clone(), + e.operation_id(), + e.command_digest(), + e.recipient(), + id.clone(), + copy(&self.proof), + ) + .await + .unwrap() + } +} +fn captured(outcome: DeliveryObservationOutcome) -> DeliveryObservation { + match outcome { + DeliveryObservationOutcome::Committed(value) => value, + _ => panic!("confirmed original observation expected"), + } +} +#[tokio::test] +async fn native_delivery_retains_exact_outer_before_loopback_and_reopen_preserves_original_observation() + { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let ready = Ready::new(&[format!("ws://{}", listener.local_addr().unwrap())], 230).await; + let request = ready.request(1); + let original_intent = request.intent().clone(); + let DeliveryAttemptOutcome::Claimed(attempt) = ready + .store + .prepare_delivery_attempt(request, copy(&ready.proof), Some(&ready.candidate)) + .await + .unwrap() + else { + panic!("new retention required") + }; + let replay = ready + .store + .prepare_delivery_attempt( + DeliveryAttemptRequest::new(ready.binding.clone(), ready.work, original_intent) + .unwrap(), + copy(&ready.proof), + None, + ) + .await + .unwrap(); + assert!(matches!(replay, DeliveryAttemptOutcome::Retained(_))); + let expected = format!("[\"EVENT\",{}]", attempt.raw_json()); + let e = ready.work.evidence(); + let pending = ready + .store + .operation_snapshot( + ready.binding.clone(), + e.operation_id(), + None, + copy(&ready.proof), + ) + .await + .unwrap() + .unwrap(); + assert_eq!(pending.delivery_attempts().len(), 1); + assert!(pending.delivery_observations().is_empty()); + let projected = radroots_execution::OperationReceipt::from_snapshot(&pending); + assert_eq!(projected.delivery(), radroots_execution::Delivery::Unknown); + assert_eq!( + projected.delivery_attempts()[0].envelope_digest(), + attempt.envelope_digest() + ); + let expected_message = pending.installed_message().unwrap().id(); + let server = tokio::spawn(async move { + let (tcp, _) = listener.accept().await.unwrap(); + let mut socket = accept_async(tcp).await.unwrap(); + let raw = tokio::time::timeout(Duration::from_secs(5), async { + loop { + if let Message::Text(raw) = socket.next().await.unwrap().unwrap() { + break raw.to_string(); + } + } + }) + .await + .unwrap(); + assert_eq!(raw, expected); + let event: serde_json::Value = serde_json::from_str(&raw).unwrap(); + let outer = nostr::Event::from_json(event[1].to_string()).unwrap(); + let recovered = nostr::nips::nip59::extract_rumor(&keys(2), &outer) + .await + .unwrap(); + assert_eq!(recovered.sender, keys(1).public_key()); + assert_eq!( + recovered.rumor.id.unwrap().as_bytes(), + expected_message.as_bytes() + ); + assert_eq!(recovered.rumor.content, "one exact native delivery"); + socket + .send(Message::Text( + json!(["OK", "ff".repeat(32), true, ""]).to_string().into(), + )) + .await + .unwrap(); + socket + .send(Message::Text( + json!(["OK", event[1]["id"], true, ""]).to_string().into(), + )) + .await + .unwrap(); + }); + let transport = NostrTransport::new(ready.config.clone()); + let request = attempt.transport_request().unwrap(); + let receipt = transport.deliver(request.clone()).await.unwrap(); + receipt.validate_for_request(&request).unwrap(); + assert!(receipt.is_satisfied(&request).unwrap()); + server.await.unwrap(); + let target = request.target_set().targets()[0].fingerprint(); + let observed = DeliveryObservation::from_receipt(&attempt, &receipt, target, None).unwrap(); + assert_eq!(observed.kind(), DeliveryObservationKind::Accepted); + let first = captured( + ready + .store + .record_delivery_observation(ready.binding.clone(), copy(&ready.proof), observed) + .await + .unwrap(), + ); + assert_eq!(first.observed_at_unix_ms(), None); + let complete = ready + .store + .operation_snapshot( + ready.binding.clone(), + e.operation_id(), + None, + copy(&ready.proof), + ) + .await + .unwrap() + .unwrap(); + assert_ne!(complete.checkpoint_digest(), pending.checkpoint_digest()); + let projected = radroots_execution::OperationReceipt::from_snapshot(&complete); + assert_eq!( + projected.business_installation(), + radroots_execution::BusinessInstallation::Installed + ); + assert_eq!(projected.delivery(), radroots_execution::Delivery::Observed); + assert!( + !projected + .recovery() + .contains(&radroots_execution::RecoveryRequirement::ResumeDelivery) + ); + assert_eq!(projected.delivery_observations().len(), 1); + assert_eq!( + projected.delivery_observations()[0].event_id(), + attempt.intent().event_id() + ); + assert_eq!( + projected.delivery_observations()[0].request_id(), + attempt.intent().request_id().as_str() + ); + assert_eq!(projected.delivery_observations()[0].target_ordinal(), 1); + assert_eq!( + projected.delivery_observations()[0].outcome(), + radroots_execution::TargetObservation::Accepted + ); + let later = + DeliveryObservation::from_receipt(&attempt, &receipt, target, NonZeroU64::new(now_ms())) + .unwrap(); + assert_eq!( + captured( + ready + .store + .record_delivery_observation(ready.binding.clone(), copy(&ready.proof), later) + .await + .unwrap() + ), + first + ); + ready.store.close().await.unwrap(); + let reopened = ready.fixture.reopen(ready.input.clone()).await; + let retained = ready.query(&reopened, attempt.intent().request_id()).await; + assert_eq!(retained.raw_json(), ready.candidate.raw_json()); + assert_eq!(retained.transaction_id(), attempt.transaction_id()); + assert_eq!(retained.target_ordinals(), &[1]); + assert_eq!( + ready + .observations(&reopened, attempt.intent().request_id()) + .await, + vec![first] + ); + reopened.close().await.unwrap(); +} + +#[tokio::test] +async fn predecessor_schema_six_preserves_original_installation_and_seal_without_automatic_upgrade() +{ + let ready = Ready::new(&[String::from("ws://127.0.0.1:9")], 236).await; + ready.store.close().await.unwrap(); + let mut database = ready.fixture.database().await; + sqlx::raw_sql("DROP TABLE chat_delivery_observations; DROP TABLE chat_delivery_parts; DROP TABLE chat_delivery_attempts; PRAGMA user_version=6").execute(&mut database).await.unwrap(); + let counts:(i64,i64,i64)=sqlx::query_as("SELECT (SELECT count(*) FROM sqlite_schema),(SELECT count(*) FROM chat_messages),(SELECT count(*) FROM seal_evidence)").fetch_one(&mut database).await.unwrap(); + assert_eq!(counts, (16, 1, 1)); + database.close().await.unwrap(); + let path = ready.fixture.path.join("execution_v1.sqlite"); + let original = std::fs::read(&path).unwrap(); + assert!(matches!( + NativeWorkspace::open_existing( + &ready.fixture.path, + ready.input, + ready.fixture.crypto(), + ready.fixture.clock.clone() + ) + .await, + Err(WorkspaceError::RecoveryRequired) + )); + assert_eq!(std::fs::read(path).unwrap(), original); +} + +#[tokio::test] +async fn native_retry_reuses_exact_bytes_and_enforces_twelve_potential_attempts_per_endpoint() { + let ready = Ready::new(&[String::from("ws://127.0.0.1:9")], 231).await; + let first = ready.retain(1).await; + for ordinal in 2..=12u8 { + let DeliveryAttemptOutcome::Claimed(attempt) = ready + .store + .prepare_delivery_attempt(ready.request(ordinal), copy(&ready.proof), None) + .await + .unwrap() + else { + panic!("fresh bounded retry expected") + }; + assert_eq!(attempt.target_ordinals(), &[ordinal]); + assert_eq!(attempt.raw_json(), first.raw_json()); + assert_eq!(attempt.envelope_digest(), first.envelope_digest()); + assert_ne!(attempt.transaction_id(), first.transaction_id()); + } + assert!(matches!( + ready + .store + .prepare_delivery_attempt(ready.request(13), copy(&ready.proof), None) + .await, + Err(OperationError::DeliveryAttemptLimit) + )); + let original = ready.query(&ready.store, first.intent().request_id()).await; + assert_eq!(original.raw_json(), first.raw_json()); + assert_eq!(original.target_ordinals(), &[1]); + ready.store.close().await.unwrap(); +} + +#[tokio::test] +async fn actual_delivery_part_failure_rolls_back_attempt_and_keeps_original_installation() { + let ready = Ready::new(&[String::from("ws://127.0.0.1:9")], 232).await; + let mut database = ready.fixture.database().await; + sqlx::raw_sql("CREATE TRIGGER refuse_delivery_part BEFORE INSERT ON chat_delivery_parts BEGIN SELECT RAISE(ABORT,'synthetic part failure'); END").execute(&mut database).await.unwrap(); + assert!(matches!( + ready + .store + .prepare_delivery_attempt(ready.request(1), copy(&ready.proof), Some(&ready.candidate)) + .await, + Err(OperationError::StorageFailure) + )); + let counts:(i64,i64,i64,i64)=sqlx::query_as("SELECT (SELECT count(*) FROM chat_delivery_attempts),(SELECT count(*) FROM chat_delivery_parts),(SELECT count(*) FROM chat_messages),(SELECT count(*) FROM seal_evidence)").fetch_one(&mut database).await.unwrap(); + assert_eq!(counts, (0, 0, 1, 1)); + sqlx::query("DROP TRIGGER refuse_delivery_part") + .execute(&mut database) + .await + .unwrap(); + database.close().await.unwrap(); + assert_eq!(ready.retain(1).await.target_ordinals(), &[1]); + ready.store.close().await.unwrap(); +} + +#[tokio::test] +async fn actual_delivery_commit_uncertainty_latches_and_reopen_preserves_business_facts() { + let ready = Ready::new(&[String::from("ws://127.0.0.1:9")], 233).await; + let request = ready.request(1); + let id = request.intent().request_id().clone(); + let mut database = ready.fixture.database().await; + sqlx::raw_sql("CREATE TABLE delivery_commit_fault (parent INTEGER REFERENCES workspace_grant(singleton) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER delivery_commit_fault_trigger AFTER INSERT ON chat_delivery_attempts BEGIN INSERT INTO delivery_commit_fault VALUES(2); END").execute(&mut database).await.unwrap(); + database.close().await.unwrap(); + let DeliveryAttemptOutcome::Unknown { operation, attempt } = ready + .store + .prepare_delivery_attempt(request, copy(&ready.proof), Some(&ready.candidate)) + .await + .unwrap() + else { + panic!("actual commit failure must remain unknown") + }; + assert_eq!(attempt, id); + assert_eq!( + operation.operation_id(), + ready.work.evidence().operation_id() + ); + assert!(operation.transaction_id().is_some()); + let e = ready.work.evidence(); + assert!(matches!( + ready + .store + .delivery_attempt( + ready.binding.clone(), + e.operation_id(), + e.command_digest(), + e.recipient(), + id.clone(), + copy(&ready.proof) + ) + .await, + Err(OperationError::RecoveryRequired) + )); + ready.store.close().await.unwrap(); + let mut database = ready.fixture.database().await; + sqlx::raw_sql("DROP TRIGGER delivery_commit_fault_trigger; DROP TABLE delivery_commit_fault") + .execute(&mut database) + .await + .unwrap(); + database.close().await.unwrap(); + let reopened = ready.fixture.reopen(ready.input.clone()).await; + assert!( + reopened + .delivery_attempt( + ready.binding.clone(), + e.operation_id(), + e.command_digest(), + e.recipient(), + id, + copy(&ready.proof) + ) + .await + .unwrap() + .is_none() + ); + let snapshot = reopened + .operation_snapshot( + ready.binding.clone(), + e.operation_id(), + None, + copy(&ready.proof), + ) + .await + .unwrap() + .unwrap(); + assert!(snapshot.installed_message().is_some()); + assert_eq!(snapshot.publications(), &[ready.work]); + reopened.close().await.unwrap(); +} + +#[tokio::test] +async fn admitted_delivery_survives_lost_observer_and_close_drains_original_attempt() { + let ready = Ready::new(&[String::from("ws://127.0.0.1:9")], 234).await; + let request = ready.request(1); + let id = request.intent().request_id().clone(); + let (arrived, resume) = ready.fixture.clock.arm(1); + let owner = ready.store.clone(); + let proof = copy(&ready.proof); + let candidate = ready.candidate.retained_copy(); + let observer = tokio::spawn(async move { + owner + .prepare_delivery_attempt(request, proof, Some(&candidate)) + .await + }); + reached(arrived).await; + observer.abort(); + assert!(observer.await.unwrap_err().is_cancelled()); + resume.send(()).unwrap(); + ready.store.close().await.unwrap(); + let reopened = ready.fixture.reopen(ready.input.clone()).await; + assert_eq!( + ready.query(&reopened, &id).await.raw_json(), + ready.candidate.raw_json() + ); + reopened.close().await.unwrap(); +} + +#[tokio::test] +async fn late_observation_append_requires_original_credential_but_no_fresh_clock_or_dispatch_grant() +{ + let ready = Ready::new(&[String::from("ws://127.0.0.1:9")], 235).await; + let attempt = ready.retain(1).await; + let request = attempt.transport_request().unwrap(); + let failure = radroots_transport::SinkFailure::transport_closed(&request); + let observation = DeliveryObservation::from_failure( + &attempt, + &failure, + request.target_set().targets()[0].fingerprint(), + None, + ) + .unwrap(); + assert_eq!(observation.kind(), DeliveryObservationKind::NotAttempted); + ready + .store + .revoke(ready.input.clone(), &ready.proof) + .await + .unwrap(); + ready.fixture.clock.now.store(0, Ordering::SeqCst); + let wrong = AttachmentProof::from_protected_input(&[0x33; 32]).unwrap(); + assert!( + ready + .store + .record_delivery_observation(ready.binding.clone(), wrong, observation.clone()) + .await + .is_err() + ); + let original = captured( + ready + .store + .record_delivery_observation(ready.binding.clone(), copy(&ready.proof), observation) + .await + .unwrap(), + ); + assert_eq!(original.observed_at_unix_ms(), None); + ready.store.close().await.unwrap(); + // Reopening validates the protected schema, but does not renew revoked access. + let reopened = ready.fixture.reopen(ready.input.clone()).await; + assert!( + reopened + .authenticate(ready.input.clone(), &ready.proof) + .await + .is_err() + ); + reopened.close().await.unwrap(); +} diff --git a/crates/radroots/tests/support/receipt_delivery_admission.rs b/crates/radroots/tests/support/receipt_delivery_admission.rs @@ -0,0 +1,509 @@ +use super::*; +use radroots_domain::{EnvelopeEventId, TransactionId}; +use radroots_transport::{ + outcome::DeliveryOutcome, + sink::{DeliveryRequestId, DeliveryTargetReceipt}, +}; + +#[tokio::test] +async fn native_delivery_matches_original_signed_root_slash_route_to_canonical_target() { + let ready = Ready::new(&[String::from("ws://127.0.0.1:9/")], 250).await; + let retained = ready.retain(1).await; + assert_eq!( + retained.intent().targets().targets()[0].uri().as_str(), + "ws://127.0.0.1:9" + ); + assert_eq!( + ready + .query(&ready.store, retained.intent().request_id()) + .await + .raw_json(), + retained.raw_json() + ); + ready.store.close().await.unwrap(); +} + +#[tokio::test] +async fn self_copy_delivery_refuses_the_original_peer_as_temporary_author() { + use radroots_storage::command::ClaimAttemptRequest; + let ready = Ready::new(&[String::from("ws://127.0.0.1:9")], 251).await; + let e = ready.work.evidence(); + let original = ready + .store + .operation_snapshot( + ready.binding.clone(), + e.operation_id(), + None, + copy(&ready.proof), + ) + .await + .unwrap() + .unwrap(); + let plan = *original.bound_plan().unwrap().plan().digest(); + let installation = original.installation().unwrap().transaction_id(); + let claim = issuance::claim( + ready + .store + .claim_attempt( + ClaimAttemptRequest::new( + ready.binding.clone(), + e.operation_id(), + e.command_digest(), + plan, + key(), + ), + copy(&ready.proof), + ) + .await + .unwrap(), + ); + let fields = claim.seal().fields(); + let participant = keys(1); + let signed = nostr::UnsignedEvent { + id: Some(nostr::EventId::from_slice(&claim.seal().event_id()).unwrap()), + pubkey: participant.public_key(), + created_at: nostr::Timestamp::from_secs(fields.created_at), + kind: nostr::Kind::Custom(13), + tags: nostr::Tags::new(), + content: fields.content.to_owned(), + } + .sign_with_keys(&participant) + .unwrap(); + let evidence = SealEvidence::verify(&claim, &signed.as_json(), None).unwrap(); + assert!(matches!( + ready + .store + .capture_evidence(ready.input.clone(), &ready.proof, &evidence) + .await + .unwrap(), + EvidenceCaptureOutcome::Committed(_) + )); + assert!(matches!( + ready + .store + .install_transition_and_outbox( + InstallationRequest::new( + ready.binding.clone(), + e.operation_id(), + e.command_digest(), + plan + ), + copy(&ready.proof) + ) + .await + .unwrap(), + InstallationOutcome::Committed(_) + )); + let snapshot = ready + .store + .operation_snapshot( + ready.binding.clone(), + e.operation_id(), + None, + copy(&ready.proof), + ) + .await + .unwrap() + .unwrap(); + assert_eq!( + snapshot.installation().unwrap().transaction_id(), + installation + ); + assert_eq!(snapshot.publications().len(), 2); + let work = *snapshot + .publications() + .iter() + .find(|work| work.evidence().recipient() == key()) + .unwrap(); + let peer = keys(2); + let content = nostr::nips::nip44::encrypt( + peer.secret_key(), + &participant.public_key(), + evidence.raw_json(), + nostr::nips::nip44::Version::V2, + ) + .unwrap(); + let outer = nostr::UnsignedEvent { + id: None, + pubkey: peer.public_key(), + created_at: nostr::Timestamp::from_secs(work.queued_at().get()), + kind: nostr::Kind::Custom(1059), + tags: nostr::Tags::from_list(vec![nostr::Tag::public_key(participant.public_key())]), + content, + } + .sign_with_keys(&peer) + .unwrap(); + let candidate = EnvelopeCandidate::from_obtained( + work, + &outer.as_json(), + &peer.secret_key().to_secret_bytes(), + ) + .unwrap(); + let targets = ready.request(1).intent().targets().clone(); + let deadline = NonZeroU64::new(work.queued_at().get() * 1000 + 60_000).unwrap(); + let policy = SatisfactionPolicy::new(SatisfactionClass::Accepted, TargetPolicy::all()); + let intent = DeliveryIntent::new( + &"ee".repeat(32), + candidate.event_id(), + &targets, + &policy, + deadline, + ) + .unwrap(); + match ready + .store + .prepare_delivery_attempt( + DeliveryAttemptRequest::new(ready.binding.clone(), work, intent).unwrap(), + copy(&ready.proof), + Some(&candidate), + ) + .await + { + Err(OperationError::InvalidEvidence) => {} + Ok(DeliveryAttemptOutcome::Claimed(_)) => { + panic!("native D freshly claimed an outer signed by the original peer") + } + Err(error) => panic!("unexpected refusal: {}", error.code()), + Ok(_) => panic!("unexpected retained or unknown native outcome"), + } + let wrapped = wrap_retained_seal( + evidence.raw_json(), + key(), + NonZeroU64::new(work.queued_at().get()).unwrap(), + &OsNonceSource, + ) + .unwrap(); + let candidate = wrapped.with_wrapping_key(|key| { + EnvelopeCandidate::from_obtained(work, wrapped.signed_json(), key).unwrap() + }); + let intent = DeliveryIntent::new( + &"ef".repeat(32), + candidate.event_id(), + &targets, + &policy, + deadline, + ) + .unwrap(); + assert!(matches!( + ready + .store + .prepare_delivery_attempt( + DeliveryAttemptRequest::new(ready.binding.clone(), work, intent).unwrap(), + copy(&ready.proof), + Some(&candidate) + ) + .await + .unwrap(), + DeliveryAttemptOutcome::Claimed(_) + )); + ready.store.close().await.unwrap(); +} + +fn signed_candidate(ready: &Ready, scalar: u8, content: String) -> EnvelopeCandidate { + let signer = keys(scalar); + let recipient = + nostr::PublicKey::from_slice(ready.work.evidence().recipient().as_bytes()).unwrap(); + let event = nostr::UnsignedEvent { + id: None, + pubkey: signer.public_key(), + created_at: nostr::Timestamp::from_secs(ready.work.queued_at().get()), + kind: nostr::Kind::Custom(1059), + tags: nostr::Tags::from_list(vec![nostr::Tag::public_key(recipient)]), + content, + } + .sign_with_keys(&signer) + .unwrap(); + EnvelopeCandidate::from_obtained( + ready.work, + &event.as_json(), + &signer.secret_key().to_secret_bytes(), + ) + .unwrap() +} +async fn rebound_work(ready: &Ready) -> PublicationWork { + let snapshot = ready + .store + .operation_snapshot( + ready.binding.clone(), + ready.work.evidence().operation_id(), + None, + copy(&ready.proof), + ) + .await + .unwrap() + .unwrap(); + PublicationWork::from_committed_store( + snapshot.bound_plan().unwrap(), + snapshot.installation().unwrap(), + ready.work.evidence(), + TransactionId::from_bytes([91; 32]), + ready.work.queued_at(), + ) + .unwrap() +} + +#[tokio::test] +async fn native_delivery_independently_checks_temporary_keys_exact_seal_and_original_work() { + let ready = Ready::new(&[String::from("ws://127.0.0.1:9")], 248).await; + let receiver = + nostr::PublicKey::from_slice(ready.work.evidence().recipient().as_bytes()).unwrap(); + let temp = keys(3); + let wrong_plaintext = nostr::nips::nip44::encrypt( + temp.secret_key(), + &receiver, + "another private record", + nostr::nips::nip44::Version::V2, + ) + .unwrap(); + let bad_key = + EnvelopeCandidate::from_obtained(ready.work, ready.candidate.raw_json(), &[1; 32]).unwrap(); + let zero_key = + EnvelopeCandidate::from_obtained(ready.work, ready.candidate.raw_json(), &[0; 32]).unwrap(); + for candidate in [ + signed_candidate(&ready, 1, String::from("participant outer")), + signed_candidate(&ready, 2, String::from("recipient outer")), + signed_candidate(&ready, 3, String::from("invalid cipher")), + signed_candidate(&ready, 3, wrong_plaintext), + bad_key, + zero_key, + ] { + assert!(matches!( + ready + .store + .prepare_delivery_attempt(ready.request(1), copy(&ready.proof), Some(&candidate)) + .await, + Err(OperationError::InvalidEvidence) + )); + } + let work = rebound_work(&ready).await; + let request = DeliveryAttemptRequest::new( + ready.binding.clone(), + work, + ready.request(1).intent().clone(), + ) + .unwrap(); + assert!(matches!( + ready + .store + .prepare_delivery_attempt(request, copy(&ready.proof), None) + .await, + Err(OperationError::InvalidEvidence) + )); + let candidate = ready.candidate.with_wrapping_key(|key| { + EnvelopeCandidate::from_obtained(work, ready.candidate.raw_json(), key).unwrap() + }); + assert!(matches!( + ready + .store + .prepare_delivery_attempt(ready.request(1), copy(&ready.proof), Some(&candidate)) + .await, + Err(OperationError::InvalidEvidence) + )); + let base = ready.request(1); + let changed = DeliveryIntent::new( + base.intent().request_id().as_str(), + EnvelopeEventId::from_bytes([90; 32]), + base.intent().targets(), + base.intent().policy(), + base.intent().deadline_unix_ms(), + ) + .unwrap(); + assert!(matches!( + ready + .store + .prepare_delivery_attempt( + DeliveryAttemptRequest::new(ready.binding.clone(), ready.work, changed).unwrap(), + copy(&ready.proof), + Some(&ready.candidate) + ) + .await, + Err(OperationError::InvalidEvidence) + )); + let retained = ready.retain(1).await; + let replay = ready + .store + .prepare_delivery_attempt(ready.request(1), copy(&ready.proof), Some(&ready.candidate)) + .await + .unwrap(); + let DeliveryAttemptOutcome::Retained(replayed) = replay else { + panic!("same potential invocation must never be freshly claimed") + }; + assert_eq!(replayed.transaction_id(), retained.transaction_id()); + let different = ready.candidate.with_wrapping_key(|key| { + EnvelopeCandidate::from_obtained( + ready.work, + &format!("{} ", ready.candidate.raw_json()), + key, + ) + .unwrap() + }); + for id in [1, 2] { + assert!(matches!( + ready + .store + .prepare_delivery_attempt(ready.request(id), copy(&ready.proof), Some(&different)) + .await, + Err(OperationError::IdempotencyConflict) + )); + } + assert_eq!( + ready + .query(&ready.store, retained.intent().request_id()) + .await + .raw_json(), + retained.raw_json() + ); + ready.store.close().await.unwrap(); +} + +#[tokio::test] +async fn native_delivery_refuses_backward_clock_expired_attempt_and_unbound_observations() { + let ready = Ready::new(&[String::from("ws://127.0.0.1:9")], 249).await; + let first = ready.retain(1).await; + let time = ready.work.queued_at().get(); + ready.fixture.clock.now.store(time + 1, Ordering::SeqCst); + assert!(matches!( + ready + .store + .prepare_delivery_attempt(ready.request(2), copy(&ready.proof), None) + .await + .unwrap(), + DeliveryAttemptOutcome::Claimed(_) + )); + ready.fixture.clock.now.store(time, Ordering::SeqCst); + assert!( + ready + .store + .prepare_delivery_attempt(ready.request(3), copy(&ready.proof), None) + .await + .is_err() + ); + ready.fixture.clock.now.store(time + 61, Ordering::SeqCst); + assert!(matches!( + ready + .store + .prepare_delivery_attempt(ready.request(3), copy(&ready.proof), None) + .await, + Err(OperationError::DeadlineExceeded) + )); + for id in ["short".to_string(), "AA".repeat(32)] { + let e = ready.work.evidence(); + assert!(matches!( + ready + .store + .delivery_attempt( + ready.binding.clone(), + e.operation_id(), + e.command_digest(), + e.recipient(), + DeliveryRequestId::parse(id).unwrap(), + copy(&ready.proof) + ) + .await, + Err(OperationError::InvalidCommand) + )); + } + let work = rebound_work(&ready).await; + let target = DeliveryTargetReceipt::attempted( + first.intent().targets().targets()[0].clone(), + DeliveryOutcome::accepted(), + ); + // Structural data is deliberately untrusted; these are not remote ACKs. + for (reported_work, raw, ordinals) in [ + (work, first.raw_json().to_owned(), vec![1]), + (first.work(), format!("{} ", first.raw_json()), vec![1]), + (first.work(), first.raw_json().to_owned(), vec![2]), + ] { + let attempt = DeliveryAttempt::from_store_report( + reported_work, + first.intent().clone(), + &raw, + first.transaction_id(), + first.retained_at(), + ordinals, + ) + .unwrap(); + let observation = DeliveryObservation::from_store_report(&attempt, &target, None).unwrap(); + assert!( + ready + .store + .record_delivery_observation(ready.binding.clone(), copy(&ready.proof), observation) + .await + .is_err() + ); + } + assert!( + ready + .observations(&ready.store, first.intent().request_id()) + .await + .is_empty() + ); + assert_eq!( + ready + .query(&ready.store, first.intent().request_id()) + .await + .raw_json(), + first.raw_json() + ); + ready.store.close().await.unwrap(); +} + +#[tokio::test] +async fn native_observation_query_never_crosses_original_request_or_recipient() { + let ready = Ready::new(&[String::from("ws://127.0.0.1:9")], 252).await; + let attempt = ready.retain(1).await; + // Synthetic normalized data tests query scope; loopback cases own ACK proof. + let target = DeliveryTargetReceipt::attempted( + attempt.intent().targets().targets()[0].clone(), + DeliveryOutcome::accepted(), + ); + let observation = DeliveryObservation::from_store_report(&attempt, &target, None).unwrap(); + assert!(matches!( + ready + .store + .record_delivery_observation(ready.binding.clone(), copy(&ready.proof), observation) + .await + .unwrap(), + DeliveryObservationOutcome::Committed(_) + )); + assert_eq!( + ready + .observations(&ready.store, attempt.intent().request_id()) + .await + .len(), + 1 + ); + assert!( + ready + .observations( + &ready.store, + &DeliveryRequestId::parse("ab".repeat(32)).unwrap() + ) + .await + .is_empty() + ); + let e = ready.work.evidence(); + assert!( + ready + .store + .delivery_observations( + ready.binding.clone(), + e.operation_id(), + e.command_digest(), + key(), + attempt.intent().request_id().clone(), + copy(&ready.proof) + ) + .await + .unwrap() + .is_empty() + ); + assert_eq!( + ready + .observations(&ready.store, attempt.intent().request_id()) + .await + .len(), + 1 + ); + ready.store.close().await.unwrap(); +} diff --git a/crates/radroots/tests/support/receipt_delivery_faults.rs b/crates/radroots/tests/support/receipt_delivery_faults.rs @@ -0,0 +1,573 @@ +use super::*; + +#[tokio::test] +async fn a_retry_cannot_replace_the_original_publication_target_policy() { + let ready = Ready::new( + &[ + String::from("ws://127.0.0.1:9"), + String::from("ws://127.0.0.1:10"), + ], + 243, + ) + .await; + let original = ready.retain(1).await; + let narrowed = TargetSet::new(vec![original.intent().targets().targets()[0].clone()]).unwrap(); + let replacement = DeliveryIntent::new( + &"02".repeat(32), + original.intent().event_id(), + &narrowed, + original.intent().policy(), + original.intent().deadline_unix_ms(), + ) + .unwrap(); + let request = + DeliveryAttemptRequest::new(ready.binding.clone(), ready.work, replacement).unwrap(); + assert!(matches!( + ready + .store + .prepare_delivery_attempt(request, copy(&ready.proof), None) + .await, + Err(OperationError::IdempotencyConflict) + )); + ready.store.close().await.unwrap(); +} + +fn selected_request( + ready: &Ready, + id: u8, + policy: &SatisfactionPolicy, + index: usize, +) -> DeliveryAttemptRequest { + let original = ready.request(id); + let selected = + TargetSet::new(vec![original.intent().targets().targets()[index].clone()]).unwrap(); + let intent = DeliveryIntent::new( + original.intent().request_id().as_str(), + original.intent().event_id(), + original.intent().targets(), + policy, + original.intent().deadline_unix_ms(), + ) + .unwrap() + .with_selection(&selected) + .unwrap(); + DeliveryAttemptRequest::new(ready.binding.clone(), ready.work, intent).unwrap() +} + +// Synthetic normalized adapter data exercises storage/policy composition here. +// The separate actual loopback cases qualify remote frame and ACK behavior. +fn synthetic_accepted(attempt: &DeliveryAttempt) -> DeliveryObservation { + use radroots_transport::{ + DeliveryReceipt, outcome::DeliveryOutcome, sink::DeliveryTargetReceipt, + }; + let request = attempt.transport_request().unwrap(); + let selected = &attempt.intent().selection()[0]; + let receipts = request + .target_set() + .targets() + .iter() + .map(|target| { + if target.fingerprint() == selected { + DeliveryTargetReceipt::attempted(target.clone(), DeliveryOutcome::accepted()) + } else { + DeliveryTargetReceipt::skipped(target.clone(), DeliveryOutcome::unavailable()) + .unwrap() + } + }) + .collect(); + let receipt = DeliveryReceipt::for_request(&request, receipts).unwrap(); + DeliveryObservation::from_receipt(attempt, &receipt, selected, None).unwrap() +} + +#[tokio::test] +async fn exhausted_optional_endpoint_does_not_starve_required_endpoint_or_weaken_the_original_policy() + { + let ready = Ready::new( + &[ + String::from("ws://127.0.0.1:9"), + String::from("ws://127.0.0.1:10"), + ], + 244, + ) + .await; + let base = ready.request(1); + let required = base.intent().targets().targets()[1].fingerprint().clone(); + let policy = SatisfactionPolicy::new( + SatisfactionClass::Accepted, + TargetPolicy::required(vec![required]).unwrap(), + ); + for ordinal in 1..=12u8 { + let selected = selected_request(&ready, ordinal, &policy, 0); + let candidate = if ordinal == 1 { + Some(&ready.candidate) + } else { + None + }; + let DeliveryAttemptOutcome::Claimed(attempt) = ready + .store + .prepare_delivery_attempt(selected, copy(&ready.proof), candidate) + .await + .unwrap() + else { + panic!("new selected potential attempt expected") + }; + assert_eq!(attempt.target_ordinals(), &[ordinal]); + assert_eq!(attempt.intent().targets().len(), 2); + assert_eq!(attempt.intent().selection().len(), 1); + } + assert!(matches!( + ready + .store + .prepare_delivery_attempt( + selected_request(&ready, 13, &policy, 0), + copy(&ready.proof), + None + ) + .await, + Err(OperationError::DeliveryAttemptLimit) + )); + let DeliveryAttemptOutcome::Claimed(required) = ready + .store + .prepare_delivery_attempt( + selected_request(&ready, 14, &policy, 1), + copy(&ready.proof), + None, + ) + .await + .unwrap() + else { + panic!("required endpoint must progress independently") + }; + assert_eq!(required.target_ordinals(), &[1]); + assert_eq!(required.raw_json(), ready.candidate.raw_json()); + captured( + ready + .store + .record_delivery_observation( + ready.binding.clone(), + copy(&ready.proof), + synthetic_accepted(&required), + ) + .await + .unwrap(), + ); + let e = ready.work.evidence(); + let snapshot = ready + .store + .operation_snapshot( + ready.binding.clone(), + e.operation_id(), + None, + copy(&ready.proof), + ) + .await + .unwrap() + .unwrap(); + assert_eq!(snapshot.publication_accepted(ready.work), Ok(true)); + let receipt = radroots_execution::OperationReceipt::from_snapshot(&snapshot); + assert_eq!(receipt.delivery(), radroots_execution::Delivery::Unknown); + assert!( + !receipt + .recovery() + .contains(&radroots_execution::RecoveryRequirement::ResumeDelivery) + ); + ready.store.close().await.unwrap(); +} + +#[tokio::test] +async fn original_quorum_accumulates_acceptance_across_independent_selected_attempts() { + let ready = Ready::new( + &[ + String::from("ws://127.0.0.1:9"), + String::from("ws://127.0.0.1:10"), + String::from("ws://127.0.0.1:11"), + ], + 245, + ) + .await; + let policy = SatisfactionPolicy::new( + SatisfactionClass::Accepted, + TargetPolicy::quorum(2).unwrap(), + ); + for index in 0..2 { + let selected = selected_request(&ready, index as u8 + 1, &policy, index); + let candidate = if index == 0 { + Some(&ready.candidate) + } else { + None + }; + let DeliveryAttemptOutcome::Claimed(attempt) = ready + .store + .prepare_delivery_attempt(selected, copy(&ready.proof), candidate) + .await + .unwrap() + else { + panic!("fresh selected attempt required") + }; + captured( + ready + .store + .record_delivery_observation( + ready.binding.clone(), + copy(&ready.proof), + synthetic_accepted(&attempt), + ) + .await + .unwrap(), + ); + let e = ready.work.evidence(); + let snapshot = ready + .store + .operation_snapshot( + ready.binding.clone(), + e.operation_id(), + None, + copy(&ready.proof), + ) + .await + .unwrap() + .unwrap(); + assert_eq!(snapshot.publication_accepted(ready.work), Ok(index == 1)); + let receipt = radroots_execution::OperationReceipt::from_snapshot(&snapshot); + assert_eq!( + receipt + .recovery() + .contains(&radroots_execution::RecoveryRequirement::ResumeDelivery), + index == 0 + ); + } + ready.store.close().await.unwrap(); +} + +#[tokio::test] +async fn a_fresh_delivery_deadline_after_first_issuance_expiry_reuses_original_bytes_and_policy() { + let ready = Ready::new(&[String::from("ws://127.0.0.1:9")], 246).await; + let original = ready.retain(1).await; + ready + .fixture + .clock + .now + .store(ready.work.queued_at().get() + 121, Ordering::SeqCst); + let next = DeliveryIntent::new( + &"02".repeat(32), + original.intent().event_id(), + original.intent().targets(), + original.intent().policy(), + NonZeroU64::new((ready.work.queued_at().get() + 180) * 1000).unwrap(), + ) + .unwrap(); + assert_eq!( + next.publication_policy_digest(), + original.intent().publication_policy_digest() + ); + assert_ne!(next.digest(), original.intent().digest()); + let DeliveryAttemptOutcome::Claimed(next) = ready + .store + .prepare_delivery_attempt( + DeliveryAttemptRequest::new(ready.binding.clone(), ready.work, next).unwrap(), + copy(&ready.proof), + None, + ) + .await + .unwrap() + else { + panic!("current-access delivery may retain a fresh attempt without reissuing S") + }; + assert_eq!(next.raw_json(), original.raw_json()); + assert_eq!(next.target_ordinals(), &[2]); + let retained = ready + .query(&ready.store, original.intent().request_id()) + .await; + assert_eq!( + retained.intent().deadline_unix_ms(), + original.intent().deadline_unix_ms() + ); + let mut database = ready.fixture.database().await; + let counts:(i64,i64,i64)=sqlx::query_as("SELECT (SELECT count(*) FROM signing_attempts),(SELECT count(*) FROM seal_evidence),(SELECT count(*) FROM chat_messages)").fetch_one(&mut database).await.unwrap(); + assert_eq!(counts, (1, 1, 1)); + database.close().await.unwrap(); + ready.store.close().await.unwrap(); +} + +#[tokio::test] +async fn actual_loopback_wrong_ack_timeout_disconnect_and_rejection_remain_distinct_after_reopen() { + for (case, expected) in [ + (0, DeliveryObservationKind::TimedOut), + (1, DeliveryObservationKind::Disconnected), + (2, DeliveryObservationKind::Rejected), + ] { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let ready = Ready::new( + &[format!("ws://{}", listener.local_addr().unwrap())], + 237 + case, + ) + .await; + let attempt = ready.retain(1).await; + let exact = String::from(attempt.raw_json()); + let (release, released) = tokio::sync::oneshot::channel(); + let server = tokio::spawn(async move { + let (tcp, _) = listener.accept().await.unwrap(); + let mut socket = accept_async(tcp).await.unwrap(); + let raw = tokio::time::timeout(Duration::from_secs(5), async { + loop { + if let Message::Text(raw) = socket.next().await.unwrap().unwrap() { + break raw.to_string(); + } + } + }) + .await + .unwrap(); + assert_eq!(raw, format!("[\"EVENT\",{exact}]")); + let event: serde_json::Value = serde_json::from_str(&raw).unwrap(); + match case { + 0 => { + socket + .send(Message::Text( + json!(["OK", "ff".repeat(32), true, ""]).to_string().into(), + )) + .await + .unwrap(); + let _ = released.await; + } + 1 => { + socket.close(None).await.unwrap(); + } + 2 => { + socket + .send(Message::Text( + json!(["OK", event[1]["id"], false, "restricted: synthetic refusal"]) + .to_string() + .into(), + )) + .await + .unwrap(); + } + _ => unreachable!(), + } + }); + let request = attempt.transport_request().unwrap(); + let target = request.target_set().targets()[0].fingerprint(); + let transport = NostrTransport::new(ready.config.clone()); + let result = transport.deliver(request.clone()).await; + let observation = match result { + Ok(receipt) => { + assert!(!receipt.is_satisfied(&request).unwrap()); + DeliveryObservation::from_receipt(&attempt, &receipt, target, None).unwrap() + } + Err(failure) => { + DeliveryObservation::from_failure(&attempt, &failure, target, None).unwrap() + } + }; + let _ = release.send(()); + server.await.unwrap(); + assert_eq!(observation.kind(), expected, "actual case {case}"); + let first = captured( + ready + .store + .record_delivery_observation(ready.binding.clone(), copy(&ready.proof), observation) + .await + .unwrap(), + ); + ready.store.close().await.unwrap(); + let reopened = ready.fixture.reopen(ready.input.clone()).await; + assert_eq!( + ready + .observations(&reopened, attempt.intent().request_id()) + .await, + vec![first] + ); + let e = ready.work.evidence(); + let snapshot = reopened + .operation_snapshot( + ready.binding.clone(), + e.operation_id(), + None, + copy(&ready.proof), + ) + .await + .unwrap() + .unwrap(); + assert!(snapshot.installed_message().is_some()); + let view = radroots_execution::OperationReceipt::from_snapshot(&snapshot); + assert!( + view.recovery() + .contains(&radroots_execution::RecoveryRequirement::ResumeDelivery) + ); + assert_eq!(view.delivery_observations().len(), 1); + reopened.close().await.unwrap(); + } +} + +fn not_attempted(attempt: &DeliveryAttempt) -> DeliveryObservation { + let request = attempt.transport_request().unwrap(); + DeliveryObservation::from_failure( + attempt, + &radroots_transport::SinkFailure::transport_closed(&request), + request.target_set().targets()[0].fingerprint(), + None, + ) + .unwrap() +} + +#[tokio::test] +async fn actual_observation_rollback_and_commit_uncertainty_preserve_retained_outer_and_business_facts() + { + for commit in [false, true] { + let ready = Ready::new(&[String::from("ws://127.0.0.1:9")], 240 + u8::from(commit)).await; + let attempt = ready.retain(1).await; + let observation = not_attempted(&attempt); + let mut database = ready.fixture.database().await; + if commit { + sqlx::raw_sql("CREATE TABLE observation_commit_fault (parent INTEGER REFERENCES workspace_grant(singleton) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER observation_commit_fault_trigger AFTER INSERT ON chat_delivery_observations BEGIN INSERT INTO observation_commit_fault VALUES(2); END").execute(&mut database).await.unwrap(); + } else { + sqlx::raw_sql("CREATE TRIGGER observation_insert_fault BEFORE INSERT ON chat_delivery_observations BEGIN SELECT RAISE(ABORT,'synthetic observation fault'); END").execute(&mut database).await.unwrap(); + } + database.close().await.unwrap(); + let result = ready + .store + .record_delivery_observation( + ready.binding.clone(), + copy(&ready.proof), + observation.clone(), + ) + .await; + if commit { + assert!(matches!(result, Ok(DeliveryObservationOutcome::Unknown(_)))); + } else { + assert!(matches!(result, Err(OperationError::StorageFailure))); + } + ready.store.close().await.unwrap(); + let mut database = ready.fixture.database().await; + if commit { + sqlx::raw_sql("DROP TRIGGER observation_commit_fault_trigger; DROP TABLE observation_commit_fault").execute(&mut database).await.unwrap(); + } else { + sqlx::query("DROP TRIGGER observation_insert_fault") + .execute(&mut database) + .await + .unwrap(); + } + let counts:(i64,i64,i64)=sqlx::query_as("SELECT (SELECT count(*) FROM chat_delivery_attempts),(SELECT count(*) FROM chat_delivery_observations),(SELECT count(*) FROM chat_messages)").fetch_one(&mut database).await.unwrap(); + assert_eq!(counts, (1, 0, 1)); + database.close().await.unwrap(); + let reopened = ready.fixture.reopen(ready.input.clone()).await; + assert_eq!( + ready + .query(&reopened, attempt.intent().request_id()) + .await + .raw_json(), + attempt.raw_json() + ); + assert!( + ready + .observations(&reopened, attempt.intent().request_id()) + .await + .is_empty() + ); + assert_eq!( + captured( + reopened + .record_delivery_observation( + ready.binding.clone(), + copy(&ready.proof), + observation.clone() + ) + .await + .unwrap() + ), + observation + ); + reopened.close().await.unwrap(); + } +} + +#[tokio::test] +async fn corrupted_delivery_ciphertext_and_oversized_observation_refuse_the_complete_snapshot() { + let ready = Ready::new(&[String::from("ws://127.0.0.1:9")], 242).await; + let attempt = ready.retain(1).await; + captured( + ready + .store + .record_delivery_observation( + ready.binding.clone(), + copy(&ready.proof), + not_attempted(&attempt), + ) + .await + .unwrap(), + ); + let mut database = ready.fixture.database().await; + let original: Vec<u8> = + sqlx::query_scalar("SELECT protected FROM chat_delivery_parts WHERE part=1") + .fetch_one(&mut database) + .await + .unwrap(); + let mut wrong = original.clone(); + wrong[0] ^= 1; + sqlx::query("UPDATE chat_delivery_parts SET protected=? WHERE part=1") + .bind(wrong) + .execute(&mut database) + .await + .unwrap(); + let e = ready.work.evidence(); + assert!( + ready + .store + .operation_snapshot( + ready.binding.clone(), + e.operation_id(), + None, + copy(&ready.proof) + ) + .await + .is_err() + ); + sqlx::query("UPDATE chat_delivery_parts SET protected=? WHERE part=1") + .bind(original) + .execute(&mut database) + .await + .unwrap(); + let original: Vec<u8> = sqlx::query_scalar("SELECT protected FROM chat_delivery_observations") + .fetch_one(&mut database) + .await + .unwrap(); + sqlx::query("PRAGMA ignore_check_constraints=ON") + .execute(&mut database) + .await + .unwrap(); + sqlx::query("UPDATE chat_delivery_observations SET protected=zeroblob(262145)") + .execute(&mut database) + .await + .unwrap(); + assert!( + ready + .store + .operation_snapshot( + ready.binding.clone(), + e.operation_id(), + None, + copy(&ready.proof) + ) + .await + .is_err() + ); + sqlx::query("UPDATE chat_delivery_observations SET protected=?") + .bind(original) + .execute(&mut database) + .await + .unwrap(); + database.close().await.unwrap(); + let snapshot = ready + .store + .operation_snapshot( + ready.binding.clone(), + e.operation_id(), + None, + copy(&ready.proof), + ) + .await + .unwrap() + .unwrap(); + assert_eq!(snapshot.delivery_attempts().len(), 1); + assert_eq!(snapshot.delivery_observations().len(), 1); + ready.store.close().await.unwrap(); +} diff --git a/crates/radroots/tests/support/receipt_delivery_validation.rs b/crates/radroots/tests/support/receipt_delivery_validation.rs @@ -0,0 +1,451 @@ +//! Untrusted data projections must not bypass the retained native graph. +use super::*; +use radroots_domain::TransactionId; +use radroots_storage::command::{DeliveryAttemptSummary, OperationSnapshot}; +use radroots_transport::{ + Target, TransportId, outcome::DeliveryOutcome, sink::DeliveryTargetReceipt, target::TargetScope, +}; + +fn targets(urls: &[&str]) -> TargetSet { + TargetSet::new( + urls.iter() + .map(|url| Target::nostr_relay(url).unwrap()) + .collect(), + ) + .unwrap() +} +fn intent(set: &TargetSet, policy: TargetPolicy) -> Result<DeliveryIntent, OperationError> { + DeliveryIntent::new( + &"01".repeat(32), + EnvelopeEventId::from_bytes([3; 32]), + set, + &SatisfactionPolicy::new(SatisfactionClass::Accepted, policy), + NonZeroU64::new(1_000).unwrap(), + ) +} + +#[test] +fn delivery_intent_bounds_and_selection_preserve_the_full_publication_criterion() { + let set = targets(&["wss://a.example", "wss://b.example"]); + let original = intent(&set, TargetPolicy::all()).unwrap(); + for id in [ + String::new(), + "01".repeat(31), + "AA".repeat(32), + "gg".repeat(32), + ] { + assert_eq!( + DeliveryIntent::new( + &id, + original.event_id(), + &set, + original.policy(), + original.deadline_unix_ms() + ), + Err(OperationError::InvalidCommand) + ); + } + let too_many = TargetSet::new( + (0..17) + .map(|i| Target::nostr_relay(format!("wss://r{i}.example")).unwrap()) + .collect(), + ) + .unwrap(); + let duplicate_endpoint = TargetSet::new(vec![ + Target::nostr_relay_with_metadata( + "wss://a.example", + Some(TargetScope::parse("first").unwrap()), + None, + ) + .unwrap(), + Target::nostr_relay_with_metadata( + "wss://a.example/", + Some(TargetScope::parse("second").unwrap()), + None, + ) + .unwrap(), + ]) + .unwrap(); + let other_transport = TargetSet::new(vec![ + Target::new(TransportId::LOCAL, "test://endpoint").unwrap(), + ]) + .unwrap(); + assert!(TargetSet::new(vec![]).is_err()); + for rejected in [too_many, duplicate_endpoint, other_transport] { + assert_eq!( + intent(&rejected, TargetPolicy::all()), + Err(OperationError::InvalidCommand) + ); + } + assert!( + DeliveryIntent::new( + original.request_id().as_str(), + original.event_id(), + &set, + original.policy(), + NonZeroU64::new(253_402_300_800_000).unwrap() + ) + .is_err() + ); + assert!( + DeliveryIntent::new( + original.request_id().as_str(), + original.event_id(), + &set, + &SatisfactionPolicy::new(SatisfactionClass::Delivered, TargetPolicy::all()), + original.deadline_unix_ms() + ) + .is_err() + ); + assert!(intent(&set, TargetPolicy::quorum(3).unwrap()).is_err()); + let foreign = targets(&["wss://foreign.example"]); + assert!( + intent( + &set, + TargetPolicy::required(vec![foreign.targets()[0].fingerprint().clone()]).unwrap() + ) + .is_err() + ); + assert!(original.clone().with_selection(&foreign).is_err()); + let selected = original + .clone() + .with_selection(&TargetSet::new(vec![set.targets()[1].clone()]).unwrap()) + .unwrap(); + assert_eq!(selected.targets(), original.targets()); + assert_eq!( + selected.publication_policy_digest(), + original.publication_policy_digest() + ); + assert_ne!(selected.digest(), original.digest()); + assert_eq!( + selected.selected_target_set().unwrap().targets(), + &[set.targets()[1].clone()] + ); + assert!(intent(&set, TargetPolicy::any()).is_ok()); + assert!(intent(&set, TargetPolicy::quorum(2).unwrap()).is_ok()); +} + +fn outer_variant(ready: &Ready, kind: u16, time: u64, recipient: Option<u8>) -> String { + let original = nostr::Event::from_json(ready.candidate.raw_json()).unwrap(); + let signer = keys(3); + nostr::UnsignedEvent { + id: None, + pubkey: signer.public_key(), + created_at: nostr::Timestamp::from_secs(time), + kind: nostr::Kind::Custom(kind), + tags: nostr::Tags::from_list( + recipient + .map(|id| nostr::Tag::public_key(keys(id).public_key())) + .into_iter() + .collect(), + ), + content: original.content, + } + .sign_with_keys(&signer) + .unwrap() + .as_json() +} +fn report( + original: &DeliveryAttempt, + intent: DeliveryIntent, + raw: &str, + at: UnixSeconds, + ordinals: Vec<u8>, +) -> Result<DeliveryAttempt, OperationError> { + DeliveryAttempt::from_store_report( + original.work(), + intent, + raw, + TransactionId::from_bytes([88; 32]), + at, + ordinals, + ) +} +fn observation(attempt: &DeliveryAttempt, outcome: DeliveryOutcome) -> DeliveryObservation { + DeliveryObservation::from_store_report( + attempt, + &DeliveryTargetReceipt::attempted( + attempt.intent().selected_targets().next().unwrap().clone(), + outcome, + ), + None, + ) + .unwrap() +} +async fn snapshot(ready: &Ready) -> OperationSnapshot { + ready + .store + .operation_snapshot( + ready.binding.clone(), + ready.work.evidence().operation_id(), + None, + copy(&ready.proof), + ) + .await + .unwrap() + .unwrap() +} + +#[tokio::test] +async fn malformed_carriers_and_reconstructed_attempts_cannot_replace_original_native_bytes() { + let ready = Ready::new(&[String::from("ws://127.0.0.1:9")], 246).await; + let retained = ready.retain(1).await; + let wrapping_key = [3; 32]; + let time = retained.retained_at().get(); + for raw in [ + " ".repeat(65_537), + outer_variant(&ready, 14, time, Some(2)), + outer_variant(&ready, 1059, 0, Some(2)), + outer_variant(&ready, 1059, 253_402_300_800, Some(2)), + outer_variant(&ready, 1059, time, None), + outer_variant(&ready, 1059, time, Some(1)), + ] { + assert!(matches!( + EnvelopeCandidate::from_obtained(ready.work, &raw, &wrapping_key), + Err(OperationError::InvalidEvidence) + )); + } + for field in ["id", "sig"] { + let mut wire: serde_json::Value = serde_json::from_str(ready.candidate.raw_json()).unwrap(); + wire[field] = "0".repeat(if field == "id" { 64 } else { 128 }).into(); + assert!( + EnvelopeCandidate::from_obtained(ready.work, &wire.to_string(), &wrapping_key).is_err() + ); + } + assert_eq!(ready.candidate.work(), ready.work); + assert_eq!( + ready.candidate.created_at().get(), + nostr::Event::from_json(ready.candidate.raw_json()) + .unwrap() + .created_at + .as_secs() + ); + let mismatched = DeliveryIntent::new( + retained.intent().request_id().as_str(), + EnvelopeEventId::from_bytes([44; 32]), + retained.intent().targets(), + retained.intent().policy(), + retained.intent().deadline_unix_ms(), + ) + .unwrap(); + assert!( + report( + &retained, + mismatched, + retained.raw_json(), + retained.retained_at(), + vec![1] + ) + .is_err() + ); + assert!( + report( + &retained, + retained.intent().clone(), + retained.raw_json(), + UnixSeconds::new(ready.work.queued_at().get() - 1).unwrap(), + vec![1] + ) + .is_err() + ); + for ordinals in [vec![], vec![0], vec![13]] { + assert!( + report( + &retained, + retained.intent().clone(), + retained.raw_json(), + retained.retained_at(), + ordinals + ) + .is_err() + ); + } + let expired = DeliveryIntent::new( + retained.intent().request_id().as_str(), + retained.intent().event_id(), + retained.intent().targets(), + retained.intent().policy(), + NonZeroU64::new(time * 1000).unwrap(), + ) + .unwrap(); + assert!( + report( + &retained, + expired, + retained.raw_json(), + retained.retained_at(), + vec![1] + ) + .is_err() + ); + let future = outer_variant(&ready, 1059, time + 1, Some(2)); + let future_candidate = + EnvelopeCandidate::from_obtained(ready.work, &future, &wrapping_key).unwrap(); + let future_intent = DeliveryIntent::new( + retained.intent().request_id().as_str(), + future_candidate.event_id(), + retained.intent().targets(), + retained.intent().policy(), + retained.intent().deadline_unix_ms(), + ) + .unwrap(); + assert!( + report( + &retained, + future_intent, + &future, + retained.retained_at(), + vec![1] + ) + .is_err() + ); + assert_eq!( + ready + .query(&ready.store, retained.intent().request_id()) + .await + .raw_json(), + retained.raw_json() + ); + ready.store.close().await.unwrap(); +} + +#[tokio::test] +async fn delivery_projections_reject_foreign_payloads_targets_ordinals_and_duplicate_facts() { + let ready = Ready::new(&[String::from("ws://127.0.0.1:9")], 247).await; + let retained = ready.retain(1).await; + let summary = DeliveryAttemptSummary::from_attempt(&retained); + let accepted = observation(&retained, DeliveryOutcome::accepted()); + assert!(!summary.acceptance_satisfied(&[]).unwrap()); + assert!( + summary + .acceptance_satisfied(std::slice::from_ref(&accepted)) + .unwrap() + ); + assert_eq!(summary.work(), retained.work()); + assert_eq!(summary.transaction_id(), retained.transaction_id()); + assert_eq!(summary.retained_at(), retained.retained_at()); + assert_eq!(summary.target_ordinals(), retained.target_ordinals()); + let foreign = Target::nostr_relay("wss://foreign.example").unwrap(); + assert!( + DeliveryObservation::from_store_report( + &retained, + &DeliveryTargetReceipt::attempted(foreign, DeliveryOutcome::accepted()), + None + ) + .is_err() + ); + let target = retained.intent().targets().targets()[0].clone(); + assert!( + DeliveryObservation::from_store_report( + &retained, + &DeliveryTargetReceipt::attempted(target.clone(), DeliveryOutcome::delivered()), + None + ) + .is_err() + ); + assert!( + DeliveryObservation::from_store_report( + &retained, + &DeliveryTargetReceipt::attempted(target, DeliveryOutcome::accepted()), + NonZeroU64::new(253_402_300_800_000) + ) + .is_err() + ); + let different_bytes = report( + &retained, + retained.intent().clone(), + &format!("{} ", retained.raw_json()), + retained.retained_at(), + vec![1], + ) + .unwrap(); + let wrong_payload = observation(&different_bytes, DeliveryOutcome::accepted()); + assert!( + summary + .acceptance_satisfied(std::slice::from_ref(&wrong_payload)) + .is_err() + ); + assert!( + snapshot(&ready) + .await + .with_delivery(vec![summary.clone()], vec![wrong_payload]) + .is_err() + ); + let wrong_ordinal = report( + &retained, + retained.intent().clone(), + retained.raw_json(), + retained.retained_at(), + vec![2], + ) + .unwrap(); + assert!( + snapshot(&ready) + .await + .with_delivery( + vec![DeliveryAttemptSummary::from_attempt(&wrong_ordinal)], + vec![] + ) + .is_err() + ); + assert!( + snapshot(&ready) + .await + .with_delivery( + vec![summary.clone()], + vec![observation(&wrong_ordinal, DeliveryOutcome::accepted())] + ) + .is_err() + ); + assert!( + snapshot(&ready) + .await + .with_delivery(vec![summary.clone(), summary.clone()], vec![]) + .is_err() + ); + assert!( + snapshot(&ready) + .await + .with_delivery( + vec![summary.clone()], + vec![accepted.clone(), accepted.clone()] + ) + .is_err() + ); + assert!( + snapshot(&ready) + .await + .with_delivery(vec![summary.clone(); 385], vec![]) + .is_err() + ); + assert!( + snapshot(&ready) + .await + .with_delivery(vec![summary.clone()], vec![accepted.clone(); 2305]) + .is_err() + ); + assert!( + snapshot(&ready) + .await + .with_installation(None, vec![]) + .is_err() + ); + let missing = snapshot(&ready) + .await + .with_delivery(vec![], vec![]) + .unwrap(); + assert!(!missing.publication_accepted(ready.work).unwrap()); + let projected = snapshot(&ready) + .await + .with_delivery(vec![summary], vec![accepted]) + .unwrap(); + assert!(projected.publication_accepted(ready.work).unwrap()); + assert_eq!(projected.delivery_attempts().len(), 1); + assert!( + ready + .observations(&ready.store, retained.intent().request_id()) + .await + .is_empty() + ); + ready.store.close().await.unwrap(); +} diff --git a/crates/radroots/tests/support/receipt_installation.rs b/crates/radroots/tests/support/receipt_installation.rs @@ -422,7 +422,7 @@ async fn predecessor_schema_five_retains_actual_claims_and_obtained_bytes_withou 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", + "DROP TABLE chat_delivery_observations; DROP TABLE chat_delivery_parts; DROP TABLE chat_delivery_attempts; DROP TABLE chat_publication_work; DROP TABLE chat_messages; PRAGMA user_version=5", ) .execute(&mut database) .await 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_delivery.rs"] +mod delivery; #[path = "receipt_installation.rs"] mod installation; #[path = "receipt_issuance.rs"] @@ -28,6 +30,14 @@ fn plan( created: u64, suffix: &str, ) -> ChatSigningPlan { + plan_with_routes(command, created, suffix, None) +} +fn plan_with_routes( + command: &radroots_protocol::command::CapturedChatCommand, + created: u64, + suffix: &str, + selected: Option<&[String]>, +) -> ChatSigningPlan { let rumor = ChatRumor::from_command(command, UnixSeconds::new(created).unwrap()).unwrap(); let author = keys(1); let mut seals = Vec::new(); @@ -55,13 +65,20 @@ fn plan( } else { keys(2) }; - let url = format!("wss://relay.example/{suffix}"); + let urls = selected.map_or_else( + || vec![format!("wss://relay.example/{suffix}")], + <[String]>::to_vec, + ); let source = nostr::UnsignedEvent { id: None, pubkey: signer.public_key(), created_at: nostr::Timestamp::from_secs(created - 1), kind: nostr::Kind::Custom(10050), - tags: nostr::Tags::from_list(vec![nostr::Tag::parse(["relay", &url]).unwrap()]), + tags: nostr::Tags::from_list( + urls.iter() + .map(|url| nostr::Tag::parse(["relay", url]).unwrap()) + .collect(), + ), content: "public signed route provenance ".repeat(1000), } .sign_with_keys(&signer) @@ -72,7 +89,7 @@ fn plan( *recipient, EnvelopeEventId::from_bytes(*source.id.as_bytes()), &source.as_json(), - &[url], + &urls, ) .unwrap(), ); diff --git a/crates/storage/README.md b/crates/storage/README.md @@ -323,3 +323,26 @@ 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. + +Delivery preparation retains an exact outer representation and complete bounded +target intent against actual original publication work before transport. Only +an actual fresh confirmed claim may proceed once through its qualified owner; +replay and unknown outcomes grant no dispatch right. Retries retain new attempt +identities while reusing original exact bytes. Canonical endpoint budgets cannot +be reset by changing a target scope. + +The first full target identity set and completion policy stay fixed for each +recipient's work. Each attempt independently retains a nonempty exact subset; +only those endpoints spend attempt slots. Shared acceptance evaluation accumulates +original target facts across retries without weakening the full criterion. +An exhausted optional endpoint cannot block a required endpoint. A distinct +current-access attempt may retain a new delivery deadline, while replay keeps +its original deadline and never repeats participant issuance. + +`record_delivery_observation` appends independently scoped accepted, rejected, +timeout, disconnect, unknown and unattempted facts. Original credential and +actual retained attempt linkage are required; late capture grants no new lease +or read rights. The first original optional time in each class remains unchanged. +Snapshots carry bounded summaries and original observations without duplicating +raw outer bytes. Their v3 progress digest includes these facts; structural +construction and an adapter's report establish no backend qualification. diff --git a/crates/storage/src/command.rs b/crates/storage/src/command.rs @@ -11,9 +11,17 @@ pub use radroots_protocol::chat::ChatSigningPlan; use radroots_protocol::command::CapturedChatCommand; use radroots_transport::BoxFuture; +mod delivery; +mod delivery_summary; mod installation; mod issuance; +mod observation; mod snapshot; +pub use delivery::{ + DeliveryAttempt, DeliveryAttemptOutcome, DeliveryAttemptRequest, DeliveryIntent, + EnvelopeCandidate, +}; +pub use delivery_summary::DeliveryAttemptSummary; pub use installation::{ ChatInstallation, InstallationOutcome, InstallationRequest, PublicationWork, }; @@ -21,6 +29,7 @@ pub use issuance::{ CapturedSeal, ClaimAttemptOutcome, ClaimAttemptRequest, EvidenceCaptureOutcome, EvidenceReceipt, SealEvidence, SigningAttempt, }; +pub use observation::{DeliveryObservation, DeliveryObservationKind, DeliveryObservationOutcome}; pub use snapshot::{ClaimSnapshot, OperationSnapshot, RecipientSnapshot, RetainedOperation}; /// Fixed operation-store failures known before a successful admission commit. @@ -47,6 +56,8 @@ pub enum OperationError { StorageFailure, /// State, completion or decoding requires explicit reconciliation. RecoveryRequired, + /// The retained target's twelve potential attempts require operator review. + DeliveryAttemptLimit, } impl OperationError { /// Returns an input-free diagnostic code. @@ -62,6 +73,7 @@ impl OperationError { Self::IdempotencyConflict => "idempotency_conflict", Self::StorageFailure => "storage_failure", Self::RecoveryRequired => "recovery_required", + Self::DeliveryAttemptLimit => "delivery_attempt_limit", } } } @@ -260,6 +272,46 @@ 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 { + /// Appends obtained target evidence against the actual original retained + /// attempt. Original credential authentication is required; late capture + /// does not acquire a fresh lease, deadline, grant or dispatch permission. + fn record_delivery_observation( + &self, + binding: WorkspaceBinding, + proof: AttachmentProof, + observation: DeliveryObservation, + ) -> BoxFuture<'_, Result<DeliveryObservationOutcome, OperationError>>; + /// Returns original per-target observations under current access. Startup + /// and query never initiate delivery or imply a missing acknowledgement. + fn delivery_observations( + &self, + binding: WorkspaceBinding, + operation: radroots_domain::OperationId, + digest: radroots_domain::CommandDigest, + recipient: radroots_domain::PublicKey, + request: radroots_transport::sink::DeliveryRequestId, + proof: AttachmentProof, + ) -> BoxFuture<'_, Result<Vec<DeliveryObservation>, OperationError>>; + /// Atomically retains exact outer bytes and every selected target intent + /// before transport. A fresh actual commit may be dispatched once by its + /// qualified owner. Restored or unknown claims never permit replay. + fn prepare_delivery_attempt<'a>( + &'a self, + request: DeliveryAttemptRequest, + proof: AttachmentProof, + candidate: Option<&'a EnvelopeCandidate>, + ) -> BoxFuture<'a, Result<DeliveryAttemptOutcome, OperationError>>; + /// Reads an original attempt under current access, including after expiry; + /// the returned report supplies no dispatch or safe-nonissuance proof. + fn delivery_attempt( + &self, + binding: WorkspaceBinding, + operation: OperationId, + digest: CommandDigest, + recipient: radroots_domain::PublicKey, + request: radroots_transport::sink::DeliveryRequestId, + proof: AttachmentProof, + ) -> BoxFuture<'_, Result<Option<DeliveryAttempt>, OperationError>>; /// 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. diff --git a/crates/storage/src/command/delivery.rs b/crates/storage/src/command/delivery.rs @@ -0,0 +1,478 @@ +//! Bounded private delivery data. Constructors authenticate carriers only; +//! actual protected work, containment, authorization and dispatch belong to +//! qualified store/execution owners, never to a report or enum variant. + +use super::{CommitAttempt, OperationError, PublicationWork}; +use crate::workspace::WorkspaceBinding; +use core::{fmt, num::NonZeroU64}; +use radroots_domain::{EnvelopeEventId, PublicKey, TransactionId, UnixSeconds}; +use radroots_event::{ + SignedEvent, + wire::{EventWireLimits, Nip01EventWire}, +}; +use radroots_event_codec::verify::{self, Nip01SignatureVerifier, RawEvent}; +use radroots_transport::{ + TargetSet, TransportId, + policy::{SatisfactionClass, SatisfactionPolicy}, + sink::{DeliveryPayload, DeliveryRequest, DeliveryRequestId}, +}; +use sha2::{Digest, Sha256}; +use std::{boxed::Box, collections::BTreeSet, string::String, vec, vec::Vec}; +use zeroize::Zeroizing; + +const SIGNED_MAX_BYTES: usize = 65_536; +const MAX_TIME: u64 = 253_402_300_799; + +fn outer(raw: &str, recipient: PublicKey) -> Result<SignedEvent, OperationError> { + if raw.len() > SIGNED_MAX_BYTES { + return Err(OperationError::InvalidEvidence); + } + let limits = EventWireLimits { + max_raw_json_bytes: SIGNED_MAX_BYTES, + max_extra_fields: 0, + max_total_extra_json_bytes: 0, + ..Default::default() + }; + let wire = Nip01EventWire::parse_json_exact_with_limits(raw, limits) + .map_err(|_| OperationError::InvalidEvidence)?; + if wire.kind != 1059 + || wire.created_at == 0 + || wire.created_at > MAX_TIME + || wire.tags != [vec![String::from("p"), recipient.to_hex()]] + { + return Err(OperationError::InvalidEvidence); + } + let event = SignedEvent::from_wire_verified_id(wire, raw) + .map_err(|_| OperationError::InvalidEvidence)?; + let checked = verify::id(RawEvent::new(event.envelope().clone())) + .map_err(|_| OperationError::InvalidEvidence)?; + verify::signature(checked, &Nip01SignatureVerifier) + .map_err(|_| OperationError::InvalidEvidence)?; + Ok(event) +} + +/// One obtained outer awaiting protected native verification and retention. +/// The key is temporary wrapping material, not participant custody. A matching +/// signature proves neither original-seal containment nor an actual store link. +pub struct EnvelopeCandidate { + work: PublicationWork, + event: EnvelopeEventId, + author: PublicKey, + created_at: UnixSeconds, + digest: [u8; 32], + raw: Zeroizing<String>, + key: Zeroizing<[u8; 32]>, +} +impl EnvelopeCandidate { + /// Bounds before copying and verifies the exact one-recipient outer carrier. + /// Native capture must independently derive the key and decrypt its content + /// against actual original retained seal bytes before any transport right. + pub fn from_obtained( + work: PublicationWork, + raw: &str, + wrapping_key: &[u8; 32], + ) -> Result<Self, OperationError> { + let carrier = outer(raw, work.evidence().recipient())?; + Ok(Self { + work, + event: EnvelopeEventId::from_bytes(*carrier.envelope().id().as_bytes()), + author: *carrier.envelope().author(), + created_at: UnixSeconds::new(carrier.envelope().created_at_u64()) + .map_err(|_| OperationError::InvalidEvidence)?, + digest: Sha256::digest(raw.as_bytes()).into(), + raw: Zeroizing::new(String::from(raw)), + key: Zeroizing::new(*wrapping_key), + }) + } + /// Reported original work; a getter does not authenticate backend state. + pub const fn work(&self) -> PublicationWork { + self.work + } + /// Actual signed outer event identity, separate from the original seal. + pub const fn event_id(&self) -> EnvelopeEventId { + self.event + } + /// Actual outer author; native capture must prove this temporary key. + pub const fn author(&self) -> PublicKey { + self.author + } + /// Signed randomized outer time, not a grant or trusted clock observation. + pub const fn created_at(&self) -> UnixSeconds { + self.created_at + } + /// SHA-256 of the exact original outer representation. + pub const fn digest(&self) -> &[u8; 32] { + &self.digest + } + /// Exact obtained bytes for protected handling; never diagnostic output. + pub fn raw_json(&self) -> &str { + &self.raw + } + /// Scoped borrow for native containment verification only. + pub fn with_wrapping_key<T>(&self, verify: impl FnOnce(&[u8; 32]) -> T) -> T { + verify(&self.key) + } + /// Explicit bounded owner copy; the caller retains its original until the + /// actual outcome is reconciled. This creates no persistence fact. + pub fn retained_copy(&self) -> Self { + Self { + work: self.work, + event: self.event, + author: self.author, + created_at: self.created_at, + digest: self.digest, + raw: Zeroizing::new(String::from(self.raw_json())), + key: Zeroizing::new(*self.key), + } + } +} +impl fmt::Debug for EnvelopeCandidate { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("EnvelopeCandidate(<redacted>)") + } +} + +/// Immutable one-attempt transport intent. Every target is explicit; neither +/// policy nor target construction proves an original route approval or lease. +#[derive(Clone, Eq, PartialEq)] +pub struct DeliveryIntent { + request: DeliveryRequestId, + event: EnvelopeEventId, + targets: TargetSet, + policy: SatisfactionPolicy, + selection: Box<[radroots_transport::target::TargetFingerprint]>, + publication_policy: [u8; 32], + deadline: NonZeroU64, + digest: [u8; 32], +} +impl DeliveryIntent { + /// Captures a full 256-bit lowercase-hex request identity, at most sixteen + /// Nostr targets and explicit relay-acceptance policy. Relay acceptance + /// cannot prove recipient delivery. Owned copies shed caller spare capacity. + pub fn new( + request_id: &str, + event: EnvelopeEventId, + targets: &TargetSet, + policy: &SatisfactionPolicy, + deadline: NonZeroU64, + ) -> Result<Self, OperationError> { + if request_id.len() != 64 + || !request_id + .bytes() + .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) + || targets.len() > 16 + || targets.is_empty() + || deadline.get() > MAX_TIME * 1000 + 999 + || policy.class() != SatisfactionClass::Accepted + || targets + .targets() + .iter() + .any(|t| t.kind() != &TransportId::NOSTR || t.uri().as_str().len() > 2048) + { + return Err(OperationError::InvalidCommand); + } + policy + .validate_for(targets) + .map_err(|_| OperationError::InvalidCommand)?; + let mut endpoints = BTreeSet::new(); + if targets + .targets() + .iter() + .any(|target| !endpoints.insert(target.uri().as_str())) + { + return Err(OperationError::InvalidCommand); + } + let request = + DeliveryRequestId::parse(request_id).map_err(|_| OperationError::InvalidCommand)?; + let mut hash = Sha256::new(); + hash.update(b"radroots.private.delivery.publication_policy.v1\0"); + hash.update((targets.len() as u16).to_be_bytes()); + for target in targets.targets() { + hash.update(target.fingerprint().as_str().as_bytes()); + } + let selected = policy.targets(); + if selected.is_any() { + hash.update([0]); + } else if selected.is_all() { + hash.update([1]); + } else if let Some(quorum) = selected.quorum_threshold() { + hash.update([2]); + hash.update(quorum.to_be_bytes()); + } else if let Some(required) = selected.required_targets() { + hash.update([3]); + hash.update((required.len() as u16).to_be_bytes()); + for target in required { + hash.update(target.as_str().as_bytes()); + } + } else { + return Err(OperationError::InvalidCommand); + } + let mut value = Self { + request, + event, + targets: targets.clone(), + policy: policy.clone(), + selection: targets + .targets() + .iter() + .map(|target| target.fingerprint().clone()) + .collect(), + publication_policy: hash.finalize().into(), + deadline, + digest: [0; 32], + }; + value.refresh_digest(); + Ok(value) + } + fn refresh_digest(&mut self) { + let mut hash = Sha256::new(); + hash.update(b"radroots.private.delivery.intent.v1\0"); + hash.update(self.request.as_str().as_bytes()); + hash.update(self.event.as_bytes()); + hash.update(self.publication_policy); + hash.update(self.deadline.get().to_be_bytes()); + hash.update((self.selection.len() as u16).to_be_bytes()); + for target in &self.selection { + hash.update(target.as_str().as_bytes()); + } + self.digest = hash.finalize().into(); + } + /// Selects a nonempty exact subset for this potential invocation, retaining + /// the full original target set, completion policy and deadline. Only selected + /// endpoints spend attempt budgets. Data construction grants no dispatch right. + pub fn with_selection(mut self, selected: &TargetSet) -> Result<Self, OperationError> { + if selected.is_empty() + || selected + .targets() + .iter() + .any(|target| !self.targets.targets().contains(target)) + { + return Err(OperationError::InvalidCommand); + } + self.selection = self + .targets + .targets() + .iter() + .filter(|target| selected.contains(target.fingerprint())) + .map(|target| target.fingerprint().clone()) + .collect(); + self.refresh_digest(); + Ok(self) + } + /// Complete original publication criterion, excluding attempt ID, envelope + /// selection and per-attempt deadline. The native owner freezes this + /// criterion for the original recipient work. + pub const fn publication_policy_digest(&self) -> &[u8; 32] { + &self.publication_policy + } + /// Selected target identities in original full-set order. + pub fn selection(&self) -> &[radroots_transport::target::TargetFingerprint] { + &self.selection + } + /// Exact selected descriptors in original full-set order. + pub fn selected_targets(&self) -> impl Iterator<Item = &radroots_transport::Target> { + self.targets + .targets() + .iter() + .filter(|target| self.selection.contains(target.fingerprint())) + } + /// Builds the bounded exact subset for `EventSink::deliver_selected`. + /// The full original transport request must still be validated separately. + pub fn selected_target_set(&self) -> Result<TargetSet, OperationError> { + TargetSet::new(self.selected_targets().cloned().collect()) + .map_err(|_| OperationError::InvalidCommand) + } + /// Exact full attempt identity, not diagnostic correlation. + pub const fn request_id(&self) -> &DeliveryRequestId { + &self.request + } + /// Actual selected outer identity. + pub const fn event_id(&self) -> EnvelopeEventId { + self.event + } + /// Original selected target descriptors in request order. + pub const fn targets(&self) -> &TargetSet { + &self.targets + } + /// Explicit relay-acceptance law, independent of business installation. + pub const fn policy(&self) -> &SatisfactionPolicy { + &self.policy + } + /// Original absolute deadline; replay grants no fresh time window. + pub const fn deadline_unix_ms(&self) -> NonZeroU64 { + self.deadline + } + /// Commitment to identity, envelope, deadline, ordered target identities and policy. + /// Human target labels are diagnostic and excluded. + pub const fn digest(&self) -> &[u8; 32] { + &self.digest + } +} +impl fmt::Debug for DeliveryIntent { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("DeliveryIntent(<redacted>)") + } +} + +/// Selects actual retained publication work for one potential transport attempt. +pub struct DeliveryAttemptRequest { + binding: WorkspaceBinding, + work: PublicationWork, + intent: DeliveryIntent, +} +impl DeliveryAttemptRequest { + /// Structural scope comparison only; a native owner must authenticate the + /// original complete graph, current grant and exact selected routes. + pub fn new( + binding: WorkspaceBinding, + work: PublicationWork, + intent: DeliveryIntent, + ) -> Result<Self, OperationError> { + if binding.input().workspace_id() != work.evidence().workspace_id() { + return Err(OperationError::InvalidCommand); + } + Ok(Self { + binding, + work, + intent, + }) + } + /// Reported authenticated binding, subject to current native revalidation. + pub const fn binding(&self) -> &WorkspaceBinding { + &self.binding + } + /// Immutable reported work; native capture compares its actual protected row. + pub const fn work(&self) -> PublicationWork { + self.work + } + /// Exact requested transport intent. + pub const fn intent(&self) -> &DeliveryIntent { + &self.intent + } + /// Original operation anchor; no outcome is inferred from construction. + pub fn identity(&self) -> CommitAttempt { + let e = self.work.evidence(); + CommitAttempt::new(e.workspace_id(), e.operation_id(), e.command_digest(), None) + } +} +impl fmt::Debug for DeliveryAttemptRequest { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("DeliveryAttemptRequest(<redacted>)") + } +} + +/// Complete original attempt report and exact bytes. Constructing or querying +/// this value never permits replay of a potential invocation. +pub struct DeliveryAttempt { + work: PublicationWork, + intent: DeliveryIntent, + raw: Zeroizing<String>, + digest: [u8; 32], + transaction: TransactionId, + retained_at: UnixSeconds, + ordinals: Box<[u8]>, +} +impl DeliveryAttempt { + /// Checks scoped data and carrier authenticity only. Native backends must + /// independently verify original protected state and execute retention. + pub fn from_store_report( + work: PublicationWork, + intent: DeliveryIntent, + raw: &str, + transaction: TransactionId, + retained_at: UnixSeconds, + ordinals: Vec<u8>, + ) -> Result<Self, OperationError> { + let event = outer(raw, work.evidence().recipient())?; + if event.envelope().id().as_bytes() != intent.event_id().as_bytes() + || retained_at < work.queued_at() + || event.envelope().created_at_u64() > retained_at.get() + || retained_at + .get() + .checked_mul(1000) + .is_none_or(|now| now >= intent.deadline_unix_ms().get()) + || ordinals.len() != intent.selection().len() + || ordinals.iter().any(|n| !(1..=12).contains(n)) + { + return Err(OperationError::RecoveryRequired); + } + Ok(Self { + work, + intent, + raw: Zeroizing::new(String::from(raw)), + digest: Sha256::digest(raw.as_bytes()).into(), + transaction, + retained_at, + ordinals: ordinals.into_boxed_slice(), + }) + } + /// Original protected publication reference. + pub const fn work(&self) -> PublicationWork { + self.work + } + /// Original complete transport intent. + pub const fn intent(&self) -> &DeliveryIntent { + &self.intent + } + /// Exact originally retained outer representation. + pub fn raw_json(&self) -> &str { + &self.raw + } + /// Digest of the exact retained representation. + pub const fn envelope_digest(&self) -> &[u8; 32] { + &self.digest + } + /// Original actual store transaction. + pub const fn transaction_id(&self) -> TransactionId { + self.transaction + } + /// Original retention time; never a renewed dispatch deadline. + pub const fn retained_at(&self) -> UnixSeconds { + self.retained_at + } + /// Per-target potential invocation numbers, in exact target order. + pub fn target_ordinals(&self) -> &[u8] { + &self.ordinals + } + /// Reconstructs the full original request. A qualified owner must use + /// `EventSink::deliver_selected` with `intent().selected_target_set()`; + /// unselected endpoints have no retained invocation. This data conversion + /// grants no dispatch permission or automatic retry. + pub fn transport_request(&self) -> Result<DeliveryRequest, OperationError> { + let event = outer(self.raw_json(), self.work.evidence().recipient())?; + DeliveryRequest::new( + self.intent.request_id().as_str(), + DeliveryPayload::new(event), + self.intent.targets().clone(), + self.intent.policy().clone(), + self.intent.deadline_unix_ms().get(), + ) + .map_err(|_| OperationError::RecoveryRequired) + } +} +impl fmt::Debug for DeliveryAttempt { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("DeliveryAttempt(<redacted>)") + } +} + +/// Only an actual freshly committed native claim can permit its qualified +/// owner to proceed once after current route/provider/access checks. +pub enum DeliveryAttemptOutcome { + /// Actual new protected attempt and exact bytes committed before transport. + Claimed(Box<DeliveryAttempt>), + /// Existing potential invocation, never permission to dispatch again. + Retained(Box<DeliveryAttempt>), + /// Reconcile this exact original identity before any further scheduling. + Unknown { + /// Original workspace/operation/command and optional transaction anchor. + operation: CommitAttempt, + /// Full original transport attempt identity. + attempt: DeliveryRequestId, + }, +} +impl fmt::Debug for DeliveryAttemptOutcome { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("DeliveryAttemptOutcome(<redacted>)") + } +} diff --git a/crates/storage/src/command/delivery_summary.rs b/crates/storage/src/command/delivery_summary.rs @@ -0,0 +1,93 @@ +use super::{ + DeliveryAttempt, DeliveryIntent, DeliveryObservation, OperationError, PublicationWork, +}; +use core::fmt; +use radroots_domain::{TransactionId, UnixSeconds}; + +/// Bounded original delivery references without another copy of its signed +/// bytes. This data projection grants no dispatch permission or storage proof. +#[derive(Clone, Eq, PartialEq)] +pub struct DeliveryAttemptSummary { + work: PublicationWork, + intent: DeliveryIntent, + digest: [u8; 32], + transaction: TransactionId, + retained_at: UnixSeconds, + ordinals: Box<[u8]>, +} +impl DeliveryAttemptSummary { + /// Copies original report facts, never a scheduling capability. + pub fn from_attempt(attempt: &DeliveryAttempt) -> Self { + Self { + work: attempt.work(), + intent: attempt.intent().clone(), + digest: *attempt.envelope_digest(), + transaction: attempt.transaction_id(), + retained_at: attempt.retained_at(), + ordinals: attempt.target_ordinals().into(), + } + } + /// Original protected publication linkage. + pub const fn work(&self) -> PublicationWork { + self.work + } + /// Complete immutable original transport intent. + pub const fn intent(&self) -> &DeliveryIntent { + &self.intent + } + /// SHA-256 of the original exact signed outer representation. + pub const fn envelope_digest(&self) -> &[u8; 32] { + &self.digest + } + /// Actual original retention transaction report. + pub const fn transaction_id(&self) -> TransactionId { + self.transaction + } + /// Original retention time, never a renewed lease or deadline. + pub const fn retained_at(&self) -> UnixSeconds { + self.retained_at + } + /// Original endpoint attempt numbers in target order. + pub fn target_ordinals(&self) -> &[u8] { + &self.ordinals + } + /// Evaluates only this attempt's retained exact target evidence with the + /// shared acceptance law. A success does not prove recipient delivery. + pub fn acceptance_satisfied( + &self, + observations: &[DeliveryObservation], + ) -> Result<bool, OperationError> { + let relevant = observations.iter().filter(|value| { + value.work() == self.work && value.request_id() == self.intent.request_id() + }); + for value in relevant.clone() { + if value.event_id() != self.intent.event_id() + || value.envelope_digest() != &self.digest + || !self + .intent + .targets() + .targets() + .contains(value.target_receipt().target()) + { + return Err(OperationError::InvalidEvidence); + } + } + radroots_transport::policy::evaluate_satisfaction( + self.intent.policy(), + self.intent.targets(), + relevant.map(|value| { + ( + value.target_receipt().target().fingerprint(), + value.target_receipt().outcome(), + ) + }), + ) + .map(|value| value == radroots_transport::policy::SatisfactionState::Satisfied) + .map_err(|_| OperationError::InvalidEvidence) + } +} +impl fmt::Debug for DeliveryAttemptSummary { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("DeliveryAttemptSummary(<redacted>)") + } +} diff --git a/crates/storage/src/command/observation.rs b/crates/storage/src/command/observation.rs @@ -0,0 +1,202 @@ +//! Bounded observations of an already retained exact transport attempt. + +use super::{DeliveryAttempt, OperationError, PublicationWork}; +use core::{fmt, num::NonZeroU64}; +use radroots_domain::EnvelopeEventId; +use radroots_transport::{ + DeliveryReceipt, SinkFailure, + outcome::DeliveryOutcomeKind, + sink::{DeliveryRequestId, DeliveryTargetReceipt}, + target::TargetFingerprint, +}; + +/// Independent append-only outcome classes. Each actual attempt/target has at +/// most one original observation of each class; a later class never erases it. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum DeliveryObservationKind { + /// The relay accepted this exact outer event, not recipient delivery. + Accepted, + /// The relay refused the event. + Rejected, + /// The bounded attempt ended without an acknowledgement before its deadline. + TimedOut, + /// The connection failed without establishing acceptance. + Disconnected, + /// The target result is uncertain or another normalized failure occurred. + Unknown, + /// The adapter explicitly reports that it did not attempt this target. + NotAttempted, +} +impl DeliveryObservationKind { + /// Fixed protected-record discriminator, never an authorization token. + pub const fn code(self) -> u8 { + match self { + Self::Accepted => 1, + Self::Rejected => 2, + Self::TimedOut => 3, + Self::Disconnected => 4, + Self::Unknown => 5, + Self::NotAttempted => 6, + } + } +} + +/// Exact attempt-bound normalized data. Construction authenticates no adapter +/// or storage. The native owner independently checks actual protected linkage. +#[derive(Clone, Eq, PartialEq)] +pub struct DeliveryObservation { + work: PublicationWork, + request: DeliveryRequestId, + event: EnvelopeEventId, + envelope_digest: [u8; 32], + target: DeliveryTargetReceipt, + kind: DeliveryObservationKind, + ordinal: u8, + observed_at: Option<NonZeroU64>, +} +impl DeliveryObservation { + /// Selects one exact target only after checking the complete receipt's + /// original request, target set and exact payload binding. + pub fn from_receipt( + attempt: &DeliveryAttempt, + receipt: &DeliveryReceipt, + target: &TargetFingerprint, + observed_at_unix_ms: Option<NonZeroU64>, + ) -> Result<Self, OperationError> { + receipt + .validate_for_request(&attempt.transport_request()?) + .map_err(|_| OperationError::InvalidEvidence)?; + let report = receipt + .target_receipts() + .iter() + .find(|report| report.target().fingerprint() == target) + .ok_or(OperationError::InvalidEvidence)?; + Self::from_store_report(attempt, report, observed_at_unix_ms) + } + + /// Selects actual partial evidence from a request-bound sink failure. + /// Missing target evidence remains absent; it is never invented as failure. + pub fn from_failure( + attempt: &DeliveryAttempt, + failure: &SinkFailure, + target: &TargetFingerprint, + observed_at_unix_ms: Option<NonZeroU64>, + ) -> Result<Self, OperationError> { + failure + .validate_for_request(&attempt.transport_request()?) + .map_err(|_| OperationError::InvalidEvidence)?; + let report = failure + .partial_evidence() + .iter() + .find(|report| report.target().fingerprint() == target) + .ok_or(OperationError::InvalidEvidence)?; + Self::from_store_report(attempt, report, observed_at_unix_ms) + } + + /// Reconstructs bounded data against an original attempt. This is not proof + /// of execution, remote acknowledgement, current access or a durable row. + pub fn from_store_report( + attempt: &DeliveryAttempt, + report: &DeliveryTargetReceipt, + observed_at_unix_ms: Option<NonZeroU64>, + ) -> Result<Self, OperationError> { + if observed_at_unix_ms.is_some_and(|time| time.get() > 253_402_300_799_999) + || !attempt + .intent() + .targets() + .targets() + .contains(report.target()) + || report.outcome().kind() == DeliveryOutcomeKind::Delivered + { + return Err(OperationError::InvalidEvidence); + } + let kind = if !report.was_attempted() { + DeliveryObservationKind::NotAttempted + } else { + match report.outcome().kind() { + DeliveryOutcomeKind::Accepted => DeliveryObservationKind::Accepted, + DeliveryOutcomeKind::Rejected => DeliveryObservationKind::Rejected, + DeliveryOutcomeKind::Failed | DeliveryOutcomeKind::Unavailable => { + match report.outcome().code() { + Some("timeout" | "deadline_exceeded") => DeliveryObservationKind::TimedOut, + Some("connection_failed" | "disconnected") => { + DeliveryObservationKind::Disconnected + } + _ => DeliveryObservationKind::Unknown, + } + } + DeliveryOutcomeKind::Delivered => return Err(OperationError::InvalidEvidence), + } + }; + let index = attempt + .intent() + .selection() + .iter() + .position(|target| target == report.target().fingerprint()) + .ok_or(OperationError::InvalidEvidence)?; + let ordinal = *attempt + .target_ordinals() + .get(index) + .ok_or(OperationError::InvalidEvidence)?; + Ok(Self { + work: attempt.work(), + request: attempt.intent().request_id().clone(), + event: attempt.intent().event_id(), + envelope_digest: *attempt.envelope_digest(), + target: report.clone(), + kind, + ordinal, + observed_at: observed_at_unix_ms, + }) + } + /// Original publication scope, independently checked by the native owner. + pub const fn work(&self) -> PublicationWork { + self.work + } + /// Full original potential invocation identity. + pub const fn request_id(&self) -> &DeliveryRequestId { + &self.request + } + /// Actual outer identity, separate from logical message and recipient seal. + pub const fn event_id(&self) -> EnvelopeEventId { + self.event + } + /// Digest of the exact original retained signed representation. + pub const fn envelope_digest(&self) -> &[u8; 32] { + &self.envelope_digest + } + /// Original normalized target evidence, for protected handling only. + pub const fn target_receipt(&self) -> &DeliveryTargetReceipt { + &self.target + } + /// Independent observation class; it changes no command installation fact. + pub const fn kind(&self) -> DeliveryObservationKind { + self.kind + } + /// Original potential invocation number for this exact selected endpoint. + pub const fn target_ordinal(&self) -> u8 { + self.ordinal + } + /// First original optional observation time. No ambient clock is consulted. + pub const fn observed_at_unix_ms(&self) -> Option<NonZeroU64> { + self.observed_at + } +} +impl fmt::Debug for DeliveryObservation { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("DeliveryObservation(<redacted>)") + } +} + +/// Actual owner outcome, separate from the observation's remote meaning. +pub enum DeliveryObservationOutcome { + /// Confirmed append or matching original first observation. + Committed(DeliveryObservation), + /// Reconcile this original observation before inferring durable capture. + Unknown(DeliveryObservation), +} +impl fmt::Debug for DeliveryObservationOutcome { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("DeliveryObservationOutcome(<redacted>)") + } +} diff --git a/crates/storage/src/command/snapshot.rs b/crates/storage/src/command/snapshot.rs @@ -1,8 +1,8 @@ //! Coherent retained graph reports. Values are not persistence qualification or //! dispatch permission; concrete owners must authenticate and compare all facts. use super::{ - BoundSigningPlan, ChatInstallation, EvidenceReceipt, OperationError, PreparedOperation, - PublicationWork, SigningAttempt, + BoundSigningPlan, ChatInstallation, DeliveryAttemptSummary, DeliveryObservation, + EvidenceReceipt, OperationError, PreparedOperation, PublicationWork, SigningAttempt, }; use core::fmt; use radroots_domain::{ @@ -11,7 +11,12 @@ use radroots_domain::{ }; use radroots_protocol::intent::IntentBinding; use sha2::{Digest, Sha256}; -use std::{boxed::Box, vec, vec::Vec}; +use std::{ + boxed::Box, + collections::{BTreeMap, BTreeSet}, + vec, + vec::Vec, +}; /// Fixed original claim facts, without another copy of its complete private plan. #[derive(Clone, Copy, Eq, PartialEq)] @@ -178,6 +183,8 @@ pub struct OperationSnapshot { installation: Option<ChatInstallation>, message: Option<ChatMessage>, publications: Box<[PublicationWork]>, + deliveries: Box<[DeliveryAttemptSummary]>, + observations: Box<[DeliveryObservation]>, } impl OperationSnapshot { /// Stable scoped progress identity over original transaction, command, @@ -187,7 +194,7 @@ impl OperationSnapshot { let prepared = self.prepared(); let identity = prepared.identity(); let mut digest = Sha256::new(); - digest.update(b"radroots.operation.snapshot.v2\0"); + digest.update(b"radroots.operation.snapshot.v3\0"); digest.update(identity.workspace_id().as_bytes()); digest.update(identity.operation_id().as_bytes()); digest.update(identity.command_digest().as_bytes()); @@ -262,6 +269,63 @@ impl OperationSnapshot { digest.update(work.transaction_id().as_bytes()); digest.update(work.queued_at().get().to_be_bytes()); } + for attempt in self.delivery_attempts() { + digest.update(attempt.work().evidence().recipient().as_bytes()); + digest.update(attempt.intent().digest()); + digest.update(attempt.envelope_digest()); + digest.update(attempt.transaction_id().as_bytes()); + digest.update(attempt.retained_at().get().to_be_bytes()); + digest.update(attempt.target_ordinals()); + } + for observation in self.delivery_observations() { + digest.update(observation.work().evidence().recipient().as_bytes()); + digest.update(observation.request_id().as_str().as_bytes()); + digest.update(observation.event_id().as_bytes()); + digest.update(observation.envelope_digest()); + digest.update( + observation + .target_receipt() + .target() + .fingerprint() + .as_str() + .as_bytes(), + ); + digest.update([observation.kind().code()]); + digest.update([ + observation.target_ordinal(), + u8::from(observation.target_receipt().was_attempted()), + ]); + let outcome = observation.target_receipt().outcome(); + digest.update([match outcome.kind() { + radroots_transport::outcome::DeliveryOutcomeKind::Accepted => 0, + radroots_transport::outcome::DeliveryOutcomeKind::Delivered => 1, + radroots_transport::outcome::DeliveryOutcomeKind::Rejected => 2, + radroots_transport::outcome::DeliveryOutcomeKind::Unavailable => 3, + radroots_transport::outcome::DeliveryOutcomeKind::Failed => 4, + }]); + digest.update([match outcome.retryability() { + radroots_transport::outcome::Retryability::NotApplicable => 0, + radroots_transport::outcome::Retryability::Retryable => 1, + radroots_transport::outcome::Retryability::Terminal => 2, + }]); + for value in [outcome.code(), outcome.message()] { + match value { + Some(value) => { + digest.update([1]); + digest.update((value.len() as u32).to_be_bytes()); + digest.update(value.as_bytes()); + } + None => digest.update([0]), + } + } + match observation.observed_at_unix_ms() { + Some(time) => { + digest.update([1]); + digest.update(time.get().to_be_bytes()); + } + None => digest.update([0]), + } + } radroots_domain::CheckpointDigest::from_bytes(digest.finalize().into()) } /// Checks the exact complete recipient set, original claim scope and obtained @@ -342,6 +406,8 @@ impl OperationSnapshot { installation: None, message: None, publications: Vec::new().into_boxed_slice(), + deliveries: Vec::new().into_boxed_slice(), + observations: Vec::new().into_boxed_slice(), }) } /// Adds structurally checked T2 reports to this data snapshot. The concrete @@ -352,6 +418,9 @@ impl OperationSnapshot { installation: Option<ChatInstallation>, publications: Vec<PublicationWork>, ) -> Result<Self, OperationError> { + if !self.deliveries.is_empty() || !self.observations.is_empty() { + return Err(OperationError::RecoveryRequired); + } let Some(installed) = installation else { if !publications.is_empty() { return Err(OperationError::RecoveryRequired); @@ -439,6 +508,163 @@ impl OperationSnapshot { pub fn publications(&self) -> &[PublicationWork] { &self.publications } + /// Adds bounded original D references and append-only target observations. + /// The native owner must authenticate and fence the complete actual graph; + /// constructing these values proves neither execution nor permission. + pub fn with_delivery( + mut self, + mut attempts: Vec<DeliveryAttemptSummary>, + mut observations: Vec<DeliveryObservation>, + ) -> Result<Self, OperationError> { + if attempts.len() > 384 || observations.len() > 2304 { + return Err(OperationError::RecoveryRequired); + } + attempts.sort_by(|a, b| { + ( + a.work().evidence().recipient(), + a.intent().request_id().as_str(), + ) + .cmp(&( + b.work().evidence().recipient(), + b.intent().request_id().as_str(), + )) + }); + let mut keys = BTreeSet::new(); + let mut transactions = BTreeSet::new(); + let mut counters = BTreeMap::<(PublicKey, &str), BTreeSet<u8>>::new(); + let mut slots = 0usize; + let mut policies = BTreeMap::new(); + for attempt in &attempts { + let recipient = attempt.work().evidence().recipient(); + slots = slots + .checked_add(attempt.intent().selection().len()) + .filter(|n| *n <= 384) + .ok_or(OperationError::RecoveryRequired)?; + if !self.publications.contains(&attempt.work()) + || !keys.insert((recipient, attempt.intent().request_id().as_str())) + || !transactions.insert(attempt.transaction_id()) + || attempt.target_ordinals().len() != attempt.intent().selection().len() + || policies + .insert(recipient, *attempt.intent().publication_policy_digest()) + .is_some_and(|previous| { + previous != *attempt.intent().publication_policy_digest() + }) + { + return Err(OperationError::RecoveryRequired); + } + for (target, ordinal) in attempt + .intent() + .selected_targets() + .zip(attempt.target_ordinals()) + { + if !(1..=12).contains(ordinal) + || !counters + .entry((recipient, target.uri().as_str())) + .or_default() + .insert(*ordinal) + { + return Err(OperationError::RecoveryRequired); + } + } + } + if counters + .values() + .any(|values| values.iter().copied().ne(1..=values.len() as u8)) + { + return Err(OperationError::RecoveryRequired); + } + observations.sort_by(|a, b| { + ( + a.work().evidence().recipient(), + a.request_id().as_str(), + a.target_receipt().target().fingerprint().as_str(), + a.kind().code(), + ) + .cmp(&( + b.work().evidence().recipient(), + b.request_id().as_str(), + b.target_receipt().target().fingerprint().as_str(), + b.kind().code(), + )) + }); + let mut seen = BTreeSet::new(); + for observation in &observations { + let attempt = attempts + .iter() + .find(|attempt| { + attempt.work() == observation.work() + && attempt.intent().request_id() == observation.request_id() + }) + .ok_or(OperationError::RecoveryRequired)?; + let ordinal = attempt + .intent() + .selection() + .iter() + .position(|target| target == observation.target_receipt().target().fingerprint()) + .and_then(|index| attempt.target_ordinals().get(index)) + .copied(); + if ordinal != Some(observation.target_ordinal()) { + return Err(OperationError::RecoveryRequired); + } + if observation.event_id() != attempt.intent().event_id() + || observation.envelope_digest() != attempt.envelope_digest() + || !attempt + .intent() + .targets() + .targets() + .contains(observation.target_receipt().target()) + || !seen.insert(( + observation.work().evidence().recipient(), + observation.request_id().as_str(), + observation.target_receipt().target().fingerprint().as_str(), + observation.kind().code(), + )) + { + return Err(OperationError::RecoveryRequired); + } + } + self.deliveries = attempts.into_boxed_slice(); + self.observations = observations.into_boxed_slice(); + Ok(self) + } + /// Original exact-envelope/target intent references, with no duplicated raw payload. + pub fn delivery_attempts(&self) -> &[DeliveryAttemptSummary] { + &self.deliveries + } + /// Individual original per-attempt target facts; absence is never an ACK. + pub fn delivery_observations(&self) -> &[DeliveryObservation] { + &self.observations + } + /// Evaluates the original full publication criterion across all exact + /// attempts for this work. Retry selection never narrows the criterion; + /// an earlier acceptance remains authoritative across later failures. + pub fn publication_accepted(&self, work: PublicationWork) -> Result<bool, OperationError> { + if !self.publications.contains(&work) { + return Err(OperationError::InvalidEvidence); + } + let Some(original) = self + .deliveries + .iter() + .find(|attempt| attempt.work() == work) + else { + return Ok(false); + }; + radroots_transport::policy::evaluate_satisfaction( + original.intent().policy(), + original.intent().targets(), + self.observations + .iter() + .filter(|value| value.work() == work) + .map(|value| { + ( + value.target_receipt().target().fingerprint(), + value.target_receipt().outcome(), + ) + }), + ) + .map(|state| state == radroots_transport::policy::SatisfactionState::Satisfied) + .map_err(|_| OperationError::RecoveryRequired) + } /// Original preparation, without a fabricated installed business result. pub fn prepared(&self) -> &PreparedOperation { self.retained.prepared() diff --git a/crates/storage_sqlite/Cargo.toml b/crates/storage_sqlite/Cargo.toml @@ -30,6 +30,7 @@ features = [] name = "radroots_storage_sqlite" [dependencies] +nostr = { workspace = true, default-features = false, features = ["std", "nip44"] } fs2 = { workspace = true } getrandom = { workspace = true } radroots_domain = { workspace = true, features = ["std", "serde"] } diff --git a/crates/storage_sqlite/README.md b/crates/storage_sqlite/README.md @@ -214,7 +214,7 @@ 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 6. Older namespaces are preserved and +The current workspace schema is version 7. 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. @@ -244,7 +244,7 @@ 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. Schema 6 has sixteen exact objects +after a lost observer and closes explicitly. Schema 7 has twenty exact objects and preserves/refuses predecessors without automatic rewriting. Native T2 uses that same connection owner. It authenticates the complete original @@ -262,5 +262,34 @@ 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. +observer loss. + +Native D uses the same owner. Before retention it authenticates the original +work, selected signed DM routes and outer signature, derives the temporary key's +public identity and decrypts the outer against exact original retained seal +bytes. Original actor, peer and selected-recipient keys are refused as temporary +authors, including for a self copy. The temporary wrapping key is never +persisted. Each protected frame is +bounded to 393216 bytes in at most twenty-four 16 KiB parts, with a manifest, +full original work linkage, exact representation digest, complete target intent +and immutable endpoint attempt numbers. Its protected aggregate is at most +2 MiB. Twelve potential invocations per recipient/canonical endpoint and at most +384 operation target slots bound retention; replay spends no additional slot. + +The first full target set and completion policy remain fixed per recipient work. +Each exact attempt retains a separate subset, and only that subset spends slots +or may be passed to selected delivery. Original acceptance facts accumulate +across retries under the full policy, without optional exhausted targets blocking +required targets. A distinct current-access attempt may use a fresh delivery +deadline; it changes neither an older attempt nor participant issuance. + +Only those exact retained bytes can become transport input. Original per-target +observations append independently in at most six classes per slot, each in a +bounded protected record. Missing ACKs and uncertain transport outcomes never +reverse installation or release participant claims. Late capture authenticates +the original credential and protected attempt without acquiring a fresh deadline, +clock observation, scheduling lease or read permission. The first original time, +including null, remains immutable. Current-access snapshots fence the complete +T0/T1/S/T2/D graph and all target observations before projecting bounded summaries. +Reopen performs no publication. Ordinary library-owned inbox orchestration, +browser/bindings, consumer adoption and final qualification remain separate work. diff --git a/crates/storage_sqlite/src/workspace/crypto.rs b/crates/storage_sqlite/src/workspace/crypto.rs @@ -27,6 +27,19 @@ pub(super) enum IssuancePayload { Publication, } +#[derive(Clone, Copy)] +pub(super) struct DeliveryScope { + pub workspace: WorkspaceId, + pub operation: OperationId, + pub recipient: radroots_domain::PublicKey, + pub request: [u8; 32], +} +#[derive(Clone, Copy)] +pub(super) enum DeliveryPayload { + Attempt, + Observation, +} + /// Explicit host-owned encryption material. This is a data-encryption key, /// independent of the participant signer and the scoped bearer credential. /// The host chooses its qualified provider and access policy; no fallback or @@ -37,6 +50,34 @@ pub struct WorkspaceCrypto { key: SecretMaterial, } impl WorkspaceCrypto { + pub(super) async fn seal_delivery( + &self, + scope: DeliveryScope, + payload: DeliveryPayload, + part: u8, + plaintext: &[u8], + ) -> Result<Vec<u8>, WorkspaceError> { + self.seal_bound( + delivery_context(scope, payload, part)?, + PRIVATE_PAYLOAD_MAX_BYTES, + plaintext, + ) + .await + } + pub(super) async fn open_delivery( + &self, + scope: DeliveryScope, + payload: DeliveryPayload, + part: u8, + protected: &[u8], + ) -> Result<SecretMaterial, WorkspaceError> { + self.open_bound( + delivery_context(scope, payload, part)?, + PRIVATE_PAYLOAD_MAX_BYTES, + protected, + ) + .await + } pub(super) async fn seal_message( &self, workspace: WorkspaceId, @@ -260,6 +301,40 @@ impl WorkspaceCrypto { Ok(plaintext) } } + +fn delivery_context( + scope: DeliveryScope, + payload: DeliveryPayload, + part: u8, +) -> Result<EnvelopeContext, WorkspaceError> { + use sha2::{Digest, Sha256}; + let (tag, purpose, schema) = match payload { + DeliveryPayload::Attempt => ( + 0u8, + "radroots.workspace.delivery_attempt", + "radroots.workspace.delivery_attempt.v1", + ), + DeliveryPayload::Observation => ( + 1u8, + "radroots.workspace.delivery_observation", + "radroots.workspace.delivery_observation.v1", + ), + }; + let mut hash = Sha256::new(); + hash.update(b"radroots.workspace.delivery.context.v1\0"); + hash.update([tag, part]); + hash.update(scope.workspace.as_bytes()); + hash.update(scope.operation.as_bytes()); + hash.update(scope.recipient.as_bytes()); + hash.update(scope.request); + let subject = radroots_domain::CommandDigest::from_bytes(hash.finalize().into()); + Ok(EnvelopeContext::new( + EnvelopePurpose::parse(purpose).map_err(|_| WorkspaceError::InvalidInput)?, + EnvelopeSubject::parse("workspace_delivery", subject.to_string()) + .map_err(|_| WorkspaceError::InvalidInput)?, + PayloadSchemaId::parse(schema).map_err(|_| WorkspaceError::InvalidInput)?, + )) +} impl fmt::Debug for WorkspaceCrypto { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("WorkspaceCrypto(<redacted>)") diff --git a/crates/storage_sqlite/src/workspace/delivery/codec.rs b/crates/storage_sqlite/src/workspace/delivery/codec.rs @@ -0,0 +1,441 @@ +use super::super::{ + crypto::{DeliveryPayload, DeliveryScope, PRIVATE_PAYLOAD_MAX_BYTES, WorkspaceCrypto}, + installation::EvidenceRef, +}; +use super::{MAX_BYTES, MAX_PROTECTED_BYTES}; +use radroots_domain::{EnvelopeEventId, TransactionId, UnixSeconds}; +use radroots_storage::command::{DeliveryAttempt, DeliveryIntent, OperationError, PublicationWork}; +use radroots_transport::{TargetSet, policy::SatisfactionPolicy}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use sqlx::{Row, SqliteConnection}; +use zeroize::Zeroizing; + +#[cfg(test)] +#[path = "codec_tests.rs"] +mod tests; + +#[derive(Serialize, Deserialize, Eq, PartialEq)] +#[serde(deny_unknown_fields)] +pub(super) struct WorkRef { + installation: TransactionId, + plan: [u8; 32], + evidence: EvidenceRef, + transaction: TransactionId, + queued_at: UnixSeconds, +} +impl WorkRef { + pub(super) fn new(work: PublicationWork) -> Self { + Self { + installation: work.installation_transaction_id(), + plan: *work.plan_digest(), + evidence: EvidenceRef::new(work.evidence()), + transaction: work.transaction_id(), + queued_at: work.queued_at(), + } + } +} + +#[derive(Serialize, Deserialize, Eq, PartialEq)] +#[serde(deny_unknown_fields)] +struct Header { + version: u8, + work: WorkRef, + request: String, + event: EnvelopeEventId, + intent: [u8; 32], + #[serde(deserialize_with = "super::json::targets")] + targets: TargetSet, + #[serde(deserialize_with = "super::json::small_vec")] + selection: Vec<radroots_transport::target::TargetFingerprint>, + #[serde(deserialize_with = "super::json::policy")] + policy: SatisfactionPolicy, + deadline: u64, + transaction: TransactionId, + retained_at: UnixSeconds, + #[serde(deserialize_with = "super::json::small_vec")] + ordinals: Vec<u8>, +} +impl Header { + fn new(value: &DeliveryAttempt) -> Self { + Self { + version: 1, + work: WorkRef::new(value.work()), + request: value.intent().request_id().as_str().into(), + event: value.intent().event_id(), + intent: *value.intent().digest(), + targets: value.intent().targets().clone(), + selection: value.intent().selection().to_vec(), + policy: value.intent().policy().clone(), + deadline: value.intent().deadline_unix_ms().get(), + transaction: value.transaction_id(), + retained_at: value.retained_at(), + ordinals: value.target_ordinals().to_vec(), + } + } + fn into_attempt( + self, + work: PublicationWork, + raw: &str, + ) -> Result<DeliveryAttempt, OperationError> { + if self.version != 1 || self.work != WorkRef::new(work) { + return Err(OperationError::RecoveryRequired); + } + let intent = DeliveryIntent::new( + &self.request, + self.event, + &self.targets, + &self.policy, + core::num::NonZeroU64::new(self.deadline).ok_or(OperationError::RecoveryRequired)?, + ) + .map_err(|_| OperationError::RecoveryRequired)?; + let selected = TargetSet::new( + intent + .targets() + .targets() + .iter() + .filter(|target| self.selection.contains(target.fingerprint())) + .cloned() + .collect(), + ) + .map_err(|_| OperationError::RecoveryRequired)?; + if selected + .targets() + .iter() + .map(|target| target.fingerprint()) + .ne(self.selection.iter()) + { + return Err(OperationError::RecoveryRequired); + } + let intent = intent + .with_selection(&selected) + .map_err(|_| OperationError::RecoveryRequired)?; + if intent.digest() != &self.intent { + return Err(OperationError::RecoveryRequired); + } + DeliveryAttempt::from_store_report( + work, + intent, + raw, + self.transaction, + self.retained_at, + self.ordinals, + ) + .map_err(|_| OperationError::RecoveryRequired) + } +} +#[derive(Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +struct Manifest { + version: u8, + work: WorkRef, + request: String, + event: EnvelopeEventId, + transaction: TransactionId, + parts: usize, + bytes: usize, + digest: [u8; 32], +} + +pub(super) fn request_key(request: &str) -> [u8; 32] { + let mut hash = Sha256::new(); + hash.update(b"radroots.delivery.request.key.v1\0"); + hash.update(request.as_bytes()); + hash.finalize().into() +} +pub(super) fn scope(work: PublicationWork, key: [u8; 32]) -> DeliveryScope { + let e = work.evidence(); + DeliveryScope { + workspace: e.workspace_id(), + operation: e.operation_id(), + recipient: e.recipient(), + request: key, + } +} +fn invalid<E>(_: E) -> OperationError { + OperationError::RecoveryRequired +} +fn bytes(row: &sqlx::sqlite::SqliteRow, index: usize) -> Result<Vec<u8>, OperationError> { + row.try_get::<Option<Vec<u8>>, _>(index) + .map_err(invalid)? + .ok_or(OperationError::RecoveryRequired) +} + +pub(super) struct ProtectedAttempt { + pub report: DeliveryAttempt, + pub manifest: Vec<u8>, + pub parts: Vec<Vec<u8>>, + pub bytes: usize, +} +pub(super) struct Fact { + pub scope: DeliveryScope, + pub event: EnvelopeEventId, + pub transaction: TransactionId, + pub bytes: usize, + pub manifest_digest: [u8; 32], + pub parts: Vec<[u8; 32]>, +} +impl ProtectedAttempt { + pub(super) fn fact(&self) -> Fact { + Fact { + scope: scope( + self.report.work(), + request_key(self.report.intent().request_id().as_str()), + ), + event: self.report.intent().event_id(), + transaction: self.report.transaction_id(), + bytes: self.bytes, + manifest_digest: Sha256::digest(&self.manifest).into(), + parts: self + .parts + .iter() + .map(|v| Sha256::digest(v).into()) + .collect(), + } + } +} + +pub(super) async fn protect( + report: DeliveryAttempt, + crypto: &WorkspaceCrypto, +) -> Result<ProtectedAttempt, OperationError> { + // Every target descriptor is independently within the private-record bound; + // the composite frame has the fixed 24-part owning-store bound. + for target in report.intent().targets().targets() { + if serde_json::to_vec(target) + .map_err(|_| OperationError::InvalidCommand)? + .len() + > PRIVATE_PAYLOAD_MAX_BYTES + { + return Err(OperationError::InvalidCommand); + } + } + let header = Zeroizing::new( + serde_json::to_vec(&Header::new(&report)).map_err(|_| OperationError::InvalidCommand)?, + ); + let size = 4usize + .checked_add(header.len()) + .and_then(|n| n.checked_add(report.raw_json().len())) + .filter(|n| *n <= MAX_BYTES) + .ok_or(OperationError::InvalidCommand)?; + let mut plain = Zeroizing::new(Vec::with_capacity(size)); + plain.extend_from_slice(&(header.len() as u32).to_be_bytes()); + plain.extend_from_slice(&header); + plain.extend_from_slice(report.raw_json().as_bytes()); + let selected = scope( + report.work(), + request_key(report.intent().request_id().as_str()), + ); + let mut parts = Vec::new(); + let mut total = 0usize; + for (index, chunk) in plain.chunks(PRIVATE_PAYLOAD_MAX_BYTES).enumerate() { + let protected = crypto + .seal_delivery(selected, DeliveryPayload::Attempt, (index + 1) as u8, chunk) + .await?; + total = total + .checked_add(protected.len()) + .filter(|n| *n <= MAX_PROTECTED_BYTES) + .ok_or(OperationError::InvalidEvidence)?; + parts.push(protected); + } + let manifest = Manifest { + version: 1, + work: WorkRef::new(report.work()), + request: report.intent().request_id().as_str().into(), + event: report.intent().event_id(), + transaction: report.transaction_id(), + parts: parts.len(), + bytes: plain.len(), + digest: Sha256::digest(&plain).into(), + }; + let encoded = + Zeroizing::new(serde_json::to_vec(&manifest).map_err(|_| OperationError::InvalidCommand)?); + let manifest = crypto + .seal_delivery(selected, DeliveryPayload::Attempt, 0, &encoded) + .await?; + if total + .checked_add(manifest.len()) + .is_none_or(|n| n > MAX_PROTECTED_BYTES) + { + return Err(OperationError::InvalidEvidence); + } + Ok(ProtectedAttempt { + report, + manifest, + parts, + bytes: plain.len(), + }) +} + +pub(super) async fn read( + connection: &mut SqliteConnection, + work: PublicationWork, + key: [u8; 32], + crypto: &WorkspaceCrypto, +) -> Result<Option<ProtectedAttempt>, OperationError> { + let s = scope(work, key); + let row=sqlx::query("SELECT CASE WHEN length(event_id)=32 THEN event_id END,CASE WHEN length(transaction_id)=32 THEN transaction_id END,part_count,byte_count,CASE WHEN length(protected)>0 AND length(protected)<=262144 THEN protected END FROM chat_delivery_attempts WHERE workspace=? AND operation_id=? AND recipient=? AND request_key=?") + .bind(s.workspace.as_bytes().as_slice()).bind(s.operation.as_bytes().as_slice()).bind(s.recipient.as_bytes().as_slice()).bind(s.request.as_slice()).fetch_optional(&mut *connection).await.map_err(invalid)?; + let Some(row) = row else { return Ok(None) }; + let event = bytes(&row, 0)?; + let transaction = bytes(&row, 1)?; + let manifest = bytes(&row, 4)?; + let count: i64 = row.try_get(2).map_err(invalid)?; + let size: i64 = row.try_get(3).map_err(invalid)?; + if !(1..=24).contains(&count) + || !(1..=MAX_BYTES as i64).contains(&size) + || (size as usize).div_ceil(PRIVATE_PAYLOAD_MAX_BYTES) != count as usize + { + return Err(OperationError::RecoveryRequired); + } + let totals:(i64,i64)=sqlx::query_as("SELECT count(*),coalesce(sum(n),0) FROM (SELECT length(protected) AS n FROM chat_delivery_parts WHERE workspace=? AND operation_id=? AND recipient=? AND request_key=? LIMIT 25)") + .bind(s.workspace.as_bytes().as_slice()).bind(s.operation.as_bytes().as_slice()).bind(s.recipient.as_bytes().as_slice()).bind(s.request.as_slice()).fetch_one(&mut *connection).await.map_err(invalid)?; + if totals.0 != count + || totals.1 <= 0 + || (totals.1 as usize) + .checked_add(manifest.len()) + .is_none_or(|n| n > MAX_PROTECTED_BYTES) + { + return Err(OperationError::RecoveryRequired); + } + let decoded = crypto + .open_delivery(s, DeliveryPayload::Attempt, 0, &manifest) + .await?; + let value: Manifest = decoded.expose_secret(|raw| { + if !super::json::bounded(raw, 32) { + return Err(OperationError::RecoveryRequired); + } + serde_json::from_slice(raw).map_err(invalid) + })?; + if value.version != 1 + || value.work != WorkRef::new(work) + || request_key(&value.request) != key + || value.event.as_bytes().as_slice() != event + || value.transaction.as_bytes().as_slice() != transaction + || value.parts != count as usize + || value.bytes != size as usize + { + return Err(OperationError::RecoveryRequired); + } + let rows=sqlx::query("SELECT part,CASE WHEN length(protected)>0 AND length(protected)<=262144 THEN protected END FROM chat_delivery_parts WHERE workspace=? AND operation_id=? AND recipient=? AND request_key=? ORDER BY part LIMIT 25") + .bind(s.workspace.as_bytes().as_slice()).bind(s.operation.as_bytes().as_slice()).bind(s.recipient.as_bytes().as_slice()).bind(s.request.as_slice()).fetch_all(&mut *connection).await.map_err(invalid)?; + let mut parts = Vec::with_capacity(rows.len()); + let mut plain = Zeroizing::new(Vec::with_capacity(value.bytes)); + for (index, row) in rows.iter().enumerate() { + if row.try_get::<i64, _>(0).map_err(invalid)? != index as i64 + 1 { + return Err(OperationError::RecoveryRequired); + } + let protected = bytes(row, 1)?; + let piece = crypto + .open_delivery(s, DeliveryPayload::Attempt, (index + 1) as u8, &protected) + .await?; + piece.expose_secret(|raw| { + if plain + .len() + .checked_add(raw.len()) + .is_none_or(|n| n > value.bytes) + { + return Err(OperationError::RecoveryRequired); + } + plain.extend_from_slice(raw); + Ok(()) + })?; + parts.push(protected); + } + if plain.len() != value.bytes + || <[u8; 32]>::from(Sha256::digest(&plain)) != value.digest + || plain.len() < 4 + { + return Err(OperationError::RecoveryRequired); + } + let header_size = u32::from_be_bytes(plain[..4].try_into().map_err(invalid)?) as usize; + let boundary = 4usize + .checked_add(header_size) + .filter(|n| *n < plain.len()) + .ok_or(OperationError::RecoveryRequired)?; + if !super::json::bounded(&plain[4..boundary], 32) { + return Err(OperationError::RecoveryRequired); + } + let header: Header = serde_json::from_slice(&plain[4..boundary]).map_err(invalid)?; + let report = header.into_attempt( + work, + core::str::from_utf8(&plain[boundary..]).map_err(invalid)?, + )?; + if report.intent().request_id().as_str() != value.request + || report.intent().event_id() != value.event + || report.transaction_id() != value.transaction + { + return Err(OperationError::RecoveryRequired); + } + Ok(Some(ProtectedAttempt { + report, + manifest, + parts, + bytes: value.bytes, + })) +} + +pub(super) async fn insert( + connection: &mut SqliteConnection, + value: &ProtectedAttempt, +) -> Result<(), OperationError> { + let s = scope( + value.report.work(), + request_key(value.report.intent().request_id().as_str()), + ); + sqlx::query("INSERT INTO chat_delivery_attempts(workspace,operation_id,recipient,request_key,event_id,transaction_id,part_count,byte_count,protected) VALUES(?,?,?,?,?,?,?,?,?)") + .bind(s.workspace.as_bytes().as_slice()).bind(s.operation.as_bytes().as_slice()).bind(s.recipient.as_bytes().as_slice()).bind(s.request.as_slice()).bind(value.report.intent().event_id().as_bytes().as_slice()).bind(value.report.transaction_id().as_bytes().as_slice()).bind(value.parts.len() as i64).bind(value.bytes as i64).bind(&value.manifest).execute(&mut *connection).await.map_err(|_|OperationError::StorageFailure)?; + for (index, part) in value.parts.iter().enumerate() { + sqlx::query("INSERT INTO chat_delivery_parts(workspace,operation_id,recipient,request_key,part,protected) VALUES(?,?,?,?,?,?)") + .bind(s.workspace.as_bytes().as_slice()).bind(s.operation.as_bytes().as_slice()).bind(s.recipient.as_bytes().as_slice()).bind(s.request.as_slice()).bind(index as i64+1).bind(part).execute(&mut *connection).await.map_err(|_|OperationError::StorageFailure)?; + } + Ok(()) +} + +impl Fact { + pub(super) async fn unchanged( + &self, + connection: &mut SqliteConnection, + ) -> Result<(), OperationError> { + let s = self.scope; + let row=sqlx::query("SELECT CASE WHEN length(event_id)=32 THEN event_id END,CASE WHEN length(transaction_id)=32 THEN transaction_id END,part_count,byte_count,CASE WHEN length(protected)>0 AND length(protected)<=262144 THEN protected END FROM chat_delivery_attempts WHERE workspace=? AND operation_id=? AND recipient=? AND request_key=?") + .bind(s.workspace.as_bytes().as_slice()).bind(s.operation.as_bytes().as_slice()).bind(s.recipient.as_bytes().as_slice()).bind(s.request.as_slice()).fetch_optional(&mut *connection).await.map_err(invalid)?.ok_or(OperationError::RecoveryRequired)?; + if bytes(&row, 0)? != self.event.as_bytes() + || bytes(&row, 1)? != self.transaction.as_bytes() + || row.try_get::<i64, _>(2).map_err(invalid)? != self.parts.len() as i64 + || row.try_get::<i64, _>(3).map_err(invalid)? != self.bytes as i64 + || <[u8; 32]>::from(Sha256::digest(bytes(&row, 4)?)) != self.manifest_digest + { + return Err(OperationError::RecoveryRequired); + } + // Compare one bounded ciphertext at a time. No provider, decryption, + // signing or network operation occurs while holding the write fence. + for (index, expected) in self.parts.iter().enumerate() { + let row=sqlx::query("SELECT CASE WHEN length(protected)>0 AND length(protected)<=262144 THEN protected END FROM chat_delivery_parts WHERE workspace=? AND operation_id=? AND recipient=? AND request_key=? AND part=?") + .bind(s.workspace.as_bytes().as_slice()).bind(s.operation.as_bytes().as_slice()).bind(s.recipient.as_bytes().as_slice()).bind(s.request.as_slice()).bind(index as i64+1).fetch_optional(&mut *connection).await.map_err(invalid)?.ok_or(OperationError::RecoveryRequired)?; + if <[u8; 32]>::from(Sha256::digest(bytes(&row, 0)?)) != *expected { + return Err(OperationError::RecoveryRequired); + } + } + let count:i64=sqlx::query_scalar("SELECT count(*) FROM (SELECT 1 FROM chat_delivery_parts WHERE workspace=? AND operation_id=? AND recipient=? AND request_key=? LIMIT 25)") + .bind(s.workspace.as_bytes().as_slice()).bind(s.operation.as_bytes().as_slice()).bind(s.recipient.as_bytes().as_slice()).bind(s.request.as_slice()).fetch_one(connection).await.map_err(invalid)?; + if count != self.parts.len() as i64 { + return Err(OperationError::RecoveryRequired); + } + Ok(()) + } +} + +pub(super) async fn absent( + connection: &mut SqliteConnection, + work: PublicationWork, + key: [u8; 32], +) -> Result<(), OperationError> { + let s = scope(work, key); + let count:i64=sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM chat_delivery_attempts WHERE workspace=?1 AND operation_id=?2 AND recipient=?3 AND request_key=?4)+EXISTS(SELECT 1 FROM chat_delivery_parts WHERE workspace=?1 AND operation_id=?2 AND recipient=?3 AND request_key=?4)") + .bind(s.workspace.as_bytes().as_slice()).bind(s.operation.as_bytes().as_slice()).bind(s.recipient.as_bytes().as_slice()).bind(s.request.as_slice()).fetch_one(connection).await.map_err(invalid)?; + if count != 0 { + return Err(OperationError::RecoveryRequired); + } + Ok(()) +} diff --git a/crates/storage_sqlite/src/workspace/delivery/codec_tests.rs b/crates/storage_sqlite/src/workspace/delivery/codec_tests.rs @@ -0,0 +1,358 @@ +//! Authenticated malformed records must fail closed without repairing storage. +#[cfg(test)] +mod cases { + use super::super::*; + use crate::workspace::tests::issuance_records::{DeliveryData, delivery_data}; + use nostr::JsonUtil; + use serde_json::{Value, json}; + + fn copy(report: &DeliveryAttempt) -> DeliveryAttempt { + DeliveryAttempt::from_store_report( + report.work(), + report.intent().clone(), + report.raw_json(), + report.transaction_id(), + report.retained_at(), + report.target_ordinals().to_vec(), + ) + .unwrap() + } + async fn restore(data: &mut DeliveryData, original: &ProtectedAttempt) { + sqlx::query("DELETE FROM chat_delivery_parts") + .execute(&mut data.database) + .await + .unwrap(); + sqlx::query("DELETE FROM chat_delivery_attempts") + .execute(&mut data.database) + .await + .unwrap(); + insert(&mut data.database, original).await.unwrap(); + } + async fn refused(data: &mut DeliveryData) { + let key = request_key(data.report.intent().request_id().as_str()); + let error = read(&mut data.database, data.report.work(), key, &data.crypto) + .await + .err() + .expect("malformed retained record must be refused"); + assert_eq!(error.code(), "recovery_required"); + } + async fn manifest(data: &mut DeliveryData, original: &ProtectedAttempt, value: &Value) { + let selected = scope( + data.report.work(), + request_key(data.report.intent().request_id().as_str()), + ); + let protected = data + .crypto + .seal_delivery( + selected, + DeliveryPayload::Attempt, + 0, + &serde_json::to_vec(value).unwrap(), + ) + .await + .unwrap(); + sqlx::query("UPDATE chat_delivery_attempts SET protected=?") + .bind(protected) + .execute(&mut data.database) + .await + .unwrap(); + assert!(!original.parts.is_empty()); + } + async fn frame(data: &mut DeliveryData, raw: &[u8]) { + let selected = scope( + data.report.work(), + request_key(data.report.intent().request_id().as_str()), + ); + let pieces = raw.len().div_ceil(PRIVATE_PAYLOAD_MAX_BYTES); + let value = json!({"version":1,"work":WorkRef::new(data.report.work()),"request":data.report.intent().request_id().as_str(),"event":data.report.intent().event_id(),"transaction":data.report.transaction_id(),"parts":pieces,"bytes":raw.len(),"digest":<[u8;32]>::from(Sha256::digest(raw))}); + let protected = data + .crypto + .seal_delivery( + selected, + DeliveryPayload::Attempt, + 0, + &serde_json::to_vec(&value).unwrap(), + ) + .await + .unwrap(); + sqlx::query("DELETE FROM chat_delivery_parts") + .execute(&mut data.database) + .await + .unwrap(); + for (index, piece) in raw.chunks(PRIVATE_PAYLOAD_MAX_BYTES).enumerate() { + let protected = data + .crypto + .seal_delivery(selected, DeliveryPayload::Attempt, (index + 1) as u8, piece) + .await + .unwrap(); + sqlx::query("INSERT INTO chat_delivery_parts(workspace,operation_id,recipient,request_key,part,protected) VALUES(?,?,?,?,?,?)").bind(selected.workspace.as_bytes().as_slice()).bind(selected.operation.as_bytes().as_slice()).bind(selected.recipient.as_bytes().as_slice()).bind(selected.request.as_slice()).bind(index as i64+1).bind(protected).execute(&mut data.database).await.unwrap(); + } + sqlx::query("UPDATE chat_delivery_attempts SET part_count=?,byte_count=?,protected=?") + .bind(pieces as i64) + .bind(raw.len() as i64) + .bind(protected) + .execute(&mut data.database) + .await + .unwrap(); + } + fn encoded(header: &Value, tail: &[u8]) -> Vec<u8> { + let header = serde_json::to_vec(header).unwrap(); + let mut result = (header.len() as u32).to_be_bytes().to_vec(); + result.extend_from_slice(&header); + result.extend_from_slice(tail); + result + } + + #[tokio::test] + async fn protected_delivery_headers_lengths_parts_and_fences_refuse_each_inconsistency() { + let mut data = delivery_data().await; + let key = request_key(data.report.intent().request_id().as_str()); + assert!( + read(&mut data.database, data.report.work(), key, &data.crypto) + .await + .unwrap() + .is_none() + ); + absent(&mut data.database, data.report.work(), key) + .await + .unwrap(); + let original = protect(copy(&data.report), &data.crypto).await.unwrap(); + assert!(original.parts.len() > 1); + insert(&mut data.database, &original).await.unwrap(); + assert!( + absent(&mut data.database, data.report.work(), key) + .await + .is_err() + ); + let fact = original.fact(); + fact.unchanged(&mut data.database).await.unwrap(); + let selected = scope(data.report.work(), key); + let decoded = data + .crypto + .open_delivery(selected, DeliveryPayload::Attempt, 0, &original.manifest) + .await + .unwrap(); + let base: Value = decoded.expose_secret(|raw| serde_json::from_slice(raw).unwrap()); + let mut rebound = base["work"].clone(); + rebound["transaction"] = json!("99".repeat(32)); + for (field, value) in [ + ("version", json!(2)), + ("work", rebound), + ("request", json!("02".repeat(32))), + ("event", json!("99".repeat(32))), + ("transaction", json!("99".repeat(32))), + ("parts", json!(original.parts.len() + 1)), + ("bytes", json!(original.bytes - 1)), + ("digest", json!(vec![0u8; 32])), + ] { + restore(&mut data, &original).await; + let mut changed = base.clone(); + changed[field] = value; + manifest(&mut data, &original, &changed).await; + refused(&mut data).await; + assert!(fact.unchanged(&mut data.database).await.is_err()); + } + let nested: Value = + serde_json::from_str(&format!("{}0{}", "[".repeat(17), "]".repeat(17))).unwrap(); + restore(&mut data, &original).await; + manifest(&mut data, &original, &nested).await; + refused(&mut data).await; + for statement in [ + "UPDATE chat_delivery_attempts SET event_id=zeroblob(31)", + "UPDATE chat_delivery_attempts SET transaction_id=zeroblob(31)", + "UPDATE chat_delivery_attempts SET event_id=zeroblob(32)", + "UPDATE chat_delivery_attempts SET transaction_id=zeroblob(32)", + "UPDATE chat_delivery_attempts SET part_count=0", + "UPDATE chat_delivery_attempts SET part_count=25", + "UPDATE chat_delivery_attempts SET byte_count=0", + "UPDATE chat_delivery_attempts SET byte_count=393217", + "UPDATE chat_delivery_attempts SET byte_count=1", + "UPDATE chat_delivery_attempts SET protected=zeroblob(262145)", + "DELETE FROM chat_delivery_parts WHERE part=1", + "UPDATE chat_delivery_parts SET part=0 WHERE part=1", + "UPDATE chat_delivery_parts SET protected=zeroblob(262145) WHERE part=1", + "UPDATE chat_delivery_parts SET protected=zeroblob(1) WHERE part=1", + "UPDATE chat_delivery_parts SET protected=zeroblob(0)", + ] { + restore(&mut data, &original).await; + sqlx::query(statement) + .execute(&mut data.database) + .await + .unwrap(); + refused(&mut data).await; + assert!( + fact.unchanged(&mut data.database).await.is_err(), + "{statement}" + ); + } + restore(&mut data, &original).await; + let part = original.parts.len() + 1; + sqlx::query("INSERT INTO chat_delivery_parts SELECT workspace,operation_id,recipient,request_key,?,protected FROM chat_delivery_parts WHERE part=1").bind(part as i64).execute(&mut data.database).await.unwrap(); + assert!(fact.unchanged(&mut data.database).await.is_err()); + refused(&mut data).await; + restore(&mut data, &original).await; + for increase in [false, true] { + let index = if increase { original.parts.len() } else { 1 }; + let decoded = data + .crypto + .open_delivery( + selected, + DeliveryPayload::Attempt, + index as u8, + &original.parts[index - 1], + ) + .await + .unwrap(); + let mut plain = decoded.expose_secret(<[u8]>::to_vec); + if increase { + plain.push(0); + } else { + plain.pop(); + } + let changed = data + .crypto + .seal_delivery(selected, DeliveryPayload::Attempt, index as u8, &plain) + .await + .unwrap(); + sqlx::query("UPDATE chat_delivery_parts SET protected=? WHERE part=?") + .bind(changed) + .bind(index as i64) + .execute(&mut data.database) + .await + .unwrap(); + refused(&mut data).await; + restore(&mut data, &original).await; + } + sqlx::query("DELETE FROM chat_delivery_parts") + .execute(&mut data.database) + .await + .unwrap(); + for index in 1..=24i64 { + sqlx::query("INSERT INTO chat_delivery_parts(workspace,operation_id,recipient,request_key,part,protected) VALUES(?,?,?,?,?,zeroblob(262144))").bind(selected.workspace.as_bytes().as_slice()).bind(selected.operation.as_bytes().as_slice()).bind(selected.recipient.as_bytes().as_slice()).bind(selected.request.as_slice()).bind(index).execute(&mut data.database).await.unwrap(); + } + sqlx::query("UPDATE chat_delivery_attempts SET part_count=24,byte_count=393216") + .execute(&mut data.database) + .await + .unwrap(); + refused(&mut data).await; + restore(&mut data, &original).await; + let retained = read(&mut data.database, data.report.work(), key, &data.crypto) + .await + .unwrap() + .unwrap(); + assert_eq!(retained.report.raw_json(), data.report.raw_json()); + fact.unchanged(&mut data.database).await.unwrap(); + } + + #[tokio::test] + async fn authenticated_delivery_frames_reject_rebound_fields_and_invalid_nested_encodings() { + let mut data = delivery_data().await; + let original = protect(copy(&data.report), &data.crypto).await.unwrap(); + insert(&mut data.database, &original).await.unwrap(); + let base = serde_json::to_value(Header::new(&data.report)).unwrap(); + let mut rebound = base["work"].clone(); + rebound["transaction"] = json!("99".repeat(32)); + let header = Header::new(&data.report); + assert!( + header + .into_attempt(data.report.work(), data.report.raw_json()) + .is_ok() + ); + let foreign = radroots_transport::Target::nostr_relay("wss://foreign.example/").unwrap(); + for (field, value) in [ + ("version", json!(2)), + ("work", rebound), + ("selection", json!([])), + ("selection", json!([foreign.fingerprint().as_str()])), + ( + "selection", + json!([ + data.report.intent().selection()[0].as_str(), + data.report.intent().selection()[0].as_str() + ]), + ), + ("intent", json!(vec![0u8; 32])), + ("ordinals", json!([0])), + ("unknown", json!(true)), + ] { + let mut changed = base.clone(); + changed[field] = value; + let raw = encoded(&changed, data.report.raw_json().as_bytes()); + frame(&mut data, &raw).await; + refused(&mut data).await; + } + let nested: Value = + serde_json::from_str(&format!("{}0{}", "[".repeat(17), "]".repeat(17))).unwrap(); + for raw in [ + vec![0], + u32::MAX.to_be_bytes().to_vec(), + encoded(&nested, b"tail"), + encoded(&base, &[0xff]), + ] { + frame(&mut data, &raw).await; + refused(&mut data).await; + } + for field in ["request", "transaction"] { + let mut changed = base.clone(); + if field == "request" { + let next = DeliveryIntent::new( + &"02".repeat(32), + data.report.intent().event_id(), + data.report.intent().targets(), + data.report.intent().policy(), + data.report.intent().deadline_unix_ms(), + ) + .unwrap(); + changed["request"] = json!(next.request_id().as_str()); + changed["intent"] = json!(next.digest()); + } else { + changed["transaction"] = json!("99".repeat(32)); + } + let raw = encoded(&changed, data.report.raw_json().as_bytes()); + frame(&mut data, &raw).await; + refused(&mut data).await; + } + let original_event = nostr::Event::from_json(data.report.raw_json()).unwrap(); + let mut scalar = [0; 32]; + scalar[31] = 3; + let keys = nostr::Keys::new(nostr::SecretKey::from_slice(&scalar).unwrap()); + let other = nostr::UnsignedEvent { + id: None, + pubkey: keys.public_key(), + created_at: original_event.created_at, + kind: original_event.kind, + tags: original_event.tags.clone(), + content: format!("{} ", original_event.content), + } + .sign_with_keys(&keys) + .unwrap(); + let intent = DeliveryIntent::new( + data.report.intent().request_id().as_str(), + EnvelopeEventId::from_bytes(*other.id.as_bytes()), + data.report.intent().targets(), + data.report.intent().policy(), + data.report.intent().deadline_unix_ms(), + ) + .unwrap(); + let mut changed = base.clone(); + changed["event"] = json!(intent.event_id()); + changed["intent"] = json!(intent.digest()); + frame(&mut data, &encoded(&changed, other.as_json().as_bytes())).await; + refused(&mut data).await; + restore(&mut data, &original).await; + assert_eq!( + read( + &mut data.database, + data.report.work(), + request_key(data.report.intent().request_id().as_str()), + &data.crypto + ) + .await + .unwrap() + .unwrap() + .report + .raw_json(), + data.report.raw_json() + ); + } +} diff --git a/crates/storage_sqlite/src/workspace/delivery/graph.rs b/crates/storage_sqlite/src/workspace/delivery/graph.rs @@ -0,0 +1,271 @@ +use super::super::{ + AttachmentInput, crypto::WorkspaceCrypto, installation::Records, snapshot::RetainedGraph, +}; +use super::{ + MAX_ATTEMPTS, + codec::{self, Fact}, +}; +use radroots_domain::{OperationId, PublicKey}; +use radroots_event::{ + SignedEvent, + wire::{EventWireLimits, Nip01EventWire}, +}; +use radroots_event_codec::verify::{self, Nip01SignatureVerifier, RawEvent}; +use radroots_storage::command::{ + BoundSigningPlan, DeliveryAttempt, DeliveryIntent, OperationError, +}; +use radroots_transport::target::EndpointUri; +use sqlx::{Row, SqliteConnection}; +use std::collections::{BTreeMap, BTreeSet}; + +#[cfg(test)] +#[path = "graph_tests.rs"] +mod tests; + +pub(in crate::workspace) struct Graph { + pub attempts: Vec<DeliveryAttempt>, + facts: Vec<Fact>, + parts: usize, + counters: BTreeMap<(PublicKey, String), BTreeSet<u8>>, + routes: BTreeMap<PublicKey, BTreeSet<String>>, +} +fn bad<E>(_: E) -> OperationError { + OperationError::RecoveryRequired +} +fn keys(row: &sqlx::sqlite::SqliteRow, index: usize) -> Result<[u8; 32], OperationError> { + row.try_get::<Option<Vec<u8>>, _>(index) + .map_err(bad)? + .ok_or(OperationError::RecoveryRequired)? + .try_into() + .map_err(bad) +} + +fn allowed_routes( + bound: &BoundSigningPlan, + recipient: PublicKey, +) -> Result<BTreeMap<PublicKey, BTreeSet<String>>, OperationError> { + let mut result = BTreeMap::new(); + for route in bound + .plan() + .routes() + .iter() + .filter(|route| route.participant() == recipient) + { + let limits = EventWireLimits { + max_raw_json_bytes: 65536, + max_tag_element_bytes: 32768, + max_total_tag_bytes: 65536, + max_extra_fields: 0, + max_total_extra_json_bytes: 0, + ..Default::default() + }; + let wire = Nip01EventWire::parse_json_exact_with_limits(route.raw_json(), limits) + .map_err(|_| OperationError::InvalidEvidence)?; + if wire.kind != 10050 + || wire.pubkey != route.participant().to_hex() + || wire.id != route.source_id().to_string() + || wire.created_at == 0 + || wire.created_at > 253_402_300_799 + { + return Err(OperationError::InvalidEvidence); + } + for tag in &wire.tags { + if tag.first().is_some_and(|v| v == "relay") && (tag.len() != 2 || tag[1].len() > 2048) + { + return Err(OperationError::InvalidEvidence); + } + } + let mut selected = BTreeSet::new(); + for target in route.selected_relays() { + if !wire + .tags + .iter() + .any(|tag| tag.len() == 2 && tag[0] == "relay" && tag[1] == target.as_ref()) + { + return Err(OperationError::UnverifiedApproval); + } + let endpoint = + EndpointUri::parse(target.as_ref()).map_err(|_| OperationError::InvalidCommand)?; + if endpoint.as_str().len() > 2048 { + return Err(OperationError::InvalidCommand); + } + selected.insert(target.as_ref().to_owned()); + } + let event = SignedEvent::from_wire_verified_id(wire, route.raw_json()) + .map_err(|_| OperationError::InvalidEvidence)?; + let checked = verify::id(RawEvent::new(event.envelope().clone())) + .map_err(|_| OperationError::InvalidEvidence)?; + verify::signature(checked, &Nip01SignatureVerifier) + .map_err(|_| OperationError::InvalidEvidence)?; + if selected.is_empty() || result.insert(route.participant(), selected).is_some() { + return Err(OperationError::InvalidCommand); + } + } + Ok(result) +} +impl Graph { + pub(in crate::workspace) async fn read( + connection: &mut SqliteConnection, + selected: &AttachmentInput, + operation: OperationId, + original: &RetainedGraph, + installed: &Records, + crypto: &WorkspaceCrypto, + ) -> Result<Self, OperationError> { + let rows=sqlx::query("SELECT CASE WHEN length(recipient)=32 THEN recipient END,CASE WHEN length(request_key)=32 THEN request_key END FROM chat_delivery_attempts WHERE workspace=? AND operation_id=? ORDER BY recipient,request_key LIMIT 385") + .bind(selected.workspace_id().as_bytes().as_slice()).bind(operation.as_bytes().as_slice()).fetch_all(&mut *connection).await.map_err(bad)?; + if rows.len() > MAX_ATTEMPTS { + return Err(OperationError::RecoveryRequired); + } + let routes = BTreeMap::new(); + let mut result = Self { + attempts: Vec::with_capacity(rows.len()), + facts: Vec::with_capacity(rows.len()), + parts: 0, + counters: BTreeMap::new(), + routes, + }; + let mut target_count = 0usize; + let mut representations = BTreeMap::new(); + let mut policies = BTreeMap::new(); + for row in rows { + let recipient = PublicKey::from_bytes(keys(&row, 0)?).map_err(bad)?; + let key = keys(&row, 1)?; + if !result.routes.contains_key(&recipient) { + result + .authorize_routes( + original + .bound_plan() + .ok_or(OperationError::RecoveryRequired)?, + recipient, + ) + .map_err(|_| OperationError::RecoveryRequired)?; + } + let work = installed + .work + .iter() + .find(|work| work.report.evidence().recipient() == recipient) + .ok_or(OperationError::RecoveryRequired)? + .report; + let value = codec::read(connection, work, key, crypto) + .await? + .ok_or(OperationError::RecoveryRequired)?; + if policies + .insert( + recipient, + *value.report.intent().publication_policy_digest(), + ) + .is_some_and(|original| { + original != *value.report.intent().publication_policy_digest() + }) + { + return Err(OperationError::RecoveryRequired); + } + if representations + .insert( + value.report.intent().event_id(), + *value.report.envelope_digest(), + ) + .is_some_and(|digest| digest != *value.report.envelope_digest()) + { + return Err(OperationError::RecoveryRequired); + } + target_count = target_count + .checked_add(value.report.intent().selection().len()) + .filter(|n| *n <= MAX_ATTEMPTS) + .ok_or(OperationError::RecoveryRequired)?; + result + .validate_routes(recipient, value.report.intent()) + .map_err(|_| OperationError::RecoveryRequired)?; + for (target, ordinal) in value + .report + .intent() + .selected_targets() + .zip(value.report.target_ordinals()) + { + let slots = result + .counters + .entry((recipient, target.uri().as_str().to_owned())) + .or_default(); + if !slots.insert(*ordinal) { + return Err(OperationError::RecoveryRequired); + } + } + result.parts += value.parts.len(); + result.facts.push(value.fact()); + result.attempts.push(value.report); + } + for slots in result.counters.values() { + if slots.iter().copied().ne(1..=slots.len() as u8) { + return Err(OperationError::RecoveryRequired); + } + } + Ok(result) + } + pub(super) fn validate_routes( + &self, + recipient: PublicKey, + intent: &DeliveryIntent, + ) -> Result<(), OperationError> { + let routes = self + .routes + .get(&recipient) + .ok_or(OperationError::UnverifiedApproval)?; + if intent.targets().targets().iter().any(|target| { + !routes + .iter() + .any(|raw| matches!(target.matches_nostr_relay(raw), Ok(true))) + }) { + return Err(OperationError::UnverifiedApproval); + } + Ok(()) + } + pub(super) fn authorize_routes( + &mut self, + bound: &BoundSigningPlan, + recipient: PublicKey, + ) -> Result<(), OperationError> { + let routes = allowed_routes(bound, recipient)?; + if !routes.contains_key(&recipient) { + return Err(OperationError::UnverifiedApproval); + } + self.routes.extend(routes); + Ok(()) + } + pub(super) fn ordinals( + &self, + recipient: PublicKey, + intent: &DeliveryIntent, + ) -> Result<Vec<u8>, OperationError> { + self.validate_routes(recipient, intent)?; + intent + .selected_targets() + .map(|target| { + let count = self + .counters + .get(&(recipient, target.uri().as_str().to_owned())) + .map_or(0, BTreeSet::len); + if count >= 12 { + return Err(OperationError::DeliveryAttemptLimit); + } + Ok(count as u8 + 1) + }) + .collect() + } + pub(in crate::workspace) 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_delivery_attempts WHERE workspace=?1 AND operation_id=?2 LIMIT 385)),(SELECT count(*) FROM (SELECT 1 FROM chat_delivery_parts WHERE workspace=?1 AND operation_id=?2 LIMIT 9217))") + .bind(selected.workspace_id().as_bytes().as_slice()).bind(operation.as_bytes().as_slice()).fetch_one(&mut *connection).await.map_err(bad)?; + if counts != (self.facts.len() as i64, self.parts as i64) { + return Err(OperationError::RecoveryRequired); + } + for fact in &self.facts { + fact.unchanged(connection).await?; + } + Ok(()) + } +} diff --git a/crates/storage_sqlite/src/workspace/delivery/graph_tests.rs b/crates/storage_sqlite/src/workspace/delivery/graph_tests.rs @@ -0,0 +1,372 @@ +#[cfg(test)] +mod cases { + use super::super::*; + use crate::workspace::tests::issuance_records::delivery_data; + use nostr::JsonUtil; + use radroots_domain::EnvelopeEventId; + use radroots_protocol::chat::{CapturedDmRoute, ChatRumor, ChatSeal, ChatSigningPlan}; + use radroots_transport::{Target, TargetSet}; + + fn route_plan( + original: BoundSigningPlan, + raw: &str, + source: EnvelopeEventId, + selected: &[String], + ) -> BoundSigningPlan { + let transaction = original.transaction_id(); + let time = original.bound_at(); + let (prepared, plan) = original.into_parts(); + let command = prepared.request().command(); + let rumor = ChatRumor::from_command(command, plan.created_at()).unwrap(); + let seals = plan + .seals() + .iter() + .map(|seal| { + ChatSeal::new( + &rumor, + seal.recipient(), + radroots_domain::UnixSeconds::new(seal.fields().created_at).unwrap(), + seal.fields().content, + ) + .unwrap() + }) + .collect(); + let route = CapturedDmRoute::from_observed_descriptor( + plan.routes()[0].participant(), + source, + raw, + selected, + ) + .unwrap(); + let replacement = ChatSigningPlan::new( + command, + plan.created_at(), + seals, + vec![route], + plan.replay(), + ) + .unwrap(); + BoundSigningPlan::from_committed_store(prepared, replacement, transaction, time).unwrap() + } + fn signed(kind: u16, time: u64, scalar: u8, tags: Vec<nostr::Tag>) -> nostr::Event { + let mut bytes = [0; 32]; + bytes[31] = scalar; + let keys = nostr::Keys::new(nostr::SecretKey::from_slice(&bytes).unwrap()); + nostr::UnsignedEvent { + id: None, + pubkey: keys.public_key(), + created_at: nostr::Timestamp::from_secs(time), + kind: nostr::Kind::Custom(kind), + tags: nostr::Tags::from_list(tags), + content: String::new(), + } + .sign_with_keys(&keys) + .unwrap() + } + fn relay() -> nostr::Tag { + nostr::Tag::parse(["relay", "wss://route.example/"]).unwrap() + } + + #[tokio::test] + async fn original_route_authentication_refuses_invalid_sources_and_unadvertised_selection() { + let mut data = delivery_data().await; + let recipient = data.report.work().evidence().recipient(); + let selected = [String::from("wss://route.example/")]; + assert!( + allowed_routes(&data.bound().await, recipient) + .unwrap() + .contains_key(&recipient) + ); + for event in [ + signed(14, 99, 1, vec![relay()]), + signed(10050, 99, 2, vec![relay()]), + signed(10050, 0, 1, vec![relay()]), + signed(10050, 253_402_300_800, 1, vec![relay()]), + signed(10050, 99, 1, vec![nostr::Tag::parse(["relay"]).unwrap()]), + signed( + 10050, + 99, + 1, + vec![nostr::Tag::parse(["relay", "wss://route.example/", "extra"]).unwrap()], + ), + signed( + 10050, + 99, + 1, + vec![ + nostr::Tag::parse([ + "relay", + &format!("wss://route.example/{}", "x".repeat(2048)), + ]) + .unwrap(), + ], + ), + ] { + let source = EnvelopeEventId::from_bytes(*event.id.as_bytes()); + let bound = route_plan(data.bound().await, &event.as_json(), source, &selected); + assert!(allowed_routes(&bound, recipient).is_err()); + } + let event = signed(10050, 99, 1, vec![relay()]); + let source = EnvelopeEventId::from_bytes(*event.id.as_bytes()); + let bound = route_plan( + data.bound().await, + &event.as_json(), + EnvelopeEventId::from_bytes([0; 32]), + &selected, + ); + assert!(allowed_routes(&bound, recipient).is_err()); + let missing = [String::from("wss://unadvertised.example/")]; + let bound = route_plan(data.bound().await, &event.as_json(), source, &missing); + assert!(matches!( + allowed_routes(&bound, recipient), + Err(OperationError::UnverifiedApproval) + )); + let mut raw: serde_json::Value = serde_json::from_str(&event.as_json()).unwrap(); + raw["sig"] = serde_json::Value::String("0".repeat(128)); + let bound = route_plan(data.bound().await, &raw.to_string(), source, &selected); + assert!(allowed_routes(&bound, recipient).is_err()); + let metadata = signed( + 10050, + 99, + 1, + vec![ + nostr::Tag::parse(["client", "wss://unadvertised.example/"]).unwrap(), + nostr::Tag::parse(["metadata"]).unwrap(), + relay(), + ], + ); + let source = EnvelopeEventId::from_bytes(*metadata.id.as_bytes()); + let bound = route_plan(data.bound().await, &metadata.as_json(), source, &selected); + assert!( + allowed_routes(&bound, recipient) + .unwrap() + .contains_key(&recipient) + ); + let bound = route_plan(data.bound().await, &metadata.as_json(), source, &missing); + assert!(matches!( + allowed_routes(&bound, recipient), + Err(OperationError::UnverifiedApproval) + )); + let unrelated = + PublicKey::from_bytes(signed(10050, 99, 2, vec![relay()]).pubkey.to_bytes()).unwrap(); + assert!( + allowed_routes(&data.bound().await, unrelated) + .unwrap() + .is_empty() + ); + } + + #[tokio::test] + async fn route_scope_budget_and_complete_fact_count_fences_are_independent() { + let mut data = delivery_data().await; + let recipient = data.report.work().evidence().recipient(); + let mut graph = Graph { + attempts: vec![], + facts: vec![], + parts: 0, + counters: BTreeMap::new(), + routes: BTreeMap::new(), + }; + assert!( + graph + .validate_routes(recipient, data.report.intent()) + .is_err() + ); + graph + .authorize_routes(&data.bound().await, recipient) + .unwrap(); + let unrelated = + PublicKey::from_bytes(signed(10050, 99, 2, vec![relay()]).pubkey.to_bytes()).unwrap(); + assert!(matches!( + graph.authorize_routes(&data.bound().await, unrelated), + Err(OperationError::UnverifiedApproval) + )); + assert_eq!( + graph.ordinals(recipient, data.report.intent()).unwrap(), + [1] + ); + let set = TargetSet::new(vec![ + Target::nostr_relay("wss://unadvertised.example/").unwrap(), + ]) + .unwrap(); + let foreign = DeliveryIntent::new( + data.report.intent().request_id().as_str(), + data.report.intent().event_id(), + &set, + data.report.intent().policy(), + data.report.intent().deadline_unix_ms(), + ) + .unwrap(); + assert!(graph.validate_routes(recipient, &foreign).is_err()); + graph.counters.insert( + ( + recipient, + data.report.intent().targets().targets()[0] + .uri() + .as_str() + .to_owned(), + ), + (1..=12).collect(), + ); + assert!(matches!( + graph.ordinals(recipient, data.report.intent()), + Err(OperationError::DeliveryAttemptLimit) + )); + let selected = data.selection(); + graph + .unchanged( + &mut data.database, + &selected, + data.report.work().evidence().operation_id(), + ) + .await + .unwrap(); + graph.parts = 1; + assert!( + graph + .unchanged( + &mut data.database, + &selected, + data.report.work().evidence().operation_id() + ) + .await + .is_err() + ); + } + + #[tokio::test] + async fn observation_keys_ciphertexts_and_excess_counts_cannot_escape_the_actual_graph_fence() { + use crate::workspace::delivery::{codec, observation}; + use radroots_storage::command::DeliveryObservation; + use radroots_transport::{outcome::DeliveryOutcome, sink::DeliveryTargetReceipt}; + let mut data = delivery_data().await; + let selected = data.selection(); + let e = data.report.work().evidence(); + let request = codec::request_key(data.report.intent().request_id().as_str()); + let report = DeliveryAttempt::from_store_report( + data.report.work(), + data.report.intent().clone(), + data.report.raw_json(), + data.report.transaction_id(), + data.report.retained_at(), + data.report.target_ordinals().to_vec(), + ) + .unwrap(); + let graph = Graph { + attempts: vec![report], + facts: vec![], + parts: 0, + counters: BTreeMap::new(), + routes: BTreeMap::new(), + }; + let target = graph.attempts[0].intent().targets().targets()[0].clone(); + let original = DeliveryObservation::from_store_report( + &graph.attempts[0], + &DeliveryTargetReceipt::attempted(target, DeliveryOutcome::accepted()), + None, + ) + .unwrap(); + let protected = observation::protect(original.clone(), &data.crypto) + .await + .unwrap(); + protected.insert(&mut data.database).await.unwrap(); + let observed = observation::Observations::read( + &mut data.database, + &graph, + &selected, + e.operation_id(), + &data.crypto, + ) + .await + .unwrap(); + observed + .unchanged(&mut data.database, &selected, e.operation_id()) + .await + .unwrap(); + for statement in [ + "UPDATE chat_delivery_observations SET recipient=zeroblob(31)", + "UPDATE chat_delivery_observations SET recipient=zeroblob(32)", + "UPDATE chat_delivery_observations SET request_key=zeroblob(32)", + "UPDATE chat_delivery_observations SET target_key=zeroblob(31)", + "UPDATE chat_delivery_observations SET kind=7", + "UPDATE chat_delivery_observations SET protected=zeroblob(262145)", + "UPDATE chat_delivery_observations SET protected=zeroblob(1)", + ] { + sqlx::query(statement) + .execute(&mut data.database) + .await + .unwrap(); + let error = observation::Observations::read( + &mut data.database, + &graph, + &selected, + e.operation_id(), + &data.crypto, + ) + .await + .err() + .expect("malformed observation must fail"); + assert_eq!(error.code(), "recovery_required", "{statement}"); + assert!( + observed + .unchanged(&mut data.database, &selected, e.operation_id()) + .await + .is_err() + ); + sqlx::query("DELETE FROM chat_delivery_observations") + .execute(&mut data.database) + .await + .unwrap(); + protected.insert(&mut data.database).await.unwrap(); + } + sqlx::query("DELETE FROM chat_delivery_observations") + .execute(&mut data.database) + .await + .unwrap(); + assert!( + observed + .unchanged(&mut data.database, &selected, e.operation_id()) + .await + .is_err() + ); + protected.insert(&mut data.database).await.unwrap(); + sqlx::query("BEGIN") + .execute(&mut data.database) + .await + .unwrap(); + sqlx::query("DELETE FROM chat_delivery_observations") + .execute(&mut data.database) + .await + .unwrap(); + for index in 0..2305u32 { + let mut target = [0; 32]; + target[..4].copy_from_slice(&index.to_be_bytes()); + sqlx::query("INSERT INTO chat_delivery_observations(workspace,operation_id,recipient,request_key,target_key,kind,protected) VALUES(?,?,?,?,?,1,x'01')").bind(e.workspace_id().as_bytes().as_slice()).bind(e.operation_id().as_bytes().as_slice()).bind(e.recipient().as_bytes().as_slice()).bind(request.as_slice()).bind(target.as_slice()).execute(&mut data.database).await.unwrap(); + } + let error = observation::Observations::read( + &mut data.database, + &graph, + &selected, + e.operation_id(), + &data.crypto, + ) + .await + .err() + .expect("excess observation keys must fail"); + assert_eq!(error.code(), "recovery_required"); + sqlx::query("ROLLBACK") + .execute(&mut data.database) + .await + .unwrap(); + let restored = observation::Observations::read( + &mut data.database, + &graph, + &selected, + e.operation_id(), + &data.crypto, + ) + .await + .unwrap(); + assert_eq!(restored.reports, [original]); + } +} diff --git a/crates/storage_sqlite/src/workspace/delivery/json.rs b/crates/storage_sqlite/src/workspace/delivery/json.rs @@ -0,0 +1,172 @@ +use core::{fmt, marker::PhantomData}; +use serde::{ + Deserialize, Deserializer, + de::{self, SeqAccess, Visitor}, +}; +use serde_json::value::RawValue; + +// Allocation-free structural preflight, followed by the actual closed serde +// codec. Fixed identifier arrays contain 32 bytes; descriptor lists use the +// stricter sixteen-element visitor below. +pub(super) fn bounded(raw: &[u8], array_limit: usize) -> bool { + let mut stack = [(0u8, false, 0usize); 16]; + let mut depth = 0usize; + let mut quoted = false; + let mut escaped = false; + for byte in raw.iter().copied() { + if quoted { + if escaped { + escaped = false; + } else if byte == b'\\' { + escaped = true; + } else if byte == b'"' { + quoted = false; + } + continue; + } + if byte.is_ascii_whitespace() || byte == b':' { + continue; + } + if byte == b',' { + if depth > 0 && stack[depth - 1].0 == b'[' { + stack[depth - 1].1 = true; + } + continue; + } + if matches!(byte, b'}' | b']') { + if depth == 0 { + return false; + } + depth -= 1; + if stack[depth].0 != if byte == b'}' { b'{' } else { b'[' } { + return false; + } + continue; + } + if depth > 0 && stack[depth - 1].0 == b'[' && stack[depth - 1].1 { + stack[depth - 1].2 += 1; + stack[depth - 1].1 = false; + if stack[depth - 1].2 > array_limit { + return false; + } + } + if matches!(byte, b'{' | b'[') { + if depth == stack.len() { + return false; + } + stack[depth] = (byte, byte == b'[', 0); + depth += 1; + } else if byte == b'"' { + quoted = true; + } + } + !quoted && !escaped && depth == 0 +} + +pub(super) fn small_vec<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error> +where + D: Deserializer<'de>, + T: Deserialize<'de>, +{ + struct Items<T>(PhantomData<T>); + impl<'de, T: Deserialize<'de>> Visitor<'de> for Items<T> { + type Value = Vec<T>; + fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("at most sixteen bounded entries") + } + fn visit_seq<A: SeqAccess<'de>>(self, mut sequence: A) -> Result<Self::Value, A::Error> { + let mut result = Vec::with_capacity(sequence.size_hint().unwrap_or(0).min(16)); + while let Some(raw) = sequence.next_element::<&'de RawValue>()? { + if result.len() == 16 || raw.get().len() > 16384 { + return Err(de::Error::custom("private delivery entry limit")); + } + result.push(serde_json::from_str(raw.get()).map_err(de::Error::custom)?); + } + Ok(result) + } + } + deserializer.deserialize_seq(Items(PhantomData)) +} + +pub(super) fn targets<'de, D: Deserializer<'de>>( + deserializer: D, +) -> Result<radroots_transport::TargetSet, D::Error> { + #[derive(Deserialize)] + #[serde(deny_unknown_fields)] + struct Wire { + #[serde(deserialize_with = "small_vec")] + targets: Vec<radroots_transport::Target>, + } + let wire = Wire::deserialize(deserializer)?; + radroots_transport::TargetSet::new(wire.targets).map_err(de::Error::custom) +} + +pub(super) fn policy<'de, D: Deserializer<'de>>( + deserializer: D, +) -> Result<radroots_transport::policy::SatisfactionPolicy, D::Error> { + let raw = <&RawValue>::deserialize(deserializer)?; + if raw.get().len() > 16384 || !bounded(raw.get().as_bytes(), 16) { + return Err(de::Error::custom("private delivery policy limit")); + } + serde_json::from_str(raw.get()).map_err(de::Error::custom) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn structural_preflight_and_typed_lists_bound_untrusted_private_records() { + for raw in [ + br#"{"a":["escaped\\\"[]",null,{}]}"#.as_slice(), + b"[]", + b"{}", + br#"{"a":1,"b":2}"#, + b"[ [1], [2] ]", + ] { + assert!(bounded(raw, 3)); + } + for raw in [ + b"]".as_slice(), + b"[}", + b"\"unfinished", + b"\"escape\\", + b"[1,2,3]", + b"[[1]", + ] { + assert!(!bounded(raw, 2)); + } + assert!(!bounded( + format!("{}0{}", "[".repeat(17), "]".repeat(17)).as_bytes(), + 32 + )); + #[derive(Deserialize)] + struct List { + #[serde(deserialize_with = "small_vec")] + items: Vec<String>, + } + let accepted: List = serde_json::from_str(r#"{"items":["one","two"]}"#).unwrap(); + assert_eq!(accepted.items, ["one", "two"]); + for value in [ + serde_json::json!({"items":vec!["item";17]}), + serde_json::json!({"items":["x".repeat(16385)]}), + serde_json::json!({"items":false}), + ] { + assert!(serde_json::from_str::<List>(&value.to_string()).is_err()); + } + #[derive(Deserialize)] + struct Policy { + #[serde(deserialize_with = "policy")] + value: radroots_transport::policy::SatisfactionPolicy, + } + let raw = serde_json::json!({"value":{"class":"accepted","targets":"all"}}).to_string(); + let decoded = serde_json::from_str::<Policy>(&raw).unwrap(); + assert!(decoded.value.targets().is_all()); + for raw in [ + format!("{{\"value\":\"{}\"}}", "x".repeat(16385)), + format!("{{\"value\":[{}]}}", vec!["0"; 17].join(",")), + String::from("{\"value\":false}"), + ] { + assert!(serde_json::from_str::<Policy>(&raw).is_err()); + } + } +} diff --git a/crates/storage_sqlite/src/workspace/delivery/mod.rs b/crates/storage_sqlite/src/workspace/delivery/mod.rs @@ -0,0 +1,17 @@ +//! Append-only protected transport attempts on the existing workspace owner. + +mod codec; +pub(super) mod graph; +mod json; +pub(super) mod observation; +mod observation_operations; +mod operations; + +pub(super) const TABLE: &str = "CREATE TABLE chat_delivery_attempts (workspace BLOB NOT NULL, operation_id BLOB NOT NULL, recipient BLOB NOT NULL CHECK(length(recipient)=32), request_key BLOB NOT NULL CHECK(length(request_key)=32), event_id BLOB NOT NULL CHECK(length(event_id)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), part_count INTEGER NOT NULL CHECK(part_count BETWEEN 1 AND 24), byte_count INTEGER NOT NULL CHECK(byte_count BETWEEN 1 AND 393216), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), PRIMARY KEY(workspace,operation_id,recipient,request_key), FOREIGN KEY(workspace,operation_id,recipient) REFERENCES chat_publication_work(workspace,operation_id,recipient)) STRICT, WITHOUT ROWID"; +pub(super) const PARTS: &str = "CREATE TABLE chat_delivery_parts (workspace BLOB NOT NULL, operation_id BLOB NOT NULL, recipient BLOB NOT NULL, request_key BLOB NOT NULL, part INTEGER NOT NULL CHECK(part BETWEEN 1 AND 24), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), PRIMARY KEY(workspace,operation_id,recipient,request_key,part), FOREIGN KEY(workspace,operation_id,recipient,request_key) REFERENCES chat_delivery_attempts(workspace,operation_id,recipient,request_key)) STRICT, WITHOUT ROWID"; +pub(super) const TX_INDEX: &str = "CREATE UNIQUE INDEX chat_delivery_transaction_identity ON chat_delivery_attempts(workspace,transaction_id)"; + +const MAX_BYTES: usize = 393216; +const MAX_PROTECTED_BYTES: usize = 2 * 1024 * 1024; +const MAX_ATTEMPTS: usize = 2 * 16 * 12; +pub(super) const OBSERVATIONS: &str = observation::TABLE; diff --git a/crates/storage_sqlite/src/workspace/delivery/observation.rs b/crates/storage_sqlite/src/workspace/delivery/observation.rs @@ -0,0 +1,241 @@ +use super::super::crypto::{DeliveryPayload, DeliveryScope, WorkspaceCrypto}; +use super::{ + codec::{self, WorkRef}, + graph::Graph, +}; +use radroots_domain::EnvelopeEventId; +use radroots_storage::command::{DeliveryAttempt, DeliveryObservation, OperationError}; +use radroots_transport::sink::DeliveryTargetReceipt; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use sqlx::{Row, SqliteConnection}; +use zeroize::Zeroizing; + +#[cfg(test)] +#[path = "observation_tests.rs"] +mod tests; + +pub(super) const TABLE: &str = "CREATE TABLE chat_delivery_observations (workspace BLOB NOT NULL, operation_id BLOB NOT NULL, recipient BLOB NOT NULL, request_key BLOB NOT NULL, target_key BLOB NOT NULL CHECK(length(target_key)=32), kind INTEGER NOT NULL CHECK(kind BETWEEN 1 AND 6), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), PRIMARY KEY(workspace,operation_id,recipient,request_key,target_key,kind), FOREIGN KEY(workspace,operation_id,recipient,request_key) REFERENCES chat_delivery_attempts(workspace,operation_id,recipient,request_key)) STRICT, WITHOUT ROWID"; +const MAX_OBSERVATIONS: usize = 2 * 16 * 12 * 6; + +#[derive(Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +struct Wire { + version: u8, + work: WorkRef, + request: String, + event: EnvelopeEventId, + envelope_digest: [u8; 32], + target: DeliveryTargetReceipt, + kind: u8, + observed_at: Option<core::num::NonZeroU64>, +} +fn bad<E>(_: E) -> OperationError { + OperationError::RecoveryRequired +} +fn target_key(value: &DeliveryObservation) -> [u8; 32] { + Sha256::digest( + value + .target_receipt() + .target() + .fingerprint() + .as_str() + .as_bytes(), + ) + .into() +} +fn context(scope: DeliveryScope, target: [u8; 32], kind: u8) -> DeliveryScope { + let mut hash = Sha256::new(); + hash.update(b"radroots.delivery.observation.key.v1\0"); + hash.update(scope.request); + hash.update(target); + hash.update([kind]); + DeliveryScope { + request: hash.finalize().into(), + ..scope + } +} +pub(super) struct Fact { + scope: DeliveryScope, + target: [u8; 32], + kind: u8, + digest: [u8; 32], +} +pub(in crate::workspace) struct Observations { + pub reports: Vec<DeliveryObservation>, + facts: Vec<Fact>, +} +pub(super) struct Protected { + pub report: DeliveryObservation, + scope: DeliveryScope, + target: [u8; 32], + protected: Vec<u8>, +} +pub(super) async fn protect( + report: DeliveryObservation, + crypto: &WorkspaceCrypto, +) -> Result<Protected, OperationError> { + let wire = Wire { + version: 1, + work: WorkRef::new(report.work()), + request: report.request_id().as_str().into(), + event: report.event_id(), + envelope_digest: *report.envelope_digest(), + target: report.target_receipt().clone(), + kind: report.kind().code(), + observed_at: report.observed_at_unix_ms(), + }; + let plain = Zeroizing::new(serde_json::to_vec(&wire).map_err(bad)?); + if plain.len() > 16 * 1024 { + return Err(OperationError::InvalidEvidence); + } + let scope = codec::scope( + report.work(), + codec::request_key(report.request_id().as_str()), + ); + let target = target_key(&report); + let protected = crypto + .seal_delivery( + context(scope, target, report.kind().code()), + DeliveryPayload::Observation, + 0, + &plain, + ) + .await?; + Ok(Protected { + report, + scope, + target, + protected, + }) +} +impl Protected { + pub(super) async fn insert( + &self, + connection: &mut SqliteConnection, + ) -> Result<(), OperationError> { + let s = self.scope; + sqlx::query("INSERT INTO chat_delivery_observations(workspace,operation_id,recipient,request_key,target_key,kind,protected) VALUES(?,?,?,?,?,?,?)") + .bind(s.workspace.as_bytes().as_slice()).bind(s.operation.as_bytes().as_slice()).bind(s.recipient.as_bytes().as_slice()).bind(s.request.as_slice()).bind(self.target.as_slice()).bind(self.report.kind().code() as i64).bind(&self.protected).execute(connection).await.map_err(|_|OperationError::StorageFailure)?; + Ok(()) + } +} +impl Observations { + pub(in crate::workspace) async fn read( + connection: &mut SqliteConnection, + graph: &Graph, + selected: &super::super::AttachmentInput, + operation: radroots_domain::OperationId, + crypto: &WorkspaceCrypto, + ) -> Result<Self, OperationError> { + // Bound keys before fetching or opening any protected observation body. + let rows=sqlx::query("SELECT CASE WHEN length(recipient)=32 THEN recipient END,CASE WHEN length(request_key)=32 THEN request_key END,CASE WHEN length(target_key)=32 THEN target_key END,kind FROM chat_delivery_observations WHERE workspace=? AND operation_id=? ORDER BY recipient,request_key,target_key,kind LIMIT 2305") + .bind(selected.workspace_id().as_bytes().as_slice()).bind(operation.as_bytes().as_slice()).fetch_all(&mut *connection).await.map_err(bad)?; + if rows.len() > MAX_OBSERVATIONS { + return Err(OperationError::RecoveryRequired); + } + let mut result = Self { + reports: Vec::with_capacity(rows.len()), + facts: Vec::with_capacity(rows.len()), + }; + for row in rows { + let key = |index| -> Result<[u8; 32], OperationError> { + row.try_get::<Option<Vec<u8>>, _>(index) + .map_err(bad)? + .ok_or(OperationError::RecoveryRequired)? + .try_into() + .map_err(bad) + }; + let recipient = key(0)?; + let request = key(1)?; + let target = key(2)?; + let kind: i64 = row.try_get(3).map_err(bad)?; + if !(1..=6).contains(&kind) { + return Err(OperationError::RecoveryRequired); + } + let attempt = graph + .attempts + .iter() + .find(|attempt| { + attempt.work().evidence().recipient().as_bytes() == &recipient + && codec::request_key(attempt.intent().request_id().as_str()) == request + }) + .ok_or(OperationError::RecoveryRequired)?; + let scope = codec::scope(attempt.work(), request); + let protected = read_body(connection, scope, target, kind as u8).await?; + let plain = crypto + .open_delivery( + context(scope, target, kind as u8), + DeliveryPayload::Observation, + 0, + &protected, + ) + .await?; + let wire: Wire = plain.expose_secret(|raw| { + if !super::json::bounded(raw, 32) { + return Err(OperationError::RecoveryRequired); + } + serde_json::from_slice(raw).map_err(bad) + })?; + let report = decode(wire, attempt, target, kind as u8)?; + result.reports.push(report); + result.facts.push(Fact { + scope, + target, + kind: kind as u8, + digest: Sha256::digest(&protected).into(), + }); + } + Ok(result) + } + pub(in crate::workspace) async fn unchanged( + &self, + connection: &mut SqliteConnection, + selected: &super::super::AttachmentInput, + operation: radroots_domain::OperationId, + ) -> Result<(), OperationError> { + let count:i64=sqlx::query_scalar("SELECT count(*) FROM (SELECT 1 FROM chat_delivery_observations WHERE workspace=? AND operation_id=? LIMIT 2305)") + .bind(selected.workspace_id().as_bytes().as_slice()).bind(operation.as_bytes().as_slice()).fetch_one(&mut *connection).await.map_err(bad)?; + if count != self.facts.len() as i64 { + return Err(OperationError::RecoveryRequired); + } + for fact in &self.facts { + let protected = read_body(connection, fact.scope, fact.target, fact.kind).await?; + if <[u8; 32]>::from(Sha256::digest(&protected)) != fact.digest { + return Err(OperationError::RecoveryRequired); + } + } + Ok(()) + } +} +fn decode( + wire: Wire, + attempt: &DeliveryAttempt, + target: [u8; 32], + kind: u8, +) -> Result<DeliveryObservation, OperationError> { + if wire.version != 1 + || wire.work != WorkRef::new(attempt.work()) + || wire.request != attempt.intent().request_id().as_str() + || wire.event != attempt.intent().event_id() + || wire.envelope_digest != *attempt.envelope_digest() + || wire.kind != kind + { + return Err(OperationError::RecoveryRequired); + } + let report = DeliveryObservation::from_store_report(attempt, &wire.target, wire.observed_at) + .map_err(bad)?; + if report.kind().code() != kind || target_key(&report) != target { + return Err(OperationError::RecoveryRequired); + } + Ok(report) +} +async fn read_body( + connection: &mut SqliteConnection, + s: DeliveryScope, + target: [u8; 32], + kind: u8, +) -> Result<Vec<u8>, OperationError> { + sqlx::query_scalar::<_,Option<Vec<u8>>>("SELECT CASE WHEN length(protected)>0 AND length(protected)<=262144 THEN protected END FROM chat_delivery_observations WHERE workspace=? AND operation_id=? AND recipient=? AND request_key=? AND target_key=? AND kind=?") + .bind(s.workspace.as_bytes().as_slice()).bind(s.operation.as_bytes().as_slice()).bind(s.recipient.as_bytes().as_slice()).bind(s.request.as_slice()).bind(target.as_slice()).bind(kind as i64).fetch_optional(connection).await.map_err(bad)?.flatten().ok_or(OperationError::RecoveryRequired) +} diff --git a/crates/storage_sqlite/src/workspace/delivery/observation_operations.rs b/crates/storage_sqlite/src/workspace/delivery/observation_operations.rs @@ -0,0 +1,237 @@ +use super::super::{ + AttachmentProof, WorkspaceBinding, + admission::{Journal, recheck}, + installation::Records, + snapshot::RetainedGraph, + worker, +}; +use super::{ + graph::Graph, + observation::{self, Observations}, +}; +use radroots_domain::{CommandDigest, OperationId, PublicKey}; +use radroots_storage::command::{DeliveryObservation, DeliveryObservationOutcome, OperationError}; +use radroots_transport::sink::DeliveryRequestId; +use sqlx::Connection; + +impl Journal<'_> { + pub(in crate::workspace) async fn capture_delivery_observation( + &mut self, + binding: WorkspaceBinding, + proof: AttachmentProof, + supplied: DeliveryObservation, + ) -> Result<DeliveryObservationOutcome, OperationError> { + let (record, grant) = self.capture_access(binding.input(), &proof).await?; + let work = supplied.work(); + let e = work.evidence(); + let original = RetainedGraph::read( + self.connection, + self.selected, + &binding, + e.operation_id(), + Some(e.command_digest()), + self.crypto, + ) + .await?; + let installed = Records::read( + self.connection, + self.selected, + e.operation_id(), + &original, + self.crypto, + ) + .await?; + let deliveries = Graph::read( + self.connection, + self.selected, + e.operation_id(), + &original, + &installed, + self.crypto, + ) + .await?; + let observations = Observations::read( + self.connection, + &deliveries, + self.selected, + e.operation_id(), + self.crypto, + ) + .await?; + let actual = deliveries + .attempts + .iter() + .find(|value| { + value.work() == work && value.intent().request_id() == supplied.request_id() + }) + .ok_or(OperationError::InvalidEvidence)?; + let checked = DeliveryObservation::from_store_report( + actual, + supplied.target_receipt(), + supplied.observed_at_unix_ms(), + )?; + if checked != supplied { + return Err(OperationError::InvalidEvidence); + } + let existing = observations.reports.iter().find(|value| { + value.request_id() == supplied.request_id() + && value.work() == work + && value.target_receipt().target().fingerprint() + == supplied.target_receipt().target().fingerprint() + && value.kind() == supplied.kind() + }); + // The first original outcome of each class, including absent time, is + // immutable. Further classes append; they do not replace earlier facts. + let retained = existing.cloned().unwrap_or(supplied); + let fresh = existing.is_none(); + let protected = if fresh { + Some(observation::protect(retained.clone(), self.crypto).await?) + } else { + None + }; + let mut transaction = self + .connection + .begin_with("BEGIN IMMEDIATE") + .await + .map_err(|_| OperationError::StorageFailure)?; + let changed = async { + self.namespace.validate()?; + if worker::read(&mut transaction, self.selected).await? != grant { + return Err(OperationError::RecoveryRequired); + } + record.authenticate_retained_capture(binding.input(), &proof)?; + original + .unchanged(&mut transaction, self.selected, e.operation_id()) + .await?; + installed + .unchanged(&mut transaction, self.selected, e.operation_id()) + .await?; + deliveries + .unchanged(&mut transaction, self.selected, e.operation_id()) + .await?; + observations + .unchanged(&mut transaction, self.selected, e.operation_id()) + .await?; + if let Some(value) = &protected { + value.insert(&mut transaction).await?; + } + Ok::<_, OperationError>(()) + } + .await; + if let Err(error) = changed { + if transaction.rollback().await.is_err() { + *self.uncertain = true; + return Ok(DeliveryObservationOutcome::Unknown(retained)); + } + return Err(error); + } + let finished = if fresh { + transaction.commit().await + } else { + transaction.rollback().await + }; + if finished.is_err() { + *self.uncertain = true; + return Ok(DeliveryObservationOutcome::Unknown(retained)); + } + Ok(DeliveryObservationOutcome::Committed(retained)) + } + + pub(in crate::workspace) async fn query_delivery_observations( + &mut self, + binding: WorkspaceBinding, + operation: OperationId, + digest: CommandDigest, + recipient: PublicKey, + request: DeliveryRequestId, + proof: AttachmentProof, + ) -> Result<Vec<DeliveryObservation>, OperationError> { + if request.as_str().len() != 64 + || !request + .as_str() + .bytes() + .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) + { + return Err(OperationError::InvalidCommand); + } + let (record, grant, _) = self.access(&binding, &proof).await?; + let original = RetainedGraph::read( + self.connection, + self.selected, + &binding, + operation, + Some(digest), + self.crypto, + ) + .await?; + let installed = Records::read( + self.connection, + self.selected, + operation, + &original, + self.crypto, + ) + .await?; + let deliveries = Graph::read( + self.connection, + self.selected, + operation, + &original, + &installed, + self.crypto, + ) + .await?; + let observations = Observations::read( + self.connection, + &deliveries, + self.selected, + operation, + self.crypto, + ) + .await?; + let mut transaction = self + .connection + .begin_with("BEGIN IMMEDIATE") + .await + .map_err(|_| OperationError::StorageFailure)?; + let checked = async { + recheck( + &mut transaction, + self.namespace, + self.selected, + &grant, + &record, + &binding, + &proof, + self.clock, + ) + .await?; + original + .unchanged(&mut transaction, self.selected, operation) + .await?; + installed + .unchanged(&mut transaction, self.selected, operation) + .await?; + deliveries + .unchanged(&mut transaction, self.selected, operation) + .await?; + observations + .unchanged(&mut transaction, self.selected, operation) + .await?; + Ok::<_, OperationError>(()) + } + .await; + if transaction.rollback().await.is_err() { + *self.uncertain = true; + return Err(OperationError::RecoveryRequired); + } + checked?; + Ok(observations + .reports + .into_iter() + .filter(|value| { + value.request_id() == &request && value.work().evidence().recipient() == recipient + }) + .collect()) + } +} diff --git a/crates/storage_sqlite/src/workspace/delivery/observation_tests.rs b/crates/storage_sqlite/src/workspace/delivery/observation_tests.rs @@ -0,0 +1,89 @@ +#[cfg(test)] +mod cases { + use super::super::*; + use crate::workspace::tests::issuance_records::delivery_data; + use radroots_transport::outcome::DeliveryOutcome; + use serde_json::{Value, json}; + + #[tokio::test] + async fn observation_wire_binds_every_original_fact_and_preserves_nullable_time() { + let mut data = delivery_data().await; + let report = DeliveryObservation::from_store_report( + &data.report, + &DeliveryTargetReceipt::attempted( + data.report.intent().targets().targets()[0].clone(), + DeliveryOutcome::accepted(), + ), + None, + ) + .unwrap(); + let protected = protect(report.clone(), &data.crypto).await.unwrap(); + protected.insert(&mut data.database).await.unwrap(); + let kind = report.kind().code(); + let target = target_key(&report); + let original = read_body(&mut data.database, protected.scope, target, kind) + .await + .unwrap(); + let plain = data + .crypto + .open_delivery( + context(protected.scope, target, kind), + DeliveryPayload::Observation, + 0, + &original, + ) + .await + .unwrap(); + let base: Value = plain.expose_secret(|raw| serde_json::from_slice(raw).unwrap()); + let wire: Wire = serde_json::from_slice(&serde_json::to_vec(&base).unwrap()).unwrap(); + assert_eq!(decode(wire, &data.report, target, kind).unwrap(), report); + let mut rebound = base["work"].clone(); + rebound["transaction"] = json!("99".repeat(32)); + for (field, value) in [ + ("version", json!(2)), + ("work", rebound), + ("request", json!("02".repeat(32))), + ("event", json!("99".repeat(32))), + ("envelope_digest", json!(vec![0u8; 32])), + ("kind", json!(2)), + ("observed_at", json!(253_402_300_800_000u64)), + ] { + let mut changed = base.clone(); + changed[field] = value; + let decoded: Wire = + serde_json::from_slice(&serde_json::to_vec(&changed).unwrap()).unwrap(); + assert!( + decode(decoded, &data.report, target, kind).is_err(), + "{field}" + ); + } + let wire: Wire = serde_json::from_slice(&serde_json::to_vec(&base).unwrap()).unwrap(); + assert!(decode(wire, &data.report, [0; 32], kind).is_err()); + let mut changed = base.clone(); + changed["target"] = serde_json::to_value(DeliveryTargetReceipt::attempted( + data.report.intent().targets().targets()[0].clone(), + DeliveryOutcome::rejected(), + )) + .unwrap(); + let wire: Wire = serde_json::from_slice(&serde_json::to_vec(&changed).unwrap()).unwrap(); + assert!(decode(wire, &data.report, target, kind).is_err()); + sqlx::query("UPDATE chat_delivery_observations SET protected=zeroblob(262145)") + .execute(&mut data.database) + .await + .unwrap(); + assert!( + read_body(&mut data.database, protected.scope, target, kind) + .await + .is_err() + ); + sqlx::query("DELETE FROM chat_delivery_observations") + .execute(&mut data.database) + .await + .unwrap(); + assert!( + read_body(&mut data.database, protected.scope, target, kind) + .await + .is_err() + ); + } +} diff --git a/crates/storage_sqlite/src/workspace/delivery/operations.rs b/crates/storage_sqlite/src/workspace/delivery/operations.rs @@ -0,0 +1,413 @@ +use super::super::{ + AttachmentProof, WorkspaceBinding, + admission::{Journal, recheck}, + installation::Records, + snapshot::RetainedGraph, +}; +use super::{codec, graph::Graph, observation::Observations}; +use nostr::JsonUtil; +use radroots_domain::{CommandDigest, OperationId, PublicKey, TransactionId}; +use radroots_storage::command::{ + CommitAttempt, DeliveryAttempt, DeliveryAttemptOutcome, DeliveryAttemptRequest, + EnvelopeCandidate, OperationError, PublicationWork, +}; +use radroots_transport::sink::DeliveryRequestId; +use sha2::{Digest, Sha256}; +use sqlx::Connection; +use subtle::ConstantTimeEq; +use zeroize::Zeroizing; + +fn contains_original( + candidate: &EnvelopeCandidate, + work: PublicationWork, + original: &RetainedGraph, +) -> Result<(), OperationError> { + let bound = original + .bound_plan() + .ok_or(OperationError::RecoveryRequired)?; + let source = original + .obtained(work.evidence().recipient()) + .ok_or(OperationError::RecoveryRequired)?; + let actor = bound + .prepared() + .request() + .command() + .context() + .actor() + .participant(); + if candidate.work() != work + || candidate.author() == actor + || candidate.author() == bound.prepared().request().command().peer() + || candidate.author() == work.evidence().recipient() + || source.digest() != work.evidence().digest() + { + return Err(OperationError::InvalidEvidence); + } + let event = nostr::Event::from_json(candidate.raw_json()) + .map_err(|_| OperationError::InvalidEvidence)?; + event + .verify() + .map_err(|_| OperationError::InvalidEvidence)?; + let recipient = nostr::PublicKey::from_slice(work.evidence().recipient().as_bytes()) + .map_err(|_| OperationError::InvalidEvidence)?; + let opened = candidate.with_wrapping_key(|key| { + let secret = + nostr::SecretKey::from_slice(key).map_err(|_| OperationError::InvalidEvidence)?; + let keys = nostr::Keys::new(secret); + if keys.public_key().to_bytes() != *candidate.author().as_bytes() { + return Err(OperationError::InvalidEvidence); + } + nostr::nips::nip44::decrypt(keys.secret_key(), &recipient, &event.content) + .map(Zeroizing::new) + .map_err(|_| OperationError::InvalidEvidence) + })?; + let digest: [u8; 32] = Sha256::digest(opened.as_bytes()).into(); + if digest.ct_eq(source.digest()).unwrap_u8() != 1 + || opened.as_bytes() != source.raw_json().as_bytes() + { + return Err(OperationError::InvalidEvidence); + } + Ok(()) +} +fn work(records: &Records, requested: PublicationWork) -> Result<PublicationWork, OperationError> { + let actual = records + .work + .iter() + .find(|value| value.report.evidence().recipient() == requested.evidence().recipient()) + .ok_or(OperationError::InvalidEvidence)? + .report; + if actual != requested { + return Err(OperationError::InvalidEvidence); + } + Ok(actual) +} +fn unknown(anchor: CommitAttempt, attempt: DeliveryRequestId) -> DeliveryAttemptOutcome { + DeliveryAttemptOutcome::Unknown { + operation: anchor, + attempt, + } +} + +impl Journal<'_> { + pub(in crate::workspace) async fn prepare_delivery( + &mut self, + request: DeliveryAttemptRequest, + proof: AttachmentProof, + candidate: Option<EnvelopeCandidate>, + ) -> Result<DeliveryAttemptOutcome, OperationError> { + let supplied = request.work(); + let evidence = supplied.evidence(); + let (record, grant, current) = self.access(request.binding(), &proof).await?; + let original = RetainedGraph::read( + self.connection, + self.selected, + request.binding(), + evidence.operation_id(), + Some(evidence.command_digest()), + self.crypto, + ) + .await?; + let installed = Records::read( + self.connection, + self.selected, + evidence.operation_id(), + &original, + self.crypto, + ) + .await?; + let actual = work(&installed, supplied)?; + let mut deliveries = Graph::read( + self.connection, + self.selected, + evidence.operation_id(), + &original, + &installed, + self.crypto, + ) + .await?; + let existing = deliveries.attempts.iter().position(|value| { + value.work().evidence().recipient() == evidence.recipient() + && value.intent().request_id() == request.intent().request_id() + }); + let observations = Observations::read( + self.connection, + &deliveries, + self.selected, + evidence.operation_id(), + self.crypto, + ) + .await?; + if let Some(index) = existing { + let value = &deliveries.attempts[index]; + if value.work() != actual + || value.intent().digest() != request.intent().digest() + || candidate.as_ref().is_some_and(|candidate| { + candidate.work() != actual || candidate.raw_json() != value.raw_json() + }) + { + return Err(OperationError::IdempotencyConflict); + } + } + let fresh = existing.is_none(); + if deliveries.attempts.iter().any(|value| { + value.work() == actual + && value.intent().publication_policy_digest() + != request.intent().publication_policy_digest() + }) { + return Err(OperationError::IdempotencyConflict); + } + let prepared = if fresh { + let bound = original + .bound_plan() + .ok_or(OperationError::RecoveryRequired)?; + deliveries.authorize_routes(bound, evidence.recipient())?; + let ordinals = deliveries.ordinals(evidence.recipient(), request.intent())?; + if current.observed_at() < actual.queued_at() + || deliveries + .attempts + .iter() + .any(|value| value.retained_at() > current.observed_at()) + { + return Err(super::super::WorkspaceError::Unavailable.into()); + } + if current + .observed_at() + .get() + .checked_mul(1000) + .is_none_or(|now| now >= request.intent().deadline_unix_ms().get()) + { + return Err(OperationError::DeadlineExceeded); + } + let raw = match &candidate { + Some(candidate) => { + if candidate.event_id() != request.intent().event_id() { + return Err(OperationError::InvalidEvidence); + } + contains_original(candidate, actual, &original)?; + Zeroizing::new(String::from(candidate.raw_json())) + } + None => { + let retained = deliveries + .attempts + .iter() + .find(|value| { + value.work() == actual + && value.intent().event_id() == request.intent().event_id() + }) + .ok_or(OperationError::InvalidEvidence)?; + Zeroizing::new(String::from(retained.raw_json())) + } + }; + // A second representation of one outer ID cannot replace the first. + if deliveries.attempts.iter().any(|value| { + value.work() == actual + && value.intent().event_id() == request.intent().event_id() + && value.raw_json() != raw.as_str() + }) { + return Err(OperationError::IdempotencyConflict); + } + let value = DeliveryAttempt::from_store_report( + actual, + request.intent().clone(), + &raw, + TransactionId::from_bytes(super::super::random()?), + current.observed_at(), + ordinals, + )?; + Some(codec::protect(value, self.crypto).await?) + } else { + None + }; + let retained = match (&prepared, existing) { + (Some(value), _) => &value.report, + (None, Some(index)) => &deliveries.attempts[index], + _ => return Err(OperationError::RecoveryRequired), + }; + let anchor = CommitAttempt::new( + evidence.workspace_id(), + evidence.operation_id(), + evidence.command_digest(), + Some(retained.transaction_id()), + ); + let identity = request.intent().request_id().clone(); + 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?; + original + .unchanged(&mut transaction, self.selected, evidence.operation_id()) + .await?; + installed + .unchanged(&mut transaction, self.selected, evidence.operation_id()) + .await?; + deliveries + .unchanged(&mut transaction, self.selected, evidence.operation_id()) + .await?; + observations + .unchanged(&mut transaction, self.selected, evidence.operation_id()) + .await?; + if fresh { + if now.observed_at() < current.observed_at() { + return Err(super::super::WorkspaceError::Unavailable.into()); + } + if now + .observed_at() + .get() + .checked_mul(1000) + .is_none_or(|now| now >= request.intent().deadline_unix_ms().get()) + { + return Err(OperationError::DeadlineExceeded); + } + codec::absent( + &mut transaction, + actual, + codec::request_key(identity.as_str()), + ) + .await?; + codec::insert( + &mut transaction, + prepared.as_ref().ok_or(OperationError::RecoveryRequired)?, + ) + .await?; + } + Ok::<_, OperationError>(()) + } + .await; + if let Err(error) = changed { + if transaction.rollback().await.is_err() { + *self.uncertain = true; + return Ok(unknown(anchor, identity)); + } + return Err(error); + } + let completed = if fresh { + transaction.commit().await + } else { + transaction.rollback().await + }; + if completed.is_err() { + *self.uncertain = true; + return Ok(unknown(anchor, identity)); + } + match prepared { + Some(value) => Ok(DeliveryAttemptOutcome::Claimed(Box::new(value.report))), + None => Ok(DeliveryAttemptOutcome::Retained(Box::new( + deliveries + .attempts + .remove(existing.ok_or(OperationError::RecoveryRequired)?), + ))), + } + } + + pub(in crate::workspace) async fn query_delivery( + &mut self, + binding: WorkspaceBinding, + operation: OperationId, + digest: CommandDigest, + recipient: PublicKey, + request: DeliveryRequestId, + proof: AttachmentProof, + ) -> Result<Option<DeliveryAttempt>, OperationError> { + if request.as_str().len() != 64 + || !request + .as_str() + .bytes() + .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) + { + return Err(OperationError::InvalidCommand); + } + let (record, grant, _) = self.access(&binding, &proof).await?; + let original = RetainedGraph::read( + self.connection, + self.selected, + &binding, + operation, + Some(digest), + self.crypto, + ) + .await?; + let installed = Records::read( + self.connection, + self.selected, + operation, + &original, + self.crypto, + ) + .await?; + let mut deliveries = Graph::read( + self.connection, + self.selected, + operation, + &original, + &installed, + self.crypto, + ) + .await?; + let observations = Observations::read( + self.connection, + &deliveries, + self.selected, + operation, + self.crypto, + ) + .await?; + let mut transaction = self + .connection + .begin_with("BEGIN IMMEDIATE") + .await + .map_err(|_| OperationError::StorageFailure)?; + let checked = async { + recheck( + &mut transaction, + self.namespace, + self.selected, + &grant, + &record, + &binding, + &proof, + self.clock, + ) + .await?; + original + .unchanged(&mut transaction, self.selected, operation) + .await?; + installed + .unchanged(&mut transaction, self.selected, operation) + .await?; + deliveries + .unchanged(&mut transaction, self.selected, operation) + .await?; + observations + .unchanged(&mut transaction, self.selected, operation) + .await?; + Ok::<_, OperationError>(()) + } + .await; + if transaction.rollback().await.is_err() { + *self.uncertain = true; + return Err(OperationError::RecoveryRequired); + } + checked?; + Ok(deliveries + .attempts + .iter() + .position(|value| { + value.work().evidence().recipient() == recipient + && value.intent().request_id() == &request + }) + .map(|index| deliveries.attempts.remove(index))) + } +} diff --git a/crates/storage_sqlite/src/workspace/installation.rs b/crates/storage_sqlite/src/workspace/installation.rs @@ -30,7 +30,7 @@ pub(super) const WORK: &str = "CREATE TABLE chat_publication_work (workspace BLO #[derive(Serialize, Deserialize, Eq, PartialEq)] #[serde(deny_unknown_fields)] -struct EvidenceRef { +pub(super) struct EvidenceRef { workspace: WorkspaceId, operation: OperationId, command: CommandDigest, @@ -42,7 +42,7 @@ struct EvidenceRef { observed_at: Option<u64>, } impl EvidenceRef { - fn new(value: EvidenceReceipt) -> Self { + pub(super) fn new(value: EvidenceReceipt) -> Self { Self { workspace: value.workspace_id(), operation: value.operation_id(), diff --git a/crates/storage_sqlite/src/workspace/issuance.rs b/crates/storage_sqlite/src/workspace/issuance.rs @@ -288,7 +288,7 @@ impl Journal<'_> { /// Original-scope append admission deliberately does not consult a clock, /// revocation flag or fresh command permission. The actual existing claim, /// immutable plan and authenticated exact bytes are independently required. - async fn capture_access( + pub(super) async fn capture_access( &mut self, input: &AttachmentInput, proof: &AttachmentProof, diff --git a/crates/storage_sqlite/src/workspace/mod.rs b/crates/storage_sqlite/src/workspace/mod.rs @@ -8,6 +8,7 @@ mod admission; mod attempt; mod crypto; +mod delivery; mod evidence; mod fs; mod installation; @@ -219,6 +220,77 @@ impl WorkspaceStore for NativeWorkspace { } } impl radroots_storage::command::OperationJournal for NativeWorkspace { + fn record_delivery_observation( + &self, + binding: WorkspaceBinding, + proof: AttachmentProof, + observation: radroots_storage::command::DeliveryObservation, + ) -> BoxFuture< + '_, + Result< + radroots_storage::command::DeliveryObservationOutcome, + radroots_storage::command::OperationError, + >, + > { + Box::pin( + self.worker + .capture_delivery_observation(binding, proof, observation), + ) + } + fn delivery_observations( + &self, + binding: WorkspaceBinding, + operation: radroots_domain::OperationId, + digest: radroots_domain::CommandDigest, + recipient: PublicKey, + request: radroots_transport::sink::DeliveryRequestId, + proof: AttachmentProof, + ) -> BoxFuture< + '_, + Result< + Vec<radroots_storage::command::DeliveryObservation>, + radroots_storage::command::OperationError, + >, + > { + Box::pin( + self.worker + .query_delivery_observations(binding, operation, digest, recipient, request, proof), + ) + } + fn prepare_delivery_attempt<'a>( + &'a self, + request: radroots_storage::command::DeliveryAttemptRequest, + proof: AttachmentProof, + candidate: Option<&'a radroots_storage::command::EnvelopeCandidate>, + ) -> BoxFuture< + 'a, + Result< + radroots_storage::command::DeliveryAttemptOutcome, + radroots_storage::command::OperationError, + >, + > { + Box::pin(self.worker.prepare_delivery(request, proof, candidate)) + } + fn delivery_attempt( + &self, + binding: WorkspaceBinding, + operation: radroots_domain::OperationId, + digest: radroots_domain::CommandDigest, + recipient: PublicKey, + request: radroots_transport::sink::DeliveryRequestId, + proof: AttachmentProof, + ) -> BoxFuture< + '_, + Result< + Option<radroots_storage::command::DeliveryAttempt>, + radroots_storage::command::OperationError, + >, + > { + Box::pin( + self.worker + .query_delivery(binding, operation, digest, recipient, request, proof), + ) + } fn install_transition_and_outbox( &self, request: radroots_storage::command::InstallationRequest, diff --git a/crates/storage_sqlite/src/workspace/snapshot.rs b/crates/storage_sqlite/src/workspace/snapshot.rs @@ -4,6 +4,7 @@ use super::{ admission::{Journal, recheck, same_binding}, attempt, crypto::IssuanceScope, + delivery::{graph::Graph as DeliveryGraph, observation::Observations}, evidence::{self, ProtectedEvidence}, installation::Records, issuance::absent_claim, @@ -12,8 +13,8 @@ use super::{ }; use radroots_domain::{CommandDigest, EnvelopeEventId, OperationId, PublicKey, UnixSeconds}; use radroots_storage::command::{ - BoundSigningPlan, ClaimSnapshot, OperationError, OperationSnapshot, RecipientSnapshot, - RetainedOperation, SigningAttempt, + BoundSigningPlan, ClaimSnapshot, DeliveryAttemptSummary, OperationError, OperationSnapshot, + RecipientSnapshot, RetainedOperation, SealEvidence, SigningAttempt, }; use sqlx::{Connection, SqliteConnection}; @@ -21,6 +22,7 @@ struct SeenRecipient { report: RecipientSnapshot, claim: Option<Vec<u8>>, evidence: Option<ProtectedEvidence>, + obtained: Option<SealEvidence>, } pub(super) struct RetainedGraph { retained: Option<(RetainedOperation, Vec<u8>)>, @@ -28,6 +30,12 @@ pub(super) struct RetainedGraph { recipients: Vec<SeenRecipient>, } impl RetainedGraph { + pub(super) fn obtained(&self, recipient: PublicKey) -> Option<&SealEvidence> { + self.recipients + .iter() + .find(|seen| seen.report.recipient() == recipient) + .and_then(|seen| seen.obtained.as_ref()) + } pub(super) fn bound_plan(&self) -> Option<&BoundSigningPlan> { self.retained .as_ref() @@ -81,9 +89,11 @@ impl RetainedGraph { )?; 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)), - None => (None, None), + let (obtained, evidence, original) = match captured { + Some((value, protected)) => { + (Some(protected.receipt), Some(protected), Some(value)) + } + None => (None, None, None), }; recipients.push(SeenRecipient { report: RecipientSnapshot::from_store_report( @@ -94,6 +104,7 @@ impl RetainedGraph { ), claim: Some(protected), evidence, + obtained: original, }); bound = attempt.into_bound_plan(); } else { @@ -106,6 +117,7 @@ impl RetainedGraph { ), claim: None, evidence: None, + obtained: None, }); bound = returned; } @@ -121,6 +133,7 @@ impl RetainedGraph { report: RecipientSnapshot::from_store_report(recipient, None, None, None), claim: None, evidence: None, + obtained: None, }); } RetainedOperation::Prepared(prepared) @@ -220,6 +233,23 @@ impl Journal<'_> { self.crypto, ) .await?; + let deliveries = DeliveryGraph::read( + self.connection, + self.selected, + operation_id, + &graph, + &records, + self.crypto, + ) + .await?; + let observations = Observations::read( + self.connection, + &deliveries, + self.selected, + operation_id, + self.crypto, + ) + .await?; // Provider and signature verification work precedes the transaction. let mut transaction = self .connection @@ -244,6 +274,12 @@ impl Journal<'_> { records .unchanged(&mut transaction, self.selected, operation_id) .await?; + deliveries + .unchanged(&mut transaction, self.selected, operation_id) + .await?; + observations + .unchanged(&mut transaction, self.selected, operation_id) + .await?; Ok::<_, OperationError>(observed.observed_at()) } .await; @@ -254,10 +290,19 @@ impl Journal<'_> { graph .into_snapshot(checked?)? .map(|snapshot| { - snapshot.with_installation( - records.message.map(|value| value.report), - records.work.into_iter().map(|value| value.report).collect(), - ) + snapshot + .with_installation( + records.message.map(|value| value.report), + records.work.into_iter().map(|value| value.report).collect(), + )? + .with_delivery( + deliveries + .attempts + .iter() + .map(DeliveryAttemptSummary::from_attempt) + .collect(), + observations.reports, + ) }) .transpose() } diff --git a/crates/storage_sqlite/src/workspace/tests/issuance_records.rs b/crates/storage_sqlite/src/workspace/tests/issuance_records.rs @@ -24,6 +24,105 @@ struct Issued { evidence: SealEvidence, path: std::path::PathBuf, } + +/// Codec-only data fixture. Structural reports and encrypted records here do +/// not establish native T2/D authorization; the facade owns those runtime cases. +#[cfg(test)] +pub(in crate::workspace) struct DeliveryData { + _issued: Issued, + pub(in crate::workspace) database: SqliteConnection, + pub(in crate::workspace) crypto: WorkspaceCrypto, + pub(in crate::workspace) report: radroots_storage::command::DeliveryAttempt, +} +#[cfg(test)] +impl DeliveryData { + pub(in crate::workspace) fn selection(&self) -> AttachmentInput { + self._issued.selected.clone() + } + pub(in crate::workspace) async fn bound(&mut self) -> BoundSigningPlan { + self._issued.bound(&mut self.database).await + } +} +#[cfg(test)] +pub(in crate::workspace) async fn delivery_data() -> DeliveryData { + use radroots_storage::command::{ + ChatInstallation, DeliveryAttempt, DeliveryIntent, EvidenceReceipt, PublicationWork, + }; + use radroots_transport::{ + Target, TargetSet, + policy::{SatisfactionClass, SatisfactionPolicy, TargetPolicy}, + }; + let issued = Issued::new().await; + let mut database = issued.database().await; + let bound = issued.bound(&mut database).await; + let evidence = EvidenceReceipt::from_committed_store( + &issued.evidence, + TransactionId::from_bytes([70; 32]), + ); + let time = UnixSeconds::new(100).unwrap(); + let installed = ChatInstallation::from_committed_store( + &bound, + evidence, + TransactionId::from_bytes([71; 32]), + time, + ) + .unwrap(); + let work = PublicationWork::from_committed_store( + &bound, + installed, + evidence, + TransactionId::from_bytes([72; 32]), + time, + ) + .unwrap(); + let mut scalar = [0; 32]; + scalar[31] = 3; + let keys = nostr::Keys::new(nostr::SecretKey::from_slice(&scalar).unwrap()); + let recipient = nostr::PublicKey::from_slice(work.evidence().recipient().as_bytes()).unwrap(); + let raw = nostr::UnsignedEvent { + id: None, + pubkey: keys.public_key(), + created_at: nostr::Timestamp::from_secs(99), + kind: nostr::Kind::Custom(1059), + tags: nostr::Tags::from_list(vec![nostr::Tag::public_key(recipient)]), + content: nip44::encrypt( + keys.secret_key(), + &recipient, + "codec private record ".repeat(1200), + nip44::Version::V2, + ) + .unwrap(), + } + .sign_with_keys(&keys) + .unwrap() + .as_json(); + let targets = + TargetSet::new(vec![Target::nostr_relay("wss://route.example/").unwrap()]).unwrap(); + let intent = DeliveryIntent::new( + &"01".repeat(32), + EnvelopeEventId::from_bytes(*nostr::Event::from_json(&raw).unwrap().id.as_bytes()), + &targets, + &SatisfactionPolicy::new(SatisfactionClass::Accepted, TargetPolicy::all()), + core::num::NonZeroU64::new(110_000).unwrap(), + ) + .unwrap(); + let report = DeliveryAttempt::from_store_report( + work, + intent, + &raw, + TransactionId::from_bytes([73; 32]), + time, + vec![1], + ) + .unwrap(); + let crypto = issued.fixture.crypto(); + DeliveryData { + _issued: issued, + database, + crypto, + report, + } +} impl Issued { async fn new() -> Self { let fixture = CryptoFixture::new(); diff --git a/crates/storage_sqlite/src/workspace/tests/mod.rs b/crates/storage_sqlite/src/workspace/tests/mod.rs @@ -1,5 +1,5 @@ use super::*; -mod issuance_records; +pub(super) mod issuance_records; mod operation_records; mod plan_records; use radroots_domain::{ActorRef, SourceId}; @@ -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=7", + 0 => "PRAGMA user_version=8", 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 { 7 } else { 6 }); + assert_eq!(version, if fault == 0 { 8 } else { 7 }); 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,15 +1,18 @@ use super::{ AttachmentInput, AttachmentProof, WorkspaceBinding, WorkspaceClock, WorkspaceCrypto, - WorkspaceError, admission::Journal, attempt, evidence, fs::Namespace, installation, operation, - plan, record::GrantRecord, + WorkspaceError, admission::Journal, attempt, delivery, evidence, fs::Namespace, installation, + operation, plan, record::GrantRecord, }; -use radroots_domain::{CommandDigest, OperationId}; +use radroots_domain::{CommandDigest, OperationId, PublicKey}; use radroots_storage::command::{ AdmissionOutcome, AdmissionRequest, BoundSigningPlan, CapturedSeal, ChatSigningPlan, - ClaimAttemptOutcome, ClaimAttemptRequest, CommitAttempt, EvidenceCaptureOutcome, - InstallationOutcome, InstallationRequest, OperationError, OperationSnapshot, - PlanBindingOutcome, PreparedOperation, SealEvidence, SigningAttempt, + ClaimAttemptOutcome, ClaimAttemptRequest, CommitAttempt, DeliveryAttempt, + DeliveryAttemptOutcome, DeliveryAttemptRequest, DeliveryObservation, + DeliveryObservationOutcome, EnvelopeCandidate, EvidenceCaptureOutcome, InstallationOutcome, + InstallationRequest, OperationError, OperationSnapshot, PlanBindingOutcome, PreparedOperation, + SealEvidence, SigningAttempt, }; +use radroots_transport::sink::DeliveryRequestId; use sqlx::{ Connection, Row, SqliteConnection, sqlite::{SqliteConnectOptions, SqliteJournalMode, SqliteSynchronous}, @@ -33,6 +36,36 @@ 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 { + CaptureDeliveryObservation( + Box<WorkspaceBinding>, + AttachmentProof, + Box<DeliveryObservation>, + Reply<DeliveryObservationOutcome, OperationError>, + ), + QueryDeliveryObservations( + Box<WorkspaceBinding>, + OperationId, + CommandDigest, + PublicKey, + DeliveryRequestId, + AttachmentProof, + Reply<Vec<DeliveryObservation>, OperationError>, + ), + PrepareDelivery( + Box<DeliveryAttemptRequest>, + AttachmentProof, + Option<Box<EnvelopeCandidate>>, + Reply<DeliveryAttemptOutcome, OperationError>, + ), + QueryDelivery( + Box<WorkspaceBinding>, + OperationId, + CommandDigest, + PublicKey, + DeliveryRequestId, + AttachmentProof, + Reply<Option<DeliveryAttempt>, OperationError>, + ), Install( Box<InstallationRequest>, AttachmentProof, @@ -184,6 +217,87 @@ impl Handle { if ready.send(Ok(())).is_ok() { while let Ok(Queued { job, _budget }) = requests.recv() { match job { + Job::CaptureDeliveryObservation( + binding, + proof, + observation, + reply, + ) => { + let result = Journal { + connection: &mut connection, + namespace: &namespace, + selected: &input, + crypto: &crypto, + clock: clock.as_ref(), + uncertain: &mut uncertain, + } + .capture_delivery_observation(*binding, proof, *observation) + .await; + let _ = reply.send(result); + } + Job::QueryDeliveryObservations( + binding, + operation, + digest, + recipient, + request, + proof, + reply, + ) => { + let result = Journal { + connection: &mut connection, + namespace: &namespace, + selected: &input, + crypto: &crypto, + clock: clock.as_ref(), + uncertain: &mut uncertain, + } + .query_delivery_observations( + *binding, operation, digest, recipient, request, proof, + ) + .await; + let _ = reply.send(result); + } + Job::PrepareDelivery(request, proof, candidate, reply) => { + let result = Journal { + connection: &mut connection, + namespace: &namespace, + selected: &input, + crypto: &crypto, + clock: clock.as_ref(), + uncertain: &mut uncertain, + } + .prepare_delivery( + *request, + proof, + candidate.map(|value| *value), + ) + .await; + let _ = reply.send(result); + } + Job::QueryDelivery( + binding, + operation, + digest, + recipient, + request, + proof, + reply, + ) => { + let result = Journal { + connection: &mut connection, + namespace: &namespace, + selected: &input, + crypto: &crypto, + clock: clock.as_ref(), + uncertain: &mut uncertain, + } + .query_delivery( + *binding, operation, digest, recipient, request, proof, + ) + .await; + let _ = reply.send(result); + } Job::Install(request, proof, reply) => { let result = Journal { connection: &mut connection, @@ -382,6 +496,8 @@ impl Handle { } fn send(&self, job: Job) -> Result<(), WorkspaceError> { let bytes = match &job { + Job::CaptureDeliveryObservation(..) => 96 * 1024, + Job::PrepareDelivery(..) => 1024 * 1024, Job::BindPlan(..) => 1024 * 1024, Job::Capture(..) => 256 * 1024, Job::Admit(..) => 96 * 1024, @@ -411,6 +527,89 @@ impl Handle { mpsc::TrySendError::Disconnected(_) => WorkspaceError::Closed, }) } + pub(super) async fn prepare_delivery( + &self, + request: DeliveryAttemptRequest, + proof: AttachmentProof, + candidate: Option<&EnvelopeCandidate>, + ) -> Result<DeliveryAttemptOutcome, OperationError> { + let anchor = request.identity(); + let identity = request.intent().request_id().clone(); + let (reply, result) = oneshot::channel(); + self.send(Job::PrepareDelivery( + Box::new(request), + proof, + candidate.map(|value| Box::new(value.retained_copy())), + reply, + ))?; + match result.await { + Ok(result) => result, + Err(_) => Ok(DeliveryAttemptOutcome::Unknown { + operation: anchor, + attempt: identity, + }), + } + } + pub(super) async fn capture_delivery_observation( + &self, + binding: WorkspaceBinding, + proof: AttachmentProof, + observation: DeliveryObservation, + ) -> Result<DeliveryObservationOutcome, OperationError> { + let fallback = observation.clone(); + let (reply, result) = oneshot::channel(); + self.send(Job::CaptureDeliveryObservation( + Box::new(binding), + proof, + Box::new(observation), + reply, + ))?; + result + .await + .unwrap_or(Ok(DeliveryObservationOutcome::Unknown(fallback))) + } + pub(super) async fn query_delivery_observations( + &self, + binding: WorkspaceBinding, + operation: OperationId, + digest: CommandDigest, + recipient: PublicKey, + request: DeliveryRequestId, + proof: AttachmentProof, + ) -> Result<Vec<DeliveryObservation>, OperationError> { + let (reply, result) = oneshot::channel(); + self.send(Job::QueryDeliveryObservations( + Box::new(binding), + operation, + digest, + recipient, + request, + proof, + reply, + ))?; + result.await.map_err(|_| OperationError::RecoveryRequired)? + } + pub(super) async fn query_delivery( + &self, + binding: WorkspaceBinding, + operation: OperationId, + digest: CommandDigest, + recipient: PublicKey, + request: DeliveryRequestId, + proof: AttachmentProof, + ) -> Result<Option<DeliveryAttempt>, OperationError> { + let (reply, result) = oneshot::channel(); + self.send(Job::QueryDelivery( + Box::new(binding), + operation, + digest, + recipient, + request, + proof, + reply, + ))?; + result.await.map_err(|_| OperationError::RecoveryRequired)? + } pub(super) async fn operation_snapshot( &self, binding: WorkspaceBinding, @@ -688,13 +887,17 @@ async fn initialize( installation::TX_INDEX, installation::THREAD_INDEX, installation::WORK, + delivery::TABLE, + delivery::PARTS, + delivery::TX_INDEX, + delivery::OBSERVATIONS, ] { sqlx::raw_sql(definition) .execute(&mut *transaction) .await .map_err(|_| WorkspaceError::StorageFailure)?; } - sqlx::query("PRAGMA user_version=6") + sqlx::query("PRAGMA user_version=7") .execute(&mut *transaction) .await .map_err(|_| WorkspaceError::StorageFailure)?; @@ -716,11 +919,11 @@ async fn initialize( .fetch_one(&mut *connection) .await .map_err(|_| WorkspaceError::RecoveryRequired)?; - if version != 6 { + if version != 7 { return 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)?; + let catalog_matches: i64 = sqlx::query_scalar("SELECT CASE WHEN count(*)=20 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 WHEN type='table' AND name='chat_delivery_attempts' AND tbl_name='chat_delivery_attempts' AND sql=? THEN 1 WHEN type='table' AND name='chat_delivery_parts' AND tbl_name='chat_delivery_parts' AND sql=? THEN 1 WHEN type='index' AND name='chat_delivery_transaction_identity' AND tbl_name='chat_delivery_attempts' AND sql=? THEN 1 WHEN type='table' AND name='chat_delivery_observations' AND tbl_name='chat_delivery_observations' AND sql=? THEN 1 ELSE 0 END)=20 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).bind(delivery::TABLE).bind(delivery::PARTS).bind(delivery::TX_INDEX).bind(delivery::OBSERVATIONS).fetch_one(&mut *connection).await.map_err(|_| WorkspaceError::RecoveryRequired)?; if catalog_matches != 1 { return Err(WorkspaceError::RecoveryRequired); } diff --git a/crates/storage_sqlite/tests/package_boundary.rs b/crates/storage_sqlite/tests/package_boundary.rs @@ -25,6 +25,7 @@ fn sqlite_storage_declares_the_final_backend_boundaries() { BTreeSet::from([ "fs2", "getrandom", + "nostr", "radroots_domain", "radroots_event", "radroots_event_codec", @@ -41,6 +42,25 @@ fn sqlite_storage_declares_the_final_backend_boundaries() { "zeroize", ]) ); + let manifest: toml::Value = toml::from_str(MANIFEST).expect("checked manifest"); + let crypto = manifest["dependencies"]["nostr"] + .as_table() + .expect("explicit crypto dependency"); + assert_eq!( + crypto.keys().map(String::as_str).collect::<BTreeSet<_>>(), + BTreeSet::from(["workspace", "default-features", "features"]) + ); + assert_eq!(crypto["workspace"].as_bool(), Some(true)); + assert_eq!(crypto["default-features"].as_bool(), Some(false)); + assert_eq!( + crypto["features"] + .as_array() + .unwrap() + .iter() + .map(|value| value.as_str().unwrap()) + .collect::<BTreeSet<_>>(), + BTreeSet::from(["std", "nip44"]) + ); assert!(!ROOT.contains("SqlitePool")); assert!(!ROOT.contains("sqlx")); for forbidden in [ diff --git a/crates/transport/README.md b/crates/transport/README.md @@ -11,6 +11,11 @@ outbox claiming, retry loops, schedulers, timers, or fallback policy. Concrete adapters such as `radroots_transport_nostr` implement the SPI; applications compose those adapters in `radroots_sdk` or their own host layer. +`Target::matches_nostr_relay` compares a captured relay identifier using the +target's original construction policy and shared canonical endpoint rules. +Callers preserve original signed source bytes and approve the exact selected +route separately; a canonical match grants no network permission. + The authoritative package charter is the [`radroots_transport` section of the Release V1 specification](../../contracts/crates/release_v1/radroots_crates_release_v1.toml). @@ -29,6 +34,12 @@ The authoritative package charter is the originating request and decides whether normalized retryable outcomes merit another explicit operation. +Delivery receipts and sink-wide partial failures retain the SHA-256 digest of +the exact original signed JSON bytes. Validation refuses another payload even +when request identity and targets match, including a different representation +of the same event ID. Serialized receipts require that digest; decoding alone +does not authenticate an adapter or establish a remote acknowledgement. + [`TransportId`]: crate::TransportId [`Target`]: crate::Target [`TargetSet`]: crate::TargetSet diff --git a/crates/transport/src/error.rs b/crates/transport/src/error.rs @@ -70,6 +70,7 @@ pub enum Error { DeliveryTargetReceiptAttemptMismatch, TransportOutcomeStatusMismatch, DeliveryReceiptRequestIdMismatch, + DeliveryReceiptPayloadMismatch, DeliveryReceiptTargetSetMismatch, EmptyPayloadId, InvalidPayloadId, @@ -227,6 +228,9 @@ impl fmt::Display for Error { Self::DeliveryReceiptRequestIdMismatch => { f.write_str("transport delivery receipt request id does not match its request") } + Self::DeliveryReceiptPayloadMismatch => { + f.write_str("transport delivery receipt exact payload does not match its request") + } Self::DeliveryReceiptTargetSetMismatch => { f.write_str("transport delivery receipt target set does not match its request") } diff --git a/crates/transport/src/sink.rs b/crates/transport/src/sink.rs @@ -14,6 +14,7 @@ use alloc::{ vec::Vec, }; use radroots_event::SignedEvent; +use sha2::{Digest, Sha256}; pub use crate::status::SinkStatus; @@ -25,6 +26,7 @@ pub const DELIVERY_REQUEST_ID_MAX_BYTES: usize = 256; #[derive(Clone, Debug, Eq, PartialEq)] pub struct SinkFailure { request_id: DeliveryRequestId, + exact_payload_sha256: [u8; 32], target_set: TargetSet, code: String, retryability: Retryability, @@ -216,6 +218,7 @@ impl DeliveryTargetReceipt { #[derive(Clone, Debug, Eq, PartialEq)] pub struct DeliveryReceipt { request_id: DeliveryRequestId, + exact_payload_sha256: [u8; 32], target_set: TargetSet, target_receipts: Vec<DeliveryTargetReceipt>, } @@ -228,6 +231,7 @@ impl DeliveryReceipt { ) -> Result<Self, Error> { Self::new( request.request_id.clone(), + exact_payload_sha256(request), request.target_set.clone(), target_receipts, ) @@ -235,6 +239,7 @@ impl DeliveryReceipt { fn new( request_id: DeliveryRequestId, + exact_payload_sha256: [u8; 32], target_set: TargetSet, target_receipts: Vec<DeliveryTargetReceipt>, ) -> Result<Self, Error> { @@ -263,16 +268,20 @@ impl DeliveryReceipt { } Ok(Self { request_id, + exact_payload_sha256, target_set, target_receipts: ordered, }) } - /// Validates the receipt against the exact request identity and targets. + /// Validates the original request identity, exact payload bytes and targets. pub fn validate_for_request(&self, request: &DeliveryRequest) -> Result<(), Error> { if &self.request_id != request.request_id() { return Err(Error::DeliveryReceiptRequestIdMismatch); } + if self.exact_payload_sha256 != exact_payload_sha256(request) { + return Err(Error::DeliveryReceiptPayloadMismatch); + } if self.target_set != *request.target_set() { return Err(Error::DeliveryReceiptTargetSetMismatch); } @@ -372,6 +381,7 @@ impl SinkFailure { ) -> Result<Self, Error> { let failure = Self { request_id: request.request_id.clone(), + exact_payload_sha256: exact_payload_sha256(request), target_set: request.target_set.clone(), code: code.into(), retryability, @@ -396,11 +406,14 @@ impl SinkFailure { .expect("static sink failure is valid") } - /// Validates identity, retry timing, and bounded partial evidence. + /// Validates identity, exact payload bytes, retry timing and partial evidence. pub fn validate_for_request(&self, request: &DeliveryRequest) -> Result<(), Error> { if self.request_id != *request.request_id() { return Err(Error::DeliveryReceiptRequestIdMismatch); } + if self.exact_payload_sha256 != exact_payload_sha256(request) { + return Err(Error::DeliveryReceiptPayloadMismatch); + } if self.target_set != *request.target_set() { return Err(Error::DeliveryReceiptTargetSetMismatch); } @@ -457,6 +470,10 @@ impl SinkFailure { } } +fn exact_payload_sha256(request: &DeliveryRequest) -> [u8; 32] { + Sha256::digest(request.payload().event().raw_json().as_bytes()).into() +} + /// Host SPI for outbound event delivery. /// /// This trait supports external implementations and is dyn-compatible. Its @@ -589,6 +606,7 @@ mod serde_impl { #[serde(deny_unknown_fields)] struct DeliveryReceiptWire { request_id: DeliveryRequestId, + exact_payload_sha256: [u8; 32], target_set: TargetSet, target_receipts: Vec<DeliveryTargetReceipt>, } @@ -599,8 +617,13 @@ mod serde_impl { D: serde::Deserializer<'de>, { let wire = DeliveryReceiptWire::deserialize(deserializer)?; - Self::new(wire.request_id, wire.target_set, wire.target_receipts) - .map_err(serde::de::Error::custom) + Self::new( + wire.request_id, + wire.exact_payload_sha256, + wire.target_set, + wire.target_receipts, + ) + .map_err(serde::de::Error::custom) } } @@ -608,6 +631,7 @@ mod serde_impl { #[serde(deny_unknown_fields)] struct SinkFailureWire { request_id: DeliveryRequestId, + exact_payload_sha256: [u8; 32], target_set: TargetSet, code: String, retryability: Retryability, @@ -624,6 +648,7 @@ mod serde_impl { let wire = SinkFailureWire::deserialize(deserializer)?; let failure = Self { request_id: wire.request_id, + exact_payload_sha256: wire.exact_payload_sha256, target_set: wire.target_set, code: wire.code, retryability: wire.retryability, diff --git a/crates/transport/src/target.rs b/crates/transport/src/target.rs @@ -434,6 +434,21 @@ impl Target { &self.uri } + /// Compares a captured Nostr relay identifier using this target's original + /// construction policy and canonical endpoint rules. Scope and labels are + /// separate. Matching supplies no route approval or network permission. + pub fn matches_nostr_relay(&self, raw: &str) -> Result<bool, TransportError> { + if self.kind != TransportId::NOSTR { + return Ok(false); + } + let policy = if self.private_device_cleartext { + TargetNetworkPolicy::PrivateDevice + } else { + TargetNetworkPolicy::TlsOrLoopback + }; + Ok(EndpointUri::parse_nostr_relay(raw, policy)? == self.uri) + } + pub fn scope(&self) -> Option<&TargetScope> { self.scope.as_ref() } diff --git a/crates/transport/tests/delivery_contract.rs b/crates/transport/tests/delivery_contract.rs @@ -55,6 +55,112 @@ fn mixed_receipt(request: &DeliveryRequest) -> DeliveryReceipt { .expect("mixed receipt") } +fn different_payload_with_same_request_scope(original: &DeliveryRequest) -> DeliveryRequest { + let mut wire = Nip01EventWire::parse_json(original.payload().event().raw_json()).unwrap(); + wire.content = "different exact event".into(); + wire.id = wire.computed_event_id().unwrap().to_string(); + let raw = serde_json::to_string(&wire).unwrap(); + let event = SignedEvent::from_wire_verified_id(wire, raw).unwrap(); + DeliveryRequest::new( + original.request_id().as_str(), + DeliveryPayload::new(event), + original.target_set().clone(), + original.satisfaction().clone(), + original.deadline_unix_ms(), + ) + .unwrap() +} + +#[test] +fn receipt_scope_includes_the_exact_payload_even_when_request_id_and_targets_match() { + let original = request(SatisfactionPolicy::new( + SatisfactionClass::Accepted, + TargetPolicy::all(), + )); + let different = different_payload_with_same_request_scope(&original); + let receipt = mixed_receipt(&original); + receipt.validate_for_request(&original).unwrap(); + assert!(receipt.validate_for_request(&different).is_err()); +} + +#[test] +fn partial_failure_scope_includes_the_exact_payload() { + let original = request(SatisfactionPolicy::new( + SatisfactionClass::Accepted, + TargetPolicy::all(), + )); + let different = different_payload_with_same_request_scope(&original); + let failure = SinkFailure::for_request( + &original, + "partial", + Retryability::Retryable, + None, + None, + vec![DeliveryTargetReceipt::attempted( + original.target_set().targets()[0].clone(), + DeliveryOutcome::accepted(), + )], + ) + .unwrap(); + failure.validate_for_request(&original).unwrap(); + assert!(failure.validate_for_request(&different).is_err()); +} + +#[test] +fn receipt_scope_preserves_exact_representation_and_serialized_binding() { + let original = request(SatisfactionPolicy::new( + SatisfactionClass::Accepted, + TargetPolicy::all(), + )); + let raw = format!(" \n{}\n", original.payload().event().raw_json()); + let wire = Nip01EventWire::parse_json(&raw).unwrap(); + let event = SignedEvent::from_wire_verified_id(wire, raw).unwrap(); + let changed = DeliveryRequest::new( + original.request_id().as_str(), + DeliveryPayload::new(event), + original.target_set().clone(), + original.satisfaction().clone(), + original.deadline_unix_ms(), + ) + .unwrap(); + assert_eq!( + original.payload().event().envelope().id(), + changed.payload().event().envelope().id() + ); + let receipt = mixed_receipt(&original); + assert_eq!( + receipt.validate_for_request(&changed), + Err(Error::DeliveryReceiptPayloadMismatch) + ); + let failure = SinkFailure::transport_closed(&original); + assert_eq!( + failure.validate_for_request(&changed), + Err(Error::DeliveryReceiptPayloadMismatch) + ); + #[cfg(feature = "serde")] + { + let mut value = serde_json::to_value(&receipt).unwrap(); + value + .as_object_mut() + .unwrap() + .remove("exact_payload_sha256"); + assert!(serde_json::from_value::<DeliveryReceipt>(value).is_err()); + let mut digest_only = serde_json::to_value(&original).unwrap(); + digest_only.as_object_mut().unwrap().remove("payload"); + digest_only["exact_payload_sha256"] = + serde_json::to_value(&receipt).unwrap()["exact_payload_sha256"].clone(); + assert!(serde_json::from_value::<DeliveryRequest>(digest_only).is_err()); + let mut value = serde_json::to_value(&failure).unwrap(); + let original_byte = value["exact_payload_sha256"][0].as_u64().unwrap(); + value["exact_payload_sha256"][0] = serde_json::json!(original_byte ^ 1); + let altered: SinkFailure = serde_json::from_value(value).unwrap(); + assert_eq!( + altered.validate_for_request(&original), + Err(Error::DeliveryReceiptPayloadMismatch) + ); + } +} + #[test] fn target_selection_is_exact_and_default_adapter_never_widens_it() { use radroots_transport::{BoxFuture, EventSink, SinkStatus}; diff --git a/crates/transport/tests/target_contract.rs b/crates/transport/tests/target_contract.rs @@ -11,6 +11,49 @@ use radroots_transport::{ use serde_json::Value; #[test] +fn captured_relay_matching_preserves_canonical_identity_and_original_network_policy() { + let target = Target::nostr_relay("wss://relay.example").unwrap(); + for raw in [ + "wss://relay.example/", + "WSS://RELAY.EXAMPLE:443/", + "wss://relay.example:443", + ] { + assert!(target.matches_nostr_relay(raw).unwrap()); + } + for raw in [ + "wss://other.example/", + "wss://relay.example:444/", + "wss://relay.example/path", + ] { + assert!(!target.matches_nostr_relay(raw).unwrap()); + } + for raw in [ + "ws://relay.example/", + "wss://relay.example/?q=1", + "wss://relay.example/#fragment", + "wss://user@relay.example/", + ] { + assert!(target.matches_nostr_relay(raw).is_err()); + } + let private = + Target::nostr_relay_with_policy("ws://192.168.1.2", TargetNetworkPolicy::PrivateDevice) + .unwrap(); + assert!(private.matches_nostr_relay("WS://192.168.1.2:80/").unwrap()); + assert!(private.matches_nostr_relay("ws://public.example/").is_err()); + let ipv6 = Target::nostr_relay("wss://[2001:db8::1]").unwrap(); + assert!( + ipv6.matches_nostr_relay("wss://[2001:0DB8:0:0:0:0:0:1]:443/") + .unwrap() + ); + assert!( + !Target::local("local:preview") + .unwrap() + .matches_nostr_relay("wss://relay.example/") + .unwrap() + ); +} + +#[test] fn endpoint_scope_and_label_values_are_canonical_and_bounded() { let endpoint = EndpointUri::from_str("MESH://Node.Example/path").expect("endpoint"); assert_eq!(endpoint.as_ref(), "mesh://node.example/path");