agcodex-core 0.1.0

Core business logic with AST-RAG engine and tree-sitter integration
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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
use regex::Regex;
use serde::Deserialize;
use serde::Serialize;
use std::collections::HashMap;
use std::collections::HashSet;
use std::sync::Arc;
use thiserror::Error;
use tree_sitter::Parser;
use tree_sitter::Tree;
use tree_sitter_bash::LANGUAGE as BASH;

/// Errors that can occur during bash parsing and validation
#[derive(Error, Debug, Clone, PartialEq)]
#[allow(dead_code)]
pub enum BashParseError {
    #[error("Failed to parse bash syntax: {message}")]
    ParseError { message: String },

    #[error("Dangerous pattern detected: {pattern} in command: {command}")]
    DangerousPattern { pattern: String, command: String },

    #[error("Command not allowed: {command}")]
    ForbiddenCommand { command: String },

    #[error("Unsafe context detected: {reason}")]
    UnsafeContext { reason: String },

    #[error("Rewriting failed: {reason}")]
    RewriteError { reason: String },
}

/// Confidence levels for safety analysis
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConfidenceLevel {
    Low,      // 0-40% confidence
    Medium,   // 41-70% confidence
    High,     // 71-90% confidence
    VeryHigh, // 91-100% confidence
}

impl ConfidenceLevel {
    #[allow(dead_code)]
    pub fn from_score(score: f32) -> Self {
        match score {
            s if s <= 0.40 => Self::Low,
            s if s <= 0.70 => Self::Medium,
            s if s <= 0.90 => Self::High,
            _ => Self::VeryHigh,
        }
    }

    #[allow(dead_code)]
    pub const fn as_score(&self) -> f32 {
        match self {
            Self::Low => 0.25,
            Self::Medium => 0.55,
            Self::High => 0.80,
            Self::VeryHigh => 0.95,
        }
    }
}

/// Applied command rewrite information
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AppliedRewrite {
    pub original_pattern: String,
    pub replacement: String,
    pub reason: String,
    pub position: usize,
}

/// Security analysis result
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SecurityAnalysis {
    pub threat_level: ThreatLevel,
    pub detected_patterns: Vec<String>,
    pub risk_factors: Vec<String>,
    pub recommendations: Vec<String>,
}

/// Threat assessment levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ThreatLevel {
    Safe,
    Low,
    Medium,
    High,
    Critical,
}

/// Safe command with validation context
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SafeCommand {
    pub commands: Vec<Vec<String>>,
    pub original_command: String,
    pub final_command: String,
}

/// Complete parsing and validation result
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SafeCommandReport {
    pub safe_command: SafeCommand,
    pub security_analysis: SecurityAnalysis,
    pub applied_rewrites: Vec<AppliedRewrite>,
    pub confidence_score: f32,
    pub confidence_level: ConfidenceLevel,
    pub validation_time_ms: u64,
}

/// Sandbox rule for command restriction
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SandboxRule {
    pub name: String,
    pub description: String,
    pub pattern: String,
    pub action: SandboxAction,
}

/// Actions to take when sandbox rule matches
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum SandboxAction {
    Block,
    Rewrite { replacement: String },
    Restrict { allowed_paths: Vec<String> },
    RequireApproval,
}

/// Command validator for security analysis
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct CommandValidator {
    dangerous_patterns: Vec<Regex>,
    allowed_commands: HashSet<String>,
    context_rules: Vec<SandboxRule>,
}

