eggsact 1.2.7

Deterministic MCP and in-process utility tools for coding agents
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
use regex::Regex;
use std::sync::LazyLock;

#[derive(Clone, Copy, PartialEq)]
enum QuoteState {
    None,
    Single,
    Double,
}

#[derive(Default)]
pub struct ShellFeatures {
    pub has_pipe: bool,
    pub has_redirection: bool,
    pub has_command_substitution: bool,
    pub has_variable_expansion: bool,
    pub has_glob_pattern: bool,
    pub has_control_operator: bool,
    pub has_background: bool,
    pub has_unbalanced_quotes: bool,
}

pub struct ShellSplitResult {
    pub parse_ok: bool,
    pub argv: Vec<String>,
    pub argc: usize,
    pub features: ShellFeatures,
    pub findings: Vec<String>,
}

pub struct ShellQuoteJoinResult {
    pub command: String,
    pub roundtrip_ok: bool,
    pub findings: Vec<String>,
}

#[derive(Debug, PartialEq)]
pub struct ArgvCompareResult {
    pub argv_equal: bool,
    pub left_argv: Vec<String>,
    pub right_argv: Vec<String>,
    pub first_difference: Option<usize>,
    pub findings: Vec<String>,
}

fn is_glob_char(c: char) -> bool {
    c == '*' || c == '?' || c == '['
}

fn detect_features(argv: &[String], raw: &str, unbalanced: bool) -> ShellFeatures {
    let joined = argv.join(" ");

    let has_pipe = joined.contains('|');
    let has_redirection = joined.contains('<') || joined.contains('>');
    let has_command_substitution = COMMAND_SUB_PATTERN.is_match(raw);
    // Strip single-quoted sections before checking for variable expansion
    // Single quotes prevent variable expansion in POSIX shells. Use proper
    // quote-state tracking so the shell idiom `'a'"'"'b'` (single-quote
    // escape via '"'"') does not misclassify $VAR inside that construct.
    let mut raw_without_single_quotes = String::with_capacity(raw.len());
    let mut qstate = QuoteState::None;
    for c in raw.chars() {
        match qstate {
            QuoteState::None => {
                if c == '\'' {
                    qstate = QuoteState::Single;
                    raw_without_single_quotes.push(' ');
                } else if c == '"' {
                    qstate = QuoteState::Double;
                    raw_without_single_quotes.push(c);
                } else {
                    raw_without_single_quotes.push(c);
                }
            }
            QuoteState::Single => {
                if c == '\'' {
                    qstate = QuoteState::None;
                    raw_without_single_quotes.push(' ');
                } else {
                    raw_without_single_quotes.push(' ');
                }
            }
            QuoteState::Double => {
                if c == '"' {
                    qstate = QuoteState::None;
                }
                raw_without_single_quotes.push(c);
            }
        }
    }
    let has_variable_expansion = VARIABLE_PATTERN.is_match(&raw_without_single_quotes);
    let has_glob_pattern = argv.iter().any(|t| t.chars().any(is_glob_char));

    let mut has_control_operator = false;
    for op in &["&&", "||"] {
        if joined.contains(op) {
            has_control_operator = true;
            break;
        }
    }
    if !has_control_operator && (joined.contains(';') || joined.contains('&')) {
        has_control_operator = true;
    }

    // Background execution: standalone '&' at end of command or between commands
    // (not '&&')
    let has_background = detect_background(raw);

    ShellFeatures {
        has_pipe,
        has_redirection,
        has_command_substitution,
        has_variable_expansion,
        has_glob_pattern,
        has_control_operator,
        has_background,
        has_unbalanced_quotes: unbalanced,
    }
}

static VARIABLE_PATTERN: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\$\{[^}]*\}|\$[A-Za-z_][A-Za-z0-9_]*").unwrap());

