"use strict";
/**
 * Capability-Class Rule Vocabulary (W1.5 of F-NEW-PRODUCT-AGENT-VISIBILITY-CLUSTER-2026-05-12)
 *
 * Translates plain-English company rules ("no MCP", "no external DB outside BAA",
 * "no shell exec") into pack categories that flow through the existing PackEvaluator
 * hot path. A company pack declares categoryActions for any of the canonical
 * capability-class IDs below, and the matching detector fires when an agent's
 * tool call falls into that class.
 *
 * Action mapping (already in pack-evaluator.ts:actionToVerdict):
 *   BLOCK -> DENY
 *   REQUIRE_APPROVAL / WARN -> PENDING (queues an ApprovalRecord)
 *   LOG / ALLOW -> ALLOW
 *
 * @connexum/ai-governance  F-NEW-PRODUCT-CAPABILITY-CLASS-RULES
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.CAPABILITY_DETECTORS = exports.detectsExternalDbWrite = exports.detectsSecretAccess = exports.detectsNetworkEgress = exports.detectsFilesystemWrite = exports.detectsShellExec = exports.detectsWebFetch = exports.detectsMcp = exports.ALL_CAPABILITY_CLASS_IDS = exports.CAPABILITY_CLASS = void 0;
/** Canonical capability class IDs. Company packs reference these as category keys. */
exports.CAPABILITY_CLASS = {
    MCP: 'capability:mcp',
    WEB_FETCH: 'capability:web-fetch',
    EXTERNAL_DB_WRITE: 'capability:external-db-write',
    SHELL_EXEC: 'capability:shell-exec',
    FILESYSTEM_WRITE: 'capability:filesystem-write',
    NETWORK_EGRESS: 'capability:network-egress',
    SECRET_ACCESS: 'capability:secret-access',
};
exports.ALL_CAPABILITY_CLASS_IDS = Object.freeze(Object.values(exports.CAPABILITY_CLASS));
function parseSerialized(serialized) {
    try {
        const obj = JSON.parse(serialized);
        return {
            tool: typeof obj.tool === 'string' ? obj.tool : '',
            input: obj.input && typeof obj.input === 'object' ? obj.input : {},
        };
    }
    catch {
        return { tool: serialized, input: {} };
    }
}
const isMcpToolName = (tool) => {
    const t = tool.toLowerCase();
    return t.startsWith('mcp__') || t.startsWith('mcp_') || t.startsWith('mcp-') || t === 'mcp';
};
const isWebFetchToolName = (tool) => {
    const t = tool.toLowerCase();
    return (t === 'webfetch' ||
        t === 'web_fetch' ||
        t === 'web-fetch' ||
        t === 'fetch' ||
        t === 'webrequest' ||
        t === 'browser_fetch');
};
const isShellExecToolName = (tool) => {
    const t = tool.toLowerCase();
    return (t === 'bash' ||
        t === 'shell' ||
        t === 'exec' ||
        t === 'execute' ||
        t === 'execute_command' ||
        t === 'run_command');
};
const isFilesystemWriteToolName = (tool) => {
    const t = tool.toLowerCase();
    return t === 'write' || t === 'edit' || t === 'multiedit' || t === 'write_file' || t === 'fs_write';
};
const isNetworkEgressToolName = (tool) => {
    const t = tool.toLowerCase();
    if (isWebFetchToolName(tool))
        return true;
    return t === 'http' || t === 'https' || t === 'curl' || t === 'request' || t.startsWith('http_');
};
const isSecretAccessToolName = (tool) => {
    const t = tool.toLowerCase();
    return t.includes('secret') || t.includes('credential') || t.includes('vault') || t.includes('keychain');
};
const DESTRUCTIVE_SQL = /\b(INSERT|UPDATE|DELETE|DROP|TRUNCATE|ALTER|CREATE|REPLACE|MERGE)\b/i;
const DB_TOOL_HINTS = ['sql', 'query', 'database', 'db_', 'postgres', 'mysql', 'mongo', 'execute'];
const isExternalDbWriteAction = (tool, input) => {
    const t = tool.toLowerCase();
    if (!DB_TOOL_HINTS.some((h) => t.includes(h)))
        return false;
    const stmtCandidates = ['query', 'sql', 'statement', 'command', 'cmd'];
    for (const key of stmtCandidates) {
        const v = input[key];
        if (typeof v === 'string' && DESTRUCTIVE_SQL.test(v))
            return true;
    }
    return false;
};
const inputArgsContainSecretPatterns = (input) => {
    try {
        const flat = JSON.stringify(input).toLowerCase();
        return /\b(api[_-]?key|password|secret|access[_-]?token|bearer\s+\S+)\b/.test(flat);
    }
    catch {
        return false;
    }
};
const detectsMcp = (serialized) => isMcpToolName(parseSerialized(serialized).tool);
exports.detectsMcp = detectsMcp;
const detectsWebFetch = (serialized) => isWebFetchToolName(parseSerialized(serialized).tool);
exports.detectsWebFetch = detectsWebFetch;
const detectsShellExec = (serialized) => isShellExecToolName(parseSerialized(serialized).tool);
exports.detectsShellExec = detectsShellExec;
const detectsFilesystemWrite = (serialized) => isFilesystemWriteToolName(parseSerialized(serialized).tool);
exports.detectsFilesystemWrite = detectsFilesystemWrite;
const detectsNetworkEgress = (serialized) => isNetworkEgressToolName(parseSerialized(serialized).tool);
exports.detectsNetworkEgress = detectsNetworkEgress;
const detectsSecretAccess = (serialized) => {
    const { tool, input } = parseSerialized(serialized);
    if (isSecretAccessToolName(tool))
        return true;
    return inputArgsContainSecretPatterns(input);
};
exports.detectsSecretAccess = detectsSecretAccess;
const detectsExternalDbWrite = (serialized) => {
    const { tool, input } = parseSerialized(serialized);
    return isExternalDbWriteAction(tool, input);
};
exports.detectsExternalDbWrite = detectsExternalDbWrite;
exports.CAPABILITY_DETECTORS = {
    [exports.CAPABILITY_CLASS.MCP]: exports.detectsMcp,
    [exports.CAPABILITY_CLASS.WEB_FETCH]: exports.detectsWebFetch,
    [exports.CAPABILITY_CLASS.EXTERNAL_DB_WRITE]: exports.detectsExternalDbWrite,
    [exports.CAPABILITY_CLASS.SHELL_EXEC]: exports.detectsShellExec,
    [exports.CAPABILITY_CLASS.FILESYSTEM_WRITE]: exports.detectsFilesystemWrite,
    [exports.CAPABILITY_CLASS.NETWORK_EGRESS]: exports.detectsNetworkEgress,
    [exports.CAPABILITY_CLASS.SECRET_ACCESS]: exports.detectsSecretAccess,
};
//# sourceMappingURL=capability-classes.js.map