import { normalizeCliArea } from "./area-aliases.js"; import { parseArgs } from "./parser.js"; /** * Read-only classification for CLI commands, used to decide whether a command may be executed * through the read-only MCP tool. * * This is an allow-list: anything not listed here — including a command added to the CLI later — * is treated as mutating. The failure mode is therefore a refused read, never an unsafe write. * * Matching is token-wise prefix, because positional arguments are parsed into the command: a * command line of `approval-runs get 01a09dac...` must still match the allowed `approval-runs get`. * Prefix matching is boundary-safe by construction: `list-evil` does not match `list`. */ /** * Commands that only read tenant state, produce a local file, or inspect local configuration. * Deliberately excluded even though they look harmless: anything that writes local config * (`corps switch`, `config test-dir`, `config mode `, `config lint `), mints * or prints credentials (`auth keys`, `corps keys`), * self-updates (`update`), or stands up a long-running server (`mcp`). */ const READ_ONLY_COMMANDS: Record = { corps: ["list", "search", "get", "current"], rules: ["count", "list", "versions", "version", "diff", "get", "search", "lint", "linters", "pull", "map"], documents: ["list", "get", "search"], webhook: ["ready", "status", "list", "get", "resource-pack export", "resource-pack preview"], "approval-runs": [ "summary", "stats", "count", "export", "flow", "hose-flow", "list", // `search` is an accepted alias of `list` in the dispatcher but has no help-registry entry. "search", "pick", "bridge", "get", "cited-rules", "diagnose", "document-summary", "attachments", "result", "link", "url" ], simulation: [ "batch-records", "status", "wait", "specifications", "approval-definitions", "list", "get", "templates", "integrations" ], deploy: ["hosecloud requirements", "hosecloud list", "hosecloud get", "hosecloud status"], // `hose-ebot callback` is deliberately absent: it mints an accessToken and writes an // approval action into the live flow. Only the two discovery verbs read. "hose-ebot": ["search", "messages"], preference: ["get", "thinking-depth policies list"], // `config mode`/`config lint` are omitted: classification is token-prefix based, so their // read forms cannot be separated from the value-taking write forms. `config get` returns // the same summary (mode + lint included), so read access loses nothing. config: ["paths", "get"], auth: ["check", "diagnose", "status", "test", "doctor", "link list", "seal-link list"], users: ["search", "get"], contacts: ["departments", "children", "users", "sync-time"], data: ["expenses", "invoices"], expenses: ["", "list"], llm: ["config", "providers"], permissions: ["available", "configs"], billing: ["status", "plans", "features"], // `analyze` is deliberately absent: it POSTs and starts an analysis server-side, so it is a // mutation despite reading like a query. See SEAL_MUTATING_CAPABILITIES. analytics: ["insight", "risks", "performance", "export"], report: ["monthly", "data", "template", "chart venn", "validate", "render", "review list"], source: ["config"], skill: ["dump", "status"], doctor: ["", "guide", "paths", "tenant", "auth"], // The whole Langfuse area is read-only: the embedded client issues GET only. langfuse: [ "check", "cache-refresh", "outline", "search", "observation", "session", "url", "observations", "attachment-flow", "rule-flow" ], // Bare form only; the optional trailing `` selector is handled by // SELECTOR_POSITIONAL_AREAS below, since a bare entry matches no tokens. probe: [""] }; /** * Areas whose trailing positional is an enterprise selector, not a subcommand: * `seal-use probe []` has no verb to list, so its read form is the bare * command plus at most that one selector. A second token is not a form the area * accepts at all, and falls through to the refusal below. */ const SELECTOR_POSITIONAL_AREAS = new Set(["probe"]); /** * Read-only commands handled by the CLI outside the help registry (no area/subcommand shape). * `help` is limited to the bare form on purpose: `help ` adds nothing over the skill docs. */ const TOP_LEVEL_READ_ONLY = new Set(["version", "v", "help", "changelog", "mcp-config"]); export function describeCliCommand(argv: string[]): { area: string; actionTokens: string[] } { const { command } = parseArgs(argv); const area = (command[0] ?? "").toLowerCase(); return { area: normalizeCliArea(area), actionTokens: command.slice(1).map((token) => token.toLowerCase()) }; } export function isReadOnlyCliCommand(argv: string[]): boolean { let area: string; let actionTokens: string[]; let options: Record; try { const parsed = parseArgs(argv); options = parsed.options; ({ area, actionTokens } = describeCliCommand(argv)); } catch { // Unparsable command lines are refused rather than guessed at. return false; } if (actionTokens.length === 0 && TOP_LEVEL_READ_ONLY.has(area)) return true; // The `` selector form; the bare form falls through to the `""` entry. if (SELECTOR_POSITIONAL_AREAS.has(area) && actionTokens.length === 1) return true; // `rules push status` / `rules push --check` inspects the recovery journal and // reconciles against the remote draft without mutating anything. if ( area === "rules" && actionTokens[0] === "push" && (actionTokens[1] === "status" || options.check === true || options.status === "true") && !options.yes && !options.confirm ) { return true; } const allowed = READ_ONLY_COMMANDS[area]; if (!allowed) return false; return allowed.some((entry) => { const allowedTokens = entry.split(" ").filter(Boolean); // A bare command (empty entry) matches only when no subcommand was given, so that listing // `doctor` does not also cover every `doctor `. if (allowedTokens.length === 0) return actionTokens.length === 0; if (allowedTokens.length > actionTokens.length) return false; return allowedTokens.every((token, index) => token === actionTokens[index]); }); } /** Registry-backed entries only; the top-level commands above have no registry row. */ export function listReadOnlyCommands(): Array<{ area: string; action: string }> { return Object.entries(READ_ONLY_COMMANDS).flatMap(([area, actions]) => actions.map((action) => ({ area, action })) ); }