mecha10-core 0.6.5

Mecha10 core runtime and foundational types
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Lifecycle event schemas for mode-based node management
//!
//! This module defines the event types used for dynamic lifecycle management:
//! - Mode change requests and confirmations
//! - Node lifecycle events
//! - Node status reporting
//!
//! # Event Flow
//!
//! ```text
//! CLI → ModeRequest → Runtime Lifecycle → Supervisor → Nodes
//!                                       ↓
//!                                 ModeChanged
//! ```
//!
//! # Example
//!
//! ```rust,ignore
//! use mecha10_core::lifecycle::*;
//!
//! // Request mode change
//! context.publish(
//!     TOPIC_MODE_REQUEST,
//!     &ModeRequest {
//!         mode: "simulation".to_string(),
//!         requested_by: "cli".to_string(),
//!         timestamp: get_timestamp_ms(),
//!     }
//! ).await?;
//!
//! // Listen for mode changes
//! let mut sub = context.subscribe(TOPIC_MODE_CHANGED).await?;
//! while let Some(msg) = sub.next().await {
//!     let event: ModeChanged = msg.decode()?;
//!     println!("Mode changed to: {}", event.current_mode);
//! }
//! ```

use serde::{Deserialize, Serialize};

/// Request to change operational mode
///
/// Published by CLI or nodes to request a mode transition.
/// Runtime lifecycle manager subscribes to these requests.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ModeRequest {
    /// Target mode name (e.g., "simulation", "teleop", "autonomous")
    pub mode: String,

    /// Who requested the change (e.g., "cli", "mission-controller")
    pub requested_by: String,

    /// Unix timestamp in milliseconds
    pub timestamp: u64,
}

/// Notification that mode has changed successfully
///
/// Published by runtime lifecycle manager after mode transition completes.
/// Nodes can subscribe to react to mode changes.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ModeChanged {
    /// Previous mode name
    pub previous_mode: String,

    /// New current mode name
    pub current_mode: String,

    /// Nodes that were started during transition
    pub nodes_started: Vec<String>,

    /// Nodes that were stopped during transition
    pub nodes_stopped: Vec<String>,

    /// Unix timestamp in milliseconds
    pub timestamp: u64,
}

/// Error during mode transition
///
/// Published by runtime lifecycle manager if mode change fails.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ModeError {
    /// Requested mode that failed
    pub mode: String,

    /// Error message
    pub error: String,

    /// Current mode (unchanged)
    pub current_mode: String,

    /// Unix timestamp in milliseconds
    pub timestamp: u64,
}

/// Lifecycle action for a specific node (internal to runtime)
///
/// Note: These events are primarily for internal runtime use.
/// Most users should use mode requests instead.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct NodeLifecycleEvent {
    /// Node name
    pub node: String,

    /// Action to perform
    pub action: NodeLifecycleAction,

    /// Reason for action
    pub reason: String,

    /// Target mode context (if mode change)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub target_mode: Option<String>,

    /// Graceful shutdown timeout in milliseconds
    #[serde(default = "default_graceful_timeout")]
    pub graceful_timeout_ms: u64,

    /// Node-specific configuration
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub config: Option<serde_json::Value>,
}

/// Actions that can be performed on a node
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum NodeLifecycleAction {
    /// Start the node
    Start,

    /// Stop the node gracefully
    Stop,

    /// Force kill the node (if graceful stop times out)
    Kill,

    /// Restart the node (stop then start)
    Restart,
}

/// Node status report
///
/// Published by runtime supervisor to report node state changes.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct NodeStatusEvent {
    /// Node name
    pub node: String,

    /// Current status
    pub status: NodeLifecycleStatus,

    /// Process ID (if running)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pid: Option<u32>,

    /// Error message (if failed)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,

    /// Unix timestamp in milliseconds
    pub timestamp: u64,
}

/// Lifecycle status of a node
///
/// This represents the operational state of a node process.
/// Note: This is different from `discovery::NodeStatus` which is for service discovery.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum NodeLifecycleStatus {
    /// Node is starting up
    Starting,

    /// Node successfully started
    Started,

    /// Node is running normally
    Running,

    /// Node is shutting down
    Stopping,

    /// Node has stopped
    Stopped,

    /// Node failed to start or crashed
    Failed,
}

/// One-shot dashboard-initiated trigger event for a node
///
/// Published by [`crate::command_relay::CommandRelay`] when it relays a
/// `Command::Trigger` (LAB-2082). Nodes that expose a single-shot action (e.g.
/// image-classifier's one-shot classify) may subscribe to
/// [`topic_node_trigger`] alongside their own node-specific control topic to
/// receive these without `CommandRelay` needing to know each node's own
/// control-topic wire shape.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct NodeTriggerEvent {
    /// Node name the trigger targets
    pub node: String,

    /// Unix timestamp in milliseconds
    pub timestamp: u64,
}

impl crate::messages::Message for NodeTriggerEvent {}

/// Topic for mode change requests
///
/// Format: `system/lifecycle/mode/request`
///
/// Payload: [`ModeRequest`]
pub const TOPIC_MODE_REQUEST: &str = "system/lifecycle/mode/request";

/// Topic for mode change confirmations
///
/// Format: `system/lifecycle/mode/changed`
///
/// Payload: [`ModeChanged`]
pub const TOPIC_MODE_CHANGED: &str = "system/lifecycle/mode/changed";

