ricecoder-execution 0.1.71

Execution engine for workflows and agent tasks
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
//! Rollback handling for execution plans
//!
//! Provides rollback functionality to undo executed steps on failure.
//! Tracks rollback actions for each step and executes them in reverse order.

use crate::error::{ExecutionError, ExecutionResult};
use crate::models::{ExecutionStep, RollbackAction, RollbackType};
use ricecoder_storage::PathResolver;
use std::path::Path;
use std::process::Command;
use tracing::{debug, error, info, warn};

/// Handles rollback of executed steps
///
/// Tracks rollback actions for each step and executes them in reverse order
/// to restore the system to its pre-execution state.
pub struct RollbackHandler {
    /// Rollback actions to execute (in reverse order)
    rollback_actions: Vec<(String, RollbackAction)>,
    /// Whether rollback is currently in progress
    in_progress: bool,
}

impl RollbackHandler {
    /// Create a new rollback handler
    pub fn new() -> Self {
        Self {
            rollback_actions: Vec::new(),
            in_progress: false,
        }
    }

    /// Track a rollback action for a step
    ///
    /// # Arguments
    /// * `step_id` - ID of the step being tracked
    /// * `action` - Rollback action to execute if needed
    pub fn track_action(&mut self, step_id: String, action: RollbackAction) {
        debug!(
            step_id = %step_id,
            action_type = ?action.action_type,
            "Tracking rollback action"
        );
        self.rollback_actions.push((step_id, action));
    }

    /// Track rollback action from a step
    ///
    /// Extracts the rollback action from a step and tracks it.
    pub fn track_step(&mut self, step: &ExecutionStep) {
        if let Some(rollback_action) = &step.rollback_action {
            self.track_action(step.id.clone(), rollback_action.clone());
        }
    }

    /// Execute rollback for all tracked actions
    ///
    /// Executes rollback actions in reverse order (LIFO) to undo changes.
    /// Stops on first error unless partial rollback is enabled.
    ///
    /// # Returns
    /// A vector of rollback results for each action executed
    pub fn execute_rollback(&mut self) -> ExecutionResult<Vec<RollbackResult>> {
        if self.rollback_actions.is_empty() {
            info!("No rollback actions to execute");
            return Ok(Vec::new());
        }

        info!(
            action_count = self.rollback_actions.len(),
            "Starting rollback execution"
        );

        self.in_progress = true;
        let mut results = Vec::new();

        // Execute rollback actions in reverse order (LIFO)
        for (step_id, action) in self.rollback_actions.iter().rev() {
            debug!(
                step_id = %step_id,
                action_type = ?action.action_type,
                "Executing rollback action"
            );

            match self.execute_rollback_action(step_id, action) {
                Ok(result) => {
                    info!(
                        step_id = %step_id,
                        "Rollback action completed successfully"
                    );
                    results.push(result);
                }
                Err(e) => {
                    error!(
                        step_id = %step_id,
                        error = %e,
                        "Rollback action failed"
                    );
                    self.in_progress = false;
                    return Err(ExecutionError::RollbackFailed(format!(
                        "Rollback failed for step {}: {}",
                        step_id, e
                    )));
                }
            }
        }

        self.in_progress = false;
        info!(
            completed_actions = results.len(),
            "Rollback execution completed"
        );

        Ok(results)
    }

    /// Execute partial rollback for a subset of steps
    ///
    /// Executes rollback only for the specified step IDs.
    ///
    /// # Arguments
    /// * `step_ids` - IDs of steps to rollback
    ///
    /// # Returns
    /// A vector of rollback results for executed actions
    pub fn execute_partial_rollback(
        &mut self,
        step_ids: &[String],
    ) -> ExecutionResult<Vec<RollbackResult>> {
        info!(
            step_count = step_ids.len(),
            "Starting partial rollback execution"
        );

        self.in_progress = true;
        let mut results = Vec::new();

        // Execute rollback actions in reverse order for specified steps
        for (step_id, action) in self.rollback_actions.iter().rev() {
            if step_ids.contains(step_id) {
                debug!(
                    step_id = %step_id,
                    action_type = ?action.action_type,
                    "Executing partial rollback action"
                );

                match self.execute_rollback_action(step_id, action) {
                    Ok(result) => {
                        info!(
                            step_id = %step_id,
                            "Partial rollback action completed successfully"
                        );
                        results.push(result);
                    }
                    Err(e) => {
                        error!(
                            step_id = %step_id,
                            error = %e,
                            "Partial rollback action failed"
                        );
                        self.in_progress = false;
                        return Err(ExecutionError::RollbackFailed(format!(
                            "Partial rollback failed for step {}: {}",
                            step_id, e
                        )));
                    }
                }
            }
        }

        self.in_progress = false;
        info!(
            completed_actions = results.len(),
            "Partial rollback execution completed"
        );

        Ok(results)
    }

