Skip to content

Show it a video you love. Get a new video in the same style. An AI crew (Claude Code or Codex) plans, builds and reviews it with you.

12.1 KB
Raw Blame
// Reads an agent's shell command the way the shell will run it — enough for policy, not a shell. Splits a command line
// into simple commands (on ; && || | & newlines, subshells and $( ) / `` substitutions, whose insides run too), honours
// quotes and escapes, and tracks what changes between them: `cd` / `pushd` / `git -C` move the working directory,
// `export K=V` and `K=V cmd` set the environment, `$HOME` / `${VAR}` / `$env:VAR` / `%VAR%` expand from it, and
// `bash -c '…'` / `sh -c` / `env` / `nohup` / `sudo` / `timeout` / `npx` and friends are unwrapped.
// A regex over the raw string would miss `cd app && npm start` and `echo $(cat ~/.reelmimic/secrets.json)`.
import { homedir } from 'node:os';
import { resolve } from 'node:path';
import { LIST_VAR } from '../hooks/fast.mjs';

export interface Simple {
  argv: string[];
  env: Record<string, string>;        // inherited exports + this command's K=V prefixes
  redirects: string[];                // files written by > >> &> (not 2>&1 style fd copies)
  cwd: string;                        // where it runs
}
type Tok = { k: 'w'; v: string } | { k: 'op'; v: string };

// Launchers that run the rest of the line as the real program, with the options that take a value (`sudo -u user`,
// not `sudo -n`) and how many positionals they eat first (`timeout 600 node x`: the duration).
const LAUNCHERS: Record<string, { valued?: string[]; positional?: number }> = {
  sudo: { valued: ['-u', '-g', '-C', '-D', '-h', '-p', '-r', '-t', '-U', '-T'] }, nohup: {}, time: {}, command: {}, exec: { valued: ['-a'] },
  nice: { valued: ['-n'] }, stdbuf: { valued: ['-i', '-o', '-e'] }, timeout: { valued: ['-s', '-k', '--signal', '--kill-after'], positional: 1 },
  npx: { valued: ['-p', '--package'] }, pnpx: { valued: ['-p', '--package'] }, bunx: { valued: ['-p', '--package'] },
};
const PM_EXEC = new Set(['exec', 'dlx', 'x']);   // `npm exec tsx …`, `pnpm dlx …`, `bun x …`
const SHELLS = /^(?:.*[\\/])?(?:bash|sh|zsh|dash)(?:\.exe)?$/i;

