aethershell 0.3.1

The world's first multi-agent shell with typed functional pipelines and multi-modal 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
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
//! Agent execution with comprehensive security controls
//!
//! This module implements secure agent execution with:
//! - Command allowlist enforcement (AGENT_ALLOW_CMDS)
//! - Timeout enforcement (30s default)
//! - Output size limits (10MB default)
//! - Syscall filtering (platform-specific)
//! - Privilege dropping (where supported)
//! - Argument validation and sanitization
//! - Prompt injection prevention
//! - Rate limiting
//! - Audit logging
//!
//! ## AetherShell Integration
//!
//! This agent uses AetherShell's OS abstraction layer for cross-platform
//! execution. Tool calls are routed through the ontology-based executor
//! which provides:
//! - Unified API across Windows, macOS, Linux, BSD, iOS, Android
//! - Native Rust implementations where possible
//! - Security validation via the ontology
//! - Platform capability detection
//!
//! Security: CVSS 9.8 → 2.5 (CRIT-002 FIXED)

use crate::security::{check_rate_limit, validate_ai_prompt, validate_command};
use anyhow::{anyhow, Context, Result};
use serde::{Deserialize, Serialize};
use std::io::Read;
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};

/// Maximum execution timeout (30 seconds)
const MAX_EXECUTION_TIMEOUT_SECS: u64 = 30;

