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
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
//! Input validation for execution plans, steps, and configurations

use crate::error::{ExecutionError, ExecutionResult};
use crate::models::{ExecutionPlan, ExecutionStep, StepAction};

/// Validator for execution plans and their components
pub struct ExecutionValidator;

impl ExecutionValidator {
    /// Validate an entire execution plan
    ///
    /// # Arguments
    /// * `plan` - The execution plan to validate
    ///
    /// # Returns
    /// * `Ok(())` if the plan is valid
    /// * `Err(ExecutionError)` if validation fails
    pub fn validate_plan(plan: &ExecutionPlan) -> ExecutionResult<()> {
        // Validate plan name
        Self::validate_plan_name(&plan.name)?;

        // Validate plan has at least one step
        if plan.steps.is_empty() {
            return Err(ExecutionError::ValidationError(
                "Execution plan must contain at least one step".to_string(),
            ));
        }

        // Validate each step
        for step in &plan.steps {
            Self::validate_step(step)?;
        }

        // Validate step dependencies
        Self::validate_dependencies(plan)?;

        Ok(())
    }

    /// Validate a single execution step
    ///
    /// # Arguments
    /// * `step` - The execution step to validate
    ///
    /// # Returns
    /// * `Ok(())` if the step is valid
    /// * `Err(ExecutionError)` if validation fails
    pub fn validate_step(step: &ExecutionStep) -> ExecutionResult<()> {
        // Validate step description
        if step.description.is_empty() {
            return Err(ExecutionError::ValidationError(
                "Step description cannot be empty".to_string(),
            ));
        }

        if step.description.len() > 1000 {
            return Err(ExecutionError::ValidationError(
                "Step description cannot exceed 1000 characters".to_string(),
            ));
        }

        // Validate step action
        Self::validate_step_action(&step.action)?;

        Ok(())
    }

    /// Validate a step action
    ///
    /// # Arguments
    /// * `action` - The step action to validate
    ///
    /// # Returns
    /// * `Ok(())` if the action is valid
    /// * `Err(ExecutionError)` if validation fails
    pub fn validate_step_action(action: &StepAction) -> ExecutionResult<()> {
        match action {
            StepAction::CreateFile { path, content } => {
                Self::validate_file_path(path)?;
                Self::validate_file_content(content)?;
            }
            StepAction::ModifyFile { path, diff } => {
                Self::validate_file_path(path)?;
                Self::validate_diff(diff)?;
            }
            StepAction::DeleteFile { path } => {
                Self::validate_file_path(path)?;
            }
            StepAction::RunCommand { command, args } => {
                Self::validate_command(command)?;
                Self::validate_command_args(args)?;
            }
            StepAction::RunTests { pattern } => {
                if let Some(p) = pattern {
                    Self::validate_test_pattern(p)?;
                }
            }
        }

        Ok(())
    }

    /// Validate a file path
    ///
    /// # Arguments
    /// * `path` - The file path to validate
    ///
    /// # Returns
    /// * `Ok(())` if the path is valid
    /// * `Err(ExecutionError)` if validation fails
    pub fn validate_file_path(path: &str) -> ExecutionResult<()> {
        // Path cannot be empty
        if path.is_empty() {
            return Err(ExecutionError::ValidationError(
                "File path cannot be empty".to_string(),
            ));
        }

        // Path cannot exceed reasonable length
        if path.len() > 4096 {
            return Err(ExecutionError::ValidationError(
                "File path cannot exceed 4096 characters".to_string(),
            ));
        }

        // Path cannot contain null bytes
        if path.contains('\0') {
            return Err(ExecutionError::ValidationError(
                "File path cannot contain null bytes".to_string(),
            ));
        }

        // Path should not start with absolute path indicators (security check)
        // Allow relative paths and paths resolved by PathResolver
        if path.starts_with('/') && !path.starts_with("./") && !path.starts_with("../") {
            // Absolute paths are allowed but should be validated by PathResolver
            // This is a warning-level check
        }

        Ok(())
    }

    /// Validate file content
    ///
    /// # Arguments
    /// * `content` - The file content to validate
    ///
    /// # Returns
    /// * `Ok(())` if the content is valid
    /// * `Err(ExecutionError)` if validation fails
    pub fn validate_file_content(content: &str) -> ExecutionResult<()> {
        // Content can be empty (empty files are valid)
        // Content cannot exceed reasonable size (100MB)
        if content.len() > 100 * 1024 * 1024 {
            return Err(ExecutionError::ValidationError(
                "File content cannot exceed 100MB".to_string(),
            ));
        }

        Ok(())
    }

