/**
 * 任务上下文 — 膨胀隐私扫描辅助函数
 *
 * 从 setExpansion 中提取的隐私/敏感性扫描逻辑。
 */
import { scanTextSensitivity } from "../../audit/privacy_secret_contract.js";
/**
 * 扫描膨胀结果中的隐私/敏感信息，执行脱敏处理。
 * 返回处理后的 result、收集的 labels 和 redaction records。
 */
export function scanExpansionForPrivacy(result) {
    const allLabels = [];
    const scanField = (text, ref) => {
        const labels = scanTextSensitivity(text, ref);
        allLabels.push(...labels);
        return labels;
    };
    const blockOnForbidden = (labels, _ref) => {
        const forbidden = labels.filter((l) => l.label === "secret" || l.label === "credential");
        if (forbidden.length > 0) {
            throw new Error(`TaskContext.setExpansion 阻断: 膨胀结果包含禁止级别的敏感信息 (${forbidden.map((l) => l.label).join(", ")}), 不允许保存`);
        }
    };
    const summarizeField = (text) => {
        const labels = scanTextSensitivity(text, "summarize");
        const needsRedact = labels.some((l) => l.label === "pii" || l.label === "customer_data" || l.label === "production_data");
        if (!needsRedact)
            return text;
        const detected = labels.map((l) => l.label).join(",");
        return `[摘要]检测到${detected}敏感内容，已脱敏`;
    };
    // 1. 扫描 prompt
    const promptLabels = scanField(result.prompt, "expansion_prompt");
    blockOnForbidden(promptLabels, "expansion_prompt");
    const promptSensitive = promptLabels.filter((l) => l.label === "pii" || l.label === "customer_data" || l.label === "production_data");
    const promptRedactionRecords = [];
    if (promptSensitive.length > 0) {
        const redactedPrompt = `[摘要]检测到${promptSensitive.map((l) => l.label).join(",")}敏感内容，已脱敏`;
        result = { ...result, prompt: redactedPrompt };
        promptRedactionRecords.push({
            source_ref: "expansion_prompt",
            redaction_type: "pii_mask",
            after_hash: redactedPrompt,
            fields_redacted: promptSensitive.map((l) => l.label),
        });
    }
    // 2. 扫描 ingestion_evidences
    if (result.ingestion_evidences?.length) {
        for (const ev of result.ingestion_evidences) {
            if (ev.source_ref) {
                const evLabels = scanField(ev.source_ref, "ingestion_evidence_source_ref");
                blockOnForbidden(evLabels, "ingestion_evidence_source_ref");
            }
        }
    }
    // 3. 清理 output_artifact_record
    if (result.output_artifact_record) {
        const oar = result.output_artifact_record;
        if (oar.path) {
            oar.path = summarizeField(oar.path);
        }
        if (oar.source_material_refs?.length) {
            oar.source_material_refs = oar.source_material_refs.map((ref) => summarizeField(ref));
        }
    }
    // 4. 清理 prompt_injection_plan
    if (result.prompt_injection_plan?.evidence_refs?.length) {
        result.prompt_injection_plan.evidence_refs =
            result.prompt_injection_plan.evidence_refs.map((ref) => summarizeField(ref));
    }
    // 5. 清理 privacy_findings
    if (result.privacy_findings?.length) {
        result.privacy_findings = result.privacy_findings.map((pf) => ({
            ...pf,
            source_ref: summarizeField(pf.source_ref ?? ""),
            rule: summarizeField(pf.rule ?? ""),
            message: summarizeField(pf.message ?? ""),
        }));
    }
    return { result, allLabels, promptRedactionRecords };
}
//# sourceMappingURL=expansion_privacy.js.map