UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

82 lines (81 loc) • 10.3 kB
import { declaresOption, programRows } from "../tools/fs/program-read-model.js"; export const SHELL_WRAPPER_TABLE = { command: { valueFlags: [], flags: ["-p"], commandString: [] }, builtin: { valueFlags: [], flags: [], commandString: [] }, noglob: { valueFlags: [], flags: [], commandString: [] }, nocorrect: { valueFlags: [], flags: [], commandString: [] }, exec: { valueFlags: ["-a"], flags: ["-c", "-l"], commandString: [] }, env: { valueFlags: ["--argv0", "--chdir", "--unset", "-C", "-P", "-a", "-u"], flags: ["--debug", "--default-signal", "--help", "--ignore-environment", "--ignore-signal", "--list-signal-handling", "--version", "-i", "-v"], commandString: ["--split-string", "-S"], bareDash: true, assignments: true }, sudo: { valueFlags: ["--auth-type", "--chdir", "--chroot", "--close-from", "--command-timeout", "--group", "--host", "--other-user", "--prompt", "--role", "--type", "--user", "-C", "-D", "-R", "-T", "-U", "-a", "-g", "-h", "-p", "-r", "-t", "-u"], flags: ["--askpass", "--background", "--bell", "--help", "--list", "--login", "--non-interactive", "--preserve-env", "--preserve-groups", "--remove-timestamp", "--reset-timestamp", "--set-home", "--shell", "--stdin", "--validate", "--version", "-A", "-E", "-H", "-K", "-P", "-S", "-V", "-b", "-i", "-k", "-l", "-n", "-s", "-v"], commandString: [], assignments: true }, doas: { valueFlags: ["-C", "-a", "-u"], flags: ["-L", "-n", "-s"], commandString: [] }, pkexec: { valueFlags: ["--user"], flags: ["--disable-internal-agent", "--help", "--keep-cwd", "--version"], commandString: [] }, nohup: { valueFlags: [], flags: ["--help", "--version"], commandString: [] }, time: { valueFlags: ["--format", "--output", "-f", "-o"], flags: ["--append", "--help", "--portability", "--quiet", "--verbose", "--version", "-a", "-p", "-q", "-v"], commandString: [] }, nice: { valueFlags: ["--adjustment", "-n"], flags: ["--help", "--version"], commandString: [] }, timeout: { valueFlags: ["--kill-after", "--signal", "-k", "-s"], flags: ["--foreground", "--help", "--preserve-status", "--verbose", "--version", "-v"], commandString: [], duration: true }, stdbuf: { valueFlags: ["--error", "--input", "--output", "-e", "-i", "-o"], flags: ["--help", "--version"], commandString: [] }, xargs: { valueFlags: ["--arg-file", "--delimiter", "--max-args", "--max-chars", "--max-procs", "--process-slot-var", "-E", "-I", "-L", "-P", "-a", "-d", "-n", "-s"], flags: ["--eof", "--exit", "--help", "--interactive", "--max-lines", "--no-run-if-empty", "--null", "--open-tty", "--replace", "--show-limits", "--verbose", "--version", "-0", "-o", "-p", "-r", "-t", "-x"], attached: ["-i", "-l"], commandString: [] }, watch: { valueFlags: ["--equexit", "--interval", "--shotsdir", "-n", "-q", "-s"], flags: ["--beep", "--chgexit", "--color", "--differences", "--errexit", "--exec", "--follow", "--help", "--no-color", "--no-rerun", "--no-title", "--no-wrap", "--precise", "--version", "-C", "-b", "-c", "-e", "-f", "-g", "-h", "-p", "-r", "-t", "-v", "-w", "-x"], commandString: [], viaShell: true }, ionice: { valueFlags: ["--class", "--classdata", "--pgid", "--pid", "--uid", "-P", "-c", "-n", "-p", "-u"], flags: ["--help", "--ignore", "--version", "-V", "-h", "-t"], commandString: [] }, setsid: { valueFlags: [], flags: ["--ctty", "--fork", "--help", "--version", "--wait", "-c", "-f", "-w"], commandString: [] }, taskset: { valueFlags: [], flags: ["--all-tasks", "--cpu-list", "--help", "--pid", "--version", "-a", "-c", "-p"], commandString: [], positional: /^(?:0x)?[\da-f]+$|^[\d,:-]+$/i }, chrt: { valueFlags: ["--pid", "--sched-deadline", "--sched-period", "--sched-runtime", "-D", "-P", "-T", "-p"], flags: ["--all-tasks", "--batch", "--deadline", "--fifo", "--help", "--idle", "--max", "--other", "--reset-on-fork", "--rr", "--verbose", "--version", "-R", "-a", "-b", "-d", "-f", "-i", "-m", "-o", "-r", "-v"], commandString: [], positional: /^\d+$/ }, strace: { valueFlags: ["--attach", "--columns", "--const-print-style", "--detach-on", "--env", "--expr", "--interruptible", "--output", "--string-limit", "--summary-columns", "--summary-sort-by", "--summary-syscall-overhead", "--trace", "--trace-path", "--user", "-E", "-I", "-O", "-P", "-S", "-U", "-X", "-a", "-b", "-e", "-o", "-p", "-s", "-u"], flags: ["--debug", "--follow-forks", "--help", "--summary", "--summary-only", "--version", "-C", "-D", "-F", "-T", "-V", "-Z", "-c", "-d", "-f", "-h", "-i", "-k", "-q", "-r", "-t", "-v", "-w", "-x", "-y", "-z"], commandString: [] }, ltrace: { valueFlags: ["--align", "--config", "--debug", "--indent", "--library", "--output", "--string-max", "--where", "-A", "-D", "-F", "-a", "-e", "-l", "-n", "-o", "-p", "-s", "-u", "-w", "-x"], flags: ["--demangle", "--help", "--version", "-C", "-L", "-S", "-T", "-V", "-c", "-f", "-i", "-r", "-t"], commandString: [] }, flock: { valueFlags: ["--conflict-exit-code", "--timeout", "--wait", "-E", "-w"], flags: ["--close", "--exclusive", "--help", "--nb", "--no-fork", "--nonblock", "--shared", "--unlock", "--verbose", "--version", "-F", "-n", "-o", "-s", "-u", "-x"], commandString: ["--command", "-c"], positional: /^/ }, script: { valueFlags: [], flags: [], commandString: ["--command", "-c"], viaShell: true }, unshare: { valueFlags: ["--boottime", "--map-group", "--map-user", "--monotonic", "--propagation", "--root", "--setgid", "--setgroups", "--setuid", "--wd", "-G", "-R", "-S", "-w"], flags: ["--cgroup", "--fork", "--help", "--ipc", "--keep-caps", "--kill-child", "--map-current-user", "--map-root-user", "--mount", "--mount-proc", "--net", "--pid", "--time", "--user", "--uts", "--version", "-C", "-T", "-U", "-c", "-f", "-i", "-m", "-n", "-p", "-r", "-u"], commandString: [] }, nsenter: { valueFlags: ["--setgid", "--setuid", "--target", "-G", "-S", "-t"], flags: ["--all", "--cgroup", "--help", "--ipc", "--mount", "--net", "--no-fork", "--pid", "--preserve-credentials", "--root", "--time", "--user", "--uts", "--version", "--wd", "-C", "-F", "-T", "-U", "-Z", "-a", "-i", "-m", "-n", "-p", "-u"], attached: ["-r", "-w"], commandString: [] }, eval: { valueFlags: [], flags: [], commandString: [], viaShell: true }, source: { valueFlags: [], flags: [], commandString: [], viaShell: true }, ".": { valueFlags: [], flags: [], commandString: [], viaShell: true }, trap: { valueFlags: [], flags: [], commandString: [], viaShell: true }, let: { valueFlags: [], flags: [], commandString: [], viaShell: true }, sh: { valueFlags: [], flags: [], commandString: [], viaShell: true }, bash: { valueFlags: [], flags: [], commandString: [], viaShell: true }, zsh: { valueFlags: [], flags: [], commandString: [], viaShell: true }, dash: { valueFlags: [], flags: [], commandString: [], viaShell: true }, ksh: { valueFlags: [], flags: [], commandString: [], viaShell: true }, fish: { valueFlags: [], flags: [], commandString: [], viaShell: true }, csh: { valueFlags: [], flags: [], commandString: [], viaShell: true }, tcsh: { valueFlags: [], flags: [], commandString: [], viaShell: true }, find: { valueFlags: [], flags: [], commandString: [], executes: ["-exec", "-execdir", "-ok", "-okdir"] }, compgen: { valueFlags: [], flags: [], commandString: [], executes: ["-C", "-F"] }, complete: { valueFlags: [], flags: [], commandString: [], executes: ["-C", "-F"] }, busybox: { valueFlags: [], flags: [], commandString: [] }, su: { valueFlags: ["--group", "--shell", "--supp-group", "--whitelist-environment", "-G", "-g", "-s", "-w"], flags: ["--fast", "--help", "--login", "--no-pty", "--preserve-environment", "--pty", "--version", "-P", "-T", "-V", "-f", "-h", "-l", "-m", "-p"], commandString: ["--command", "--session-command", "-c"], viaShell: true }, runuser: { valueFlags: ["--group", "--shell", "--supp-group", "--user", "--whitelist-environment", "-G", "-g", "-s", "-u", "-w"], flags: ["--fast", "--help", "--login", "--no-pty", "--preserve-environment", "--pty", "--version", "-P", "-T", "-V", "-f", "-h", "-l", "-m", "-p"], commandString: ["--command", "--session-command", "-c"], viaShell: true }, chroot: { valueFlags: [], flags: [], commandString: [], positional: /^/ }, setpriv: { valueFlags: [], flags: [], commandString: [] }, numactl: { valueFlags: [], flags: [], commandString: [] }, prlimit: { valueFlags: [], flags: [], commandString: [] }, "systemd-run": { valueFlags: [], flags: [], commandString: [] }, valgrind: { valueFlags: [], flags: [], commandString: [] }, firejail: { valueFlags: [], flags: [], commandString: [] }, bwrap: { valueFlags: [], flags: [], commandString: [] }, ash: { valueFlags: [], flags: [], commandString: [], viaShell: true }, mksh: { valueFlags: [], flags: [], commandString: [], viaShell: true }, yash: { valueFlags: [], flags: [], commandString: [], viaShell: true }, pdksh: { valueFlags: [], flags: [], commandString: [], viaShell: true }, parallel: { valueFlags: [], flags: [], commandString: [], viaShell: true }, alias: { valueFlags: [], flags: [], commandString: [], viaShell: true }, mapfile: { valueFlags: [], flags: [], commandString: [], executes: ["-C"] }, readarray: { valueFlags: [], flags: [], commandString: [], executes: ["-C"] }, }; export const VARIABLE_OPERAND_BUILTINS = new Set(programRows().filter((row) => row.operands?.rest?.kind === "binds-name" || declaresOption(row, (spec) => spec.value?.kind === "binds-name")).map((row) => row.key)); export function evaluatesOperand(run) { if (!VARIABLE_OPERAND_BUILTINS.has(run[0]?.text ?? "")) return false; const integer = run.some((w) => /^-[A-Za-z]*i/.test(w.text)); return run.some((w) => namesArrayElement(w.text) || namesArrayElement(w.raw) || (integer && /^[A-Za-z_][A-Za-z0-9_]*\+?=.*[A-Za-z_$]/.test(w.text))); } function namesArrayElement(word) { for (let open = word.indexOf("["); open > 0; open = word.indexOf("[", open + 1)) { if (!/[A-Za-z0-9_]/.test(word[open - 1])) continue; const close = word.indexOf("]", open); if (close > 0 && /[A-Za-z_$]/.test(word.slice(open + 1, close))) return true; } return false; }