terraphim_rlm 1.20.5

Recursive Language Model (RLM) orchestration for Terraphim AI
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
//! Session management for RLM.
//!
//! The SessionManager handles:
//! - Session lifecycle (create, extend, destroy)
//! - VM affinity (mapping sessions to VMs)
//! - Context variables (get/set per-session state)
//! - Snapshot coordination with VMs

use dashmap::DashMap;
use jiff::{Timestamp, ToSpan};
use std::sync::atomic::{AtomicU32, Ordering};

use crate::config::RlmConfig;
use crate::error::{RlmError, RlmResult};
use crate::types::{BudgetStatus, SessionId, SessionInfo, SessionState};

/// Manages RLM sessions with VM affinity.
///
/// The SessionManager owns session state while VmManager owns VM lifecycle.
/// This separation ensures clear state ownership per the design specification.
pub struct SessionManager {
    /// Active sessions indexed by session ID.
    sessions: DashMap<SessionId, SessionInfo>,

    /// VM to session mapping for affinity.
    vm_to_session: DashMap<String, SessionId>,

    /// Session to VM mapping for reverse lookup.
    session_to_vm: DashMap<SessionId, String>,

    /// Configuration.
    config: RlmConfig,

    /// Total sessions created (for metrics).
    total_sessions_created: AtomicU32,

    /// Active session count.
    active_session_count: AtomicU32,
}

impl SessionManager {
    /// Create a new session manager.
    pub fn new(config: RlmConfig) -> Self {
        Self {
            sessions: DashMap::new(),
            vm_to_session: DashMap::new(),
            session_to_vm: DashMap::new(),
            config,
            total_sessions_created: AtomicU32::new(0),
            active_session_count: AtomicU32::new(0),
        }
    }

    /// Create a new session.
    pub fn create_session(&self) -> RlmResult<SessionInfo> {
        let session_id = SessionId::new();
        let mut session_info = SessionInfo::new(session_id, self.config.session_duration_secs);

        // Apply config-based budget limits
        session_info.budget_status.max_recursion_depth = self.config.max_recursion_depth;
        session_info.budget_status.token_budget = self.config.token_budget;
        session_info.budget_status.time_budget_ms = self.config.time_budget_ms;

        self.sessions.insert(session_id, session_info.clone());
        self.total_sessions_created.fetch_add(1, Ordering::Relaxed);
        self.active_session_count.fetch_add(1, Ordering::Relaxed);

        log::info!("Created session: {}", session_id);
        Ok(session_info)
    }

    /// Get a session by ID.
    pub fn get_session(&self, session_id: &SessionId) -> RlmResult<SessionInfo> {
        self.sessions
            .get(session_id)
            .map(|r| r.clone())
            .ok_or(RlmError::SessionNotFound {
                session_id: *session_id,
            })
    }

    /// Update session state.
    pub fn update_session_state(
        &self,
        session_id: &SessionId,
        state: SessionState,
    ) -> RlmResult<()> {
        let mut session = self
            .sessions
            .get_mut(session_id)
            .ok_or(RlmError::SessionNotFound {
                session_id: *session_id,
            })?;

        session.state = state;
        Ok(())
    }

    /// Check if a session is valid (exists and not expired).
    pub fn is_session_valid(&self, session_id: &SessionId) -> bool {
        self.sessions
            .get(session_id)
            .map(|s| !s.is_expired() && s.state != SessionState::Terminated)
            .unwrap_or(false)
    }

    /// Validate a session token (used by LLM bridge).
    pub fn validate_session(&self, session_id: &SessionId) -> RlmResult<SessionInfo> {
        let session = self.get_session(session_id)?;

        if session.is_expired() {
            return Err(RlmError::SessionExpired {
                session_id: *session_id,
            });
        }

        if session.state == SessionState::Terminated {
            return Err(RlmError::InvalidSessionState {
                session_id: *session_id,
                state: "Terminated".to_string(),
                operation: "validate".to_string(),
            });
        }

        Ok(session)
    }

    /// Extend a session's lifetime.
    pub fn extend_session(&self, session_id: &SessionId) -> RlmResult<SessionInfo> {
        let mut session = self
            .sessions
            .get_mut(session_id)
            .ok_or(RlmError::SessionNotFound {
                session_id: *session_id,
            })?;

        if !session.can_extend(self.config.max_extensions) {
            return Err(RlmError::MaxExtensionsReached {
                session_id: *session_id,
                max: self.config.max_extensions,
            });
        }

        if session.is_expired() {
            return Err(RlmError::SessionExpired {
                session_id: *session_id,
            });
        }

        // Extend the expiration time
        session.expires_at = session
            .expires_at
            .checked_add((self.config.extension_increment_secs as i64).seconds())
            .expect("adding seconds to timestamp should not fail");
        session.extension_count += 1;

        log::info!(
            "Extended session {} (extension {}/{})",
            session_id,
            session.extension_count,
            self.config.max_extensions
        );

        Ok(session.clone())
    }