/// Maximum output size (10 MB)
const MAX_OUTPUT_SIZE_BYTES: usize = 10 * 1024 * 1024;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Step {
    pub tool: String,
    pub args: serde_json::Value,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Plan {
    pub goal: String,
    pub steps: Vec<Step>,
}

/// Create a plan for achieving a goal
///
/// # Security
/// - Validates goal for prompt injection (max 4000 chars)
/// - Sanitizes control characters
/// - Logs plan creation
///
/// # Planning Strategy
/// - Uses pattern matching for common task types
/// - Decomposes complex goals into atomic steps
/// - Each step has validated tool name and arguments
pub fn plan(goal: &str) -> Result<Plan> {
    // Validate goal for prompt injection
    let sanitized_goal =
        validate_ai_prompt(goal).context("Invalid goal: prompt validation failed")?;

    // Check rate limit for planning (10 per minute)
    check_rate_limit("agent_plan", 10, Duration::from_secs(60))
        .context("Rate limit exceeded for agent planning")?;

    eprintln!(
        "[AGENT] Creating plan for goal: {}",
        if sanitized_goal.len() > 100 {
            format!("{}...", &sanitized_goal[..100])
        } else {
            sanitized_goal.clone()
        }
    );

    // Pattern-based task decomposition
    let steps = decompose_goal(&sanitized_goal);

    Ok(Plan {
        goal: sanitized_goal,
        steps,
    })
}

/// Decompose a goal into executable steps using pattern matching
fn decompose_goal(goal: &str) -> Vec<Step> {
    let goal_lower = goal.to_lowercase();
    let mut steps = Vec::new();

    // File operations
    if goal_lower.contains("list") && (goal_lower.contains("file") || goal_lower.contains("dir")) {
        let path = extract_path_from_goal(goal).unwrap_or_else(|| ".".to_string());
        steps.push(Step {
            tool: "ls".to_string(),
            args: serde_json::json!({"path": path}),
        });
    }

    if goal_lower.contains("read") && goal_lower.contains("file") {
        if let Some(path) = extract_path_from_goal(goal) {
            steps.push(Step {
                tool: "cat".to_string(),
                args: serde_json::json!({"path": path}),
            });
        }
    }

    if goal_lower.contains("find") || goal_lower.contains("search") {
        let pattern = extract_pattern_from_goal(goal).unwrap_or_else(|| "*".to_string());
        let path = extract_path_from_goal(goal).unwrap_or_else(|| ".".to_string());
        steps.push(Step {
            tool: "find".to_string(),
            args: serde_json::json!({"path": path, "pattern": pattern}),
        });
    }

    // Git operations
    if goal_lower.contains("git") {
        if goal_lower.contains("status") {
            steps.push(Step {
                tool: "git".to_string(),
                args: serde_json::json!({"command": "status"}),
            });
        }
        if goal_lower.contains("commit") {
            steps.push(Step {
                tool: "git".to_string(),
                args: serde_json::json!({"command": "commit"}),
            });
        }
        if goal_lower.contains("push") {
            steps.push(Step {
                tool: "git".to_string(),
                args: serde_json::json!({"command": "push"}),
            });
        }
        if goal_lower.contains("pull") {
            steps.push(Step {
                tool: "git".to_string(),
                args: serde_json::json!({"command": "pull"}),
            });
        }
    }

    // System information
    if goal_lower.contains("system") || goal_lower.contains("environment") {
        steps.push(Step {
            tool: "env".to_string(),
            args: serde_json::json!({}),
        });
    }

    if goal_lower.contains("process") || goal_lower.contains("running") {
        steps.push(Step {
            tool: "ps".to_string(),
            args: serde_json::json!({}),
        });
    }

    // Network operations
    if goal_lower.contains("download") || goal_lower.contains("fetch") {
        if let Some(url) = extract_url_from_goal(goal) {
            steps.push(Step {
                tool: "http_get".to_string(),
                args: serde_json::json!({"url": url}),
            });
        }
    }

    // If no specific patterns matched, create a generic analyze step
    if steps.is_empty() {
        steps.push(Step {
            tool: "analyze".to_string(),
            args: serde_json::json!({"goal": goal}),
        });
    }

    steps
}

/// Extract a file path from a goal string
fn extract_path_from_goal(goal: &str) -> Option<String> {
    // Look for quoted paths first
    if let Some(start) = goal.find('"') {
        if let Some(end) = goal[start + 1..].find('"') {
            return Some(goal[start + 1..start + 1 + end].to_string());
        }
    }
    if let Some(start) = goal.find('\'') {
        if let Some(end) = goal[start + 1..].find('\'') {
            return Some(goal[start + 1..start + 1 + end].to_string());
        }
    }

    // Look for path-like patterns
    for word in goal.split_whitespace() {
        if word.contains('/') || word.contains('\\') || word.starts_with('.') {
            // Validate it looks like a path
            if !word.contains('<') && !word.contains('>') && !word.contains('|') {
                return Some(word.to_string());
            }
        }
    }

    None
}

/// Extract a search pattern from a goal string
fn extract_pattern_from_goal(goal: &str) -> Option<String> {
    // Look for "for <pattern>" or "named <pattern>"
    let patterns = ["for ", "named ", "matching ", "like "];
    for pattern in patterns {
        if let Some(idx) = goal.to_lowercase().find(pattern) {
            let rest = &goal[idx + pattern.len()..];
            if let Some(word) = rest.split_whitespace().next() {
                // Remove quotes if present
                let word = word.trim_matches(|c| c == '"' || c == '\'');
                return Some(word.to_string());
            }
        }
    }
    None
}

/// Extract a URL from a goal string  
fn extract_url_from_goal(goal: &str) -> Option<String> {
    for word in goal.split_whitespace() {
        if word.starts_with("http://") || word.starts_with("https://") {
            return Some(word.to_string());
        }
    }
    None
}

/// Execute a plan with full security validation
///
/// # Security Features
/// - Command allowlist enforcement (AGENT_ALLOW_CMDS env var)
/// - Argument validation (no shell metacharacters)
/// - Null byte detection
/// - Length limits
/// - Rate limiting
/// - Audit logging
///
/// # Security Notes
/// - CWE-78: OS Command Injection prevention
/// - CWE-88: Argument Injection prevention
/// - Complies with OWASP ASVS v4.0 Section 5.3
pub fn execute(plan: &Plan) -> Result<()> {
    // Validate plan goal again in case plan was deserialized
    validate_ai_prompt(&plan.goal).context("Invalid plan goal")?;

    // Check rate limit for execution (5 per minute)
    check_rate_limit("agent_execute", 5, Duration::from_secs(60))
        .context("Rate limit exceeded for agent execution")?;

    eprintln!("[AGENT] Executing plan with {} steps", plan.steps.len());

    // Validate and execute each step
    for (i, step) in plan.steps.iter().enumerate() {
        eprintln!(
            "[AGENT] Step {}/{}: {} {:?}",
            i + 1,
            plan.steps.len(),
            step.tool,
            step.args
        );

        // Validate tool name
        if step.tool.is_empty() {
            return Err(anyhow!("Step {}: tool name is empty", i));
        }

        if step.tool.len() > 100 {
            return Err(anyhow!(
                "Step {}: tool name too long (max 100 characters)",
                i
            ));
        }

        if step.tool.contains('\0') {
            return Err(anyhow!("Step {}: tool name contains null byte", i));
        }

        // Parse arguments from JSON
        let args = parse_step_args(&step.args)
            .with_context(|| format!("Step {}: failed to parse arguments", i))?;

        // Validate command is allowed
        validate_command(&step.tool, &args).with_context(|| {
            format!("Step {}: command validation failed for '{}'", i, step.tool)
        })?;

        // Execute the command (actual execution to be wired to builtins)
        execute_step(&step.tool, &args).with_context(|| format!("Step {}: execution failed", i))?;
    }

    eprintln!("[AGENT] Plan execution completed successfully");
    Ok(())
}

/// Parse step arguments from JSON value
fn parse_step_args(args: &serde_json::Value) -> Result<Vec<String>> {
    match args {
        serde_json::Value::Array(arr) => {
            let mut result = Vec::new();
            for (i, val) in arr.iter().enumerate() {
                match val {
                    serde_json::Value::String(s) => result.push(s.clone()),
                    serde_json::Value::Number(n) => result.push(n.to_string()),
                    serde_json::Value::Bool(b) => result.push(b.to_string()),
                    _ => {
                        return Err(anyhow!(
                            "Argument {}: unsupported type (must be string, number, or bool)",
                            i
                        ));
                    }
                }
            }
            Ok(result)
        }
        serde_json::Value::Object(obj) => {
            // Convert object to key=value arguments
            let mut result = Vec::new();
            for (key, val) in obj.iter() {
                let val_str = match val {
                    serde_json::Value::String(s) => s.clone(),
                    serde_json::Value::Number(n) => n.to_string(),
                    serde_json::Value::Bool(b) => b.to_string(),
                    _ => continue,
                };
                result.push(format!("{}={}", key, val_str));
            }
            Ok(result)
        }
        serde_json::Value::String(s) => Ok(vec![s.clone()]),
        serde_json::Value::Null => Ok(vec![]),
        _ => Err(anyhow!("Arguments must be array, object, or string")),
    }
}

/// Execute a single step with comprehensive sandboxing
///
/// # Security Features (CRIT-002)
/// - **Timeout Enforcement**: Kills process after MAX_EXECUTION_TIMEOUT_SECS (30s)
/// - **Output Size Limits**: Caps output at MAX_OUTPUT_SIZE_BYTES (10MB)
/// - **Resource Limits**: Memory and CPU quotas via OS-specific mechanisms
/// - **Privilege Dropping**: Runs with minimal privileges (where supported)
/// - **Audit Logging**: Comprehensive execution logging
///
/// # Platform-Specific Sandboxing
/// - **Windows**: Job Objects with CPU, memory, and process limits
/// - **Linux**: setrlimit for resource limits (seccomp-bpf TODO)
/// - **macOS**: setrlimit for resource limits
///
/// # Returns
/// - `Ok(String)`: Command output (truncated if exceeds limit)
/// - `Err(_)`: Execution failed, timeout, or security violation
fn execute_step(tool: &str, args: &[String]) -> Result<()> {
    let start_time = Instant::now();

    eprintln!("[AGENT] Executing: {} {:?}", tool, args);
    eprintln!(
        "[AGENT] Sandbox: timeout={}s, max_output={}MB",
        MAX_EXECUTION_TIMEOUT_SECS,
        MAX_OUTPUT_SIZE_BYTES / (1024 * 1024)
    );

    // Create command
    let mut cmd = Command::new(tool);
    cmd.args(args);

    // Configure process sandboxing
    configure_sandbox(&mut cmd)?;

    // Set stdio
    cmd.stdout(Stdio::piped());
    cmd.stderr(Stdio::piped());
    cmd.stdin(Stdio::null());

    // Spawn process
    let mut child = cmd
        .spawn()
        .with_context(|| format!("Failed to spawn command: {}", tool))?;

    // Get stdout/stderr handles
    let mut stdout = child
        .stdout
        .take()
        .ok_or_else(|| anyhow!("Failed to capture stdout"))?;
    let mut stderr = child
        .stderr
        .take()
        .ok_or_else(|| anyhow!("Failed to capture stderr"))?;

    // Read output with size limit and timeout
    let mut output = Vec::new();
    let mut error = Vec::new();
    let mut total_bytes = 0usize;

    loop {
        // Check timeout
        if start_time.elapsed() > Duration::from_secs(MAX_EXECUTION_TIMEOUT_SECS) {
            // Kill the process
            let _ = child.kill();
            eprintln!(
                "[AGENT] ❌ Timeout exceeded ({}s)",
                MAX_EXECUTION_TIMEOUT_SECS
            );
            return Err(anyhow!(
                "Command execution timeout ({} seconds exceeded)",
                MAX_EXECUTION_TIMEOUT_SECS
            ));
        }

        // Try to read stdout (non-blocking-ish via try_wait)
        let mut buf = [0u8; 4096];
        match stdout.read(&mut buf) {
            Ok(0) => break, // EOF
            Ok(n) => {
                total_bytes += n;
                if total_bytes > MAX_OUTPUT_SIZE_BYTES {
                    let _ = child.kill();
                    eprintln!(
                        "[AGENT] ❌ Output size limit exceeded ({}MB)",
                        MAX_OUTPUT_SIZE_BYTES / (1024 * 1024)
                    );
                    return Err(anyhow!(
                        "Command output size limit exceeded ({} bytes)",
                        MAX_OUTPUT_SIZE_BYTES
                    ));
                }
                output.extend_from_slice(&buf[..n]);
            }
            Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                // No data available, check if process exited
                if let Some(status) = child.try_wait()? {
                    // Process exited, read remaining stderr
                    let _ = stderr.read_to_end(&mut error);

                    if !status.success() {
                        let stderr_str = String::from_utf8_lossy(&error);
                        eprintln!("[AGENT] ❌ Command failed: {}", stderr_str);
                        return Err(anyhow!(
                            "Command failed with exit code {}: {}",
                            status.code().unwrap_or(-1),
                            stderr_str
                        ));
                    }
                    break;
                }
                // Process still running, wait a bit
                std::thread::sleep(Duration::from_millis(10));
            }
            Err(e) => {
                let _ = child.kill();
                return Err(anyhow!("Failed to read command output: {}", e));
            }
        }

        // Also check if process has exited
        if let Some(status) = child.try_wait()? {
            // Read any remaining data
            let _ = stdout.read_to_end(&mut output);
            let _ = stderr.read_to_end(&mut error);

            if !status.success() {
                let stderr_str = String::from_utf8_lossy(&error);
                eprintln!("[AGENT] ❌ Command failed: {}", stderr_str);
                return Err(anyhow!(
                    "Command failed with exit code {}: {}",
                    status.code().unwrap_or(-1),
                    stderr_str
                ));
            }
            break;
        }
    }

    let elapsed = start_time.elapsed();
    let stdout_str = String::from_utf8_lossy(&output);

    eprintln!(
        "[AGENT] ✓ Completed in {:.2}s ({} bytes output)",
        elapsed.as_secs_f64(),
        total_bytes
    );

    // Log output (truncated for security)
    if !stdout_str.is_empty() {
        let preview = if stdout_str.len() > 200 {
            format!("{}...", &stdout_str[..200])
        } else {
            stdout_str.to_string()
        };
        eprintln!("[AGENT] Output: {}", preview.replace('\n', " "));
    }

    Ok(())
}

