rpi_cli/host/runtime.rs
1//! In-memory ownership shared by Host adapters. No catalog, profiles, IPC,
2//! environment discovery or Agent loop is needed to create this core.
3//!
4//! One Host has one authoritative table. Adapters provide independently built
5//! owners and retain failed-start resources until native settlement is proved.
6//! The core never treats dropping an observer (or this map) as asynchronous
7//! shutdown; its owner must cancel, wait, drain and explicitly finish closing.
8
9use super::AgentId;
10use std::{
11 collections::BTreeMap,
12 sync::{
13 atomic::{AtomicBool, Ordering},
14 Arc, Mutex, MutexGuard,
15 },
16};
17use tokio::sync::RwLock;
18
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
20pub enum OwnerPhase {
21 Starting,
22 /// Startup failed without proof that all reserved resources were released.
23 /// No new start is permitted until explicit reconciliation/close.
24 StartFailed,
25 Live,
26 Closing,
27 /// Native settlement failed. Keep ownership for an explicit close retry.
28 CloseFailed,
29}
30
31pub(super) struct OwnerEntry<Owner, StartupResidue> {
32 pub phase: OwnerPhase,
33 pub owner: Option<Arc<Owner>>,
34 pub failed_start: Option<Arc<StartupResidue>>,
35}
36
37/// Not Clone: sharing a Host means sharing this exact table through its owner,
38/// never copying members into a second registry. No entry lock crosses await.
39pub(super) struct HostRuntime<Owner, StartupResidue = ()> {
40 entries: Mutex<BTreeMap<AgentId, OwnerEntry<Owner, StartupResidue>>>,
41 // A transaction holds a reader; whole-Host stop holds the fair writer.
42 // Admission checks stopping both before and after acquiring its reader.
43 pub lifecycle: RwLock<()>,
44 stopping: Arc<AtomicBool>,
45 closed: AtomicBool,
46}
47
48impl<Owner, StartupResidue> HostRuntime<Owner, StartupResidue> {
49 pub fn new() -> Self {
50 Self {
51 entries: Mutex::new(BTreeMap::new()),
52 lifecycle: RwLock::new(()),
53 stopping: Arc::new(AtomicBool::new(false)),
54 closed: AtomicBool::new(false),
55 }
56 }
57
58 pub fn entries(&self) -> MutexGuard<'_, BTreeMap<AgentId, OwnerEntry<Owner, StartupResidue>>> {
59 // A panic must not silently discard live owners or cleanup custody.
60 self.entries
61 .lock()
62 .unwrap_or_else(|poison| poison.into_inner())
63 }
64
65 pub fn owner_count(&self) -> usize {
66 self.entries().len()
67 }
68
69 pub fn phase(&self, id: AgentId) -> Option<OwnerPhase> {
70 self.entries().get(&id).map(|entry| entry.phase)
71 }
72
73 pub fn is_stopping(&self) -> bool {
74 self.stopping.load(Ordering::Acquire)
75 }
76
77 /// Existing Mini observers share this exact admission seal, not a copied
78 /// stopping flag or a parallel owner table.
79 pub fn stopping_signal(&self) -> Arc<AtomicBool> {
80 self.stopping.clone()
81 }
82
83 pub fn is_closed(&self) -> bool {
84 self.closed.load(Ordering::Acquire)
85 }
86
87 /// Signal admission rejection; existing readers still own their transaction.
88 /// This is not proof of cancellation, native settlement or storage closure.
89 pub fn begin_stopping(&self) {
90 self.stopping.store(true, Ordering::Release);
91 }
92
93 /// Adapter holds the exclusive lifecycle reservation and has established
94 /// native settlement plus any required external storage shutdown boundary.
95 pub fn mark_closed(&self) {
96 self.closed.store(true, Ordering::Release);
97 }
98}
99
100#[cfg(test)]
101mod tests;