1use meerkat_core::lifecycle::{InputId, RunId};
6use serde::{Deserialize, Serialize};
7
8use crate::accept::AcceptOutcome;
9use crate::identifiers::LogicalRuntimeId;
10use crate::input::Input;
11use crate::input_state::{InputLifecycleState, InputState, StoredInputState};
12use crate::runtime_event::RuntimeEventEnvelope;
13use crate::runtime_state::RuntimeState;
14
15#[derive(Debug, Clone, thiserror::Error)]
17#[non_exhaustive]
18pub enum RuntimeDriverError {
19 #[error("Runtime not ready: {state}")]
21 NotReady { state: RuntimeState },
22
23 #[error("Runtime not found: {runtime_id}")]
30 NotFound { runtime_id: LogicalRuntimeId },
31
32 #[error("Input validation failed: {reason}")]
34 ValidationFailed { reason: String },
35
36 #[error("Runtime destroyed")]
38 Destroyed,
39
40 #[error("Recovery corruption: {reason}")]
42 RecoveryCorruption { reason: String },
43
44 #[error("Internal error: {0}")]
46 Internal(String),
47}
48
49#[derive(Debug, Clone, thiserror::Error)]
51#[non_exhaustive]
52pub enum RuntimeControlPlaneError {
53 #[error("Runtime not found: {0}")]
55 NotFound(LogicalRuntimeId),
56
57 #[error("Invalid state for operation: {state}")]
59 InvalidState { state: RuntimeState },
60
61 #[error("Store error: {0}")]
63 StoreError(String),
64
65 #[error("Internal error: {0}")]
67 Internal(String),
68}
69
70#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct RecoveryReport {
73 pub inputs_recovered: usize,
75 pub inputs_abandoned: usize,
77 pub inputs_requeued: usize,
79 #[serde(default, skip_serializing_if = "Vec::is_empty")]
81 pub details: Vec<String>,
82}
83
84#[derive(Debug, Clone, Serialize, Deserialize)]
86pub struct RetireReport {
87 pub inputs_abandoned: usize,
89 #[serde(default)]
91 pub inputs_pending_drain: usize,
92}
93
94#[derive(Debug, Clone, Serialize, Deserialize)]
96pub struct ResetReport {
97 pub inputs_abandoned: usize,
99}
100
101#[derive(Debug, Clone, Serialize, Deserialize)]
103pub struct RecycleReport {
104 pub inputs_transferred: usize,
106}
107
108#[derive(Debug, Clone, Serialize, Deserialize)]
110pub struct DestroyReport {
111 pub inputs_abandoned: usize,
113}
114
115#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
120#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
121pub trait RuntimeDriver: Send + Sync {
122 async fn accept_input(&mut self, input: Input) -> Result<AcceptOutcome, RuntimeDriverError>;
124
125 async fn on_runtime_event(
127 &mut self,
128 event: RuntimeEventEnvelope,
129 ) -> Result<(), RuntimeDriverError>;
130
131 async fn recover(&mut self) -> Result<RecoveryReport, RuntimeDriverError>;
133
134 fn runtime_state(&self) -> RuntimeState;
136
137 fn input_state(&self, input_id: &InputId) -> Option<&InputState>;
139
140 fn input_phase(&self, input_id: &InputId) -> Option<InputLifecycleState>;
142
143 fn input_last_run_id(&self, input_id: &InputId) -> Option<RunId>;
145
146 fn input_last_boundary_sequence(&self, input_id: &InputId) -> Option<u64>;
148
149 fn stored_input_state(&self, input_id: &InputId) -> Option<StoredInputState>;
151
152 fn input_id_for_idempotency_key(&self, idempotency_key: &str) -> Option<InputId>;
158
159 fn active_input_ids(&self) -> Vec<InputId>;
161}
162
163#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
165#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
166pub trait RuntimeControlPlane: Send + Sync {
167 async fn ingest(
169 &self,
170 runtime_id: &LogicalRuntimeId,
171 input: Input,
172 ) -> Result<AcceptOutcome, RuntimeControlPlaneError>;
173
174 async fn publish_event(
176 &self,
177 event: RuntimeEventEnvelope,
178 ) -> Result<(), RuntimeControlPlaneError>;
179
180 async fn retire(
182 &self,
183 runtime_id: &LogicalRuntimeId,
184 ) -> Result<RetireReport, RuntimeControlPlaneError>;
185
186 async fn recycle(
188 &self,
189 runtime_id: &LogicalRuntimeId,
190 ) -> Result<RecycleReport, RuntimeControlPlaneError>;
191
192 async fn reset(
194 &self,
195 runtime_id: &LogicalRuntimeId,
196 ) -> Result<ResetReport, RuntimeControlPlaneError>;
197
198 async fn recover(
200 &self,
201 runtime_id: &LogicalRuntimeId,
202 ) -> Result<RecoveryReport, RuntimeControlPlaneError>;
203
204 async fn runtime_state(
206 &self,
207 runtime_id: &LogicalRuntimeId,
208 ) -> Result<RuntimeState, RuntimeControlPlaneError>;
209
210 async fn destroy(
212 &self,
213 runtime_id: &LogicalRuntimeId,
214 ) -> Result<DestroyReport, RuntimeControlPlaneError>;
215
216 async fn load_boundary_receipt(
218 &self,
219 runtime_id: &LogicalRuntimeId,
220 run_id: &RunId,
221 sequence: u64,
222 ) -> Result<Option<meerkat_core::lifecycle::RunBoundaryReceipt>, RuntimeControlPlaneError>;
223}
224
225#[cfg(test)]
226#[allow(clippy::unwrap_used)]
227mod tests {
228 use super::*;
229
230 fn _assert_driver_object_safe(_: &dyn RuntimeDriver) {}
232 fn _assert_control_plane_object_safe(_: &dyn RuntimeControlPlane) {}
233
234 #[test]
235 fn runtime_driver_error_display() {
236 let err = RuntimeDriverError::NotReady {
237 state: RuntimeState::Initializing,
238 };
239 assert!(err.to_string().contains("initializing"));
240
241 let err = RuntimeDriverError::ValidationFailed {
242 reason: "bad input".into(),
243 };
244 assert!(err.to_string().contains("bad input"));
245 }
246
247 #[test]
248 fn runtime_control_plane_error_display() {
249 let err = RuntimeControlPlaneError::NotFound(LogicalRuntimeId::new("missing"));
250 assert!(err.to_string().contains("missing"));
251 }
252
253 #[test]
254 fn recovery_report_serde() {
255 let report = RecoveryReport {
256 inputs_recovered: 5,
257 inputs_abandoned: 1,
258 inputs_requeued: 3,
259 details: vec!["requeued 3 staged inputs".into()],
260 };
261 let json = serde_json::to_value(&report).unwrap();
262 let parsed: RecoveryReport = serde_json::from_value(json).unwrap();
263 assert_eq!(parsed.inputs_recovered, 5);
264 }
265}