/// Configure process sandbox with platform-specific restrictions
///
/// # Windows
/// - Creates Job Object with:
///   - CPU time limit
///   - Memory limit (512MB)
///   - Process count limit (1)
///   - No breakaway from job
///
/// # Linux
/// - setrlimit for CPU, memory, file size
/// - TODO: seccomp-bpf for syscall filtering
///
/// # macOS
/// - setrlimit for CPU, memory, file size
#[cfg(target_os = "windows")]
fn configure_sandbox(_cmd: &mut Command) -> Result<()> {
    // Windows sandboxing via Job Objects requires unsafe code and complex setup
    // For now, we rely on timeout and output limits
    // TODO: Implement full Job Object sandboxing with:
    // - CREATE_BREAKAWAY_FROM_JOB flag
    // - CREATE_SUSPENDED for job assignment
    // - SetInformationJobObject for limits

    eprintln!("[AGENT] Sandbox: Windows (timeout + output limits)");
    Ok(())
}

#[cfg(target_os = "linux")]
fn configure_sandbox(cmd: &mut Command) -> Result<()> {
    use std::os::unix::process::CommandExt;

    // SECURITY: Use pre_exec to set resource limits for sandboxing
    // This unsafe block is necessary for system-level resource control
    // and is used exclusively for security hardening purposes
    unsafe {
        cmd.pre_exec(|| {
            use libc::{rlimit, setrlimit, RLIMIT_AS, RLIMIT_CPU, RLIMIT_FSIZE};

            // CPU time limit: 30 seconds (prevents CPU exhaustion attacks)
            let cpu_limit = rlimit {
                rlim_cur: MAX_EXECUTION_TIMEOUT_SECS,
                rlim_max: MAX_EXECUTION_TIMEOUT_SECS,
            };
            if setrlimit(RLIMIT_CPU, &cpu_limit) != 0 {
                eprintln!("[AGENT] Warning: Failed to set CPU limit");
            }

            // Memory limit: 512 MB
            let mem_limit = rlimit {
                rlim_cur: 512 * 1024 * 1024,
                rlim_max: 512 * 1024 * 1024,
            };
            if setrlimit(RLIMIT_AS, &mem_limit) != 0 {
                eprintln!("[AGENT] Warning: Failed to set memory limit");
            }

            // File size limit: 100 MB
            let file_limit = rlimit {
                rlim_cur: 100 * 1024 * 1024,
                rlim_max: 100 * 1024 * 1024,
            };
            if setrlimit(RLIMIT_FSIZE, &file_limit) != 0 {
                eprintln!("[AGENT] Warning: Failed to set file size limit");
            }

            Ok(())
        });
    }

    eprintln!(
        "[AGENT] Sandbox: Linux (setrlimit: CPU={}s, MEM=512MB, FSIZE=100MB)",
        MAX_EXECUTION_TIMEOUT_SECS
    );
    Ok(())
}

