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
//! Core types for the RLM orchestration system.
//!
//! This module defines shared types used across the crate including session identifiers,
//! command types, and state tracking structures.

use jiff::{Timestamp, ToSpan};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use ulid::Ulid;

/// Unique identifier for an RLM session.
///
/// A session represents a single conversation context with VM affinity,
/// accumulated state, and budget tracking.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SessionId(pub Ulid);

impl SessionId {
    /// Create a new random session ID.
    pub fn new() -> Self {
        Self(Ulid::new())
    }

    /// Create a session ID from a ULID.
    pub fn from_ulid(ulid: Ulid) -> Self {
        Self(ulid)
    }

    /// Get the inner ULID.
    pub fn as_ulid(&self) -> &Ulid {
        &self.0
    }

    /// Parse from string.
    pub fn from_string(s: &str) -> Result<Self, ulid::DecodeError> {
        Ok(Self(Ulid::from_string(s)?))
    }
}

impl Default for SessionId {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Display for SessionId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Current state of an RLM session.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum SessionState {
    /// Session is being initialized.
    #[default]
    Initializing,
    /// Session is ready for commands.
    Ready,
    /// Session is executing a command.
    Executing,
    /// Session is paused (e.g., waiting for user input).
    Paused,
    /// Session has been terminated.
    Terminated,
    /// Session has expired due to timeout.
    Expired,
}

/// Information about an active session.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionInfo {
    /// Unique session identifier.
    pub id: SessionId,
    /// Current session state.
    pub state: SessionState,
    /// When the session was created.
    pub created_at: Timestamp,
    /// When the session expires.
    pub expires_at: Timestamp,
    /// Number of extensions applied.
    pub extension_count: u32,
    /// Current recursion depth.
    pub recursion_depth: u32,
    /// Budget status.
    pub budget_status: BudgetStatus,
    /// Associated VM instance ID (if assigned).
    pub vm_instance_id: Option<String>,
    /// Context variables stored in the session.
    pub context_variables: HashMap<String, String>,
    /// Current active snapshot ID (for rollback support).
    /// This tracks the last successfully restored snapshot.
    pub current_snapshot_id: Option<String>,
    /// Number of snapshots created for this session.
    pub snapshot_count: u32,
}

impl SessionInfo {
    /// Create a new session info with default values.
    pub fn new(id: SessionId, duration_secs: u64) -> Self {
        let now = Timestamp::now();
        Self {
            id,
            state: SessionState::Initializing,
            created_at: now,
            expires_at: now
                .checked_add((duration_secs as i64).seconds())
                .expect("adding seconds to timestamp should not fail"),
            extension_count: 0,
            recursion_depth: 0,
            budget_status: BudgetStatus::default(),
            vm_instance_id: None,
            context_variables: HashMap::new(),
            current_snapshot_id: None,
            snapshot_count: 0,
        }
    }

    /// Check if the session has expired.
    pub fn is_expired(&self) -> bool {
        Timestamp::now() > self.expires_at
    }

    /// Check if the session can be extended.
    pub fn can_extend(&self, max_extensions: u32) -> bool {
        self.extension_count < max_extensions
    }
}

/// Budget tracking status for a session.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BudgetStatus {
    /// Total tokens allowed.
    pub token_budget: u64,
    /// Tokens consumed so far.
    pub tokens_used: u64,
    /// Time budget in milliseconds.
    pub time_budget_ms: u64,
    /// Time consumed in milliseconds.
    pub time_used_ms: u64,
    /// Maximum recursion depth allowed.
    pub max_recursion_depth: u32,
    /// Current recursion depth.
    pub current_recursion_depth: u32,
}

impl Default for BudgetStatus {
    fn default() -> Self {
        Self {
            token_budget: crate::DEFAULT_TOKEN_BUDGET,
            tokens_used: 0,
            time_budget_ms: crate::DEFAULT_TIME_BUDGET_MS,
            time_used_ms: 0,
            max_recursion_depth: crate::DEFAULT_MAX_RECURSION_DEPTH,
            current_recursion_depth: 0,
        }
    }
}

impl BudgetStatus {
    /// Check if token budget is exhausted.
    pub fn tokens_exhausted(&self) -> bool {
        self.tokens_used >= self.token_budget
    }

    /// Check if time budget is exhausted.
    pub fn time_exhausted(&self) -> bool {
        self.time_used_ms >= self.time_budget_ms
    }

    /// Check if recursion depth limit is reached.
    pub fn depth_exhausted(&self) -> bool {
        self.current_recursion_depth > self.max_recursion_depth
    }

    /// Check if any budget is exhausted.
    pub fn is_exhausted(&self) -> bool {
        self.tokens_exhausted() || self.time_exhausted() || self.depth_exhausted()
    }

    /// Get remaining token budget.
    pub fn tokens_remaining(&self) -> u64 {
        self.token_budget.saturating_sub(self.tokens_used)
    }

    /// Get remaining time budget in milliseconds.
    pub fn time_remaining_ms(&self) -> u64 {
        self.time_budget_ms.saturating_sub(self.time_used_ms)
    }
}

/// Parsed command from LLM output.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Command {
    /// Execute a bash command in the VM.
    Run(BashCommand),
    /// Execute Python code in the VM.
    Code(PythonCode),
    /// Return a final result (terminates the query loop).
    Final(String),
    /// Return a variable's value as final result.
    FinalVar(String),
    /// Invoke the LLM recursively.
    QueryLlm(LlmQuery),
    /// Invoke the LLM with batched queries.
    QueryLlmBatched(Vec<LlmQuery>),
    /// Create a named snapshot.
    Snapshot(String),
    /// Restore to a previous snapshot.
    Rollback(String),
}