impl CommandValidator {
    pub fn new() -> Self {
        let dangerous_patterns = vec![
            // Command injection patterns
            Regex::new(r"[;&|`$(){}\[\]<>]").unwrap(),
            // Suspicious command sequences
            Regex::new(r"(rm|del|delete).*-r").unwrap(),
            Regex::new(r"(chmod|chown).*777").unwrap(),
            // Network operations
            Regex::new(r"(curl|wget|nc|netcat).*\|").unwrap(),
            // System manipulation
            Regex::new(r"(sudo|su)\s").unwrap(),
            // File system traversal
            Regex::new(r"\.\./.*\.\./").unwrap(),
        ];

        let allowed_commands = [
            // File operations (safe subset)
            "ls", "cat", "head", "tail", "less", "more", "file", "stat", "find", "grep", "awk",
            "sed", "sort", "uniq", "wc", "cut", // Directory operations
            "pwd", "cd", "mkdir", "rmdir", // Text processing
            "echo", "printf", "tr", "diff", "comm", "join",
            // Archive operations (read-only)
            "tar", "gzip", "gunzip", "zip", "unzip", // Development tools
            "git", "cargo", "npm", "python", "python3", "node", "rustc", "gcc", "clang", "make",
            "cmake", // System info (read-only)
            "ps", "top", "df", "du", "free", "uname", "whoami", "id", "date", "uptime", "env",
            "printenv", // Safe utilities
            "true", "false", "yes", "sleep", "timeout", // Package managers (query only)
            "apt", "yum", "brew", "pacman",
        ]
        .iter()
        .map(|&s| s.to_string())
        .collect();

        let context_rules = vec![
            SandboxRule {
                name: "no_root_commands".to_string(),
                description: "Block commands that require root access".to_string(),
                pattern: r"^(sudo|su|doas)\s".to_string(),
                action: SandboxAction::Block,
            },
            SandboxRule {
                name: "safe_rm".to_string(),
                description: "Rewrite dangerous rm commands".to_string(),
                pattern: r"rm\s+(-rf?|--recursive)\s+/".to_string(),
                action: SandboxAction::Block,
            },
        ];

        Self {
            dangerous_patterns,
            allowed_commands,
            context_rules,
        }
    }

    #[allow(dead_code)]
    pub fn validate_command(&self, command: &[String]) -> Result<SecurityAnalysis, BashParseError> {
        if command.is_empty() {
            return Ok(SecurityAnalysis {
                threat_level: ThreatLevel::Safe,
                detected_patterns: vec![],
                risk_factors: vec![],
                recommendations: vec![],
            });
        }

        let cmd_name = &command[0];
        let full_command = command.join(" ");
        let mut detected_patterns = Vec::new();
        let mut risk_factors = Vec::new();
        let mut recommendations = Vec::new();

        // Check if command is in allowed list
        if !self.allowed_commands.contains(cmd_name) {
            return Err(BashParseError::ForbiddenCommand {
                command: cmd_name.clone(),
            });
        }

        // Check for dangerous patterns
        for pattern in &self.dangerous_patterns {
            if pattern.is_match(&full_command) {
                detected_patterns.push(pattern.as_str().to_string());
            }
        }

        // Assess threat level
        let threat_level = if !detected_patterns.is_empty() {
            risk_factors.push("Command contains potentially dangerous patterns".to_string());
            recommendations.push("Review command arguments for security implications".to_string());
            ThreatLevel::Medium
        } else if self.is_high_risk_command(cmd_name) {
            risk_factors.push("Command has elevated privileges or system access".to_string());
            recommendations.push("Use with caution in production environments".to_string());
            ThreatLevel::Low
        } else {
            ThreatLevel::Safe
        };

        Ok(SecurityAnalysis {
            threat_level,
            detected_patterns,
            risk_factors,
            recommendations,
        })
    }

    #[allow(dead_code)]
    fn is_high_risk_command(&self, command: &str) -> bool {
        matches!(
            command,
            "rm" | "mv" | "cp" | "chmod" | "chown" | "git" | "cargo" | "npm"
        )
    }
}

/// Command rewriter for dangerous patterns
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct CommandRewriter {
    rewrite_rules: HashMap<String, String>,
}

impl CommandRewriter {
    pub fn new() -> Self {
        let mut rewrite_rules = HashMap::new();

        // Safe alternatives for dangerous commands
        rewrite_rules.insert("rm -rf".to_string(), "rm -r".to_string());
        rewrite_rules.insert("chmod 777".to_string(), "chmod 755".to_string());
        rewrite_rules.insert("chown -R".to_string(), "chown".to_string());

        Self { rewrite_rules }
    }

