/** * Codex-aligned bash parser. * * Hand-written TypeScript reproduction of the accept/reject set implemented by * OpenAI Codex's tree-sitter-bash parser in `shell-command/src/bash.rs`. We do * NOT pull in tree-sitter; instead we re-derive the same decision boundary with * a quote-aware scanner. The goal is behavioural parity with Codex on the set * of scripts that classify as "word-only command sequences" and on the heredoc * single-command-prefix fallback. * * Codex parity points (see bash.rs): * - A script is a "plain command sequence" iff every command is built only * from bare words / quoted strings / numbers / quote-concatenations, joined * ONLY by the side-effect-free operators `&&`, `||`, `;`, `|`. * - Any of the following make the whole script NON-plain (returns undefined): * parentheses / subshells, redirections (`>` `<` `>>` `2>` …), * background `&`, command substitution `$( … )` / backticks, * parameter expansion `$VAR` / `${VAR}` (even inside double quotes), * variable-assignment prefix (`FOO=bar cmd`), empty command positions * (leading / trailing / doubled operators), and parse errors. * - Double-quoted strings keep their literal content (Codex rejects them only * when they contain an expansion); single quotes are fully literal. * * Unlike Codex's AST walk we cannot rely on a grammar, so the scanner is * deliberately conservative: anything it is unsure about is rejected (returns * undefined), matching Codex's "if we cannot prove it word-only, it is not * auto-safe" stance. */ export type ShellName = 'bash' | 'zsh' | 'sh'; /** Shells whose `-lc` / `-c` script arg we are willing to parse. */ const KNOWN_SHELLS = new Set(['bash', 'zsh', 'sh']); /** * Extract the `(shell, script)` pair from a `bash -lc "