/**
 * Main Path Integration Contract — 孤岛模块门禁 + 集成审计 + 治理验证 + 交付验证
 */
import { debug } from "../../core/logger.js";
import { isExemptFile, hasEntryTrace, hasObservability, hasIntegrationTest, hasProductionImport } from "./queries.js";
/**
 * 孤岛模块门禁检查。
 */
export function orphanGate(contract, capabilityKind) {
    const findings = [];
    const path = contract.module_path;
    debug("主链路接入", `开始孤岛门禁检查 — ${path}`);
    // 豁免文件始终允许
    const exempt = isExemptFile(path);
    if (exempt.exempt) {
        debug("主链路接入", "孤岛门禁 — 豁免文件 " + path + " (" + exempt.reason + ")");
        findings.push({
            severity: "allow",
            module_path: path,
            reason: `豁免文件: ${exempt.reason}`,
            recommended_action: "allow",
        });
        return findings;
    }
    // 规则 1: 只被测试导入 → hard_fail
    if (contract.production_imports.length === 0 && contract.integration_tests.length > 0) {
        debug("主链路接入", "孤岛门禁 — 规则1命中: " + path + " 只被测试导入");
        findings.push({
            severity: "hard_fail",
            module_path: path,
            reason: `模块 ${path} 只被测试导入，无生产代码调用`,
            recommended_action: "wire_to_main_path",
        });
    }
    // 规则 1b: 被导入但从未被调用（仅导入）→ hard_fail
    if (contract.production_imports.length === 0 && contract.status === "implemented_not_integrated") {
        debug("主链路接入", "孤岛门禁 — 规则1b命中: " + path + " 被导入但从未被调用");
        findings.push({
            severity: "hard_fail",
            module_path: path,
            reason: `模块 ${path} 被导入但从未被调用，无真实调用证据`,
            recommended_action: "wire_to_main_path",
        });
    }
    // 规则 2: enforced 能力无入口链路 → hard_fail
    if (contract.status === "implemented_not_integrated" && !hasEntryTrace(contract)) {
        debug("主链路接入", "孤岛门禁 — 规则2命中: " + path + " 无入口链路");
        findings.push({
            severity: "hard_fail",
            module_path: path,
            reason: `新增 enforced 能力 ${path} 无入口链路`,
            recommended_action: "add_entry_trace",
        });
    }
    // 规则 3: guard/verifier/contract 只被测试调用 → hard_fail
    if (capabilityKind &&
        (capabilityKind === "guard" || capabilityKind === "verifier" || capabilityKind === "contract") &&
        !hasProductionImport(contract)) {
        debug("主链路接入", "孤岛门禁 — 规则3命中: " + capabilityKind + " " + path + " 只被测试调用");
        findings.push({
            severity: "hard_fail",
            module_path: path,
            reason: `新增 ${capabilityKind} ${path} 只被测试调用`,
            recommended_action: "wire_to_main_path",
        });
    }
    // 规则 4: 影响交付结论但无可观测证据 → hard_fail
    if (contract.status === "integrated" &&
        !hasObservability(contract) &&
        (capabilityKind === "contract" || capabilityKind === "guard" || capabilityKind === "verifier")) {
        findings.push({
            severity: "hard_fail",
            module_path: path,
            reason: `新增代码影响交付结论但无可观测证据: ${path}`,
            recommended_action: "add_entry_trace",
        });
    }
    // 规则 5: 无集成测试 → hard_fail (experimental_isolated 降级 warning)
    if (!hasIntegrationTest(contract) && contract.status !== "experimental_isolated") {
        debug("主链路接入", "孤岛门禁 — 规则5命中: " + path + " 缺少集成测试");
        findings.push({
            severity: "hard_fail",
            module_path: path,
            reason: `模块 ${path} 缺少集成/契约测试`,
            recommended_action: "add_integration_test",
        });
    }
    // 规则 6: experimental_isolated 无入口 → warning (not hard_fail)
    if (contract.status === "experimental_isolated" && !hasEntryTrace(contract)) {
        findings.push({
            severity: "warning",
            module_path: path,
            reason: `experimental 代码 ${path} 无入口但已明确隔离`,
            recommended_action: "mark_experimental",
        });
    }
    // 规则 7: dead_code_candidate
    if (contract.status === "dead_code_candidate") {
        findings.push({
            severity: "warning",
            module_path: path,
            reason: `模块 ${path} 为 dead_code_candidate`,
            recommended_action: "retire_dead_code",
        });
    }
    debug("主链路接入", "孤岛门禁完成 — " + path + " 发现 " + findings.length + " 个问题");
    return findings;
}
/**
 * 集成审计。
 */