    #[allow(dead_code)]
    pub fn rewrite_command(&self, command: &mut Vec<String>) -> Vec<AppliedRewrite> {
        let mut applied_rewrites = Vec::new();
        let _original_command = command.join(" ");

        // Apply rewrite rules
        for (pattern, replacement) in &self.rewrite_rules {
            let original_joined = command.join(" ");
            if original_joined.contains(pattern) {
                let new_command = original_joined.replace(pattern, replacement);
                let new_parts: Vec<String> = new_command
                    .split_whitespace()
                    .map(|s| s.to_string())
                    .collect();

                if new_parts != *command {
                    applied_rewrites.push(AppliedRewrite {
                        original_pattern: pattern.clone(),
                        replacement: replacement.clone(),
                        reason: "Security enhancement".to_string(),
                        position: original_joined.find(pattern).unwrap_or(0),
                    });
                    *command = new_parts;
                }
            }
        }

        applied_rewrites
    }
}

/// Sandbox rules engine
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct SandboxRules {
    rules: Vec<SandboxRule>,
}

impl SandboxRules {
    pub fn new() -> Self {
        let rules = vec![SandboxRule {
            name: "workspace_only".to_string(),
            description: "Restrict file operations to workspace directory".to_string(),
            pattern: r"^(/|~|\.\./)".to_string(),
            action: SandboxAction::Restrict {
                allowed_paths: vec!["./*".to_string(), "./target/*".to_string()],
            },
        }];

        Self { rules }
    }

    #[allow(dead_code)]
    pub fn apply_rules(&self, command: &[String]) -> Result<(), BashParseError> {
        let full_command = command.join(" ");

        for rule in &self.rules {
            let pattern = Regex::new(&rule.pattern).map_err(|e| BashParseError::UnsafeContext {
                reason: format!("Invalid rule pattern: {}", e),
            })?;

            if pattern.is_match(&full_command) {
                match &rule.action {
                    SandboxAction::Block => {
                        return Err(BashParseError::UnsafeContext {
                            reason: format!("Blocked by rule: {}", rule.name),
                        });
                    }
                    SandboxAction::RequireApproval => {
                        // In a real implementation, this would prompt for user approval
                        return Err(BashParseError::UnsafeContext {
                            reason: format!("Requires approval: {}", rule.description),
                        });
                    }
                    SandboxAction::Rewrite { replacement: _ } => {
                        // Rewriting would be handled by CommandRewriter
                    }
                    SandboxAction::Restrict { allowed_paths: _ } => {
                        // Path restriction validation would be implemented here
                    }
                }
            }
        }

        Ok(())
    }
}

/// Enhanced bash parser with comprehensive safety validation
#[derive(Clone)]
#[allow(dead_code)]
pub struct EnhancedBashParser {
    parser: Arc<std::sync::Mutex<Parser>>,
    validator: CommandValidator,
    sandbox_rules: SandboxRules,
    rewriter: CommandRewriter,
}

impl std::fmt::Debug for EnhancedBashParser {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EnhancedBashParser")
            .field("validator", &self.validator)
            .field("sandbox_rules", &self.sandbox_rules)
            .field("rewriter", &self.rewriter)
            .field("parser", &"<tree_sitter::Parser>")
            .finish()
    }
}

impl EnhancedBashParser {
    pub fn new() -> Result<Self, BashParseError> {
        let lang = BASH.into();
        let mut parser = Parser::new();
        parser
            .set_language(&lang)
            .map_err(|e| BashParseError::ParseError {
                message: format!("Failed to load bash grammar: {}", e),
            })?;

        Ok(Self {
            parser: Arc::new(std::sync::Mutex::new(parser)),
            validator: CommandValidator::new(),
            sandbox_rules: SandboxRules::new(),
            rewriter: CommandRewriter::new(),
        })
    }

    /// Parse and validate a bash command with comprehensive safety analysis
    #[allow(dead_code)]
    pub fn parse_and_validate(&self, cmd: &str) -> Result<SafeCommandReport, BashParseError> {
        let start_time = std::time::Instant::now();

        // Step 1: Parse with tree-sitter
        let tree = self.parse_bash(cmd)?;

        // Step 2: Extract command structure
        let mut commands = self.extract_commands(&tree, cmd)?;

        // Step 3: Apply command rewriting
        let mut all_rewrites = Vec::new();
        for command in &mut commands {
            let rewrites = self.rewriter.rewrite_command(command);
            all_rewrites.extend(rewrites);
        }

        // Step 4: Security validation
        let mut security_analyses = Vec::new();
        for command in &commands {
            let analysis = self.validator.validate_command(command)?;
            security_analyses.push(analysis);
        }

        // Step 5: Apply sandbox restrictions
        for command in &commands {
            self.sandbox_rules.apply_rules(command)?;
        }

        // Step 6: Generate comprehensive report
        let final_command = commands
            .iter()
            .map(|cmd| cmd.join(" "))
            .collect::<Vec<_>>()
            .join(" && ");

        let overall_security_analysis = self.merge_security_analyses(security_analyses);
        let confidence_score =
            self.calculate_confidence_score(&commands, &overall_security_analysis);
        let validation_time_ms = start_time.elapsed().as_millis() as u64;

        Ok(SafeCommandReport {
            safe_command: SafeCommand {
                commands,
                original_command: cmd.to_string(),
                final_command,
            },
            security_analysis: overall_security_analysis,
            applied_rewrites: all_rewrites,
            confidence_score,
            confidence_level: ConfidenceLevel::from_score(confidence_score),
            validation_time_ms,
        })
    }

