/*
 * 文件定位：Agent 运行态实现层 / 应用承托面。
 * 核心目的：隔离应用扩展对 runtime 内部状态的影响。
 * 能力要求1：需要允许应用扩展能力，但所有扩展都应经过契约和治理约束。
 * 能力要求2：它让 agentCore 能被第三方应用复用，同时保持内核稳定。
 * 边界：承托和治理运行态，不吞并执行引擎、模型适配器或官方模块内部实现。
 * 对接：需要服务 applicationSurface、officialModuleSurface、governancePlane、invocationMethod 和 inspection/debug 等运行面。
 * 实现提示：先补稳定类型契约、最小可测行为和清晰错误边界，再接入真实执行逻辑。
 */
function isBlank(value) {
    return typeof value !== "string" || value.trim().length === 0;
}
function cleanList(values) {
    return [...new Set((values ?? []).map((value) => value.trim()).filter(Boolean))];
}
function cleanEffects(values) {
    return [...new Set(values ?? [])];
}
function failure(code, message, boundary) {
    return {
        ok: false,
        error: { code, message, boundary },
        events: ["application.runtime.sandbox.rejected"],
    };
}
export function createApplicationRuntimeSandbox(request) {
    if (isBlank(request.runtimeId)) {
        return failure("MISSING_RUNTIME_ID", "runtimeId is required before creating an application sandbox", "input");
    }
    if (isBlank(request.applicationId)) {
        return failure("MISSING_APPLICATION_ID", "applicationId is required before creating an application sandbox", "input");
    }
    if (request.extension === undefined || isBlank(request.extension.extensionId)) {
        return failure("MISSING_EXTENSION_ID", "extensionId is required before creating an application sandbox", "input");
    }
    if (request.contract?.accepted === false) {
        return failure("CONTRACT_REJECTED", request.contract.reason ?? "application extension contract was rejected", "contract");
    }
    if (request.governance?.accepted === false) {
        return failure("GOVERNANCE_REJECTED", request.governance.reason ?? "application extension was rejected by governance", "governance");
    }
    if (request.extension.mutatesInternalState === true) {
        return failure("INTERNAL_STATE_MUTATION_DENIED", "application extensions cannot mutate agentCore runtime internal state directly", "runtime-state");
    }
    const requestedCapabilities = cleanList(request.extension.requestedCapabilities);
    const allowedCapabilities = cleanList(request.allowedCapabilities);
    const grantedCapabilities = allowedCapabilities.length === 0
        ? requestedCapabilities
        : requestedCapabilities.filter((capability) => allowedCapabilities.includes(capability));
    const deniedCapabilities = allowedCapabilities.length === 0
        ? []
        : requestedCapabilities.filter((capability) => !allowedCapabilities.includes(capability));
    const blockedEffects = cleanEffects(request.extension.requestedEffects);
    const runtimeId = request.runtimeId.trim();
    const applicationId = request.applicationId.trim();
    const extensionId = request.extension.extensionId.trim();
    return {
        ok: true,
        envelope: {
            sandboxId: `${runtimeId}:${applicationId}:${extensionId}`,
            runtimeId,
            applicationId,
            extensionId,
            grantedCapabilities,
            deniedCapabilities,
            blockedEffects,
            dryRun: true,
            unsafeSideEffects: false,
            internalStateMutable: false,
        },
        events: blockedEffects.length > 0
            ? ["application.runtime.sandbox.created", "application.runtime.sandbox.effects.blocked"]
            : ["application.runtime.sandbox.created"],
    };
}
