/**
 * @deprecated [DC-1] 无运行时消费方。保留供未来抽样策略实现。
 *
 * 风险加权抽样决策 — 基于 audit pool 中的 AuditItem 决定是否抽检。
 * P2-2 范围: 只输出抽样决策，不生成逃逸报告，不改能力状态。
 *
 * 抽样率:
 * - low:      20% baseline
 * - medium:   50%
 * - high:     80%
 * - critical: 100%
 *
 * 可复现: 同 seed + 同 task_id → 同决策。
 */
import { createHash } from "node:crypto";
import { debug } from "../core/logger.js";
const RISK_THRESHOLDS = {
    low: 20,
    medium: 50,
    high: 80,
    critical: 100,
};
function computeHash(taskId, seed) {
    const h = createHash("sha256").update(`${taskId}:${seed}`).digest();
    return h.readUInt32BE(0) % 100;
}
function collectRiskFactors(risk, changedFileCount, hasCapabilityRefs) {
    const factors = [];
    factors.push({
        factor: "risk_level",
        detail: `task risk is ${risk}, threshold ${RISK_THRESHOLDS[risk]}%`,
        impact: risk === "low" ? "decreases" : risk === "critical" ? "increases" : "neutral",
    });
    if (changedFileCount > 10) {
        factors.push({
            factor: "large_change_set",
            detail: `${changedFileCount} files changed (>10)`,
            impact: "increases",
        });
    }
    if (hasCapabilityRefs) {
        factors.push({
            factor: "capability_coverage",
            detail: "task touches registered capabilities",
            impact: "increases",
        });
    }
    return factors;
}
/**
 * 执行 shouldSample 操作
 */
export function shouldSample(item, seed = 0) {
    debug("风险采样", "shouldSample() 开始, task_id=${item.task_id}, risk=${item.risk}");
    const threshold = RISK_THRESHOLDS[item.risk];
    const hashValue = computeHash(item.task_id, seed);
    const shouldSample = hashValue < threshold;
    const riskFactors = collectRiskFactors(item.risk, item.changed_files.length, item.capability_refs.length > 0);
    debug("风险采样", "hash=${hashValue}, threshold=${threshold}, should_sample=${shouldSample}");
    const reason = shouldSample
        ? `sampled: hash ${hashValue} < threshold ${threshold} (risk=${item.risk})`
        : `skipped: hash ${hashValue} >= threshold ${threshold} (risk=${item.risk})`;
    return {
        item_task_id: item.task_id,
        item_risk: item.risk,
        should_sample: shouldSample,
        seed,
        threshold_pct: threshold,
        hash_value: hashValue,
        risk_factors: riskFactors,
        reason,
    };
}
/**
 * 执行 getThreshold 操作
 */
export function getThreshold(risk) {
    debug("风险采样", "getThreshold() risk=${risk}, threshold=${RISK_THRESHOLDS[risk]}");
    return RISK_THRESHOLDS[risk];
}
//# sourceMappingURL=risk_sampler.js.map