/**
 * @deprecated [DC-1] 被 developer_sovereignty.ts 和 delivery.ts 消费，标记待重构。
 *
 * 语义交付证据 — R9 需求→语义 diff→断言→变异证据链。
 * CAP-011 advisory: 不 hard block，只产出 advisory 报告。
 *
 * 职责:
 * - computeSemanticEvidence: 从变更文件 + 需求点计算语义证据
 * - buildDeliveryEvidenceChain: 从任务上下文构建完整交付证据链
 */
import { debug } from "../core/logger.js";
/**
 * 从任务上下文计算语义证据。
 * 基于变更文件和需求点，推断变更行为和需求覆盖状态。
 * @param changedFiles - 变更文件路径列表
 * @param requirementPoints - 需求点列表
 * @param acceptanceResults - 可选的验收测试结果列表
 * @returns 语义证据对象
 */
export function computeSemanticEvidence(changedFiles, requirementPoints, acceptanceResults) {
    debug("SemanticEvidence", "计算语义证据", "文件数:", changedFiles.length, "需求点:", requirementPoints.length);
    const changedBehaviors = changedFiles.map((f) => {
        const matchedReq = requirementPoints.find((rp) => rp.acceptance_test_ids?.some((tid) => f.toLowerCase().includes(tid.toLowerCase().replace(/[^a-z0-9]/g, ""))));
        return {
            file_path: f,
            description: `modified: ${f}`,
            requirement_point_id: matchedReq?.id,
        };
    });
    const requirementCoverage = requirementPoints.map((rp) => {
        const hasMatchingFile = changedBehaviors.some((cb) => cb.requirement_point_id === rp.id);
        const acceptancePassed = acceptanceResults?.filter((ar) => rp.acceptance_test_ids?.includes(ar.id));
        const allAcceptancePassed = acceptancePassed
            ? acceptancePassed.length > 0 && acceptancePassed.every((a) => a.passed)
            : false;
        const covered = hasMatchingFile || allAcceptancePassed;
        return {
            requirement_point_id: rp.id,
            covered,
            evidence: covered
                ? hasMatchingFile
                    ? "file change matched"
                    : "acceptance tests passed"
                : "no matching evidence",
        };
    });
    return {
        changed_behaviors: changedBehaviors,
        unchanged_assertions: [],
        requirement_coverage: requirementCoverage,
    };
}
/**
 * 从任务上下文构建完整交付证据链。
 * 聚合 expand 的需求点、verify 的语义证据、execution 的变更文件。
 * @param taskContext - 任务上下文
 * @returns 完整交付证据链，无需求点且无变更文件时返回 null
 */
export function buildDeliveryEvidenceChain(taskContext) {
    debug("SemanticEvidence", "构建交付证据链");
    const requirementPoints = taskContext.expansion?.requirement_points ?? [];
    const semanticEvidence = taskContext.verification?.semantic_evidence;
    const changedFiles = taskContext.execution?.changed_files ?? [];
    if (requirementPoints.length === 0 && changedFiles.length === 0) {
        debug("SemanticEvidence", "无需求点且无变更文件，无法构建证据链");
        return null;
    }
    const fileToRequirements = changedFiles.map((f) => ({
        file_path: f,
        requirement_point_ids: requirementPoints
            .filter((rp) => semanticEvidence?.changed_behaviors.some((cb) => cb.file_path === f && cb.requirement_point_id === rp.id))
            .map((rp) => rp.id),
    }));
    const uncoveredPoints = requirementPoints
        .filter((rp) => {
        const coverage = semanticEvidence?.requirement_coverage?.find((rc) => rc.requirement_point_id === rp.id);
        return !coverage?.covered;
    })
        .map((rp) => rp.id);
    return {
        requirement_points: requirementPoints,
        semantic_evidence: semanticEvidence ?? undefined,
        file_to_requirements: fileToRequirements,
        completeness: {
            all_requirements_covered: uncoveredPoints.length === 0,
            uncovered_points: uncoveredPoints,
            advisory: uncoveredPoints.length > 0
                ? `advisory: ${uncoveredPoints.length} requirement point(s) not covered by evidence: ${uncoveredPoints.join(", ")}`
                : "all requirement points covered",
        },
    };
}
//# sourceMappingURL=semantic_evidence.js.map