/**
 * Mechanism Contract Registry — 查询函数与治理验证。
 */
import fs from "node:fs";
import path from "node:path";
import { PIPELINE_CONTRACTS } from "./contracts-pipeline.js";
import { PLATFORM_CONTRACTS } from "./contracts-platform.js";
import { GOVERNANCE_CONTRACTS } from "./contracts-governance.js";
import { AUDIT_CONTRACTS } from "./contracts-audit.js";
// ── 聚合 P0 契约 ──
/** P0 级机制契约列表 */
export const P0_MECHANISM_CONTRACTS = [
    ...PIPELINE_CONTRACTS,
    ...PLATFORM_CONTRACTS,
    ...GOVERNANCE_CONTRACTS,
    ...AUDIT_CONTRACTS,
];
// ── 查询函数 ──
/**
 * 执行 listMechanismContracts 操作
 */
export function listMechanismContracts() {
    return P0_MECHANISM_CONTRACTS;
}
/**
 * 根据契约 ID 查找机制契约。
 * @param id - 机制契约 ID
 * @returns 匹配的机制契约，未找到时返回 undefined
 */
export function findMechanismContractById(id) {
    return P0_MECHANISM_CONTRACTS.find((c) => c.id === id);
}
/**
 * 执行 listMechanismContractsByStatus 操作
 */
export function listMechanismContractsByStatus(status) {
    return P0_MECHANISM_CONTRACTS.filter((c) => c.status === status);
}
// ── 治理验证 ──
/**
 * 校验机制契约的结构完整性和一致性。
 * @param contracts - 待校验的契约列表，默认使用内置 P0 契约
 * @param rootDir - 项目根目录，默认为当前工作目录
 * @returns 治理发现列表（hard_fail 为硬性错误，advisory 为建议项）
 */
export function validateMechanismContracts(contracts = P0_MECHANISM_CONTRACTS, rootDir = process.cwd()) {
    const findings = [];
    const seenIds = new Set();
    for (const c of contracts) {
        // 重复 id
        if (seenIds.has(c.id)) {
            findings.push({ severity: "hard_fail", contract_id: c.id, field: "id", message: `Duplicate id: ${c.id}` });
        }
        seenIds.add(c.id);
        // owner_module 文件存在性
        if (!fs.existsSync(path.join(rootDir, c.owner_module))) {
            findings.push({ severity: "hard_fail", contract_id: c.id, field: "owner_module", message: `Module not found: ${c.owner_module}` });
        }
        // owner_exports 验证：每个导出必须在模块文件中存在
        if (c.owner_exports?.length && fs.existsSync(path.join(rootDir, c.owner_module))) {
            const moduleContent = fs.readFileSync(path.join(rootDir, c.owner_module), "utf-8");
            for (const exp of c.owner_exports) {
                if (!moduleContent.includes(exp)) {
                    findings.push({ severity: "hard_fail", contract_id: c.id, field: "owner_exports", message: `Export not found in module: ${exp}` });
                }
            }
        }
        // enforced 状态必须有 lifecycle_tests
        if (c.status === "enforced" && c.lifecycle_tests.length === 0) {
            findings.push({ severity: "hard_fail", contract_id: c.id, field: "lifecycle_tests", message: "Enforced mechanism must have lifecycle_tests" });
        }
        // 测试文件存在性
        for (const t of c.tests) {
            if (!fs.existsSync(path.join(rootDir, t))) {
                findings.push({ severity: "hard_fail", contract_id: c.id, field: "tests", message: `Test file not found: ${t}` });
            }
        }
        // lifecycle_tests 文件存在性
        for (const t of c.lifecycle_tests) {
            if (!fs.existsSync(path.join(rootDir, t))) {
                findings.push({ severity: "hard_fail", contract_id: c.id, field: "lifecycle_tests", message: `Lifecycle test file not found: ${t}` });
            }
        }
        // required_inputs 非空
        if (c.required_inputs.length === 0) {
            findings.push({ severity: "hard_fail", contract_id: c.id, field: "required_inputs", message: "required_inputs must not be empty" });
        }
        // outputs 非空
        if (c.outputs.length === 0) {
            findings.push({ severity: "hard_fail", contract_id: c.id, field: "outputs", message: "outputs must not be empty" });
        }
        // downstream_consumers 非空
        if (c.downstream_consumers.length === 0) {
            findings.push({ severity: "hard_fail", contract_id: c.id, field: "downstream_consumers", message: "downstream_consumers must not be empty" });
        }
        // state_lifecycle 必须有 state_transitions
        if (c.mechanism_type === "state_lifecycle" && (!c.state_transitions || c.state_transitions.length === 0)) {
            findings.push({ severity: "hard_fail", contract_id: c.id, field: "state_transitions", message: "state_lifecycle mechanism must have state_transitions" });
        }
        // 有 cleanup_hooks 但无 lifecycle_tests
        if (c.cleanup_hooks && c.cleanup_hooks.length > 0 && c.lifecycle_tests.length === 0) {
            findings.push({ severity: "hard_fail", contract_id: c.id, field: "lifecycle_tests", message: "Mechanism with cleanup_hooks must have lifecycle_tests" });
        }
        // ── 建议性检查 ──
        // failure_modes 为空
        if (c.failure_modes.length === 0) {
            findings.push({ severity: "advisory", contract_id: c.id, field: "failure_modes", message: "failure_modes is empty" });
        }
        // degradation_behavior 为空或过短
        if (!c.degradation_behavior || c.degradation_behavior.length < 10) {
            findings.push({ severity: "advisory", contract_id: c.id, field: "degradation_behavior", message: "degradation_behavior is empty or too short" });
        }
        // 通用 downstream_consumers
        if (c.downstream_consumers.includes("unknown")) {
            findings.push({ severity: "advisory", contract_id: c.id, field: "downstream_consumers", message: "downstream_consumers contains generic 'unknown'" });
        }
        // 已废弃但无 notes
        if (c.status === "deprecated" && !c.notes) {
            findings.push({ severity: "advisory", contract_id: c.id, field: "notes", message: "Deprecated mechanism must have notes explaining removal" });
        }
    }
    return findings;
}
//# sourceMappingURL=validation.js.map