/**
 * 问题十九: Metric / Governance Action Binding — 指标契约与治理动作绑定。
 *
 * 强制执行:
 * - metrics 不得把 skipped / manual_required / degraded / plan_only 算 passed
 * - governance action 不得由指标自动触发高风险动作
 */
/** 不得算作 passed 的样本状态 */
const NON_PASS_STATUSES = new Set(["skipped", "manual_required", "degraded", "plan_only"]);
/** 高风险动作不得自动触发 */
const HIGH_RISK_ACTIONS = new Set(["downgrade", "disable", "require_contract_fix"]);
/**
 * 创建指标契约。
 */
export function createMetricContract(overrides) {
    return {
        metric_type: "runtime",
        owner_component: "unknown",
        definition: "",
        collection_source: [],
        sampling_window: "daily",
        thresholds: {},
        action_binding: [],
        metric_status: "active",
        ...overrides,
    };
}
/**
 * 验证指标样本 — 禁止将 skipped/degraded/manual_required/plan_only 算作 passed。
 */
export function validateMetricSamples(samples) {
    const violations = [];
    for (const sample of samples) {
        if (sample.sample_status === "passed") {
            // 已通过的不需要额外检查
            continue;
        }
        if (NON_PASS_STATUSES.has(sample.sample_status)) {
            // 这些状态不允许被误算为 passed，但我们只检查一致性
        }
    }
    return { valid: violations.length === 0, violations };
}
/**
 * 评估指标样本集合 — 真实计算通过率，不含伪装。
 */
export function evaluateMetrics(samples) {
    let actualPassCount = 0;
    let falsifiedCount = 0;
    let degradedCount = 0;
    let skippedCount = 0;
    for (const sample of samples) {
        switch (sample.sample_status) {
            case "passed":
                actualPassCount++;
                break;
            case "skipped":
                skippedCount++;
                break;
            case "degraded":
                degradedCount++;
                break;
            case "manual_required":
                falsifiedCount++;
                break;
            case "plan_only":
                skippedCount++;
                break;
        }
    }
    const total = samples.length;
    const passed = total === 0 || (actualPassCount === total);
    return {
        passed,
        falsified_count: falsifiedCount,
        degraded_count: degradedCount,
        skipped_count: skippedCount,
        actual_pass_count: actualPassCount,
        total_count: total,
    };
}
/**
 * 创建治理发现。
 */
export function createGovernanceFinding(metric, severity, evidenceRefs) {
    const requiresHuman = severity === "critical";
    return {
        id: `gf-${metric.id}-${Date.now()}`,
        severity,
        component_id: metric.owner_component,
        metric_id: metric.id,
        evidence_refs: evidenceRefs,
        recommended_action: severity === "critical" ? "require_human_review" : "warn",
        requires_human_confirmation: requiresHuman,
        governance_action_status: "suggested",
    };
}
/**
 * 评估治理动作 — 禁止自动触发高风险动作。
 */
export function evaluateGovernanceAction(finding, action) {
    const isHighRisk = HIGH_RISK_ACTIONS.has(action);
    if (isHighRisk && finding.severity === "critical") {
        return {
            allowed: true,
            auto_triggered: false,
            requires_human: true,
            reason: "高风险动作需要人类确认，不得自动触发",
        };
    }
    if (isHighRisk) {
        return {
            allowed: false,
            auto_triggered: false,
            requires_human: true,
            reason: "非 critical 级别不允许触发高风险动作",
        };
    }
    return {
        allowed: true,
        auto_triggered: true,
        requires_human: false,
        reason: "低风险动作可自动执行",
    };
}
/**
 * 绑定治理动作到发现。
 */
export function bindGovernanceAction(finding, action, evidenceIds) {
    const evalResult = evaluateGovernanceAction(finding, action);
    return {
        finding_id: finding.id,
        action,
        action_status: evalResult.requires_human ? "manual_required" : "queued",
        evidence_ids: evidenceIds,
    };
}
//# sourceMappingURL=metric_governance.js.map