    /// Assign a VM to a session (VM affinity).
    pub fn assign_vm(&self, session_id: &SessionId, vm_instance_id: String) -> RlmResult<()> {
        // Check if VM is already assigned to another session
        if let Some(existing_session) = self.vm_to_session.get(&vm_instance_id) {
            if *existing_session != *session_id {
                log::warn!(
                    "VM {} already assigned to session {}, reassigning to {}",
                    vm_instance_id,
                    *existing_session,
                    session_id
                );
                // Remove old mapping
                self.session_to_vm.remove(&existing_session);
            }
        }

        // Update session with VM ID
        let mut session = self
            .sessions
            .get_mut(session_id)
            .ok_or(RlmError::SessionNotFound {
                session_id: *session_id,
            })?;

        session.vm_instance_id = Some(vm_instance_id.clone());
        session.state = SessionState::Ready;

        // Update mappings
        self.vm_to_session
            .insert(vm_instance_id.clone(), *session_id);
        self.session_to_vm.insert(*session_id, vm_instance_id);

        Ok(())
    }

    /// Get the VM assigned to a session.
    pub fn get_assigned_vm(&self, session_id: &SessionId) -> Option<String> {
        self.session_to_vm.get(session_id).map(|v| v.clone())
    }

    /// Get the session assigned to a VM.
    pub fn get_session_for_vm(&self, vm_instance_id: &str) -> Option<SessionId> {
        self.vm_to_session.get(vm_instance_id).map(|v| *v)
    }

    /// Set a context variable for a session.
    pub fn set_context_variable(
        &self,
        session_id: &SessionId,
        key: String,
        value: String,
    ) -> RlmResult<()> {
        let mut session = self
            .sessions
            .get_mut(session_id)
            .ok_or(RlmError::SessionNotFound {
                session_id: *session_id,
            })?;

        session.context_variables.insert(key, value);
        Ok(())
    }

    /// Get a context variable from a session.
    pub fn get_context_variable(
        &self,
        session_id: &SessionId,
        key: &str,
    ) -> RlmResult<Option<String>> {
        let session = self.get_session(session_id)?;
        Ok(session.context_variables.get(key).cloned())
    }

    /// Get all context variables for a session.
    pub fn get_all_context_variables(
        &self,
        session_id: &SessionId,
    ) -> RlmResult<std::collections::HashMap<String, String>> {
        let session = self.get_session(session_id)?;
        Ok(session.context_variables.clone())
    }

    /// Delete a context variable from a session.
    pub fn delete_context_variable(
        &self,
        session_id: &SessionId,
        key: &str,
    ) -> RlmResult<Option<String>> {
        let mut session = self
            .sessions
            .get_mut(session_id)
            .ok_or(RlmError::SessionNotFound {
                session_id: *session_id,
            })?;

        Ok(session.context_variables.remove(key))
    }

    /// Update budget status for a session.
    pub fn update_budget(&self, session_id: &SessionId, budget: BudgetStatus) -> RlmResult<()> {
        let mut session = self
            .sessions
            .get_mut(session_id)
            .ok_or(RlmError::SessionNotFound {
                session_id: *session_id,
            })?;

        session.budget_status = budget;
        Ok(())
    }

    /// Increment recursion depth for a session.
    pub fn increment_recursion_depth(&self, session_id: &SessionId) -> RlmResult<u32> {
        let mut session = self
            .sessions
            .get_mut(session_id)
            .ok_or(RlmError::SessionNotFound {
                session_id: *session_id,
            })?;

        let new_depth = session.recursion_depth + 1;

        // Check before committing the increment
        if new_depth > session.budget_status.max_recursion_depth {
            return Err(RlmError::RecursionDepthExceeded {
                depth: new_depth,
                max_depth: session.budget_status.max_recursion_depth,
            });
        }

        session.recursion_depth = new_depth;
        session.budget_status.current_recursion_depth = session.recursion_depth;

        Ok(session.recursion_depth)
    }

    /// Decrement recursion depth for a session.
    pub fn decrement_recursion_depth(&self, session_id: &SessionId) -> RlmResult<u32> {
        let mut session = self
            .sessions
            .get_mut(session_id)
            .ok_or(RlmError::SessionNotFound {
                session_id: *session_id,
            })?;

        session.recursion_depth = session.recursion_depth.saturating_sub(1);
        session.budget_status.current_recursion_depth = session.recursion_depth;

        Ok(session.recursion_depth)
    }

