/*
 * 文件定位：Agent 运行态实现层。
 * 核心目的：承载 runtime Operation Bus 这一能力位点。
 * 能力要求1：需要把文件名表达的能力落实成清晰的类型、输入输出和最小行为。
 * 能力要求2：如果后续发现语义不足，应优先补接口契约，而不是把逻辑散落到相邻文件。
 * 边界：只服务 agentCore 内核，不写上层产品逻辑。
 * 对接：需要服务 applicationSurface、officialModuleSurface、governancePlane、invocationMethod 和 inspection/debug 等运行面。
 * 实现提示：先补稳定类型契约、最小可测行为和清晰错误边界，再接入真实执行逻辑。
 */
export const runtimeOperationBusDescriptor = {
    surface: "runtime.operationBus",
    capability: "runtimeOperationBus",
    purpose: "accept runtime operations from applications and official modules through contract and governance checks before real dispatch exists",
    unsafeSideEffects: false,
};
function isBlank(value) {
    return typeof value !== "string" || value.trim().length === 0;
}
function isRecord(value) {
    return typeof value === "object" && value !== null && !Array.isArray(value);
}
function cleanList(values) {
    return [...new Set((values ?? []).map((value) => value.trim()).filter(Boolean))];
}
function failure(code, message, boundary) {
    return {
        ok: false,
        error: {
            code,
            message,
            boundary,
            safeForRuntimeInspection: true,
            internalDetailExposed: false,
        },
        events: ["runtime.operationBus.rejected"],
    };
}
function resolveScopes(requestedScopes, allowedScopes) {
    const requested = cleanList(requestedScopes);
    const allowed = cleanList(allowedScopes);
    if (requested.length === 0) {
        return [];
    }
    if (allowed.length === 0) {
        return requested;
    }
    const denied = requested.filter((scope) => !allowed.includes(scope));
    if (denied.length > 0) {
        return failure("SCOPE_DENIED", `runtime operation scope ${denied[0]} is outside governance`, "scope");
    }
    return requested;
}
function normalizeCaller(caller) {
    if (caller === undefined || isBlank(caller.callerId) || caller.kind === undefined) {
        return undefined;
    }
    return {
        callerId: (caller.callerId ?? "").trim(),
        kind: caller.kind,
    };
}
export function submitRuntimeOperation(request) {
    if (request === undefined || isBlank(request.runtimeId)) {
        return failure("MISSING_RUNTIME_ID", "runtime operation bus requires runtimeId", "input");
    }
    if (request.runtimeReady === false) {
        return failure("RUNTIME_NOT_READY", "runtime operation bus can only accept operations for a ready runtime", "runtime-state");
    }
    if (request.dryRun === false) {
        return failure("REAL_DISPATCH_NOT_ALLOWED", "runtime operation bus only supports dry-run audit envelopes in the first implementation", "governance");
    }
    if (request.contract?.accepted === false) {
        return failure("CONTRACT_REJECTED", request.contract.reason ?? "runtime operation was rejected by contract surface", "contract");
    }
    if (request.governance?.accepted === false) {
        return failure("GOVERNANCE_REJECTED", request.governance.reason ?? "runtime operation was rejected by runtime governance", "governance");
    }
    if (request.operation === undefined) {
        return failure("MISSING_OPERATION", "runtime operation bus requires an operation envelope", "input");
    }
    const operationKind = request.operation.kind?.trim();
    if (!operationKind) {
        return failure("MISSING_OPERATION_KIND", "runtime operation bus requires operation.kind", "input");
    }
    const targetSurface = request.operation.targetSurface?.trim();
    if (!targetSurface) {
        return failure("MISSING_TARGET_SURFACE", "runtime operation bus requires operation.targetSurface", "input");
    }
    const caller = normalizeCaller(request.operation.caller);
    if (caller === undefined) {
        return failure("MISSING_CALLER", "runtime operation bus requires callerId and caller kind", "input");
    }
    if (request.operation.payload !== undefined && !isRecord(request.operation.payload)) {
        return failure("INVALID_PAYLOAD", "runtime operation payload must be a plain record", "input");
    }
    const mountedSurfaces = cleanList(request.mountedSurfaces);
    if (mountedSurfaces.length > 0 && !mountedSurfaces.includes(targetSurface)) {
        return failure("TARGET_SURFACE_NOT_MOUNTED", `runtime operation target surface ${targetSurface} is not mounted`, "bus");
    }
    const acceptedScopes = resolveScopes(request.requestedScopes, request.allowedScopes);
    if ("ok" in acceptedScopes) {
        return acceptedScopes;
    }
    const runtimeId = (request.runtimeId ?? "").trim();
    const operationId = request.operation.operationId?.trim() || `${runtimeId}:${caller.kind}:${targetSurface}:${operationKind}`;
    return {
        ok: true,
        envelope: {
            runtimeId,
            busSurface: "runtime.operationBus",
            operationId,
            operationKind,
            caller,
            targetSurface,
            payload: request.operation.payload ?? {},
            acceptedScopes,
            trace: {
                correlationId: request.operation.trace?.correlationId?.trim() || undefined,
                causationId: request.operation.trace?.causationId?.trim() || undefined,
            },
            dispatchMode: "dry-run",
            status: "accepted-for-audit",
            contractSurface: "runtime.contractSurface",
            governanceRequired: true,
            unsafeSideEffects: false,
        },
        events: ["runtime.operationBus.accepted"],
    };
}