    /// Validate a diff
    ///
    /// # Arguments
    /// * `diff` - The diff to validate
    ///
    /// # Returns
    /// * `Ok(())` if the diff is valid
    /// * `Err(ExecutionError)` if validation fails
    pub fn validate_diff(diff: &str) -> ExecutionResult<()> {
        // Diff cannot be empty
        if diff.is_empty() {
            return Err(ExecutionError::ValidationError(
                "Diff cannot be empty".to_string(),
            ));
        }

        // Diff cannot exceed reasonable size (10MB)
        if diff.len() > 10 * 1024 * 1024 {
            return Err(ExecutionError::ValidationError(
                "Diff cannot exceed 10MB".to_string(),
            ));
        }

        Ok(())
    }

    /// Validate a shell command
    ///
    /// # Arguments
    /// * `command` - The command to validate
    ///
    /// # Returns
    /// * `Ok(())` if the command is valid
    /// * `Err(ExecutionError)` if validation fails
    pub fn validate_command(command: &str) -> ExecutionResult<()> {
        // Command cannot be empty
        if command.is_empty() {
            return Err(ExecutionError::ValidationError(
                "Command cannot be empty".to_string(),
            ));
        }

        // Command cannot exceed reasonable length
        if command.len() > 4096 {
            return Err(ExecutionError::ValidationError(
                "Command cannot exceed 4096 characters".to_string(),
            ));
        }

        // Command cannot contain null bytes
        if command.contains('\0') {
            return Err(ExecutionError::ValidationError(
                "Command cannot contain null bytes".to_string(),
            ));
        }

        Ok(())
    }

    /// Validate command arguments
    ///
    /// # Arguments
    /// * `args` - The command arguments to validate
    ///
    /// # Returns
    /// * `Ok(())` if the arguments are valid
    /// * `Err(ExecutionError)` if validation fails
    pub fn validate_command_args(args: &[String]) -> ExecutionResult<()> {
        // Arguments list cannot exceed reasonable size
        if args.len() > 1000 {
            return Err(ExecutionError::ValidationError(
                "Command arguments cannot exceed 1000 items".to_string(),
            ));
        }

        // Each argument must be valid
        for arg in args {
            if arg.len() > 4096 {
                return Err(ExecutionError::ValidationError(
                    "Command argument cannot exceed 4096 characters".to_string(),
                ));
            }

            if arg.contains('\0') {
                return Err(ExecutionError::ValidationError(
                    "Command argument cannot contain null bytes".to_string(),
                ));
            }
        }

        Ok(())
    }

    /// Validate a test pattern
    ///
    /// # Arguments
    /// * `pattern` - The test pattern to validate
    ///
    /// # Returns
    /// * `Ok(())` if the pattern is valid
    /// * `Err(ExecutionError)` if validation fails
    pub fn validate_test_pattern(pattern: &str) -> ExecutionResult<()> {
        // Pattern cannot be empty
        if pattern.is_empty() {
            return Err(ExecutionError::ValidationError(
                "Test pattern cannot be empty".to_string(),
            ));
        }

        // Pattern cannot exceed reasonable length
        if pattern.len() > 1024 {
            return Err(ExecutionError::ValidationError(
                "Test pattern cannot exceed 1024 characters".to_string(),
            ));
        }

        // Pattern cannot contain null bytes
        if pattern.contains('\0') {
            return Err(ExecutionError::ValidationError(
                "Test pattern cannot contain null bytes".to_string(),
            ));
        }

        Ok(())
    }

    /// Validate plan name
    ///
    /// # Arguments
    /// * `name` - The plan name to validate
    ///
    /// # Returns
    /// * `Ok(())` if the name is valid
    /// * `Err(ExecutionError)` if validation fails
    pub fn validate_plan_name(name: &str) -> ExecutionResult<()> {
        // Name cannot be empty
        if name.is_empty() {
            return Err(ExecutionError::ValidationError(
                "Plan name cannot be empty".to_string(),
            ));
        }

        // Name cannot exceed reasonable length
        if name.len() > 256 {
            return Err(ExecutionError::ValidationError(
                "Plan name cannot exceed 256 characters".to_string(),
            ));
        }

        // Name cannot contain null bytes
        if name.contains('\0') {
            return Err(ExecutionError::ValidationError(
                "Plan name cannot contain null bytes".to_string(),
            ));
        }

        Ok(())
    }

