/**
 * 日志治理与用户可见输出检查。
 *
 * 面向 SoloForge 自身输出路径，补足统一 logger 之外的行为级约束。
 */
import { safeJsonParse } from "./helpers.js";
const INTERNAL_NOISE_PATTERNS = [
    /\[soloForge\].*(debug|trace|internal)/i,
    /validate-release|BATCH\d/i,
    /raw_prompt|authorization_token|sk-[A-Za-z0-9_-]{8,}/i,
];
export function classifyOutputVisibility(content) {
    const trimmed = content.trim();
    if (!trimmed)
        return "user_visible";
    if ((trimmed.startsWith("{") && trimmed.endsWith("}")) || (trimmed.startsWith("[") && trimmed.endsWith("]"))) {
        if (safeJsonParse(trimmed) !== undefined) {
            return "json";
        }
        return "user_visible";
    }
    return INTERNAL_NOISE_PATTERNS.some((p) => p.test(content)) ? "internal_noise" : "user_visible";
}
export function assessOutputNoise(outputs) {
    const findings = [];
    for (const output of outputs) {
        if (classifyOutputVisibility(output) === "internal_noise") {
            findings.push({
                severity: "hard_fail",
                kind: "raw_noise",
                message_zh: "用户可见输出包含内部治理噪音或敏感信息",
                evidence: output.slice(0, 160),
            });
        }
    }
    return { passed: findings.length === 0, findings };
}
export function validateLogFormat(entry) {
    const findings = [];
    if (typeof entry === "string") {
        if (classifyOutputVisibility(entry) === "internal_noise") {
            findings.push({ severity: "hard_fail", kind: "internal_leak", message_zh: "日志字符串包含内部噪音", evidence: entry });
        }
        return { passed: findings.length === 0, findings };
    }
    if (!entry || typeof entry !== "object") {
        return { passed: false, findings: [{ severity: "hard_fail", kind: "format_violation", message_zh: "日志条目必须是字符串或对象" }] };
    }
    const obj = entry;
    if (!obj.level || !obj.message) {
        findings.push({ severity: "warning", kind: "format_violation", message_zh: "结构化日志缺少 level 或 message 字段" });
    }
    return { passed: findings.every((f) => f.severity !== "hard_fail"), findings };
}
export function enforceLogGovernance(output) {
    const outputs = Array.isArray(output) ? output : [output];
    return assessOutputNoise(outputs);
}
export function evaluateFeedbackOutputQuality(params) {
    const findings = [...assessOutputNoise([params.output]).findings];
    if (params.json_mode) {
        if (safeJsonParse(params.output) === undefined) {
            findings.push({ severity: "hard_fail", kind: "format_violation", message_zh: "--json 模式 stdout 必须是纯 JSON" });
        }
    }
    if (params.critical_failure_hidden) {
        findings.push({ severity: "hard_fail", kind: "internal_leak", message_zh: "用户反馈隐藏了关键失败" });
    }
    return { passed: findings.every((f) => f.severity !== "hard_fail"), findings };
}
//# sourceMappingURL=log_governance.js.map