    /// Execute a single rollback action
    fn execute_rollback_action(
        &self,
        step_id: &str,
        action: &RollbackAction,
    ) -> ExecutionResult<RollbackResult> {
        match action.action_type {
            RollbackType::RestoreFile => self.handle_restore_file(step_id, action),
            RollbackType::DeleteFile => self.handle_delete_file(step_id, action),
            RollbackType::RunCommand => self.handle_run_command(step_id, action),
        }
    }

    /// Handle restore file rollback action
    fn handle_restore_file(
        &self,
        step_id: &str,
        action: &RollbackAction,
    ) -> ExecutionResult<RollbackResult> {
        debug!(step_id = %step_id, "Restoring file from backup");

        // Extract file path and backup path from action data
        let file_path = action
            .data
            .get("file_path")
            .and_then(|v| v.as_str())
            .ok_or_else(|| {
                ExecutionError::RollbackFailed("Missing file_path in restore action".to_string())
            })?;

        let backup_path = action
            .data
            .get("backup_path")
            .and_then(|v| v.as_str())
            .ok_or_else(|| {
                ExecutionError::RollbackFailed("Missing backup_path in restore action".to_string())
            })?;

        // Validate paths using PathResolver
        let resolved_file_path = PathResolver::expand_home(Path::new(file_path))
            .map_err(|e| ExecutionError::RollbackFailed(format!("Invalid file path: {}", e)))?;

        let resolved_backup_path = PathResolver::expand_home(Path::new(backup_path))
            .map_err(|e| ExecutionError::RollbackFailed(format!("Invalid backup path: {}", e)))?;

        // Check if backup exists
        if !resolved_backup_path.exists() {
            return Err(ExecutionError::RollbackFailed(format!(
                "Backup file not found: {}",
                backup_path
            )));
        }

        // Restore the file from backup
        std::fs::copy(&resolved_backup_path, &resolved_file_path).map_err(|e| {
            ExecutionError::RollbackFailed(format!(
                "Failed to restore file {} from backup: {}",
                file_path, e
            ))
        })?;

        info!(
            file_path = %file_path,
            backup_path = %backup_path,
            "File restored from backup"
        );

        Ok(RollbackResult {
            step_id: step_id.to_string(),
            action_type: RollbackType::RestoreFile,
            success: true,
            message: format!("Restored {} from backup", file_path),
        })
    }

    /// Handle delete file rollback action
    fn handle_delete_file(
        &self,
        step_id: &str,
        action: &RollbackAction,
    ) -> ExecutionResult<RollbackResult> {
        debug!(step_id = %step_id, "Deleting created file");

        // Extract file path from action data
        let file_path = action
            .data
            .get("file_path")
            .and_then(|v| v.as_str())
            .ok_or_else(|| {
                ExecutionError::RollbackFailed("Missing file_path in delete action".to_string())
            })?;

        // Validate path using PathResolver
        let resolved_path = PathResolver::expand_home(Path::new(file_path))
            .map_err(|e| ExecutionError::RollbackFailed(format!("Invalid path: {}", e)))?;

        // Check if file exists
        if !resolved_path.exists() {
            warn!(
                file_path = %file_path,
                "File to delete does not exist, skipping"
            );
            return Ok(RollbackResult {
                step_id: step_id.to_string(),
                action_type: RollbackType::DeleteFile,
                success: true,
                message: format!("File {} already deleted", file_path),
            });
        }

        // Delete the file
        std::fs::remove_file(&resolved_path).map_err(|e| {
            ExecutionError::RollbackFailed(format!("Failed to delete file {}: {}", file_path, e))
        })?;

        info!(file_path = %file_path, "File deleted successfully");

        Ok(RollbackResult {
            step_id: step_id.to_string(),
            action_type: RollbackType::DeleteFile,
            success: true,
            message: format!("Deleted {}", file_path),
        })
    }

