agshell 0.5.4

A supervised execution shell for coding agents: per-command and per-stage exit status instead of one exit code per command string.
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
//! A deliberately small scanner over shell source.
//!
//! It answers two questions the supervisor needs before it runs a command:
//! where the command substitutions are, and whether an expansion is unquoted.
//! It is not a shell lexer: it tracks quotes and escapes, which is enough to
//! locate `$( )` and backticks. Statements containing heredocs are excluded by
//! the caller, because a heredoc body would need real parsing.

/// One command substitution, located in the source by character index.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Substitution {
    /// Character index of the `$(` or backtick.
    pub start: usize,
    /// Character index just past the closing `)` or backtick.
    pub end: usize,
    /// The inner script, without the delimiters.
    pub inner: String,
    /// Whether the substitution sits inside a double-quoted region.
    pub quoted: bool,
}

/// Finds `$( ... )` and `` `...` `` substitutions, skipping single-quoted text.
pub fn substitutions(src: &str) -> Vec<Substitution> {
    let chars: Vec<char> = src.chars().collect();
    let mut found = Vec::new();
    let mut i = 0;
    let mut in_single = false;
    let mut in_double = false;

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

        if in_single {
            if c == '\'' {
                in_single = false;
            }
            i += 1;
            continue;
        }

        match c {
            '\\' => {
                i += 2;
                continue;
            }
            '\'' if !in_double => {
                in_single = true;
                i += 1;
                continue;
            }
            '"' => {
                in_double = !in_double;
                i += 1;
                continue;
            }
            '`' => {
                if let Some(end) = find_backtick_end(&chars, i + 1) {
                    let inner: String = chars[i + 1..end].iter().collect();
                    found.push(Substitution {
                        start: i,
                        end: end + 1,
                        inner,
                        quoted: in_double,
                    });
                    i = end + 1;
                    continue;
                }
                i += 1;
                continue;
            }
            '$' if i + 1 < chars.len() && chars[i + 1] == '(' => {
                // `$((` is arithmetic expansion, not a command substitution.
                if i + 2 < chars.len() && chars[i + 2] == '(' {
                    i += 3;
                    continue;
                }
                if let Some(end) = find_paren_end(&chars, i + 2) {
                    let inner: String = chars[i + 2..end].iter().collect();
                    found.push(Substitution {
                        start: i,
                        end: end + 1,
                        inner,
                        quoted: in_double,
                    });
                    i = end + 1;
                    continue;
                }
                i += 1;
                continue;
            }
            _ => {}
        }

        i += 1;
    }

    found
}

fn find_backtick_end(chars: &[char], from: usize) -> Option<usize> {
    let mut i = from;
    while i < chars.len() {
        match chars[i] {
            '\\' => i += 2,
            '`' => return Some(i),
            _ => i += 1,
        }
    }
    None
}

fn find_paren_end(chars: &[char], from: usize) -> Option<usize> {
    let mut depth = 1usize;
    let mut i = from;
    let mut in_single = false;
    let mut in_double = false;

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

        if in_single {
            if c == '\'' {
                in_single = false;
            }
            i += 1;
            continue;
        }

        match c {
            '\\' => {
                i += 2;
                continue;
            }
            '\'' if !in_double => in_single = true,
            '"' => in_double = !in_double,
            '(' if !in_double => depth += 1,
            ')' if !in_double => {
                depth -= 1;
                if depth == 0 {
                    return Some(i);
                }
            }
            _ => {}
        }
        i += 1;
    }
    None
}

/// Another interpreter's script, handed to it as a quoted word.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ForeignScript {
    /// The interpreter the script was meant for: `pwsh` or `powershell`.
    pub interpreter: String,
    /// The quoted argument, quotes included, exactly as written.
    pub word: String,
}

/// Programs that run a script handed to them as text, so a `$…` inside that text
/// belongs to *them*, not to the shell that is about to expand it.
const SCRIPT_INTERPRETERS: &[&str] = &["pwsh", "powershell"];