    #[allow(dead_code)]
    fn parse_bash(&self, cmd: &str) -> Result<Tree, BashParseError> {
        let mut parser = self.parser.lock().map_err(|_| BashParseError::ParseError {
            message: "Failed to acquire parser lock".to_string(),
        })?;

        parser
            .parse(cmd, None)
            .ok_or_else(|| BashParseError::ParseError {
                message: "Failed to parse bash command".to_string(),
            })
    }

    #[allow(dead_code)]
    fn extract_commands(&self, tree: &Tree, src: &str) -> Result<Vec<Vec<String>>, BashParseError> {
        try_parse_word_only_commands_sequence(tree, src).ok_or_else(|| BashParseError::ParseError {
            message: "Command contains unsupported constructs".to_string(),
        })
    }

    #[allow(dead_code)]
    fn merge_security_analyses(&self, analyses: Vec<SecurityAnalysis>) -> SecurityAnalysis {
        let mut detected_patterns = Vec::new();
        let mut risk_factors = Vec::new();
        let mut recommendations = Vec::new();
        let mut max_threat_level = ThreatLevel::Safe;

        for analysis in analyses {
            detected_patterns.extend(analysis.detected_patterns);
            risk_factors.extend(analysis.risk_factors);
            recommendations.extend(analysis.recommendations);

            if self.threat_level_severity(analysis.threat_level)
                > self.threat_level_severity(max_threat_level)
            {
                max_threat_level = analysis.threat_level;
            }
        }

        // Remove duplicates
        detected_patterns.sort();
        detected_patterns.dedup();
        risk_factors.sort();
        risk_factors.dedup();
        recommendations.sort();
        recommendations.dedup();

        SecurityAnalysis {
            threat_level: max_threat_level,
            detected_patterns,
            risk_factors,
            recommendations,
        }
    }

    #[allow(dead_code)]
    const fn threat_level_severity(&self, level: ThreatLevel) -> u8 {
        match level {
            ThreatLevel::Safe => 0,
            ThreatLevel::Low => 1,
            ThreatLevel::Medium => 2,
            ThreatLevel::High => 3,
            ThreatLevel::Critical => 4,
        }
    }

    #[allow(dead_code)]
    fn calculate_confidence_score(
        &self,
        commands: &[Vec<String>],
        analysis: &SecurityAnalysis,
    ) -> f32 {
        let mut score = 1.0;

        // Reduce confidence for each detected risk factor
        score -= analysis.detected_patterns.len() as f32 * 0.1;
        score -= analysis.risk_factors.len() as f32 * 0.05;

        // Adjust for threat level
        score *= match analysis.threat_level {
            ThreatLevel::Safe => 1.0,
            ThreatLevel::Low => 0.9,
            ThreatLevel::Medium => 0.7,
            ThreatLevel::High => 0.5,
            ThreatLevel::Critical => 0.2,
        };

        // Reduce confidence for complex command chains
        if commands.len() > 3 {
            score *= 0.8;
        }

        score.clamp(0.0, 1.0)
    }
}

impl Default for EnhancedBashParser {
    fn default() -> Self {
        Self::new().expect("Failed to create default EnhancedBashParser")
    }
}

