Skip to main content

meerkat_mob/runtime/
state.rs

1#[cfg(test)]
2use super::mob_orchestrator_authority::MobOrchestratorSnapshot;
3use super::*;
4use crate::run::MobRun;
5#[cfg(target_arch = "wasm32")]
6use crate::tokio;
7
8// ---------------------------------------------------------------------------
9// MobState
10// ---------------------------------------------------------------------------
11
12/// Lifecycle state of a mob, stored as `Arc<AtomicU8>` for lock-free reads.
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14#[repr(u8)]
15pub enum MobState {
16    Creating = 0,
17    Running = 1,
18    Stopped = 2,
19    Completed = 3,
20    Destroyed = 4,
21}
22
23impl MobState {
24    pub(super) fn from_u8(v: u8) -> Self {
25        match v {
26            0 => Self::Creating,
27            1 => Self::Running,
28            2 => Self::Stopped,
29            3 => Self::Completed,
30            4 => Self::Destroyed,
31            _ => {
32                debug_assert!(false, "invalid mob lifecycle state byte: {v}");
33                tracing::error!(state_byte = v, "invalid mob lifecycle state byte");
34                Self::Destroyed
35            }
36        }
37    }
38
39    /// Human-readable name for the state.
40    pub fn as_str(self) -> &'static str {
41        match self {
42            Self::Creating => "Creating",
43            Self::Running => "Running",
44            Self::Stopped => "Stopped",
45            Self::Completed => "Completed",
46            Self::Destroyed => "Destroyed",
47        }
48    }
49}
50
51impl std::fmt::Display for MobState {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        f.write_str(self.as_str())
54    }
55}
56
57// ---------------------------------------------------------------------------
58// MobCommand
59// ---------------------------------------------------------------------------
60
61/// Commands sent from [`MobHandle`] to the [`MobActor`] for serialized processing.
62pub(super) enum MobCommand {
63    Spawn {
64        spec: Box<super::handle::SpawnMemberSpec>,
65        owner_session_id: Option<SessionId>,
66        ops_registry: Option<Arc<dyn meerkat_core::ops_lifecycle::OpsLifecycleRegistry>>,
67        reply_tx: oneshot::Sender<Result<super::handle::MemberSpawnReceipt, MobError>>,
68    },
69    SpawnProvisioned {
70        spawn_ticket: u64,
71        result: Result<super::handle::MemberSpawnReceipt, MobError>,
72    },
73    Retire {
74        meerkat_id: MeerkatId,
75        reply_tx: oneshot::Sender<Result<(), MobError>>,
76    },
77    Respawn {
78        meerkat_id: MeerkatId,
79        initial_message: Option<ContentInput>,
80        reply_tx: oneshot::Sender<
81            Result<super::handle::MemberRespawnReceipt, super::handle::MobRespawnError>,
82        >,
83    },
84    RetireAll {
85        reply_tx: oneshot::Sender<Result<(), MobError>>,
86    },
87    Wire {
88        local: MeerkatId,
89        target: super::handle::PeerTarget,
90        reply_tx: oneshot::Sender<Result<(), MobError>>,
91    },
92    Unwire {
93        local: MeerkatId,
94        target: super::handle::PeerTarget,
95        reply_tx: oneshot::Sender<Result<(), MobError>>,
96    },
97    ExternalTurn {
98        meerkat_id: MeerkatId,
99        content: ContentInput,
100        handling_mode: meerkat_core::types::HandlingMode,
101        render_metadata: Option<meerkat_core::types::RenderMetadata>,
102        reply_tx: oneshot::Sender<Result<SessionId, MobError>>,
103    },
104    InternalTurn {
105        meerkat_id: MeerkatId,
106        content: ContentInput,
107        reply_tx: oneshot::Sender<Result<SessionId, MobError>>,
108    },
109    KickoffBarrierSnapshot {
110        meerkat_ids: Vec<MeerkatId>,
111        reply_tx: oneshot::Sender<Vec<(MeerkatId, tokio::sync::watch::Receiver<bool>)>>,
112    },
113    KickoffOutcomeResolved {
114        meerkat_id: MeerkatId,
115        outcome: meerkat_runtime::completion::CompletionOutcome,
116        ack_tx: oneshot::Sender<()>,
117    },
118    RunFlow {
119        flow_id: FlowId,
120        activation_params: serde_json::Value,
121        scoped_event_tx: Option<tokio::sync::mpsc::Sender<meerkat_core::ScopedAgentEvent>>,
122        reply_tx: oneshot::Sender<Result<RunId, MobError>>,
123    },
124    CancelFlow {
125        run_id: RunId,
126        reply_tx: oneshot::Sender<Result<(), MobError>>,
127    },
128    FlowStatus {
129        run_id: RunId,
130        reply_tx: oneshot::Sender<Result<Option<MobRun>, MobError>>,
131    },
132    FlowFinished {
133        run_id: RunId,
134    },
135    FlowCanceledCleanup {
136        run_id: RunId,
137    },
138    #[cfg(test)]
139    FlowTrackerCounts {
140        reply_tx: oneshot::Sender<(usize, usize)>,
141    },
142    #[cfg(test)]
143    OrchestratorSnapshot {
144        reply_tx: oneshot::Sender<MobOrchestratorSnapshot>,
145    },
146    Stop {
147        reply_tx: oneshot::Sender<Result<(), MobError>>,
148    },
149    ResumeLifecycle {
150        reply_tx: oneshot::Sender<Result<(), MobError>>,
151    },
152    Complete {
153        reply_tx: oneshot::Sender<Result<(), MobError>>,
154    },
155    Destroy {
156        reply_tx: oneshot::Sender<Result<(), MobError>>,
157    },
158    Reset {
159        reply_tx: oneshot::Sender<Result<(), MobError>>,
160    },
161    TaskCreate {
162        subject: String,
163        description: String,
164        blocked_by: Vec<TaskId>,
165        reply_tx: oneshot::Sender<Result<TaskId, MobError>>,
166    },
167    TaskUpdate {
168        task_id: TaskId,
169        status: TaskStatus,
170        owner: Option<MeerkatId>,
171        reply_tx: oneshot::Sender<Result<(), MobError>>,
172    },
173    ForceCancel {
174        meerkat_id: MeerkatId,
175        reply_tx: oneshot::Sender<Result<(), MobError>>,
176    },
177    SetSpawnPolicy {
178        policy: Option<Arc<dyn super::spawn_policy::SpawnPolicy>>,
179        reply_tx: oneshot::Sender<()>,
180    },
181    Shutdown {
182        reply_tx: oneshot::Sender<Result<(), MobError>>,
183    },
184}