diff --git a/web/tests/CloudFinancialWorkflowTest.php b/web/tests/CloudFinancialWorkflowTest.php index 0454b595..761044f6 100644 --- a/web/tests/CloudFinancialWorkflowTest.php +++ b/web/tests/CloudFinancialWorkflowTest.php @@ -3036,6 +3036,16 @@ namespace { 'active', 'active' ); + $legacyNetworkSpec = (string) $legacyNetworkInstance->spec_json; + $legacyNetworkIpLedger = Capsule::table('cloud_public_ips') + ->orderBy('id')->get([ + 'id', 'state', 'reservation_id', 'instance_id', + ])->map(static fn ($row): array => (array) $row)->all(); + $legacyNetworkClaimLedger = Capsule::table( + 'cloud_resource_reservation_ips' + )->orderBy('id')->get([ + 'id', 'state', 'reservation_id', 'public_ip_id', 'instance_id', + ])->map(static fn ($row): array => (array) $row)->all(); $legacyWorkerJob = new CloudJob(); $legacyWorkerJob->operation_id = bin2hex(random_bytes(16)); $legacyWorkerJob->request_idempotency_key = @@ -3075,8 +3085,20 @@ namespace { $assert(count($history) === $beforeLegacyWorker && (int) $legacyWorkerJob->dispatch_attempts === 0 && $legacyWorkerJob->phase === 'terminal' - && $legacyWorkerJob->outcome === 'failed', - 'Legacy network task reached the Agent or remained claimable.'); + && $legacyWorkerJob->outcome === 'failed' + && (string) $legacyNetworkInstance->fresh()->spec_json + === $legacyNetworkSpec + && Capsule::table('cloud_public_ips')->orderBy('id')->get([ + 'id', 'state', 'reservation_id', 'instance_id', + ])->map(static fn ($row): array => (array) $row)->all() + === $legacyNetworkIpLedger + && Capsule::table('cloud_resource_reservation_ips') + ->orderBy('id')->get([ + 'id', 'state', 'reservation_id', 'public_ip_id', 'instance_id', + ])->map(static fn ($row): array => (array) $row)->all() + === $legacyNetworkClaimLedger, + 'Legacy network task reached the Agent, remained claimable, or changed ' + . 'the canonical instance/address ledgers.'); $worker->tick(); $assert(count($history) === $beforeLegacyWorker && (int) $legacyWorkerJob->fresh()->dispatch_attempts === 0, diff --git a/web/tests/CloudPortableRestoreJobDispatcherTest.php b/web/tests/CloudPortableRestoreJobDispatcherTest.php index 48c2b84a..39092165 100644 --- a/web/tests/CloudPortableRestoreJobDispatcherTest.php +++ b/web/tests/CloudPortableRestoreJobDispatcherTest.php @@ -53,6 +53,7 @@ $schema->table('cloud_jobs', static function (Blueprint $t): void { $t->string('request_idempotency_key')->default(''); $t->string('request_fingerprint', 64)->default(''); $t->integer('instance_id')->nullable(); + $t->integer('parent_job_id')->nullable(); $t->integer('vpc_repair_root_job_id')->nullable(); $t->string('execution_mode')->default('agent'); $t->string('status')->default('queued'); @@ -262,30 +263,6 @@ $assert((int) $restore->job_id === (int) $job->id, 'Generation-one job projection was not written to the parent restore.'); $payload = CloudSecrets::decrypt((string) $job->sealed_payload, 'job-payload'); -$dispatchGate = new ReflectionMethod( - CloudWorkerService::class, - 'assertPortableRestoreDispatchReady' -); -$dispatchGate->invoke(null, $job, $payload); -$missingAttempt = $payload; -unset($missingAttempt['attempt_key']); -$rejects409( - static fn () => $dispatchGate->invoke(null, $job, $missingAttempt), - 'Worker accepted a portable restore without an attempt identity.' -); -$invalidAttempt = $payload; -$invalidAttempt['attempt_key'] = 'invalid'; -$rejects409( - static fn () => $dispatchGate->invoke(null, $job, $invalidAttempt), - 'Worker accepted a malformed portable restore attempt identity.' -); -$legacyJob = $job->replicate(); -$legacyJob->id = 999999; -$legacyJob->kind = 'backup_restore'; -$rejects409( - static fn () => $dispatchGate->invoke(null, $legacyJob, $payload), - 'Worker accepted a restore job without an attempt-owned ledger.' -); $body = is_array($payload['body'] ?? null) ? $payload['body'] : []; $public = is_array($body['public_network'] ?? null) ? $body['public_network'] : []; @@ -715,6 +692,76 @@ try { )->state === 'released', 'Valid never-dispatched restore fixture did not release its dual stack.'); + // Exercise the public Worker seam rather than its private admission helper: + // a legacy restore payload without an attempt identity must fail locally, + // before Agent contact, without changing any canonical resource ledger. + $missingAttemptPayload = $payload; + unset($missingAttemptPayload['attempt_key']); + $legacyRestoreJob = new CloudJob(); + $legacyRestoreJob->operation_id = bin2hex(random_bytes(16)); + $legacyRestoreJob->request_idempotency_key = + 'legacy-restore-worker-gate-0001'; + $legacyRestoreJob->request_fingerprint = hash( + 'sha256', + 'legacy-restore-worker-gate-0001' + ); + $legacyRestoreJob->user_id = (int) $job->user_id; + $legacyRestoreJob->host_id = (int) $job->host_id; + $legacyRestoreJob->instance_id = null; + $legacyRestoreJob->backup_id = (int) $job->backup_id; + $legacyRestoreJob->backup_restore_id = (int) $job->backup_restore_id; + $legacyRestoreJob->kind = 'backup_restore'; + $legacyRestoreJob->execution_mode = 'agent'; + $legacyRestoreJob->sealed_payload = CloudSecrets::encrypt( + $missingAttemptPayload, + 'job-payload' + ); + $legacyRestoreJob->available_at = $legacyRestoreJob->created_at = + $legacyRestoreJob->updated_at = time(); + $legacyRestoreJob->save(); + $legacyRestoreIpLedger = Capsule::table('cloud_public_ips') + ->orderBy('id')->get([ + 'id', 'state', 'reservation_id', 'instance_id', + ])->map(static fn ($row): array => (array) $row)->all(); + $legacyRestoreClaimLedger = Capsule::table( + 'cloud_resource_reservation_ips' + )->orderBy('id')->get([ + 'id', 'state', 'reservation_id', 'public_ip_id', 'instance_id', + ])->map(static fn ($row): array => (array) $row)->all(); + $legacyRestoreAgentCalls = 0; + $legacyRestoreTransport = new \GuzzleHttp\Client([ + 'handler' => static function () use (&$legacyRestoreAgentCalls): void { + $legacyRestoreAgentCalls++; + throw new RuntimeException('Legacy restore reached Agent.'); + }, + ]); + $legacyRestoreAgent = new CloudAgentClient( + '3', + str_repeat('a', 32), + $legacyRestoreTransport, + 20.0, + 'http://127.0.0.1:8001/internal/agent/request', + str_repeat('b', 32) + ); + $assert((new CloudWorkerService($legacyRestoreAgent))->tick() === true, + 'Worker did not consume the legacy restore job.'); + $legacyRestoreJob->refresh(); + $assert($legacyRestoreAgentCalls === 0 + && (int) $legacyRestoreJob->dispatch_attempts === 0 + && (string) $legacyRestoreJob->phase === 'terminal' + && (string) $legacyRestoreJob->outcome === 'failed' + && Capsule::table('cloud_public_ips')->orderBy('id')->get([ + 'id', 'state', 'reservation_id', 'instance_id', + ])->map(static fn ($row): array => (array) $row)->all() + === $legacyRestoreIpLedger + && Capsule::table('cloud_resource_reservation_ips') + ->orderBy('id')->get([ + 'id', 'state', 'reservation_id', 'public_ip_id', 'instance_id', + ])->map(static fn ($row): array => (array) $row)->all() + === $legacyRestoreClaimLedger, + 'Legacy restore reached Agent, remained claimable, or changed the ' + . 'canonical address ledgers.'); + // Freeze an internally consistent backup whose stored object-runtime shape // cannot mint a capability. The immutable plan binds that exact runtime; // changing an already-frozen ciphertext would be ledger damage instead.