/**
 * Knowledge Asset Generation Gate — 问题五十七核心机制之四。
 *
 * 新增或修改知识资产前必须通过准入门:
 * - Schema 校验
 * - 重复/冲突校验
 * - Owner 校验
 * - 消费路径校验
 * - 中文质量门
 * - 回归门
 */
import { validateKnowledgeAssetFrontmatter } from "./knowledge_asset_schema.js";
// ── 核心函数 ──
/**
 * 验证知识资产生成门 — 新增资产必须通过所有检查。
 */
export function validateKnowledgeAssetGenerationGate(input) {
    const findings = [];
    // 1. Schema 校验
    const schemaFindings = validateKnowledgeAssetFrontmatter(input.asset.frontmatter, input.asset.file_path);
    for (const sf of schemaFindings) {
        findings.push({
            asset_id: sf.asset_id,
            file_path: sf.file_path,
            severity: sf.severity === "advisory" ? "warning" : sf.severity,
            kind: "missing_required_field",
            detail: sf.message_zh,
            suggested_action: "repair",
        });
    }
    // 2. ID 唯一性检查
    const id = input.asset.frontmatter.id;
    if (id) {
        const existingIds = new Set(input.existing_assets.map((a) => a.frontmatter.id).filter(Boolean));
        if (existingIds.has(id)) {
            findings.push({
                asset_id: id,
                file_path: input.asset.file_path,
                severity: "hard_fail",
                kind: "duplicate_id",
                detail: `资产 ID ${id} 已存在 (SF-KA-5702)`,
                suggested_action: "merge",
            });
        }
    }
    // 3. canonical 冲突检查
    if (input.asset.frontmatter.authority === "canonical" && input.asset.frontmatter.name) {
        const existingCanonical = input.existing_assets.filter((a) => a.frontmatter.authority === "canonical" && a.frontmatter.name === input.asset.frontmatter.name);
        if (existingCanonical.length > 0) {
            findings.push({
                asset_id: id ?? "unknown",
                file_path: input.asset.file_path,
                severity: "hard_fail",
                kind: "duplicate_canonical",
                detail: `主题 "${input.asset.frontmatter.name}" 已有 canonical 定义: ${existingCanonical.map((a) => a.frontmatter.id).join(", ")} (SF-KA-5703)`,
                suggested_action: "mark_supporting",
            });
        }
    }
    // 4. Owner 检查
    const kind = input.asset.frontmatter.asset_kind;
    if (kind && ["hard_rule", "workflow"].includes(kind)) {
        if (!input.asset.frontmatter.owner_mechanism_id) {
            findings.push({
                asset_id: id ?? "unknown",
                file_path: input.asset.file_path,
                severity: "hard_fail",
                kind: "owner_missing",
                detail: `${kind} 必须有 owner_mechanism_id (SF-KA-5704)`,
                suggested_action: "repair",
            });
        }
    }
    // 5. required_evidence 检查
    if (input.asset.frontmatter.priority && ["P0", "P1"].includes(input.asset.frontmatter.priority)) {
        if (!input.asset.frontmatter.required_evidence || input.asset.frontmatter.required_evidence.length === 0) {
            findings.push({
                asset_id: id ?? "unknown",
                file_path: input.asset.file_path,
                severity: "hard_fail",
                kind: "required_evidence_missing",
                detail: `P0/P1 资产必须声明 required_evidence (SF-KA-5706)`,
                suggested_action: "repair",
            });
        }
    }
    // 6. 用户修改保护
    if (id && input.user_modified_assets.includes(id)) {
        findings.push({
            asset_id: id,
            file_path: input.asset.file_path,
            severity: "hard_fail",
            kind: "missing_required_field",
            detail: `资产 ${id} 已被用户修改，不得覆盖`,
            suggested_action: "manual_required",
        });
    }
    // 7. checklist 硬规则检查
    if (kind === "checklist" && input.asset.frontmatter.hard_blocks && input.asset.frontmatter.hard_blocks.length > 0) {
        findings.push({
            asset_id: id ?? "unknown",
            file_path: input.asset.file_path,
            severity: "hard_fail",
            kind: "semantic_conflict",
            detail: "checklist 不得平行定义硬规则，只能引用 canonical",
            suggested_action: "mark_supporting",
        });
    }
    // 8. when/triggers 格式检查
    if (input.asset.frontmatter.primary_triggers && input.asset.frontmatter.primary_triggers.length > 10) {
        findings.push({
            asset_id: id ?? "unknown",
            file_path: input.asset.file_path,
            severity: "warning",
            kind: "trigger_conflict",
            detail: `primary_triggers 过多 (${input.asset.frontmatter.primary_triggers.length} 个)，可能导致误召回`,
            suggested_action: "repair",
        });
    }
    const hardFails = findings.filter((f) => f.severity === "hard_fail");
    const passed = hardFails.length === 0;
    return {
        passed,
        generation_gate_status: passed ? "accepted" : "rejected",
        findings,
        reason_zh: passed
            ? "资产生成门通过"
            : `资产生成门未通过: ${hardFails.map((f) => f.detail).join("; ")}`,
    };
}
//# sourceMappingURL=knowledge_asset_generation_gate.js.map