/**
 * 发布兼容契约 — 问题三十五 (Release Compatibility)。
 *
 * 管理 npm 包版本、Contract Registry 版本、项目 schema 和迁移策略。
 * - migrate 默认 dry-run，apply 必须用户确认
 * - 升级不得覆盖用户项目已有配置
 * - 破坏性/语义变更必须人工确认、备份和中文迁移报告
 */
// ── 内置兼容契约 ──
const CURRENT_COMPATIBILITY = {
    package_version: "1.2.20",
    contract_registry_version: "1",
    supported_project_schema_versions: ["1"],
    migrations: [],
    breaking_changes: [],
    deprecated_features: [],
    compatibility_policy: {
        read_old_state: true,
        auto_migrate_safe_changes: true,
        require_human_for_destructive_migration: true,
        rollback_supported: true,
    },
};
// ── 核心函数 ──
/**
 * 获取当前兼容契约。
 */
export function getCurrentCompatibility() {
    return { ...CURRENT_COMPATIBILITY };
}
/**
 * 检查项目兼容性状态。
 */
export function checkProjectCompatibility(projectSchemaVersion, contract) {
    if (contract.supported_project_schema_versions.includes(projectSchemaVersion)) {
        return "compatible";
    }
    if (contract.migrations.some((m) => m.from_version === projectSchemaVersion)) {
        if (contract.breaking_changes.some((b) => b.migration_required)) {
            return "breaking_changes";
        }
        return "migration_required";
    }
    return "incompatible";
}
/**
 * 创建项目迁移计划。
 * 硬规则：
 * - 默认 dry-run，不写文件
 * - apply 必须用户确认
 * - 破坏性迁移必须 human_required
 * - 会改变 sovereignty 的迁移必须 human confirm
 * - 必须包含回滚路径
 */
export function createMigrationPlan(options, contract) {
    const plan = {
        project_path: options.project_path,
        from_version: options.from_version,
        to_version: options.to_version,
        required_migrations: [],
        auto_applicable: [],
        requires_human: [],
        blocked: [],
        backup_path: undefined,
        rollback_path: undefined,
    };
    // 分类迁移步骤
    for (const migration of contract.migrations) {
        plan.required_migrations.push(`${migration.from_version}->${migration.to_version}: ${migration.description_zh}`);
        if (migration.affects_sovereignty || migration.requires_human_confirm) {
            plan.requires_human.push(migration.description_zh);
        }
        else if (migration.auto_applicable) {
            plan.auto_applicable.push(migration.description_zh);
        }
        else {
            plan.requires_human.push(migration.description_zh);
        }
    }
    // 检查破坏性变更
    for (const bc of contract.breaking_changes) {
        plan.required_migrations.push(`破坏性变更: ${bc.description_zh}`);
        plan.requires_human.push(bc.description_zh);
    }
    // 安全规则：apply 必须确认
    if (options.apply) {
        if (!options.confirm) {
            plan.blocked.push("apply 模式必须提供用户确认");
            return {
                success: false,
                dry_run: false,
                plan,
            };
        }
        // 备份路径
        const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
        plan.backup_path = `.soloforge/backups/migration/${timestamp}`;
        plan.rollback_path = plan.backup_path;
    }
    return {
        success: plan.blocked.length === 0,
        dry_run: options.dry_run || !options.apply,
        plan,
        backup_path: plan.backup_path,
    };
}
/**
 * 验证迁移安全性。
 * 硬规则：
 * - apply 无确认必须阻断
 * - apply 无回滚路径必须阻断
 * - sovereignty 变更无人工确认必须阻断
 */
export function validateMigrationSafety(plan, apply, confirm) {
    const blocked = [];
    if (apply && !confirm) {
        blocked.push("迁移 apply 模式必须提供用户确认");
    }
    if (apply && !plan.rollback_path) {
        blocked.push("迁移 apply 模式必须包含回滚路径");
    }
    if (plan.requires_human.length > 0 && apply && !confirm) {
        blocked.push(`需要人工确认的迁移项: ${plan.requires_human.join("; ")}`);
    }
    return {
        safe: blocked.length === 0,
        blocked,
        reason: blocked.length > 0 ? blocked.join("; ") : undefined,
    };
}
/**
 * 生成项目版本文件内容。
 */
export function generateVersionFile(packageVersion, contractRegistryVersion) {
    return {
        project_schema_version: "1",
        soloforge_package_version: packageVersion,
        contract_registry_version: contractRegistryVersion,
        template_manifest_version: "1",
        adapter_prompt_version: "1",
        created_at: new Date().toISOString(),
        last_migrated_at: new Date().toISOString(),
    };
}
//# sourceMappingURL=release_compatibility.js.map