/**
 * 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
 */
/** Canonical capability class IDs. Company packs reference these as category keys. */
export const 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',
};
export const ALL_CAPABILITY_CLASS_IDS = Object.freeze(Object.values(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;
    }
};
export const detectsMcp = (serialized) => isMcpToolName(parseSerialized(serialized).tool);
export const detectsWebFetch = (serialized) => isWebFetchToolName(parseSerialized(serialized).tool);
export const detectsShellExec = (serialized) => isShellExecToolName(parseSerialized(serialized).tool);
export const detectsFilesystemWrite = (serialized) => isFilesystemWriteToolName(parseSerialized(serialized).tool);
export const detectsNetworkEgress = (serialized) => isNetworkEgressToolName(parseSerialized(serialized).tool);
export const detectsSecretAccess = (serialized) => {
    const { tool, input } = parseSerialized(serialized);
    if (isSecretAccessToolName(tool))
        return true;
    return inputArgsContainSecretPatterns(input);
};
export const detectsExternalDbWrite = (serialized) => {
    const { tool, input } = parseSerialized(serialized);
    return isExternalDbWriteAction(tool, input);
};
export const CAPABILITY_DETECTORS = {
    [CAPABILITY_CLASS.MCP]: detectsMcp,
    [CAPABILITY_CLASS.WEB_FETCH]: detectsWebFetch,
    [CAPABILITY_CLASS.EXTERNAL_DB_WRITE]: detectsExternalDbWrite,
    [CAPABILITY_CLASS.SHELL_EXEC]: detectsShellExec,
    [CAPABILITY_CLASS.FILESYSTEM_WRITE]: detectsFilesystemWrite,
    [CAPABILITY_CLASS.NETWORK_EGRESS]: detectsNetworkEgress,
    [CAPABILITY_CLASS.SECRET_ACCESS]: detectsSecretAccess,
};
//# sourceMappingURL=capability-classes.js.map