export function auditIntegration(contracts, capabilityKinds) {
    debug("主链路接入", `开始集成审计 — 共 ${contracts.length} 个契约`);
    const report = {
        orphan_modules: [],
        registry_only_modules: [],
        test_only_modules: [],
        unreachable_capabilities: [],
        missing_observability: [],
        required_entry_traces: [],
        findings: [],
    };
    for (const c of contracts) {
        const kind = capabilityKinds?.get(c.module_path);
        const gateFindings = orphanGate(c, kind);
        report.findings.push(...gateFindings);
        if (c.production_imports.length === 0 && c.integration_tests.length > 0) {
            report.orphan_modules.push(c.module_path);
        }
        if (c.production_imports.length === 0 && c.entry_traces.length === 0) {
            report.registry_only_modules.push(c.module_path);
        }
        if (!hasProductionImport(c) && c.integration_tests.length > 0) {
            report.test_only_modules.push(c.module_path);
        }
        if (!hasEntryTrace(c) && c.status !== "experimental_isolated") {
            report.unreachable_capabilities.push(c.module_path);
        }
        if (!hasObservability(c) && c.status === "integrated") {
            report.missing_observability.push(c.module_path);
        }
        if (!hasEntryTrace(c) && c.status !== "integrated" && c.status !== "experimental_isolated") {
            report.required_entry_traces.push({
                entry_type: "cli",
                entry_id: `required-for-${c.module_path}`,
                call_chain: [],
                trigger_example: `需要为 ${c.module_path} 建立入口链路`,
            });
        }
    }
    debug("主链路接入", "集成审计完成 — 孤岛 " + report.orphan_modules.length + ", 注册表 " + report.registry_only_modules.length + ", 仅测试 " + report.test_only_modules.length + ", 不可达 " + report.unreachable_capabilities.length);
    return report;
}
/**
 * 治理验证。
 */
