addConnection([ 'driver' => 'sqlite', 'database' => ':memory:', ], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schemaBuilder = $capsule->schema('default'); $schemaBuilder->create('cloud_backup_restore_attempts', static function ( $table ): void { $table->integer('id')->primary(); $table->string('attempt_key'); $table->integer('reservation_id'); }); $schemaBuilder->create('cloud_instance_migration_source_debts', static function ( $table ): void { $table->integer('id')->primary(); $table->integer('source_reservation_id'); }); $schemaBuilder->create('cloud_resource_reservations', static function ( $table ): void { $table->integer('id')->primary(); $table->string('backup_restore_attempt_key')->nullable(); $table->integer('migration_source_debt_id')->nullable(); }); $attempts = $table('cloud_backup_restore_attempts'); $debts = $table('cloud_instance_migration_source_debts'); $reservations = $table('cloud_resource_reservations'); $assert($attempts !== '' && str_contains($attempts, 'reservation_id BIGINT UNSIGNED NOT NULL') && str_contains($attempts, 'UNIQUE KEY cloud_backup_restore_attempt_generation') && str_contains($attempts, 'UNIQUE KEY cloud_backup_restore_attempt_active') && str_contains($attempts, 'UNIQUE KEY cloud_backup_restore_attempt_operation (operation_id)') && str_contains($attempts, 'target_identity_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL') && str_contains($attempts, 'target_claim_set_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL') && str_contains($attempts, 'target_public_network_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL') && str_contains($attempts, 'CONSTRAINT chk_cloud_backup_restore_attempt_shape CHECK'), 'Backup restore attempts do not fence immutable generations and network identity.'); $attemptShape = $normalized($attempts); $assert(str_contains($attemptShape, "state = 'reserved' AND job_id IS NULL AND effect_resolution = 'not_applicable' AND active_attempt IS NOT NULL AND active_attempt = 1") && str_contains($attemptShape, "state IN ('queued','awaiting_receipt') AND job_id IS NOT NULL AND effect_resolution = 'not_applicable' AND active_attempt IS NOT NULL AND active_attempt = 1") && str_contains($attemptShape, "state = 'effect_unknown' AND effect_resolution = 'unknown' AND job_id IS NOT NULL AND active_attempt IS NOT NULL AND active_attempt = 1") && str_contains($attemptShape, "state = 'finalization_pending' AND job_id IS NOT NULL AND effect_resolution IN ('applied','not_applied') AND active_attempt IS NOT NULL AND active_attempt = 1") && str_contains($attemptShape, "state = 'succeeded' AND effect_resolution = 'applied' AND job_id IS NOT NULL AND active_attempt IS NULL") && str_contains($attemptShape, "state = 'failed' AND effect_resolution = 'not_applied' AND job_id IS NOT NULL AND active_attempt IS NULL"), 'Restore-attempt terminal and unknown-effect fencing can diverge.'); $assert($debts !== '' && str_contains($debts, 'UNIQUE KEY cloud_migration_source_debt_migration (migration_id)') && str_contains($debts, 'UNIQUE KEY cloud_migration_source_debt_active_reservation') && str_contains($debts, 'source_identity_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL') && str_contains($debts, 'source_claim_set_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL') && str_contains($debts, 'source_public_network_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL') && str_contains($debts, "state IN ('prepared','retained','cleanup_running','released',") && str_contains($debts, 'CONSTRAINT chk_cloud_migration_source_debt_shape CHECK'), 'Maintenance migration source cleanup debt is not durable and exclusive.'); $debtShape = $normalized($debts); $assert(str_contains($debtShape, "(cleanup_operation_id IS NULL AND cleanup_job_id IS NULL) OR (cleanup_operation_id IS NOT NULL AND cleanup_job_id IS NOT NULL)") && str_contains($debtShape, "state IN ('prepared','retained') AND active_debt IS NOT NULL AND active_debt = 1 AND cleanup_operation_id IS NULL AND cleanup_job_id IS NULL AND resolved_at IS NULL") && str_contains($debtShape, "state = 'cleanup_running' AND active_debt IS NOT NULL AND active_debt = 1 AND cleanup_operation_id IS NOT NULL AND cleanup_job_id IS NOT NULL AND resolved_at IS NULL") && str_contains($debtShape, "state = 'attention' AND active_debt IS NOT NULL AND active_debt = 1 AND resolved_at IS NULL") && str_contains($debtShape, "state = 'released' AND active_debt IS NULL AND cleanup_operation_id IS NOT NULL AND cleanup_job_id IS NOT NULL AND resolved_at IS NOT NULL") && str_contains($debtShape, "state = 'rollback_restored' AND active_debt IS NULL AND cleanup_operation_id IS NULL AND cleanup_job_id IS NULL AND resolved_at IS NOT NULL"), 'Source debt can become terminal without confirmed resolution.'); // MySQL accepts CHECK expressions that evaluate to UNKNOWN. Every live-state // branch must therefore reject NULL explicitly before comparing the nullable // unique-index fence with 1. $assert(!str_contains($attemptShape, "effect_resolution = 'unknown' AND active_attempt = 1") && !str_contains($debtShape, "state = 'attention' AND active_debt = 1"), 'A nullable active fence can satisfy MySQL CHECK through UNKNOWN.'); $assert($reservations !== '' && str_contains($reservations, 'backup_restore_attempt_key CHAR(32) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL') && str_contains($reservations, 'migration_source_debt_id BIGINT UNSIGNED DEFAULT NULL') && str_contains($reservations, 'UNIQUE KEY cloud_resource_reservation_backup_attempt') && str_contains($reservations, 'UNIQUE KEY cloud_resource_reservation_migration_debt') && !str_contains($reservations, 'backup_restore_attempt_id') && substr_count($reservations, 'backup_restore_attempt_key IS NOT NULL') === 1 && substr_count($reservations, 'migration_source_debt_id IS NOT NULL') === 1, 'Reservation ownership does not enforce one source ledger.'); foreach ([ "'cloud_backup_restore_attempts'", "'cloud_instance_migration_source_debts'", "'backup_restore_attempt_key'", "'migration_source_debt_id'", "'chk_cloud_backup_restore_attempt_shape'", "'chk_cloud_migration_source_debt_shape'", "'chk_cloud_migration_source_debt_cleanup_pair'", "'cloud_backup_restore_attempt_active'", "'cloud_migration_source_debt_active_reservation'", "'columns' => ['backup_restore_attempt_key']", "'columns' => ['migration_source_debt_id']", ] as $required) { $assert(str_contains($setup, $required), 'CloudSetup omits restore/migration ledger contract: ' . $required); } $assert(str_contains($setup, "SCHEMA_VERSION = 'go-agent-wss-v31-restore-migration-ledgers'") && str_contains($attemptModel, "protected \$table = 'cloud_backup_restore_attempts';") && str_contains($attemptModel, "'active_attempt' => 'int'") && str_contains($debtModel, "protected \$table = 'cloud_instance_migration_source_debts';") && str_contains($debtModel, "'active_debt' => 'int'") && str_contains($reservationModel, "function backupRestoreAttemptQuery()") && str_contains($attemptModel, "function boundReservationQuery()") && str_contains($debtModel, "function boundReservationQuery()") && str_contains($reservationModel, "'migration_source_debt_id' => 'int'"), 'Models or setup version do not expose the new ownership boundary.'); $frozenIdentity = file_get_contents( $root . '/app/Services/Cloud/CloudFrozenPublicNetworkIdentity.php' ); $assert(is_string($frozenIdentity) && str_contains($frozenIdentity, 'public static function fromClaimSet(int $hostId, string $uuid,') && str_contains($frozenIdentity, 'CloudActivePublicNetworkClaimSet $claimSet') && str_contains($frozenIdentity, 'CloudPublicNetworkAllocationService::currentClaimSet()') && !str_contains($frozenIdentity, 'public static function fromLedger(') && !str_contains($frozenIdentity, 'array $claims') && !str_contains($frozenIdentity, 'array $addresses'), 'Frozen identity can bypass the authoritative claim-set module.'); $attemptRecord = new CloudBackupRestoreAttempt(); $attemptRecord->id = 91; $attemptRecord->attempt_key = str_repeat('e', 32); $attemptRecord->reservation_id = 92; $reservationRecord = new CloudResourceReservation(); $reservationRecord->id = 92; $reservationRecord->backup_restore_attempt_key = str_repeat('e', 32); Capsule::table('cloud_backup_restore_attempts')->insert([ 'id' => 91, 'attempt_key' => str_repeat('e', 32), 'reservation_id' => 92, ]); Capsule::table('cloud_resource_reservations')->insert([ 'id' => 92, 'backup_restore_attempt_key' => str_repeat('e', 32), 'migration_source_debt_id' => null, ]); $assert($attemptRecord->matchesReservation($reservationRecord) && (int) $attemptRecord->boundReservationQuery()->first()?->id === 92 && (int) $reservationRecord->backupRestoreAttemptQuery()->first()?->id === 91, 'Restore attempt and reservation are not joined by id and stable key.'); $reservationRecord->backup_restore_attempt_key = str_repeat('f', 32); Capsule::table('cloud_resource_reservations')->where('id', 92)->update([ 'backup_restore_attempt_key' => str_repeat('f', 32), ]); $assert(!$attemptRecord->matchesReservation($reservationRecord) && $attemptRecord->boundReservationQuery()->first() === null && $reservationRecord->backupRestoreAttemptQuery()->first() === null, 'Restore attempt accepted a cross-bound reservation identity.'); $debtRecord = new CloudInstanceMigrationSourceDebt(); $debtRecord->id = 101; $debtRecord->source_reservation_id = 102; $debtReservation = new CloudResourceReservation(); $debtReservation->id = 102; $debtReservation->migration_source_debt_id = 101; Capsule::table('cloud_instance_migration_source_debts')->insert([ 'id' => 101, 'source_reservation_id' => 102, ]); Capsule::table('cloud_resource_reservations')->insert([ 'id' => 102, 'backup_restore_attempt_key' => null, 'migration_source_debt_id' => 101, ]); $assert($debtRecord->matchesReservation($debtReservation) && (int) $debtRecord->boundReservationQuery()->first()?->id === 102 && (int) $debtReservation->migrationSourceDebtQuery()->first()?->id === 101, 'Migration source debt does not bind the exact source reservation both ways.'); $debtReservation->migration_source_debt_id = 103; Capsule::table('cloud_resource_reservations')->where('id', 102)->update([ 'migration_source_debt_id' => 103, ]); $assert(!$debtRecord->matchesReservation($debtReservation) && $debtRecord->boundReservationQuery()->first() === null && $debtReservation->migrationSourceDebtQuery()->first() === null, 'Migration source debt accepted a cross-bound reservation identity.'); echo "Restore/migration durable-ledger contract tests passed.\n";