client_commerce.rs (13789B)
1 //! Client-owned acceptance tasks; caller observation is not execution ownership. 2 use super::{ClientSession, SessionHost, Shared}; 3 use crate::{ 4 Error, OperationContext, 5 session::{ 6 HostBound, 7 command::{RunMode, SigningResources}, 8 commerce::*, 9 }, 10 workspace::{CommandContext, WorkspaceSession}, 11 }; 12 use alloc::boxed::Box; 13 use radroots_domain::{CommandDigest, OperationId}; 14 use radroots_protocol::command::{ 15 AcceptOrderInput, CapturedOrderAcceptance, CapturedTermsInstallation, InstallTermsInput, 16 }; 17 use radroots_storage::{ 18 command::OperationError, 19 inventory::{ 20 InventoryCheckpointRef, InventoryStore, StockAcceptanceOutcome, StockAcceptanceRequest, 21 }, 22 workspace::{ 23 WorkspaceBinding, 24 seller_stock::{StockListingCheckpointRef, StockSourceCheckpointRef}, 25 }, 26 }; 27 28 impl<H: SessionHost + 'static> ClientSession<H> { 29 /// Owns original acceptance through T0/T1/S/T2 and bounded original delivery. 30 /// Inspect independent signing, installation and per-recipient relay facts; 31 /// relay acceptance never establishes buyer receipt or stock release. 32 /// Missing IO leaves actual preparation pending without signing. 33 #[allow(clippy::too_many_arguments)] 34 pub async fn accept_stock_order( 35 &self, 36 inventory: Shared<dyn InventoryStore>, 37 store: Shared<dyn AcceptanceStore>, 38 scope: &WorkspaceSession, 39 context: CommandContext, 40 minimum: InventoryCheckpointRef, 41 sources: StockSourceCheckpointRef, 42 listings: StockListingCheckpointRef, 43 input: AcceptOrderInput, 44 ) -> crate::ExecutionResult<AcceptanceStatus> { 45 let operation = context.data().operation_id(); 46 if !scope.owns_context(&context) { 47 return Err(Error::operation( 48 OperationError::Unauthorized, 49 OperationContext::proposed(Some(operation)), 50 ) 51 .into()); 52 } 53 self.submit_acceptance( 54 inventory, 55 store, 56 scope, 57 operation, 58 RunMode::Command, 59 move |binding| { 60 let command = CapturedOrderAcceptance::new(context.data().clone(), input) 61 .map_err(|_| OperationError::InvalidCommand)?; 62 let digest = command.digest(); 63 let request = StockAcceptanceRequest::capture( 64 binding.clone(), 65 minimum, 66 sources, 67 listings, 68 &command, 69 )?; 70 Ok(( 71 AcceptanceSelection { 72 binding, 73 minimum, 74 operation, 75 digest, 76 }, 77 Some(request), 78 )) 79 }, 80 ) 81 .await 82 } 83 /// Installs complete mutually authorized replacement terms through the 84 /// same owned T0/T1/S/T2 and original delivery/recovery pipeline. Pending 85 /// preparation preserves the old agreement and its original obligations. 86 #[allow(clippy::too_many_arguments)] 87 pub async fn install_stock_terms( 88 &self, 89 inventory: Shared<dyn InventoryStore>, 90 store: Shared<dyn AcceptanceStore>, 91 scope: &WorkspaceSession, 92 context: CommandContext, 93 minimum: InventoryCheckpointRef, 94 sources: StockSourceCheckpointRef, 95 listings: StockListingCheckpointRef, 96 input: InstallTermsInput, 97 ) -> crate::ExecutionResult<AcceptanceStatus> { 98 let operation = context.data().operation_id(); 99 if !scope.owns_context(&context) { 100 return Err(Error::operation( 101 OperationError::Unauthorized, 102 OperationContext::proposed(Some(operation)), 103 ) 104 .into()); 105 } 106 self.submit_acceptance( 107 inventory, 108 store, 109 scope, 110 operation, 111 RunMode::Command, 112 move |binding| { 113 let command = CapturedTermsInstallation::new(context.data().clone(), input) 114 .map_err(|_| OperationError::InvalidCommand)?; 115 let digest = command.digest(); 116 let request = StockAcceptanceRequest::capture_terms( 117 binding.clone(), 118 minimum, 119 sources, 120 listings, 121 &command, 122 )?; 123 Ok(( 124 AcceptanceSelection { 125 binding, 126 minimum, 127 operation, 128 digest, 129 }, 130 Some(request), 131 )) 132 }, 133 ) 134 .await 135 } 136 /// Resumes only original retained inputs. Non-replayable existing claims 137 /// remain unknown; a qualified original local replay requires its actual 138 /// current host permit and original deadline. This never releases stock. 139 #[allow(clippy::too_many_arguments)] 140 pub async fn resume_stock_acceptance( 141 &self, 142 inventory: Shared<dyn InventoryStore>, 143 store: Shared<dyn AcceptanceStore>, 144 scope: &WorkspaceSession, 145 minimum: InventoryCheckpointRef, 146 operation: OperationId, 147 digest: CommandDigest, 148 ) -> crate::ExecutionResult<AcceptanceStatus> { 149 self.submit_acceptance( 150 inventory, 151 store, 152 scope, 153 operation, 154 RunMode::Resume, 155 move |binding| { 156 Ok(( 157 AcceptanceSelection { 158 binding, 159 minimum, 160 operation, 161 digest, 162 }, 163 None, 164 )) 165 }, 166 ) 167 .await 168 } 169 /// Reads original stock acceptance facts without signing or replaying work. 170 pub async fn stock_acceptance_status( 171 &self, 172 store: Shared<dyn AcceptanceStore>, 173 scope: &WorkspaceSession, 174 minimum: InventoryCheckpointRef, 175 operation: OperationId, 176 digest: CommandDigest, 177 ) -> crate::Result<AcceptanceStatus> { 178 self.run_stock( 179 scope, 180 OperationContext::retained(operation), 181 move |binding, proof| { 182 Box::pin(async move { 183 let selection = AcceptanceSelection { 184 binding, 185 minimum, 186 operation, 187 digest, 188 }; 189 let view = store.snapshot(selection.clone(), &proof).await?; 190 crate::session::commerce::observe(&selection, view.as_ref()) 191 }) 192 }, 193 ) 194 .await 195 } 196 async fn submit_acceptance( 197 &self, 198 inventory: Shared<dyn InventoryStore>, 199 store: Shared<dyn AcceptanceStore>, 200 scope: &WorkspaceSession, 201 operation: OperationId, 202 mode: RunMode, 203 capture: impl FnOnce( 204 WorkspaceBinding, 205 ) -> Result< 206 (AcceptanceSelection, Option<StockAcceptanceRequest>), 207 OperationError, 208 >, 209 ) -> crate::ExecutionResult<AcceptanceStatus> { 210 let proposed = OperationContext::proposed(Some(operation)); 211 self.submit_stock_commerce( 212 scope, 213 operation, 214 mode, 215 capture, 216 move |(selection, request): (AcceptanceSelection, Option<StockAcceptanceRequest>), 217 proof, 218 runtime, 219 resources| { 220 Box::pin(async move { 221 if let Some(request) = request { 222 let credential = proof 223 .with_secret(|bytes| { 224 radroots_storage::workspace::AttachmentProof::from_protected_input( 225 bytes, 226 ) 227 }) 228 .map_err(|e| Error::operation(e.into(), proposed))?; 229 match inventory 230 .prepare_stock_acceptance(request, credential) 231 .await 232 .map_err(|e| Error::operation(e, proposed))? 233 { 234 StockAcceptanceOutcome::Unknown(_) => { 235 return Err(Error::admission_uncertain(operation).into()); 236 } 237 StockAcceptanceOutcome::Prepared(value) 238 if value.attempt().workspace_id() 239 == selection.binding.input().workspace_id() 240 && value.attempt().operation_id() == operation 241 && value.attempt().command_digest() == selection.digest 242 && value.attempt().transaction_id().is_some() => {} 243 StockAcceptanceOutcome::Aborted(value) 244 if value.attempt().workspace_id() 245 == selection.binding.input().workspace_id() 246 && value.attempt().operation_id() == operation 247 && value.attempt().command_digest() == selection.digest 248 && value.attempt().transaction_id().is_some() => {} 249 _ => { 250 return Err(Error::operation( 251 OperationError::RecoveryRequired, 252 OperationContext::admission_unknown(operation), 253 ) 254 .into()); 255 } 256 } 257 } 258 if let Some(runtime) = runtime { 259 runtime 260 .run_acceptance(store, inventory, selection, &proof, resources, mode) 261 .await 262 } else { 263 let retained = OperationContext::retained(operation); 264 let view = store 265 .snapshot(selection.clone(), &proof) 266 .await 267 .map_err(|e| Error::operation(e, retained))?; 268 crate::session::commerce::observe(&selection, view.as_ref()) 269 .map_err(|e| Error::operation(e, retained).into()) 270 } 271 }) 272 }, 273 ) 274 .await 275 } 276 /// One owned lifecycle path for initial acceptance and explicit rewrapping. 277 /// Caller observation never owns the admitted task or obtained materials. 278 pub(super) async fn submit_stock_commerce<P: HostBound + 'static, T: HostBound + 'static>( 279 &self, 280 scope: &WorkspaceSession, 281 operation: OperationId, 282 mode: RunMode, 283 prepare: impl FnOnce(WorkspaceBinding) -> Result<P, OperationError>, 284 work: impl FnOnce( 285 P, 286 radroots_storage::workspace::AttachmentProof, 287 Option<Shared<crate::session::command::CommandRuntime>>, 288 SigningResources, 289 ) -> crate::session::HostFuture<'static, crate::ExecutionResult<T>>, 290 ) -> crate::ExecutionResult<T> { 291 let proposed = OperationContext::proposed(Some(operation)); 292 if matches!(mode, RunMode::Command) { 293 self.stock_captures 294 .ensure_ready() 295 .map_err(|e| Error::operation(e, proposed))?; 296 } 297 let permit = self.session.admit().map_err(|e| e.with_context(proposed))?; 298 let budget = self 299 .task_budget 300 .reserve() 301 .map_err(|e| e.with_context(proposed))?; 302 { 303 let scopes = self 304 .workspaces 305 .lock() 306 .unwrap_or_else(std::sync::PoisonError::into_inner); 307 if !scopes.iter().any(|v| v.same_scope(scope)) { 308 return Err(Error::operation(OperationError::Unauthorized, proposed).into()); 309 } 310 } 311 let host = self 312 .task_host 313 .as_ref() 314 .ok_or_else(|| Error::operation(OperationError::UnsupportedOperation, proposed))?; 315 let (binding, proof) = scope 316 .captured_input() 317 .map_err(|e| Error::operation(e.into(), proposed))?; 318 let input = prepare(binding).map_err(|e| Error::operation(e, proposed))?; 319 let resources = SigningResources { 320 signer: self.signer_session(), 321 lease: Shared::new((permit, budget)) as Shared<dyn HostBound>, 322 stock_captures: self.stock_captures.clone(), 323 }; 324 let captures = self.stock_captures.clone(); 325 let future = work(input, proof, self.commands.clone(), resources); 326 let (reply, observed) = futures::channel::oneshot::channel(); 327 host.submit(Box::pin(async move { 328 let result = async { 329 if matches!(mode, RunMode::Resume) { 330 captures.drain().await?; 331 } else { 332 captures 333 .ensure_ready() 334 .map_err(|e| Error::operation(e, proposed))?; 335 } 336 future.await 337 } 338 .await; 339 let _ = reply.send(result); 340 })) 341 .map_err(|e| e.with_context(proposed))?; 342 observed 343 .await 344 .unwrap_or_else(|_| Err(Error::admission_uncertain(operation).into())) 345 } 346 }