receipt_client.rs (17449B)
1 use super::*; 2 use radroots::{ 3 Client, CommandContext, NativeConfig, StartMode, WorkspaceSession, inbox::InquireInput, 4 }; 5 use radroots_domain::{ChatMessage, ThreadId}; 6 use radroots_execution::{ 7 Admission, BusinessInstallation, Code, Delivery, Issuance, OperationReceipt, 8 RecoveryRequirement, 9 }; 10 use radroots_storage::event::SourceGeneration; 11 use radroots_storage_sqlite::{OpenMode, OpenOptions, Paths}; 12 13 struct Ordinary { 14 fixture: Fixture, 15 store: Arc<NativeWorkspace>, 16 client: Client, 17 scope: WorkspaceSession, 18 input: AttachmentInput, 19 proof: AttachmentProof, 20 } 21 impl Ordinary { 22 async fn new() -> Self { 23 let fixture = Fixture::new(); 24 let (store, input, proof, _) = fixture.create().await; 25 let cache = fixture.path.parent().unwrap().join("cache"); 26 std::fs::create_dir(&cache).unwrap(); 27 let options = OpenOptions::new(Paths::from_directory(cache).unwrap(), OpenMode::Create) 28 .with_source_generation(SourceGeneration::new([17; 32]).unwrap(), 1) 29 .unwrap(); 30 let client = 31 Client::open(NativeConfig::public_read(options).with_workspace(store.as_ref().clone())) 32 .await 33 .unwrap(); 34 let scope = client.attach(input.clone(), copy(&proof)).await.unwrap(); 35 Self { 36 fixture, 37 store, 38 client, 39 scope, 40 input, 41 proof, 42 } 43 } 44 fn context(&self, id: u8, correlation: &str) -> CommandContext { 45 self.scope 46 .command_context_with_correlation( 47 OperationId::from_bytes([id; 32]), 48 None, 49 None, 50 UnixSeconds::new(150).unwrap(), 51 Some(CorrelationId::new(correlation).unwrap()), 52 ) 53 .unwrap() 54 } 55 async fn inquire( 56 &self, 57 id: u8, 58 text: &str, 59 correlation: &str, 60 ) -> radroots::ExecutionResult<OperationReceipt<ChatMessage>> { 61 self.client 62 .inbox() 63 .inquire(self.context(id, correlation), input(text)) 64 .await 65 } 66 async fn start(&self) { 67 self.client.start(StartMode::LocalOnly).await.unwrap(); 68 } 69 } 70 fn input(text: &str) -> InquireInput { 71 InquireInput::new(ThreadId::from_bytes([0x66; 32]), key(), None, text).unwrap() 72 } 73 74 #[tokio::test] 75 async fn ordinary_inquiry_confirms_actual_t0_and_current_pending_receipt_without_signing() { 76 let ready = Ordinary::new().await; 77 let before = ready 78 .inquire(91, "private ordinary inquiry", "first") 79 .await 80 .unwrap_err(); 81 assert_eq!(before.code(), Code::CapabilityUnavailable); 82 assert_eq!( 83 before.context().operation_id(), 84 Some(OperationId::from_bytes([91; 32])) 85 ); 86 assert_eq!(before.context().admission(), Admission::NotAdmitted); 87 ready.start().await; 88 let receipt = ready 89 .inquire(91, "private ordinary inquiry", "first") 90 .await 91 .unwrap(); 92 assert!(receipt.durable()); 93 assert_eq!( 94 receipt.business_installation(), 95 BusinessInstallation::NotInstalled 96 ); 97 assert!(receipt.business_result().is_none()); 98 assert_eq!(receipt.issuance(), Issuance::None); 99 assert_eq!(receipt.delivery(), Delivery::NotStarted); 100 assert_eq!( 101 receipt.recovery(), 102 &[RecoveryRequirement::ContinueOriginalOperation] 103 ); 104 assert!(receipt.plan_transaction_id().is_none()); 105 assert!( 106 receipt 107 .artifacts() 108 .iter() 109 .all(|artifact| artifact.attempt_id().is_none()) 110 ); 111 assert!(!format!("{receipt:?}").contains("private ordinary")); 112 let queried = ready 113 .client 114 .operations(&ready.scope) 115 .get_matching( 116 OperationId::from_bytes([91; 32]), 117 Some(receipt.command_digest()), 118 ) 119 .await 120 .unwrap() 121 .unwrap(); 122 assert_eq!( 123 queried.admission_transaction_id(), 124 receipt.admission_transaction_id() 125 ); 126 assert_eq!( 127 queried.operation_checkpoint(), 128 receipt.operation_checkpoint() 129 ); 130 let actual = ready 131 .store 132 .operation_snapshot( 133 ready.scope.binding().clone(), 134 OperationId::from_bytes([91; 32]), 135 None, 136 copy(&ready.proof), 137 ) 138 .await 139 .unwrap() 140 .unwrap(); 141 assert_eq!( 142 actual.prepared().request().chat_command().unwrap().text(), 143 "private ordinary inquiry" 144 ); 145 assert!(actual.bound_plan().is_none()); 146 assert!(actual.delivery_attempts().is_empty()); 147 ready.client.close().await.unwrap(); 148 } 149 150 #[tokio::test] 151 async fn ordinary_replay_preserves_original_correlation_and_window_after_deadline() { 152 let ready = Ordinary::new().await; 153 ready.start().await; 154 let receipt = ready 155 .inquire(92, "retained inquiry", "first") 156 .await 157 .unwrap(); 158 ready.fixture.clock.now.store(151, Ordering::SeqCst); 159 let replay = ready 160 .inquire(92, "retained inquiry", "replacement") 161 .await 162 .unwrap(); 163 assert_eq!( 164 replay.admission_transaction_id(), 165 receipt.admission_transaction_id() 166 ); 167 assert_eq!(replay.issuance_window(), receipt.issuance_window()); 168 assert_eq!(replay.context().correlation_id().unwrap().as_str(), "first"); 169 assert_eq!(replay.command_digest(), receipt.command_digest()); 170 assert_eq!( 171 replay.operation_checkpoint(), 172 receipt.operation_checkpoint() 173 ); 174 assert_eq!(replay.error().unwrap().code(), Code::DeadlineExceeded); 175 let conflict = ready 176 .inquire(92, "changed private text", "replacement") 177 .await 178 .unwrap_err(); 179 assert_eq!(conflict.code(), Code::IdempotencyConflict); 180 assert_eq!( 181 conflict.context().operation_id(), 182 Some(OperationId::from_bytes([92; 32])) 183 ); 184 let new = ready 185 .inquire(93, "new expired inquiry", "first") 186 .await 187 .unwrap_err(); 188 assert_eq!(new.code(), Code::DeadlineExceeded); 189 assert!( 190 ready 191 .client 192 .operations(&ready.scope) 193 .get(OperationId::from_bytes([93; 32])) 194 .await 195 .unwrap() 196 .is_none() 197 ); 198 let mismatch = ready 199 .client 200 .operations(&ready.scope) 201 .get_matching( 202 OperationId::from_bytes([92; 32]), 203 Some(CommandDigest::from_bytes([0x55; 32])), 204 ) 205 .await 206 .unwrap_err(); 207 assert_eq!(mismatch.code(), Code::IdempotencyConflict); 208 ready.client.close().await.unwrap(); 209 } 210 211 #[tokio::test] 212 async fn ordinary_context_must_belong_to_this_client_and_remain_open_and_authorized() { 213 let ready = Ordinary::new().await; 214 ready.start().await; 215 let other_workspace = Ordinary::new().await; 216 other_workspace.start().await; 217 assert_eq!( 218 other_workspace 219 .client 220 .inbox() 221 .inquire(ready.context(94, "other journal"), input("private")) 222 .await 223 .unwrap_err() 224 .code(), 225 Code::Unauthorized 226 ); 227 assert!( 228 other_workspace 229 .client 230 .operations(&other_workspace.scope) 231 .get(OperationId::from_bytes([94; 32])) 232 .await 233 .unwrap() 234 .is_none() 235 ); 236 other_workspace.client.close().await.unwrap(); 237 let other = Client::preview(); 238 other.start(StartMode::LocalOnly).await.unwrap(); 239 assert_eq!( 240 other 241 .inbox() 242 .inquire(ready.context(94, "wrong client"), input("private")) 243 .await 244 .unwrap_err() 245 .code(), 246 Code::MissingAuthority 247 ); 248 // The same persisted credential in a separately attached scope must not 249 // bypass this client's attachment registry, even with the exact same store. 250 let foreign = 251 WorkspaceSession::attach(ready.store.clone(), ready.input.clone(), copy(&ready.proof)) 252 .await 253 .unwrap(); 254 let context = foreign 255 .command_context( 256 OperationId::from_bytes([94; 32]), 257 None, 258 None, 259 UnixSeconds::new(150).unwrap(), 260 ) 261 .unwrap(); 262 assert_eq!( 263 ready 264 .client 265 .inbox() 266 .inquire(context, input("private")) 267 .await 268 .unwrap_err() 269 .code(), 270 Code::Unauthorized 271 ); 272 assert_eq!( 273 ready 274 .client 275 .operations(&foreign) 276 .get(OperationId::from_bytes([94; 32])) 277 .await 278 .unwrap_err() 279 .code(), 280 Code::Unauthorized 281 ); 282 assert!( 283 ready 284 .client 285 .operations(&ready.scope) 286 .get(OperationId::from_bytes([94; 32])) 287 .await 288 .unwrap() 289 .is_none() 290 ); 291 let closed_context = ready.context(94, "closed"); 292 ready.scope.close(); 293 assert_eq!( 294 ready 295 .client 296 .inbox() 297 .inquire(closed_context, input("private")) 298 .await 299 .unwrap_err() 300 .code(), 301 Code::CapabilityUnavailable 302 ); 303 let scope = ready 304 .client 305 .attach(ready.input.clone(), copy(&ready.proof)) 306 .await 307 .unwrap(); 308 let context = scope 309 .command_context( 310 OperationId::from_bytes([94; 32]), 311 None, 312 None, 313 UnixSeconds::new(150).unwrap(), 314 ) 315 .unwrap(); 316 ready 317 .store 318 .revoke(ready.input.clone(), &ready.proof) 319 .await 320 .unwrap(); 321 assert_eq!( 322 ready 323 .client 324 .inbox() 325 .inquire(context, input("private")) 326 .await 327 .unwrap_err() 328 .code(), 329 Code::GrantRevoked 330 ); 331 assert_eq!( 332 ready 333 .client 334 .operations(&scope) 335 .get(OperationId::from_bytes([94; 32])) 336 .await 337 .unwrap_err() 338 .code(), 339 Code::GrantRevoked 340 ); 341 ready.client.close().await.unwrap(); 342 other.close().await.unwrap(); 343 } 344 345 #[tokio::test] 346 async fn accepted_ordinary_inquiry_survives_observer_loss_and_client_close_drains_native_t0() { 347 use std::{ 348 future::Future, 349 task::{Context, Poll}, 350 }; 351 let ready = Ordinary::new().await; 352 ready.start().await; 353 let inbox = ready.client.inbox(); 354 let (pause, release) = ready.fixture.clock.arm(0); 355 let mut wait = Box::pin(inbox.inquire( 356 ready.context(95, "lost observer"), 357 input("retained after wait loss"), 358 )); 359 assert!(matches!( 360 wait.as_mut() 361 .poll(&mut Context::from_waker(futures::task::noop_waker_ref())), 362 Poll::Pending 363 )); 364 reached(pause).await; 365 drop(wait); 366 let mut close = Box::pin(ready.client.close()); 367 assert!(matches!( 368 close 369 .as_mut() 370 .poll(&mut Context::from_waker(futures::task::noop_waker_ref())), 371 Poll::Pending 372 )); 373 release.send(()).unwrap(); 374 close.await.unwrap(); 375 let reopened = ready.fixture.reopen(ready.input.clone()).await; 376 let current = reopened 377 .authenticate(ready.input.clone(), &ready.proof) 378 .await 379 .unwrap(); 380 let snapshot = reopened 381 .operation_snapshot( 382 current, 383 OperationId::from_bytes([95; 32]), 384 None, 385 copy(&ready.proof), 386 ) 387 .await 388 .unwrap() 389 .unwrap(); 390 assert_eq!( 391 snapshot.prepared().request().chat_command().unwrap().text(), 392 "retained after wait loss" 393 ); 394 assert!(snapshot.bound_plan().is_none()); 395 assert!(snapshot.publications().is_empty()); 396 reopened.close().await.unwrap(); 397 } 398 399 #[tokio::test] 400 async fn unpolled_ordinary_inquiry_and_post_close_admission_have_no_effect() { 401 let ready = Ordinary::new().await; 402 ready.start().await; 403 let inbox = ready.client.inbox(); 404 drop(inbox.inquire(ready.context(96, "not polled"), input("not submitted"))); 405 assert!( 406 ready 407 .client 408 .operations(&ready.scope) 409 .get(OperationId::from_bytes([96; 32])) 410 .await 411 .unwrap() 412 .is_none() 413 ); 414 let context = ready.context(96, "after close"); 415 ready.client.close().await.unwrap(); 416 let error = inbox 417 .inquire(context, input("not submitted")) 418 .await 419 .unwrap_err(); 420 assert_eq!(error.code(), Code::CapabilityUnavailable); 421 assert_eq!(error.context().admission(), Admission::NotAdmitted); 422 assert!( 423 ready 424 .client 425 .operations(&ready.scope) 426 .get(OperationId::from_bytes([96; 32])) 427 .await 428 .is_err() 429 ); 430 } 431 432 #[tokio::test] 433 async fn ordinary_owned_budget_survives_lost_observers_and_all_accepted_commands_drain() { 434 use std::{ 435 future::Future, 436 task::{Context, Poll}, 437 }; 438 let ready = Ordinary::new().await; 439 ready.start().await; 440 let inbox = ready.client.inbox(); 441 let (paused, release) = ready.fixture.clock.arm(0); 442 let mut first = Box::pin(inbox.inquire( 443 ready.context(97, "first owned"), 444 input("first owned command"), 445 )); 446 assert!(matches!( 447 first 448 .as_mut() 449 .poll(&mut Context::from_waker(futures::task::noop_waker_ref())), 450 Poll::Pending 451 )); 452 reached(paused).await; 453 let mut second = Box::pin(inbox.inquire( 454 ready.context(98, "second owned"), 455 input("second owned command"), 456 )); 457 assert!(matches!( 458 second 459 .as_mut() 460 .poll(&mut Context::from_waker(futures::task::noop_waker_ref())), 461 Poll::Pending 462 )); 463 drop(first); 464 drop(second); 465 let copy_client = ready.client.clone(); 466 let error = copy_client 467 .inbox() 468 .inquire(ready.context(99, "over quota"), input("not accepted")) 469 .await 470 .unwrap_err(); 471 assert_eq!(error.code(), Code::CapabilityUnavailable); 472 assert_eq!( 473 error.context().operation_id(), 474 Some(OperationId::from_bytes([99; 32])) 475 ); 476 assert_eq!(error.context().admission(), Admission::NotAdmitted); 477 let mut close = Box::pin(ready.client.close()); 478 assert!(matches!( 479 close 480 .as_mut() 481 .poll(&mut Context::from_waker(futures::task::noop_waker_ref())), 482 Poll::Pending 483 )); 484 release.send(()).unwrap(); 485 close.await.unwrap(); 486 let reopened = ready.fixture.reopen(ready.input.clone()).await; 487 let binding = reopened 488 .authenticate(ready.input.clone(), &ready.proof) 489 .await 490 .unwrap(); 491 for (id, body) in [(97, "first owned command"), (98, "second owned command")] { 492 let snapshot = reopened 493 .operation_snapshot( 494 binding.clone(), 495 OperationId::from_bytes([id; 32]), 496 None, 497 copy(&ready.proof), 498 ) 499 .await 500 .unwrap() 501 .unwrap(); 502 assert_eq!( 503 snapshot.prepared().request().chat_command().unwrap().text(), 504 body 505 ); 506 } 507 assert!( 508 reopened 509 .operation_snapshot( 510 binding, 511 OperationId::from_bytes([99; 32]), 512 None, 513 copy(&ready.proof) 514 ) 515 .await 516 .unwrap() 517 .is_none() 518 ); 519 reopened.close().await.unwrap(); 520 } 521 522 #[tokio::test] 523 async fn owned_inquiry_continues_to_its_coherent_query_after_the_caller_disappears() { 524 use std::{ 525 future::Future, 526 task::{Context, Poll}, 527 }; 528 let ready = Ordinary::new().await; 529 ready.start().await; 530 let inbox = ready.client.inbox(); 531 let (first_paused, release_first) = ready.fixture.clock.arm(0); 532 let mut wait = Box::pin(inbox.inquire( 533 ready.context(100, "lost before admission reply"), 534 input("independent owner continuation"), 535 )); 536 assert!(matches!( 537 wait.as_mut() 538 .poll(&mut Context::from_waker(futures::task::noop_waker_ref())), 539 Poll::Pending 540 )); 541 reached(first_paused).await; 542 drop(wait); 543 // After the first access observation, T0's fence consumes one clock read; 544 // the coherent query's access is the next actual boundary. 545 let (query_paused, release_query) = ready.fixture.clock.arm(1); 546 release_first.send(()).unwrap(); 547 reached(query_paused).await; 548 let mut close = Box::pin(ready.client.close()); 549 assert!(matches!( 550 close 551 .as_mut() 552 .poll(&mut Context::from_waker(futures::task::noop_waker_ref())), 553 Poll::Pending 554 )); 555 release_query.send(()).unwrap(); 556 close.await.unwrap(); 557 let reopened = ready.fixture.reopen(ready.input.clone()).await; 558 let binding = reopened 559 .authenticate(ready.input.clone(), &ready.proof) 560 .await 561 .unwrap(); 562 let snapshot = reopened 563 .operation_snapshot( 564 binding, 565 OperationId::from_bytes([100; 32]), 566 None, 567 copy(&ready.proof), 568 ) 569 .await 570 .unwrap() 571 .unwrap(); 572 assert_eq!( 573 snapshot.prepared().request().chat_command().unwrap().text(), 574 "independent owner continuation" 575 ); 576 reopened.close().await.unwrap(); 577 }