Skip to main content

meerkat_runtime/
traits.rs

1//! ยง23 Runtime traits โ€” RuntimeDriver and RuntimeControlPlane.
2//!
3//! These define the interface between surfaces and the runtime control-plane.
4
5use meerkat_core::lifecycle::{InputId, RunEvent, RunId};
6use serde::{Deserialize, Serialize};
7
8use crate::accept::AcceptOutcome;
9use crate::identifiers::LogicalRuntimeId;
10use crate::input::Input;
11use crate::input_state::InputState;
12use crate::runtime_event::RuntimeEventEnvelope;
13use crate::runtime_state::RuntimeState;
14
15/// Errors from RuntimeDriver operations.
16#[derive(Debug, Clone, thiserror::Error)]
17#[non_exhaustive]
18pub enum RuntimeDriverError {
19    /// The runtime is not in a state that can accept this operation.
20    #[error("Runtime not ready: {state}")]
21    NotReady { state: RuntimeState },
22
23    /// Input validation failed.
24    #[error("Input validation failed: {reason}")]
25    ValidationFailed { reason: String },
26
27    /// The runtime has been destroyed.
28    #[error("Runtime destroyed")]
29    Destroyed,
30
31    /// Internal error.
32    #[error("Internal error: {0}")]
33    Internal(String),
34}
35
36/// Errors from RuntimeControlPlane operations.
37#[derive(Debug, Clone, thiserror::Error)]
38#[non_exhaustive]
39pub enum RuntimeControlPlaneError {
40    /// Runtime not found.
41    #[error("Runtime not found: {0}")]
42    NotFound(LogicalRuntimeId),
43
44    /// Invalid state for this operation.
45    #[error("Invalid state for operation: {state}")]
46    InvalidState { state: RuntimeState },
47
48    /// Store error.
49    #[error("Store error: {0}")]
50    StoreError(String),
51
52    /// Internal error.
53    #[error("Internal error: {0}")]
54    Internal(String),
55}
56
57/// Runtime control commands (distinct from RunControlCommand which is core-level).
58#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
59#[serde(tag = "command", rename_all = "snake_case")]
60pub enum RuntimeControlCommand {
61    /// Stop the runtime gracefully.
62    Stop,
63    /// Resume the runtime after recovery.
64    Resume,
65}
66
67/// Report from a recovery operation.
68#[derive(Debug, Clone, Serialize, Deserialize)]
69pub struct RecoveryReport {
70    /// How many inputs were recovered.
71    pub inputs_recovered: usize,
72    /// How many inputs were abandoned during recovery.
73    pub inputs_abandoned: usize,
74    /// How many inputs were re-queued.
75    pub inputs_requeued: usize,
76    /// Details of recovery actions.
77    #[serde(default, skip_serializing_if = "Vec::is_empty")]
78    pub details: Vec<String>,
79}
80
81/// Report from a retire operation.
82#[derive(Debug, Clone, Serialize, Deserialize)]
83pub struct RetireReport {
84    /// How many non-terminal inputs were abandoned.
85    pub inputs_abandoned: usize,
86    /// How many inputs are pending drain (will be processed before stopping).
87    #[serde(default)]
88    pub inputs_pending_drain: usize,
89}
90
91/// Report from a reset operation.
92#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct ResetReport {
94    /// How many non-terminal inputs were abandoned.
95    pub inputs_abandoned: usize,
96}
97
98/// Report from a respawn operation.
99#[derive(Debug, Clone, Serialize, Deserialize)]
100pub struct RespawnReport {
101    /// How many inputs were transferred to the new instance.
102    pub inputs_transferred: usize,
103}
104
105/// Report from a destroy operation.
106#[derive(Debug, Clone, Serialize, Deserialize)]
107pub struct DestroyReport {
108    /// How many non-terminal inputs were abandoned.
109    pub inputs_abandoned: usize,
110}
111
112/// The runtime driver โ€” per-session interface for input acceptance and lifecycle.
113///
114/// Each session gets its own RuntimeDriver instance. The driver manages the
115/// InputState ledger, policy resolution, and input queue for that session.
116#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
117#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
118pub trait RuntimeDriver: Send + Sync {
119    /// Accept an input into the runtime.
120    async fn accept_input(&mut self, input: Input) -> Result<AcceptOutcome, RuntimeDriverError>;
121
122    /// Handle a runtime event (from the event bus).
123    async fn on_runtime_event(
124        &mut self,
125        event: RuntimeEventEnvelope,
126    ) -> Result<(), RuntimeDriverError>;
127
128    /// Handle a run event (from core).
129    async fn on_run_event(&mut self, event: RunEvent) -> Result<(), RuntimeDriverError>;
130
131    /// Handle a runtime control command.
132    async fn on_runtime_control(
133        &mut self,
134        command: RuntimeControlCommand,
135    ) -> Result<(), RuntimeDriverError>;
136
137    /// Recover from a crash/restart.
138    async fn recover(&mut self) -> Result<RecoveryReport, RuntimeDriverError>;
139
140    /// Retire the runtime (no new input, abandon pending).
141    async fn retire(&mut self) -> Result<RetireReport, RuntimeDriverError>;
142
143    /// Reset the runtime (abandon all pending input, drain queue).
144    async fn reset(&mut self) -> Result<ResetReport, RuntimeDriverError>;
145
146    /// Get the current runtime state.
147    fn runtime_state(&self) -> RuntimeState;
148
149    /// Get the state of a specific input.
150    fn input_state(&self, input_id: &InputId) -> Option<&InputState>;
151
152    /// List all non-terminal input IDs.
153    fn active_input_ids(&self) -> Vec<InputId>;
154}
155
156/// The runtime control plane โ€” manages multiple runtime instances.
157#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
158#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
159pub trait RuntimeControlPlane: Send + Sync {
160    /// Ingest an input into a specific runtime.
161    async fn ingest(
162        &self,
163        runtime_id: &LogicalRuntimeId,
164        input: Input,
165    ) -> Result<AcceptOutcome, RuntimeControlPlaneError>;
166
167    /// Publish a runtime event.
168    async fn publish_event(
169        &self,
170        event: RuntimeEventEnvelope,
171    ) -> Result<(), RuntimeControlPlaneError>;
172
173    /// Retire a runtime (no new input, drain existing).
174    async fn retire(
175        &self,
176        runtime_id: &LogicalRuntimeId,
177    ) -> Result<RetireReport, RuntimeControlPlaneError>;
178
179    /// Respawn a runtime (transfer pending input to new instance).
180    async fn respawn(
181        &self,
182        runtime_id: &LogicalRuntimeId,
183    ) -> Result<RespawnReport, RuntimeControlPlaneError>;
184
185    /// Reset a runtime (abandon all pending input).
186    async fn reset(
187        &self,
188        runtime_id: &LogicalRuntimeId,
189    ) -> Result<ResetReport, RuntimeControlPlaneError>;
190
191    /// Recover a runtime from crash.
192    async fn recover(
193        &self,
194        runtime_id: &LogicalRuntimeId,
195    ) -> Result<RecoveryReport, RuntimeControlPlaneError>;
196
197    /// Get the state of a runtime.
198    async fn runtime_state(
199        &self,
200        runtime_id: &LogicalRuntimeId,
201    ) -> Result<RuntimeState, RuntimeControlPlaneError>;
202
203    /// Load a boundary receipt for verification.
204    async fn load_boundary_receipt(
205        &self,
206        runtime_id: &LogicalRuntimeId,
207        run_id: &RunId,
208        sequence: u64,
209    ) -> Result<Option<meerkat_core::lifecycle::RunBoundaryReceipt>, RuntimeControlPlaneError>;
210}
211
212#[cfg(test)]
213#[allow(clippy::unwrap_used)]
214mod tests {
215    use super::*;
216
217    // Verify traits are object-safe
218    fn _assert_driver_object_safe(_: &dyn RuntimeDriver) {}
219    fn _assert_control_plane_object_safe(_: &dyn RuntimeControlPlane) {}
220
221    #[test]
222    fn runtime_control_command_serde() {
223        let cmd = RuntimeControlCommand::Stop;
224        let json = serde_json::to_value(&cmd).unwrap();
225        assert_eq!(json["command"], "stop");
226
227        let cmd = RuntimeControlCommand::Resume;
228        let json = serde_json::to_value(&cmd).unwrap();
229        assert_eq!(json["command"], "resume");
230    }
231
232    #[test]
233    fn runtime_driver_error_display() {
234        let err = RuntimeDriverError::NotReady {
235            state: RuntimeState::Initializing,
236        };
237        assert!(err.to_string().contains("initializing"));
238
239        let err = RuntimeDriverError::ValidationFailed {
240            reason: "bad input".into(),
241        };
242        assert!(err.to_string().contains("bad input"));
243    }
244
245    #[test]
246    fn runtime_control_plane_error_display() {
247        let err = RuntimeControlPlaneError::NotFound(LogicalRuntimeId::new("missing"));
248        assert!(err.to_string().contains("missing"));
249    }
250
251    #[test]
252    fn recovery_report_serde() {
253        let report = RecoveryReport {
254            inputs_recovered: 5,
255            inputs_abandoned: 1,
256            inputs_requeued: 3,
257            details: vec!["requeued 3 staged inputs".into()],
258        };
259        let json = serde_json::to_value(&report).unwrap();
260        let parsed: RecoveryReport = serde_json::from_value(json).unwrap();
261        assert_eq!(parsed.inputs_recovered, 5);
262    }
263}