    /// Record that a snapshot was created for a session.
    ///
    /// This updates the session's snapshot count and optionally sets the current snapshot.
    pub fn record_snapshot_created(
        &self,
        session_id: &SessionId,
        snapshot_id: String,
        set_as_current: bool,
    ) -> RlmResult<()> {
        let mut session = self
            .sessions
            .get_mut(session_id)
            .ok_or(RlmError::SessionNotFound {
                session_id: *session_id,
            })?;

        session.snapshot_count += 1;
        if set_as_current {
            session.current_snapshot_id = Some(snapshot_id);
        }

        log::debug!(
            "Recorded snapshot for session {} (count: {})",
            session_id,
            session.snapshot_count
        );

        Ok(())
    }

    /// Record that a snapshot was restored for a session.
    ///
    /// This sets the current snapshot ID for rollback tracking.
    pub fn record_snapshot_restored(
        &self,
        session_id: &SessionId,
        snapshot_id: String,
    ) -> RlmResult<()> {
        let mut session = self
            .sessions
            .get_mut(session_id)
            .ok_or(RlmError::SessionNotFound {
                session_id: *session_id,
            })?;

        session.current_snapshot_id = Some(snapshot_id.clone());

        log::debug!(
            "Recorded snapshot restore for session {}: {}",
            session_id,
            snapshot_id
        );

        Ok(())
    }

    /// Get the current snapshot ID for a session.
    pub fn get_current_snapshot(&self, session_id: &SessionId) -> RlmResult<Option<String>> {
        let session = self.get_session(session_id)?;
        Ok(session.current_snapshot_id)
    }

    /// Clear snapshot tracking for a session (used when all snapshots are deleted).
    pub fn clear_snapshot_tracking(&self, session_id: &SessionId) -> RlmResult<()> {
        let mut session = self
            .sessions
            .get_mut(session_id)
            .ok_or(RlmError::SessionNotFound {
                session_id: *session_id,
            })?;

        session.current_snapshot_id = None;
        session.snapshot_count = 0;

        log::debug!("Cleared snapshot tracking for session {}", session_id);

        Ok(())
    }

    /// Destroy a session and release resources.
    pub fn destroy_session(&self, session_id: &SessionId) -> RlmResult<()> {
        // Remove from sessions
        let session = self.sessions.remove(session_id);

        if session.is_none() {
            return Err(RlmError::SessionNotFound {
                session_id: *session_id,
            });
        }

        // Remove VM mappings
        if let Some(vm_id) = self.session_to_vm.remove(session_id) {
            self.vm_to_session.remove(&vm_id.1);
        }

        self.active_session_count.fetch_sub(1, Ordering::Relaxed);

        log::info!("Destroyed session: {}", session_id);
        Ok(())
    }

    /// Clean up expired sessions.
    pub fn cleanup_expired_sessions(&self) -> Vec<SessionId> {
        let now = Timestamp::now();
        let mut expired = Vec::new();

        self.sessions.retain(|session_id, session| {
            if session.expires_at < now {
                expired.push(*session_id);

                // Clean up VM mappings
                if let Some(vm_id) = &session.vm_instance_id {
                    self.vm_to_session.remove(vm_id);
                    self.session_to_vm.remove(session_id);
                }

                self.active_session_count.fetch_sub(1, Ordering::Relaxed);
                false
            } else {
                true
            }
        });

        if !expired.is_empty() {
            log::info!("Cleaned up {} expired sessions", expired.len());
        }

        expired
    }

    /// Get session statistics.
    pub fn get_stats(&self) -> SessionStats {
        SessionStats {
            total_sessions_created: self.total_sessions_created.load(Ordering::Relaxed),
            active_sessions: self.active_session_count.load(Ordering::Relaxed),
            sessions_with_vm: self.session_to_vm.len() as u32,
        }
    }

    /// List all active sessions.
    pub fn list_sessions(&self) -> Vec<SessionInfo> {
        self.sessions.iter().map(|r| r.clone()).collect()
    }
}

