client_commands.rs (13752B)
1 use super::{ClientSession, SessionHost, Shared}; 2 use crate::session::{ 3 HostBound, 4 command::{CommandRuntime, SigningResources}, 5 }; 6 use crate::{ 7 Error, OperationContext, OperationReceipt, OperationStatus, 8 workspace::{CommandContext, WorkspaceSession}, 9 }; 10 use alloc::boxed::Box; 11 use radroots_domain::{ChatMessage, CommandDigest, OperationId, listing::Listing}; 12 use radroots_protocol::{ 13 command::{CapturedChatCommand, CapturedCommand, CommandError, InquireInput}, 14 listing::{CapturedListingCommand, PublishInput}, 15 }; 16 use radroots_storage::command::{ 17 AdmissionOutcome, AdmissionRequest, OperationError, OperationJournal, OperationSnapshot, 18 }; 19 20 type ReceiptProjection<T> = fn(&OperationSnapshot) -> Result<OperationReceipt<T>, OperationError>; 21 22 impl<H: SessionHost + 'static> ClientSession<H> { 23 /// Explicitly resumes an original command under current attached access. 24 /// Original claims are never signed again; delivery retries preserve exact 25 /// retained outer bytes and the first publication criterion, selecting only 26 /// targets without recorded acceptance. No first-issue deadline is renewed. 27 pub async fn resume_command( 28 &self, 29 journal: Shared<dyn OperationJournal>, 30 scope: &WorkspaceSession, 31 operation: OperationId, 32 expected: Option<CommandDigest>, 33 ) -> crate::ExecutionResult<OperationStatus> { 34 let proposed = OperationContext::proposed(Some(operation)); 35 let permit = self 36 .session 37 .admit() 38 .map_err(|error| error.with_context(proposed))?; 39 let budget = self 40 .task_budget 41 .reserve() 42 .map_err(|error| error.with_context(proposed))?; 43 { 44 let scopes = self 45 .workspaces 46 .lock() 47 .unwrap_or_else(std::sync::PoisonError::into_inner); 48 if !scopes.iter().any(|owned| owned.same_scope(scope)) { 49 return Err(Error::operation(OperationError::Unauthorized, proposed).into()); 50 } 51 } 52 let (binding, proof) = scope 53 .captured_input() 54 .map_err(|error| Error::operation(error.into(), proposed))?; 55 let runtime = self 56 .commands 57 .clone() 58 .ok_or_else(|| Error::operation(OperationError::Unauthorized, proposed))?; 59 let host = self 60 .task_host 61 .as_ref() 62 .ok_or_else(|| Error::operation(OperationError::Unauthorized, proposed))?; 63 let signer = self.signer_session(); 64 let stock_captures = self.stock_captures.clone(); 65 let lease: Shared<dyn HostBound> = Shared::new((permit, budget)); 66 let (reply, observed) = futures::channel::oneshot::channel(); 67 host.submit(Box::pin(async move { 68 let result = async { 69 runtime.drain().await?; 70 let prepared = journal 71 .operation( 72 binding.clone(), 73 operation, 74 expected, 75 proof 76 .with_secret(|secret| { 77 radroots_storage::workspace::AttachmentProof::from_protected_input( 78 secret, 79 ) 80 }) 81 .map_err(|error| Error::operation(error.into(), proposed))?, 82 ) 83 .await 84 .map_err(|error| Error::operation(error, proposed))? 85 .ok_or_else(|| Error::operation(OperationError::MissingHistory, proposed))?; 86 let retained = OperationContext::retained(operation); 87 let digest = prepared.identity().command_digest(); 88 runtime 89 .run( 90 journal.clone(), 91 prepared, 92 &proof, 93 SigningResources { 94 signer, 95 lease, 96 stock_captures, 97 }, 98 crate::session::command::RunMode::Resume, 99 ) 100 .await 101 .map_err(|error| error.into_failure(operation))?; 102 let snapshot = journal 103 .operation_snapshot(binding, operation, Some(digest), proof) 104 .await 105 .map_err(|error| Error::operation(error, retained))? 106 .ok_or_else(|| Error::operation(OperationError::RecoveryRequired, retained))?; 107 OperationStatus::try_from_snapshot(&snapshot) 108 .map_err(|error| Error::operation(error, retained).into()) 109 } 110 .await; 111 let _ = reply.send(result); 112 })) 113 .map_err(|error| error.with_context(proposed))?; 114 observed.await.unwrap_or_else(|_| { 115 Err(Error::operation( 116 OperationError::RecoveryRequired, 117 OperationContext::admission_unknown(operation), 118 ) 119 .into()) 120 }) 121 } 122 /// Captures and durably admits one inquiry through the configured journal. 123 /// The concrete bounded worker owns accepted T0 work after observer loss. 124 /// A pending receipt does not claim signing, installation or publication. 125 /// The journal is an explicit host trust boundary, not adapter qualification. 126 pub async fn inquire( 127 &self, 128 journal: Shared<dyn OperationJournal>, 129 context: CommandContext, 130 input: InquireInput, 131 ) -> crate::ExecutionResult<OperationReceipt<ChatMessage>> { 132 self.submit_command( 133 journal, 134 context, 135 move |data| CapturedChatCommand::inquire(data, input).map(CapturedCommand::Chat), 136 OperationReceipt::<ChatMessage>::try_from_snapshot, 137 ) 138 .await 139 } 140 /// Captures an initial public listing under its actual attached seller 141 /// scope. The same owner retains T0/T1/S/T2/D after observer loss. 142 pub async fn publish_listing( 143 &self, 144 journal: Shared<dyn OperationJournal>, 145 context: CommandContext, 146 input: PublishInput, 147 ) -> crate::ExecutionResult<OperationReceipt<Listing>> { 148 self.submit_command( 149 journal, 150 context, 151 move |data| CapturedListingCommand::capture(data, input).map(CapturedCommand::Listing), 152 OperationReceipt::<Listing>::try_from_snapshot, 153 ) 154 .await 155 } 156 async fn submit_command<T: HostBound + 'static, F>( 157 &self, 158 journal: Shared<dyn OperationJournal>, 159 context: CommandContext, 160 capture: F, 161 project: ReceiptProjection<T>, 162 ) -> crate::ExecutionResult<OperationReceipt<T>> 163 where 164 F: FnOnce(radroots_domain::CommandContextData) -> Result<CapturedCommand, CommandError>, 165 { 166 let operation = context.data().operation_id(); 167 let proposed = OperationContext::proposed(Some(operation)); 168 self.stock_captures 169 .ensure_ready() 170 .map_err(|error| Error::operation(error, proposed))?; 171 let permit = self 172 .session 173 .admit() 174 .map_err(|error| error.with_context(proposed))?; 175 let budget = if self.task_host.is_some() || self.commands.is_some() { 176 Some( 177 self.task_budget 178 .reserve() 179 .map_err(|error| error.with_context(proposed))?, 180 ) 181 } else { 182 None 183 }; 184 { 185 let scopes = self 186 .workspaces 187 .lock() 188 .unwrap_or_else(std::sync::PoisonError::into_inner); 189 if !scopes.iter().any(|scope| scope.owns_context(&context)) { 190 return Err(Error::operation(OperationError::Unauthorized, proposed).into()); 191 } 192 } 193 let (binding, proof) = context 194 .captured_input() 195 .map_err(|error| Error::operation(error.into(), proposed))?; 196 let command = capture(context.data().clone()) 197 .map_err(|_| Error::operation(OperationError::InvalidCommand, proposed))?; 198 let request = AdmissionRequest::new(binding, command) 199 .map_err(|error| Error::operation(error, proposed))?; 200 let runtime = self.commands.clone(); 201 let signer = self.signer_session(); 202 let stock_captures = self.stock_captures.clone(); 203 let lease: Shared<dyn HostBound> = Shared::new((permit, budget)); 204 if let Some(host) = &self.task_host { 205 let (reply, observed) = futures::channel::oneshot::channel(); 206 let task = Box::pin(async move { 207 let result = execute_command( 208 journal, 209 request, 210 proof, 211 runtime, 212 SigningResources { 213 signer, 214 lease, 215 stock_captures, 216 }, 217 project, 218 ) 219 .await; 220 let _ = reply.send(result); 221 }); 222 host.submit(task) 223 .map_err(|error| error.with_context(proposed))?; 224 observed 225 .await 226 .unwrap_or_else(|_| Err(Error::admission_uncertain(operation).into())) 227 } else { 228 execute_command( 229 journal, 230 request, 231 proof, 232 runtime, 233 SigningResources { 234 signer, 235 lease, 236 stock_captures, 237 }, 238 project, 239 ) 240 .await 241 } 242 } 243 } 244 245 impl<H: SessionHost> ClientSession<H> { 246 /// Reads the original complete operation through a scope attached to this 247 /// client. Query time never renews the original first-issue deadline and 248 /// absence never proves an uncertain invocation was safely unissued. 249 pub async fn operation( 250 &self, 251 journal: Shared<dyn OperationJournal>, 252 scope: &WorkspaceSession, 253 operation: OperationId, 254 expected_digest: Option<CommandDigest>, 255 ) -> crate::Result<Option<OperationStatus>> { 256 let proposed = OperationContext::proposed(Some(operation)); 257 let _permit = self 258 .session 259 .admit() 260 .map_err(|error| error.with_context(proposed))?; 261 { 262 let scopes = self 263 .workspaces 264 .lock() 265 .unwrap_or_else(std::sync::PoisonError::into_inner); 266 if !scopes.iter().any(|owned| owned.same_scope(scope)) { 267 return Err(Error::operation(OperationError::Unauthorized, proposed)); 268 } 269 } 270 let (binding, proof) = scope 271 .captured_input() 272 .map_err(|error| Error::operation(error.into(), proposed))?; 273 let snapshot = journal 274 .operation_snapshot(binding, operation, expected_digest, proof) 275 .await 276 .map_err(|error| Error::operation(error, proposed))?; 277 snapshot 278 .as_ref() 279 .map(OperationStatus::try_from_snapshot) 280 .transpose() 281 .map_err(|error| Error::operation(error, OperationContext::retained(operation))) 282 } 283 } 284 285 async fn execute_command<T: HostBound + 'static>( 286 journal: Shared<dyn OperationJournal>, 287 request: AdmissionRequest, 288 proof: radroots_storage::workspace::AttachmentProof, 289 runtime: Option<Shared<CommandRuntime>>, 290 resources: SigningResources, 291 project: ReceiptProjection<T>, 292 ) -> crate::ExecutionResult<OperationReceipt<T>> { 293 let operation = request.command().context().operation_id(); 294 let proposed = OperationContext::proposed(Some(operation)); 295 resources 296 .stock_captures 297 .ensure_ready() 298 .map_err(|error| Error::operation(error, proposed))?; 299 let binding = request.binding().clone(); 300 let digest = request.command().digest(); 301 // Original lookup precedes fresh deadline/planning. Authentication of the 302 // actual transaction remains native; a captured descriptor grants nothing. 303 let query_proof = proof 304 .with_secret(|secret| { 305 radroots_storage::workspace::AttachmentProof::from_protected_input(secret) 306 }) 307 .map_err(|error| Error::operation(error.into(), proposed))?; 308 let result = journal 309 .admit_command_and_hold(request, proof) 310 .await 311 .map_err(|error| Error::operation(error, proposed))?; 312 match result { 313 AdmissionOutcome::Unknown(_) => Err(Error::admission_uncertain(operation).into()), 314 AdmissionOutcome::Committed(prepared) => { 315 let retained = OperationContext::retained(operation); 316 if let Some(runtime) = runtime { 317 runtime 318 .run( 319 journal.clone(), 320 prepared, 321 &query_proof, 322 resources, 323 crate::session::command::RunMode::Command, 324 ) 325 .await 326 .map_err(|error| error.into_failure(operation))?; 327 } 328 let snapshot = journal 329 .operation_snapshot(binding, operation, Some(digest), query_proof) 330 .await 331 .map_err(|error| Error::operation(error, retained))? 332 .ok_or_else(|| Error::operation(OperationError::RecoveryRequired, retained))?; 333 project(&snapshot).map_err(|error| Error::operation(error, retained).into()) 334 } 335 } 336 }