export function validateMainPathIntegration(options) {
    const findings = [];
    debug("主链路接入", `开始治理验证 — 共 ${options.contracts.length} 个契约`);
    for (const c of options.contracts) {
        const kind = options.capabilityKinds?.get(c.module_path);
        if (c.module_path.startsWith("src/") && !hasProductionImport(c) && c.integration_tests.length > 0 && !isExemptFile(c.module_path).exempt) {
            findings.push({
                severity: "hard_fail",
                rule: "gc-test-only-module",
                module_path: c.module_path,
                message: `新增 src/ 生产代码 ${c.module_path} 只被测试导入，必须 hard_fail`,
            });
        }
        if (kind &&
            (kind === "contract" || kind === "guard" || kind === "verifier" || kind === "workflow") &&
            c.status !== "experimental_isolated" &&
            !hasEntryTrace(c)) {
            findings.push({
                severity: "hard_fail",
                rule: "gc-enforced-no-entry",
                module_path: c.module_path,
                message: `新增 enforced ${kind} ${c.module_path} 没有入口链路和真实编排路径`,
            });
        }
        if (c.status === "integrated" &&
            !hasProductionImport(c) &&
            !hasEntryTrace(c)) {
            findings.push({
                severity: "hard_fail",
                rule: "gc-registry-only-completion",
                module_path: c.module_path,
                message: `模块 ${c.module_path} 只有 registry/index export 引用，不得标记为 integrated`,
            });
        }
        if (c.status === "integrated" &&
            !hasIntegrationTest(c) &&
            !isExemptFile(c.module_path).exempt) {
            findings.push({
                severity: "hard_fail",
                rule: "gc-no-integration-test",
                module_path: c.module_path,
                message: `已接入模块 ${c.module_path} 必须有集成测试证明真实入口能触发`,
            });
        }
        if (c.status === "integrated" &&
            !hasObservability(c) &&
            kind &&
            (kind === "contract" || kind === "guard" || kind === "verifier" || kind === "diagnostic_code" || kind === "governance_report")) {
            findings.push({
                severity: "hard_fail",
                rule: "gc-no-observability",
                module_path: c.module_path,
                message: `新增 ${kind} ${c.module_path} 缺少可观测痕迹`,
            });
        }
        if (kind === "diagnostic_code" && !hasEntryTrace(c)) {
            findings.push({
                severity: "hard_fail",
                rule: "gc-diagnostic-no-path",
                module_path: c.module_path,
                message: `新增诊断码 ${c.module_path} 无法由真实路径产出`,
            });
        }
        if (kind === "workflow" && c.status !== "experimental_isolated" && !hasEntryTrace(c)) {
            findings.push({
                severity: "hard_fail",
                rule: "gc-workflow-no-orchestration",
                module_path: c.module_path,
                message: `新增 workflow ${c.module_path} 没有实际编排路径`,
            });
        }
        if (kind === "cli_command" && !c.entry_traces.some(t => t.entry_type === "cli")) {
            findings.push({
                severity: "hard_fail",
                rule: "gc-cli-no-bin",
                module_path: c.module_path,
                message: `新增 CLI 能力 ${c.module_path} 必须接入 bin/soloforge`,
            });
        }
        if (c.status === "experimental_isolated" && !hasEntryTrace(c)) {
            findings.push({
                severity: "warning",
                rule: "gc-advisory-no-entry",
                module_path: c.module_path,
                message: `advisory/experimental 能力 ${c.module_path} 有 registry 但暂无入口`,
            });
        }
        if (c.status === "implemented_not_integrated") {
            findings.push({
                severity: "warning",
                rule: "gc-not-integrated-status",
                module_path: c.module_path,
                message: `模块 ${c.module_path} 状态为 implemented_not_integrated，不能标记完成`,
            });
        }
    }
    debug("主链路接入", `治理验证完成 — 发现 ${findings.length} 个问题`);
    return findings;
}
/**
 * 验证交付答案。
 */
export function validateDeliveryAnswers(answers) {
    const findings = [];
    for (const mod of answers.new_modules) {
        if (answers.called_by.length === 0) {
            findings.push({
                severity: "hard_fail",
                module_path: mod,
                reason: `新增模块 ${mod} 没有被任何生产代码调用`,
                recommended_action: "wire_to_main_path",
            });
        }
        if (!answers.user_trigger) {
            findings.push({
                severity: "hard_fail",
                module_path: mod,
                reason: `新增模块 ${mod} 没有用户触发路径`,
                recommended_action: "add_entry_trace",
            });
        }
    }
    for (const f of answers.test_only_files) {
        findings.push({
            severity: "hard_fail",
            module_path: f,
            reason: `文件 ${f} 只被测试导入`,
            recommended_action: "wire_to_main_path",
        });
    }
    for (const r of answers.registered_not_executed) {
        findings.push({
            severity: "hard_fail",
            module_path: r,
            reason: `能力 ${r} 只注册未执行`,
            recommended_action: "wire_to_main_path",
        });
    }
    for (const s of answers.non_integrated_statuses) {
        if (s === "implemented_not_integrated" || s === "dead_code_candidate") {
            findings.push({
                severity: "warning",
                module_path: "*",
                reason: `存在 ${s} 状态的模块`,
                recommended_action: "wire_to_main_path",
            });
        }
    }
    return findings;
}
//# sourceMappingURL=gates.js.map