llm-toolkit 0.63.1

A low-level, unopinionated Rust toolkit for the LLM last mile problem.
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
//! Execution state management for parallel orchestration.
//!
//! This module provides a state machine to track the lifecycle of each step
//! during concurrent execution.

use crate::orchestrator::OrchestratorError;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// A serializable representation of a step failure with error type preserved.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StepFailure {
    /// The error kind (timeout, cancelled, or other)
    pub kind: SerializableErrorKind,
    /// The full error message
    pub message: String,
}

/// A serializable representation of an error type for step failures.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum SerializableErrorKind {
    /// A step execution timed out.
    StepTimeout { step_id: String, timeout_secs: u64 },
    /// A step execution was cancelled.
    Cancelled { step_id: String },
    /// Any other error type (stored as string).
    Other,
}

impl StepFailure {
    /// Creates a StepFailure from an OrchestratorError, preserving the error type.
    pub fn from_orchestrator_error(error: &OrchestratorError) -> Self {
        let kind = match error {
            OrchestratorError::StepTimeout { step_id, timeout } => {
                SerializableErrorKind::StepTimeout {
                    step_id: step_id.clone(),
                    timeout_secs: timeout.as_secs(),
                }
            }
            OrchestratorError::Cancelled { step_id } => SerializableErrorKind::Cancelled {
                step_id: step_id.clone(),
            },
            _ => SerializableErrorKind::Other,
        };

        StepFailure {
            kind,
            message: error.to_string(),
        }
    }

    /// Returns true if this is a timeout error.
    pub fn is_timeout(&self) -> bool {
        matches!(self.kind, SerializableErrorKind::StepTimeout { .. })
    }

    /// Returns true if this is a cancellation error.
    pub fn is_cancelled(&self) -> bool {
        matches!(self.kind, SerializableErrorKind::Cancelled { .. })
    }
}

impl PartialEq for StepFailure {
    fn eq(&self, other: &Self) -> bool {
        self.kind == other.kind && self.message == other.message
    }
}

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

/// The execution state of a step in the parallel orchestrator.
///
/// Steps progress through states as follows:
/// - `Pending` -> `Ready` (when all dependencies complete)
/// - `Ready` -> `Running` (when execution begins)
/// - `Running` -> `Completed` (on success)
/// - `Running` -> `Failed` (on error)
/// - `Running` -> `PausedForApproval` (when human approval is required)
/// - `PausedForApproval` -> `Running` (when approval is granted)
/// - Any state -> `Skipped` (when a dependency fails)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum StepState {
    /// Step is waiting for dependencies to complete
    Pending,
    /// Step is ready to execute (all dependencies satisfied)
    Ready,
    /// Step is currently executing
    Running,
    /// Step completed successfully
    Completed,
    /// Step failed with an error
    Failed(StepFailure),
    /// Step was skipped due to a failed dependency
    Skipped,
    /// Step is paused and waiting for human approval
    PausedForApproval {
        /// Message describing what approval is needed
        message: String,
        /// Payload data for the approval request
        payload: serde_json::Value,
    },
}

impl PartialEq for StepState {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (StepState::Pending, StepState::Pending) => true,
            (StepState::Ready, StepState::Ready) => true,
            (StepState::Running, StepState::Running) => true,
            (StepState::Completed, StepState::Completed) => true,
            (StepState::Skipped, StepState::Skipped) => true,
            (StepState::Failed(e1), StepState::Failed(e2)) => e1 == e2,
            (
                StepState::PausedForApproval {
                    message: m1,
                    payload: p1,
                },
                StepState::PausedForApproval {
                    message: m2,
                    payload: p2,
                },
            ) => m1 == m2 && p1 == p2,
            _ => false,
        }
    }
}

/// Manages the execution state of all steps in a parallel workflow.
///
/// This provides thread-safe access to step states and helper methods
/// for querying workflow progress.
///
/// # Examples
///
/// ```ignore
/// use llm_toolkit::orchestrator::parallel::{ExecutionStateManager, StepState};
///
/// let mut manager = ExecutionStateManager::new();
/// manager.set_state("step_1", StepState::Ready);
/// manager.set_state("step_2", StepState::Pending);
///
/// let ready_steps = manager.get_ready_steps();
/// assert_eq!(ready_steps.len(), 1);
/// assert!(ready_steps.contains(&"step_1".to_string()));
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionStateManager {
    states: HashMap<String, StepState>,
}

impl ExecutionStateManager {
    /// Creates a new execution state manager with no steps.
    pub fn new() -> Self {
        Self {
            states: HashMap::new(),
        }
    }

    /// Sets the state of a step.
    ///
    /// If the step doesn't exist yet, it will be added.
    pub fn set_state(&mut self, step_id: &str, state: StepState) {
        self.states.insert(step_id.to_string(), state);
    }