/// Finds `pwsh … -Command "… $… …"`.
///
/// bash expands `$…` and backticks inside double quotes before the interpreter
/// ever sees the script, so PowerShell's own `$_`, `$env:X` and `$( )` arrive
/// mangled or empty — as a `ParserError` pointing at nonsense. The fix is to
/// single-quote the argument.
#[must_use]
pub fn foreign_script_expansion(src: &str) -> Option<ForeignScript> {
    let mut words = split_words(src);
    while words
        .first()
        .is_some_and(|w| matches!(w.as_str(), "&" | "command") || is_assignment(w))
    {
        words.remove(0);
    }
    let interpreter = script_interpreter(words.first()?)?;

    for (i, word) in words.iter().enumerate().skip(1) {
        let flag = word.trim_start_matches(['-', '/']).to_ascii_lowercase();
        if !matches!(flag.as_str(), "command" | "c" | "commandwithargs") {
            continue;
        }
        let Some(argument) = words.get(i + 1) else {
            continue;
        };
        if argument.starts_with('"') && (argument.contains('$') || argument.contains('`')) {
            return Some(ForeignScript {
                interpreter,
                word: argument.clone(),
            });
        }
    }

    None
}

/// `NAME=value` in front of the command, as in `NO_COLOR=1 pwsh …`.
fn is_assignment(word: &str) -> bool {
    let Some((name, _)) = word.split_once('=') else {
        return false;
    };
    !name.is_empty()
        && name
            .chars()
            .next()
            .is_some_and(|c| c.is_ascii_alphabetic() || c == '_')
        && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
}

/// The interpreter's bare name, or `None` if this word is not one of ours.
fn script_interpreter(word: &str) -> Option<String> {
    let name = word
        .rsplit(['/', '\\'])
        .next()
        .unwrap_or(word)
        .trim_matches('"')
        .to_ascii_lowercase();
    let name = name.strip_suffix(".exe").unwrap_or(name.as_str());
    SCRIPT_INTERPRETERS
        .contains(&name)
        .then(|| name.to_string())
}