/// Legacy function maintained for backward compatibility
/// Parse the provided bash source using tree-sitter-bash, returning a Tree on
/// success or None if parsing failed.
pub fn try_parse_bash(bash_lc_arg: &str) -> Option<Tree> {
    let lang = BASH.into();
    let mut parser = Parser::new();
    #[expect(clippy::expect_used)]
    parser.set_language(&lang).expect("load bash grammar");
    let old_tree: Option<&Tree> = None;
    parser.parse(bash_lc_arg, old_tree)
}

/// Parse a script which may contain multiple simple commands joined only by
/// the safe logical/pipe/sequencing operators: `&&`, `||`, `;`, `|`.
///
/// Returns `Some(Vec<command_words>)` if every command is a plain word‑only
/// command and the parse tree does not contain disallowed constructs
/// (parentheses, redirections, substitutions, control flow, etc.). Otherwise
/// returns `None`.
pub fn try_parse_word_only_commands_sequence(tree: &Tree, src: &str) -> Option<Vec<Vec<String>>> {
    if tree.root_node().has_error() {
        return None;
    }

    // List of allowed (named) node kinds for a "word only commands sequence".
    // If we encounter a named node that is not in this list we reject.
    const ALLOWED_KINDS: &[&str] = &[
        // top level containers
        "program",
        "list",
        "pipeline",
        "negated_command",
        // commands & words
        "command",
        "command_name",
        "word",
        "string",
        "string_content",
        "raw_string",
        "number",
        "concatenation",
        // safe expansions
        "simple_expansion",
        "expansion",
        // control flow (safe subset)
        "if_statement",
        "then_clause",
        "else_clause",
        "elif_clause",
        "case_statement",
        "case_item",
        // loops (safe subset)
        "for_statement",
        "while_statement",
        "do_group",
        // functions (safe subset)
        "function_definition",
        "compound_statement",
        // safe file descriptors
        "file_descriptor",
        "heredoc_start",
        "heredoc_body",
        // comments and whitespace
        "comment",
    ];
    // Allow only safe punctuation / operator tokens; anything else causes reject.
    const ALLOWED_PUNCT_TOKENS: &[&str] = &["&&", "||", ";", "|", "\"", "'"];

    let root = tree.root_node();
    let mut cursor = root.walk();
    let mut stack = vec![root];
    let mut command_nodes = Vec::new();
    while let Some(node) = stack.pop() {
        let kind = node.kind();
        if node.is_named() {
            if !ALLOWED_KINDS.contains(&kind) {
                return None;
            }
            if kind == "command" {
                command_nodes.push(node);
            }
        } else {
            // Reject any punctuation / operator tokens that are not explicitly allowed.
            if kind.chars().any(|c| "&;|".contains(c)) && !ALLOWED_PUNCT_TOKENS.contains(&kind) {
                return None;
            }
            if !(ALLOWED_PUNCT_TOKENS.contains(&kind) || kind.trim().is_empty()) {
                // If it's a quote token or operator it's allowed above; we also allow whitespace tokens.
                // Any other punctuation like parentheses, braces, redirects, backticks, etc are rejected.
                return None;
            }
        }
        for child in node.children(&mut cursor) {
            stack.push(child);
        }
    }

    let mut commands = Vec::new();
    for node in command_nodes {
        if let Some(words) = parse_plain_command_from_node(node, src) {
            commands.push(words);
        } else {
            return None;
        }
    }
    Some(commands)
}