    /// Gets the current state of a step.
    ///
    /// Returns `None` if the step doesn't exist.
    pub fn get_state(&self, step_id: &str) -> Option<&StepState> {
        self.states.get(step_id)
    }

    /// Returns the total number of steps being tracked.
    pub fn step_count(&self) -> usize {
        self.states.len()
    }

    /// Returns all step IDs that are in the Ready state.
    ///
    /// These steps can be executed immediately as all their dependencies
    /// have been satisfied.
    pub fn get_ready_steps(&self) -> Vec<String> {
        self.states
            .iter()
            .filter(|(_, state)| matches!(state, StepState::Ready))
            .map(|(id, _)| id.clone())
            .collect()
    }

    /// Returns all step IDs that are currently running.
    pub fn get_running_steps(&self) -> Vec<String> {
        self.states
            .iter()
            .filter(|(_, state)| matches!(state, StepState::Running))
            .map(|(id, _)| id.clone())
            .collect()
    }

    /// Returns all step IDs that were skipped due to failed dependencies.
    pub fn get_skipped_steps(&self) -> Vec<String> {
        self.states
            .iter()
            .filter(|(_, state)| matches!(state, StepState::Skipped))
            .map(|(id, _)| id.clone())
            .collect()
    }

    /// Returns true if all steps are in the Completed state.
    pub fn all_completed(&self) -> bool {
        !self.states.is_empty()
            && self
                .states
                .values()
                .all(|s| matches!(s, StepState::Completed))
    }

    /// Returns true if all steps are either Completed or Skipped.
    ///
    /// This indicates the workflow has finished, even if some steps
    /// were skipped due to failures.
    pub fn all_completed_or_skipped(&self) -> bool {
        !self.states.is_empty()
            && self
                .states
                .values()
                .all(|s| matches!(s, StepState::Completed | StepState::Skipped))
    }

    /// Returns true if any step is in the Failed state.
    pub fn has_failures(&self) -> bool {
        self.states
            .values()
            .any(|s| matches!(s, StepState::Failed(_)))
    }

    /// Returns true if any step is in the Ready or Running state.
    ///
    /// This indicates the workflow is still actively executing.
    pub fn has_ready_or_running_steps(&self) -> bool {
        self.states
            .values()
            .any(|s| matches!(s, StepState::Ready | StepState::Running))
    }

    /// Returns true if any step is still pending (not Ready, Running, Completed, Failed, or Skipped).
    pub fn has_pending_steps(&self) -> bool {
        self.states
            .values()
            .any(|s| matches!(s, StepState::Pending))
    }

    /// Returns all steps in Failed state with their errors.
    pub fn get_failed_steps(&self) -> Vec<(String, StepFailure)> {
        self.states
            .iter()
            .filter_map(|(id, state)| {
                if let StepState::Failed(err) = state {
                    Some((id.clone(), err.clone()))
                } else {
                    None
                }
            })
            .collect()
    }