/// Topic for mode change errors
///
/// Format: `system/lifecycle/mode/error`
///
/// Payload: [`ModeError`]
pub const TOPIC_MODE_ERROR: &str = "system/lifecycle/mode/error";

/// Get topic for node lifecycle events (internal)
///
/// Format: `system/node/{node}/lifecycle`
///
/// Payload: [`NodeLifecycleEvent`]
///
/// Note: Primarily for internal runtime use
pub fn topic_node_lifecycle(node: &str) -> String {
    format!("system/node/{}/lifecycle", node)
}

/// Get topic for node status events
///
/// Format: `system/node/{node}/status`
///
/// Payload: [`NodeStatusEvent`]
pub fn topic_node_status(node: &str) -> String {
    format!("system/node/{}/status", node)
}

/// Get topic for one-shot dashboard trigger events
///
/// Format: `system/node/{node}/trigger`
///
/// Payload: [`NodeTriggerEvent`]
pub fn topic_node_trigger(node: &str) -> String {
    format!("system/node/{}/trigger", node)
}

/// Default graceful shutdown timeout (5 seconds)
fn default_graceful_timeout() -> u64 {
    5000
}

/// Get current timestamp in milliseconds since Unix epoch
pub fn get_timestamp_ms() -> u64 {
    crate::prelude::now_millis()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_mode_request_serialization() {
        let request = ModeRequest {
            mode: "simulation".to_string(),
            requested_by: "cli".to_string(),
            timestamp: 1234567890,
        };

        let json = serde_json::to_string(&request).unwrap();
        let deserialized: ModeRequest = serde_json::from_str(&json).unwrap();

        assert_eq!(request, deserialized);
    }

    #[test]
    fn test_mode_changed_serialization() {
        let changed = ModeChanged {
            previous_mode: "startup".to_string(),
            current_mode: "simulation".to_string(),
            nodes_started: vec!["sim-bridge".to_string(), "object-detector".to_string()],
            nodes_stopped: vec!["imu".to_string(), "motor".to_string()],
            timestamp: 1234567890,
        };

        let json = serde_json::to_string(&changed).unwrap();
        let deserialized: ModeChanged = serde_json::from_str(&json).unwrap();

        assert_eq!(changed, deserialized);
    }

    #[test]
    fn test_node_lifecycle_action_serialization() {
        let actions = vec![
            NodeLifecycleAction::Start,
            NodeLifecycleAction::Stop,
            NodeLifecycleAction::Kill,
            NodeLifecycleAction::Restart,
        ];

        for action in actions {
            let json = serde_json::to_string(&action).unwrap();
            let deserialized: NodeLifecycleAction = serde_json::from_str(&json).unwrap();
            assert_eq!(action, deserialized);
        }
    }

    #[test]
    fn test_node_lifecycle_status_serialization() {
        let statuses = vec![
            NodeLifecycleStatus::Starting,
            NodeLifecycleStatus::Started,
            NodeLifecycleStatus::Running,
            NodeLifecycleStatus::Stopping,
            NodeLifecycleStatus::Stopped,
            NodeLifecycleStatus::Failed,
        ];

        for status in statuses {
            let json = serde_json::to_string(&status).unwrap();
            let deserialized: NodeLifecycleStatus = serde_json::from_str(&json).unwrap();
            assert_eq!(status, deserialized);
        }
    }

    #[test]
    fn test_topic_helpers() {
        assert_eq!(topic_node_lifecycle("test-node"), "system/node/test-node/lifecycle");
        assert_eq!(topic_node_status("test-node"), "system/node/test-node/status");
        assert_eq!(topic_node_trigger("test-node"), "system/node/test-node/trigger");
    }

    #[test]
    fn test_node_trigger_event_serialization() {
        let event = NodeTriggerEvent {
            node: "image-classifier".to_string(),
            timestamp: 1234567890,
        };

        let json = serde_json::to_string(&event).unwrap();
        let deserialized: NodeTriggerEvent = serde_json::from_str(&json).unwrap();

        assert_eq!(event, deserialized);
    }

    #[test]
    fn test_default_graceful_timeout() {
        let event = NodeLifecycleEvent {
            node: "test".to_string(),
            action: NodeLifecycleAction::Stop,
            reason: "test".to_string(),
            target_mode: None,
            graceful_timeout_ms: default_graceful_timeout(),
            config: None,
        };

        assert_eq!(event.graceful_timeout_ms, 5000);
    }

    #[test]
    fn test_get_timestamp_ms() {
        let ts1 = get_timestamp_ms();
        std::thread::sleep(std::time::Duration::from_millis(10));
        let ts2 = get_timestamp_ms();

        assert!(ts2 > ts1);
        assert!(ts2 - ts1 >= 10);
    }

    #[test]
    fn test_node_status_event_optional_fields() {
        let event1 = NodeStatusEvent {
            node: "test".to_string(),
            status: NodeLifecycleStatus::Starting,
            pid: None,
            error: None,
            timestamp: 1234567890,
        };

        let json1 = serde_json::to_string(&event1).unwrap();
        assert!(!json1.contains("\"pid\""));
        assert!(!json1.contains("\"error\""));

        let event2 = NodeStatusEvent {
            node: "test".to_string(),
            status: NodeLifecycleStatus::Running,
            pid: Some(12345),
            error: Some("test error".to_string()),
            timestamp: 1234567890,
        };

        let json2 = serde_json::to_string(&event2).unwrap();
        assert!(json2.contains("\"pid\""));
        assert!(json2.contains("\"error\""));
    }
}