rlmesh-runtime 0.1.0-beta.1

Runtime driver for RLMesh model-environment evaluation sessions
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use async_trait::async_trait;
use rlmesh_proto::common::v1::MessageBytes;

use super::{
    ActionReceivedEvent, EnvConnectedEvent, EpisodeCompletedEvent, EpisodeStartedEvent, HookError,
    LogEvent, ModelConnectedEvent, ObservationEmittedEvent, RuntimeHooks, SessionEndedEvent,
    SessionFailedEvent, SessionStartedEvent, StepCompletedEvent, TelemetrySummaryEvent,
    TelemetryWindowEvent,
};

/// Ordered runtime hook composition.
///
/// Lifecycle, progress, telemetry, and log events are sent to every hook.
/// Transform hooks are applied in order, with each hook receiving the payload
/// returned by the previous hook.
#[derive(Default)]
pub struct RuntimeHookChain {
    hooks: Vec<std::sync::Arc<dyn RuntimeHooks>>,
}

impl RuntimeHookChain {
    /// Creates a chain from hooks in invocation order.
    pub fn new(hooks: Vec<std::sync::Arc<dyn RuntimeHooks>>) -> Self {
        Self { hooks }
    }

    /// Creates an empty chain with the same behavior as `NoopRuntimeHooks`.
    pub fn empty() -> Self {
        Self::default()
    }

    /// Returns the number of hooks in the chain.
    pub fn len(&self) -> usize {
        self.hooks.len()
    }

    /// Returns true when the chain contains no hooks.
    pub fn is_empty(&self) -> bool {
        self.hooks.is_empty()
    }
}

#[async_trait]
impl RuntimeHooks for RuntimeHookChain {
    async fn env_connected(&self, event: EnvConnectedEvent) -> Result<(), HookError> {
        let mut first_error = None;
        for hook in &self.hooks {
            if let Err(error) = hook.env_connected(event.clone()).await {
                first_error.get_or_insert(error);
            }
        }
        first_error.map_or(Ok(()), Err)
    }

    async fn model_connected(&self, event: ModelConnectedEvent) -> Result<(), HookError> {
        let mut first_error = None;
        for hook in &self.hooks {
            if let Err(error) = hook.model_connected(event.clone()).await {
                first_error.get_or_insert(error);
            }
        }
        first_error.map_or(Ok(()), Err)
    }

    async fn session_started(&self, event: SessionStartedEvent) -> Result<(), HookError> {
        let mut first_error = None;
        for hook in &self.hooks {
            if let Err(error) = hook.session_started(event.clone()).await {
                first_error.get_or_insert(error);
            }
        }
        first_error.map_or(Ok(()), Err)
    }

    async fn episode_started(&self, event: EpisodeStartedEvent) -> Result<(), HookError> {
        let mut first_error = None;
        for hook in &self.hooks {
            if let Err(error) = hook.episode_started(event.clone()).await {
                first_error.get_or_insert(error);
            }
        }
        first_error.map_or(Ok(()), Err)
    }

    async fn episode_completed(&self, event: EpisodeCompletedEvent) -> Result<(), HookError> {
        let mut first_error = None;
        for hook in &self.hooks {
            if let Err(error) = hook.episode_completed(event.clone()).await {
                first_error.get_or_insert(error);
            }
        }
        first_error.map_or(Ok(()), Err)
    }

    async fn action_received(&self, event: ActionReceivedEvent) -> Result<(), HookError> {
        let mut first_error = None;
        for hook in &self.hooks {
            if let Err(error) = hook.action_received(event.clone()).await {
                first_error.get_or_insert(error);
            }
        }
        first_error.map_or(Ok(()), Err)
    }

    async fn transform_action(
        &self,
        event: ActionReceivedEvent,
    ) -> Result<Option<MessageBytes>, HookError> {
        let ActionReceivedEvent {
            session_id,
            route,
            episode_id,
            episode_record_id,
            episode_ids,
            episode_record_ids,
            step,
            env_index,
            action_space,
            mut action,
        } = event;
        for hook in &self.hooks {
            action = hook
                .transform_action(ActionReceivedEvent {
                    session_id: session_id.clone(),
                    route: route.clone(),
                    episode_id: episode_id.clone(),
                    episode_record_id: episode_record_id.clone(),
                    episode_ids: episode_ids.clone(),
                    episode_record_ids: episode_record_ids.clone(),
                    step,
                    env_index,
                    action_space: action_space.clone(),
                    action,
                })
                .await?;
        }
        Ok(action)
    }