static COMMAND_SUB_PATTERN: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\$\(|`").unwrap());

/// Detect background execution (`cmd &` or `cmd1 && cmd2 &`).
///
/// Background execution is a standalone `&` at the end of a pipeline or
/// command, NOT `&&` (logical AND). Matches a non-`&` char followed by
/// `&` not followed by another `&`, then optional whitespace to end.
static BACKGROUND_PATTERN: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"[^&]&[^&]\s*$|[^&]&\s*$").unwrap());

fn detect_background(raw: &str) -> bool {
    // Check each line independently (for multi-line commands)
    for line in raw.lines() {
        let trimmed = line.trim_end();
        if BACKGROUND_PATTERN.is_match(trimmed) {
            return true;
        }
        // Also handle single-char `&` at start of line
        if trimmed == "&" {
            return true;
        }
    }
    false
}

pub fn shell_split(command: &str, shell: &str, detect_risky_features: bool) -> ShellSplitResult {
    let mut findings = Vec::new();

    if shell != "posix" {
        return ShellSplitResult {
            parse_ok: false,
            argv: vec![],
            argc: 0,
            features: ShellFeatures::default(),
            findings: vec![format!(
                "Unsupported shell: {:?}. Only 'posix' is supported.",
                shell
            )],
        };
    }

    if command.trim().is_empty() {
        return ShellSplitResult {
            parse_ok: true,
            argv: vec![],
            argc: 0,
            features: ShellFeatures::default(),
            findings: vec!["Empty command".to_string()],
        };
    }

    let (argv, unbalanced, parse_error) = posix_shell_split(command);

    if let Some(err) = &parse_error {
        findings.push(format!("Parse error: {}", err));
    }

    let features = if detect_risky_features {
        detect_features(&argv, command, unbalanced)
    } else {
        ShellFeatures::default()
    };

    if detect_risky_features && !unbalanced {
        if features.has_pipe {
            findings.push("Contains pipe operator (|)".to_string());
        }
        if features.has_redirection {
            findings.push("Contains redirection operator (< or >)".to_string());
        }
        if features.has_command_substitution {
            findings.push("Contains command substitution ($( ) or backticks)".to_string());
        }
        if features.has_variable_expansion {
            findings.push("Contains variable expansion ($VAR or ${VAR})".to_string());
        }
        if features.has_glob_pattern {
            findings.push("Contains glob pattern characters (* ? [)".to_string());
        }
        if features.has_control_operator {
            findings.push("Contains control operator (; & && ||)".to_string());
        }
        if features.has_background {
            findings.push("Contains background execution operator (&)".to_string());
        }
    }

    ShellSplitResult {
        parse_ok: parse_error.is_none(),
        argv: argv.clone(),
        argc: argv.len(),
        features,
        findings,
    }
}

fn posix_shell_split(command: &str) -> (Vec<String>, bool, Option<String>) {
    let mut tokens: Vec<String> = Vec::new();
    let mut current = String::new();
    let mut in_quote: QuoteState = QuoteState::None;
    let mut token_started = false;

    let chars: Vec<char> = command.chars().collect();
    let len = chars.len();
    let mut i = 0;

    let flush_current =
        |tokens: &mut Vec<String>, current: &mut String, token_started: &mut bool| {
            if *token_started {
                tokens.push(std::mem::take(current));
                *token_started = false;
            }
        };

    while i < len {
        let c = chars[i];

        match in_quote {
            QuoteState::None => {
                if c.is_whitespace() {
                    flush_current(&mut tokens, &mut current, &mut token_started);
                    i += 1;
                    continue;
                }

                if c == '#' && !token_started {
                    break;
                }

                if c == '\'' {
                    in_quote = QuoteState::Single;
                    token_started = true;
                } else if c == '"' {
                    in_quote = QuoteState::Double;
                    token_started = true;
                } else if c == '\\' {
                    if i + 1 < len {
                        current.push(chars[i + 1]);
                        token_started = true;
                        i += 2;
                        continue;
                    } else {
                        return (tokens, true, Some("No escaped character".to_string()));
                    }
                } else {
                    current.push(c);
                    token_started = true;
                }
            }
            QuoteState::Single => {
                if c == '\'' {
                    in_quote = QuoteState::None;
                } else {
                    current.push(c);
                }
            }
            QuoteState::Double => {
                if c == '"' {
                    in_quote = QuoteState::None;
                } else if c == '\\' {
                    if i + 1 < len {
                        let next = chars[i + 1];
                        if next == '"' || next == '\\' {
                            current.push(next);
                            i += 2;
                            continue;
                        } else if next == '$' || next == '`' {
                            current.push('\\');
                            current.push(next);
                            token_started = true;
                            i += 2;
                            continue;
                        } else if next == '\n' {
                            token_started = true;
                            i += 2;
                            continue;
                        } else {
                            // POSIX: inside double quotes, backslash before
                            // non-special characters loses the backslash
                            // (e.g. "\a" -> "a"). Preserve only the char.
                            current.push(next);
                            token_started = true;
                            i += 2;
                            continue;
                        }
                    }
                    return (tokens, true, Some("No escaped character".to_string()));
                } else {
                    current.push(c);
                    token_started = true;
                }
            }
        }

        i += 1;
    }

    if in_quote != QuoteState::None {
        return (tokens, true, Some("No closing quotation".to_string()));
    }

    if token_started {
        tokens.push(current);
    }

    (tokens, false, None)
}