/// Session statistics.
#[derive(Debug, Clone)]
pub struct SessionStats {
    pub total_sessions_created: u32,
    pub active_sessions: u32,
    pub sessions_with_vm: u32,
}

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

    fn test_config() -> RlmConfig {
        RlmConfig {
            session_duration_secs: 3600,
            extension_increment_secs: 1800,
            max_extensions: 3,
            ..Default::default()
        }
    }

    #[test]
    fn test_session_create_and_get() {
        let manager = SessionManager::new(test_config());

        let session = manager.create_session().unwrap();
        assert_eq!(session.state, SessionState::Initializing);

        let retrieved = manager.get_session(&session.id).unwrap();
        assert_eq!(retrieved.id, session.id);
    }

    #[test]
    fn test_session_validation() {
        let manager = SessionManager::new(test_config());

        let session = manager.create_session().unwrap();
        assert!(manager.is_session_valid(&session.id));

        let validated = manager.validate_session(&session.id);
        assert!(validated.is_ok());
    }

    #[test]
    fn test_session_extension() {
        let manager = SessionManager::new(test_config());

        let session = manager.create_session().unwrap();
        let original_expiry = session.expires_at;

        let extended = manager.extend_session(&session.id).unwrap();
        assert!(extended.expires_at > original_expiry);
        assert_eq!(extended.extension_count, 1);

        // Test max extensions
        manager.extend_session(&session.id).unwrap();
        manager.extend_session(&session.id).unwrap();

        let result = manager.extend_session(&session.id);
        assert!(matches!(result, Err(RlmError::MaxExtensionsReached { .. })));
    }

    #[test]
    fn test_vm_affinity() {
        let manager = SessionManager::new(test_config());

        let session = manager.create_session().unwrap();
        manager
            .assign_vm(&session.id, "vm-123".to_string())
            .unwrap();

        assert_eq!(
            manager.get_assigned_vm(&session.id),
            Some("vm-123".to_string())
        );
        assert_eq!(manager.get_session_for_vm("vm-123"), Some(session.id));
    }

    #[test]
    fn test_context_variables() {
        let manager = SessionManager::new(test_config());

        let session = manager.create_session().unwrap();
        manager
            .set_context_variable(&session.id, "key1".to_string(), "value1".to_string())
            .unwrap();

        let value = manager.get_context_variable(&session.id, "key1").unwrap();
        assert_eq!(value, Some("value1".to_string()));

        let all_vars = manager.get_all_context_variables(&session.id).unwrap();
        assert_eq!(all_vars.len(), 1);
    }

    #[test]
    fn test_recursion_depth() {
        let manager = SessionManager::new(RlmConfig {
            max_recursion_depth: 3,
            ..test_config()
        });

        let session = manager.create_session().unwrap();

        assert_eq!(manager.increment_recursion_depth(&session.id).unwrap(), 1);
        assert_eq!(manager.increment_recursion_depth(&session.id).unwrap(), 2);
        assert_eq!(manager.increment_recursion_depth(&session.id).unwrap(), 3);

        // Should fail on exceeding max depth
        let result = manager.increment_recursion_depth(&session.id);
        assert!(matches!(
            result,
            Err(RlmError::RecursionDepthExceeded { .. })
        ));

        // Decrement should work
        assert_eq!(manager.decrement_recursion_depth(&session.id).unwrap(), 2);
    }

    #[test]
    fn test_session_destroy() {
        let manager = SessionManager::new(test_config());

        let session = manager.create_session().unwrap();
        manager
            .assign_vm(&session.id, "vm-456".to_string())
            .unwrap();

        let stats_before = manager.get_stats();
        assert_eq!(stats_before.active_sessions, 1);

        manager.destroy_session(&session.id).unwrap();

        let stats_after = manager.get_stats();
        assert_eq!(stats_after.active_sessions, 0);

        // VM mapping should be cleaned up
        assert!(manager.get_session_for_vm("vm-456").is_none());
    }

    #[test]
    fn test_session_stats() {
        let manager = SessionManager::new(test_config());

        manager.create_session().unwrap();
        manager.create_session().unwrap();

        let stats = manager.get_stats();
        assert_eq!(stats.total_sessions_created, 2);
        assert_eq!(stats.active_sessions, 2);
    }

    #[test]
    fn test_snapshot_tracking() {
        let manager = SessionManager::new(test_config());
        let session = manager.create_session().unwrap();

        // Initial state - no snapshots
        assert!(manager.get_current_snapshot(&session.id).unwrap().is_none());
        let s = manager.get_session(&session.id).unwrap();
        assert_eq!(s.snapshot_count, 0);

        // Record a snapshot creation without setting as current
        manager
            .record_snapshot_created(&session.id, "snap1".to_string(), false)
            .unwrap();
        let s = manager.get_session(&session.id).unwrap();
        assert_eq!(s.snapshot_count, 1);
        assert!(s.current_snapshot_id.is_none());

        // Record a snapshot creation and set as current
        manager
            .record_snapshot_created(&session.id, "snap2".to_string(), true)
            .unwrap();
        let s = manager.get_session(&session.id).unwrap();
        assert_eq!(s.snapshot_count, 2);
        assert_eq!(s.current_snapshot_id, Some("snap2".to_string()));

        // Record a snapshot restore
        manager
            .record_snapshot_restored(&session.id, "snap1".to_string())
            .unwrap();
        let current = manager.get_current_snapshot(&session.id).unwrap();
        assert_eq!(current, Some("snap1".to_string()));

        // Clear snapshot tracking
        manager.clear_snapshot_tracking(&session.id).unwrap();
        let s = manager.get_session(&session.id).unwrap();
        assert_eq!(s.snapshot_count, 0);
        assert!(s.current_snapshot_id.is_none());
    }
}