/// A bash command to execute.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BashCommand {
    /// The command string to execute.
    pub command: String,
    /// Optional timeout override in milliseconds.
    pub timeout_ms: Option<u64>,
    /// Working directory (relative to session root).
    pub working_dir: Option<String>,
}

impl BashCommand {
    /// Create a new bash command.
    pub fn new(command: impl Into<String>) -> Self {
        Self {
            command: command.into(),
            timeout_ms: None,
            working_dir: None,
        }
    }
}

/// Python code to execute.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PythonCode {
    /// The Python code to execute.
    pub code: String,
    /// Optional timeout override in milliseconds.
    pub timeout_ms: Option<u64>,
}

impl PythonCode {
    /// Create new Python code.
    pub fn new(code: impl Into<String>) -> Self {
        Self {
            code: code.into(),
            timeout_ms: None,
        }
    }
}

/// A query to send to the LLM.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LlmQuery {
    /// The prompt/query text.
    pub prompt: String,
    /// Optional model override.
    pub model: Option<String>,
    /// Optional temperature override.
    pub temperature: Option<f32>,
    /// Optional max tokens override.
    pub max_tokens: Option<u32>,
}

impl LlmQuery {
    /// Create a new LLM query.
    pub fn new(prompt: impl Into<String>) -> Self {
        Self {
            prompt: prompt.into(),
            model: None,
            temperature: None,
            max_tokens: None,
        }
    }
}

/// Metadata about a query/execution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryMetadata {
    /// Unique query identifier.
    pub query_id: Ulid,
    /// Parent query ID (for recursive queries).
    pub parent_query_id: Option<Ulid>,
    /// Session this query belongs to.
    pub session_id: SessionId,
    /// Iteration number within the query loop.
    pub iteration: u32,
    /// Recursion depth.
    pub depth: u32,
    /// Timestamp when query started.
    pub started_at: Timestamp,
    /// Timestamp when query completed (if done).
    pub completed_at: Option<Timestamp>,
}

impl QueryMetadata {
    /// Create new query metadata.
    pub fn new(session_id: SessionId) -> Self {
        Self {
            query_id: Ulid::new(),
            parent_query_id: None,
            session_id,
            iteration: 0,
            depth: 0,
            started_at: Timestamp::now(),
            completed_at: None,
        }
    }

    /// Create child query metadata (for recursive calls).
    pub fn child(&self) -> Self {
        Self {
            query_id: Ulid::new(),
            parent_query_id: Some(self.query_id),
            session_id: self.session_id,
            iteration: 0,
            depth: self.depth + 1,
            started_at: Timestamp::now(),
            completed_at: None,
        }
    }

    /// Mark the query as completed.
    pub fn complete(&mut self) {
        self.completed_at = Some(Timestamp::now());
    }
}

/// History of commands executed in a session.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CommandHistory {
    /// List of executed commands with their results.
    pub entries: Vec<CommandHistoryEntry>,
}

impl CommandHistory {
    /// Create a new empty command history.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add an entry to the history.
    pub fn push(&mut self, entry: CommandHistoryEntry) {
        self.entries.push(entry);
    }

    /// Get the last successful command index.
    pub fn last_successful_index(&self) -> Option<usize> {
        self.entries.iter().rposition(|e| e.success)
    }

    /// Get commands since the last checkpoint.
    pub fn since_checkpoint(&self, checkpoint_index: usize) -> &[CommandHistoryEntry] {
        &self.entries[checkpoint_index..]
    }
}

/// A single entry in the command history.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommandHistoryEntry {
    /// The command that was executed.
    pub command: Command,
    /// Whether the command succeeded.
    pub success: bool,
    /// stdout output (may be truncated).
    pub stdout: String,
    /// stderr output (may be truncated).
    pub stderr: String,
    /// Exit code (for bash commands).
    pub exit_code: Option<i32>,
    /// Execution time in milliseconds.
    pub execution_time_ms: u64,
    /// Timestamp of execution.
    pub executed_at: Timestamp,
}

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

    #[test]
    fn test_session_id_creation() {
        let id1 = SessionId::new();
        let id2 = SessionId::new();
        assert_ne!(id1, id2);
    }

    #[test]
    fn test_budget_status_exhaustion() {
        let mut budget = BudgetStatus::default();
        assert!(!budget.is_exhausted());

        budget.tokens_used = budget.token_budget;
        assert!(budget.tokens_exhausted());
        assert!(budget.is_exhausted());
    }

    #[test]
    fn test_session_info_expiry() {
        let id = SessionId::new();
        let info = SessionInfo::new(id, 1); // 1 second duration
        assert!(!info.is_expired());

        // Session with 0 duration should be expired immediately
        let expired_info = SessionInfo::new(id, 0);
        // Note: This might still pass due to timing, but demonstrates the concept
        assert!(expired_info.expires_at <= Timestamp::now() || !expired_info.is_expired());
    }

    #[test]
    fn test_query_metadata_child() {
        let session_id = SessionId::new();
        let parent = QueryMetadata::new(session_id);
        let child = parent.child();

        assert_eq!(child.parent_query_id, Some(parent.query_id));
        assert_eq!(child.depth, parent.depth + 1);
        assert_eq!(child.session_id, parent.session_id);
    }

    #[test]
    fn test_command_history() {
        let mut history = CommandHistory::new();
        assert!(history.last_successful_index().is_none());

        history.push(CommandHistoryEntry {
            command: Command::Code(PythonCode::new("x = 1")),
            success: true,
            stdout: "".to_string(),
            stderr: "".to_string(),
            exit_code: Some(0),
            execution_time_ms: 100,
            executed_at: Timestamp::now(),
        });

        assert_eq!(history.last_successful_index(), Some(0));
    }
}