fn shell_quote(token: &str) -> String {
    if token.is_empty() {
        return "''".to_string();
    }

    let needs_quote = token.chars().any(|c| {
        c.is_whitespace()
            || c == '\''
            || c == '"'
            || c == '\\'
            || c == '$'
            || c == '`'
            || c == '|'
            || c == '<'
            || c == '>'
            || c == ';'
            || c == '&'
    });

    if !needs_quote {
        return token.to_string();
    }

    let mut result = String::new();
    result.push('\'');
    for c in token.chars() {
        if c == '\'' {
            result.push_str("'\"'\"'");
        } else {
            result.push(c);
        }
    }
    result.push('\'');
    result
}

pub fn shell_quote_join(argv: &[String], shell: &str) -> ShellQuoteJoinResult {
    let mut findings = Vec::new();

    if shell != "posix" {
        return ShellQuoteJoinResult {
            command: String::new(),
            roundtrip_ok: false,
            findings: vec![format!(
                "Unsupported shell: {:?}. Only 'posix' is supported.",
                shell
            )],
        };
    }

    let parts: Vec<String> = argv.iter().map(|t| shell_quote(t)).collect();
    let command = parts.join(" ");

    let result = shell_split(&command, "posix", false);
    let roundtrip_ok = if result.parse_ok && result.argv == argv {
        true
    } else {
        if result.parse_ok {
            findings.push(format!(
                "Round-trip mismatch: expected {:?}, got {:?}",
                argv, result.argv
            ));
        } else {
            findings.push("Round-trip parse failed".to_string());
        }
        false
    };

    ShellQuoteJoinResult {
        command,
        roundtrip_ok,
        findings,
    }
}

pub fn argv_compare(
    left_command: Option<&str>,
    right_command: Option<&str>,
    left_argv: Option<&[String]>,
    right_argv: Option<&[String]>,
    shell: &str,
) -> ArgvCompareResult {
    let mut findings = Vec::new();

    let mut resolved_left: Option<Vec<String>> = None;
    let mut resolved_right: Option<Vec<String>> = None;

    if let Some(cmd) = left_command {
        let split = shell_split(cmd, shell, false);
        if !split.parse_ok {
            return ArgvCompareResult {
                argv_equal: false,
                left_argv: vec![],
                right_argv: right_argv.map(|v| v.to_vec()).unwrap_or_default(),
                first_difference: Some(0),
                findings: vec![format!(
                    "Failed to parse left command: {:?}",
                    split.findings
                )],
            };
        }
        resolved_left = Some(split.argv.clone());
        if let Some(lv) = left_argv {
            if split.argv != lv {
                findings.push("Left command parse differs from provided left_argv".to_string());
            }
        }
    } else if let Some(lv) = left_argv {
        resolved_left = Some(lv.to_vec());
    }

    if let Some(cmd) = right_command {
        let split = shell_split(cmd, shell, false);
        if !split.parse_ok {
            return ArgvCompareResult {
                argv_equal: false,
                left_argv: resolved_left.clone().unwrap_or_default(),
                right_argv: vec![],
                first_difference: Some(0),
                findings: vec![format!(
                    "Failed to parse right command: {:?}",
                    split.findings
                )],
            };
        }
        resolved_right = Some(split.argv.clone());
        if let Some(rv) = right_argv {
            if split.argv != rv {
                findings.push("Right command parse differs from provided right_argv".to_string());
            }
        }
    } else if let Some(rv) = right_argv {
        resolved_right = Some(rv.to_vec());
    }

    let left = resolved_left.unwrap_or_default();
    let right = resolved_right.unwrap_or_default();

    let argv_equal = left == right;
    let mut first_diff: Option<usize> = None;

    if !argv_equal {
        for i in 0..std::cmp::min(left.len(), right.len()) {
            if left[i] != right[i] {
                first_diff = Some(i);
                findings.push(format!(
                    "First difference at index {}: '{}' != '{}'",
                    i, left[i], right[i]
                ));
                break;
            }
        }

        if first_diff.is_none() {
            first_diff = Some(std::cmp::min(left.len(), right.len()));
            if left.len() > right.len() {
                findings.push(format!(
                    "Left has {} extra tokens",
                    left.len() - right.len()
                ));
            } else {
                findings.push(format!(
                    "Right has {} extra tokens",
                    right.len() - left.len()
                ));
            }
        }
    }

    ArgvCompareResult {
        argv_equal,
        left_argv: left,
        right_argv: right,
        first_difference: first_diff,
        findings,
    }
}