export function lex(src: string): Tok[] {
  const out: Tok[] = []; let cur = '', word = false, i = 0;
  const end = () => { if (word) out.push({ k: 'w', v: cur }); cur = ''; word = false; };
  const op = (v: string) => { end(); out.push({ k: 'op', v }); };
  // Here-documents whose bodies start after the current line: `cat <<'EOF' > msg.txt && git commit -F msg.txt`.
  // A body is the command's stdin, not commands. Quoted delimiter: plain data. Unquoted: its $( ) and `…` still run.
  const heredocs: { delim: string; tabs: boolean; quoted: boolean }[] = [];
  const bodies = () => {
    for (const h of heredocs.splice(0)) {
      for (;;) {
        const e = src.indexOf('\n', i), line = src.slice(i, e < 0 ? src.length : e).replace(/\r$/, '');
        i = e < 0 ? src.length : e + 1;
        if ((h.tabs ? line.replace(/^\t+/, '') : line) === h.delim || e < 0) break;
        for (let j = 0; !h.quoted && j < line.length; j++) {
          let inner: string | null = null;
          if (line[j] === '$' && line[j + 1] === '(') { const e = closeParen(line, j + 1); inner = line.slice(j + 2, e); j = e; }
          else if (line[j] === '`') { const e = line.indexOf('`', j + 1), k = e < 0 ? line.length : e; inner = line.slice(j + 1, k); j = k; }
          if (inner != null) out.push({ k: 'op', v: ';' }, ...lex(inner), { k: 'op', v: ';' });
        }
      }
    }
  };
  while (i < src.length) {
    const c = src[i], n = src[i + 1] ?? '';
    if (c === '\\') { if (n === '\n') { i += 2; continue; } cur += n; word = true; i += 2; continue; }
    if (c === "'") { const j = src.indexOf("'", i + 1), e = j < 0 ? src.length : j; cur += src.slice(i + 1, e); word = true; i = e + 1; continue; }
    if (c === '"') {
      // A $( ) inside double quotes still runs: hand it to the caller as its own command, keep the rest as the word.
      word = true; i++;
      while (i < src.length && src[i] !== '"') {
        if (src[i] === '\\' && '"\\$`'.includes(src[i + 1] ?? '')) { cur += src[i + 1]; i += 2; }
        else if (src[i] === '$' && src[i + 1] === '(') { const j = closeParen(src, i + 1); out.push({ k: 'op', v: '(' }, ...lex(src.slice(i + 2, j)), { k: 'op', v: ')' }); i = j + 1; }
        else if (src[i] === '`') { const j = src.indexOf('`', i + 1), e = j < 0 ? src.length : j; out.push({ k: 'op', v: '(' }, ...lex(src.slice(i + 1, e)), { k: 'op', v: ')' }); i = e + 1; }
        else cur += src[i++];
      }
      i++; continue;
    }
    if (c === '#' && !word) { while (i < src.length && src[i] !== '\n') i++; continue; }
    if (c === ' ' || c === '\t' || c === '\r') { end(); i++; continue; }
    if (c === '\n') { op(';'); i++; bodies(); continue; }
    if (c === '<' && n === '<' && src[i + 2] === '<') { op('<'); i += 3; continue; }   // here-string: the word after it is data
    if (c === '<' && n === '<') {
      end(); i += 2;
      const tabs = src[i] === '-'; if (tabs) i++;
      while (src[i] === ' ' || src[i] === '\t') i++;
      const m = /^(?:'([^']*)'|"([^"]*)"|((?:\\.|[^\s;&|<>()])+))/.exec(src.slice(i)) ?? ['', '', '', ''];
      const raw = m[3] ?? '';
      heredocs.push({ delim: m[1] ?? m[2] ?? raw.replace(/\\(.)/g, '$1'), tabs, quoted: m[1] != null || m[2] != null || raw.includes('\\') });
      i += m[0].length; continue;
    }
    if (c === '$' && n === '(') { const j = closeParen(src, i + 1); out.push({ k: 'op', v: '(' }, ...lex(src.slice(i + 2, j)), { k: 'op', v: ')' }); i = j + 1; continue; }
    if (c === '`') { const j = src.indexOf('`', i + 1), e = j < 0 ? src.length : j; out.push({ k: 'op', v: '(' }, ...lex(src.slice(i + 1, e)), { k: 'op', v: ')' }); i = e + 1; continue; }
    if (c === '&' && n === '&') { op('&&'); i += 2; continue; }
    if (c === '|' && n === '|') { op('||'); i += 2; continue; }
    if (c === '&' && n === '>') { op(src[i + 2] === '>' ? '>>' : '>'); i += src[i + 2] === '>' ? 3 : 2; continue; }
    if (c === '>' || c === '<') {
      if (word && /^\d+$/.test(cur)) { cur = ''; word = false; }            // 2> file: the fd number is not a word
      if (c === '>' && n === '&') { end(); i += 2; while (/[\d-]/.test(src[i] ?? '')) i++; continue; }   // 2>&1: no file
      const v = c === '>' && n === '>' ? '>>' : c === '<' && n === '<' ? '<<' : c;
      op(v); i += v.length; continue;
    }
    if (';|&()'.includes(c)) { op(c); i++; continue; }
    cur += c; word = true; i++;
  }
  end();
  return out;
}

function closeParen(s: string, open: number) {
  for (let d = 0, i = open; i < s.length; i++) { if (s[i] === '(') d++; else if (s[i] === ')' && --d === 0) return i; }
  return s.length;
}

/** Every simple command in `src`, in the order they run, with the cwd and environment each one sees. */
export function parse(src: string, cwd: string, env: Record<string, string> = {}, depth = 0): Simple[] {
  const out: Simple[] = []; let words: string[] = [], redirects: string[] = [], pending = '';
  const state = { cwd, env: { ...env } };
  // ( … ) and $( … ) run on their own: park the enclosing command's words, parse the inside, then pick them back up.
  // The inside's `cd` does not leak out (a subshell has its own cwd).
  const stack: { words: string[]; redirects: string[]; pending: string; cwd: string }[] = [];
  const flush = () => {
    if (words.length || redirects.length) out.push(...expand(words, redirects, state, depth));
    words = []; redirects = []; pending = '';
  };
  for (const t of lex(src)) {
    if (t.k === 'op' && (t.v === '>' || t.v === '>>' || t.v === '<' || t.v === '<<')) { pending = t.v; continue; }
    if (t.k === 'op' && t.v === '(') { stack.push({ words, redirects, pending, cwd: state.cwd }); words = []; redirects = []; pending = ''; continue; }
    if (t.k === 'op' && t.v === ')') { flush(); const s = stack.pop(); if (s) { ({ words, redirects, pending } = s); state.cwd = s.cwd; } continue; }
    if (t.k === 'op') { flush(); continue; }
    if (pending) { if (pending === '>' || pending === '>>') redirects.push(resolvePath(expandVars(t.v, state.env), state.cwd)); pending = ''; continue; }
    words.push(t.v);
  }
  flush();
  return out;
}

