/**
 * Tool Policy — Per-agent access control for tools
 *
 * Policy profiles are defined in YAML files under .proletariat/policies/.
 * Each profile specifies which MCP servers and CLI tools an agent can access.
 */
import * as fs from 'node:fs';
import * as path from 'node:path';
import yaml from 'js-yaml';
import { BUILTIN_PRLT_TOOL } from './types.js';
const POLICIES_DIR = 'policies';
/**
 * Get path to the policies directory.
 */
export function getPoliciesDir(hqPath) {
    return path.join(hqPath, '.proletariat', POLICIES_DIR);
}
/**
 * Load a tool policy by name.
 * Looks in .proletariat/policies/{name}.yaml
 */
export function loadToolPolicy(hqPath, policyName) {
    const policyPath = path.join(getPoliciesDir(hqPath), `${policyName}.yaml`);
    if (!fs.existsSync(policyPath)) {
        return null;
    }
    try {
        const content = fs.readFileSync(policyPath, 'utf-8');
        const parsed = yaml.load(content);
        return parsed?.tools ?? null;
    }
    catch {
        return null;
    }
}
/**
 * Save a tool policy.
 */
export function saveToolPolicy(hqPath, policyName, policy) {
    const policiesDir = getPoliciesDir(hqPath);
    fs.mkdirSync(policiesDir, { recursive: true });
    const policyPath = path.join(policiesDir, `${policyName}.yaml`);
    const content = yaml.dump({ tools: policy }, {
        indent: 2,
        lineWidth: 120,
        noRefs: true,
    });
    fs.writeFileSync(policyPath, content, 'utf-8');
}
/**
 * List all available policy profiles.
 */
export function listPolicies(hqPath) {
    const policiesDir = getPoliciesDir(hqPath);
    if (!fs.existsSync(policiesDir))
        return [];
    return fs.readdirSync(policiesDir)
        .filter(f => f.endsWith('.yaml') || f.endsWith('.yml'))
        .map(f => f.replace(/\.(yaml|yml)$/, ''));
}
/**
 * Filter registry by policy — returns only the tools the agent is allowed to use.
 * If no policy is provided, returns all registered tools.
 */
export function filterByPolicy(registry, policy) {
    const allMcp = Object.entries(registry['mcp-servers']);
    const allCli = Object.entries(registry['cli-tools']);
    // No policy = full access
    if (!policy) {
        return {
            mcpServers: allMcp.map(([name, config]) => ({ name, ...config })),
            cliTools: [
                ...allCli.map(([name, config]) => ({ name, ...config })),
                // Always include prlt
                ...(allCli.some(([name]) => name === 'prlt') ? [] : [BUILTIN_PRLT_TOOL]),
            ],
        };
    }
    // Filter MCP servers
    const allowedMcp = new Set(policy.mcp || []);
    const mcpServers = allMcp
        .filter(([name]) => allowedMcp.has(name))
        .map(([name, config]) => ({ name, ...config }));
    // Filter CLI tools
    const allowedCli = new Set(policy.cli || []);
    // Always allow prlt
    allowedCli.add('prlt');
    const cliTools = allCli
        .filter(([name]) => allowedCli.has(name))
        .map(([name, config]) => ({ name, ...config }));
    // Ensure prlt is always present
    if (!cliTools.some(t => t.name === 'prlt')) {
        cliTools.push(BUILTIN_PRLT_TOOL);
    }
    return { mcpServers, cliTools };
}
//# sourceMappingURL=policy.js.map