import { isReadOnlySealTool } from "../domains/seal/tools.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`. */ /** Area aliases the CLI dispatches under an alternative name; mirrors the pairs in runner.ts. */ const AREA_ALIASES: Record = { corp: "corps", webhooks: "webhook", preferences: "preference" }; /** * 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`), mints or prints credentials (`auth keys`, `corps keys`), * self-updates (`update`), or stands up a long-running server (`http`, `mcp`, `service run`). */ const READ_ONLY_COMMANDS: Record = { corps: ["list", "search", "get", "current"], rules: ["count", "list", "versions", "version", "get", "search", "lint", "linters", "pull"], webhook: ["ready", "status", "list", "get", "resource-pack export", "resource-pack preview"], "approval-runs": [ "summary", "stats", "count", "export", "flow", "list", // `search` is an accepted alias of `list` in the dispatcher but has no help-registry entry. "search", "pick", "bridge", "get", "cited-rules", "document-summary", "attachments", "result", "url" ], simulation: [ "batch-records", "status", "wait", "specifications", "approval-definitions", "list", "get", "templates", "integrations" ], deploy: ["hosecloud requirements", "hosecloud list", "hosecloud get", "hosecloud status"], preference: ["get", "thinking-depth policies list"], config: ["paths", "get", "mode", "lint"], auth: ["diagnose"], delegation: ["list"], users: ["search", "get", "delegation"], contacts: ["departments", "children", "users", "sync-time"], data: ["expenses", "invoices"], 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_TOOLS. analytics: ["insight", "risks", "performance", "export"], source: ["config"], tools: ["list"], skill: ["dump", "status"], service: ["status", "logs"], doctor: ["", "guide", "paths", "tenant"], openapi: [""] }; /** * 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: AREA_ALIASES[area] ?? area, actionTokens: command.slice(1).map((token) => token.toLowerCase()) }; } export function isReadOnlyCliCommand(argv: string[]): boolean { let area: string; let actionTokens: string[]; try { ({ 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; // `seal-use tool ` reaches the internal tool registry, which carries its own exhaustive // read/write classification. Delegating to it keeps the two from drifting apart. if (area === "tool") { const toolName = actionTokens[0]; return toolName !== undefined && isReadOnlySealTool(toolName); } 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 })) ); }