    async fn step_completed(&self, event: StepCompletedEvent) -> Result<(), HookError> {
        let mut first_error = None;
        for hook in &self.hooks {
            if let Err(error) = hook.step_completed(event.clone()).await {
                first_error.get_or_insert(error);
            }
        }
        first_error.map_or(Ok(()), Err)
    }

    async fn observation_emitted(&self, event: ObservationEmittedEvent) -> Result<(), HookError> {
        let mut first_error = None;
        for hook in &self.hooks {
            if let Err(error) = hook.observation_emitted(event.clone()).await {
                first_error.get_or_insert(error);
            }
        }
        first_error.map_or(Ok(()), Err)
    }

    async fn transform_observation(
        &self,
        event: ObservationEmittedEvent,
    ) -> Result<Option<MessageBytes>, HookError> {
        let ObservationEmittedEvent {
            session_id,
            route,
            episode_id,
            episode_record_id,
            episode_ids,
            episode_record_ids,
            step,
            env_index,
            is_reset,
            num_envs,
            observation_space,
            mut observation,
        } = event;
        for hook in &self.hooks {
            observation = hook
                .transform_observation(ObservationEmittedEvent {
                    session_id: session_id.clone(),
                    route: route.clone(),
                    episode_id: episode_id.clone(),
                    episode_record_id: episode_record_id.clone(),
                    episode_ids: episode_ids.clone(),
                    episode_record_ids: episode_record_ids.clone(),
                    step,
                    env_index,
                    is_reset,
                    num_envs,
                    observation_space: observation_space.clone(),
                    observation,
                })
                .await?;
        }
        Ok(observation)
    }

    async fn telemetry_window(&self, event: TelemetryWindowEvent) -> Result<(), HookError> {
        let mut first_error = None;
        for hook in &self.hooks {
            if let Err(error) = hook.telemetry_window(event.clone()).await {
                first_error.get_or_insert(error);
            }
        }
        first_error.map_or(Ok(()), Err)
    }

    async fn telemetry_summary(&self, event: TelemetrySummaryEvent) -> Result<(), HookError> {
        let mut first_error = None;
        for hook in &self.hooks {
            if let Err(error) = hook.telemetry_summary(event.clone()).await {
                first_error.get_or_insert(error);
            }
        }
        first_error.map_or(Ok(()), Err)
    }

    async fn session_ended(&self, event: SessionEndedEvent) -> Result<(), HookError> {
        let mut first_error = None;
        for hook in &self.hooks {
            if let Err(error) = hook.session_ended(event.clone()).await {
                first_error.get_or_insert(error);
            }
        }
        first_error.map_or(Ok(()), Err)
    }

    async fn session_failed(&self, event: SessionFailedEvent) -> Result<(), HookError> {
        let mut first_error = None;
        for hook in &self.hooks {
            if let Err(error) = hook.session_failed(event.clone()).await {
                first_error.get_or_insert(error);
            }
        }
        first_error.map_or(Ok(()), Err)
    }

    async fn log(&self, event: LogEvent) -> Result<(), HookError> {
        let mut first_error = None;
        for hook in &self.hooks {
            if let Err(error) = hook.log(event.clone()).await {
                first_error.get_or_insert(error);
            }
        }
        first_error.map_or(Ok(()), Err)
    }
}

#[cfg(test)]
mod tests {
    use std::sync::{Arc, Mutex};

    use async_trait::async_trait;
    use rlmesh_proto::common::v1::MessageBytes;
    use rlmesh_proto::spaces::v1::SpaceSpec;

    use super::*;
    use crate::hooks::{LogLevel, RuntimeRouteContext};

    struct RecordingHook {
        name: &'static str,
        calls: Arc<Mutex<Vec<String>>>,
        log_error: Option<&'static str>,
        action_suffix: Option<u8>,
        transform_error: Option<&'static str>,
    }

    impl RecordingHook {
        fn new(name: &'static str, calls: Arc<Mutex<Vec<String>>>) -> Self {
            Self {
                name,
                calls,
                log_error: None,
                action_suffix: None,
                transform_error: None,
            }
        }

        fn with_log_error(mut self, error: &'static str) -> Self {
            self.log_error = Some(error);
            self
        }

        fn with_action_suffix(mut self, suffix: u8) -> Self {
            self.action_suffix = Some(suffix);
            self
        }

        fn with_transform_error(mut self, error: &'static str) -> Self {
            self.transform_error = Some(error);
            self
        }

        fn record(&self, call: impl Into<String>) {
            self.calls
                .lock()
                .expect("calls mutex poisoned")
                .push(call.into());
        }
    }

