/**
 * 冲突合并门 — 统一 diff ownership CAS 冲突 + scope lease 冲突的 advisory 报告。
 * CAP-011 实验性: advisory only, 不 hard block。
 *
 * 职责:
 * - preWriteGate: 写前统一检查 CAS + Lease 冲突
 * - 返回 ConflictGateResult 含所有冲突和建议操作
 * - human_confirm: 标记人工确认（advisory, 可忽略）
 */
import { debug } from "../core/logger.js";
export class ConflictGate {
    diffOwnership;
    scopeLease;
    constructor(diffOwnership, scopeLease) {
        this.diffOwnership = diffOwnership;
        this.scopeLease = scopeLease;
    }
    /**
     * 写前统一检查 CAS + 租约冲突。
     *
     * @param taskId - 任务 ID
     * @param filePath - 待写入文件路径
     * @returns 冲突检测结果
     */
    preWriteGate(taskId, filePath) {
        debug("冲突门禁", "preWriteGate() — taskId:", taskId, "filePath:", filePath);
        const entries = [];
        // CAS 检查
        const { conflict } = this.diffOwnership.preWriteCheck(taskId, filePath);
        if (conflict) {
            entries.push({
                source: "cas",
                severity: "advisory",
                file_path: filePath,
                message: conflict.message,
                detail: conflict,
            });
        }
        // 租约检查
        const leaseConflict = this.scopeLease.queryLease(taskId, filePath);
        if (leaseConflict) {
            entries.push({
                source: "lease",
                severity: "advisory",
                file_path: filePath,
                message: leaseConflict.message,
                detail: leaseConflict,
            });
        }
        const hasConflict = entries.length > 0;
        if (hasConflict) {
            debug("冲突门禁", "写前冲突检测 —", filePath, "冲突数:", entries.length);
        }
        const recommendedAction = inferRecommendedAction(entries);
        const summary = hasConflict
            ? entries.map(e => `[${e.source}] ${e.message}`).join("; ")
            : "no conflicts";
        return {
            file_path: filePath,
            has_conflict: hasConflict,
            entries,
            recommended_action: recommendedAction,
            summary,
            confirmed_at: null,
        };
    }
    /**
     * 标记冲突结果为已确认。
     *
     * @param result - 冲突检测结果
     * @returns 更新确认时间后的结果
     */
    markConfirmed(result) {
        debug("冲突门禁", "markConfirmed() — filePath:", result.file_path);
        return {
            ...result,
            confirmed_at: new Date().toISOString(),
        };
    }
}
function inferRecommendedAction(entries) {
    if (entries.length === 0)
        return "proceed";
    const hasCas = entries.some(e => e.source === "cas");
    const hasLease = entries.some(e => e.source === "lease");
    if (hasCas && hasLease)
        return "escalate";
    if (hasCas)
        return "confirm_recommended";
    return "confirm_recommended";
}
//# sourceMappingURL=conflict_gate.js.map