fn parse_plain_command_from_node(cmd: tree_sitter::Node, src: &str) -> Option<Vec<String>> {
    if cmd.kind() != "command" {
        return None;
    }
    let mut words = Vec::new();
    let mut cursor = cmd.walk();
    for child in cmd.named_children(&mut cursor) {
        match child.kind() {
            "command_name" => {
                let word_node = child.named_child(0)?;
                if word_node.kind() != "word" {
                    return None;
                }
                words.push(word_node.utf8_text(src.as_bytes()).ok()?.to_owned());
            }
            "word" | "number" => {
                words.push(child.utf8_text(src.as_bytes()).ok()?.to_owned());
            }
            "string" => {
                if child.child_count() == 3
                    && child.child(0)?.kind() == "\""
                    && child.child(1)?.kind() == "string_content"
                    && child.child(2)?.kind() == "\""
                {
                    words.push(child.child(1)?.utf8_text(src.as_bytes()).ok()?.to_owned());
                } else {
                    return None;
                }
            }
            "raw_string" => {
                let raw_string = child.utf8_text(src.as_bytes()).ok()?;
                let stripped = raw_string
                    .strip_prefix('\'')
                    .and_then(|s| s.strip_suffix('\''));
                if let Some(s) = stripped {
                    words.push(s.to_owned());
                } else {
                    return None;
                }
            }
            _ => return None,
        }
    }
    Some(words)
}

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

    fn parse_seq(src: &str) -> Option<Vec<Vec<String>>> {
        let tree = try_parse_bash(src)?;
        try_parse_word_only_commands_sequence(&tree, src)
    }

    #[test]
    fn accepts_single_simple_command() {
        let cmds = parse_seq("ls -1").unwrap();
        assert_eq!(cmds, vec![vec!["ls".to_string(), "-1".to_string()]]);
    }

    #[test]
    fn accepts_multiple_commands_with_allowed_operators() {
        let src = "ls && pwd; echo 'hi there' | wc -l";
        let cmds = parse_seq(src).unwrap();
        let expected: Vec<Vec<String>> = vec![
            vec!["wc".to_string(), "-l".to_string()],
            vec!["echo".to_string(), "hi there".to_string()],
            vec!["pwd".to_string()],
            vec!["ls".to_string()],
        ];
        assert_eq!(cmds, expected);
    }

    #[test]
    fn extracts_double_and_single_quoted_strings() {
        let cmds = parse_seq("echo \"hello world\"").unwrap();
        assert_eq!(
            cmds,
            vec![vec!["echo".to_string(), "hello world".to_string()]]
        );

        let cmds2 = parse_seq("echo 'hi there'").unwrap();
        assert_eq!(
            cmds2,
            vec![vec!["echo".to_string(), "hi there".to_string()]]
        );
    }

    #[test]
    fn accepts_numbers_as_words() {
        let cmds = parse_seq("echo 123 456").unwrap();
        assert_eq!(
            cmds,
            vec![vec![
                "echo".to_string(),
                "123".to_string(),
                "456".to_string()
            ]]
        );
    }

    #[test]
    fn rejects_parentheses_and_subshells() {
        assert!(parse_seq("(ls)").is_none());
        assert!(parse_seq("ls || (pwd && echo hi)").is_none());
    }

    #[test]
    fn rejects_redirections_and_unsupported_operators() {
        assert!(parse_seq("ls > out.txt").is_none());
        assert!(parse_seq("echo hi & echo bye").is_none());
    }

    #[test]
    fn rejects_command_and_process_substitutions_and_expansions() {
        assert!(parse_seq("echo $(pwd)").is_none());
        assert!(parse_seq("echo `pwd`").is_none());
        assert!(parse_seq("echo $HOME").is_none());
        assert!(parse_seq("echo \"hi $USER\"").is_none());
    }

    #[test]
    fn rejects_variable_assignment_prefix() {
        assert!(parse_seq("FOO=bar ls").is_none());
    }

    #[test]
    fn rejects_trailing_operator_parse_error() {
        assert!(parse_seq("ls &&").is_none());
    }

    // ========== Enhanced Parser Tests ==========

    #[test]
    fn enhanced_parser_creation() {
        let parser = EnhancedBashParser::new();
        assert!(parser.is_ok(), "Enhanced parser should create successfully");
    }

    #[test]
    fn enhanced_parser_validates_safe_command() {
        let parser = EnhancedBashParser::new().unwrap();
        let result = parser.parse_and_validate("echo hello world");

        assert!(result.is_ok(), "Safe command should validate successfully");
        let report = result.unwrap();

        assert_eq!(
            report.safe_command.commands,
            vec![vec![
                "echo".to_string(),
                "hello".to_string(),
                "world".to_string()
            ]]
        );
        assert_eq!(report.security_analysis.threat_level, ThreatLevel::Safe);
        assert!(
            report.confidence_score > 0.8,
            "Confidence should be high for safe commands"
        );
        assert_eq!(report.confidence_level, ConfidenceLevel::VeryHigh);
        assert!(
            report.applied_rewrites.is_empty(),
            "No rewrites should be applied to safe commands"
        );
    }

    #[test]
    fn enhanced_parser_detects_forbidden_command() {
        let parser = EnhancedBashParser::new().unwrap();
        let result = parser.parse_and_validate("dangerous_command --malicious");

        assert!(result.is_err(), "Forbidden command should be rejected");
        match result.unwrap_err() {
            BashParseError::ForbiddenCommand { command } => {
                assert_eq!(command, "dangerous_command");
            }
            _ => panic!("Expected ForbiddenCommand error"),
        }
    }

    #[test]
    fn enhanced_parser_applies_rewrites() {
        let parser = EnhancedBashParser::new().unwrap();
        // This would be rewritten if the pattern exists in the rewriter
        let result = parser.parse_and_validate("chmod 777 file.txt");

        match result {
            Ok(report) => {
                // If the command is allowed and rewritten
                if !report.applied_rewrites.is_empty() {
                    assert!(report.applied_rewrites[0].original_pattern.contains("777"));
                    assert!(report.applied_rewrites[0].replacement.contains("755"));
                    assert_eq!(report.applied_rewrites[0].reason, "Security enhancement");
                }
            }
            Err(BashParseError::ForbiddenCommand { .. }) => {
                // chmod might not be in allowed commands, which is also valid
            }
            Err(e) => panic!("Unexpected error: {:?}", e),
        }
    }

    #[test]
    fn enhanced_parser_security_analysis() {
        let parser = EnhancedBashParser::new().unwrap();
        let result = parser.parse_and_validate("find . -name '*.rs'");

        match result {
            Ok(report) => {
                assert!(matches!(
                    report.security_analysis.threat_level,
                    ThreatLevel::Safe | ThreatLevel::Low
                ));
                assert!(
                    report.confidence_score > 0.5,
                    "Should have reasonable confidence"
                );
                // Relaxed timing check - validation time might be 0 on very fast systems
                assert!(
                    report.validation_time_ms >= 0,
                    "Should have non-negative validation time"
                );
            }
            Err(BashParseError::ForbiddenCommand { .. }) => {
                // find might not be in allowed commands list
            }
            Err(e) => panic!("Unexpected error: {:?}", e),
        }
    }

    #[test]
    fn enhanced_parser_handles_complex_safe_pipeline() {
        let parser = EnhancedBashParser::new().unwrap();
        let result = parser.parse_and_validate("ls -la | grep 'test' | sort");

        match result {
            Ok(report) => {
                assert_eq!(
                    report.safe_command.commands.len(),
                    3,
                    "Should parse three commands in pipeline"
                );
                let commands: HashSet<String> = report
                    .safe_command
                    .commands
                    .iter()
                    .map(|cmd| cmd[0].clone())
                    .collect();
                assert!(commands.contains("ls"));
                assert!(commands.contains("grep"));
                assert!(commands.contains("sort"));
            }
            Err(BashParseError::ForbiddenCommand { command }) => {
                // One of these commands might not be allowed
                assert!(matches!(command.as_str(), "ls" | "grep" | "sort"));
            }
            Err(e) => panic!("Unexpected error: {:?}", e),
        }
    }

    #[test]
    fn command_validator_creation() {
        let validator = CommandValidator::new();

        // Test that allowed commands are properly initialized
        let echo_analysis = validator.validate_command(&["echo".to_string(), "test".to_string()]);
        assert!(echo_analysis.is_ok(), "Echo should be allowed");

        let forbidden_analysis = validator.validate_command(&["malicious_cmd".to_string()]);
        assert!(
            forbidden_analysis.is_err(),
            "Unknown command should be forbidden"
        );
    }

    #[test]
    fn command_rewriter_functionality() {
        let rewriter = CommandRewriter::new();
        let mut command = vec!["rm".to_string(), "-rf".to_string(), "file.txt".to_string()];

        let rewrites = rewriter.rewrite_command(&mut command);

        // Check if any rewrite was applied (depends on the exact patterns)
        if !rewrites.is_empty() {
            assert!(!rewrites[0].original_pattern.is_empty());
            assert!(!rewrites[0].replacement.is_empty());
            assert_eq!(rewrites[0].reason, "Security enhancement");
        }
    }

    #[test]
    fn sandbox_rules_blocking() {
        let sandbox = SandboxRules::new();

        // Test workspace restriction (might trigger on absolute paths)
        let result = sandbox.apply_rules(&["cat".to_string(), "/etc/passwd".to_string()]);

        match result {
            Ok(()) => {
                // Rule might not match this exact pattern
            }
            Err(BashParseError::UnsafeContext { reason }) => {
                assert!(reason.contains("Blocked by rule") || reason.contains("Requires approval"));
            }
            Err(e) => panic!("Unexpected error type: {:?}", e),
        }
    }

    #[test]
    fn confidence_level_conversions() {
        assert_eq!(ConfidenceLevel::from_score(0.1), ConfidenceLevel::Low);
        assert_eq!(ConfidenceLevel::from_score(0.5), ConfidenceLevel::Medium);
        assert_eq!(ConfidenceLevel::from_score(0.8), ConfidenceLevel::High);
        assert_eq!(ConfidenceLevel::from_score(0.95), ConfidenceLevel::VeryHigh);

        assert!(ConfidenceLevel::VeryHigh.as_score() > 0.9);
        assert!(ConfidenceLevel::Low.as_score() < 0.5);
    }

    #[test]
    fn threat_level_severity_ordering() {
        let parser = EnhancedBashParser::new().unwrap();

        assert!(
            parser.threat_level_severity(ThreatLevel::Critical)
                > parser.threat_level_severity(ThreatLevel::High)
        );
        assert!(
            parser.threat_level_severity(ThreatLevel::High)
                > parser.threat_level_severity(ThreatLevel::Medium)
        );
        assert!(
            parser.threat_level_severity(ThreatLevel::Medium)
                > parser.threat_level_severity(ThreatLevel::Low)
        );
        assert!(
            parser.threat_level_severity(ThreatLevel::Low)
                > parser.threat_level_severity(ThreatLevel::Safe)
        );
    }

    #[test]
    fn security_analysis_merging() {
        let parser = EnhancedBashParser::new().unwrap();

        let analyses = vec![
            SecurityAnalysis {
                threat_level: ThreatLevel::Low,
                detected_patterns: vec!["pattern1".to_string()],
                risk_factors: vec!["risk1".to_string()],
                recommendations: vec!["rec1".to_string()],
            },
            SecurityAnalysis {
                threat_level: ThreatLevel::Medium,
                detected_patterns: vec!["pattern2".to_string(), "pattern1".to_string()], // duplicate
                risk_factors: vec!["risk2".to_string()],
                recommendations: vec!["rec2".to_string()],
            },
        ];

        let merged = parser.merge_security_analyses(analyses);

        assert_eq!(merged.threat_level, ThreatLevel::Medium); // Highest level
        assert_eq!(merged.detected_patterns.len(), 2); // Deduplicated
        assert!(merged.detected_patterns.contains(&"pattern1".to_string()));
        assert!(merged.detected_patterns.contains(&"pattern2".to_string()));
        assert_eq!(merged.risk_factors.len(), 2);
        assert_eq!(merged.recommendations.len(), 2);
    }

    #[test]
    fn error_types_display_correctly() {
        let parse_error = BashParseError::ParseError {
            message: "test message".to_string(),
        };
        let error_str = format!("{}", parse_error);
        assert!(error_str.contains("Failed to parse bash syntax"));
        assert!(error_str.contains("test message"));

        let danger_error = BashParseError::DangerousPattern {
            pattern: "$()".to_string(),
            command: "echo $(pwd)".to_string(),
        };
        let danger_str = format!("{}", danger_error);
        assert!(danger_str.contains("Dangerous pattern detected"));
        assert!(danger_str.contains("$()"));
    }

    #[test]
    fn serialization_roundtrip() {
        let report = SafeCommandReport {
            safe_command: SafeCommand {
                commands: vec![vec!["echo".to_string(), "test".to_string()]],
                original_command: "echo test".to_string(),
                final_command: "echo test".to_string(),
            },
            security_analysis: SecurityAnalysis {
                threat_level: ThreatLevel::Safe,
                detected_patterns: vec![],
                risk_factors: vec![],
                recommendations: vec![],
            },
            applied_rewrites: vec![],
            confidence_score: 0.95,
            confidence_level: ConfidenceLevel::VeryHigh,
            validation_time_ms: 10,
        };

        let json = serde_json::to_string(&report).expect("Should serialize to JSON");
        let deserialized: SafeCommandReport =
            serde_json::from_str(&json).expect("Should deserialize from JSON");

        assert_eq!(report, deserialized);
    }
}