/**
 * 降级契约 — 问题十八 (Degradation Contract)。
 *
 * 核心规则:
 * - DegradationEvent 必须用户可见
 * - degraded 不得标记 completed
 * - 恢复 full mode 必须经过 RecoveryGate
 * - safety/enforcement 类组件不可降级为允许写入
 */
import { createDiagnosticEvent } from "./diagnostic_registry.js";
// 不可降级的组件（safety/enforcement 类）
const NON_DEGRADABLE_COMPONENTS = ["enforcement_guard"];
/**
 * 创建降级事件。
 */
export function createDegradationEvent(taskId, component, previousMode, reason, missingCapabilities) {
    // safety/enforcement 类组件不可降级
    if (NON_DEGRADABLE_COMPONENTS.includes(component)) {
        return null;
    }
    const diagEvent = createDiagnosticEvent("SF-DEGRADE-0001", { component, reason });
    const currentMode = previousMode === "full" ? "degraded" : "unavailable";
    return {
        event_id: `de-${taskId}-${Date.now()}`,
        task_id: taskId,
        component,
        previous_mode: previousMode,
        current_mode: currentMode,
        reason,
        user_visible_message: `${component} 不可用，当前为降级模式。${reason}`,
        missing_capabilities: missingCapabilities,
        risk_description: `降级后 ${missingCapabilities.join("、")} 不可用`,
        next_action: "等待恢复后重新验证，或人工确认",
        diagnostic_event: diagEvent,
        occurred_at: new Date().toISOString(),
    };
}
/**
 * 评估恢复门。
 * 从降级恢复到 full mode 必须通过所有检查。
 */
export function evaluateRecoveryGate(taskId, component, checks) {
    const allPassed = checks.every((c) => c.passed);
    return {
        gate_id: `rg-${taskId}-${Date.now()}`,
        task_id: taskId,
        component,
        checks,
        all_passed: allPassed,
        evaluated_at: new Date().toISOString(),
    };
}
/**
 * 验证降级规则。
 */
export function validateDegradation(event) {
    const errors = [];
    if (event.current_mode === "degraded" || event.current_mode === "unavailable") {
        if (!event.user_visible_message || event.user_visible_message.trim() === "") {
            errors.push({
                error: "降级事件必须有用户可见消息",
                severity: "hard_fail",
            });
        }
        if (event.missing_capabilities.length === 0) {
            errors.push({
                error: "降级事件必须列出缺失能力",
                severity: "warning",
            });
        }
    }
    return errors;
}
//# sourceMappingURL=degradation.js.map