UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

80 lines (79 loc) • 3.89 kB
import { commandPatternMayRun, parseRuleText, ruleToolGrammarOf } from "./permission-rule-model.js"; import { isShellFaceToolName } from "./tool-registry.js"; import { splitShellCompoundSegments } from "../tools/fs/bash-readonly-classifier.js"; import { SHELL_LEX_DIALECT } from "../shell/lex.js"; export const AUTO_STRIPPED_PROGRAMS = Object.freeze([ "python", "python3", "python2", "node", "deno", "tsx", "ruby", "perl", "php", "lua", "npx", "bunx", "npm run", "yarn run", "pnpm run", "bun run", "bash", "sh", "ssh", "zsh", "fish", "eval", "exec", "env", "xargs", "sudo", ]); export const AUTO_STRIP_REASONS = Object.freeze(["whole_shell", "code_runner", "classify_all_shell"]); function shellContentStripped(content) { if (content === undefined || content === "" || /^[\s*]+$/.test(content)) return "whole_shell"; const n = content.trim().toLowerCase(); if (n === "*") return "whole_shell"; for (const program of AUTO_STRIPPED_PROGRAMS) { const g = program.toLowerCase(); if (n === g || n === `${g}:*` || n === `${g} *` || n === `${g}*`) return "code_runner"; if (n.startsWith(`${g} `) && n.endsWith("*")) { const tail = n.slice(g.length + 1); if (tail.startsWith("-") && !(/^python[\d.]*$/.test(g) && /^-m\s+\w+\.[\w.]+(\s*:|\s+)$/.test(tail.slice(0, -1)))) return "code_runner"; } } return undefined; } function shellRuleContent(rule) { const split = splitShellCompoundSegments(SHELL_LEX_DIALECT, rule.command, { trailingTerminator: "keep", redirection: "reject" }); const body = "reject" in split ? rule.command : (split.segments[split.segments.length - 1] ?? rule.command).trim(); return rule.match === "prefix" ? `${body}:*` : rule.match === "wildcard" ? `${body} *` : body; } export function autoStripReasonOf(compiled, callAnswersToShell, allShell) { switch (compiled.kind) { case "shell": return allShell ? "classify_all_shell" : "whole_shell"; case "tool": return callAnswersToShell() ? (allShell ? "classify_all_shell" : "whole_shell") : undefined; case "command": if (allShell) return "classify_all_shell"; if (compiled.rule.match === "call") return undefined; if (compiled.rule.match === "pattern") return commandPatternMayRun(compiled.rule.command, AUTO_STRIPPED_PROGRAMS) ? "code_runner" : undefined; return shellContentStripped(shellRuleContent(compiled.rule)); case "covering": case "path": case "name": case "domain": return allShell && callAnswersToShell() ? "classify_all_shell" : undefined; default: { const exhaustive = compiled; return exhaustive; } } } const NO_CALL = () => false; export function readFaceAutoStrip(compiled) { const reason = autoStripReasonOf(compiled, NO_CALL, false); return reason === "classify_all_shell" ? undefined : reason; } export function entryOfRow(rule) { if (rule.match === "tool") return isShellFaceToolName(rule.tool) ? { kind: "shell", name: rule.tool } : { kind: "tool", name: rule.tool }; const grammar = ruleToolGrammarOf(rule.tool); return grammar === undefined ? undefined : { kind: grammar, rule }; } export function storedAllowRowRead(rule) { if (rule.behavior !== "allow" || typeof rule.rule !== "string") return undefined; const read = parseRuleText(rule.rule, "allow"); return "reject" in read ? undefined : { ...read.rule, scope: rule.scope, adds: rule.adds }; } export function storedRowAutoStrip(rule) { const read = storedAllowRowRead(rule); const entry = read === undefined ? undefined : entryOfRow(read); return entry === undefined ? undefined : readFaceAutoStrip(entry); }