/**
 * 问题十七: State Fact Classifier — TaskContext 事实分区治理。
 *
 * 将 TaskContext 数据分为三类:
 * - engineering_fact: 可恢复工程事实（文件路径、git 状态、测试结果、验收标准）
 * - ai_inference: AI 推理结果（分类、意图分析、知识匹配、路由决策）
 * - prompt_content: Prompt 内容（发送给 AI 的原始文本、chain_of_thought）
 *
 * 核心规则:
 * - AI 推理不得作为工程事实保存
 * - raw_prompt / chain_of_thought 不得进入 engineering_fact
 * - 违规必须 hard fail 或阻断保存
 */
// 字段 → 分区映射
const FIELD_CATEGORY_MAP = {
    // engineering_fact: 可验证的工程事实
    task_id: "engineering_fact",
    status: "engineering_fact",
    project_path: "engineering_fact",
    config: "engineering_fact",
    scope: "engineering_fact",
    acceptance: "engineering_fact",
    changed_files: "engineering_fact",
    test_results: "engineering_fact",
    verification_evidence: "engineering_fact",
    artifacts: "engineering_fact",
    lease: "engineering_fact",
    created_at: "engineering_fact",
    updated_at: "engineering_fact",
    delivery_report: "engineering_fact",
    plan_proposal_gate: "engineering_fact",
    core_experience_evaluation: "engineering_fact",
    failure_report: "engineering_fact",
    degradation_event: "engineering_fact",
    governance_report: "engineering_fact",
    // ai_inference: AI 推理结果
    classification: "ai_inference",
    route_decision: "ai_inference",
    matched_knowledge: "ai_inference",
    matched_patterns: "ai_inference",
    uncertainty_triggers: "ai_inference",
    circuit_breaker: "ai_inference",
    legacy_conventions: "ai_inference",
    ingestion_evidences: "ai_inference",
    prompt_injection_plan: "ai_inference",
    sensitivity_labels: "ai_inference",
    redaction_records: "ai_inference",
    privacy_findings: "ai_inference",
    config_resolution_reports: "ai_inference",
    workflow_trace: "ai_inference",
    output_artifact_record: "ai_inference",
    // prompt_content: Prompt 原始内容
    prompt: "prompt_content",
    expand_result: "prompt_content",
};
// 禁止进入 engineering_fact 的关键词
const ENGINEERING_FACT_BLOCKED_PATTERNS = [
    /chain_of_thought/i,
    /raw_prompt/i,
    /raw_stdout/i,
    /raw_stderr/i,
    /ai_generated/i,
    /inferred/i,
];
/**
 * 获取字段的分区类别。
 */
export function classifyField(key) {
    return FIELD_CATEGORY_MAP[key] ?? "ai_inference"; // 未知字段默认 ai_inference
}
/**
 * 验证 TaskContext 数据分区合规性。
 * 检查所有字段是否符合分区规则。
 */
export function validateStateFactPartition(data) {
    const violations = [];
    for (const [key, value] of Object.entries(data)) {
        const category = classifyField(key);
        // engineering_fact 字段不得包含 AI 推理内容
        if (category === "engineering_fact" && value !== null && value !== undefined) {
            const strValue = typeof value === "string" ? value : JSON.stringify(value);
            if (strValue) {
                for (const pattern of ENGINEERING_FACT_BLOCKED_PATTERNS) {
                    if (pattern.test(strValue)) {
                        violations.push({
                            severity: "hard_fail",
                            key,
                            expected_category: "engineering_fact",
                            actual_category: "prompt_content",
                            message_zh: `工程事实字段 "${key}" 包含禁止内容 (${pattern.source}): 不得将 AI 原始输出保存为工程事实`,
                        });
                    }
                }
            }
        }
        // prompt_content 字段不得包含结构化工程数据
        if (category === "prompt_content" && typeof value === "object" && value !== null) {
            const obj = value;
            if (obj.test_results || obj.acceptance || obj.changed_files) {
                violations.push({
                    severity: "warning",
                    key,
                    expected_category: "prompt_content",
                    actual_category: "engineering_fact",
                    message_zh: `Prompt 内容字段 "${key}" 包含工程事实数据: 建议将工程数据与 Prompt 分离`,
                });
            }
        }
    }
    return violations;
}
/**
 * 检查指定字段是否允许写入目标分区。
 * 用于 TaskContext save 前的实时校验。
 */
export function isWriteAllowed(key, targetCategory, value) {
    const sourceCategory = classifyField(key);
    // AI 推理写入 engineering_fact 槽位 → 阻断
    if (targetCategory === "engineering_fact" && sourceCategory === "ai_inference") {
        return {
            allowed: false,
            reason_zh: `禁止将 AI 推理字段 "${key}" 写入工程事实分区`,
        };
    }
    // prompt_content 写入 engineering_fact → 阻断
    if (targetCategory === "engineering_fact" && sourceCategory === "prompt_content") {
        return {
            allowed: false,
            reason_zh: `禁止将 Prompt 内容字段 "${key}" 写入工程事实分区`,
        };
    }
    // 工程事实字段中的禁止内容 → 阻断
    if (targetCategory === "engineering_fact" && value !== null && value !== undefined) {
        const strValue = typeof value === "string" ? value : JSON.stringify(value);
        if (strValue) {
            for (const pattern of ENGINEERING_FACT_BLOCKED_PATTERNS) {
                if (pattern.test(strValue)) {
                    return {
                        allowed: false,
                        reason_zh: `工程事实字段 "${key}" 包含禁止模式 (${pattern.source})`,
                    };
                }
            }
        }
    }
    return { allowed: true };
}
/**
 * 获取字段分区摘要（用于诊断输出）。
 */
export function getStateFactSummary(data) {
    const result = { engineering_fact: [], ai_inference: [], prompt_content: [] };
    for (const key of Object.keys(data)) {
        result[classifyField(key)].push(key);
    }
    return result;
}
//# sourceMappingURL=state_fact_classifier.js.map