    /// Returns the first failed step's ID and error, if any.
    ///
    /// This is useful for triggering redesign logic based on the initial failure.
    /// Returns `None` if there are no failed steps.
    pub fn get_first_failure(&self) -> Option<(String, StepFailure)> {
        self.states.iter().find_map(|(id, state)| {
            if let StepState::Failed(err) = state {
                Some((id.clone(), err.clone()))
            } else {
                None
            }
        })
    }
}

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

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

    #[test]
    fn test_new_manager_is_empty() {
        let manager = ExecutionStateManager::new();
        assert_eq!(manager.step_count(), 0);
    }

    #[test]
    fn test_set_and_get_state() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Pending);

        assert_eq!(manager.get_state("step_1").unwrap(), &StepState::Pending);
    }

    #[test]
    fn test_get_state_nonexistent() {
        let manager = ExecutionStateManager::new();
        assert!(manager.get_state("nonexistent").is_none());
    }

    #[test]
    fn test_update_state() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Pending);
        manager.set_state("step_1", StepState::Ready);

        assert_eq!(manager.get_state("step_1").unwrap(), &StepState::Ready);
    }

    #[test]
    fn test_get_ready_steps() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Ready);
        manager.set_state("step_2", StepState::Pending);
        manager.set_state("step_3", StepState::Ready);

        let ready = manager.get_ready_steps();
        assert_eq!(ready.len(), 2);
        assert!(ready.contains(&"step_1".to_string()));
        assert!(ready.contains(&"step_3".to_string()));
    }

    #[test]
    fn test_get_ready_steps_empty() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Pending);
        manager.set_state("step_2", StepState::Running);

        let ready = manager.get_ready_steps();
        assert_eq!(ready.len(), 0);
    }

    #[test]
    fn test_get_running_steps() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Running);
        manager.set_state("step_2", StepState::Pending);
        manager.set_state("step_3", StepState::Running);

        let running = manager.get_running_steps();
        assert_eq!(running.len(), 2);
        assert!(running.contains(&"step_1".to_string()));
        assert!(running.contains(&"step_3".to_string()));
    }

    #[test]
    fn test_all_completed() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Completed);
        manager.set_state("step_2", StepState::Completed);

        assert!(manager.all_completed());
    }

    #[test]
    fn test_all_completed_false_with_pending() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Completed);
        manager.set_state("step_2", StepState::Pending);

        assert!(!manager.all_completed());
    }

    #[test]
    fn test_all_completed_false_when_empty() {
        let manager = ExecutionStateManager::new();
        assert!(!manager.all_completed());
    }

    #[test]
    fn test_all_completed_or_skipped() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Completed);
        manager.set_state("step_2", StepState::Skipped);

        assert!(manager.all_completed_or_skipped());
    }

    #[test]
    fn test_all_completed_or_skipped_false_with_pending() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Completed);
        manager.set_state("step_2", StepState::Skipped);
        manager.set_state("step_3", StepState::Pending);

        assert!(!manager.all_completed_or_skipped());
    }

    #[test]
    fn test_has_failures() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Completed);
        manager.set_state(
            "step_2",
            StepState::Failed(StepFailure::from_orchestrator_error(
                &OrchestratorError::ExecutionFailed("test error".to_string()),
            )),
        );

        assert!(manager.has_failures());
    }

    #[test]
    fn test_has_failures_false() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Completed);
        manager.set_state("step_2", StepState::Pending);

        assert!(!manager.has_failures());
    }

    #[test]
    fn test_has_ready_or_running_steps() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Running);
        manager.set_state("step_2", StepState::Completed);

        assert!(manager.has_ready_or_running_steps());
    }

    #[test]
    fn test_has_ready_or_running_steps_with_ready() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Ready);
        manager.set_state("step_2", StepState::Completed);

        assert!(manager.has_ready_or_running_steps());
    }

    #[test]
    fn test_has_ready_or_running_steps_false() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Completed);
        manager.set_state("step_2", StepState::Skipped);

        assert!(!manager.has_ready_or_running_steps());
    }

    #[test]
    fn test_has_pending_steps() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Pending);
        manager.set_state("step_2", StepState::Completed);

        assert!(manager.has_pending_steps());
    }

    #[test]
    fn test_has_pending_steps_false() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Completed);
        manager.set_state("step_2", StepState::Ready);

        assert!(!manager.has_pending_steps());
    }

    #[test]
    fn test_get_failed_steps() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Completed);
        manager.set_state(
            "step_2",
            StepState::Failed(StepFailure::from_orchestrator_error(
                &OrchestratorError::ExecutionFailed("test error".to_string()),
            )),
        );

        let failed = manager.get_failed_steps();
        assert_eq!(failed.len(), 1);
        assert_eq!(failed[0].0, "step_2");
        assert_eq!(failed[0].1.message, "Execution failed: test error");
    }

    #[test]
    fn test_get_failed_steps_empty() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Completed);
        manager.set_state("step_2", StepState::Running);

        let failed = manager.get_failed_steps();
        assert_eq!(failed.len(), 0);
    }

    #[test]
    fn test_step_state_equality() {
        assert_eq!(StepState::Pending, StepState::Pending);
        assert_eq!(StepState::Ready, StepState::Ready);
        assert_ne!(StepState::Pending, StepState::Ready);
    }

    #[test]
    fn test_step_state_failed_equality() {
        let error1 = StepFailure::from_orchestrator_error(&OrchestratorError::ExecutionFailed(
            "error1".to_string(),
        ));
        let error2 = StepFailure::from_orchestrator_error(&OrchestratorError::ExecutionFailed(
            "error2".to_string(),
        ));
        let error1_clone = error1.clone();

        assert_eq!(
            StepState::Failed(error1.clone()),
            StepState::Failed(error1_clone)
        );
        assert_ne!(StepState::Failed(error1), StepState::Failed(error2));
    }

    #[test]
    fn test_get_first_failure() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Completed);
        manager.set_state(
            "step_2",
            StepState::Failed(StepFailure::from_orchestrator_error(
                &OrchestratorError::ExecutionFailed("error_2".to_string()),
            )),
        );
        manager.set_state(
            "step_3",
            StepState::Failed(StepFailure::from_orchestrator_error(
                &OrchestratorError::ExecutionFailed("error_3".to_string()),
            )),
        );

        let first_failure = manager.get_first_failure();
        assert!(first_failure.is_some());
        let (step_id, error) = first_failure.unwrap();
        // Note: HashMap iteration order is not guaranteed, so we just check that we got one of the failures
        assert!(step_id == "step_2" || step_id == "step_3");
        assert!(error.message.contains("error_2") || error.message.contains("error_3"));
    }

    #[test]
    fn test_get_first_failure_none() {
        let mut manager = ExecutionStateManager::new();
        manager.set_state("step_1", StepState::Completed);
        manager.set_state("step_2", StepState::Running);

        let first_failure = manager.get_first_failure();
        assert!(first_failure.is_none());
    }
}