/// Splits source into words at unquoted whitespace, keeping the quotes in the
/// word text. Only word boundaries matter here, not expansion.
fn split_words(src: &str) -> Vec<String> {
    let chars: Vec<char> = src.chars().collect();
    let mut words = Vec::new();
    let mut i = 0;
    let mut in_single = false;
    let mut in_double = false;

    while i < chars.len() {
        while i < chars.len() && chars[i].is_whitespace() && !in_single && !in_double {
            i += 1;
        }
        if i >= chars.len() {
            break;
        }

        let start = i;
        while i < chars.len() {
            let c = chars[i];
            if in_single {
                if c == '\'' {
                    in_single = false;
                }
                i += 1;
                continue;
            }
            match c {
                '\\' => i = (i + 2).min(chars.len()),
                '\'' if !in_double => {
                    in_single = true;
                    i += 1;
                }
                '"' => {
                    in_double = !in_double;
                    i += 1;
                }
                c if c.is_whitespace() && !in_double => break,
                _ => i += 1,
            }
        }

        words.push(chars[start..i].iter().collect());
        if i == start {
            // Never stall on a character the scan above refused to advance past.
            i += 1;
        }
    }

    words
}

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

    #[test]
    fn finds_a_substitution() {
        let subs = substitutions("echo $(date +%s) done");
        assert_eq!(subs.len(), 1);
        assert_eq!(subs[0].inner, "date +%s");
        assert!(!subs[0].quoted);
    }

    #[test]
    fn ignores_single_quotes_and_arithmetic() {
        assert!(substitutions("echo '$(date)'").is_empty());
        assert!(substitutions("echo $((1 + 2))").is_empty());
    }

    #[test]
    fn handles_nesting_and_quotes() {
        let subs = substitutions(r#"echo "$(echo "$(date)")" x"#);
        assert_eq!(subs.len(), 1);
        assert_eq!(subs[0].inner, r#"echo "$(date)""#);
        assert!(subs[0].quoted);
    }

    #[test]
    fn finds_backticks() {
        let subs = substitutions("echo `date`");
        assert_eq!(subs.len(), 1);
        assert_eq!(subs[0].inner, "date");
    }

    #[test]
    fn finds_a_script_bash_would_expand_first() {
        let hit = foreign_script_expansion(
            r#"pwsh -NoProfile -Command "Get-CimInstance X | Select-Object @{n='GB';e={$_.Capacity/1GB}}""#,
        )
        .expect("a hit");
        assert_eq!(hit.interpreter, "pwsh");
        assert!(hit.word.contains("$_"));
    }

    #[test]
    fn accepts_single_quoted_and_file_scripts() {
        assert!(foreign_script_expansion(r#"pwsh -Command 'Get-Item $_.Name'"#).is_none());
        assert!(foreign_script_expansion("pwsh -NoProfile -File build.ps1").is_none());
        assert!(foreign_script_expansion(r#"powershell.exe -Command "echo plain""#).is_none());
    }

    #[test]
    fn ignores_other_programs_and_mentions() {
        assert!(foreign_script_expansion(r#"node -e "console.log($x)""#).is_none());
        assert!(foreign_script_expansion(r#"echo pwsh -Command "$x""#).is_none());
    }

    /// One table of the shapes people actually type, so a change to the scanner
    /// has to keep all of them right at once.
    #[test]
    fn the_shape_table() {
        let cases: &[(&str, bool)] = &[
            // every spelling of the interpreter
            (r#"pwsh -Command "$env:PATH""#, true),
            (r#"pwsh.exe -Command "$env:PATH""#, true),
            (r#"powershell -Command "$env:PATH""#, true),
            (r#"powershell.exe -NoProfile -Command "$env:PATH""#, true),
            (r#""C:/Program Files/PowerShell/7/pwsh.exe" -Command "$env:PATH""#, true),
            (r#"& "C:/Program Files/PowerShell/7/pwsh.exe" -Command "$env:PATH""#, true),
            (r#"command pwsh -Command "$env:PATH""#, true),
            (r#"NO_COLOR=1 pwsh -Command "$env:PATH""#, true),
            // the short flag, whatever its case
            (r#"pwsh -c "Get-Item $_.Name""#, true),
            (r#"pwsh -CommandWithArgs "Get-Item $_.Name" x"#, true),
            // backticks are expanded by this shell too
            ("pwsh -Command \"`git rev-parse HEAD`\"", true),
            // the fix: single quotes, and no expansion to find
            (r#"pwsh -Command 'Get-Item $_.Name'"#, false),
            (r#"pwsh -NoProfile -Command "Get-Item .\file.txt""#, false),
            (r#"pwsh -NoProfile -File build.ps1"#, false),
            (r#"pwsh -NoProfile -EncodedCommand RQBjAGgAbwA="#, false),
            // not ours to warn about
            (r#"cmd /c "echo %PATH%""#, false),
            (r#"node -e "console.log($x)""#, false),
            (r#"pwsh --version"#, false),
            (r#"echo 'pwsh -Command "$x"'"#, false),
        ];

        for (source, expected) in cases {
            let hit = foreign_script_expansion(source);
            assert_eq!(
                hit.is_some(),
                *expected,
                "{source:?} → {hit:?} (expected {expected})"
            );
            if let Some(hit) = hit {
                assert!(
                    source.contains(&hit.word),
                    "{source:?}: the matched word is not in the source: {hit:?}"
                );
            }
        }
    }

    /// A deterministic mini-fuzz: no dependency, same corpus every run, and it
    /// terminates. The first version of [`split_words`] spun forever on a space
    /// inside double quotes while allocating 24 GB; this is the guard that a
    /// random pile of quotes and `$` can never do that again.
    #[test]
    fn fuzz_the_scanner_survives_random_junk() {
        const TOKENS: &[&str] = &[
            "pwsh", "powershell.exe", "-Command", "-c", "-File", "-EncodedCommand", "\"", "'", "$",
            "$_", "$env:X", "`", "(", ")", "{", "}", "@", " ", "  ", "\n", "\\", "|", "&&", ";",
            "Get-Item", "echo", "&", "command", "X=1", "-NoProfile", "a", "=", "'", "\"",
        ];

        let rounds: usize = std::env::var("AGSHELL_FUZZ_ROUNDS")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(4_000);

        let mut state: u64 = 0x5f37_5b4d_2a19_c7e1;
        let mut next = move || {
            state ^= state << 13;
            state ^= state >> 7;
            state ^= state << 17;
            state
        };

        for round in 0..rounds {
            let len = (next() % 14) as usize;
            let mut source = String::new();
            for _ in 0..len {
                source.push_str(TOKENS[(next() % TOKENS.len() as u64) as usize]);
            }

            let first = foreign_script_expansion(&source);
            assert_eq!(
                first,
                foreign_script_expansion(&source),
                "round {round}: not deterministic for {source:?}"
            );

            if let Some(hit) = first {
                assert!(
                    hit.word.starts_with('"'),
                    "round {round}: a match that is not a double-quoted argument: {source:?} → {hit:?}"
                );
                assert!(
                    hit.word.contains('$') || hit.word.contains('`'),
                    "round {round}: a match with nothing to expand: {source:?} → {hit:?}"
                );
                assert!(
                    source.contains(&hit.word),
                    "round {round}: matched text is not in the source: {source:?} → {hit:?}"
                );
                assert!(
                    hit.interpreter == "pwsh" || hit.interpreter == "powershell",
                    "round {round}: {hit:?}"
                );
            }
        }
    }
}