    /// Validate step dependencies
    ///
    /// # Arguments
    /// * `plan` - The execution plan to validate dependencies for
    ///
    /// # Returns
    /// * `Ok(())` if dependencies are valid
    /// * `Err(ExecutionError)` if validation fails
    fn validate_dependencies(plan: &ExecutionPlan) -> ExecutionResult<()> {
        // Create a set of valid step IDs
        let valid_ids: std::collections::HashSet<_> =
            plan.steps.iter().map(|s| s.id.as_str()).collect();

        // Check each step's dependencies
        for step in &plan.steps {
            for dep_id in &step.dependencies {
                // Dependency must reference an existing step
                if !valid_ids.contains(dep_id.as_str()) {
                    return Err(ExecutionError::ValidationError(format!(
                        "Step {} references non-existent dependency: {}",
                        step.id, dep_id
                    )));
                }

                // Dependency cannot be self-referential
                if dep_id == &step.id {
                    return Err(ExecutionError::ValidationError(format!(
                        "Step {} has self-referential dependency",
                        step.id
                    )));
                }
            }
        }

        // Check for circular dependencies
        Self::check_circular_dependencies(plan)?;

        Ok(())
    }

    /// Check for circular dependencies in the plan
    ///
    /// # Arguments
    /// * `plan` - The execution plan to check
    ///
    /// # Returns
    /// * `Ok(())` if no circular dependencies exist
    /// * `Err(ExecutionError)` if circular dependencies are found
    fn check_circular_dependencies(plan: &ExecutionPlan) -> ExecutionResult<()> {
        // Build a map of step ID to dependencies
        let mut dep_map: std::collections::HashMap<&str, Vec<&str>> =
            std::collections::HashMap::new();

        for step in &plan.steps {
            dep_map.insert(
                &step.id,
                step.dependencies.iter().map(|s| s.as_str()).collect(),
            );
        }

        // Check each step for cycles
        for step in &plan.steps {
            let mut visited = std::collections::HashSet::new();
            let mut rec_stack = std::collections::HashSet::new();

            if Self::has_cycle(&step.id, &dep_map, &mut visited, &mut rec_stack) {
                return Err(ExecutionError::ValidationError(format!(
                    "Circular dependency detected involving step: {}",
                    step.id
                )));
            }
        }

        Ok(())
    }

