runtime_actor.rs (121564B)
1 use std::collections::BTreeMap; 2 use std::future::Future; 3 use std::num::{NonZeroU64, NonZeroUsize}; 4 use std::sync::atomic::{AtomicU64, Ordering}; 5 use std::sync::{Arc, Mutex}; 6 use std::time::{Duration, Instant}; 7 8 use harvestcircle_application::{ 9 ActiveSessionBinding, ActorMailbox, AppSnapshot, ChangeSubscriptionId, Clock, CommandContext, 10 CommandEnvelope, CommandReceipt, CommandResult, CommandSubmission, DurableRequestId, 11 GenerateIdentityReceipt, GeneratedKeyRecoveryHandle, GeneratedKeyStage, ImportIdentityReceipt, 12 LifecycleGate, NostrClient, OrderedSnapshotChanges, ProfileFetchResult, ProfileRefreshPlan, 13 RecoveryStageId, RelayConfiguration, RemovalConfirmationToken, RequestId, RuntimeCommandClass, 14 RuntimeLifecycle, SecretStore, SessionGeneration, SnapshotChange, SnapshotChangeReceiver, 15 SnapshotRevision, StagedGeneratedKey, TaskCorrelation, 16 }; 17 use harvestcircle_domain::{ 18 LocalKeyringBinding, NostrIdentityReference, PublicKey, SafeError, SafeErrorCode, SafeMessage, 19 SecretKeyInput, SignerAvailability, 20 }; 21 use radroots_runtime_paths::RuntimeContext; 22 #[cfg(test)] 23 use radroots_runtime_paths::{ 24 InstanceId, RadrootsHostEnvironment, RadrootsPathProfile, RadrootsPathResolver, 25 RadrootsPlatform, RuntimeContextBootstrap, RuntimeContextSource, ServiceId, 26 }; 27 use radroots_service_sqlite::MigrationBuildIdentity; 28 use tokio::runtime::Handle; 29 use tokio::sync::{mpsc, oneshot, watch}; 30 31 use crate::{InstallationIdentity, InstallationIdentitySource, PersistentAppCore}; 32 33 const DEFAULT_COMMAND_TIMEOUT: Duration = Duration::from_secs(30); 34 const DEFAULT_TASK_CAPACITY: usize = 64; 35 const MAX_OUTSTANDING_PROFILE_TASKS: usize = 64; 36 37 enum RuntimeCommand { 38 Snapshot, 39 GenerateIdentity { 40 durable_request: DurableRequestId, 41 expected_revision: u64, 42 }, 43 BeginGeneratedKeyStage, 44 AcknowledgeGeneratedKeyStage { 45 id: RecoveryStageId, 46 durable_request: DurableRequestId, 47 }, 48 CancelGeneratedKeyStage, 49 ImportSecretKey { 50 input: SecretKeyInput, 51 durable_request: DurableRequestId, 52 expected_revision: u64, 53 }, 54 SelectIdentity(PublicKey), 55 ActivateIdentity(PublicKey), 56 SignOut, 57 RefreshActiveProfile, 58 RequestIdentityRemoval(PublicKey), 59 ConfirmIdentityRemoval { 60 token: RemovalConfirmationToken, 61 durable_request: DurableRequestId, 62 }, 63 SubscribeChanges(NonZeroUsize), 64 UnsubscribeChanges(ChangeSubscriptionId), 65 Close, 66 } 67 68 enum RuntimeCommandValue { 69 Snapshot(Box<AppSnapshot>), 70 Generated(GenerateIdentityReceipt), 71 GeneratedKeyStage(GeneratedKeyRecoveryHandle), 72 GeneratedKeyStageCancelled(bool), 73 Imported(ImportIdentityReceipt), 74 RemovalRequest(RemovalConfirmationToken), 75 Subscription(RuntimeChangeSubscription), 76 Unsubscribed(bool), 77 Closed, 78 } 79 80 impl RuntimeCommand { 81 const fn class(&self) -> RuntimeCommandClass { 82 match self { 83 Self::Snapshot | Self::SubscribeChanges(_) | Self::UnsubscribeChanges(_) => { 84 RuntimeCommandClass::Observe 85 } 86 Self::GenerateIdentity { .. } 87 | Self::BeginGeneratedKeyStage 88 | Self::AcknowledgeGeneratedKeyStage { .. } 89 | Self::ImportSecretKey { .. } 90 | Self::ActivateIdentity(_) 91 | Self::ConfirmIdentityRemoval { .. } => RuntimeCommandClass::UseCredential, 92 Self::SelectIdentity(_) 93 | Self::SignOut 94 | Self::RequestIdentityRemoval(_) 95 | Self::CancelGeneratedKeyStage => RuntimeCommandClass::MutateLocalState, 96 Self::RefreshActiveProfile => RuntimeCommandClass::UseRelay, 97 Self::Close => RuntimeCommandClass::Shutdown, 98 } 99 } 100 101 const fn resolves_revision_through_durable_replay(&self) -> bool { 102 matches!( 103 self, 104 Self::GenerateIdentity { .. } | Self::ImportSecretKey { .. } 105 ) 106 } 107 } 108 109 struct RuntimeActor { 110 adapter: Arc<PersistentAppCore>, 111 secrets: Arc<dyn SecretStore>, 112 clock: Arc<dyn Clock>, 113 nostr: Arc<dyn NostrClient>, 114 lifecycle: Arc<Mutex<LifecycleGate>>, 115 runtime: Handle, 116 session_generation: SessionGeneration, 117 published_session_generation: Arc<AtomicU64>, 118 profile_tasks: BTreeMap<RequestId, PendingProfileTask>, 119 changes: OrderedSnapshotChanges, 120 published_foreground_session: Arc<Mutex<Option<ActiveSessionBinding>>>, 121 generated_key_stage: GeneratedKeyStage, 122 } 123 124 struct PendingProfileTask { 125 correlation: TaskCorrelation, 126 plan: ProfileRefreshPlan, 127 deadline: Instant, 128 reply: oneshot::Sender<CommandReceipt<RuntimeCommandValue>>, 129 handle: tokio::task::JoinHandle<()>, 130 } 131 132 struct ProfileCompletion { 133 request_id: RequestId, 134 result: Result<ProfileFetchResult, SafeError>, 135 } 136 137 #[derive(Clone)] 138 pub struct RuntimeActorHandle { 139 mailbox: ActorMailbox<RuntimeCommand, RuntimeCommandValue>, 140 adapter: Arc<PersistentAppCore>, 141 lifecycle: Arc<Mutex<LifecycleGate>>, 142 next_request: Arc<AtomicU64>, 143 session_generation: Arc<AtomicU64>, 144 foreground_session: Arc<Mutex<Option<ActiveSessionBinding>>>, 145 installation_identity: InstallationIdentity, 146 runtime: Handle, 147 actor_task: Arc<Mutex<Option<tokio::task::JoinHandle<()>>>>, 148 actor_exit: watch::Receiver<bool>, 149 #[cfg(test)] 150 _test_directory: Option<Arc<tempfile::TempDir>>, 151 } 152 153 #[derive(Clone)] 154 pub struct RuntimeDependencies { 155 secrets: Arc<dyn SecretStore>, 156 clock: Arc<dyn Clock>, 157 nostr: Arc<dyn NostrClient>, 158 installation_source: Arc<dyn InstallationIdentitySource>, 159 } 160 161 impl RuntimeDependencies { 162 #[must_use] 163 pub fn new( 164 secrets: Arc<dyn SecretStore>, 165 clock: Arc<dyn Clock>, 166 nostr: Arc<dyn NostrClient>, 167 installation_source: Arc<dyn InstallationIdentitySource>, 168 ) -> Self { 169 Self { 170 secrets, 171 clock, 172 nostr, 173 installation_source, 174 } 175 } 176 } 177 178 pub struct RuntimeChangeSubscription { 179 id: ChangeSubscriptionId, 180 receiver: SnapshotChangeReceiver, 181 } 182 183 impl RuntimeChangeSubscription { 184 #[must_use] 185 pub const fn id(&self) -> ChangeSubscriptionId { 186 self.id 187 } 188 189 pub async fn receive(&mut self) -> Option<SnapshotChange> { 190 self.receiver.receive().await 191 } 192 } 193 194 impl RuntimeActorHandle { 195 /// Opens, migrates, recovers, and starts one actor-owned file-backed runtime. 196 /// 197 /// # Errors 198 /// 199 /// Returns a safe storage, recovery, or lifecycle error before the actor is 200 /// published when opening cannot reach ready state. 201 pub async fn open( 202 context: &RuntimeContext, 203 relay_configuration: RelayConfiguration, 204 dependencies: RuntimeDependencies, 205 build: &MigrationBuildIdentity, 206 capacity: NonZeroUsize, 207 runtime: &Handle, 208 ) -> Result<Self, SafeError> { 209 let applied_at_unix_s = u64::try_from(dependencies.clock.now().as_seconds()) 210 .map_err(|_| invalid_runtime_evidence())?; 211 let created_at_unix_ms = applied_at_unix_s 212 .checked_mul(1_000) 213 .ok_or_else(invalid_runtime_evidence)?; 214 let adapter = PersistentAppCore::open( 215 context, 216 relay_configuration, 217 created_at_unix_ms, 218 applied_at_unix_s, 219 build, 220 ) 221 .await?; 222 Self::start(adapter, dependencies, capacity, runtime).await 223 } 224 225 async fn start( 226 adapter: PersistentAppCore, 227 dependencies: RuntimeDependencies, 228 capacity: NonZeroUsize, 229 runtime: &Handle, 230 ) -> Result<Self, SafeError> { 231 let mut gate = LifecycleGate::opening(); 232 gate.begin_compatibility_check()?; 233 gate.compatibility_accepted()?; 234 gate.ownership_acquired()?; 235 gate.migration_complete()?; 236 let RuntimeDependencies { 237 secrets, 238 clock, 239 nostr, 240 installation_source, 241 } = dependencies; 242 let adapter = Arc::new(adapter); 243 adapter.bootstrap(secrets.as_ref(), clock.as_ref()).await?; 244 let installation_identity = adapter 245 .initialize_installation_identity(installation_source.as_ref()) 246 .await?; 247 gate.recovery_complete()?; 248 249 let lifecycle = Arc::new(Mutex::new(gate)); 250 let (mailbox, receiver) = ActorMailbox::bounded(capacity); 251 let session_generation = Arc::new(AtomicU64::new(SessionGeneration::initial().value())); 252 let foreground_session = Arc::new(Mutex::new(None)); 253 let changes = OrderedSnapshotChanges::new(adapter.core().snapshot()); 254 let actor = RuntimeActor { 255 adapter: Arc::clone(&adapter), 256 secrets, 257 clock, 258 nostr, 259 lifecycle: Arc::clone(&lifecycle), 260 runtime: runtime.clone(), 261 session_generation: SessionGeneration::initial(), 262 published_session_generation: Arc::clone(&session_generation), 263 profile_tasks: BTreeMap::new(), 264 changes, 265 published_foreground_session: Arc::clone(&foreground_session), 266 generated_key_stage: GeneratedKeyStage::default(), 267 }; 268 let (actor_exit_sender, actor_exit) = watch::channel(false); 269 let actor_task = runtime.spawn(async move { 270 actor.run(receiver).await; 271 let _ = actor_exit_sender.send(true); 272 }); 273 Ok(Self { 274 mailbox, 275 adapter, 276 lifecycle, 277 next_request: Arc::new(AtomicU64::new(1)), 278 session_generation, 279 foreground_session, 280 installation_identity, 281 runtime: runtime.clone(), 282 actor_task: Arc::new(Mutex::new(Some(actor_task))), 283 actor_exit, 284 #[cfg(test)] 285 _test_directory: None, 286 }) 287 } 288 289 #[cfg(test)] 290 async fn in_memory( 291 relay_configuration: RelayConfiguration, 292 dependencies: RuntimeDependencies, 293 capacity: NonZeroUsize, 294 runtime: &Handle, 295 ) -> Result<Self, SafeError> { 296 let directory = Arc::new(tempfile::tempdir().map_err(|_| invalid_runtime_evidence())?); 297 let context = test_runtime_context(directory.path())?; 298 let build = test_migration_build_identity()?; 299 let mut actor = Self::open( 300 &context, 301 relay_configuration, 302 dependencies, 303 &build, 304 capacity, 305 runtime, 306 ) 307 .await?; 308 actor._test_directory = Some(directory); 309 Ok(actor) 310 } 311 312 #[must_use] 313 pub fn lifecycle(&self) -> RuntimeLifecycle { 314 self.lifecycle.lock().map_or_else( 315 |_| RuntimeLifecycle::Fatal(runtime_state_unavailable()), 316 |lifecycle| lifecycle.lifecycle(), 317 ) 318 } 319 320 #[must_use] 321 pub fn session_generation(&self) -> SessionGeneration { 322 SessionGeneration::from_value(self.session_generation.load(Ordering::Acquire)) 323 } 324 325 #[must_use] 326 pub fn foreground_session(&self) -> Option<ActiveSessionBinding> { 327 self.foreground_session 328 .lock() 329 .map(|session| session.clone()) 330 .unwrap_or(None) 331 } 332 333 #[must_use] 334 pub fn installation_identity(&self) -> &InstallationIdentity { 335 &self.installation_identity 336 } 337 338 #[must_use] 339 pub fn snapshot(&self) -> AppSnapshot { 340 self.adapter.core().snapshot() 341 } 342 343 /// Returns the ready snapshot through the actor command boundary. 344 /// 345 /// # Errors 346 /// 347 /// Returns a typed safe actor error. 348 pub async fn bootstrap(&self) -> Result<AppSnapshot, SafeError> { 349 Self::expect_snapshot(self.dispatch(RuntimeCommand::Snapshot, None).await?) 350 } 351 352 /// Generates one identity through the serialized actor boundary. 353 /// 354 /// # Errors 355 /// 356 /// Returns a safe identity, storage, keyring, timeout, or actor error. 357 pub async fn generate_identity( 358 &self, 359 request: DurableRequestId, 360 expected_revision: SnapshotRevision, 361 timeout: Duration, 362 ) -> Result<GenerateIdentityReceipt, SafeError> { 363 match self 364 .dispatch_durable( 365 RuntimeCommand::GenerateIdentity { 366 durable_request: request, 367 expected_revision: expected_revision.value(), 368 }, 369 expected_revision, 370 timeout, 371 ) 372 .await? 373 { 374 RuntimeCommandValue::Generated(receipt) => Ok(receipt), 375 _ => Err(invalid_actor_response()), 376 } 377 } 378 379 /// Begins the only actor-owned generated-key recovery stage. 380 /// 381 /// # Errors 382 /// 383 /// Returns a safe conflict, timeout, key-generation, or actor error. 384 pub async fn begin_generated_key_stage(&self) -> Result<GeneratedKeyRecoveryHandle, SafeError> { 385 match self 386 .dispatch(RuntimeCommand::BeginGeneratedKeyStage, None) 387 .await? 388 { 389 RuntimeCommandValue::GeneratedKeyStage(view) => Ok(view), 390 _ => Err(invalid_actor_response()), 391 } 392 } 393 394 /// Acknowledges recovery and commits the staged identity and credential once. 395 /// 396 /// # Errors 397 /// 398 /// Returns a safe unavailable, conflict, keyring, storage, timeout, or actor error. 399 pub async fn acknowledge_generated_key_stage( 400 &self, 401 id: RecoveryStageId, 402 request: DurableRequestId, 403 expected_revision: SnapshotRevision, 404 timeout: Duration, 405 ) -> Result<AppSnapshot, SafeError> { 406 let value = self 407 .dispatch_durable( 408 RuntimeCommand::AcknowledgeGeneratedKeyStage { 409 id, 410 durable_request: request, 411 }, 412 expected_revision, 413 timeout, 414 ) 415 .await?; 416 Self::expect_snapshot(value) 417 } 418 419 /// Cancels and zeroizes the active generated-key stage, if present. 420 /// 421 /// # Errors 422 /// 423 /// Returns a safe timeout or actor error. 424 pub async fn cancel_generated_key_stage(&self) -> Result<bool, SafeError> { 425 match self 426 .dispatch(RuntimeCommand::CancelGeneratedKeyStage, None) 427 .await? 428 { 429 RuntimeCommandValue::GeneratedKeyStageCancelled(cancelled) => Ok(cancelled), 430 _ => Err(invalid_actor_response()), 431 } 432 } 433 434 /// Imports one identity through the serialized actor boundary. 435 /// 436 /// # Errors 437 /// 438 /// Returns a safe identity, storage, keyring, timeout, or actor error. 439 pub async fn import_secret_key( 440 &self, 441 request: DurableRequestId, 442 expected_revision: SnapshotRevision, 443 input: SecretKeyInput, 444 timeout: Duration, 445 ) -> Result<ImportIdentityReceipt, SafeError> { 446 match self 447 .dispatch_durable( 448 RuntimeCommand::ImportSecretKey { 449 input, 450 durable_request: request, 451 expected_revision: expected_revision.value(), 452 }, 453 expected_revision, 454 timeout, 455 ) 456 .await? 457 { 458 RuntimeCommandValue::Imported(receipt) => Ok(receipt), 459 _ => Err(invalid_actor_response()), 460 } 461 } 462 463 /// Selects one identity through the serialized actor boundary. 464 /// 465 /// # Errors 466 /// 467 /// Returns a safe identity, storage, timeout, or actor error. 468 pub async fn select_identity(&self, public_key: PublicKey) -> Result<AppSnapshot, SafeError> { 469 let value = self 470 .dispatch(RuntimeCommand::SelectIdentity(public_key), None) 471 .await?; 472 Self::expect_snapshot(value) 473 } 474 475 /// Activates one identity through the serialized actor boundary. 476 /// 477 /// # Errors 478 /// 479 /// Returns a safe identity, credential, storage, timeout, or actor error. 480 pub async fn activate_identity(&self, public_key: PublicKey) -> Result<AppSnapshot, SafeError> { 481 let value = self 482 .dispatch(RuntimeCommand::ActivateIdentity(public_key), None) 483 .await?; 484 Self::expect_snapshot(value) 485 } 486 487 /// Signs out through the serialized actor boundary. 488 /// 489 /// # Errors 490 /// 491 /// Returns a safe timeout or actor error. 492 pub async fn sign_out(&self) -> Result<AppSnapshot, SafeError> { 493 let value = self.dispatch(RuntimeCommand::SignOut, None).await?; 494 Self::expect_snapshot(value) 495 } 496 497 /// Refreshes the active profile through the serialized actor boundary. 498 /// 499 /// # Errors 500 /// 501 /// Returns a safe relay, storage, timeout, or actor error. 502 pub async fn refresh_active_profile(&self) -> Result<AppSnapshot, SafeError> { 503 let value = self 504 .dispatch(RuntimeCommand::RefreshActiveProfile, None) 505 .await?; 506 Self::expect_snapshot(value) 507 } 508 509 /// Creates one removal request through the serialized actor boundary. 510 /// 511 /// # Errors 512 /// 513 /// Returns a safe identity, timeout, or actor error. 514 pub async fn request_identity_removal( 515 &self, 516 public_key: PublicKey, 517 ) -> Result<RemovalConfirmationToken, SafeError> { 518 match self 519 .dispatch(RuntimeCommand::RequestIdentityRemoval(public_key), None) 520 .await? 521 { 522 RuntimeCommandValue::RemovalRequest(token) => Ok(token), 523 _ => Err(invalid_actor_response()), 524 } 525 } 526 527 /// Confirms one removal through the serialized actor boundary. 528 /// 529 /// # Errors 530 /// 531 /// Returns a safe identity, credential, storage, timeout, or actor error. 532 pub async fn confirm_identity_removal( 533 &self, 534 token: RemovalConfirmationToken, 535 request: DurableRequestId, 536 expected_revision: SnapshotRevision, 537 timeout: Duration, 538 ) -> Result<AppSnapshot, SafeError> { 539 let value = self 540 .dispatch_durable( 541 RuntimeCommand::ConfirmIdentityRemoval { 542 token, 543 durable_request: request, 544 }, 545 expected_revision, 546 timeout, 547 ) 548 .await?; 549 Self::expect_snapshot(value) 550 } 551 552 /// Closes command admission and cancels supervised work. 553 /// 554 /// # Errors 555 /// 556 /// Returns a safe timeout or actor error. Repeated calls return closed. 557 pub async fn close(&self) -> Result<(), SafeError> { 558 self.close_with_timeout(DEFAULT_COMMAND_TIMEOUT).await 559 } 560 561 /// Closes the runtime within the supplied command deadline. 562 /// 563 /// # Errors 564 /// 565 /// Returns a safe timeout or actor error. An expired queued close cannot 566 /// later change runtime state. 567 pub async fn close_with_timeout(&self, timeout: Duration) -> Result<(), SafeError> { 568 let deadline = Instant::now() + timeout; 569 if matches!(self.lifecycle(), RuntimeLifecycle::Closed) { 570 return self.await_actor_exit(deadline).await; 571 } 572 let raw_request = self.next_request.fetch_add(1, Ordering::Relaxed); 573 let request_id = RequestId::new(raw_request).ok_or_else(request_space_exhausted)?; 574 let command_result = match self 575 .dispatch_with_deadline(RuntimeCommand::Close, None, request_id, deadline) 576 .await 577 { 578 Ok(RuntimeCommandValue::Closed) => Ok(()), 579 Ok(_) => Err(invalid_actor_response()), 580 Err(error) => Err(error), 581 }; 582 let exit_result = self.await_actor_exit(deadline).await; 583 if matches!(self.lifecycle(), RuntimeLifecycle::Closed) { 584 exit_result 585 } else { 586 command_result.and(exit_result) 587 } 588 } 589 590 async fn await_actor_exit(&self, deadline: Instant) -> Result<(), SafeError> { 591 let mut actor_exit = self.actor_exit.clone(); 592 if !*actor_exit.borrow() { 593 let remaining = deadline.saturating_duration_since(Instant::now()); 594 self.timeout(remaining, actor_exit.wait_for(|exited| *exited)) 595 .await 596 .map_err(|_| command_timed_out())? 597 .map_err(|_| runtime_closed())?; 598 } 599 let actor_task = self 600 .actor_task 601 .lock() 602 .map_err(|_| runtime_state_unavailable())? 603 .take(); 604 if let Some(actor_task) = actor_task { 605 let remaining = deadline.saturating_duration_since(Instant::now()); 606 self.timeout(remaining, actor_task) 607 .await 608 .map_err(|_| command_timed_out())? 609 .map_err(|_| runtime_closed())?; 610 } 611 Ok(()) 612 } 613 614 /// Atomically registers a bounded ordered change consumer with its initial snapshot. 615 /// 616 /// # Errors 617 /// 618 /// Returns a safe actor or subscription error. 619 pub async fn subscribe_changes( 620 &self, 621 capacity: NonZeroUsize, 622 ) -> Result<RuntimeChangeSubscription, SafeError> { 623 match self 624 .dispatch(RuntimeCommand::SubscribeChanges(capacity), None) 625 .await? 626 { 627 RuntimeCommandValue::Subscription(subscription) => Ok(subscription), 628 _ => Err(invalid_actor_response()), 629 } 630 } 631 632 /// Removes a change consumer through the serialized actor boundary. 633 /// 634 /// # Errors 635 /// 636 /// Returns a safe actor error. 637 pub async fn unsubscribe_changes(&self, id: ChangeSubscriptionId) -> Result<bool, SafeError> { 638 match self 639 .dispatch(RuntimeCommand::UnsubscribeChanges(id), None) 640 .await? 641 { 642 RuntimeCommandValue::Unsubscribed(removed) => Ok(removed), 643 _ => Err(invalid_actor_response()), 644 } 645 } 646 647 async fn dispatch( 648 &self, 649 command: RuntimeCommand, 650 expected_revision: Option<SnapshotRevision>, 651 ) -> Result<RuntimeCommandValue, SafeError> { 652 let raw_request = self.next_request.fetch_add(1, Ordering::Relaxed); 653 let request_id = RequestId::new(raw_request).ok_or_else(request_space_exhausted)?; 654 self.dispatch_with_deadline( 655 command, 656 expected_revision, 657 request_id, 658 Instant::now() + DEFAULT_COMMAND_TIMEOUT, 659 ) 660 .await 661 } 662 663 async fn dispatch_durable( 664 &self, 665 command: RuntimeCommand, 666 expected_revision: SnapshotRevision, 667 timeout: Duration, 668 ) -> Result<RuntimeCommandValue, SafeError> { 669 let raw_request = self.next_request.fetch_add(1, Ordering::Relaxed); 670 let request_id = RequestId::new(raw_request).ok_or_else(request_space_exhausted)?; 671 self.dispatch_with_deadline( 672 command, 673 Some(expected_revision), 674 request_id, 675 Instant::now() + timeout, 676 ) 677 .await 678 } 679 680 async fn dispatch_with_deadline( 681 &self, 682 command: RuntimeCommand, 683 expected_revision: Option<SnapshotRevision>, 684 request_id: RequestId, 685 deadline: Instant, 686 ) -> Result<RuntimeCommandValue, SafeError> { 687 let context = CommandContext::new(request_id, expected_revision, deadline); 688 let receipt = match self.mailbox.submit(context, command) { 689 CommandSubmission::Accepted(ticket) => { 690 let remaining = deadline.saturating_duration_since(Instant::now()); 691 match self.timeout(remaining, ticket.receipt()).await { 692 Ok(receipt) => receipt, 693 Err(_) => CommandReceipt::new(request_id, CommandResult::TimedOut), 694 } 695 } 696 CommandSubmission::Rejected(receipt) => receipt, 697 }; 698 match receipt.into_result() { 699 CommandResult::Completed(value) => Ok(value), 700 CommandResult::Conflicted { .. } => Err(command_conflicted()), 701 CommandResult::Rejected(_) => Err(command_rejected()), 702 CommandResult::TimedOut => Err(command_timed_out()), 703 CommandResult::Closed => Err(runtime_closed()), 704 CommandResult::Failed(error) => Err(error), 705 } 706 } 707 708 fn timeout<F>(&self, duration: Duration, future: F) -> tokio::time::Timeout<F> 709 where 710 F: Future, 711 { 712 let _guard = self.runtime.enter(); 713 tokio::time::timeout(duration, future) 714 } 715 716 #[cfg(test)] 717 async fn import_secret_key_test( 718 &self, 719 input: SecretKeyInput, 720 ) -> Result<ImportIdentityReceipt, SafeError> { 721 let request_number = self.next_request.fetch_add(1, Ordering::Relaxed); 722 self.import_secret_key( 723 DurableRequestId::parse(format!( 724 "01890f3e-7b1c-7000-8000-{:012x}", 725 request_number + 0x1000 726 ))?, 727 self.snapshot().revision(), 728 input, 729 DEFAULT_COMMAND_TIMEOUT, 730 ) 731 .await 732 } 733 734 #[cfg(test)] 735 async fn acknowledge_generated_key_stage_test( 736 &self, 737 id: RecoveryStageId, 738 ) -> Result<AppSnapshot, SafeError> { 739 let request_number = self.next_request.fetch_add(1, Ordering::Relaxed); 740 self.acknowledge_generated_key_stage( 741 id, 742 DurableRequestId::parse(format!( 743 "01890f3e-7b1c-7000-8000-{:012x}", 744 request_number + 0x2000 745 ))?, 746 self.snapshot().revision(), 747 DEFAULT_COMMAND_TIMEOUT, 748 ) 749 .await 750 } 751 752 #[cfg(test)] 753 async fn confirm_identity_removal_test( 754 &self, 755 token: RemovalConfirmationToken, 756 ) -> Result<AppSnapshot, SafeError> { 757 let request_number = self.next_request.fetch_add(1, Ordering::Relaxed); 758 self.confirm_identity_removal( 759 token, 760 DurableRequestId::parse(format!( 761 "01890f3e-7b1c-7000-8000-{:012x}", 762 request_number + 0x3000 763 ))?, 764 self.snapshot().revision(), 765 DEFAULT_COMMAND_TIMEOUT, 766 ) 767 .await 768 } 769 770 #[cfg(test)] 771 async fn import_secret_key_with_timeout( 772 &self, 773 input: SecretKeyInput, 774 timeout: Duration, 775 ) -> Result<ImportIdentityReceipt, SafeError> { 776 let raw_request = self.next_request.fetch_add(1, Ordering::Relaxed); 777 let request_id = RequestId::new(raw_request).ok_or_else(request_space_exhausted)?; 778 let expected_revision = self.adapter.core().snapshot().revision(); 779 let durable_request = DurableRequestId::parse(format!( 780 "01890f3e-7b1c-7000-8000-{:012x}", 781 raw_request + 0x4000 782 ))?; 783 match self 784 .dispatch_with_deadline( 785 RuntimeCommand::ImportSecretKey { 786 input, 787 durable_request, 788 expected_revision: expected_revision.value(), 789 }, 790 Some(expected_revision), 791 request_id, 792 Instant::now() + timeout, 793 ) 794 .await? 795 { 796 RuntimeCommandValue::Imported(receipt) => Ok(receipt), 797 _ => Err(invalid_actor_response()), 798 } 799 } 800 801 fn expect_snapshot(value: RuntimeCommandValue) -> Result<AppSnapshot, SafeError> { 802 match value { 803 RuntimeCommandValue::Snapshot(snapshot) => Ok(*snapshot), 804 _ => Err(invalid_actor_response()), 805 } 806 } 807 } 808 809 impl RuntimeActor { 810 async fn run( 811 mut self, 812 mut receiver: mpsc::Receiver<CommandEnvelope<RuntimeCommand, RuntimeCommandValue>>, 813 ) { 814 let (completion_sender, mut completions) = mpsc::channel(DEFAULT_TASK_CAPACITY); 815 loop { 816 tokio::select! { 817 envelope = receiver.recv() => { 818 let Some(envelope) = envelope else { 819 break; 820 }; 821 if !self.handle_command(envelope, &completion_sender).await { 822 break; 823 } 824 } 825 completion = completions.recv(), if !self.profile_tasks.is_empty() => { 826 if let Some(completion) = completion { 827 self.complete_profile_task(completion).await; 828 } 829 } 830 } 831 } 832 self.cancel_profile_tasks(None).await; 833 } 834 835 async fn handle_command( 836 &mut self, 837 envelope: CommandEnvelope<RuntimeCommand, RuntimeCommandValue>, 838 completion_sender: &mpsc::Sender<ProfileCompletion>, 839 ) -> bool { 840 let (context, command, reply) = envelope.into_parts(); 841 if matches!(command, RuntimeCommand::SubscribeChanges(_)) && reply.is_closed() { 842 return true; 843 } 844 if let Some(result) = self.preflight(context, &command) { 845 let _ = reply.send(CommandReceipt::new(context.request_id(), result)); 846 return true; 847 } 848 if matches!(command, RuntimeCommand::RefreshActiveProfile) { 849 self.start_profile_task(context, reply, completion_sender.clone()) 850 .await; 851 return true; 852 } 853 if matches!(command, RuntimeCommand::Close) { 854 let result = self.close_actor().await; 855 let closed = matches!(result, CommandResult::Completed(_)); 856 let _ = reply.send(CommandReceipt::new(context.request_id(), result)); 857 return !closed; 858 } 859 let changes_session = matches!( 860 command, 861 RuntimeCommand::ActivateIdentity(_) 862 | RuntimeCommand::SignOut 863 | RuntimeCommand::ConfirmIdentityRemoval { .. } 864 ); 865 let begins_generated_recovery = matches!(&command, RuntimeCommand::BeginGeneratedKeyStage); 866 if begins_generated_recovery { 867 let foreground = match self.published_foreground_session.lock() { 868 Ok(foreground) => foreground.clone(), 869 Err(_) => { 870 self.fail_lifecycle(runtime_state_unavailable()); 871 let _ = reply.send(CommandReceipt::new( 872 context.request_id(), 873 CommandResult::Failed(runtime_state_unavailable()), 874 )); 875 return true; 876 } 877 }; 878 let current = self.adapter.core().snapshot(); 879 if let Some(task) = self.profile_tasks.values().find(|task| { 880 task.correlation.session_generation() == self.session_generation 881 && foreground.as_ref().is_some_and(|binding| { 882 binding.generation() == task.correlation.session_generation() 883 && binding.identity().public_key() == task.correlation.identity() 884 && binding.signer_binding() == task.correlation.binding() 885 }) 886 && current.active_identity().is_some_and(|active| { 887 active.identity().public_key() == task.correlation.identity() 888 && active.profile_state() 889 == harvestcircle_application::ProfileLoadState::Loading 890 }) 891 }) { 892 // Interruption settles before stage creation, so the recovery 893 // stage captures the settled revision for acknowledgement. 894 let _ = tokio::time::timeout_at( 895 tokio::time::Instant::now(), 896 self.adapter.core().complete_profile_refresh( 897 &task.plan, 898 Err(command_unavailable()), 899 self.adapter.database(), 900 self.clock.as_ref(), 901 ), 902 ) 903 .await; 904 } 905 let snapshot = self.adapter.core().snapshot(); 906 self.cancel_profile_tasks(Some(&snapshot)).await; 907 self.changes.publish(snapshot); 908 } 909 let result = self.execute_command(context, command).await; 910 if changes_session && matches!(result, CommandResult::Completed(_)) { 911 self.advance_session_generation().await; 912 self.synchronize_foreground_session(); 913 } 914 if matches!(result, CommandResult::Completed(_)) { 915 self.changes.publish(self.adapter.core().snapshot()); 916 } 917 if let Err(receipt) = reply.send(CommandReceipt::new(context.request_id(), result)) 918 && let CommandResult::Completed(RuntimeCommandValue::Subscription(subscription)) = 919 receipt.into_result() 920 { 921 let _ = self.changes.unsubscribe(subscription.id()); 922 } 923 true 924 } 925 926 fn preflight( 927 &self, 928 context: CommandContext, 929 command: &RuntimeCommand, 930 ) -> Option<CommandResult<RuntimeCommandValue>> { 931 if context.is_expired(Instant::now()) { 932 return Some(CommandResult::TimedOut); 933 } 934 let lifecycle = match self.lifecycle.lock() { 935 Ok(lifecycle) => lifecycle.to_owned(), 936 Err(_) => return Some(CommandResult::Failed(runtime_state_unavailable())), 937 }; 938 if matches!(lifecycle.lifecycle(), RuntimeLifecycle::Closed) { 939 return Some(CommandResult::Closed); 940 } 941 if !lifecycle.allows(command.class()) { 942 return Some(CommandResult::Failed(command_unavailable())); 943 } 944 if self.generated_key_stage.pending().is_some() 945 && !matches!( 946 command, 947 RuntimeCommand::Snapshot 948 | RuntimeCommand::AcknowledgeGeneratedKeyStage { .. } 949 | RuntimeCommand::CancelGeneratedKeyStage 950 | RuntimeCommand::SubscribeChanges(_) 951 | RuntimeCommand::UnsubscribeChanges(_) 952 | RuntimeCommand::Close 953 ) 954 { 955 return Some(CommandResult::Failed(generated_recovery_route_active())); 956 } 957 let current_revision = self.adapter.core().snapshot().revision(); 958 if !command.resolves_revision_through_durable_replay() 959 && context 960 .expected_revision() 961 .is_some_and(|expected| expected != current_revision) 962 { 963 return Some(CommandResult::Conflicted { current_revision }); 964 } 965 None 966 } 967 968 async fn execute_command( 969 &mut self, 970 context: CommandContext, 971 command: RuntimeCommand, 972 ) -> CommandResult<RuntimeCommandValue> { 973 let result = match command { 974 RuntimeCommand::Snapshot => Ok(RuntimeCommandValue::Snapshot(Box::new( 975 self.adapter.core().snapshot(), 976 ))), 977 RuntimeCommand::GenerateIdentity { 978 durable_request, 979 expected_revision, 980 } => { 981 self.run_async( 982 context.deadline(), 983 move |adapter, secrets, clock| async move { 984 adapter 985 .generate_identity_durable( 986 &durable_request, 987 expected_revision, 988 secrets.as_ref(), 989 clock.as_ref(), 990 ) 991 .await 992 .map(RuntimeCommandValue::Generated) 993 }, 994 ) 995 .await 996 } 997 RuntimeCommand::BeginGeneratedKeyStage => { 998 match NonZeroU64::new(context.request_id().get()).map(RecoveryStageId::new) { 999 Some(stage_id) => self 1000 .generated_key_stage 1001 .begin( 1002 self.adapter.key_material(), 1003 stage_id, 1004 self.adapter.core().snapshot().revision().value(), 1005 self.clock.now(), 1006 ) 1007 .map(RuntimeCommandValue::GeneratedKeyStage), 1008 None => Err(request_space_exhausted()), 1009 } 1010 } 1011 RuntimeCommand::AcknowledgeGeneratedKeyStage { 1012 id, 1013 durable_request, 1014 } => match self.generated_key_stage.take(id, self.clock.now()) { 1015 Ok(staged) => { 1016 self.run_async( 1017 context.deadline(), 1018 move |adapter, secrets, clock| async move { 1019 commit_generated_key_stage( 1020 adapter.as_ref(), 1021 secrets.as_ref(), 1022 clock.as_ref(), 1023 &durable_request, 1024 staged, 1025 ) 1026 .await 1027 }, 1028 ) 1029 .await 1030 } 1031 Err(error) => Err(error), 1032 }, 1033 RuntimeCommand::CancelGeneratedKeyStage => Ok( 1034 RuntimeCommandValue::GeneratedKeyStageCancelled(self.generated_key_stage.cancel()), 1035 ), 1036 RuntimeCommand::ImportSecretKey { 1037 input, 1038 durable_request, 1039 expected_revision, 1040 } => { 1041 self.run_async( 1042 context.deadline(), 1043 move |adapter, secrets, clock| async move { 1044 adapter 1045 .import_secret_key_durable( 1046 &durable_request, 1047 expected_revision, 1048 input, 1049 secrets.as_ref(), 1050 clock.as_ref(), 1051 ) 1052 .await 1053 .map(RuntimeCommandValue::Imported) 1054 }, 1055 ) 1056 .await 1057 } 1058 RuntimeCommand::SelectIdentity(public_key) => { 1059 self.run_async(context.deadline(), move |adapter, _, _| async move { 1060 adapter 1061 .select_identity(public_key) 1062 .await 1063 .map(Box::new) 1064 .map(RuntimeCommandValue::Snapshot) 1065 }) 1066 .await 1067 } 1068 RuntimeCommand::ActivateIdentity(public_key) => { 1069 self.run_async( 1070 context.deadline(), 1071 move |adapter, secrets, clock| async move { 1072 adapter 1073 .activate_identity(public_key, secrets.as_ref(), clock.as_ref()) 1074 .await 1075 .map(Box::new) 1076 .map(RuntimeCommandValue::Snapshot) 1077 }, 1078 ) 1079 .await 1080 } 1081 RuntimeCommand::SignOut => self 1082 .adapter 1083 .sign_out() 1084 .map(Box::new) 1085 .map(RuntimeCommandValue::Snapshot), 1086 RuntimeCommand::RequestIdentityRemoval(public_key) => self 1087 .adapter 1088 .request_identity_removal(public_key, self.clock.as_ref()) 1089 .map(RuntimeCommandValue::RemovalRequest), 1090 RuntimeCommand::ConfirmIdentityRemoval { 1091 token, 1092 durable_request, 1093 } => { 1094 self.run_async( 1095 context.deadline(), 1096 move |adapter, secrets, clock| async move { 1097 adapter 1098 .confirm_identity_removal_durable( 1099 &durable_request, 1100 token, 1101 secrets.as_ref(), 1102 clock.as_ref(), 1103 ) 1104 .await 1105 .map(Box::new) 1106 .map(RuntimeCommandValue::Snapshot) 1107 }, 1108 ) 1109 .await 1110 } 1111 RuntimeCommand::SubscribeChanges(capacity) => self 1112 .changes 1113 .subscribe(capacity) 1114 .map(|(id, receiver)| { 1115 RuntimeCommandValue::Subscription(RuntimeChangeSubscription { id, receiver }) 1116 }) 1117 .ok_or_else(observer_registration_failed), 1118 RuntimeCommand::UnsubscribeChanges(id) => Ok(RuntimeCommandValue::Unsubscribed( 1119 self.changes.unsubscribe(id), 1120 )), 1121 RuntimeCommand::Close | RuntimeCommand::RefreshActiveProfile => { 1122 Err(invalid_actor_response()) 1123 } 1124 }; 1125 result.map_or_else(CommandResult::Failed, CommandResult::Completed) 1126 } 1127 1128 async fn run_async<F, Fut>( 1129 &self, 1130 deadline: Instant, 1131 operation: F, 1132 ) -> Result<RuntimeCommandValue, SafeError> 1133 where 1134 F: FnOnce(Arc<PersistentAppCore>, Arc<dyn SecretStore>, Arc<dyn Clock>) -> Fut, 1135 Fut: Future<Output = Result<RuntimeCommandValue, SafeError>>, 1136 { 1137 let adapter = Arc::clone(&self.adapter); 1138 let secrets = Arc::clone(&self.secrets); 1139 let clock = Arc::clone(&self.clock); 1140 let remaining = deadline.saturating_duration_since(Instant::now()); 1141 tokio::time::timeout(remaining, operation(adapter, secrets, clock)) 1142 .await 1143 .map_err(|_| command_timed_out())? 1144 } 1145 1146 async fn start_profile_task( 1147 &mut self, 1148 context: CommandContext, 1149 reply: oneshot::Sender<CommandReceipt<RuntimeCommandValue>>, 1150 completion_sender: mpsc::Sender<ProfileCompletion>, 1151 ) { 1152 let foreground = match self.published_foreground_session.lock() { 1153 Ok(foreground) => foreground.clone(), 1154 Err(_) => { 1155 let _ = reply.send(CommandReceipt::new( 1156 context.request_id(), 1157 CommandResult::Failed(runtime_state_unavailable()), 1158 )); 1159 return; 1160 } 1161 }; 1162 let current = self.adapter.core().snapshot(); 1163 let Some(active) = current.active_identity() else { 1164 let _ = reply.send(CommandReceipt::new( 1165 context.request_id(), 1166 CommandResult::Completed(RuntimeCommandValue::Snapshot(Box::new(current))), 1167 )); 1168 return; 1169 }; 1170 let Some(foreground) = foreground.filter(|binding| { 1171 binding.identity().public_key() == active.identity().public_key() 1172 && binding.generation() == self.session_generation 1173 }) else { 1174 let _ = reply.send(CommandReceipt::new( 1175 context.request_id(), 1176 CommandResult::Failed(stale_profile_binding()), 1177 )); 1178 return; 1179 }; 1180 if self.profile_tasks.len() >= MAX_OUTSTANDING_PROFILE_TASKS { 1181 let _ = reply.send(CommandReceipt::new( 1182 context.request_id(), 1183 CommandResult::Failed(command_rejected()), 1184 )); 1185 return; 1186 } 1187 let plan = match self.adapter.core().begin_profile_refresh() { 1188 Ok(Some(plan)) => plan, 1189 Ok(None) => { 1190 let _ = reply.send(CommandReceipt::new( 1191 context.request_id(), 1192 CommandResult::Completed(RuntimeCommandValue::Snapshot(Box::new( 1193 self.adapter.core().snapshot(), 1194 ))), 1195 )); 1196 return; 1197 } 1198 Err(error) => { 1199 let _ = reply.send(CommandReceipt::new( 1200 context.request_id(), 1201 CommandResult::Failed(error), 1202 )); 1203 return; 1204 } 1205 }; 1206 let correlation = TaskCorrelation::new( 1207 context.request_id(), 1208 plan.public_key(), 1209 foreground.signer_binding(), 1210 plan.expected_revision(), 1211 self.session_generation, 1212 ); 1213 let client = Arc::clone(&self.nostr); 1214 let relays = plan.relays().to_vec(); 1215 let request_id = context.request_id(); 1216 let handle = self.runtime.spawn(async move { 1217 let deadline = tokio::time::Instant::from_std(context.deadline()); 1218 let result = tokio::time::timeout_at( 1219 deadline, 1220 client.fetch_profile(correlation.identity(), &relays, context.deadline()), 1221 ) 1222 .await 1223 .unwrap_or_else(|_| Err(command_timed_out())); 1224 // Tokio polls ready work before its timer; expired success cannot apply. 1225 let result = if tokio::time::Instant::now() >= deadline { 1226 Err(command_timed_out()) 1227 } else { 1228 result 1229 }; 1230 let _ = completion_sender 1231 .send(ProfileCompletion { request_id, result }) 1232 .await; 1233 }); 1234 let previous = self.profile_tasks.insert( 1235 request_id, 1236 PendingProfileTask { 1237 correlation, 1238 plan, 1239 deadline: context.deadline(), 1240 reply, 1241 handle, 1242 }, 1243 ); 1244 if let Some(previous) = previous { 1245 self.fail_lifecycle(request_space_exhausted()); 1246 previous.handle.abort(); 1247 let _ = previous.handle.await; 1248 let _ = previous.reply.send(CommandReceipt::new( 1249 previous.correlation.request_id(), 1250 CommandResult::Failed(request_space_exhausted()), 1251 )); 1252 self.cancel_profile_tasks(None).await; 1253 } 1254 } 1255 1256 async fn close_actor(&mut self) -> CommandResult<RuntimeCommandValue> { 1257 let begin = { 1258 let Ok(mut lifecycle) = self.lifecycle.lock() else { 1259 return CommandResult::Failed(runtime_state_unavailable()); 1260 }; 1261 lifecycle.begin_shutdown() 1262 }; 1263 match begin { 1264 Ok(()) => { 1265 self.generated_key_stage.cancel(); 1266 self.cancel_profile_tasks(None).await; 1267 if let Err(error) = self.adapter.close().await { 1268 self.fail_lifecycle(error); 1269 return CommandResult::Failed(error); 1270 } 1271 self.changes.close(); 1272 let Ok(mut foreground) = self.published_foreground_session.lock() else { 1273 return CommandResult::Failed(runtime_state_unavailable()); 1274 }; 1275 *foreground = None; 1276 drop(foreground); 1277 let Ok(mut lifecycle) = self.lifecycle.lock() else { 1278 return CommandResult::Failed(runtime_state_unavailable()); 1279 }; 1280 match lifecycle.finish_shutdown() { 1281 Ok(()) => CommandResult::Completed(RuntimeCommandValue::Closed), 1282 Err(error) => CommandResult::Failed(error), 1283 } 1284 } 1285 Err(error) => CommandResult::Failed(error), 1286 } 1287 } 1288 1289 async fn complete_profile_task(&mut self, completion: ProfileCompletion) { 1290 let Some(task) = self.profile_tasks.remove(&completion.request_id) else { 1291 return; 1292 }; 1293 let _ = task.handle.await; 1294 let current = self.adapter.core().snapshot(); 1295 let foreground = match self.published_foreground_session.lock() { 1296 Ok(foreground) => foreground.clone(), 1297 Err(_) => { 1298 self.fail_lifecycle(runtime_state_unavailable()); 1299 let _ = task.reply.send(CommandReceipt::new( 1300 task.correlation.request_id(), 1301 CommandResult::Failed(runtime_state_unavailable()), 1302 )); 1303 return; 1304 } 1305 }; 1306 let correlated = task.correlation.session_generation() == self.session_generation 1307 && foreground.is_some_and(|binding| { 1308 binding.generation() == task.correlation.session_generation() 1309 && binding.identity().public_key() == task.correlation.identity() 1310 && binding.signer_binding() == task.correlation.binding() 1311 }) 1312 && current.active_identity().is_some_and(|active| { 1313 active.identity().public_key() == task.correlation.identity() 1314 }); 1315 let result = if correlated { 1316 let expired = 1317 tokio::time::Instant::now() >= tokio::time::Instant::from_std(task.deadline); 1318 let fetched = if expired { 1319 Err(command_timed_out()) 1320 } else { 1321 completion.result 1322 }; 1323 let deadline = tokio::time::Instant::from_std(task.deadline); 1324 let fetch_succeeded = fetched.is_ok(); 1325 let complete = self.adapter.core().complete_profile_refresh( 1326 &task.plan, 1327 fetched, 1328 self.adapter.database(), 1329 self.clock.as_ref(), 1330 ); 1331 // For successful fetches, check the timer first so ready persistence 1332 // at the deadline cannot install a late success. Failure completion 1333 // must poll its synchronous state transition before its audit await. 1334 let completed = if fetch_succeeded { 1335 complete_profile_before_deadline(deadline, complete).await 1336 } else { 1337 tokio::time::timeout_at(deadline, complete) 1338 .await 1339 .map_err(|_| command_timed_out()) 1340 .and_then(|result| result) 1341 } 1342 .map(Box::new) 1343 .map(RuntimeCommandValue::Snapshot); 1344 let completed = if expired || tokio::time::Instant::now() >= deadline { 1345 Err(command_timed_out()) 1346 } else { 1347 completed 1348 }; 1349 if let Err(error) = completed 1350 && self 1351 .adapter 1352 .core() 1353 .snapshot() 1354 .active_identity() 1355 .is_some_and(|active| { 1356 active.profile_state() 1357 == harvestcircle_application::ProfileLoadState::Loading 1358 }) 1359 { 1360 // Tokio 1.47.1 polls the future before checking the timer. The 1361 // failure transition precedes audit persistence, so even an 1362 // already-expired timeout settles Loading without extra time. 1363 let _ = tokio::time::timeout_at( 1364 deadline, 1365 self.adapter.core().complete_profile_refresh( 1366 &task.plan, 1367 Err(error), 1368 self.adapter.database(), 1369 self.clock.as_ref(), 1370 ), 1371 ) 1372 .await; 1373 } 1374 completed.map_or_else(CommandResult::Failed, CommandResult::Completed) 1375 } else { 1376 CommandResult::Completed(RuntimeCommandValue::Snapshot(Box::new(current))) 1377 }; 1378 self.changes.publish(self.adapter.core().snapshot()); 1379 let _ = task 1380 .reply 1381 .send(CommandReceipt::new(task.correlation.request_id(), result)); 1382 } 1383 1384 async fn advance_session_generation(&mut self) { 1385 let Some(next) = self.session_generation.next() else { 1386 self.fail_lifecycle(request_space_exhausted()); 1387 self.cancel_profile_tasks(None).await; 1388 return; 1389 }; 1390 self.session_generation = next; 1391 self.published_session_generation 1392 .store(next.value(), Ordering::Release); 1393 let snapshot = self.adapter.core().snapshot(); 1394 self.cancel_profile_tasks(Some(&snapshot)).await; 1395 } 1396 1397 fn synchronize_foreground_session(&mut self) { 1398 let session = self 1399 .adapter 1400 .core() 1401 .snapshot() 1402 .active_identity() 1403 .map(|active| { 1404 let public_key = active.identity().public_key(); 1405 ActiveSessionBinding::new( 1406 NostrIdentityReference::derive(public_key)?, 1407 LocalKeyringBinding::new(public_key, SignerAvailability::Available), 1408 self.session_generation, 1409 ) 1410 }); 1411 let session = match session.transpose() { 1412 Ok(session) => session, 1413 Err(error) => { 1414 self.fail_lifecycle(error); 1415 None 1416 } 1417 }; 1418 match self.published_foreground_session.lock() { 1419 Ok(mut foreground) => *foreground = session, 1420 Err(_) => self.fail_lifecycle(runtime_state_unavailable()), 1421 } 1422 } 1423 1424 fn fail_lifecycle(&self, error: SafeError) { 1425 if let Ok(mut lifecycle) = self.lifecycle.lock() { 1426 lifecycle.fail(error); 1427 } 1428 } 1429 1430 async fn cancel_profile_tasks(&mut self, snapshot: Option<&AppSnapshot>) { 1431 let tasks = std::mem::take(&mut self.profile_tasks); 1432 for (_, task) in tasks { 1433 task.handle.abort(); 1434 let _ = task.handle.await; 1435 let receipt_result = snapshot.map_or(CommandResult::Closed, |snapshot| { 1436 CommandResult::Completed(RuntimeCommandValue::Snapshot(Box::new(snapshot.clone()))) 1437 }); 1438 let _ = task.reply.send(CommandReceipt::new( 1439 task.correlation.request_id(), 1440 receipt_result, 1441 )); 1442 } 1443 } 1444 } 1445 1446 async fn complete_profile_before_deadline( 1447 deadline: tokio::time::Instant, 1448 complete: impl Future<Output = Result<AppSnapshot, SafeError>>, 1449 ) -> Result<AppSnapshot, SafeError> { 1450 tokio::pin!(complete); 1451 let guarded = std::future::poll_fn(|context| { 1452 if tokio::time::Instant::now() >= deadline { 1453 std::task::Poll::Ready(Err(command_timed_out())) 1454 } else { 1455 complete.as_mut().poll(context) 1456 } 1457 }); 1458 tokio::select! { 1459 biased; 1460 _ = tokio::time::sleep_until(deadline) => Err(command_timed_out()), 1461 result = guarded => result, 1462 } 1463 } 1464 1465 async fn commit_generated_key_stage( 1466 adapter: &PersistentAppCore, 1467 secrets: &dyn SecretStore, 1468 clock: &dyn Clock, 1469 request: &DurableRequestId, 1470 staged: StagedGeneratedKey, 1471 ) -> Result<RuntimeCommandValue, SafeError> { 1472 adapter 1473 .commit_staged_generated_key(request, staged, secrets, clock) 1474 .await?; 1475 Ok(RuntimeCommandValue::Snapshot(Box::new( 1476 adapter.core().snapshot(), 1477 ))) 1478 } 1479 1480 #[cfg(test)] 1481 fn test_runtime_context(root: &std::path::Path) -> Result<RuntimeContext, SafeError> { 1482 let canonical = root 1483 .canonicalize() 1484 .map_err(|_| invalid_runtime_evidence())?; 1485 let resolver = RadrootsPathResolver::new( 1486 RadrootsPlatform::current(), 1487 RadrootsHostEnvironment::default(), 1488 ); 1489 let bootstrap = RuntimeContextBootstrap::new( 1490 RadrootsPathProfile::RepoLocal, 1491 Some(canonical), 1492 RuntimeContextSource::BootstrapCli, 1493 RuntimeContextSource::SafeDefault, 1494 ) 1495 .map_err(|_| invalid_runtime_evidence())?; 1496 let context = RuntimeContext::resolve( 1497 &resolver, 1498 bootstrap, 1499 ServiceId::new("harvestcircle").map_err(|_| invalid_runtime_evidence())?, 1500 InstanceId::new("desktop").map_err(|_| invalid_runtime_evidence())?, 1501 ) 1502 .map_err(|_| invalid_runtime_evidence())?; 1503 std::fs::create_dir_all(root.join("data")).map_err(|_| invalid_runtime_evidence())?; 1504 Ok(context) 1505 } 1506 1507 #[cfg(test)] 1508 fn test_migration_build_identity() -> Result<MigrationBuildIdentity, SafeError> { 1509 MigrationBuildIdentity::new( 1510 "0.1.0-alpha", 1511 "1111111111111111111111111111111111111111", 1512 "2222222222222222222222222222222222222222", 1513 "1.97.1", 1514 "test", 1515 "test", 1516 1, 1517 1, 1518 1, 1519 1, 1520 1, 1521 ) 1522 .map_err(|_| invalid_runtime_evidence()) 1523 } 1524 1525 const fn request_space_exhausted() -> SafeError { 1526 SafeError::new( 1527 SafeErrorCode::InvalidApplicationState, 1528 SafeMessage::new("The runtime request identifier space is exhausted."), 1529 ) 1530 } 1531 1532 const fn invalid_runtime_evidence() -> SafeError { 1533 SafeError::new( 1534 SafeErrorCode::InvalidApplicationState, 1535 SafeMessage::new("The runtime startup evidence is invalid."), 1536 ) 1537 } 1538 1539 const fn stale_profile_binding() -> SafeError { 1540 SafeError::new( 1541 SafeErrorCode::InvalidApplicationState, 1542 SafeMessage::new("The active identity binding changed before profile refresh."), 1543 ) 1544 } 1545 1546 const fn command_conflicted() -> SafeError { 1547 SafeError::new( 1548 SafeErrorCode::InvalidApplicationState, 1549 SafeMessage::new("The command conflicts with newer application state."), 1550 ) 1551 } 1552 1553 const fn command_rejected() -> SafeError { 1554 SafeError::new( 1555 SafeErrorCode::InvalidApplicationState, 1556 SafeMessage::new("The runtime is busy. Try again."), 1557 ) 1558 } 1559 1560 const fn command_timed_out() -> SafeError { 1561 SafeError::new( 1562 SafeErrorCode::InvalidApplicationState, 1563 SafeMessage::new("The runtime command timed out."), 1564 ) 1565 } 1566 1567 const fn runtime_closed() -> SafeError { 1568 SafeError::new( 1569 SafeErrorCode::InvalidApplicationState, 1570 SafeMessage::new("The application runtime is closed."), 1571 ) 1572 } 1573 1574 const fn runtime_state_unavailable() -> SafeError { 1575 SafeError::new( 1576 SafeErrorCode::InvalidApplicationState, 1577 SafeMessage::new("The application runtime state is unavailable."), 1578 ) 1579 } 1580 1581 const fn command_unavailable() -> SafeError { 1582 SafeError::new( 1583 SafeErrorCode::InvalidApplicationState, 1584 SafeMessage::new("The command is unavailable in the current runtime state."), 1585 ) 1586 } 1587 1588 const fn generated_recovery_route_active() -> SafeError { 1589 SafeError::new( 1590 SafeErrorCode::InvalidApplicationState, 1591 SafeMessage::new("Complete or cancel generated-key recovery before another action."), 1592 ) 1593 } 1594 1595 const fn invalid_actor_response() -> SafeError { 1596 SafeError::new( 1597 SafeErrorCode::InvalidApplicationState, 1598 SafeMessage::new("The runtime returned an invalid command response."), 1599 ) 1600 } 1601 1602 const fn observer_registration_failed() -> SafeError { 1603 SafeError::new( 1604 SafeErrorCode::ObserverRegistrationFailed, 1605 SafeMessage::new("The application change subscription could not be registered."), 1606 ) 1607 } 1608 1609 #[cfg(test)] 1610 mod tests { 1611 use std::future::Future; 1612 use std::num::NonZeroUsize; 1613 use std::sync::Arc; 1614 use std::sync::atomic::{AtomicBool, Ordering}; 1615 use std::task::{Context, Poll, Wake, Waker}; 1616 use std::thread::{self, Thread}; 1617 use std::time::{Duration, Instant}; 1618 1619 use harvestcircle_application::{ 1620 ActiveSessionBinding, ActorMailbox, BoxFuture, Clock, CommandContext, CommandSubmission, 1621 DurableRequestId, FailureSecretStore, GeneratedKeyStage, InMemorySecretStore, NostrClient, 1622 OrderedSnapshotChanges, ProfileFetchResult, RelayConfiguration, RequestId, 1623 RuntimeLifecycle, SecretStore, SecretStoreOperation, SessionGeneration, SessionState, 1624 SnapshotRevision, 1625 }; 1626 use harvestcircle_domain::{ 1627 LocalKeyringBinding, NostrIdentityReference, PublicKey, SafeError, SafeErrorCode, 1628 SecretKeyInput, SignerAvailability, UnixTimestamp, 1629 }; 1630 use radroots_transport_nostr::{RelayAccess, RelayEndpoint, RelayUrlPolicy}; 1631 1632 use super::{ 1633 DEFAULT_COMMAND_TIMEOUT, RuntimeActor, RuntimeActorHandle, RuntimeCommand, 1634 RuntimeDependencies, command_unavailable, test_migration_build_identity, 1635 test_runtime_context, 1636 }; 1637 use crate::{InstallationIdentity, InstallationIdentitySource, UuidInstallationIdentitySource}; 1638 1639 struct FixedInstallationIdentity(&'static str); 1640 1641 impl InstallationIdentitySource for FixedInstallationIdentity { 1642 fn generate(&self) -> Result<InstallationIdentity, SafeError> { 1643 InstallationIdentity::parse(self.0) 1644 } 1645 } 1646 1647 fn dependencies( 1648 secrets: Arc<dyn SecretStore>, 1649 nostr: Arc<dyn NostrClient>, 1650 ) -> RuntimeDependencies { 1651 RuntimeDependencies::new( 1652 secrets, 1653 Arc::new(FixedClock), 1654 nostr, 1655 Arc::new(UuidInstallationIdentitySource), 1656 ) 1657 } 1658 1659 struct FixedClock; 1660 1661 impl Clock for FixedClock { 1662 fn now(&self) -> UnixTimestamp { 1663 UnixTimestamp::from_seconds(50).expect("time") 1664 } 1665 } 1666 1667 struct OfflineNostr; 1668 1669 impl NostrClient for OfflineNostr { 1670 fn fetch_profile<'a>( 1671 &'a self, 1672 _public_key: PublicKey, 1673 _relays: &'a [RelayEndpoint], 1674 _deadline: Instant, 1675 ) -> BoxFuture<'a, Result<ProfileFetchResult, SafeError>> { 1676 Box::pin(async { Ok(ProfileFetchResult::complete(None)) }) 1677 } 1678 } 1679 1680 struct BlockingNostr { 1681 started: tokio::sync::Semaphore, 1682 release: tokio::sync::Semaphore, 1683 } 1684 1685 impl BlockingNostr { 1686 fn new() -> Self { 1687 Self { 1688 started: tokio::sync::Semaphore::new(0), 1689 release: tokio::sync::Semaphore::new(0), 1690 } 1691 } 1692 } 1693 1694 impl NostrClient for BlockingNostr { 1695 fn fetch_profile<'a>( 1696 &'a self, 1697 _public_key: PublicKey, 1698 _relays: &'a [RelayEndpoint], 1699 _deadline: Instant, 1700 ) -> BoxFuture<'a, Result<ProfileFetchResult, SafeError>> { 1701 Box::pin(async move { 1702 self.started.add_permits(1); 1703 let permit = self.release.acquire().await.expect("release"); 1704 permit.forget(); 1705 Ok(ProfileFetchResult::complete(None)) 1706 }) 1707 } 1708 } 1709 1710 struct BlockingSecretStore { 1711 inner: InMemorySecretStore, 1712 block_next_put: AtomicBool, 1713 put_started: AtomicBool, 1714 released: AtomicBool, 1715 release_signal: tokio::sync::Notify, 1716 } 1717 1718 impl BlockingSecretStore { 1719 fn new() -> Self { 1720 Self { 1721 inner: InMemorySecretStore::default(), 1722 block_next_put: AtomicBool::new(true), 1723 put_started: AtomicBool::new(false), 1724 released: AtomicBool::new(false), 1725 release_signal: tokio::sync::Notify::new(), 1726 } 1727 } 1728 1729 async fn wait_until_put_started(&self) { 1730 while !self.put_started.load(Ordering::Acquire) { 1731 tokio::task::yield_now().await; 1732 } 1733 } 1734 1735 fn release(&self) { 1736 self.released.store(true, Ordering::Release); 1737 self.release_signal.notify_waiters(); 1738 } 1739 } 1740 1741 impl SecretStore for BlockingSecretStore { 1742 fn put<'a>( 1743 &'a self, 1744 request_id: &'a harvestcircle_application::DurableRequestId, 1745 public_key: PublicKey, 1746 secret: SecretKeyInput, 1747 ) -> BoxFuture<'a, Result<(), SafeError>> { 1748 Box::pin(async move { 1749 if self.block_next_put.swap(false, Ordering::AcqRel) { 1750 self.put_started.store(true, Ordering::Release); 1751 loop { 1752 let notified = self.release_signal.notified(); 1753 if self.released.load(Ordering::Acquire) { 1754 break; 1755 } 1756 notified.await; 1757 } 1758 } 1759 self.inner.put(request_id, public_key, secret).await 1760 }) 1761 } 1762 1763 fn load(&self, public_key: PublicKey) -> BoxFuture<'_, Result<SecretKeyInput, SafeError>> { 1764 self.inner.load(public_key) 1765 } 1766 1767 fn contains(&self, public_key: PublicKey) -> BoxFuture<'_, Result<bool, SafeError>> { 1768 self.inner.contains(public_key) 1769 } 1770 1771 fn delete<'a>( 1772 &'a self, 1773 request_id: &'a harvestcircle_application::DurableRequestId, 1774 public_key: PublicKey, 1775 ) -> BoxFuture<'a, Result<(), SafeError>> { 1776 self.inner.delete(request_id, public_key) 1777 } 1778 } 1779 1780 async fn actor() -> (RuntimeActorHandle, Arc<InMemorySecretStore>) { 1781 let secrets = Arc::new(InMemorySecretStore::default()); 1782 let secret_port: Arc<dyn SecretStore> = secrets.clone(); 1783 let actor = RuntimeActorHandle::in_memory( 1784 RelayConfiguration::default(), 1785 dependencies(secret_port, Arc::new(OfflineNostr)), 1786 NonZeroUsize::new(8).expect("capacity"), 1787 &tokio::runtime::Handle::current(), 1788 ) 1789 .await 1790 .expect("actor"); 1791 (actor, secrets) 1792 } 1793 1794 async fn profile_owner(client: Arc<dyn NostrClient>) -> (RuntimeActor, RuntimeActorHandle) { 1795 let (handle, secrets) = actor().await; 1796 let imported = handle 1797 .import_secret_key_test(secret( 1798 "7e7e9c42a91bfef19fa7ea99d52d8afdb67d893a8fefba1f5cb9793f2107f6d7", 1799 )) 1800 .await 1801 .expect("import"); 1802 handle 1803 .activate_identity(imported.identity().public_key()) 1804 .await 1805 .expect("activate"); 1806 let owner = RuntimeActor { 1807 adapter: Arc::clone(&handle.adapter), 1808 secrets, 1809 clock: Arc::new(FixedClock), 1810 nostr: client, 1811 lifecycle: Arc::clone(&handle.lifecycle), 1812 runtime: tokio::runtime::Handle::current(), 1813 session_generation: handle.session_generation(), 1814 published_session_generation: Arc::clone(&handle.session_generation), 1815 profile_tasks: std::collections::BTreeMap::new(), 1816 changes: OrderedSnapshotChanges::new(handle.snapshot()), 1817 published_foreground_session: Arc::clone(&handle.foreground_session), 1818 generated_key_stage: GeneratedKeyStage::default(), 1819 }; 1820 (owner, handle) 1821 } 1822 1823 #[tokio::test] 1824 async fn nr001_admission_is_bounded_before_state_or_network_mutation() { 1825 let client = Arc::new(BlockingNostr::new()); 1826 let (mut owner, handle) = profile_owner(client.clone()).await; 1827 let (sender, mut completed) = tokio::sync::mpsc::channel(64); 1828 let before = owner.adapter.core().snapshot(); 1829 let generation = owner.session_generation; 1830 owner.session_generation = generation.next().expect("next generation"); 1831 let (reply, mut stale) = tokio::sync::oneshot::channel(); 1832 owner 1833 .start_profile_task( 1834 CommandContext::new( 1835 RequestId::new(100).expect("id"), 1836 None, 1837 Instant::now() + DEFAULT_COMMAND_TIMEOUT, 1838 ), 1839 reply, 1840 sender.clone(), 1841 ) 1842 .await; 1843 assert!( 1844 matches!(stale.try_recv().expect("stale preflight").result(), 1845 harvestcircle_application::CommandResult::Failed(error) if *error == super::stale_profile_binding()) 1846 ); 1847 assert_eq!(owner.adapter.core().snapshot(), before); 1848 assert!(owner.profile_tasks.is_empty()); 1849 owner.session_generation = generation; 1850 let mut receipts = Vec::new(); 1851 for id in 1..=64 { 1852 let (reply, receipt) = tokio::sync::oneshot::channel(); 1853 owner 1854 .start_profile_task( 1855 CommandContext::new( 1856 RequestId::new(id).expect("id"), 1857 None, 1858 Instant::now() + DEFAULT_COMMAND_TIMEOUT, 1859 ), 1860 reply, 1861 sender.clone(), 1862 ) 1863 .await; 1864 receipts.push(receipt); 1865 } 1866 let started = client 1867 .started 1868 .acquire_many(64) 1869 .await 1870 .expect("all admitted tasks start"); 1871 started.forget(); 1872 let before = owner.adapter.core().snapshot(); 1873 let (reply, mut rejected) = tokio::sync::oneshot::channel(); 1874 owner 1875 .start_profile_task( 1876 CommandContext::new( 1877 RequestId::new(65).expect("id"), 1878 None, 1879 Instant::now() + DEFAULT_COMMAND_TIMEOUT, 1880 ), 1881 reply, 1882 sender.clone(), 1883 ) 1884 .await; 1885 assert_eq!( 1886 owner.profile_tasks.len(), 1887 64, 1888 "65th task must not be admitted" 1889 ); 1890 assert_eq!( 1891 owner.adapter.core().snapshot(), 1892 before, 1893 "rejection must not change revision or Loading" 1894 ); 1895 assert!( 1896 matches!(rejected.try_recv().expect("immediate rejection").result(), 1897 harvestcircle_application::CommandResult::Failed(error) if *error == super::command_rejected()) 1898 ); 1899 assert_eq!(client.started.available_permits(), 0); 1900 client.release.add_permits(1); 1901 owner 1902 .complete_profile_task(completed.recv().await.expect("one completion")) 1903 .await; 1904 assert_eq!(owner.profile_tasks.len(), 63); 1905 let (reply, readmitted) = tokio::sync::oneshot::channel(); 1906 owner 1907 .start_profile_task( 1908 CommandContext::new( 1909 RequestId::new(66).expect("id"), 1910 None, 1911 Instant::now() + DEFAULT_COMMAND_TIMEOUT, 1912 ), 1913 reply, 1914 sender.clone(), 1915 ) 1916 .await; 1917 assert_eq!(owner.profile_tasks.len(), 64); 1918 owner.adapter.core().sign_out().expect("sign out"); 1919 let signed_out = owner.adapter.core().snapshot(); 1920 let (reply, mut no_op) = tokio::sync::oneshot::channel(); 1921 owner 1922 .start_profile_task( 1923 CommandContext::new( 1924 RequestId::new(67).expect("id"), 1925 None, 1926 Instant::now() + DEFAULT_COMMAND_TIMEOUT, 1927 ), 1928 reply, 1929 sender, 1930 ) 1931 .await; 1932 assert!(matches!( 1933 no_op.try_recv().expect("signed-out no-op").result(), 1934 harvestcircle_application::CommandResult::Completed(_) 1935 )); 1936 assert_eq!(owner.adapter.core().snapshot(), signed_out); 1937 owner.cancel_profile_tasks(Some(&signed_out)).await; 1938 for receipt in receipts { 1939 receipt.await.expect("receipt settled exactly once"); 1940 } 1941 readmitted.await.expect("readmitted cancellation receipt"); 1942 assert!(owner.profile_tasks.is_empty()); 1943 handle.close().await.expect("close"); 1944 } 1945 1946 #[tokio::test] 1947 async fn nr001_stalled_client_expires_and_releases_loading_and_capacity() { 1948 let client = Arc::new(BlockingNostr::new()); 1949 let (mut owner, handle) = profile_owner(client.clone()).await; 1950 let (_, mut changes) = owner 1951 .changes 1952 .subscribe(NonZeroUsize::new(4).expect("capacity")) 1953 .expect("subscribe"); 1954 changes.receive().await.expect("initial"); 1955 tokio::time::pause(); 1956 let (sender, mut completed) = tokio::sync::mpsc::channel(64); 1957 let (reply, mut receipt) = tokio::sync::oneshot::channel(); 1958 let deadline = (tokio::time::Instant::now() + Duration::from_secs(5)).into_std(); 1959 owner 1960 .start_profile_task( 1961 CommandContext::new(RequestId::new(1).expect("id"), None, deadline), 1962 reply, 1963 sender.clone(), 1964 ) 1965 .await; 1966 client.started.acquire().await.expect("started").forget(); 1967 tokio::time::advance(Duration::from_secs(5)).await; 1968 tokio::task::yield_now().await; 1969 let completion = tokio::time::timeout(Duration::from_secs(1), completed.recv()) 1970 .await 1971 .expect("actor must enforce the client's ignored deadline") 1972 .expect("completion"); 1973 owner.complete_profile_task(completion).await; 1974 assert!(owner.profile_tasks.is_empty()); 1975 let change = changes.receive().await.expect("timeout state published"); 1976 assert_eq!(change.snapshot(), &owner.adapter.core().snapshot()); 1977 1978 assert!( 1979 matches!(receipt.try_recv().expect("timeout receipt").result(), 1980 harvestcircle_application::CommandResult::Failed(error) if *error == super::command_timed_out()) 1981 ); 1982 assert!( 1983 matches!(owner.adapter.core().snapshot().active_identity().expect("active").profile_state(), 1984 harvestcircle_application::ProfileLoadState::Error(error) if error == super::command_timed_out()) 1985 ); 1986 let (reply, readmitted) = tokio::sync::oneshot::channel(); 1987 owner 1988 .start_profile_task( 1989 CommandContext::new( 1990 RequestId::new(2).expect("id"), 1991 None, 1992 (tokio::time::Instant::now() + Duration::from_secs(5)).into_std(), 1993 ), 1994 reply, 1995 sender, 1996 ) 1997 .await; 1998 assert_eq!(owner.profile_tasks.len(), 1); 1999 owner.cancel_profile_tasks(None).await; 2000 assert!(matches!( 2001 readmitted.await.expect("close receipt").result(), 2002 harvestcircle_application::CommandResult::Closed 2003 )); 2004 tokio::time::resume(); 2005 handle.close().await.expect("close"); 2006 } 2007 2008 #[tokio::test] 2009 async fn nr001_failed_audit_still_publishes_settled_profile_state() { 2010 let client = Arc::new(BlockingNostr::new()); 2011 let (mut owner, handle) = profile_owner(client.clone()).await; 2012 let (_, mut changes) = owner 2013 .changes 2014 .subscribe(NonZeroUsize::new(4).expect("capacity")) 2015 .expect("subscribe"); 2016 changes.receive().await.expect("initial"); 2017 let (sender, mut completed) = tokio::sync::mpsc::channel(64); 2018 let (reply, receipt) = tokio::sync::oneshot::channel(); 2019 owner 2020 .start_profile_task( 2021 CommandContext::new( 2022 RequestId::new(1).expect("id"), 2023 None, 2024 Instant::now() + DEFAULT_COMMAND_TIMEOUT, 2025 ), 2026 reply, 2027 sender, 2028 ) 2029 .await; 2030 client.started.acquire().await.expect("started").forget(); 2031 owner 2032 .adapter 2033 .database() 2034 .close() 2035 .await 2036 .expect("inject unavailable persistence"); 2037 client.release.add_permits(1); 2038 let mut completion = completed.recv().await.expect("fetch completion"); 2039 let error = super::command_timed_out(); 2040 completion.result = Err(error); 2041 owner.complete_profile_task(completion).await; 2042 assert!(matches!( 2043 receipt.await.expect("receipt").result(), 2044 harvestcircle_application::CommandResult::Failed(_) 2045 )); 2046 assert!(owner.profile_tasks.is_empty()); 2047 let change = changes.receive().await.expect("failure must publish state"); 2048 assert!(matches!( 2049 change 2050 .snapshot() 2051 .active_identity() 2052 .expect("active") 2053 .profile_state(), 2054 harvestcircle_application::ProfileLoadState::Error(_) 2055 )); 2056 handle.close().await.expect("idempotent close"); 2057 } 2058 2059 #[tokio::test] 2060 async fn nr001_late_success_is_timeout_and_queued_old_generation_is_harmless() { 2061 let client = Arc::new(BlockingNostr::new()); 2062 let (mut owner, handle) = profile_owner(client.clone()).await; 2063 tokio::time::pause(); 2064 let (sender, mut completed) = tokio::sync::mpsc::channel(64); 2065 let (reply, receipt) = tokio::sync::oneshot::channel(); 2066 owner 2067 .start_profile_task( 2068 CommandContext::new( 2069 RequestId::new(1).expect("id"), 2070 None, 2071 (tokio::time::Instant::now() + Duration::from_secs(5)).into_std(), 2072 ), 2073 reply, 2074 sender.clone(), 2075 ) 2076 .await; 2077 client.started.acquire().await.expect("started").forget(); 2078 client.release.add_permits(1); 2079 let completion = completed.recv().await.expect("ready success queued"); 2080 assert!(completion.result.is_ok()); 2081 tokio::time::advance(Duration::from_secs(5)).await; 2082 owner.complete_profile_task(completion).await; 2083 assert!(matches!(receipt.await.expect("expired receipt").result(), 2084 harvestcircle_application::CommandResult::Failed(error) if *error == super::command_timed_out())); 2085 let (reply, receipt) = tokio::sync::oneshot::channel(); 2086 owner 2087 .start_profile_task( 2088 CommandContext::new( 2089 RequestId::new(2).expect("id"), 2090 None, 2091 (tokio::time::Instant::now() + Duration::from_secs(5)).into_std(), 2092 ), 2093 reply, 2094 sender, 2095 ) 2096 .await; 2097 client 2098 .started 2099 .acquire() 2100 .await 2101 .expect("second start") 2102 .forget(); 2103 client.release.add_permits(1); 2104 let old_completion = completed.recv().await.expect("queued old generation"); 2105 let public_key = owner 2106 .adapter 2107 .core() 2108 .snapshot() 2109 .active_identity() 2110 .expect("active") 2111 .identity() 2112 .public_key(); 2113 owner.adapter.core().sign_out().expect("logout"); 2114 owner.advance_session_generation().await; 2115 assert!(matches!( 2116 receipt.await.expect("cancelled receipt").result(), 2117 harvestcircle_application::CommandResult::Completed(_) 2118 )); 2119 tokio::time::resume(); 2120 handle 2121 .activate_identity(public_key) 2122 .await 2123 .expect("switch back to same identity"); 2124 let before = owner.adapter.core().snapshot(); 2125 owner.complete_profile_task(old_completion).await; 2126 assert_eq!(owner.adapter.core().snapshot(), before); 2127 assert!(owner.profile_tasks.is_empty()); 2128 handle.close().await.expect("close"); 2129 } 2130 2131 #[tokio::test] 2132 async fn nr001_generated_recovery_cancellation_releases_capacity_and_settles_loading() { 2133 let client = Arc::new(BlockingNostr::new()); 2134 let (mut owner, handle) = profile_owner(client.clone()).await; 2135 let (sender, _completed) = tokio::sync::mpsc::channel(64); 2136 let (reply, receipt) = tokio::sync::oneshot::channel(); 2137 owner 2138 .start_profile_task( 2139 CommandContext::new( 2140 RequestId::new(100).expect("id"), 2141 None, 2142 Instant::now() + DEFAULT_COMMAND_TIMEOUT, 2143 ), 2144 reply, 2145 sender.clone(), 2146 ) 2147 .await; 2148 client.started.acquire().await.expect("started").forget(); 2149 let (mailbox, mut receiver) = ActorMailbox::bounded(NonZeroUsize::MIN); 2150 let context = CommandContext::new( 2151 RequestId::new(101).expect("id"), 2152 None, 2153 Instant::now() + DEFAULT_COMMAND_TIMEOUT, 2154 ); 2155 let ticket = match mailbox.submit(context, RuntimeCommand::BeginGeneratedKeyStage) { 2156 CommandSubmission::Accepted(ticket) => ticket, 2157 CommandSubmission::Rejected(_) => panic!("admit recovery"), 2158 }; 2159 assert!( 2160 owner 2161 .handle_command(receiver.recv().await.expect("recovery command"), &sender) 2162 .await 2163 ); 2164 let recovery_id = match ticket.receipt().await.into_result() { 2165 harvestcircle_application::CommandResult::Completed( 2166 super::RuntimeCommandValue::GeneratedKeyStage(stage), 2167 ) => stage.id(), 2168 _ => panic!("recovery must begin"), 2169 }; 2170 let cancelled = receipt.await.expect("cancelled receipt exactly once"); 2171 assert!(matches!( 2172 cancelled.result(), 2173 harvestcircle_application::CommandResult::Completed(_) 2174 )); 2175 assert!(owner.profile_tasks.is_empty()); 2176 assert_ne!( 2177 owner 2178 .adapter 2179 .core() 2180 .snapshot() 2181 .active_identity() 2182 .expect("active") 2183 .profile_state(), 2184 harvestcircle_application::ProfileLoadState::Loading 2185 ); 2186 let before_commit = owner.adapter.core().snapshot().identities().len(); 2187 let context = CommandContext::new( 2188 RequestId::new(102).expect("id"), 2189 None, 2190 Instant::now() + DEFAULT_COMMAND_TIMEOUT, 2191 ); 2192 let ticket = match mailbox.submit( 2193 context, 2194 RuntimeCommand::AcknowledgeGeneratedKeyStage { 2195 id: recovery_id, 2196 durable_request: DurableRequestId::parse("01890f3e-7b1c-7000-8000-000000005003") 2197 .expect("durable request"), 2198 }, 2199 ) { 2200 CommandSubmission::Accepted(ticket) => ticket, 2201 CommandSubmission::Rejected(_) => panic!("admit acknowledgement"), 2202 }; 2203 assert!( 2204 owner 2205 .handle_command(receiver.recv().await.expect("acknowledgement"), &sender) 2206 .await 2207 ); 2208 assert!( 2209 matches!( 2210 ticket.receipt().await.result(), 2211 harvestcircle_application::CommandResult::Completed(_) 2212 ), 2213 "settling Loading must not invalidate staged expected revision" 2214 ); 2215 assert_eq!( 2216 owner.adapter.core().snapshot().identities().len(), 2217 before_commit + 1 2218 ); 2219 handle.close().await.expect("close"); 2220 } 2221 2222 struct PendingProfilePersistence { 2223 started: tokio::sync::Semaphore, 2224 release: tokio::sync::Semaphore, 2225 } 2226 2227 impl harvestcircle_application::ProfileRepository for PendingProfilePersistence { 2228 fn load_profile( 2229 &self, 2230 _key: PublicKey, 2231 ) -> BoxFuture<'_, Result<Option<harvestcircle_application::CachedProfile>, SafeError>> 2232 { 2233 Box::pin(async { Ok(None) }) 2234 } 2235 fn save_profile<'a>( 2236 &'a self, 2237 _profile: &'a harvestcircle_application::CachedProfile, 2238 ) -> BoxFuture<'a, Result<(), SafeError>> { 2239 Box::pin(async move { 2240 self.started.add_permits(1); 2241 self.release 2242 .acquire() 2243 .await 2244 .expect("release persistence") 2245 .forget(); 2246 Ok(()) 2247 }) 2248 } 2249 fn record_refresh_status<'a>( 2250 &'a self, 2251 _key: PublicKey, 2252 _at: UnixTimestamp, 2253 _status: harvestcircle_application::ProfileRefreshStatus, 2254 ) -> BoxFuture<'a, Result<(), SafeError>> { 2255 Box::pin(std::future::pending()) 2256 } 2257 fn remove_profile(&self, _key: PublicKey) -> BoxFuture<'_, Result<(), SafeError>> { 2258 Box::pin(async { Ok(()) }) 2259 } 2260 } 2261 2262 #[tokio::test] 2263 async fn nr001_ready_persistence_at_expiry_cannot_install_success_and_cancelled_audit_settles_loading() 2264 { 2265 let (owner, handle) = profile_owner(Arc::new(OfflineNostr)).await; 2266 let core = owner.adapter.core(); 2267 let plan = core 2268 .begin_profile_refresh() 2269 .expect("begin") 2270 .expect("active"); 2271 let profiles = PendingProfilePersistence { 2272 started: tokio::sync::Semaphore::new(0), 2273 release: tokio::sync::Semaphore::new(0), 2274 }; 2275 let candidate = harvestcircle_domain::Kind0ProfileCandidate::new( 2276 harvestcircle_domain::EventId::from_bytes([20; 32]), 2277 plan.public_key(), 2278 UnixTimestamp::from_seconds(20).expect("time"), 2279 harvestcircle_domain::ProfileMetadata::new( 2280 Some("Late".to_owned()), 2281 None, 2282 None, 2283 None, 2284 None, 2285 ) 2286 .expect("metadata"), 2287 ); 2288 tokio::time::pause(); 2289 let deadline = tokio::time::Instant::now() + Duration::from_secs(5); 2290 let completion = super::complete_profile_before_deadline( 2291 deadline, 2292 core.complete_profile_refresh( 2293 &plan, 2294 Ok(ProfileFetchResult::complete(Some(candidate))), 2295 &profiles, 2296 &FixedClock, 2297 ), 2298 ); 2299 tokio::pin!(completion); 2300 tokio::select! { 2301 result = &mut completion => panic!("persistence must block: {result:?}"), 2302 permit = profiles.started.acquire() => permit.expect("persistence started").forget(), 2303 } 2304 tokio::time::advance(Duration::from_secs(5)).await; 2305 profiles.release.add_permits(1); 2306 assert_eq!( 2307 completion 2308 .await 2309 .expect_err("timer must beat ready persistence"), 2310 super::command_timed_out() 2311 ); 2312 assert!( 2313 core.snapshot() 2314 .active_identity() 2315 .expect("active") 2316 .profile() 2317 .is_none() 2318 ); 2319 let cleanup = tokio::time::timeout_at( 2320 deadline, 2321 core.complete_profile_refresh( 2322 &plan, 2323 Err(super::command_timed_out()), 2324 &profiles, 2325 &FixedClock, 2326 ), 2327 ) 2328 .await; 2329 assert!( 2330 cleanup.is_err(), 2331 "audit stays pending and is cancelled at the original deadline" 2332 ); 2333 assert_eq!( 2334 core.snapshot() 2335 .active_identity() 2336 .expect("active") 2337 .profile_state(), 2338 harvestcircle_application::ProfileLoadState::Error(super::command_timed_out()) 2339 ); 2340 tokio::time::resume(); 2341 handle.close().await.expect("close"); 2342 } 2343 2344 struct ThreadWake(Thread); 2345 2346 impl Wake for ThreadWake { 2347 fn wake(self: Arc<Self>) { 2348 self.0.unpark(); 2349 } 2350 2351 fn wake_by_ref(self: &Arc<Self>) { 2352 self.0.unpark(); 2353 } 2354 } 2355 2356 fn block_on_without_runtime<F: Future>(future: F) -> F::Output { 2357 let waker = Waker::from(Arc::new(ThreadWake(thread::current()))); 2358 let mut context = Context::from_waker(&waker); 2359 let mut future = std::pin::pin!(future); 2360 loop { 2361 match future.as_mut().poll(&mut context) { 2362 Poll::Ready(output) => return output, 2363 Poll::Pending => thread::park(), 2364 } 2365 } 2366 } 2367 2368 #[test] 2369 fn actor_operations_support_foreign_executor_polling() { 2370 let runtime = tokio::runtime::Runtime::new().expect("runtime"); 2371 let (actor, _) = runtime.block_on(actor()); 2372 2373 let snapshot = block_on_without_runtime(actor.bootstrap()).expect("bootstrap"); 2374 assert_eq!(snapshot, actor.snapshot()); 2375 block_on_without_runtime(actor.close()).expect("close"); 2376 } 2377 2378 #[tokio::test(flavor = "multi_thread")] 2379 async fn installation_identity_survives_file_backed_runtime_restart() { 2380 let directory = tempfile::tempdir().expect("temporary directory"); 2381 let context = test_runtime_context(directory.path()).expect("runtime context"); 2382 let build = test_migration_build_identity().expect("build identity"); 2383 let first = RuntimeActorHandle::open( 2384 &context, 2385 RelayConfiguration::default(), 2386 RuntimeDependencies::new( 2387 Arc::new(InMemorySecretStore::default()), 2388 Arc::new(FixedClock), 2389 Arc::new(OfflineNostr), 2390 Arc::new(FixedInstallationIdentity( 2391 "11aabbccddeeff001122334455667788", 2392 )), 2393 ), 2394 &build, 2395 NonZeroUsize::new(8).expect("capacity"), 2396 &tokio::runtime::Handle::current(), 2397 ) 2398 .await 2399 .expect("first runtime"); 2400 assert_eq!( 2401 first.installation_identity().as_str(), 2402 "11aabbccddeeff001122334455667788" 2403 ); 2404 first.close().await.expect("first close"); 2405 drop(first); 2406 2407 let second = RuntimeActorHandle::open( 2408 &context, 2409 RelayConfiguration::default(), 2410 RuntimeDependencies::new( 2411 Arc::new(InMemorySecretStore::default()), 2412 Arc::new(FixedClock), 2413 Arc::new(OfflineNostr), 2414 Arc::new(FixedInstallationIdentity( 2415 "22aabbccddeeff001122334455667788", 2416 )), 2417 ), 2418 &build, 2419 NonZeroUsize::new(8).expect("capacity"), 2420 &tokio::runtime::Handle::current(), 2421 ) 2422 .await 2423 .expect("second runtime"); 2424 assert_eq!( 2425 second.installation_identity().as_str(), 2426 "11aabbccddeeff001122334455667788" 2427 ); 2428 second.close().await.expect("second close"); 2429 } 2430 2431 #[tokio::test(flavor = "multi_thread")] 2432 async fn identity_mutations_run_serially_through_one_ready_actor() { 2433 let (actor, secrets) = actor().await; 2434 assert_eq!(actor.lifecycle(), RuntimeLifecycle::Ready); 2435 2436 let imported = actor 2437 .import_secret_key_test( 2438 SecretKeyInput::parse( 2439 "7e7e9c42a91bfef19fa7ea99d52d8afdb67d893a8fefba1f5cb9793f2107f6d7".to_owned(), 2440 ) 2441 .expect("input"), 2442 ) 2443 .await 2444 .expect("import"); 2445 let public_key = imported.identity().public_key(); 2446 let activated = actor.activate_identity(public_key).await.expect("activate"); 2447 assert_eq!(activated.session(), SessionState::Active); 2448 let foreground = actor.foreground_session().expect("foreground session"); 2449 assert_eq!(foreground.identity().public_key(), public_key); 2450 assert_eq!(foreground.signer_binding().identity(), public_key); 2451 assert_eq!(foreground.generation(), actor.session_generation()); 2452 assert!(secrets.contains(public_key).await.expect("credential")); 2453 2454 let signed_out = actor.sign_out().await.expect("sign out"); 2455 assert_eq!(signed_out.session(), SessionState::SignedOut); 2456 assert!(actor.foreground_session().is_none()); 2457 let removal = actor 2458 .request_identity_removal(public_key) 2459 .await 2460 .expect("removal request"); 2461 let removed = actor 2462 .confirm_identity_removal_test(removal) 2463 .await 2464 .expect("remove"); 2465 assert!(removed.identities().is_empty()); 2466 assert!( 2467 !secrets 2468 .contains(public_key) 2469 .await 2470 .expect("credential removed") 2471 ); 2472 } 2473 2474 #[tokio::test(flavor = "multi_thread")] 2475 async fn public_actor_commands_cover_generation_selection_and_empty_profile_refresh() { 2476 let (actor, _) = actor().await; 2477 let unchanged = actor 2478 .refresh_active_profile() 2479 .await 2480 .expect("refresh without an active identity"); 2481 assert!(unchanged.active_identity().is_none()); 2482 2483 let generated = actor 2484 .generate_identity( 2485 DurableRequestId::parse("01890f3e-7b1c-7000-8000-000000005001").expect("request"), 2486 actor.snapshot().revision(), 2487 DEFAULT_COMMAND_TIMEOUT, 2488 ) 2489 .await 2490 .expect("generate identity"); 2491 let selected = actor 2492 .select_identity(generated.identity().public_key()) 2493 .await 2494 .expect("select generated identity"); 2495 assert_eq!( 2496 selected.selected_identity(), 2497 Some(generated.identity().public_key()) 2498 ); 2499 2500 let missing = 2501 PublicKey::from_hex("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798") 2502 .expect("public key"); 2503 let error = match actor.request_identity_removal(missing).await { 2504 Ok(_) => panic!("unknown identity removal must fail"), 2505 Err(error) => error, 2506 }; 2507 assert_eq!(error.code(), SafeErrorCode::IdentityNotFound); 2508 } 2509 2510 #[tokio::test(flavor = "multi_thread")] 2511 async fn staged_recovery_rejects_a_stale_expected_revision_before_commit() { 2512 let (actor, _) = actor().await; 2513 let handle = actor 2514 .begin_generated_key_stage() 2515 .await 2516 .expect("generated key stage"); 2517 let stale = SnapshotRevision::from_value(actor.snapshot().revision().value() + 1); 2518 let error = actor 2519 .acknowledge_generated_key_stage( 2520 handle.id(), 2521 DurableRequestId::parse("01890f3e-7b1c-7000-8000-000000005002").expect("request"), 2522 stale, 2523 DEFAULT_COMMAND_TIMEOUT, 2524 ) 2525 .await 2526 .expect_err("stale revision must conflict"); 2527 assert_eq!( 2528 error.message().as_str(), 2529 "The command conflicts with newer application state." 2530 ); 2531 assert!(actor.cancel_generated_key_stage().await.expect("cancel")); 2532 } 2533 2534 #[tokio::test(flavor = "multi_thread")] 2535 async fn fatal_lifecycle_rejects_commands_before_execution() { 2536 let (actor, _) = actor().await; 2537 actor 2538 .lifecycle 2539 .lock() 2540 .unwrap_or_else(std::sync::PoisonError::into_inner) 2541 .fail(command_unavailable()); 2542 2543 let error = actor 2544 .bootstrap() 2545 .await 2546 .expect_err("fatal lifecycle must reject command admission"); 2547 assert_eq!( 2548 error.message().as_str(), 2549 "The command is unavailable in the current runtime state." 2550 ); 2551 assert!(matches!(actor.lifecycle(), RuntimeLifecycle::Fatal(_))); 2552 } 2553 2554 #[tokio::test(flavor = "multi_thread")] 2555 async fn generated_key_stage_is_exclusive_cancelable_and_snapshot_free() { 2556 let (actor, secrets) = actor().await; 2557 let initial = actor.snapshot(); 2558 let stage = actor 2559 .begin_generated_key_stage() 2560 .await 2561 .expect("generated key stage"); 2562 2563 assert!(actor.begin_generated_key_stage().await.is_err()); 2564 assert_eq!(actor.snapshot(), initial); 2565 assert!( 2566 !secrets 2567 .contains(stage.view().identity().public_key()) 2568 .await 2569 .expect("keyring") 2570 ); 2571 assert!(actor.sign_out().await.is_err()); 2572 assert_eq!(actor.snapshot(), initial); 2573 assert!(actor.cancel_generated_key_stage().await.expect("cancel")); 2574 assert!( 2575 !actor 2576 .cancel_generated_key_stage() 2577 .await 2578 .expect("cancel empty") 2579 ); 2580 assert_eq!(actor.snapshot(), initial); 2581 2582 actor 2583 .begin_generated_key_stage() 2584 .await 2585 .expect("replacement stage"); 2586 actor.close().await.expect("close clears stage"); 2587 assert_eq!(actor.lifecycle(), RuntimeLifecycle::Closed); 2588 } 2589 2590 #[tokio::test(flavor = "multi_thread")] 2591 async fn recovery_handle_is_one_use_and_acknowledgement_commits_once() { 2592 let (actor, secrets) = actor().await; 2593 let initial = actor.snapshot(); 2594 let handle = actor 2595 .begin_generated_key_stage() 2596 .await 2597 .expect("generated key stage"); 2598 let public_key = handle.view().identity().public_key(); 2599 let recovery = handle.take_recovery_nsec().expect("recovery material"); 2600 assert_eq!(recovery.with_exposed_secret(str::len), 63); 2601 assert!(handle.take_recovery_nsec().is_err()); 2602 assert_eq!(actor.snapshot(), initial); 2603 assert!(!secrets.contains(public_key).await.expect("not committed")); 2604 2605 let committed = actor 2606 .acknowledge_generated_key_stage_test(handle.id()) 2607 .await 2608 .expect("acknowledge"); 2609 assert_eq!(committed.identities().len(), 1); 2610 assert_eq!(committed.selected_identity(), Some(public_key)); 2611 assert!( 2612 secrets 2613 .contains(public_key) 2614 .await 2615 .expect("credential committed") 2616 ); 2617 assert!( 2618 actor 2619 .acknowledge_generated_key_stage_test(handle.id()) 2620 .await 2621 .is_err() 2622 ); 2623 } 2624 2625 #[tokio::test(flavor = "multi_thread")] 2626 async fn failed_generated_commit_consumes_the_stage_without_poisoning_the_actor() { 2627 let secrets = Arc::new(FailureSecretStore::default()); 2628 secrets.fail_next(SecretStoreOperation::Put); 2629 let secret_port: Arc<dyn SecretStore> = secrets.clone(); 2630 let actor = RuntimeActorHandle::in_memory( 2631 RelayConfiguration::default(), 2632 dependencies(secret_port, Arc::new(OfflineNostr)), 2633 NonZeroUsize::new(8).expect("capacity"), 2634 &tokio::runtime::Handle::current(), 2635 ) 2636 .await 2637 .expect("actor"); 2638 let handle = actor 2639 .begin_generated_key_stage() 2640 .await 2641 .expect("generated key stage"); 2642 2643 let error = actor 2644 .acknowledge_generated_key_stage_test(handle.id()) 2645 .await 2646 .expect_err("injected keyring failure"); 2647 2648 assert_eq!(error.code(), SafeErrorCode::KeyringUnavailable); 2649 assert!(actor.snapshot().identities().is_empty()); 2650 actor 2651 .begin_generated_key_stage() 2652 .await 2653 .expect("fresh recovery after terminal failure"); 2654 assert!(actor.cancel_generated_key_stage().await.expect("cancel")); 2655 } 2656 2657 #[tokio::test(flavor = "multi_thread")] 2658 async fn session_generation_cancels_correlated_profile_work_on_sign_out() { 2659 let client = Arc::new(BlockingNostr::new()); 2660 let actor = RuntimeActorHandle::in_memory( 2661 RelayConfiguration::new(vec![ 2662 RelayEndpoint::new( 2663 "ws://localhost:8080", 2664 RelayUrlPolicy::Local, 2665 RelayAccess::ReadWrite, 2666 ) 2667 .expect("relay"), 2668 ]) 2669 .expect("relay configuration"), 2670 dependencies(Arc::new(InMemorySecretStore::default()), client.clone()), 2671 NonZeroUsize::new(8).expect("capacity"), 2672 &tokio::runtime::Handle::current(), 2673 ) 2674 .await 2675 .expect("actor"); 2676 let imported = actor 2677 .import_secret_key_test( 2678 SecretKeyInput::parse( 2679 "7e7e9c42a91bfef19fa7ea99d52d8afdb67d893a8fefba1f5cb9793f2107f6d7".to_owned(), 2680 ) 2681 .expect("input"), 2682 ) 2683 .await 2684 .expect("import"); 2685 actor 2686 .activate_identity(imported.identity().public_key()) 2687 .await 2688 .expect("activate"); 2689 assert_eq!(actor.session_generation().value(), 1); 2690 2691 let refresh_actor = actor.clone(); 2692 let refresh = tokio::spawn(async move { refresh_actor.refresh_active_profile().await }); 2693 let started = client.started.acquire().await.expect("refresh started"); 2694 started.forget(); 2695 let signed_out = actor.sign_out().await.expect("sign out"); 2696 let cancelled = refresh 2697 .await 2698 .expect("refresh task") 2699 .expect("safe cancellation"); 2700 2701 assert_eq!(actor.session_generation().value(), 2); 2702 assert_eq!(signed_out.session(), SessionState::SignedOut); 2703 assert_eq!(cancelled.session(), SessionState::SignedOut); 2704 assert!(cancelled.active_identity().is_none()); 2705 } 2706 2707 #[tokio::test(flavor = "multi_thread", worker_threads = 4)] 2708 async fn profile_refresh_rejects_stale_bindings_and_discards_stale_completions() { 2709 let client = Arc::new(BlockingNostr::new()); 2710 let actor = RuntimeActorHandle::in_memory( 2711 RelayConfiguration::new(vec![ 2712 RelayEndpoint::new( 2713 "ws://localhost:8080", 2714 RelayUrlPolicy::Local, 2715 RelayAccess::ReadWrite, 2716 ) 2717 .expect("relay"), 2718 ]) 2719 .expect("relay configuration"), 2720 dependencies(Arc::new(InMemorySecretStore::default()), client.clone()), 2721 NonZeroUsize::new(8).expect("capacity"), 2722 &tokio::runtime::Handle::current(), 2723 ) 2724 .await 2725 .expect("actor"); 2726 let imported = actor 2727 .import_secret_key_test(secret( 2728 "7e7e9c42a91bfef19fa7ea99d52d8afdb67d893a8fefba1f5cb9793f2107f6d7", 2729 )) 2730 .await 2731 .expect("import"); 2732 let public_key = imported.identity().public_key(); 2733 actor.activate_identity(public_key).await.expect("activate"); 2734 let binding = actor.foreground_session().expect("foreground binding"); 2735 let stale_binding = ActiveSessionBinding::new( 2736 NostrIdentityReference::derive(public_key).expect("identity"), 2737 LocalKeyringBinding::new(public_key, SignerAvailability::Available), 2738 SessionGeneration::from_value(binding.generation().value() + 1), 2739 ) 2740 .expect("stale binding fixture"); 2741 let other_public_key = 2742 PublicKey::from_hex("c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5") 2743 .expect("other public key"); 2744 let other_binding = ActiveSessionBinding::new( 2745 NostrIdentityReference::derive(other_public_key).expect("other identity"), 2746 LocalKeyringBinding::new(other_public_key, SignerAvailability::Available), 2747 binding.generation(), 2748 ) 2749 .expect("other binding fixture"); 2750 2751 *actor 2752 .foreground_session 2753 .lock() 2754 .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(stale_binding.clone()); 2755 let error = actor 2756 .refresh_active_profile() 2757 .await 2758 .expect_err("stale generation must reject before relay work"); 2759 assert_eq!( 2760 error.message().as_str(), 2761 "The active identity binding changed before profile refresh." 2762 ); 2763 2764 *actor 2765 .foreground_session 2766 .lock() 2767 .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(other_binding.clone()); 2768 let error = actor 2769 .refresh_active_profile() 2770 .await 2771 .expect_err("different identity binding must reject before relay work"); 2772 assert_eq!( 2773 error.message().as_str(), 2774 "The active identity binding changed before profile refresh." 2775 ); 2776 2777 *actor 2778 .foreground_session 2779 .lock() 2780 .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(binding.clone()); 2781 let refresh_actor = actor.clone(); 2782 let refresh = tokio::spawn(async move { refresh_actor.refresh_active_profile().await }); 2783 let started = client.started.acquire().await.expect("refresh started"); 2784 started.forget(); 2785 *actor 2786 .foreground_session 2787 .lock() 2788 .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(stale_binding); 2789 client.release.add_permits(1); 2790 let unchanged = refresh 2791 .await 2792 .expect("refresh task") 2793 .expect("stale completion returns current snapshot"); 2794 assert_eq!(unchanged.revision(), actor.snapshot().revision()); 2795 2796 *actor 2797 .foreground_session 2798 .lock() 2799 .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(binding.clone()); 2800 let refresh_actor = actor.clone(); 2801 let refresh = tokio::spawn(async move { refresh_actor.refresh_active_profile().await }); 2802 let started = client 2803 .started 2804 .acquire() 2805 .await 2806 .expect("second refresh started"); 2807 started.forget(); 2808 *actor 2809 .foreground_session 2810 .lock() 2811 .unwrap_or_else(std::sync::PoisonError::into_inner) = None; 2812 client.release.add_permits(1); 2813 let unchanged = refresh 2814 .await 2815 .expect("refresh task") 2816 .expect("missing binding returns current snapshot"); 2817 assert_eq!(unchanged.revision(), actor.snapshot().revision()); 2818 2819 *actor 2820 .foreground_session 2821 .lock() 2822 .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(binding.clone()); 2823 let refresh_actor = actor.clone(); 2824 let refresh = tokio::spawn(async move { refresh_actor.refresh_active_profile().await }); 2825 let started = client 2826 .started 2827 .acquire() 2828 .await 2829 .expect("third refresh started"); 2830 started.forget(); 2831 *actor 2832 .foreground_session 2833 .lock() 2834 .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(other_binding); 2835 client.release.add_permits(1); 2836 let unchanged = refresh 2837 .await 2838 .expect("refresh task") 2839 .expect("different identity binding returns current snapshot"); 2840 assert_eq!(unchanged.revision(), actor.snapshot().revision()); 2841 2842 *actor 2843 .foreground_session 2844 .lock() 2845 .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(binding); 2846 } 2847 2848 #[tokio::test(flavor = "multi_thread", worker_threads = 4)] 2849 async fn bounded_runtime_rejects_saturation_without_dropping_accepted_commands() { 2850 let secrets = Arc::new(BlockingSecretStore::new()); 2851 let actor = RuntimeActorHandle::in_memory( 2852 RelayConfiguration::default(), 2853 dependencies(secrets.clone(), Arc::new(OfflineNostr)), 2854 NonZeroUsize::new(1).expect("capacity"), 2855 &tokio::runtime::Handle::current(), 2856 ) 2857 .await 2858 .expect("actor"); 2859 2860 let first_actor = actor.clone(); 2861 let first = tokio::spawn(async move { 2862 first_actor 2863 .import_secret_key_test(secret( 2864 "7e7e9c42a91bfef19fa7ea99d52d8afdb67d893a8fefba1f5cb9793f2107f6d7", 2865 )) 2866 .await 2867 }); 2868 secrets.wait_until_put_started().await; 2869 2870 let second_actor = actor.clone(); 2871 let second = tokio::spawn(async move { 2872 second_actor 2873 .import_secret_key_test(secret( 2874 "0000000000000000000000000000000000000000000000000000000000000001", 2875 )) 2876 .await 2877 }); 2878 while actor.mailbox.available_capacity() != 0 { 2879 assert!( 2880 !second.is_finished(), 2881 "second command must enter the mailbox" 2882 ); 2883 tokio::task::yield_now().await; 2884 } 2885 let rejected = actor 2886 .import_secret_key_test(secret( 2887 "0000000000000000000000000000000000000000000000000000000000000002", 2888 )) 2889 .await 2890 .expect_err("full mailbox must reject"); 2891 assert_eq!( 2892 rejected.message().as_str(), 2893 "The runtime is busy. Try again." 2894 ); 2895 2896 secrets.release(); 2897 first.await.expect("first task").expect("first command"); 2898 let second = second 2899 .await 2900 .expect("second task") 2901 .expect_err("accepted stale revision conflicts explicitly"); 2902 assert_eq!( 2903 second.message().as_str(), 2904 "The identity operation conflicts with the current application state." 2905 ); 2906 assert_eq!( 2907 actor 2908 .bootstrap() 2909 .await 2910 .expect("snapshot") 2911 .identities() 2912 .len(), 2913 1 2914 ); 2915 } 2916 2917 #[tokio::test(flavor = "multi_thread", worker_threads = 4)] 2918 async fn queued_command_expiry_returns_timeout_and_prevents_late_mutation() { 2919 let secrets = Arc::new(BlockingSecretStore::new()); 2920 let actor = RuntimeActorHandle::in_memory( 2921 RelayConfiguration::default(), 2922 dependencies(secrets.clone(), Arc::new(OfflineNostr)), 2923 NonZeroUsize::new(1).expect("capacity"), 2924 &tokio::runtime::Handle::current(), 2925 ) 2926 .await 2927 .expect("actor"); 2928 2929 let first_actor = actor.clone(); 2930 let first = tokio::spawn(async move { 2931 first_actor 2932 .import_secret_key_test(secret( 2933 "7e7e9c42a91bfef19fa7ea99d52d8afdb67d893a8fefba1f5cb9793f2107f6d7", 2934 )) 2935 .await 2936 }); 2937 secrets.wait_until_put_started().await; 2938 2939 let expired = actor 2940 .import_secret_key_with_timeout( 2941 secret("0000000000000000000000000000000000000000000000000000000000000001"), 2942 Duration::from_millis(10), 2943 ) 2944 .await 2945 .expect_err("queued command must time out"); 2946 assert_eq!(expired.message().as_str(), "The runtime command timed out."); 2947 2948 secrets.release(); 2949 first.await.expect("first task").expect("first command"); 2950 assert_eq!( 2951 actor 2952 .bootstrap() 2953 .await 2954 .expect("snapshot") 2955 .identities() 2956 .len(), 2957 1 2958 ); 2959 } 2960 2961 #[tokio::test(flavor = "multi_thread")] 2962 async fn close_is_terminal_and_every_later_command_is_rejected_as_closed() { 2963 let (actor, _) = actor().await; 2964 actor.close().await.expect("close"); 2965 assert_eq!(actor.lifecycle(), RuntimeLifecycle::Closed); 2966 2967 let error = actor.bootstrap().await.expect_err("bootstrap after close"); 2968 assert_eq!( 2969 error.message().as_str(), 2970 "The application runtime is closed." 2971 ); 2972 actor.close().await.expect("repeated close is idempotent"); 2973 } 2974 2975 #[tokio::test(flavor = "multi_thread")] 2976 async fn actor_subscription_atomically_delivers_initial_then_ordered_changes() { 2977 let (actor, _) = actor().await; 2978 let mut subscription = actor 2979 .subscribe_changes(NonZeroUsize::new(4).expect("capacity")) 2980 .await 2981 .expect("subscribe"); 2982 let initial = subscription.receive().await.expect("initial snapshot"); 2983 assert_eq!(initial.revision(), actor.snapshot().revision()); 2984 assert!(initial.previous_revision().is_none()); 2985 2986 actor 2987 .import_secret_key_test(secret( 2988 "7e7e9c42a91bfef19fa7ea99d52d8afdb67d893a8fefba1f5cb9793f2107f6d7", 2989 )) 2990 .await 2991 .expect("import"); 2992 let changed = subscription.receive().await.expect("change"); 2993 assert!(changed.revision() > initial.revision()); 2994 assert_eq!(changed.previous_revision(), Some(initial.revision())); 2995 assert!( 2996 actor 2997 .unsubscribe_changes(subscription.id()) 2998 .await 2999 .expect("unsubscribe") 3000 ); 3001 assert!( 3002 !actor 3003 .unsubscribe_changes(subscription.id()) 3004 .await 3005 .expect("second unsubscribe") 3006 ); 3007 } 3008 3009 #[tokio::test] 3010 async fn cancelled_registration_before_installation_does_not_retain_actor_admission() { 3011 let (handle, secrets) = actor().await; 3012 let snapshot = handle.snapshot(); 3013 // Obtain the first identifier from an independent probe, without relying on its value. 3014 let mut probe = OrderedSnapshotChanges::new(snapshot.clone()); 3015 let (probe_id, probe_receiver) = probe.subscribe(NonZeroUsize::MIN).expect("probe id"); 3016 drop(probe_receiver); 3017 let mut owner = RuntimeActor { 3018 adapter: Arc::clone(&handle.adapter), 3019 secrets, 3020 clock: Arc::new(FixedClock), 3021 nostr: Arc::new(OfflineNostr), 3022 lifecycle: Arc::clone(&handle.lifecycle), 3023 runtime: tokio::runtime::Handle::current(), 3024 session_generation: handle.session_generation(), 3025 published_session_generation: Arc::clone(&handle.session_generation), 3026 profile_tasks: std::collections::BTreeMap::new(), 3027 changes: OrderedSnapshotChanges::new(snapshot), 3028 published_foreground_session: Arc::clone(&handle.foreground_session), 3029 generated_key_stage: GeneratedKeyStage::default(), 3030 }; 3031 let (mailbox, mut receiver) = ActorMailbox::bounded(NonZeroUsize::MIN); 3032 let context = CommandContext::new( 3033 RequestId::new(1).expect("request id"), 3034 None, 3035 Instant::now() + DEFAULT_COMMAND_TIMEOUT, 3036 ); 3037 let ticket = match mailbox 3038 .submit(context, RuntimeCommand::SubscribeChanges(NonZeroUsize::MIN)) 3039 { 3040 CommandSubmission::Accepted(ticket) => ticket, 3041 CommandSubmission::Rejected(_) => panic!("empty test mailbox must admit registration"), 3042 }; 3043 drop(ticket); 3044 let envelope = receiver.recv().await.expect("queued registration"); 3045 let (completions, _completed) = tokio::sync::mpsc::channel(1); 3046 assert!(owner.handle_command(envelope, &completions).await); 3047 let retained_abandoned_registration = owner.changes.unsubscribe(probe_id); 3048 drop(owner); 3049 handle.close().await.expect("cleanup runtime"); 3050 3051 assert!( 3052 !retained_abandoned_registration, 3053 "cancelled queued registration must not retain admission" 3054 ); 3055 } 3056 3057 #[tokio::test] 3058 async fn abandoned_installed_subscription_releases_actor_admission_without_mutation() { 3059 let (actor, _) = actor().await; 3060 let subscription = actor 3061 .subscribe_changes(NonZeroUsize::MIN) 3062 .await 3063 .expect("installed actor subscription"); 3064 let id = subscription.id(); 3065 let unchanged_revision = actor.snapshot().revision(); 3066 drop(subscription); 3067 // Awaiting an Observe command orders any cleanup through the same actor mailbox. 3068 let snapshot = actor.bootstrap().await.expect("serialized cleanup barrier"); 3069 let retained_abandoned_registration = actor 3070 .unsubscribe_changes(id) 3071 .await 3072 .expect("registration probe"); 3073 actor.close().await.expect("cleanup runtime"); 3074 3075 assert_eq!(snapshot.revision(), unchanged_revision); 3076 assert!( 3077 !retained_abandoned_registration, 3078 "receiver abandonment must release admission without a later state mutation" 3079 ); 3080 } 3081 3082 #[tokio::test] 3083 async fn full_actor_queues_preserve_initial_and_final_tail_then_terminate_on_close() { 3084 let (actor, _) = actor().await; 3085 let mut public_keys = Vec::new(); 3086 for value in [ 3087 "7e7e9c42a91bfef19fa7ea99d52d8afdb67d893a8fefba1f5cb9793f2107f6d7", 3088 "6e7e9c42a91bfef19fa7ea99d52d8afdb67d893a8fefba1f5cb9793f2107f6d7", 3089 ] { 3090 public_keys.push( 3091 actor 3092 .import_secret_key_test(secret(value)) 3093 .await 3094 .expect("memory-store import") 3095 .identity() 3096 .public_key(), 3097 ); 3098 } 3099 actor 3100 .select_identity(public_keys[1]) 3101 .await 3102 .expect("initial selection"); 3103 let initial_revision = actor.snapshot().revision(); 3104 let mut first = actor 3105 .subscribe_changes(NonZeroUsize::MIN) 3106 .await 3107 .expect("first consumer"); 3108 let mut second = actor 3109 .subscribe_changes(NonZeroUsize::MIN) 3110 .await 3111 .expect("second consumer"); 3112 for offset in 0..4 { 3113 let selected = actor 3114 .select_identity(public_keys[offset % 2]) 3115 .await 3116 .expect("revision-changing selection"); 3117 assert_eq!( 3118 selected.revision().value(), 3119 initial_revision.value() + u64::try_from(offset).expect("offset") + 1 3120 ); 3121 } 3122 let final_snapshot = actor.snapshot(); 3123 tokio::time::timeout(Duration::from_secs(1), actor.close()) 3124 .await 3125 .expect("bounded full-queue close") 3126 .expect("close"); 3127 actor.close().await.expect("repeated close"); 3128 for subscription in [&mut first, &mut second] { 3129 let initial = tokio::time::timeout(Duration::from_secs(1), subscription.receive()) 3130 .await 3131 .expect("initial deadline") 3132 .expect("initial"); 3133 assert_eq!(initial.revision(), initial_revision); 3134 assert!(initial.previous_revision().is_none()); 3135 let tail = tokio::time::timeout(Duration::from_secs(1), subscription.receive()) 3136 .await 3137 .expect("tail deadline") 3138 .expect("final tail"); 3139 assert_eq!(tail.snapshot(), &final_snapshot); 3140 assert_eq!( 3141 tail.previous_revision() 3142 .expect("producer predecessor") 3143 .value(), 3144 final_snapshot.revision().value() - 1 3145 ); 3146 for _ in 0..2 { 3147 assert!( 3148 tokio::time::timeout(Duration::from_secs(1), subscription.receive()) 3149 .await 3150 .expect("terminal delivery deadline") 3151 .is_none() 3152 ); 3153 } 3154 } 3155 assert_eq!(actor.lifecycle(), RuntimeLifecycle::Closed); 3156 } 3157 3158 #[tokio::test(flavor = "multi_thread", worker_threads = 4)] 3159 async fn expired_queued_shutdown_does_not_close_runtime_later() { 3160 let secrets = Arc::new(BlockingSecretStore::new()); 3161 let actor = RuntimeActorHandle::in_memory( 3162 RelayConfiguration::default(), 3163 dependencies(secrets.clone(), Arc::new(OfflineNostr)), 3164 NonZeroUsize::new(1).expect("capacity"), 3165 &tokio::runtime::Handle::current(), 3166 ) 3167 .await 3168 .expect("actor"); 3169 let import_actor = actor.clone(); 3170 let import = tokio::spawn(async move { 3171 import_actor 3172 .import_secret_key_test(secret( 3173 "7e7e9c42a91bfef19fa7ea99d52d8afdb67d893a8fefba1f5cb9793f2107f6d7", 3174 )) 3175 .await 3176 }); 3177 secrets.wait_until_put_started().await; 3178 3179 let timeout = actor 3180 .close_with_timeout(Duration::from_millis(10)) 3181 .await 3182 .expect_err("queued shutdown must expire"); 3183 assert_eq!(timeout.message().as_str(), "The runtime command timed out."); 3184 secrets.release(); 3185 import.await.expect("import task").expect("import"); 3186 assert_eq!(actor.lifecycle(), RuntimeLifecycle::Ready); 3187 assert_eq!( 3188 actor 3189 .bootstrap() 3190 .await 3191 .expect("still open") 3192 .identities() 3193 .len(), 3194 1 3195 ); 3196 actor.close().await.expect("later close"); 3197 } 3198 3199 #[tokio::test(flavor = "multi_thread", worker_threads = 4)] 3200 async fn shutdown_cancels_in_flight_work_and_terminates_publication() { 3201 let client = Arc::new(BlockingNostr::new()); 3202 let actor = RuntimeActorHandle::in_memory( 3203 RelayConfiguration::new(vec![ 3204 RelayEndpoint::new( 3205 "ws://localhost:8080", 3206 RelayUrlPolicy::Local, 3207 RelayAccess::ReadWrite, 3208 ) 3209 .expect("relay"), 3210 ]) 3211 .expect("relay configuration"), 3212 dependencies(Arc::new(InMemorySecretStore::default()), client.clone()), 3213 NonZeroUsize::new(8).expect("capacity"), 3214 &tokio::runtime::Handle::current(), 3215 ) 3216 .await 3217 .expect("actor"); 3218 let imported = actor 3219 .import_secret_key_test(secret( 3220 "7e7e9c42a91bfef19fa7ea99d52d8afdb67d893a8fefba1f5cb9793f2107f6d7", 3221 )) 3222 .await 3223 .expect("import"); 3224 actor 3225 .activate_identity(imported.identity().public_key()) 3226 .await 3227 .expect("activate"); 3228 let mut changes = actor 3229 .subscribe_changes(NonZeroUsize::new(4).expect("capacity")) 3230 .await 3231 .expect("subscribe"); 3232 changes.receive().await.expect("initial"); 3233 3234 let refresh_actor = actor.clone(); 3235 let refresh = tokio::spawn(async move { refresh_actor.refresh_active_profile().await }); 3236 let started = client.started.acquire().await.expect("refresh started"); 3237 started.forget(); 3238 actor.close().await.expect("close"); 3239 3240 let cancelled = refresh 3241 .await 3242 .expect("refresh task") 3243 .expect_err("refresh closes"); 3244 assert_eq!( 3245 cancelled.message().as_str(), 3246 "The application runtime is closed." 3247 ); 3248 assert!(changes.receive().await.is_none()); 3249 assert_eq!(actor.lifecycle(), RuntimeLifecycle::Closed); 3250 } 3251 3252 fn secret(value: &str) -> SecretKeyInput { 3253 SecretKeyInput::parse(value.to_owned()).expect("valid test secret") 3254 } 3255 }