commit 2a42f64be23017c16b80f30ef1e02982c281f03b parent 14f1a9e64e4f8173004f8c95605ffeffe89a5cf7 Author: triesap <tyson@radroots.org> Date: Mon, 26 Oct 2026 07:36:03 +0000 execution: reconcile retained stock evidence on the same owner - Retain original material, credentials and bounded ownership after failure. - Reconcile uncertain native connections without releasing writer authority. - Preserve actual errors and drain host tasks before capture and disposal. - Verify SQLite faults, observer loss, generic recovery and portable APIs. Diffstat:
28 files changed, 1577 insertions(+), 77 deletions(-)
diff --git a/contracts/api_baselines/radroots.txt b/contracts/api_baselines/radroots.txt @@ -177,10 +177,15 @@ pub use radroots::stock::InventoryOutcome pub use radroots::stock::InventoryReceipt pub use radroots::stock::MapOptionInput pub use radroots::stock::PoolCorrectionInput +pub use radroots::stock::StockEvidenceOutcome +pub use radroots::stock::StockEvidenceReceipt +pub use radroots::stock::StockEvidenceRequest pub struct radroots::stock::Stock<'a> impl<'a> radroots::stock::Stock<'a> pub async fn radroots::stock::Stock<'a>::adjust(&self, radroots_execution::workspace::CommandContext, radroots_storage::inventory::InventoryCheckpointRef, radroots_protocol::command::stock_correction::PoolCorrectionInput) -> radroots_execution::Result<radroots_storage::inventory::InventoryOutcome> +pub async fn radroots::stock::Stock<'a>::capture_evidence(&self, &radroots_storage::inventory::evidence::StockEvidenceRequest) -> radroots_execution::Result<radroots_storage::inventory::evidence::StockEvidenceOutcome> pub async fn radroots::stock::Stock<'a>::create_pool(&self, radroots_execution::workspace::CommandContext, radroots_storage::inventory::InventoryCheckpointRef, radroots_protocol::command::stock::CreatePoolInput) -> radroots_execution::Result<radroots_storage::inventory::InventoryOutcome> +pub async fn radroots::stock::Stock<'a>::drain_evidence(&self) -> radroots_execution::Result<()> pub async fn radroots::stock::Stock<'a>::map_option(&self, radroots_execution::workspace::CommandContext, radroots_storage::inventory::InventoryCheckpointRef, radroots_protocol::command::stock_mapping::MapOptionInput) -> radroots_execution::Result<radroots_storage::inventory::InventoryOutcome> pub async fn radroots::stock::Stock<'a>::operation(&self, radroots_storage::inventory::InventoryCheckpointRef, radroots_domain::identifier::OperationId, core::option::Option<radroots_domain::identifier::CommandDigest>) -> radroots_execution::Result<core::option::Option<radroots_storage::inventory::InventoryReceipt>> pub async fn radroots::stock::Stock<'a>::option_mapping(&self, radroots_storage::inventory::InventoryCheckpointRef, radroots_domain::stock::mapping::OptionMappingKey) -> radroots_execution::Result<core::option::Option<radroots_domain::stock::mapping::OptionPoolMapping>> diff --git a/contracts/api_baselines/radroots_execution.txt b/contracts/api_baselines/radroots_execution.txt @@ -36,8 +36,10 @@ pub radroots_execution::session::StartMode::Online pub struct radroots_execution::session::ClientSession<H: radroots_execution::session::SessionHost> impl<H: radroots_execution::session::SessionHost + 'static> radroots_execution::session::ClientSession<H> pub async fn radroots_execution::session::ClientSession<H>::abort_proven_unissued(&self, alloc::sync::Arc<dyn radroots_storage::inventory::InventoryStore>, &radroots_execution::workspace::WorkspaceSession, radroots_storage::inventory::InventoryCheckpointRef, radroots_domain::identifier::OperationId, radroots_domain::identifier::CommandDigest) -> radroots_execution::Result<radroots_storage::inventory::abort::StockAbortOutcome> +pub async fn radroots_execution::session::ClientSession<H>::capture_stock_evidence(&self, alloc::sync::Arc<dyn radroots_storage::inventory::InventoryStore>, &radroots_execution::workspace::WorkspaceSession, &radroots_storage::inventory::evidence::StockEvidenceRequest) -> radroots_execution::Result<radroots_storage::inventory::evidence::StockEvidenceOutcome> pub async fn radroots_execution::session::ClientSession<H>::correct_stock_pool(&self, alloc::sync::Arc<dyn radroots_storage::inventory::InventoryStore>, &radroots_execution::workspace::WorkspaceSession, radroots_execution::workspace::CommandContext, radroots_storage::inventory::InventoryCheckpointRef, radroots_protocol::command::stock_correction::PoolCorrectionInput) -> radroots_execution::Result<radroots_storage::inventory::InventoryOutcome> pub async fn radroots_execution::session::ClientSession<H>::create_stock_pool(&self, alloc::sync::Arc<dyn radroots_storage::inventory::InventoryStore>, &radroots_execution::workspace::WorkspaceSession, radroots_execution::workspace::CommandContext, radroots_storage::inventory::InventoryCheckpointRef, radroots_protocol::command::stock::CreatePoolInput) -> radroots_execution::Result<radroots_storage::inventory::InventoryOutcome> +pub async fn radroots_execution::session::ClientSession<H>::drain_stock_evidence(&self) -> radroots_execution::Result<()> pub async fn radroots_execution::session::ClientSession<H>::inventory_operation(&self, alloc::sync::Arc<dyn radroots_storage::inventory::InventoryStore>, &radroots_execution::workspace::WorkspaceSession, radroots_storage::inventory::InventoryCheckpointRef, radroots_domain::identifier::OperationId, core::option::Option<radroots_domain::identifier::CommandDigest>) -> radroots_execution::Result<core::option::Option<radroots_storage::inventory::InventoryReceipt>> pub async fn radroots_execution::session::ClientSession<H>::map_stock_option(&self, alloc::sync::Arc<dyn radroots_storage::inventory::InventoryStore>, &radroots_execution::workspace::WorkspaceSession, radroots_execution::workspace::CommandContext, radroots_storage::inventory::InventoryCheckpointRef, radroots_protocol::command::stock_mapping::MapOptionInput) -> radroots_execution::Result<radroots_storage::inventory::InventoryOutcome> pub async fn radroots_execution::session::ClientSession<H>::prepare_stock_acceptance(&self, alloc::sync::Arc<dyn radroots_storage::inventory::InventoryStore>, &radroots_execution::workspace::WorkspaceSession, radroots_execution::workspace::CommandContext, radroots_storage::inventory::InventoryCheckpointRef, radroots_storage::workspace::seller_stock::source::StockSourceCheckpointRef, radroots_storage::workspace::seller_stock::source::StockListingCheckpointRef, radroots_protocol::command::order_acceptance::AcceptOrderInput) -> radroots_execution::Result<radroots_storage::inventory::acceptance::StockAcceptanceOutcome> diff --git a/contracts/api_baselines/radroots_storage.txt b/contracts/api_baselines/radroots_storage.txt @@ -1471,6 +1471,7 @@ impl radroots_storage::inventory::StockEvidenceRequest pub fn radroots_storage::inventory::StockEvidenceRequest::attempt(&self) -> radroots_storage::command::CommitAttempt pub const fn radroots_storage::inventory::StockEvidenceRequest::binding(&self) -> &radroots_storage::workspace::WorkspaceBinding pub const fn radroots_storage::inventory::StockEvidenceRequest::command_digest(&self) -> radroots_domain::identifier::CommandDigest +pub fn radroots_storage::inventory::StockEvidenceRequest::evidence_digest(&self) -> [u8; 32] pub fn radroots_storage::inventory::StockEvidenceRequest::from_obtained_wire(radroots_storage::workspace::WorkspaceBinding, radroots_domain::identifier::OperationId, radroots_domain::identifier::CommandDigest, radroots_domain::identifier::SigningAttemptId, radroots_identity::key::PublicKey, [u8; 32], &str, core::option::Option<core::num::nonzero::NonZeroU64>) -> core::result::Result<Self, radroots_storage::command::OperationError> pub const fn radroots_storage::inventory::StockEvidenceRequest::intent_digest(&self) -> &[u8; 32] pub const fn radroots_storage::inventory::StockEvidenceRequest::observed_at_unix_ms(&self) -> core::option::Option<core::num::nonzero::NonZeroU64> @@ -1489,6 +1490,7 @@ pub fn radroots_storage::inventory::InventoryStore::inventory_operation(&self, r pub fn radroots_storage::inventory::InventoryStore::option_mapping(&self, radroots_storage::workspace::WorkspaceBinding, radroots_storage::inventory::InventoryCheckpointRef, radroots_domain::stock::mapping::OptionMappingKey, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_domain::stock::mapping::OptionPoolMapping>, radroots_storage::command::OperationError>> pub fn radroots_storage::inventory::InventoryStore::pool(&self, radroots_storage::workspace::WorkspaceBinding, radroots_storage::inventory::InventoryCheckpointRef, radroots_domain::identifier::PoolId, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_domain::stock::PoolView>, radroots_storage::command::OperationError>> pub fn radroots_storage::inventory::InventoryStore::prepare_stock_acceptance(&self, radroots_storage::inventory::StockAcceptanceRequest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::inventory::StockAcceptanceOutcome, radroots_storage::command::OperationError>> +pub fn radroots_storage::inventory::InventoryStore::reconcile_stock_evidence<'a>(&'a self, &'a radroots_storage::inventory::StockEvidenceRequest, &'a radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'a, core::result::Result<radroots_storage::inventory::StockEvidenceOutcome, radroots_storage::command::OperationError>> pub fn radroots_storage::inventory::InventoryStore::stock_preparation_abort(&self, radroots_storage::inventory::StockAbortRequest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::inventory::StockAbortReceipt>, radroots_storage::command::OperationError>> pub mod radroots_storage::journal pub use radroots_storage::journal::BoxFuture diff --git a/contracts/api_baselines/radroots_storage_sqlite.txt b/contracts/api_baselines/radroots_storage_sqlite.txt @@ -484,6 +484,7 @@ pub fn radroots_storage_sqlite::workspace::NativeWorkspace::inventory_operation( pub fn radroots_storage_sqlite::workspace::NativeWorkspace::option_mapping(&self, radroots_storage::workspace::WorkspaceBinding, radroots_storage::inventory::InventoryCheckpointRef, radroots_domain::stock::mapping::OptionMappingKey, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_domain::stock::mapping::OptionPoolMapping>, radroots_storage::command::OperationError>> pub fn radroots_storage_sqlite::workspace::NativeWorkspace::pool(&self, radroots_storage::workspace::WorkspaceBinding, radroots_storage::inventory::InventoryCheckpointRef, radroots_domain::identifier::PoolId, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_domain::stock::PoolView>, radroots_storage::command::OperationError>> pub fn radroots_storage_sqlite::workspace::NativeWorkspace::prepare_stock_acceptance(&self, radroots_storage::inventory::acceptance::StockAcceptanceRequest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::inventory::acceptance::StockAcceptanceOutcome, radroots_storage::command::OperationError>> +pub fn radroots_storage_sqlite::workspace::NativeWorkspace::reconcile_stock_evidence<'a>(&'a self, &'a radroots_storage::inventory::evidence::StockEvidenceRequest, &'a radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'a, core::result::Result<radroots_storage::inventory::evidence::StockEvidenceOutcome, radroots_storage::command::OperationError>> pub fn radroots_storage_sqlite::workspace::NativeWorkspace::stock_preparation_abort(&self, radroots_storage::inventory::abort::StockAbortRequest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::inventory::abort::StockAbortReceipt>, radroots_storage::command::OperationError>> impl radroots_storage::workspace::WorkspaceStore for radroots_storage_sqlite::workspace::NativeWorkspace pub fn radroots_storage_sqlite::workspace::NativeWorkspace::authenticate(&self, radroots_storage::workspace::AttachmentInput, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::workspace::WorkspaceBinding, radroots_storage::workspace::WorkspaceError>> diff --git a/contracts/architecture/decisions/shared_sdk_refactor.v1.json b/contracts/architecture/decisions/shared_sdk_refactor.v1.json @@ -256,6 +256,11 @@ }, "signing_issuance": { "shared_capture_port": "InventoryStore.capture_stock_evidence", + "shared_reconciliation_port": "InventoryStore.reconcile_stock_evidence", + "reconciliation_retains_writer_through_replacement": true, + "reconciliation_authenticates_before_connection_close": true, + "reconciliation_preflight_initializes_or_migrates": false, + "reconciliation_clears_fence_only_after_confirmed_capture": true, "shared_capture_model_owner": "radroots_storage.inventory", "shared_capture_descriptors_are_authority": false, "shared_capture_reauthenticates_original": true, @@ -806,6 +811,15 @@ }, "execution": { "ordinary_inventory_owner": { + "owned_capture_method": "capture_stock_evidence", + "capture_drain_uses_explicit_reconciliation": true, + "retained_capture_survives_observer_loss": true, + "failed_capture_blocks_fresh_work": true, + "failed_capture_blocks_disposal": true, + "capture_preserves_actual_error_code": true, + "pending_capture_retry": "manual_reconciliation", + "capture_reports_must_match_original": true, + "capture_scope_metadata_renews_issuance": false, "owned_acceptance_method": "prepare_stock_acceptance", "acceptance_is_preparation_only": true, "lost_accepted_admission_is_unknown": true, diff --git a/crates/execution/README.md b/crates/execution/README.md @@ -175,3 +175,24 @@ bound obtained bytes and nullable observation survive unchanged. The composing executor must still own the complete task and durable capture, authenticate the original store claim and enforce retained replay policy. This scope grants no stock authority, renewed deadline or proof of original nonissuance. + +`ClientSession::capture_stock_evidence` owns a bounded copy of borrowed original +evidence through the selected shared port. Accepted tasks outlive their caller's +wait. Errors, unknown appends and mismatched receipts retain exact bytes, original +credentials and resource/byte leases. A real storage failure keeps its error code +and requires manual reconciliation. `drain_stock_evidence` retries that same +material; it performs no signing, T0, installation or delivery. Close drains the +owner before callback/key/storage disposal and remains resumable on failure. +Fresh inventory preparation/mutation and new command first-signature calls +refuse while material remains unpersisted; independent reads remain available. +Borrowed originals survive unsupported/refused hosts. Scope checks preserve the +immutable namespace and actor; host observation metadata supplies no permission +or renewed issuance window. Matching host reports remain a trust boundary, not +cryptographic transaction proofs. Full ordinary commerce orchestration is a +separate requirement. + +Capture drain calls explicit `InventoryStore::reconcile_stock_evidence`; a blind +append retry cannot resolve a concrete host's uncertain connection. The qualified +native host retains writer ownership while authenticating and reopening that +same namespace. Core keeps the original material and leases until a matching +confirmed result, including through a refused preflight or lost observer. diff --git a/crates/execution/src/error.rs b/crates/execution/src/error.rs @@ -208,6 +208,18 @@ impl Error { } } + /// A failed obtained-material append preserves its actual error code while + /// requiring explicit reconciliation of the already retained original. + #[cfg(feature = "workspace_session")] + pub(crate) fn capture_pending( + error: radroots_storage::command::OperationError, + context: OperationContext, + ) -> Self { + let mut value = Self::operation(error, context); + value.retry = RetryClass::ManualReconciliation; + value + } + #[cfg(feature = "workspace_session")] /// Projects a typed journal refusal with its observed admission context. /// This data conversion performs no transaction or authorization check. diff --git a/crates/execution/src/session/client.rs b/crates/execution/src/session/client.rs @@ -19,7 +19,10 @@ struct Resources<H> { signer: Shared<Mutex<Option<SignerSession>>>, #[cfg(feature = "workspace_session")] workspaces: WorkspaceScopes, + #[cfg(feature = "workspace_session")] + stock_captures: Shared<super::stock_evidence::Owner>, host_quiesced: Mutex<bool>, + host_drained: Mutex<bool>, } #[cfg(feature = "workspace_session")] type WorkspaceScopes = Shared<Mutex<std::vec::Vec<crate::workspace::WorkspaceSession>>>; @@ -68,7 +71,24 @@ impl<H: SessionHost> SessionHost for Resources<H> { self.host.disconnect() } fn drain(&self) -> HostFuture<'_, crate::Result<()>> { - self.host.drain() + Box::pin(async move { + let drained = *self + .host_drained + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + if !drained { + // The host may drive its accepted futures here. They must reach + // settlement before retrying captures whose locks they retain. + self.host.drain().await?; + *self + .host_drained + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) = true; + } + #[cfg(feature = "workspace_session")] + self.stock_captures.drain().await?; + Ok(()) + }) } fn dispose_callbacks(&self) -> HostFuture<'_, crate::Result<()>> { Box::pin(async move { @@ -99,6 +119,8 @@ pub struct ClientSession<H: SessionHost> { pub(crate) task_budget: super::tasks::TaskBudget, #[cfg(feature = "workspace_session")] pub(crate) commands: Option<Shared<super::command::CommandRuntime>>, + #[cfg(feature = "workspace_session")] + stock_captures: Shared<super::stock_evidence::Owner>, } impl<H: SessionHost> Clone for ClientSession<H> { fn clone(&self) -> Self { @@ -113,6 +135,8 @@ impl<H: SessionHost> Clone for ClientSession<H> { task_budget: self.task_budget.clone(), #[cfg(feature = "workspace_session")] commands: self.commands.clone(), + #[cfg(feature = "workspace_session")] + stock_captures: self.stock_captures.clone(), } } } @@ -122,6 +146,8 @@ impl<H: SessionHost> ClientSession<H> { let signer = Shared::new(Mutex::new(None)); #[cfg(feature = "workspace_session")] let workspaces = Shared::new(Mutex::new(std::vec::Vec::new())); + #[cfg(feature = "workspace_session")] + let stock_captures = Shared::new(super::stock_evidence::Owner::default()); Self { session: Session::new( Resources { @@ -129,7 +155,10 @@ impl<H: SessionHost> ClientSession<H> { signer: signer.clone(), #[cfg(feature = "workspace_session")] workspaces: workspaces.clone(), + #[cfg(feature = "workspace_session")] + stock_captures: stock_captures.clone(), host_quiesced: Mutex::new(false), + host_drained: Mutex::new(false), }, maximum_in_flight, ), @@ -142,6 +171,8 @@ impl<H: SessionHost> ClientSession<H> { task_budget: super::tasks::TaskBudget::new(), #[cfg(feature = "workspace_session")] commands: None, + #[cfg(feature = "workspace_session")] + stock_captures, } } /// Pairs an explicit target task host with its owning lifecycle host. The diff --git a/crates/execution/src/session/client_commands.rs b/crates/execution/src/session/client_commands.rs @@ -1,5 +1,8 @@ use super::{ClientSession, SessionHost, Shared}; -use crate::session::{HostBound, command::CommandRuntime}; +use crate::session::{ + HostBound, + command::{CommandRuntime, SigningResources}, +}; use crate::{ Error, OperationContext, OperationReceipt, OperationStatus, workspace::{CommandContext, WorkspaceSession}, @@ -58,6 +61,7 @@ impl<H: SessionHost + 'static> ClientSession<H> { .as_ref() .ok_or_else(|| Error::operation(OperationError::Unauthorized, proposed))?; let signer = self.signer_session(); + let stock_captures = self.stock_captures.clone(); let lease: Shared<dyn HostBound> = Shared::new((permit, budget)); let (reply, observed) = futures::channel::oneshot::channel(); host.submit(Box::pin(async move { @@ -89,8 +93,11 @@ impl<H: SessionHost + 'static> ClientSession<H> { journal.clone(), prepared, &proof, - signer, - lease, + SigningResources { + signer, + lease, + stock_captures, + }, crate::session::command::RunMode::Resume, ) .await @@ -160,6 +167,9 @@ impl<H: SessionHost + 'static> ClientSession<H> { { let operation = context.data().operation_id(); let proposed = OperationContext::proposed(Some(operation)); + self.stock_captures + .ensure_ready() + .map_err(|error| Error::operation(error, proposed))?; let permit = self .session .admit() @@ -191,12 +201,24 @@ impl<H: SessionHost + 'static> ClientSession<H> { .map_err(|error| Error::operation(error, proposed))?; let runtime = self.commands.clone(); let signer = self.signer_session(); + let stock_captures = self.stock_captures.clone(); let lease: Shared<dyn HostBound> = Shared::new((permit, budget)); if let Some(host) = &self.task_host { let (reply, observed) = futures::channel::oneshot::channel(); let task = Box::pin(async move { - let result = - execute_command(journal, request, proof, runtime, signer, lease, project).await; + let result = execute_command( + journal, + request, + proof, + runtime, + SigningResources { + signer, + lease, + stock_captures, + }, + project, + ) + .await; let _ = reply.send(result); }); host.submit(task) @@ -205,7 +227,19 @@ impl<H: SessionHost + 'static> ClientSession<H> { .await .unwrap_or_else(|_| Err(Error::admission_uncertain(operation))) } else { - execute_command(journal, request, proof, runtime, signer, lease, project).await + execute_command( + journal, + request, + proof, + runtime, + SigningResources { + signer, + lease, + stock_captures, + }, + project, + ) + .await } } } @@ -255,12 +289,15 @@ async fn execute_command<T: HostBound + 'static>( request: AdmissionRequest, proof: radroots_storage::workspace::AttachmentProof, runtime: Option<Shared<CommandRuntime>>, - signer: Option<super::SignerSession>, - lease: Shared<dyn HostBound>, + resources: SigningResources, project: ReceiptProjection<T>, ) -> crate::Result<OperationReceipt<T>> { let operation = request.command().context().operation_id(); let proposed = OperationContext::proposed(Some(operation)); + resources + .stock_captures + .ensure_ready() + .map_err(|error| Error::operation(error, proposed))?; let binding = request.binding().clone(); let digest = request.command().digest(); // Original lookup precedes fresh deadline/planning. Authentication of the @@ -284,8 +321,7 @@ async fn execute_command<T: HostBound + 'static>( journal.clone(), prepared, &query_proof, - signer, - lease, + resources, crate::session::command::RunMode::Command, ) .await diff --git a/crates/execution/src/session/client_inventory.rs b/crates/execution/src/session/client_inventory.rs @@ -20,12 +20,92 @@ use radroots_storage::{ inventory::{ InventoryCheckpointRef, InventoryOutcome, InventoryReceipt, InventoryRequest, InventoryStore, StockAbortOutcome, StockAbortReceipt, StockAbortRequest, - StockAcceptanceOutcome, StockAcceptanceRequest, + StockAcceptanceOutcome, StockAcceptanceRequest, StockEvidenceOutcome, StockEvidenceRequest, }, workspace::{AttachmentProof, WorkspaceBinding}, }; impl<H: SessionHost + 'static> ClientSession<H> { + /// Owns already obtained original material until a matching durable append. + /// The borrowed original survives preflight/host refusal. Accepted work and + /// failed captures retain bytes, credentials and resource/byte leases after + /// observer loss. This signs nothing and supplies no installation right. + pub async fn capture_stock_evidence( + &self, + store: Shared<dyn InventoryStore>, + scope: &WorkspaceSession, + request: &StockEvidenceRequest, + ) -> crate::Result<StockEvidenceOutcome> { + let proposed = OperationContext::proposed(Some(request.operation_id())); + let permit = self + .session + .admit() + .map_err(|error| error.with_context(proposed))?; + let budget = self + .task_budget + .reserve() + .map_err(|error| error.with_context(proposed))?; + { + let scopes = self + .workspaces + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + if !scopes.iter().any(|owned| owned.same_scope(scope)) { + return Err(Error::operation(OperationError::Unauthorized, proposed)); + } + } + let host = self + .task_host + .as_ref() + .ok_or_else(|| Error::operation(OperationError::UnsupportedOperation, proposed))?; + let (binding, proof) = scope + .captured_input() + .map_err(|error| Error::operation(error.into(), proposed))?; + if !radroots_storage::workspace::seller_stock::SellerAuthoritySelection::from_attachment( + binding.input(), + ) + .matches_attachment(request.binding().input()) + || binding.scope() != request.binding().scope() + || binding.actor() != request.binding().actor() + { + return Err(Error::operation(OperationError::Unauthorized, proposed)); + } + // Refresh only non-authorizing host observations. Immutable namespace, + // original actor/selectors and exact obtained bytes cannot be rebound. + let retained = request + .with_json(|raw| { + StockEvidenceRequest::from_obtained_wire( + binding, + request.operation_id(), + request.command_digest(), + request.signing_attempt_id(), + request.recipient(), + *request.intent_digest(), + raw, + request.observed_at_unix_ms(), + ) + }) + .map_err(|error| Error::operation(error, proposed))?; + let owner = self.stock_captures.clone(); + let lease: Shared<dyn HostBound> = Shared::new((permit, budget)); + let (reply, observed) = futures::channel::oneshot::channel(); + host.submit(Box::pin(async move { + let entry = owner.retain(store, retained, proof, lease); + let result = owner.capture(&entry).await; + let _ = reply.send(result); + })) + .map_err(|error| error.with_context(proposed))?; + observed + .await + .unwrap_or_else(|_| Err(Error::operation(OperationError::RecoveryRequired, proposed))) + } + + /// Retries only retained original captures. This remains available during + /// closing; unresolved material keeps callbacks, keys and storage owned. + pub async fn drain_stock_evidence(&self) -> crate::Result<()> { + self.stock_captures.drain().await + } + /// Captures original acceptance and owns its T0 admission until settlement. /// A preparation report claims no signature, installed agreement or delivery. /// Concrete store authentication/consent and actual atomic effects remain @@ -43,9 +123,13 @@ impl<H: SessionHost + 'static> ClientSession<H> { ) -> crate::Result<StockAcceptanceOutcome> { let operation = context.data().operation_id(); let proposed = OperationContext::proposed(Some(operation)); + self.stock_captures + .ensure_ready() + .map_err(|error| Error::operation(error, proposed))?; if !scope.owns_context(&context) { return Err(Error::operation(OperationError::Unauthorized, proposed)); } + let stock_captures = self.stock_captures.clone(); self.run_stock_with_loss( scope, proposed, @@ -60,7 +144,10 @@ impl<H: SessionHost + 'static> ClientSession<H> { binding, minimum, sources, listings, &command, ) }); - Box::pin(async move { store.prepare_stock_acceptance(request?, proof).await }) + Box::pin(async move { + stock_captures.ensure_ready()?; + store.prepare_stock_acceptance(request?, proof).await + }) }, ) .await @@ -126,9 +213,13 @@ impl<H: SessionHost + 'static> ClientSession<H> { >, ) -> crate::Result<InventoryOutcome> { let proposed = OperationContext::proposed(Some(context.data().operation_id())); + self.stock_captures + .ensure_ready() + .map_err(|error| Error::operation(error, proposed))?; if !scope.owns_context(&context) { return Err(Error::operation(OperationError::Unauthorized, proposed)); } + let stock_captures = self.stock_captures.clone(); self.run_stock_with_loss( scope, proposed, @@ -139,7 +230,10 @@ impl<H: SessionHost + 'static> ClientSession<H> { let request = capture(context.data().clone()) .map_err(|_| OperationError::InvalidCommand) .and_then(|command| InventoryRequest::new(binding, minimum, command)); - Box::pin(async move { store.execute_inventory(request?, proof).await }) + Box::pin(async move { + stock_captures.ensure_ready()?; + store.execute_inventory(request?, proof).await + }) }, ) .await diff --git a/crates/execution/src/session/command.rs b/crates/execution/src/session/command.rs @@ -98,6 +98,11 @@ pub struct CommandRuntime { observations: Mutex<Vec<Shared<PendingObservation>>>, draining: futures::lock::Mutex<()>, } +pub(super) struct SigningResources { + pub(super) signer: Option<SignerSession>, + pub(super) lease: Shared<dyn HostBound>, + pub(super) stock_captures: Shared<super::stock_evidence::Owner>, +} pub(super) enum RunMode { Command, Resume, @@ -201,14 +206,15 @@ impl CommandRuntime { journal: Shared<dyn OperationJournal>, prepared: PreparedOperation, proof: &AttachmentProof, - signer: Option<SignerSession>, - lease: Shared<dyn HostBound>, + resources: SigningResources, mode: RunMode, ) -> Result<(), OperationError> { if !self.io.enabled() { return Ok(()); } self.drain().await?; + resources.stock_captures.ensure_ready()?; + let signer = &resources.signer; let binding = prepared.request().binding().clone(); let operation = prepared.identity().operation_id(); let digest = prepared.identity().command_digest(); @@ -234,7 +240,8 @@ impl CommandRuntime { &bound, proof, signer.as_ref(), - lease.clone(), + resources.lease.clone(), + &resources.stock_captures, ) .await?; let snapshot = journal @@ -258,7 +265,8 @@ impl CommandRuntime { ) .await?; } - self.deliver(journal, &bound, proof, lease, mode).await + self.deliver(journal, &bound, proof, resources.lease, mode) + .await } async fn plan( &self, @@ -363,6 +371,7 @@ impl CommandRuntime { proof: &AttachmentProof, signer: Option<&SignerSession>, lease: Shared<dyn HostBound>, + stock_captures: &super::stock_evidence::Owner, ) -> Result<(), OperationError> { let prepared = bound.prepared(); let binding = prepared.request().binding(); @@ -402,6 +411,7 @@ impl CommandRuntime { { continue; } + stock_captures.ensure_ready()?; let attempt = match journal.claim_attempt(select(), copy(proof)?).await { Err(OperationError::DeadlineExceeded) => continue, Err(error) => return Err(error), @@ -429,6 +439,7 @@ impl CommandRuntime { if intent.binding() != attempt.binding() { return Err(OperationError::RecoveryRequired); } + stock_captures.ensure_ready()?; let outcome = signer.sign_exact(intent).await; let (_, kind) = outcome.into_parts(); if let OutcomeKind::Obtained(artifact) = kind { diff --git a/crates/execution/src/session/mod.rs b/crates/execution/src/session/mod.rs @@ -10,6 +10,9 @@ pub mod tasks; #[cfg(feature = "workspace_session")] pub mod command; +#[cfg(feature = "workspace_session")] +mod stock_evidence; + #[cfg(feature = "client_session")] mod client; diff --git a/crates/execution/src/session/stock_evidence.rs b/crates/execution/src/session/stock_evidence.rs @@ -0,0 +1,160 @@ +//! Retention of original obtained stock material across uncertain persistence. +use super::{HostBound, Shared}; +use crate::{Error, OperationContext}; +use radroots_storage::{ + command::OperationError, + inventory::{InventoryStore, StockEvidenceOutcome, StockEvidenceReceipt, StockEvidenceRequest}, + workspace::AttachmentProof, +}; +use std::{sync::Mutex, vec::Vec}; + +pub(super) struct Pending { + store: Shared<dyn InventoryStore>, + request: StockEvidenceRequest, + proof: AttachmentProof, + receipt: Mutex<Option<StockEvidenceReceipt>>, + _lease: Shared<dyn HostBound>, +} +#[derive(Default)] +pub(super) struct Owner { + pending: Mutex<Vec<Shared<Pending>>>, + serial: futures::lock::Mutex<()>, +} +impl Owner { + pub(super) fn ensure_ready(&self) -> Result<(), OperationError> { + if self + .pending + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .is_empty() + { + Ok(()) + } else { + Err(OperationError::RecoveryRequired) + } + } + pub(super) fn retain( + &self, + store: Shared<dyn InventoryStore>, + request: StockEvidenceRequest, + proof: AttachmentProof, + lease: Shared<dyn HostBound>, + ) -> Shared<Pending> { + let entry = Shared::new(Pending { + store, + request, + proof, + receipt: Mutex::new(None), + _lease: lease, + }); + self.pending + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .push(entry.clone()); + entry + } + pub(super) async fn capture( + &self, + entry: &Shared<Pending>, + ) -> crate::Result<StockEvidenceOutcome> { + let _serial = self.serial.lock().await; + self.append(entry, false).await + } + async fn append( + &self, + entry: &Shared<Pending>, + reconciling: bool, + ) -> crate::Result<StockEvidenceOutcome> { + let error = |reason| { + Error::capture_pending( + reason, + OperationContext::proposed(Some(entry.request.operation_id())), + ) + }; + let committed = *entry + .receipt + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + if let Some(receipt) = committed { + return Ok(StockEvidenceOutcome::Committed(receipt)); + } + let outcome = if reconciling { + entry + .store + .reconcile_stock_evidence(&entry.request, &entry.proof) + .await + } else { + entry + .store + .capture_stock_evidence(&entry.request, &entry.proof) + .await + } + .map_err(error)?; + match outcome { + StockEvidenceOutcome::Committed(receipt) => { + let anchor = receipt.attempt(); + let original = entry.request.attempt(); + // Host reports cannot redirect ownership to a different original. + // Actual authentication and durable append remain host obligations. + if anchor.workspace_id() != original.workspace_id() + || anchor.operation_id() != original.operation_id() + || anchor.command_digest() != original.command_digest() + || anchor.transaction_id().is_none() + || receipt.signing_attempt_id() != entry.request.signing_attempt_id() + || receipt.recipient() != entry.request.recipient() + || receipt.digest() != &entry.request.evidence_digest() + { + return Err(error(OperationError::RecoveryRequired)); + } + *entry + .receipt + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(receipt); + let removed = { + let mut pending = self + .pending + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + pending + .iter() + .position(|value| Shared::ptr_eq(value, entry)) + .map(|index| pending.remove(index)) + }; + drop(removed); + Ok(StockEvidenceOutcome::Committed(receipt)) + } + StockEvidenceOutcome::Unknown(anchor) => { + let original = entry.request.attempt(); + if anchor.workspace_id() != original.workspace_id() + || anchor.operation_id() != original.operation_id() + || anchor.command_digest() != original.command_digest() + { + return Err(error(OperationError::RecoveryRequired)); + } + // Retain exact bytes, original credentials and the resource/byte + // lease. Neither an unknown result nor an error releases them. + Ok(StockEvidenceOutcome::Unknown(anchor)) + } + } + } + pub(super) async fn drain(&self) -> crate::Result<()> { + let _serial = self.serial.lock().await; + loop { + let entry = self + .pending + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .first() + .cloned(); + let Some(entry) = entry else { + return Ok(()); + }; + if let StockEvidenceOutcome::Unknown(_) = self.append(&entry, true).await? { + return Err(Error::operation( + OperationError::RecoveryRequired, + OperationContext::proposed(Some(entry.request.operation_id())), + )); + } + } + } +} diff --git a/crates/execution/tests/support/workspace_evidence.rs b/crates/execution/tests/support/workspace_evidence.rs @@ -0,0 +1,528 @@ +//! Controlled owner/lifecycle tests, not signature or SQLite qualification. +use super::*; +use std::sync::atomic::AtomicUsize; + +#[derive(Default)] +pub(super) struct Control { + pub(super) reconciliations: AtomicUsize, + pub(super) queries: AtomicUsize, + mode: AtomicUsize, + calls: Mutex<Vec<(OperationId, CommandDigest, SigningAttemptId, String)>>, +} +pub(super) fn capture<'a>( + store: &'a Store, + request: &'a StockEvidenceRequest, + credential: &'a AttachmentProof, +) -> radroots_storage::event::BoxFuture<'a, Result<StockEvidenceOutcome, OperationError>> { + Box::pin(async move { + assert_eq!(credential.with_secret(|value| *value), [7; 32]); + assert_eq!(request.binding().input(), &input(5)); + store.capture.calls.lock().unwrap().push(( + request.operation_id(), + request.command_digest(), + request.signing_attempt_id(), + request.with_json(str::to_owned), + )); + poll_fn(|cx| { + store.waker.register(cx.waker()); + if store.waiting.load(Ordering::SeqCst) { + Poll::Pending + } else { + Poll::Ready(()) + } + }) + .await; + let mode = store.capture.mode.load(Ordering::SeqCst); + let original = request.attempt(); + if mode == 2 { + return Err(OperationError::StorageFailure); + } + if mode == 0 { + return Ok(StockEvidenceOutcome::Unknown(original)); + } + let anchor = radroots_storage::command::CommitAttempt::new( + original.workspace_id(), + if mode == 3 || mode == 4 { + OperationId::from_bytes([123; 32]) + } else { + original.operation_id() + }, + original.command_digest(), + Some(TransactionId::from_bytes([124; 32])), + ); + if mode == 4 { + return Ok(StockEvidenceOutcome::Unknown(anchor)); + } + let receipt = StockEvidenceReceipt::from_committed_store( + anchor, + if mode == 6 { + SigningAttemptId::from_bytes([125; 32]) + } else { + request.signing_attempt_id() + }, + request.recipient(), + if mode == 5 { + [126; 32] + } else { + request.evidence_digest() + }, + request.observed_at_unix_ms(), + )?; + Ok(StockEvidenceOutcome::Committed(receipt)) + }) +} +fn selected_store() -> Shared<Store> { + let store = store(); + *store.model.binding.lock().unwrap() = WorkspaceBinding::from_authenticated_host( + input(5), + radroots_storage::workspace::WorkspaceScope::SellerStockOwner, + ActorRef::new(key(), ActorRole::Seller, None, Some(input(5).grant_id())), + "device", + Sequence::new(1).unwrap(), + UnixSeconds::new(200).unwrap(), + UnixSeconds::new(100).unwrap(), + ) + .unwrap(); + store +} +fn request(scope: &WorkspaceSession, op: u8) -> StockEvidenceRequest { + StockEvidenceRequest::from_obtained_wire( + scope.binding().clone(), + OperationId::from_bytes([op; 32]), + digest(), + SigningAttemptId::from_bytes([83; 32]), + key(), + [84; 32], + "synthetic retained material; this controlled port authenticates no signature", + None, + ) + .unwrap() +} +fn creation() -> radroots_protocol::command::CreatePoolInput { + use radroots_domain::{listing::MeasurementUnit, stock::PoolUnit}; + radroots_protocol::command::CreatePoolInput::new( + PoolId::from_bytes([85; 32]), + "synthetic inventory", + PoolUnit::new(MeasurementUnit::Each, 0).unwrap(), + Quantity::new("1".parse().unwrap(), MeasurementUnit::Each).unwrap(), + ) + .unwrap() +} +fn context(scope: &WorkspaceSession) -> radroots_execution::workspace::CommandContext { + scope + .command_context( + OperationId::from_bytes([86; 32]), + None, + None, + UnixSeconds::new(150).unwrap(), + ) + .unwrap() +} +#[derive(Clone)] +struct Lifecycle(Shared<Mutex<Vec<&'static str>>>, Option<Shared<Tasks>>); +impl Lifecycle { + fn record(&self, event: &'static str) -> HostFuture<'_, radroots_execution::Result<()>> { + Box::pin(async move { + self.0.lock().unwrap().push(event); + Ok(()) + }) + } +} +impl SessionHost for Lifecycle { + fn start(&self) -> HostFuture<'_, radroots_execution::Result<()>> { + self.record("start") + } + fn quiesce(&self) -> HostFuture<'_, radroots_execution::Result<()>> { + self.record("quiesce") + } + fn drain(&self) -> HostFuture<'_, radroots_execution::Result<()>> { + Box::pin(async move { + self.0.lock().unwrap().push("drain"); + if let Some(tasks) = &self.1 { + let jobs = core::mem::take(&mut *tasks.queued.lock().unwrap()); + for job in jobs { + job.await; + } + } + Ok(()) + }) + } + fn disconnect(&self) -> HostFuture<'_, radroots_execution::Result<()>> { + self.record("disconnect") + } + fn dispose_callbacks(&self) -> HostFuture<'_, radroots_execution::Result<()>> { + self.record("callbacks") + } + fn close_storage(&self) -> HostFuture<'_, radroots_execution::Result<()>> { + self.record("storage") + } +} + +#[test] +fn unknown_capture_retains_material_after_observer_loss_blocks_fresh_work_and_closes_only_after_exact_retry() + { + block_on(async { + let store = selected_store(); + let tasks = Shared::new(Tasks::default()); + let trace = Shared::new(Mutex::new(Vec::new())); + let client = ClientSession::new_with_task_host( + Lifecycle(trace.clone(), None), + core::num::NonZeroU16::new(8).unwrap(), + tasks.clone(), + ); + let scope = client + .attach_workspace(store.clone(), input(5), proof()) + .await + .unwrap(); + client.start().await.unwrap(); + let original = request(&scope, 90); + let original_wire = original.with_json(str::to_owned); + let mut observed = + Box::pin(client.capture_stock_evidence(store.clone(), &scope, &original)); + assert!(futures::poll!(observed.as_mut()).is_pending()); + // A mutation admitted earlier must recheck before its actual store call. + let mut queued_mutation = Box::pin(client.create_stock_pool( + store.clone(), + &scope, + context(&scope), + minimum(&scope), + creation(), + )); + assert!(futures::poll!(queued_mutation.as_mut()).is_pending()); + assert_eq!( + client + .capture_stock_evidence(store.clone(), &scope, &original) + .await + .unwrap_err() + .code(), + Code::CapabilityUnavailable + ); + drop(observed); + drop(original); + let jobs = core::mem::take(&mut *tasks.queued.lock().unwrap()); + for job in jobs { + job.await; + } + assert_eq!( + queued_mutation.await.unwrap_err().code(), + Code::RecoveryRequired + ); + assert_eq!( + client + .create_stock_pool( + store.clone(), + &scope, + context(&scope), + minimum(&scope), + creation() + ) + .await + .unwrap_err() + .code(), + Code::RecoveryRequired + ); + assert!(store.calls.lock().unwrap().is_empty()); + let mut read = Box::pin(client.stock_pool( + store.clone(), + &scope, + minimum(&scope), + PoolId::from_bytes([85; 32]), + )); + assert!(futures::poll!(read.as_mut()).is_pending()); + let job = tasks.queued.lock().unwrap().pop().unwrap(); + job.await; + assert!(read.await.unwrap().is_none()); + assert_eq!(store.capture.queries.load(Ordering::SeqCst), 1); + assert_eq!( + client.close().await.unwrap_err().code(), + Code::RecoveryRequired + ); + assert_eq!(*trace.lock().unwrap(), vec!["start", "quiesce", "drain"]); + store.capture.mode.store(1, Ordering::SeqCst); + client.close().await.unwrap(); + assert_eq!( + *trace.lock().unwrap(), + vec![ + "start", + "quiesce", + "drain", + "disconnect", + "callbacks", + "storage" + ] + ); + let calls = store.capture.calls.lock().unwrap(); + assert!(calls.len() >= 3); + assert!(store.capture.reconciliations.load(Ordering::SeqCst) >= 2); + assert!(calls.iter().all(|value| value == &calls[0])); + assert_eq!(calls[0].0, OperationId::from_bytes([90; 32])); + assert_eq!(calls[0].3, original_wire); + }); +} + +#[test] +fn storage_errors_and_foreign_receipts_keep_original_capture_until_a_matching_confirmed_append() { + block_on(async { + let store = selected_store(); + let tasks = Shared::new(Tasks::default()); + let client = ClientSession::new_with_task_host( + Host, + core::num::NonZeroU16::new(8).unwrap(), + tasks.clone(), + ); + let scope = client + .attach_workspace(store.clone(), input(5), proof()) + .await + .unwrap(); + client.start().await.unwrap(); + let original = request(&scope, 91); + let mut observed = + Box::pin(client.capture_stock_evidence(store.clone(), &scope, &original)); + assert!(futures::poll!(observed.as_mut()).is_pending()); + let job = tasks.queued.lock().unwrap().pop().unwrap(); + job.await; + assert!(matches!( + observed.await.unwrap(), + StockEvidenceOutcome::Unknown(_) + )); + for mode in 2..=6 { + store.capture.mode.store(mode, Ordering::SeqCst); + let error = client.drain_stock_evidence().await.unwrap_err(); + assert_eq!( + error.code(), + if mode == 2 { + Code::StorageFailure + } else { + Code::RecoveryRequired + } + ); + assert_eq!(error.retry(), RetryClass::ManualReconciliation); + assert_eq!( + client + .create_stock_pool( + store.clone(), + &scope, + context(&scope), + minimum(&scope), + creation() + ) + .await + .unwrap_err() + .code(), + Code::RecoveryRequired + ); + } + store.capture.mode.store(1, Ordering::SeqCst); + client.drain_stock_evidence().await.unwrap(); + assert_eq!(store.capture.reconciliations.load(Ordering::SeqCst), 6); + { + let calls = store.capture.calls.lock().unwrap(); + assert!(calls.iter().all(|value| value == &calls[0])); + } + client.close().await.unwrap(); + original.with_json(|raw| assert!(raw.contains("synthetic retained material"))); + }); +} + +#[test] +fn unpolled_foreign_closed_missing_and_refused_hosts_leave_original_input_and_execute_no_capture() { + block_on(async { + let store = selected_store(); + let tasks = Shared::new(Tasks::default()); + let client = ClientSession::new_with_task_host( + Host, + core::num::NonZeroU16::new(8).unwrap(), + tasks.clone(), + ); + let scope = client + .attach_workspace(store.clone(), input(5), proof()) + .await + .unwrap(); + client.start().await.unwrap(); + let original = request(&scope, 92); + drop(client.capture_stock_evidence(store.clone(), &scope, &original)); + assert!(tasks.queued.lock().unwrap().is_empty()); + let foreign = WorkspaceSession::attach(store.clone(), input(5), proof()) + .await + .unwrap(); + assert_eq!( + client + .capture_stock_evidence(store.clone(), &foreign, &original) + .await + .unwrap_err() + .code(), + Code::Unauthorized + ); + for selected in [ + input(9), + radroots_storage::workspace::seller_stock::SellerAuthoritySelection::from_attachment( + &input(5), + ) + .attachment(GrantId::from_bytes([99; 32])) + .unwrap(), + ] { + let actor = ActorRef::new(key(), ActorRole::Seller, None, Some(selected.grant_id())); + let binding = WorkspaceBinding::from_authenticated_host( + selected, + radroots_storage::workspace::WorkspaceScope::SellerStockOwner, + actor, + "device", + Sequence::new(1).unwrap(), + UnixSeconds::new(200).unwrap(), + UnixSeconds::new(100).unwrap(), + ) + .unwrap(); + let forged = StockEvidenceRequest::from_obtained_wire( + binding, + original.operation_id(), + original.command_digest(), + original.signing_attempt_id(), + original.recipient(), + *original.intent_digest(), + "synthetic foreign descriptor", + None, + ) + .unwrap(); + assert_eq!( + client + .capture_stock_evidence(store.clone(), &scope, &forged) + .await + .unwrap_err() + .code(), + Code::Unauthorized + ); + } + tasks.refuse.store(true, Ordering::SeqCst); + assert_eq!( + client + .capture_stock_evidence(store.clone(), &scope, &original) + .await + .unwrap_err() + .code(), + Code::CapabilityUnavailable + ); + tasks.refuse.store(false, Ordering::SeqCst); + scope.close(); + assert!( + client + .capture_stock_evidence(store.clone(), &scope, &original) + .await + .is_err() + ); + client.close().await.unwrap(); + let no_host = ClientSession::new(Host, core::num::NonZeroU16::new(8).unwrap()); + let scope = no_host + .attach_workspace(store.clone(), input(5), proof()) + .await + .unwrap(); + no_host.start().await.unwrap(); + assert!( + no_host + .capture_stock_evidence(store.clone(), &scope, &original) + .await + .is_err() + ); + no_host.close().await.unwrap(); + assert!(store.capture.calls.lock().unwrap().is_empty()); + original.with_json(|raw| assert!(raw.contains("synthetic retained material"))); + }); +} + +#[test] +fn close_waits_for_the_active_capture_and_keeps_original_material_after_the_observer_is_dropped() { + block_on(async { + let store = selected_store(); + store.capture.mode.store(1, Ordering::SeqCst); + store.waiting.store(true, Ordering::SeqCst); + let tasks = Shared::new(Tasks::default()); + let trace = Shared::new(Mutex::new(Vec::new())); + let client = ClientSession::new_with_task_host( + Lifecycle(trace.clone(), None), + core::num::NonZeroU16::new(8).unwrap(), + tasks.clone(), + ); + let scope = client + .attach_workspace(store.clone(), input(5), proof()) + .await + .unwrap(); + client.start().await.unwrap(); + let original = request(&scope, 93); + let mut observed = + Box::pin(client.capture_stock_evidence(store.clone(), &scope, &original)); + assert!(futures::poll!(observed.as_mut()).is_pending()); + let mut job = tasks.queued.lock().unwrap().pop().unwrap(); + assert!(futures::poll!(job.as_mut()).is_pending()); + drop(observed); + drop(original); + let mut closing = Box::pin(client.close()); + assert!(futures::poll!(closing.as_mut()).is_pending()); + assert_eq!(*trace.lock().unwrap(), vec!["start", "quiesce", "drain"]); + store.waiting.store(false, Ordering::SeqCst); + store.waker.wake(); + job.await; + closing.await.unwrap(); + assert_eq!( + *trace.lock().unwrap(), + vec![ + "start", + "quiesce", + "drain", + "disconnect", + "callbacks", + "storage" + ] + ); + assert_eq!(store.capture.calls.lock().unwrap().len(), 1); + }); +} + +#[test] +fn close_drives_the_host_owned_active_task_before_retrying_its_retained_capture() { + block_on(async { + let store = selected_store(); + store.capture.mode.store(1, Ordering::SeqCst); + store.waiting.store(true, Ordering::SeqCst); + let tasks = Shared::new(Tasks::default()); + let trace = Shared::new(Mutex::new(Vec::new())); + let client = ClientSession::new_with_task_host( + Lifecycle(trace.clone(), Some(tasks.clone())), + core::num::NonZeroU16::new(8).unwrap(), + tasks.clone(), + ); + let scope = client + .attach_workspace(store.clone(), input(5), proof()) + .await + .unwrap(); + client.start().await.unwrap(); + let original = request(&scope, 94); + let mut observed = + Box::pin(client.capture_stock_evidence(store.clone(), &scope, &original)); + assert!(futures::poll!(observed.as_mut()).is_pending()); + let mut job = tasks.queued.lock().unwrap().pop().unwrap(); + assert!(futures::poll!(job.as_mut()).is_pending()); + tasks.queued.lock().unwrap().push(job); + drop(observed); + drop(original); + let mut closing = Box::pin(client.close()); + assert!(futures::poll!(closing.as_mut()).is_pending()); + assert_eq!(*trace.lock().unwrap(), vec!["start", "quiesce", "drain"]); + store.waiting.store(false, Ordering::SeqCst); + store.waker.wake(); + assert!(matches!( + futures::poll!(closing.as_mut()), + Poll::Ready(Ok(())) + )); + assert_eq!( + *trace.lock().unwrap(), + vec![ + "start", + "quiesce", + "drain", + "disconnect", + "callbacks", + "storage" + ] + ); + assert_eq!(store.capture.calls.lock().unwrap().len(), 1); + }); +} diff --git a/crates/execution/tests/support/workspace_inventory.rs b/crates/execution/tests/support/workspace_inventory.rs @@ -4,6 +4,8 @@ use radroots_execution::{Code, RetryClass, session::tasks::TaskHost}; use radroots_storage::{command::OperationError, inventory::*}; #[path = "workspace_acceptance.rs"] mod acceptance; +#[path = "workspace_evidence.rs"] +mod evidence; #[derive(Default)] struct Tasks { @@ -28,6 +30,7 @@ impl TaskHost for Tasks { } } struct Store { + capture: evidence::Control, model: Shared<Model>, calls: Mutex<Vec<(OperationId, CommandDigest)>>, waiting: AtomicBool, @@ -48,6 +51,21 @@ impl WorkspaceStore for Store { } } impl InventoryStore for Store { + fn reconcile_stock_evidence<'a>( + &'a self, + request: &'a StockEvidenceRequest, + proof: &'a AttachmentProof, + ) -> radroots_storage::event::BoxFuture<'a, Result<StockEvidenceOutcome, OperationError>> { + self.capture.reconciliations.fetch_add(1, Ordering::SeqCst); + evidence::capture(self, request, proof) + } + fn capture_stock_evidence<'a>( + &'a self, + request: &'a StockEvidenceRequest, + proof: &'a AttachmentProof, + ) -> radroots_storage::event::BoxFuture<'a, Result<StockEvidenceOutcome, OperationError>> { + evidence::capture(self, request, proof) + } fn prepare_stock_acceptance( &self, request: StockAcceptanceRequest, @@ -150,11 +168,15 @@ impl InventoryStore for Store { '_, Result<Option<radroots_domain::stock::PoolView>, OperationError>, > { - panic!("unexpected port call") + Box::pin(async move { + self.capture.queries.fetch_add(1, Ordering::SeqCst); + Ok(None) + }) } } fn store() -> Shared<Store> { Shared::new(Store { + capture: evidence::Control::default(), model: new_model(), calls: Mutex::new(vec![]), waiting: AtomicBool::new(false), diff --git a/crates/radroots/README.md b/crates/radroots/README.md @@ -162,3 +162,13 @@ The ordinary native inquiry and initial-listing boundaries continue accepted online work through exact signing, protected capture, installation and delivery. Full commerce behavior, final cross-platform acceptance and generated binding qualification remain separate requirements. + +`client.stock(scope).capture_evidence(&request)` sends obtained original evidence +to that client's selected shared stock store through Core's bounded task owner. +The original request remains borrowed; failed or unknown captures stay owned +after observer loss. `drain_evidence` retries this client's retained originals +without signing or changing captured bytes. Close preserves unresolved material +and resource ownership before disposing callbacks, keys or storage. The concrete +qualified store still authenticates the actual protected claim and exact signed +bytes; input and receipt metadata grant no authority. This low-level capture +surface supplies neither full commerce execution nor original nonissuance proof. diff --git a/crates/radroots/src/stock.rs b/crates/radroots/src/stock.rs @@ -6,6 +6,9 @@ use radroots_domain::{ }; pub use radroots_protocol::command::{CreatePoolInput, MapOptionInput, PoolCorrectionInput}; pub use radroots_storage::inventory::{InventoryCheckpointRef, InventoryOutcome, InventoryReceipt}; +pub use radroots_storage::inventory::{ + StockEvidenceOutcome, StockEvidenceReceipt, StockEvidenceRequest, +}; /// Borrowed access to this client's explicitly selected private stock owner. pub struct Stock<'a> { @@ -16,6 +19,23 @@ impl<'a> Stock<'a> { pub(crate) const fn new(client: &'a Client, scope: WorkspaceSession) -> Self { Self { client, scope } } + /// Retains already obtained original evidence through actual append and + /// caller loss. Failed or uncertain capture remains owned for explicit retry + /// or close draining. This supplies no signing or installation permission. + pub async fn capture_evidence( + &self, + request: &StockEvidenceRequest, + ) -> Result<StockEvidenceOutcome> { + self.client + .session + .capture_stock_evidence(self.client.inventory()?, &self.scope, request) + .await + } + /// Retries this client's retained original captures without obtaining new + /// signatures, changing captured bytes or renewing their original windows. + pub async fn drain_evidence(&self) -> Result<()> { + self.client.session.drain_stock_evidence().await + } /// Creates a private pool with actual atomic conservation and idempotency. /// The report claims neither Nostr signing nor transport delivery. pub async fn create_pool( diff --git a/crates/storage/README.md b/crates/storage/README.md @@ -417,3 +417,12 @@ Their constructors provide no protected claim, authority, installation or safe release proof. Capture neither signs nor renews current issuing permission. Unsupported hosts refuse. An ordinary owner must separately retain admitted tasks, failed captures and resource leases until durable settlement. + +`InventoryStore::reconcile_stock_evidence` explicitly resolves original capture +after an error or unknown append. A qualified host authenticates retained capture +ownership before replacing an uncertain connection, keeps its namespace writer +lease through read-only preflight and validates the existing namespace without +initialization or migration. Confirmation permits only the matching original +capture; no first signature, new command or renewed issuing grant is supplied. +The borrowed evidence remains available after failure. Unsupported hosts refuse; +ordinary capture is not an implicit recovery fallback. diff --git a/crates/storage/src/inventory.rs b/crates/storage/src/inventory.rs @@ -278,6 +278,17 @@ pub trait InventoryStore: WorkspaceStore { ) -> BoxFuture<'a, Result<StockEvidenceOutcome, OperationError>> { Box::pin(async { Err(OperationError::UnsupportedOperation) }) } + /// Reconciles only already obtained original bytes after a failed/unknown + /// append. A qualified host resolves its actual connection/commit state + /// while retaining writer ownership; it cannot sign or renew a first issue. + /// Unsupported hosts refuse, with the borrowed original left intact. + fn reconcile_stock_evidence<'a>( + &'a self, + _request: &'a StockEvidenceRequest, + _proof: &'a AttachmentProof, + ) -> BoxFuture<'a, Result<StockEvidenceOutcome, OperationError>> { + Box::pin(async { Err(OperationError::UnsupportedOperation) }) + } /// Prepares an original acceptance with complete atomic holds and the order /// fence. Unsupported hosts execute no fallback or simulated stock effect. fn prepare_stock_acceptance( diff --git a/crates/storage/src/inventory/evidence.rs b/crates/storage/src/inventory/evidence.rs @@ -64,6 +64,11 @@ impl StockEvidenceRequest { pub const fn intent_digest(&self) -> &[u8; 32] { &self.intent } + /// Digest of these exact obtained bytes, not a signature or commit proof. + pub fn evidence_digest(&self) -> [u8; 32] { + use sha2::{Digest, Sha256}; + self.with_json(|raw| Sha256::digest(raw.as_bytes()).into()) + } pub const fn observed_at_unix_ms(&self) -> Option<NonZeroU64> { self.observed } diff --git a/crates/storage_sqlite/README.md b/crates/storage_sqlite/README.md @@ -534,3 +534,12 @@ distinct valid encodings, idempotency, late capture and unknown-commit fencing retain their existing semantics. Catalog13 and stored encodings/hashes remain unchanged. Public native receipt/outcome aliases are removed. Borrowed inputs survive errors; full ordinary task/capture ownership remains separate work. + +Original stock evidence reconciliation uses the generic journal's same-owner +connection recovery machinery through `InventoryStore::reconcile_stock_evidence`. +It authenticates retained capture credentials before confirmed close, preserves +the writer lease and cached authorization through refused read-only preflight, +and validates a replacement against the existing namespace. It never initializes +or migrates a store. Only a confirmed original capture clears database uncertainty; +errors/unknown results keep the fence. No current issuance clock, new claim, +signature or stock release is obtained. Ordinary outboxes refuse this port. diff --git a/crates/storage_sqlite/src/workspace/recovery.rs b/crates/storage_sqlite/src/workspace/recovery.rs @@ -1,15 +1,65 @@ //! Original obtained-evidence recovery on the same namespace owner. use super::{ - AttachmentInput, AttachmentProof, WorkspaceClock, WorkspaceCrypto, admission::Journal, - fs::Namespace, record::GrantRecord, worker, + AttachmentInput, AttachmentProof, WorkspaceClock, WorkspaceCrypto, + admission::Journal, + fs::{Kind, Namespace}, + record::GrantRecord, + stock, worker, +}; +use radroots_storage::{ + command::{ArtifactEvidence, EvidenceCaptureOutcome, OperationError}, + inventory::{StockEvidenceOutcome, StockEvidenceRequest}, + workspace::seller_stock::SellerAuthoritySelection, }; -use radroots_storage::command::{ArtifactEvidence, EvidenceCaptureOutcome, OperationError}; use sqlx::{Connection, SqliteConnection, sqlite::SqliteConnectOptions}; +enum GrantAuthorization { + Outbox(GrantRecord), + Stock(stock::CaptureAuthorization), +} +impl GrantAuthorization { + fn authenticate( + &self, + input: &AttachmentInput, + proof: &AttachmentProof, + ) -> Result<(), OperationError> { + match self { + Self::Outbox(record) => record + .authenticate_retained_capture(input, proof) + .map_err(Into::into), + Self::Stock(record) => record.authenticate(input, proof).map_err(Into::into), + } + } +} pub(super) struct RecoveryAuthorization { - grant: GrantRecord, + grant: GrantAuthorization, close_confirmed: bool, } +enum Material { + Outbox(Box<(AttachmentInput, ArtifactEvidence)>), + Stock(Box<StockEvidenceRequest>), +} +impl Material { + fn input(&self) -> &AttachmentInput { + match self { + Self::Outbox(value) => &value.0, + Self::Stock(value) => value.binding().input(), + } + } +} +enum Captured { + Outbox(EvidenceCaptureOutcome), + Stock(StockEvidenceOutcome), +} +impl Captured { + fn committed(&self) -> bool { + matches!( + self, + Self::Outbox(EvidenceCaptureOutcome::Committed(_)) + | Self::Stock(StockEvidenceOutcome::Committed(_)) + ) + } +} pub(super) struct RecoveryTurn<'a> { pub connection: &'a mut Option<SqliteConnection>, pub cached_grant: &'a mut Option<RecoveryAuthorization>, @@ -27,43 +77,125 @@ impl RecoveryTurn<'_> { proof: AttachmentProof, evidence: ArtifactEvidence, ) -> Result<EvidenceCaptureOutcome, OperationError> { + if self.namespace.kind() != Kind::Outbox { + return Err(OperationError::UnsupportedOperation); + } if input != *self.selected { return Err(OperationError::Unauthorized); } - if !*self.uncertain { - let connection = self - .connection - .as_mut() - .ok_or(OperationError::RecoveryRequired)?; - return Journal { - connection, - namespace: self.namespace, - selected: self.selected, - crypto: self.crypto, - clock: self.clock, - uncertain: self.uncertain, + match self + .recover(Material::Outbox(Box::new((input, evidence))), proof) + .await? + { + Captured::Outbox(value) => Ok(value), + _ => Err(OperationError::RecoveryRequired), + } + } + pub(super) async fn capture_stock( + &mut self, + request: StockEvidenceRequest, + proof: AttachmentProof, + ) -> Result<StockEvidenceOutcome, OperationError> { + if self.namespace.kind() != Kind::SellerStock { + return Err(OperationError::UnsupportedOperation); + } + if !SellerAuthoritySelection::from_attachment(self.selected) + .matches_attachment(request.binding().input()) + { + return Err(OperationError::Unauthorized); + } + match self + .recover(Material::Stock(Box::new(request)), proof) + .await? + { + Captured::Stock(value) => Ok(value), + _ => Err(OperationError::RecoveryRequired), + } + } + async fn recover( + &mut self, + material: Material, + proof: AttachmentProof, + ) -> Result<Captured, OperationError> { + let replacing = *self.uncertain; + if replacing { + self.prepare(material.input(), &proof).await?; + } + let connection = self + .connection + .as_mut() + .ok_or(OperationError::RecoveryRequired)?; + let mut capture_uncertain = false; + let uncertainty = if replacing { + &mut capture_uncertain + } else { + &mut *self.uncertain + }; + let result = match material { + Material::Outbox(value) => { + let (input, evidence) = *value; + Journal { + connection, + namespace: self.namespace, + selected: self.selected, + crypto: self.crypto, + clock: self.clock, + uncertain: uncertainty, + } + .capture_issuance(input, proof, evidence) + .await + .map(Captured::Outbox) } - .capture_issuance(input, proof, evidence) - .await; + Material::Stock(request) => Box::pin(stock::issuance::evidence::capture( + &mut stock::inventory::Turn { + connection, + namespace: self.namespace, + selected: self.selected, + crypto: self.crypto, + clock: self.clock, + uncertain: uncertainty, + }, + *request, + proof, + )) + .await + .map(Captured::Stock), + }; + if replacing && result.as_ref().is_ok_and(Captured::committed) { + *self.uncertain = false; + self.cached_grant.take(); } - + result + } + async fn prepare( + &mut self, + input: &AttachmentInput, + proof: &AttachmentProof, + ) -> Result<(), OperationError> { self.namespace.validate()?; - // Authenticate original capture ownership before disposing any existing + // Authenticate original capture ownership before disposing the existing // connection. No current lease, clock or fresh command grant is acquired. if let Some(connection) = self.connection.as_mut() { - let protected = worker::read(connection, self.selected).await?; - let plaintext = self - .crypto - .open( - self.selected.workspace_id(), - self.selected.grant_id(), - &protected, - ) - .await?; - let record = plaintext.expose_secret(GrantRecord::decode_outbox)?; - record.authenticate_retained_capture(&input, &proof)?; + let grant = match self.namespace.kind() { + Kind::Outbox => { + let protected = worker::read(connection, self.selected).await?; + let plaintext = self + .crypto + .open( + self.selected.workspace_id(), + self.selected.grant_id(), + &protected, + ) + .await?; + GrantAuthorization::Outbox(plaintext.expose_secret(GrantRecord::decode_outbox)?) + } + Kind::SellerStock => GrantAuthorization::Stock( + stock::capture_authorization(connection, input, self.crypto, proof).await?, + ), + }; + grant.authenticate(input, proof)?; *self.cached_grant = Some(RecoveryAuthorization { - grant: record, + grant, close_confirmed: false, }); } else { @@ -71,15 +203,13 @@ impl RecoveryTurn<'_> { .cached_grant .as_ref() .ok_or(OperationError::RecoveryRequired)?; - cached.grant.authenticate_retained_capture(&input, &proof)?; + cached.grant.authenticate(input, proof)?; if !cached.close_confirmed { return Err(OperationError::RecoveryRequired); } } - - // Namespace authority stays owned throughout the close, read-only - // preflight and replacement connection. Missing/foreign schemas are - // refused; there is no initialization, migration or restored dispatch. + // The namespace writer lease stays owned through confirmed close, + // read-only preflight and replacement. No initialization or migration. if let Some(connection) = self.connection.take() { connection .close() @@ -103,24 +233,18 @@ impl RecoveryTurn<'_> { .connection .as_mut() .ok_or(OperationError::RecoveryRequired)?; - // A failed validation retains this replacement connection. Its close - // must be confirmed by the same owner before any subsequent replacement. - worker::initialize(connection, self.namespace, self.selected, None, self.crypto).await?; - let mut capture_uncertain = false; - let result = Journal { - connection, - namespace: self.namespace, - selected: self.selected, - crypto: self.crypto, - clock: self.clock, - uncertain: &mut capture_uncertain, - } - .capture_issuance(input, proof, evidence) - .await; - if matches!(&result, Ok(EvidenceCaptureOutcome::Committed(_))) { - *self.uncertain = false; - self.cached_grant.take(); + // A failed validation retains the replacement; its close must be + // confirmed by this owner before a subsequent replacement. + match self.namespace.kind() { + Kind::Outbox => { + worker::initialize(connection, self.namespace, self.selected, None, self.crypto) + .await? + } + Kind::SellerStock => { + stock::initialize(connection, self.namespace, self.selected, None, self.crypto) + .await? + } } - result + Ok(()) } } diff --git a/crates/storage_sqlite/src/workspace/stock.rs b/crates/storage_sqlite/src/workspace/stock.rs @@ -860,6 +860,27 @@ pub(super) async fn initialize( read(connection, input, crypto).await?; namespace.validate() } +/// Retained credential authentication only; no new command permission. +pub(super) struct CaptureAuthorization(StockRecord); +impl CaptureAuthorization { + pub(super) fn authenticate( + &self, + input: &AttachmentInput, + proof: &AttachmentProof, + ) -> Result<(), WorkspaceError> { + self.0.authenticate_retained_capture(input, proof) + } +} +pub(super) async fn capture_authorization( + connection: &mut SqliteConnection, + input: &AttachmentInput, + crypto: &WorkspaceCrypto, + proof: &AttachmentProof, +) -> Result<CaptureAuthorization, WorkspaceError> { + let (_, record, _, _) = read(connection, input, crypto).await?; + record.authenticate_retained_capture(input, proof)?; + Ok(CaptureAuthorization(record)) +} async fn read( connection: &mut SqliteConnection, input: &AttachmentInput, @@ -1101,6 +1122,23 @@ async fn unchanged( } impl radroots_storage::inventory::InventoryStore for NativeWorkspace { + fn reconcile_stock_evidence<'a>( + &'a self, + request: &'a StockEvidenceRequest, + proof: &'a AttachmentProof, + ) -> radroots_transport::BoxFuture< + 'a, + Result<StockEvidenceOutcome, radroots_storage::command::OperationError>, + > { + Box::pin(async move { + self.worker + .reconcile_stock_evidence( + request.retained_copy(), + proof.with_secret(|bytes| AttachmentProof::from_protected_input(bytes))?, + ) + .await + }) + } fn capture_stock_evidence<'a>( &'a self, request: &'a StockEvidenceRequest, diff --git a/crates/storage_sqlite/src/workspace/stock/public_source/evidence_reconciliation_tests.rs b/crates/storage_sqlite/src/workspace/stock/public_source/evidence_reconciliation_tests.rs @@ -0,0 +1,215 @@ +//! Real SQLite original-evidence reconciliation on the same namespace owner. +use super::*; +use radroots_storage::inventory::{InventoryStore, StockEvidenceRequest}; + +async fn fault(r: &Rig, unknown: bool) -> SqliteConnection { + let mut connection = database(r).await; + if unknown { + sqlx::raw_sql("CREATE TABLE deferred_fault(value INTEGER REFERENCES stock_authority(singleton) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fault AFTER INSERT ON stock_signing_evidence BEGIN INSERT INTO deferred_fault VALUES(2); END").execute(&mut connection).await.unwrap(); + } else { + sqlx::raw_sql("CREATE TRIGGER fault BEFORE INSERT ON stock_signing_evidence BEGIN SELECT RAISE(ABORT,'synthetic original capture failure'); END").execute(&mut connection).await.unwrap(); + } + connection +} +async fn remove_fault(connection: &mut SqliteConnection, unknown: bool) { + sqlx::raw_sql(if unknown { + "DROP TRIGGER fault; DROP TABLE deferred_fault" + } else { + "DROP TRIGGER fault" + }) + .execute(connection) + .await + .unwrap(); +} +async fn writer_remains_owned(r: &Rig) { + let error = NativeWorkspace::open_seller_stock( + &r.path, + r.enrollment.input().clone(), + r.fixture.crypto(), + r.clock.clone(), + ) + .await + .err() + .unwrap(); + assert_eq!(error, WorkspaceError::WriterActive); +} +async fn request( + r: &Rig, + receipt: &StockAcceptanceReceipt, +) -> (StockSigningAttempt, StockEvidenceRequest, String) { + let recipient = receipt.rumor().recipients()[0]; + let (report, raw) = obtained(r, receipt, recipient).await; + let request = StockEvidenceCapture::verify(r.binding.clone(), &report, &raw, None) + .unwrap() + .into_request(); + (report, request, raw) +} +async fn reconcile( + r: &Rig, + request: &StockEvidenceRequest, +) -> Result<StockEvidenceOutcome, OperationError> { + let port: &dyn InventoryStore = &r.store; + port.reconcile_stock_evidence(request, r.enrollment.proof()) + .await +} + +#[tokio::test] +async fn same_owner_capture_reconciliation_resolves_real_rollback_and_unknown_commit_without_clock_or_reissue() + { + for unknown in [false, true] { + let r = Rig::new().await; + let preparation = ready(&r).await; + let (report, request, raw) = request(&r, &preparation).await; + let before = inventory_minimum(&r).await; + let mut database = fault(&r, unknown).await; + let first = + InventoryStore::capture_stock_evidence(&r.store, &request, r.enrollment.proof()).await; + if unknown { + assert!(matches!(first.unwrap(), StockEvidenceOutcome::Unknown(_))); + assert_eq!( + InventoryStore::capture_stock_evidence(&r.store, &request, r.enrollment.proof()) + .await + .unwrap_err(), + OperationError::RecoveryRequired + ); + } else { + assert!(first.is_err()); + } + writer_remains_owned(&r).await; + remove_fault(&mut database, unknown).await; + let original_clock = r.clock.0.load(Ordering::SeqCst); + r.clock.0.store(0, Ordering::SeqCst); + let retained = committed(reconcile(&r, &request).await.unwrap()); + assert_eq!(retained.signing_attempt_id(), report.attempt_id()); + assert_eq!(retained.observed_at_unix_ms(), None); + assert_eq!(retained.digest(), &request.evidence_digest()); + assert_eq!(committed(reconcile(&r, &request).await.unwrap()), retained); + request.with_json(|value| assert_eq!(value, raw)); + r.clock.0.store(original_clock, Ordering::SeqCst); + assert_eq!( + inventory_minimum(&r).await.revision().get(), + before.revision().get() + 1 + ); + let StockClaimOutcome::Existing(original) = + claim(&r, &preparation, report.recipient()).await.unwrap() + else { + panic!("reconciliation minted a fresh claim") + }; + assert_eq!(original.attempt_id(), report.attempt_id()); + assert_eq!( + sqlx::query_scalar::<_, i64>("SELECT count(*) FROM stock_signing_evidence") + .fetch_one(&mut database) + .await + .unwrap(), + 1 + ); + writer_remains_owned(&r).await; + assert_pool(&r, 80, 9, 1, 2).await; + database.close().await.unwrap(); + r.store.close().await.unwrap(); + } +} + +#[tokio::test] +async fn refused_stock_recovery_preflight_keeps_writer_and_cached_original_authorization_for_exact_retry() + { + let mut r = Rig::new().await; + let preparation = ready(&r).await; + let (report, request, raw) = request(&r, &preparation).await; + let provider = reopen(&mut r).await; + let mut database = fault(&r, true).await; + assert!(matches!( + InventoryStore::capture_stock_evidence(&r.store, &request, r.enrollment.proof()) + .await + .unwrap(), + StockEvidenceOutcome::Unknown(_) + )); + remove_fault(&mut database, true).await; + provider + .gate + .arm(Direction::Unwrap, "radroots.seller_stock.authority.v13", 0); + let mut recovery = Box::pin(reconcile(&r, &request)); + assert!(futures::poll!(recovery.as_mut()).is_pending()); + provider.gate.entered(); + // The authorized old snapshot has already been read. Future schema then + // refuses read-only preflight after close; no migration or repair occurs. + sqlx::raw_sql("PRAGMA user_version=14") + .execute(&mut database) + .await + .unwrap(); + provider.gate.release(); + assert!(recovery.await.is_err()); + writer_remains_owned(&r).await; + assert_eq!( + sqlx::query_scalar::<_, i64>("PRAGMA user_version") + .fetch_one(&mut database) + .await + .unwrap(), + 14 + ); + let wrong = AttachmentProof::from_protected_input(&[99; 32]).unwrap(); + assert!( + InventoryStore::reconcile_stock_evidence(&r.store, &request, &wrong) + .await + .is_err() + ); + request.with_json(|value| assert_eq!(value, raw)); + sqlx::raw_sql("PRAGMA user_version=13") + .execute(&mut database) + .await + .unwrap(); + let retained = committed(reconcile(&r, &request).await.unwrap()); + assert_eq!(retained.signing_attempt_id(), report.attempt_id()); + assert_eq!(committed(reconcile(&r, &request).await.unwrap()), retained); + assert_pool(&r, 80, 9, 1, 2).await; + database.close().await.unwrap(); + r.store.close().await.unwrap(); +} + +#[tokio::test] +async fn lost_reconciliation_observer_drains_the_actual_replacement_and_original_capture_before_reopen() + { + let mut r = Rig::new().await; + let preparation = ready(&r).await; + let (report, request, raw) = request(&r, &preparation).await; + let provider = reopen(&mut r).await; + let mut database = fault(&r, true).await; + assert!(matches!( + InventoryStore::capture_stock_evidence(&r.store, &request, r.enrollment.proof()) + .await + .unwrap(), + StockEvidenceOutcome::Unknown(_) + )); + remove_fault(&mut database, true).await; + provider + .gate + .arm(Direction::Unwrap, "radroots.seller_stock.authority.v13", 0); + let mut recovery = Box::pin(reconcile(&r, &request)); + assert!(futures::poll!(recovery.as_mut()).is_pending()); + provider.gate.entered(); + drop(recovery); + provider.gate.release(); + r.store.close().await.unwrap(); + database.close().await.unwrap(); + reopen_plain(&mut r).await; + let retained = committed(reconcile(&r, &request).await.unwrap()); + let stored = r + .store + .stock_signing_evidence( + selector(&r, &preparation, report.recipient()).await, + *retained.digest(), + r.enrollment.proof(), + ) + .await + .unwrap() + .unwrap(); + stored.with_json(|value| assert_eq!(value, raw)); + let StockClaimOutcome::Existing(original) = + claim(&r, &preparation, report.recipient()).await.unwrap() + else { + panic!("lost observer minted a claim") + }; + assert_eq!(original.attempt_id(), report.attempt_id()); + assert_pool(&r, 80, 9, 1, 2).await; + r.store.close().await.unwrap(); +} diff --git a/crates/storage_sqlite/src/workspace/stock/public_source/evidence_tests.rs b/crates/storage_sqlite/src/workspace/stock/public_source/evidence_tests.rs @@ -2,6 +2,8 @@ use super::*; #[path = "installation_tests.rs"] mod installations; +#[path = "evidence_reconciliation_tests.rs"] +mod reconciliation; #[path = "snapshot_tests.rs"] mod snapshots; use crate::workspace::StockEvidenceCapture; diff --git a/crates/storage_sqlite/src/workspace/stock/public_source/exact_recovery_tests.rs b/crates/storage_sqlite/src/workspace/stock/public_source/exact_recovery_tests.rs @@ -204,6 +204,14 @@ async fn ordinary_outbox_refuses_exact_replay_without_adopting_stock_authority() .unwrap_err(), OperationError::UnsupportedOperation, ); + assert_eq!( + radroots_storage::inventory::InventoryStore::reconcile_stock_evidence( + &outbox, &evidence, &proof + ) + .await + .unwrap_err(), + OperationError::UnsupportedOperation, + ); outbox.close().await.unwrap(); r.store.close().await.unwrap(); } diff --git a/crates/storage_sqlite/src/workspace/worker.rs b/crates/storage_sqlite/src/workspace/worker.rs @@ -69,6 +69,11 @@ enum Job { AttachmentProof, Reply<stock::StockEvidencePage, OperationError>, ), + RecoverStockEvidence( + Box<radroots_storage::inventory::StockEvidenceRequest>, + AttachmentProof, + Reply<stock::StockEvidenceOutcome, OperationError>, + ), CaptureStockEvidence( Box<radroots_storage::inventory::StockEvidenceRequest>, AttachmentProof, @@ -329,7 +334,7 @@ impl Job { Self::StockEvidencePage(_, _, _, reply) => { let _ = reply.send(Err(OperationError::RecoveryRequired)); } - Self::CaptureStockEvidence(_, _, reply) => { + Self::CaptureStockEvidence(_, _, reply) | Self::RecoverStockEvidence(_, _, reply) => { let _ = reply.send(Err(OperationError::RecoveryRequired)); } Self::StockEvidence(_, _, _, reply) => { @@ -605,6 +610,7 @@ impl Handle { | Job::StockSigningAttempt(..) | Job::AuthorizeStockExactReplay(..) | Job::CaptureStockEvidence(..) + | Job::RecoverStockEvidence(..) | Job::StockEvidence(..) | Job::StockEvidencePage(..) | Job::StockSnapshot(..) @@ -620,6 +626,24 @@ impl Handle { continue; } let job = match job { + Job::RecoverStockEvidence(request, proof, reply) => { + let result = Box::pin( + super::recovery::RecoveryTurn { + connection: &mut connection, + cached_grant: &mut cached_grant, + namespace: &namespace, + selected: &input, + crypto: &crypto, + clock: clock.as_ref(), + uncertain: &mut uncertain, + options: &options, + } + .capture_stock(*request, proof), + ) + .await; + let _ = reply.send(result); + continue; + } Job::RecoverCapture(selected, proof, evidence, reply) => { let result = super::recovery::RecoveryTurn { connection: &mut connection, @@ -1261,7 +1285,9 @@ impl Handle { } let _ = reply.send(result); } - job @ Job::RecoverCapture(..) => job.reject_recovery(), + job @ (Job::RecoverCapture(..) | Job::RecoverStockEvidence(..)) => { + job.reject_recovery() + } Job::CaptureDeliveryObservation( binding, proof, @@ -1806,6 +1832,16 @@ impl Handle { ))?; result.await.map_err(|_| OperationError::RecoveryRequired)? } + pub(super) async fn reconcile_stock_evidence( + &self, + request: radroots_storage::inventory::StockEvidenceRequest, + proof: AttachmentProof, + ) -> Result<stock::StockEvidenceOutcome, OperationError> { + let unknown = stock::StockEvidenceOutcome::Unknown(request.attempt()); + let (reply, result) = oneshot::channel(); + self.send(Job::RecoverStockEvidence(Box::new(request), proof, reply))?; + result.await.unwrap_or(Ok(unknown)) + } pub(super) async fn capture_stock_signing_evidence( &self, evidence: radroots_storage::inventory::StockEvidenceRequest, @@ -1986,7 +2022,9 @@ impl Handle { } fn send(&self, job: Job) -> Result<(), WorkspaceError> { let bytes = match &job { - Job::RetainStockListing(..) | Job::CaptureStockEvidence(..) => 256 * 1024, + Job::RetainStockListing(..) + | Job::CaptureStockEvidence(..) + | Job::RecoverStockEvidence(..) => 256 * 1024, Job::StockEvidence(..) | Job::StockEvidencePage(..) | Job::StockSnapshot(..) diff --git a/tools/xtask/src/architecture/decision_package.rs b/tools/xtask/src/architecture/decision_package.rs @@ -290,6 +290,11 @@ struct InitialAgreementInstallation { #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct SigningIssuance { + shared_reconciliation_port: String, + reconciliation_retains_writer_through_replacement: bool, + reconciliation_authenticates_before_connection_close: bool, + reconciliation_preflight_initializes_or_migrates: bool, + reconciliation_clears_fence_only_after_confirmed_capture: bool, shared_capture_port: String, shared_capture_model_owner: String, shared_capture_descriptors_are_authority: bool, @@ -419,6 +424,15 @@ struct Execution { #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct OrdinaryInventoryOwner { + capture_drain_uses_explicit_reconciliation: bool, + owned_capture_method: String, + retained_capture_survives_observer_loss: bool, + failed_capture_blocks_fresh_work: bool, + failed_capture_blocks_disposal: bool, + capture_preserves_actual_error_code: bool, + pending_capture_retry: String, + capture_reports_must_match_original: bool, + capture_scope_metadata_renews_issuance: bool, owned_acceptance_method: String, acceptance_is_preparation_only: bool, lost_accepted_admission_is_unknown: bool, @@ -607,6 +621,20 @@ fn validate_bytes(raw: &[u8]) -> Result<(), String> { && stock.signing_issuance.coherent_operation_snapshot_required && stock.signing_issuance.shared_capture_port == "InventoryStore.capture_stock_evidence" + && stock.signing_issuance.shared_reconciliation_port + == "InventoryStore.reconcile_stock_evidence" + && stock + .signing_issuance + .reconciliation_retains_writer_through_replacement + && stock + .signing_issuance + .reconciliation_authenticates_before_connection_close + && !stock + .signing_issuance + .reconciliation_preflight_initializes_or_migrates + && stock + .signing_issuance + .reconciliation_clears_fence_only_after_confirmed_capture && stock.signing_issuance.shared_capture_model_owner == "radroots_storage.inventory" && !stock .signing_issuance @@ -1123,6 +1151,21 @@ fn validate_values(v: &Values, limits: &BTreeMap<String, u64>) -> Result<(), Str fn validate_execution(e: &Execution, p: &Protocol, c: &Cutover) -> Result<(), String> { require( e.ordinary_inventory_owner.execution == "radroots_execution.ClientSession" + && e.ordinary_inventory_owner.owned_capture_method == "capture_stock_evidence" + && e.ordinary_inventory_owner + .capture_drain_uses_explicit_reconciliation + && e.ordinary_inventory_owner + .retained_capture_survives_observer_loss + && e.ordinary_inventory_owner.failed_capture_blocks_fresh_work + && e.ordinary_inventory_owner.failed_capture_blocks_disposal + && e.ordinary_inventory_owner + .capture_preserves_actual_error_code + && e.ordinary_inventory_owner.pending_capture_retry == "manual_reconciliation" + && e.ordinary_inventory_owner + .capture_reports_must_match_original + && !e + .ordinary_inventory_owner + .capture_scope_metadata_renews_issuance && e.ordinary_inventory_owner.owned_acceptance_method == "prepare_stock_acceptance" && e.ordinary_inventory_owner.acceptance_is_preparation_only && e.ordinary_inventory_owner @@ -1391,6 +1434,23 @@ mod tests { ("capture_worker_charge_bytes", json!(16384)), ("capture_raw_max_bytes", json!(65537)), ("shared_capture_port", json!("arbitrary_transaction")), + ("shared_reconciliation_port", json!("blind_capture_retry")), + ( + "reconciliation_retains_writer_through_replacement", + json!(false), + ), + ( + "reconciliation_authenticates_before_connection_close", + json!(false), + ), + ( + "reconciliation_preflight_initializes_or_migrates", + json!(true), + ), + ( + "reconciliation_clears_fence_only_after_confirmed_capture", + json!(false), + ), ( "shared_capture_model_owner", json!("radroots_storage_sqlite"), @@ -1492,6 +1552,15 @@ mod tests { fn ordinary_inventory_keeps_selected_owner_and_accepted_task_lifetime() { for (field, bad) in [ ("owned_acceptance_method", json!("inline_fallback")), + ("owned_capture_method", json!("drop_after_refusal")), + ("capture_drain_uses_explicit_reconciliation", json!(false)), + ("retained_capture_survives_observer_loss", json!(false)), + ("failed_capture_blocks_fresh_work", json!(false)), + ("failed_capture_blocks_disposal", json!(false)), + ("capture_preserves_actual_error_code", json!(false)), + ("pending_capture_retry", json!("new_signing_attempt")), + ("capture_reports_must_match_original", json!(false)), + ("capture_scope_metadata_renews_issuance", json!(true)), ("acceptance_is_preparation_only", json!(false)), ("lost_accepted_admission_is_unknown", json!(false)), ("execution", json!("application")),