    /// Helper function to detect cycles using DFS
    fn has_cycle(
        node: &str,
        dep_map: &std::collections::HashMap<&str, Vec<&str>>,
        visited: &mut std::collections::HashSet<String>,
        rec_stack: &mut std::collections::HashSet<String>,
    ) -> bool {
        let node_str = node.to_string();

        visited.insert(node_str.clone());
        rec_stack.insert(node_str.clone());

        if let Some(deps) = dep_map.get(node) {
            for dep in deps {
                let dep_str = dep.to_string();

                if !visited.contains(&dep_str) {
                    if Self::has_cycle(dep, dep_map, visited, rec_stack) {
                        return true;
                    }
                } else if rec_stack.contains(&dep_str) {
                    return true;
                }
            }
        }

        rec_stack.remove(&node_str);
        false
    }
}

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

    #[test]
    fn test_validate_plan_name_empty() {
        let result = ExecutionValidator::validate_plan_name("");
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("empty"));
    }

    #[test]
    fn test_validate_plan_name_valid() {
        let result = ExecutionValidator::validate_plan_name("My Execution Plan");
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_plan_name_too_long() {
        let long_name = "a".repeat(257);
        let result = ExecutionValidator::validate_plan_name(&long_name);
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_file_path_empty() {
        let result = ExecutionValidator::validate_file_path("");
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_file_path_valid() {
        let result = ExecutionValidator::validate_file_path("src/main.rs");
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_file_path_with_null_byte() {
        let result = ExecutionValidator::validate_file_path("src/main\0.rs");
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_file_content_empty() {
        let result = ExecutionValidator::validate_file_content("");
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_file_content_valid() {
        let result = ExecutionValidator::validate_file_content("fn main() {}");
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_command_empty() {
        let result = ExecutionValidator::validate_command("");
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_command_valid() {
        let result = ExecutionValidator::validate_command("cargo build");
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_command_args_valid() {
        let args = vec!["--release".to_string(), "--verbose".to_string()];
        let result = ExecutionValidator::validate_command_args(&args);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_test_pattern_empty() {
        let result = ExecutionValidator::validate_test_pattern("");
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_test_pattern_valid() {
        let result = ExecutionValidator::validate_test_pattern("test_*");
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_step_action_create_file() {
        let action = StepAction::CreateFile {
            path: "src/lib.rs".to_string(),
            content: "pub fn hello() {}".to_string(),
        };
        let result = ExecutionValidator::validate_step_action(&action);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_step_action_create_file_empty_path() {
        let action = StepAction::CreateFile {
            path: "".to_string(),
            content: "pub fn hello() {}".to_string(),
        };
        let result = ExecutionValidator::validate_step_action(&action);
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_step_action_modify_file() {
        let action = StepAction::ModifyFile {
            path: "src/lib.rs".to_string(),
            diff: "--- a/src/lib.rs\n+++ b/src/lib.rs\n@@ -1 +1 @@\n-old\n+new".to_string(),
        };
        let result = ExecutionValidator::validate_step_action(&action);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_step_action_delete_file() {
        let action = StepAction::DeleteFile {
            path: "src/old.rs".to_string(),
        };
        let result = ExecutionValidator::validate_step_action(&action);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_step_action_run_command() {
        let action = StepAction::RunCommand {
            command: "cargo".to_string(),
            args: vec!["test".to_string()],
        };
        let result = ExecutionValidator::validate_step_action(&action);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_step_action_run_tests() {
        let action = StepAction::RunTests {
            pattern: Some("test_*".to_string()),
        };
        let result = ExecutionValidator::validate_step_action(&action);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_step_valid() {
        let step = ExecutionStep::new(
            "Create a new file".to_string(),
            StepAction::CreateFile {
                path: "src/lib.rs".to_string(),
                content: "pub fn hello() {}".to_string(),
            },
        );
        let result = ExecutionValidator::validate_step(&step);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_step_empty_description() {
        let mut step = ExecutionStep::new(
            "".to_string(),
            StepAction::CreateFile {
                path: "src/lib.rs".to_string(),
                content: "pub fn hello() {}".to_string(),
            },
        );
        step.description = "".to_string();
        let result = ExecutionValidator::validate_step(&step);
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_plan_empty_steps() {
        let plan = ExecutionPlan::new("Test Plan".to_string(), vec![]);
        let result = ExecutionValidator::validate_plan(&plan);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("at least one step"));
    }

    #[test]
    fn test_validate_plan_valid() {
        let step = ExecutionStep::new(
            "Create a new file".to_string(),
            StepAction::CreateFile {
                path: "src/lib.rs".to_string(),
                content: "pub fn hello() {}".to_string(),
            },
        );
        let plan = ExecutionPlan::new("Test Plan".to_string(), vec![step]);
        let result = ExecutionValidator::validate_plan(&plan);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_plan_invalid_dependency() {
        let mut step1 = ExecutionStep::new(
            "Create a new file".to_string(),
            StepAction::CreateFile {
                path: "src/lib.rs".to_string(),
                content: "pub fn hello() {}".to_string(),
            },
        );

        let step2 = ExecutionStep::new(
            "Modify the file".to_string(),
            StepAction::ModifyFile {
                path: "src/lib.rs".to_string(),
                diff: "--- a/src/lib.rs\n+++ b/src/lib.rs\n@@ -1 +1 @@\n-old\n+new".to_string(),
            },
        );

        // Add invalid dependency
        step1.dependencies.push("non-existent-id".to_string());

        let plan = ExecutionPlan::new("Test Plan".to_string(), vec![step1, step2]);
        let result = ExecutionValidator::validate_plan(&plan);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("non-existent dependency"));
    }

    #[test]
    fn test_validate_plan_self_referential_dependency() {
        let mut step = ExecutionStep::new(
            "Create a new file".to_string(),
            StepAction::CreateFile {
                path: "src/lib.rs".to_string(),
                content: "pub fn hello() {}".to_string(),
            },
        );

        // Add self-referential dependency
        step.dependencies.push(step.id.clone());

        let plan = ExecutionPlan::new("Test Plan".to_string(), vec![step]);
        let result = ExecutionValidator::validate_plan(&plan);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("self-referential"));
    }

    #[test]
    fn test_validate_plan_circular_dependency() {
        let mut step1 = ExecutionStep::new(
            "Step 1".to_string(),
            StepAction::CreateFile {
                path: "src/lib.rs".to_string(),
                content: "pub fn hello() {}".to_string(),
            },
        );

        let mut step2 = ExecutionStep::new(
            "Step 2".to_string(),
            StepAction::CreateFile {
                path: "src/main.rs".to_string(),
                content: "fn main() {}".to_string(),
            },
        );

        let step1_id = step1.id.clone();
        let step2_id = step2.id.clone();

        // Create circular dependency: step1 -> step2 -> step1
        step1.dependencies.push(step2_id.clone());
        step2.dependencies.push(step1_id);

        let plan = ExecutionPlan::new("Test Plan".to_string(), vec![step1, step2]);
        let result = ExecutionValidator::validate_plan(&plan);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Circular dependency"));
    }
}