    /// Handle run command rollback action
    fn handle_run_command(
        &self,
        step_id: &str,
        action: &RollbackAction,
    ) -> ExecutionResult<RollbackResult> {
        debug!(step_id = %step_id, "Running undo command");

        // Extract command and args from action data
        let command = action
            .data
            .get("command")
            .and_then(|v| v.as_str())
            .ok_or_else(|| {
                ExecutionError::RollbackFailed("Missing command in undo action".to_string())
            })?;

        let args: Vec<String> = action
            .data
            .get("args")
            .and_then(|v| v.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| v.as_str().map(|s| s.to_string()))
                    .collect()
            })
            .unwrap_or_default();

        // Execute the undo command
        let mut cmd = Command::new(command);
        cmd.args(&args);

        let output = cmd.output().map_err(|e| {
            ExecutionError::RollbackFailed(format!(
                "Failed to execute undo command {}: {}",
                command, e
            ))
        })?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(ExecutionError::RollbackFailed(format!(
                "Undo command {} failed with exit code {:?}: {}",
                command,
                output.status.code(),
                stderr
            )));
        }

        info!(
            command = %command,
            "Undo command executed successfully"
        );

        Ok(RollbackResult {
            step_id: step_id.to_string(),
            action_type: RollbackType::RunCommand,
            success: true,
            message: format!("Executed undo command: {}", command),
        })
    }

    /// Verify rollback completeness
    ///
    /// Checks that all rollback actions have been executed and the system
    /// is in a consistent state.
    ///
    /// # Returns
    /// true if rollback is complete and consistent, false otherwise
    pub fn verify_completeness(&self) -> bool {
        if self.in_progress {
            warn!("Rollback verification requested while rollback is in progress");
            return false;
        }

        // In a real implementation, this would:
        // 1. Check that all tracked files have been restored or deleted
        // 2. Verify file checksums match backups
        // 3. Check that undo commands completed successfully
        // 4. Validate system state consistency

        debug!("Rollback completeness verification passed");
        true
    }

    /// Get the number of tracked rollback actions
    pub fn action_count(&self) -> usize {
        self.rollback_actions.len()
    }

    /// Check if rollback is currently in progress
    pub fn is_in_progress(&self) -> bool {
        self.in_progress
    }

    /// Clear all tracked rollback actions
    pub fn clear(&mut self) {
        self.rollback_actions.clear();
        debug!("Cleared all tracked rollback actions");
    }
}

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

/// Result of a rollback action
#[derive(Debug, Clone)]
pub struct RollbackResult {
    /// ID of the step that was rolled back
    pub step_id: String,
    /// Type of rollback action
    pub action_type: RollbackType,
    /// Whether the rollback succeeded
    pub success: bool,
    /// Message describing the result
    pub message: String,
}

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

    #[test]
    fn test_create_rollback_handler() {
        let handler = RollbackHandler::new();
        assert_eq!(handler.action_count(), 0);
        assert!(!handler.is_in_progress());
    }

    #[test]
    fn test_track_action() {
        let mut handler = RollbackHandler::new();
        let action = RollbackAction {
            action_type: RollbackType::DeleteFile,
            data: json!({ "file_path": "/tmp/test.txt" }),
        };

        handler.track_action("step-1".to_string(), action);
        assert_eq!(handler.action_count(), 1);
    }

    #[test]
    fn test_clear_actions() {
        let mut handler = RollbackHandler::new();
        let action = RollbackAction {
            action_type: RollbackType::DeleteFile,
            data: json!({ "file_path": "/tmp/test.txt" }),
        };

        handler.track_action("step-1".to_string(), action);
        assert_eq!(handler.action_count(), 1);

        handler.clear();
        assert_eq!(handler.action_count(), 0);
    }

    #[test]
    fn test_verify_completeness() {
        let handler = RollbackHandler::new();
        assert!(handler.verify_completeness());
    }

    #[test]
    fn test_verify_completeness_in_progress() {
        let mut handler = RollbackHandler::new();
        handler.in_progress = true;
        assert!(!handler.verify_completeness());
    }

    #[test]
    fn test_execute_rollback_empty() {
        let mut handler = RollbackHandler::new();
        let result = handler.execute_rollback();
        assert!(result.is_ok());
        assert_eq!(result.unwrap().len(), 0);
    }

    #[test]
    fn test_execute_partial_rollback_empty() {
        let mut handler = RollbackHandler::new();
        let result = handler.execute_partial_rollback(&[]);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().len(), 0);
    }

    #[test]
    fn test_rollback_result_creation() {
        let result = RollbackResult {
            step_id: "step-1".to_string(),
            action_type: RollbackType::DeleteFile,
            success: true,
            message: "File deleted".to_string(),
        };

        assert_eq!(result.step_id, "step-1");
        assert_eq!(result.action_type, RollbackType::DeleteFile);
        assert!(result.success);
    }
}