/**
 * 问题十六: Technology Decision Sovereignty — 技术决策主权。
 *
 * 强制执行:
 * - destructive / irreversible 技术决策必须 human gate
 * - 技术推荐不等于可执行决策
 */
/** 需要 human gate 的决策范围 */
const HUMAN_GATE_SCOPES = new Set([
    "architecture",
    "infrastructure",
    "data_store",
    "security_or_compliance",
]);
/**
 * 创建技术决策契约。
 */
export function createTechnologyDecision(overrides) {
    return {
        decision_scope: "local_implementation",
        options: [],
        recommended_option: "",
        rejected_options: [],
        evidence: [],
        falsification: [],
        failure_conditions: [],
        validation_plan: [],
        rollback_or_exit_plan: [],
        human_gate_required: false,
        executable_without_human: true,
        ...overrides,
    };
}
/**
 * 验证技术决策契约完整性。
 */
export function validateTechnologyDecision(contract) {
    const violations = [];
    const warnings = [];
    if ((contract.options?.length ?? 0) === 0) {
        violations.push("技术决策必须提供至少一个选项");
    }
    if (!contract.recommended_option) {
        violations.push("技术决策必须提供推荐选项");
    }
    if (contract.recommended_option && contract.options && !contract.options.some(o => o.option_id === contract.recommended_option)) {
        violations.push("推荐选项不在选项列表中");
    }
    if ((contract.evidence?.length ?? 0) === 0) {
        warnings.push("技术决策缺少证据支撑");
    }
    if ((contract.falsification?.length ?? 0) === 0) {
        warnings.push("技术决策缺少证伪条件");
    }
    if ((contract.failure_conditions?.length ?? 0) === 0) {
        warnings.push("技术决策缺少失败条件");
    }
    // 检查是否需要 human gate
    if (HUMAN_GATE_SCOPES.has(contract.decision_scope)) {
        if (!contract.human_gate_required) {
            violations.push(`${contract.decision_scope} 范围的决策必须 human gate`);
        }
        if (contract.executable_without_human) {
            violations.push(`${contract.decision_scope} 范围的决策不得标记为 executable_without_human`);
        }
    }
    const result = { valid: violations.length === 0, violations, warnings };
    if (!result.valid) {
        result.expected_schema = {
            decision_id: "string — 唯一标识，如 TECH-001",
            decision_scope: "local_implementation | library_choice | architecture | infrastructure | data_store | external_service | security_or_compliance",
            options: [{ option_id: "string", title: "string", description: "string" }],
            recommended_option: "string — 必须匹配 options[].option_id",
            rejected_options: [{ option: "string", reason: "string" }],
            evidence: [{ claim: "string", supporting_facts: ["string"] }],
            falsification: ["string — 可证伪的条件"],
            failure_conditions: ["string — 失败条件"],
            validation_plan: ["string — 验证步骤"],
            rollback_or_exit_plan: ["string — 回滚方案"],
            human_gate_required: "boolean — architecture/infrastructure/data_store/security_or_compliance 范围必须为 true",
            executable_without_human: "boolean — 上述范围必须为 false",
        };
    }
    return result;
}
/**
 * 评估决策是否可执行。
 * - 推荐不等于可执行
 * - 高范围决策需要 human gate
 */
export function evaluateDecisionExecution(contract, record) {
    // 技术推荐不等于可执行决策
    if (!record || record.decision_status === "recommended") {
        return {
            allowed: false,
            requires_human_gate: HUMAN_GATE_SCOPES.has(contract.decision_scope),
            executable_without_human: false,
            reason: "技术推荐不等于可执行决策，需要显式批准",
        };
    }
    // 需要人类审批但未获得
    if (contract.human_gate_required && record.decision_status !== "approved" && record.decision_status !== "implemented") {
        return {
            allowed: false,
            requires_human_gate: true,
            executable_without_human: false,
            reason: `${contract.decision_scope} 范围决策需要人类审批，当前状态: ${record.decision_status}`,
        };
    }
    // 已批准或已实现
    if (record.decision_status === "approved" || record.decision_status === "implemented") {
        return {
            allowed: true,
            requires_human_gate: false,
            executable_without_human: contract.executable_without_human,
            reason: "决策已批准，可执行",
        };
    }
    return {
        allowed: false,
        requires_human_gate: contract.human_gate_required,
        executable_without_human: false,
        reason: `决策状态不允许执行: ${record.decision_status}`,
    };
}
/**
 * 创建决策记录。
 */
export function createDecisionRecord(decisionId, overrides) {
    return {
        decision_id: decisionId,
        decision_status: "recommended",
        evidence_ids: [],
        ...overrides,
    };
}
//# sourceMappingURL=technology_decision.js.map