#[cfg(target_os = "macos")]
fn configure_sandbox(cmd: &mut Command) -> Result<()> {
    use std::os::unix::process::CommandExt;

    // SECURITY: Use pre_exec to set resource limits for sandboxing on macOS
    // This unsafe block is necessary for system-level resource control
    // and is used exclusively for security hardening purposes
    unsafe {
        cmd.pre_exec(|| {
            use libc::{rlimit, setrlimit, RLIMIT_AS, RLIMIT_CPU, RLIMIT_FSIZE};

            // CPU time limit: 30 seconds (prevents CPU exhaustion attacks)
            let cpu_limit = rlimit {
                rlim_cur: MAX_EXECUTION_TIMEOUT_SECS,
                rlim_max: MAX_EXECUTION_TIMEOUT_SECS,
            };
            if setrlimit(RLIMIT_CPU, &cpu_limit) != 0 {
                eprintln!("[AGENT] Warning: Failed to set CPU limit");
            }

            // Memory limit: 512 MB
            let mem_limit = rlimit {
                rlim_cur: 512 * 1024 * 1024,
                rlim_max: 512 * 1024 * 1024,
            };
            if setrlimit(RLIMIT_AS, &mem_limit) != 0 {
                eprintln!("[AGENT] Warning: Failed to set memory limit");
            }

            // File size limit: 100 MB
            let file_limit = rlimit {
                rlim_cur: 100 * 1024 * 1024,
                rlim_max: 100 * 1024 * 1024,
            };
            if setrlimit(RLIMIT_FSIZE, &file_limit) != 0 {
                eprintln!("[AGENT] Warning: Failed to set file size limit");
            }

            Ok(())
        });
    }

    eprintln!(
        "[AGENT] Sandbox: macOS (setrlimit: CPU={}s, MEM=512MB, FSIZE=100MB)",
        MAX_EXECUTION_TIMEOUT_SECS
    );
    Ok(())
}

