/*
 * 文件定位：Agent 运行态实现层。
 * 核心目的：承载 runtime Composition Root 这一能力位点。
 * 能力要求1：需要把文件名表达的能力落实成清晰的类型、输入输出和最小行为。
 * 能力要求2：如果后续发现语义不足，应优先补接口契约，而不是把逻辑散落到相邻文件。
 * 边界：只服务 agentCore 内核，不写上层产品逻辑。
 * 对接：需要服务 applicationSurface、officialModuleSurface、governancePlane、invocationMethod 和 inspection/debug 等运行面。
 * 实现提示：先补稳定类型契约、最小可测行为和清晰错误边界，再接入真实执行逻辑。
 */
export const runtimeCompositionRootSurface = "runtime.compositionRoot";
const defaultRequiredSurfaces = [
    "runtime.contractSurface",
    "runtime.governancePlane",
    "runtime.invocationMethod",
];
function isBlank(value) {
    return typeof value !== "string" || value.trim().length === 0;
}
function requireTrimmed(value) {
    return typeof value === "string" ? value.trim() : "";
}
function cleanList(values) {
    return [...new Set((values ?? []).map((value) => value.trim()).filter((value) => value.length > 0))];
}
function failure(code, message, boundary) {
    return {
        ok: false,
        error: { code, message, boundary, publicSafe: true },
        events: ["runtime.compositionRoot.rejected"],
    };
}
function normalizeCaller(caller) {
    return {
        kind: caller.kind,
        id: caller.id.trim(),
        moduleId: caller.moduleId?.trim() || undefined,
        sessionId: caller.sessionId?.trim() || undefined,
    };
}
function normalizeSurface(surfaceInput) {
    const surfaceText = requireTrimmed(surfaceInput.surface);
    const bindingId = requireTrimmed(surfaceInput.bindingId);
    if (surfaceText.length === 0) {
        return failure("MISSING_SURFACE_NAME", "runtime composition root requires every binding to name a surface", "composition");
    }
    const surface = surfaceText;
    if (bindingId.length === 0) {
        return failure("MISSING_SURFACE_BINDING_ID", `runtime composition root requires a bindingId for ${surface}`, "composition");
    }
    if (surfaceInput.ready === false) {
        return failure("SURFACE_NOT_READY", `runtime composition surface ${surface} is not ready`, "runtime-state");
    }
    return {
        surface,
        bindingId,
        ready: true,
        capabilities: cleanList(surfaceInput.capabilities),
        metadata: surfaceInput.metadata ?? {},
    };
}
function acceptedScopes(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 composition root scope ${denied[0]} is not allowed`, "scope");
    }
    return requested;
}
export function createRuntimeCompositionRoot(request) {
    if (request === undefined || isBlank(request.runtimeId)) {
        return failure("MISSING_RUNTIME_ID", "runtime composition root requires a runtimeId", "input");
    }
    const runtimeId = requireTrimmed(request.runtimeId);
    if (request.caller === undefined) {
        return failure("MISSING_CALLER", "runtime composition root requires a caller surface", "input");
    }
    if (isBlank(request.caller.id)) {
        return failure("MISSING_CALLER_ID", "runtime composition root caller requires a stable id", "input");
    }
    if (request.runtimeReady === false) {
        return failure("RUNTIME_NOT_READY", "runtime composition root can only compose a ready runtime host", "runtime-state");
    }
    if (request.contract?.accepted === false) {
        return failure("CONTRACT_REJECTED", request.contract.reason ?? "runtime composition root was rejected by contract surface", "contract");
    }
    if (request.governance?.accepted === false) {
        return failure("GOVERNANCE_REJECTED", request.governance.reason ?? "runtime composition root was rejected by governance", "governance");
    }
    if ((request.surfaces ?? []).length === 0) {
        return failure("MISSING_SURFACES", "runtime composition root requires explicitly injected runtime surfaces", "input");
    }
    const scopes = acceptedScopes(request.requestedScopes, request.allowedScopes);
    if ("ok" in scopes) {
        return scopes;
    }
    const surfaces = [];
    for (const surfaceInput of request.surfaces ?? []) {
        const normalized = normalizeSurface(surfaceInput);
        if ("ok" in normalized) {
            return normalized;
        }
        surfaces.push(normalized);
    }
    const surfaceNames = surfaces.map((surface) => surface.surface);
    const requiredSurfaces = cleanList(request.requiredSurfaces);
    const effectiveRequiredSurfaces = requiredSurfaces.length > 0 ? requiredSurfaces : defaultRequiredSurfaces;
    const missingRequiredSurfaces = effectiveRequiredSurfaces.filter((surface) => !surfaceNames.includes(surface));
    if (missingRequiredSurfaces.length > 0) {
        return failure("REQUIRED_SURFACE_MISSING", `runtime composition root is missing required surfaces: ${missingRequiredSurfaces.join(", ")}`, "composition");
    }
    return {
        ok: true,
        composition: {
            runtimeId,
            caller: normalizeCaller(request.caller),
            surface: runtimeCompositionRootSurface,
            phase: "composed",
            surfaces,
            surfaceNames,
            bindingIds: surfaces.map((surface) => surface.bindingId),
            requiredSurfaces: effectiveRequiredSurfaces,
            acceptedScopes: scopes,
            contractChecked: true,
            governanceChecked: true,
            dryRun: true,
            unsafeSideEffects: false,
        },
        events: ["runtime.compositionRoot.composed"],
    };
}
