"use strict";
/**
 * Typed accessor for chorus's permission/sandbox settings.
 *
 * Stored in the `settings` SQLite table as JSON-encoded values keyed by:
 *   - sandbox_profile: 'strict' | 'workspace' | 'full'
 *   - auto_approve_prompts: boolean
 *   - network_access: boolean (overlay on top of sandbox_profile)
 *
 * Defaults are conservative:
 *   - workspace: read+write inside chat dir, no network. Reviewers can't
 *     escape the chat scope or call out to the public internet.
 *   - auto_approve_prompts: true. We pre-approve everything we can so that
 *     headless reviewer spawns don't hang on UI prompts. Users on a shared
 *     box can flip this to false.
 *   - network_access: false unless template/transport specifically needs it.
 *
 * The runner consults these before each spawn and forwards them as
 * AgentSpawnOptions to the shim. Each shim translates the abstract setting
 * into the appropriate CLI flag (e.g. codex `-c sandbox_mode=read-only`,
 * kimi `--afk`, gemini `--approval-mode auto_edit`).
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.PROFILE_DESCRIPTIONS = exports.DEFAULT_PERMISSIONS = void 0;
exports.getPermissions = getPermissions;
exports.setPermissions = setPermissions;
const db_1 = require("../db");
const zod_1 = require("zod");
const SandboxProfileSchema = zod_1.z.enum(['strict', 'workspace', 'full']);
exports.DEFAULT_PERMISSIONS = {
    sandboxProfile: 'workspace',
    autoApprovePrompts: true,
    networkAccess: false,
};
const SANDBOX_KEY = 'sandbox_profile';
const AUTO_APPROVE_KEY = 'auto_approve_prompts';
const NETWORK_KEY = 'network_access';
async function getPermissions() {
    const [sandboxRaw, autoApproveRaw, networkRaw] = await Promise.all([
        db_1.settings.get(SANDBOX_KEY),
        db_1.settings.get(AUTO_APPROVE_KEY),
        db_1.settings.get(NETWORK_KEY),
    ]);
    const sandboxProfile = SandboxProfileSchema.safeParse(sandboxRaw);
    return {
        sandboxProfile: sandboxProfile.success ? sandboxProfile.data : exports.DEFAULT_PERMISSIONS.sandboxProfile,
        autoApprovePrompts: typeof autoApproveRaw === 'boolean' ? autoApproveRaw : exports.DEFAULT_PERMISSIONS.autoApprovePrompts,
        networkAccess: typeof networkRaw === 'boolean' ? networkRaw : exports.DEFAULT_PERMISSIONS.networkAccess,
    };
}
async function setPermissions(input) {
    if (input.sandboxProfile !== undefined) {
        SandboxProfileSchema.parse(input.sandboxProfile);
        await db_1.settings.set(SANDBOX_KEY, input.sandboxProfile);
    }
    if (input.autoApprovePrompts !== undefined) {
        await db_1.settings.set(AUTO_APPROVE_KEY, input.autoApprovePrompts);
    }
    if (input.networkAccess !== undefined) {
        await db_1.settings.set(NETWORK_KEY, input.networkAccess);
    }
    return getPermissions();
}
/**
 * Human-readable description of each profile, used by onboarding + /settings UI.
 */
exports.PROFILE_DESCRIPTIONS = {
    strict: {
        label: 'Strict',
        description: 'Reviewers can only read code. No file writes, no shell exec, no network. Safest, but limits what reviewers can do (e.g. can\'t run the test suite they\'re reviewing).',
    },
    workspace: {
        label: 'Workspace (recommended)',
        description: 'Reviewers can read+write inside the chat dir and run scoped shell commands. No network. Default — fits most teams.',
    },
    full: {
        label: 'Full access',
        description: 'Reviewers run with no sandbox: write anywhere, run any command, full network. Only enable on a personal machine you trust.',
    },
};
//# sourceMappingURL=permissions.js.map