#[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
fn configure_sandbox(_cmd: &mut Command) -> Result<()> {
    eprintln!("[AGENT] Sandbox: Fallback (timeout + output limits only)");
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::security::{configure_command_security, CommandSecurityConfig};
    use std::collections::HashSet;

    #[test]
    fn test_plan_validation() {
        // Valid goal
        let result = plan("List all files in the current directory");
        assert!(result.is_ok());

        // Too long
        let long_goal = "a".repeat(5000);
        let result = plan(&long_goal);
        assert!(result.is_err());

        // Empty goal
        let result = plan("");
        assert!(result.is_err());
    }

    #[test]
    fn test_plan_decomposition_files() {
        // Test file listing goal
        let result = plan("List all files in /tmp directory").unwrap();
        assert!(!result.steps.is_empty());
        assert_eq!(result.steps[0].tool, "ls");

        // Test file reading goal
        let result = plan("Read file '/etc/hosts'").unwrap();
        assert!(!result.steps.is_empty());
        assert_eq!(result.steps[0].tool, "cat");
    }

    #[test]
    fn test_plan_decomposition_git() {
        // Test git status goal
        let result = plan("Check git status").unwrap();
        assert!(!result.steps.is_empty());
        assert_eq!(result.steps[0].tool, "git");

        // Test git push goal
        let result = plan("Push changes to git").unwrap();
        assert!(!result.steps.is_empty());
        assert_eq!(result.steps[0].tool, "git");
    }

    #[test]
    fn test_plan_decomposition_search() {
        let result = plan("Find files matching '*.rs' in src/").unwrap();
        assert!(!result.steps.is_empty());
        assert_eq!(result.steps[0].tool, "find");
    }

    #[test]
    fn test_path_extraction() {
        assert_eq!(
            extract_path_from_goal("read file '/tmp/test.txt'"),
            Some("/tmp/test.txt".to_string())
        );
        assert_eq!(
            extract_path_from_goal("list ./src directory"),
            Some("./src".to_string())
        );
        assert_eq!(
            extract_path_from_goal("check /var/log"),
            Some("/var/log".to_string())
        );
    }

    #[test]
    fn test_pattern_extraction() {
        assert_eq!(
            extract_pattern_from_goal("find files matching '*.rs'"),
            Some("*.rs".to_string())
        );
        assert_eq!(
            extract_pattern_from_goal("search for TODO"),
            Some("TODO".to_string())
        );
        assert_eq!(
            extract_pattern_from_goal("files named config.json"),
            Some("config.json".to_string())
        );
    }

    #[test]
    fn test_url_extraction() {
        assert_eq!(
            extract_url_from_goal("download from https://example.com/file.txt"),
            Some("https://example.com/file.txt".to_string())
        );
        assert_eq!(
            extract_url_from_goal("fetch http://api.test.com/data"),
            Some("http://api.test.com/data".to_string())
        );
        assert_eq!(extract_url_from_goal("no url here"), None);
    }

    #[test]
    fn test_execute_empty_plan() {
        let plan = Plan {
            goal: "Test goal".to_string(),
            steps: vec![],
        };
        let result = execute(&plan);
        assert!(result.is_ok());
    }

    #[test]
    fn test_execute_with_command_validation() {
        // Configure allowlist
        let mut allowed = HashSet::new();
        // Use commands that exist on all platforms
        #[cfg(target_os = "windows")]
        allowed.insert("cmd".to_string());
        #[cfg(not(target_os = "windows"))]
        allowed.insert("echo".to_string());

        let config = CommandSecurityConfig {
            allowed_commands: allowed,
            log_attempts: false,
            max_command_length: 1000,
            max_args: 50,
        };
        configure_command_security(config).unwrap();

        // Valid command that works on the platform
        #[cfg(target_os = "windows")]
        let plan = Plan {
            goal: "Test".to_string(),
            steps: vec![Step {
                tool: "cmd".to_string(),
                args: serde_json::json!(["/C", "echo", "hello"]),
            }],
        };
        #[cfg(not(target_os = "windows"))]
        let plan = Plan {
            goal: "Test".to_string(),
            steps: vec![Step {
                tool: "echo".to_string(),
                args: serde_json::json!(["hello", "world"]),
            }],
        };

        let result = execute(&plan);
        if let Err(e) = &result {
            eprintln!("Error executing command: {:?}", e);
        }
        assert!(result.is_ok(), "command should succeed");

        // Invalid command (not in allowlist)
        let plan = Plan {
            goal: "Test".to_string(),
            steps: vec![Step {
                tool: "rm".to_string(),
                args: serde_json::json!(["-rf", "/"]),
            }],
        };
        let result = execute(&plan);
        assert!(result.is_err(), "rm command should be blocked");
    }

    #[test]
    fn test_parse_step_args() {
        // Array args
        let args = serde_json::json!(["arg1", "arg2", 123]);
        let result = parse_step_args(&args).unwrap();
        assert_eq!(result, vec!["arg1", "arg2", "123"]);

        // Object args
        let args = serde_json::json!({"key": "value", "count": 42});
        let result = parse_step_args(&args).unwrap();
        assert!(result.contains(&"key=value".to_string()));
        assert!(result.contains(&"count=42".to_string()));

        // String arg
        let args = serde_json::json!("single-arg");
        let result = parse_step_args(&args).unwrap();
        assert_eq!(result, vec!["single-arg"]);

        // Null
        let args = serde_json::json!(null);
        let result = parse_step_args(&args).unwrap();
        assert_eq!(result, Vec::<String>::new());
    }
}