    #[async_trait]
    impl RuntimeHooks for RecordingHook {
        async fn log(&self, event: LogEvent) -> Result<(), HookError> {
            self.record(format!("{}:log:{}", self.name, event.message));
            if let Some(error) = self.log_error {
                return Err(HookError::Message(error.to_string()));
            }
            Ok(())
        }

        async fn transform_action(
            &self,
            event: ActionReceivedEvent,
        ) -> Result<Option<MessageBytes>, HookError> {
            let data = event
                .action
                .as_ref()
                .map(|action| action.data.clone())
                .unwrap_or_default();
            self.record(format!("{}:action:{data:?}", self.name));
            if let Some(error) = self.transform_error {
                return Err(HookError::Message(error.to_string()));
            }
            Ok(event.action.map(|mut action| {
                if let Some(suffix) = self.action_suffix {
                    action.data.push(suffix);
                }
                action
            }))
        }
    }

    fn hook(hook: RecordingHook) -> Arc<dyn RuntimeHooks> {
        Arc::new(hook)
    }

    fn recorded(calls: &Arc<Mutex<Vec<String>>>) -> Vec<String> {
        calls.lock().expect("calls mutex poisoned").clone()
    }

    fn log_event() -> LogEvent {
        LogEvent {
            session_id: "session".to_string(),
            route: RuntimeRouteContext::default(),
            level: LogLevel::Info,
            message: "hello".to_string(),
            source: None,
        }
    }

    fn action_event(data: Vec<u8>) -> ActionReceivedEvent {
        ActionReceivedEvent {
            session_id: "session".to_string(),
            route: RuntimeRouteContext::default(),
            episode_id: "episode".to_string(),
            episode_record_id: "episode-artifact".to_string(),
            episode_ids: vec!["episode".to_string()],
            episode_record_ids: vec!["episode-artifact".to_string()],
            step: 1,
            env_index: 0,
            action_space: SpaceSpec::default(),
            action: Some(MessageBytes { data }),
        }
    }

    #[tokio::test]
    async fn event_hooks_call_every_hook_and_return_first_error() {
        let calls = Arc::new(Mutex::new(Vec::new()));
        let chain = RuntimeHookChain::new(vec![
            hook(RecordingHook::new("first", calls.clone()).with_log_error("first failed")),
            hook(RecordingHook::new("second", calls.clone()).with_log_error("second failed")),
            hook(RecordingHook::new("third", calls.clone())),
        ]);

        let error = chain.log(log_event()).await.unwrap_err();

        assert_eq!(error.to_string(), "first failed");
        assert_eq!(
            recorded(&calls),
            vec!["first:log:hello", "second:log:hello", "third:log:hello"]
        );
    }

    #[tokio::test]
    async fn transform_hooks_run_in_order() {
        let calls = Arc::new(Mutex::new(Vec::new()));
        let chain = RuntimeHookChain::new(vec![
            hook(RecordingHook::new("first", calls.clone()).with_action_suffix(1)),
            hook(RecordingHook::new("second", calls.clone()).with_action_suffix(2)),
        ]);

        let action = chain
            .transform_action(action_event(vec![0]))
            .await
            .unwrap()
            .unwrap();

        assert_eq!(action.data, vec![0, 1, 2]);
        assert_eq!(
            recorded(&calls),
            vec!["first:action:[0]", "second:action:[0, 1]"]
        );
    }

    #[tokio::test]
    async fn transform_hooks_stop_on_first_error() {
        let calls = Arc::new(Mutex::new(Vec::new()));
        let chain = RuntimeHookChain::new(vec![
            hook(RecordingHook::new("first", calls.clone()).with_action_suffix(1)),
            hook(RecordingHook::new("second", calls.clone()).with_transform_error("bad action")),
            hook(RecordingHook::new("third", calls.clone()).with_action_suffix(3)),
        ]);

        let error = chain
            .transform_action(action_event(vec![0]))
            .await
            .unwrap_err();

        assert_eq!(error.to_string(), "bad action");
        assert_eq!(
            recorded(&calls),
            vec!["first:action:[0]", "second:action:[0, 1]"]
        );
    }

    #[tokio::test]
    async fn empty_chain_is_a_noop() {
        let chain = RuntimeHookChain::empty();

        chain.log(log_event()).await.unwrap();
        let action = chain
            .transform_action(action_event(vec![7]))
            .await
            .unwrap()
            .unwrap();

        assert!(chain.is_empty());
        assert_eq!(chain.len(), 0);
        assert_eq!(action.data, vec![7]);
    }
}