function expand(words: string[], redirects: string[], state: { cwd: string; env: Record<string, string> }, depth: number): Simple[] {
  const local: Record<string, string> = {};
  // Expanded with the environment as it was before this command: in `HOME=/x cat $HOME/f` the shell reads the old HOME.
  words = words.map((w) => expandVars(w, state.env));
  let i = 0;
  for (; i < words.length && /^[A-Za-z_]\w*=/.test(words[i]); i++) { const [k, ...v] = words[i].split('='); local[k] = v.join('='); }
  let argv = words.slice(i);
  // Unwrap launchers so the policy sees the real program: `env -i K=V node x`, `nohup node x &`, `sudo -u u cmd`,
  // `timeout 600 node x`, `npx tsx x`, `pnpm exec tsx x`.
  for (;;) {
    const a0 = base(argv[0] ?? ''), l = LAUNCHERS[a0];
    if (a0 === 'env') { argv = argv.slice(1); while (argv[0] && (argv[0].startsWith('-') || argv[0].includes('='))) { const w = argv.shift()!; if (w.includes('=') && !w.startsWith('-')) { const [k, ...v] = w.split('='); local[k] = v.join('='); } } continue; }
    if (['npm', 'pnpm', 'yarn', 'bun'].includes(a0) && PM_EXEC.has(argv[1] ?? '')) { argv = argv.slice(2); while (argv[0]?.startsWith('-')) argv = argv.slice(argv[0] === '-p' || argv[0] === '--package' ? 2 : 1); continue; }
    if (l) {
      argv = argv.slice(1);
      while (argv[0]?.startsWith('-')) { const o = argv.shift()!; if (o === '--') break; if (l.valued?.includes(o)) argv.shift(); }
      argv = argv.slice(l.positional ?? 0);
      continue;
    }
    break;
  }
  const env = { ...state.env, ...local };
  if (!argv.length) { Object.assign(state.env, local); return redirects.length ? [{ argv: [], env, redirects, cwd: state.cwd }] : []; }
  const a0 = base(argv[0]);
  if ((a0 === 'cd' || a0 === 'pushd' || a0 === 'set-location' || a0 === 'sl') && argv.length <= 2) {
    const to = argv[1] && argv[1] !== '-' ? argv[1] : argv[1] ? state.cwd : homedir();
    state.cwd = resolvePath(to, state.cwd);
    return [];
  }
  if (a0 === 'export') { for (const w of argv.slice(1)) if (w.includes('=')) { const [k, ...v] = w.split('='); state.env[k] = v.join('='); } return []; }
  const self: Simple = { argv, env, redirects, cwd: state.cwd };
  if (depth < 4 && SHELLS.test(argv[0])) {
    const c = argv.indexOf('-c');
    if (c > 0 && argv[c + 1] != null) return [self, ...parse(argv[c + 1], state.cwd, env, depth + 1)];
  }
  return [self];
}

/** `$HOME`, `${VAR}`, PowerShell `$env:VAR` and cmd `%VAR%`, from the command's own exports, then the hook's
 *  environment (which the agent's shell inherits). Unknown names stay as written, and so do search lists (`$PATH`):
 *  they are never one file, and hooks/fast.mjs skips them the same way. Single quotes would stop bash expanding; the
 *  lexer has dropped them by now, so this expands a little more than bash would — the safe side. */
export function expandVars(w: string, env: Record<string, string> = {}): string {
  if (!w.includes('$') && !w.includes('%')) return w;
  return w.replace(/\$\{([A-Za-z_]\w*)\}|\$env:([A-Za-z_]\w*)|\$([A-Za-z_]\w*)|%([A-Za-z_]\w*)%/gi, (m, a, b, c, d) => {
    const k = a ?? b ?? c ?? d;
    if (LIST_VAR.test(k)) return m;
    return env[k] ?? process.env[k] ?? (/^(?:HOME|USERPROFILE)$/i.test(k) ? homedir() : m);
  });
}

/** Lower-case program name without directory or .exe/.cmd, so `C:\…\node.exe` and `node` compare equal. */
export const base = (p: string) => p.replace(/^.*[\\/]/, '').replace(/\.(exe|cmd|bat|ps1)$/i, '').toLowerCase();

/** An absolute, forward-slash path: `~` → home, Git Bash `/c/Users` → `C:/Users`, relative → against cwd. */
export function resolvePath(p: string, cwd: string): string {
  let s = p.replace(/^~(?=$|[\\/])/, homedir().replace(/\\/g, '/'));
  if (process.platform === 'win32') s = s.replace(/^\/([a-zA-Z])(?=\/|$)/, '$1:');
  return resolve(cwd, s).replace(/\\/g, '/').replace(/^[a-z](?=:)/, (d) => d.toUpperCase());   // Claude Code reports c:\…, Node C:\…
}

const norm = (p: string) => (process.platform === 'win32' ? p.toLowerCase() : p).replace(/\/+$/, '');
export const samePath = (a: string, b: string) => norm(a) === norm(b);
/** True when `child` is `parent` or anything below it. */
export const within = (child: string, parent: string) => { const c = norm(child), p = norm(parent); return c === p || c.startsWith(p + '/'); };