'completion', 'id' => str_repeat('e', 32), 'agent_id' => '7', 'operation_id' => str_repeat('a', 32), 'action' => 'test-mutation', 'resource_id' => 'test-resource', 'request_fingerprint' => str_repeat('d', 64), 'status' => 'succeeded', 'effect_may_have_applied' => false, 'safe_to_resubmit' => false, 'error_code' => '', 'result' => ['uuid' => 'test'], ]; $valid = $validate->invoke(null, $base); $assert(($valid['event_id'] ?? null) === $base['id'] && ($valid['host_id'] ?? null) === 7 && ($valid['status'] ?? null) === 'succeeded', 'A valid immutable completion receipt was rejected or altered.'); $reject = static function (array $receipt) use ($validate): bool { try { $validate->invoke(null, $receipt); return false; } catch (ReflectionException $error) { throw $error; } catch (Throwable $error) { $cause = $error instanceof ReflectionException ? $error : ($error->getPrevious() ?? $error); return $cause instanceof CloudError && $cause->httpStatus === 400; } }; $invalid = $base; $invalid['effect_may_have_applied'] = true; $assert($reject($invalid), 'A succeeded receipt claimed an ambiguous effect.'); $invalid = $base; $invalid['status'] = 'failed'; $assert($reject($invalid), 'A failed receipt without a stable error code was accepted.'); $invalid['error_code'] = 'transport_lost'; $invalid['effect_may_have_applied'] = true; $invalid['safe_to_resubmit'] = true; $assert($reject($invalid), 'An ambiguous failed receipt was marked safe to resubmit.'); $validFailure = $invalid; $validFailure['safe_to_resubmit'] = false; $validFailure = $validate->invoke(null, $validFailure); $assert(($validFailure['status'] ?? null) === 'failed' && ($validFailure['effect_may_have_applied'] ?? null) === true && ($validFailure['safe_to_resubmit'] ?? null) === false, 'A conservative terminal failure receipt was rejected.'); // Completion delivery intentionally omits the display-only retryable field. // Reconstructing its terminal Agent operation must restore the canonical // false value so every catalog deletion finalizer sees the same contract as // an immediate Agent response. foreach ([ ['template', 'debian-13-contract', 0], ['rescue_template', str_repeat('b', 32), 0], ['user_iso', str_repeat('c', 32), 42], ] as [$resourceType, $resourceId, $ownerId]) { $catalogResult = [ 'resource_type' => $resourceType, 'id' => $resourceId, 'deleted' => true, 'managed_image_absent' => true, 'managed_metadata_absent' => true, 'managed_artifacts_absent' => true, ]; if ($resourceType === 'user_iso') { $catalogResult['owner_id'] = $ownerId; } $catalogEvent = new CloudAgentCompletionEvent(); $catalogEvent->operation_id = str_repeat('9', 32); $catalogEvent->outcome = 'succeeded'; $catalogEvent->effect_may_have_applied = 0; $catalogEvent->safe_to_resubmit = 0; $catalogEvent->error_code = ''; $catalogEvent->result_json = json_encode([ 'schema' => 'virt_cloud_completion_event', 'version' => 1, 'identity' => [ 'action' => 'delete-' . str_replace('_', '-', $resourceType), 'resource_id' => $resourceType . ':' . $resourceId, 'request_fingerprint' => str_repeat('8', 64), ], 'result' => $catalogResult, ], JSON_THROW_ON_ERROR); $catalogOperation = CloudOperationCompletionService::operation( $catalogEvent ); $assert(($catalogOperation['retryable'] ?? null) === false && CloudAgentCatalogDeletion::confirms( $catalogOperation, $resourceType, $resourceId, $ownerId ), 'A durable successful ' . $resourceType . ' deletion receipt lost its terminal Agent contract.'); } // Crash window: finish() committed the terminal task and completion_event_id, // but PHP died before marking the durable receipt applied. The same immutable // receipt must repair that marker; it is not a late or rejected completion. $capsule = new Capsule(); $capsule->addConnection([ 'driver' => 'sqlite', 'database' => ':memory:', ], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema('default'); $schema->create('cloud_jobs', static function (Blueprint $table): void { $table->increments('id'); $table->integer('host_id')->nullable(); $table->string('operation_id'); $table->string('execution_mode'); $table->string('kind')->nullable(); $table->string('phase'); $table->string('outcome')->nullable(); $table->string('status'); $table->integer('dispatch_attempts'); $table->boolean('effect_may_have_applied'); $table->boolean('safe_to_resubmit'); $table->string('effect_resolution'); $table->string('completion_event_id')->nullable(); $table->boolean('finalization_incident')->default(0); $table->string('finalization_error_code')->nullable(); }); $schema->create('cloud_agent_completion_events', static function (Blueprint $table): void { $table->string('event_id')->primary(); $table->integer('host_id'); $table->string('operation_id'); $table->string('payload_sha256'); $table->string('outcome'); $table->boolean('effect_may_have_applied'); $table->boolean('safe_to_resubmit'); $table->string('error_code'); $table->text('result_json')->nullable(); $table->string('disposition'); $table->integer('received_at'); $table->integer('applied_at')->nullable(); $table->unique(['host_id', 'operation_id']); }); $normalized = $validate->invoke(null, $base); $payloadHash = hash('sha256', json_encode($normalized, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); Capsule::table('cloud_jobs')->insert([ 'id' => 1, 'host_id' => 7, 'operation_id' => $base['operation_id'], 'execution_mode' => 'agent', 'phase' => 'terminal', 'outcome' => 'succeeded', 'status' => 'succeeded', 'dispatch_attempts' => 1, 'effect_may_have_applied' => 0, 'safe_to_resubmit' => 0, 'effect_resolution' => 'applied', 'completion_event_id' => $base['id'], ]); Capsule::table('cloud_agent_completion_events')->insert([ 'event_id' => $base['id'], 'host_id' => 7, 'operation_id' => $base['operation_id'], 'payload_sha256' => $payloadHash, 'outcome' => 'succeeded', 'effect_may_have_applied' => 0, 'safe_to_resubmit' => 0, 'error_code' => '', 'result_json' => json_encode([ 'schema' => 'virt_cloud_completion_event', 'version' => 1, 'identity' => [ 'action' => $base['action'], 'resource_id' => $base['resource_id'], 'request_fingerprint' => $base['request_fingerprint'], ], 'result' => ['uuid' => 'test'], ], JSON_THROW_ON_ERROR), 'disposition' => 'received', 'received_at' => time(), 'applied_at' => null, ]); $replayed = CloudOperationCompletionService::accept($base); $event = Capsule::table('cloud_agent_completion_events') ->where('event_id', $base['id'])->first(); $assert($replayed === ['accepted' => true, 'disposition' => 'applied'] && $event !== null && $event->disposition === 'applied' && (int) $event->applied_at > 0, 'A post-commit receipt retransmission was misclassified after the PHP crash window.'); $late = $base; $late['id'] = str_repeat('f', 32); $late['operation_id'] = str_repeat('b', 32); $lateNormalized = $validate->invoke(null, $late); $lateHash = hash('sha256', json_encode($lateNormalized, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); Capsule::table('cloud_jobs')->insert([ 'id' => 2, 'host_id' => 7, 'operation_id' => $late['operation_id'], 'execution_mode' => 'agent', 'phase' => 'terminal', 'outcome' => 'failed', 'status' => 'failed', 'dispatch_attempts' => 1, 'effect_may_have_applied' => 1, 'safe_to_resubmit' => 0, 'effect_resolution' => 'unknown', 'completion_event_id' => str_repeat('c', 32), ]); Capsule::table('cloud_agent_completion_events')->insert([ 'event_id' => $late['id'], 'host_id' => 7, 'operation_id' => $late['operation_id'], 'payload_sha256' => $lateHash, 'outcome' => 'succeeded', 'effect_may_have_applied' => 0, 'safe_to_resubmit' => 0, 'error_code' => '', 'result_json' => json_encode([ 'schema' => 'virt_cloud_completion_event', 'version' => 1, 'identity' => [ 'action' => $late['action'], 'resource_id' => $late['resource_id'], 'request_fingerprint' => $late['request_fingerprint'], ], 'result' => ['uuid' => 'test'], ], JSON_THROW_ON_ERROR), 'disposition' => 'received', 'received_at' => time(), 'applied_at' => null, ]); $lateResult = CloudOperationCompletionService::accept($late); $assert($lateResult === ['accepted' => true, 'disposition' => 'late'], 'A different receipt overwrote an already-terminal task.'); // Merely reserving completion_event_id before a business finalizer runs is not // proof that the receipt settled the task. A success receipt which fell back // to failed+unknown must remain eligible for kind-specific recovery. $failedFinalizerJob = new CloudJob(); $failedFinalizerJob->execution_mode = 'agent'; $failedFinalizerJob->kind = 'instance_settings'; $failedFinalizerJob->phase = 'terminal'; $failedFinalizerJob->outcome = 'failed'; $failedFinalizerJob->status = 'failed'; $failedFinalizerJob->effect_resolution = 'unknown'; $failedFinalizerJob->finalization_incident = 0; $failedFinalizerJob->host_id = 7; $failedFinalizerJob->operation_id = $base['operation_id']; $failedFinalizerJob->completion_event_id = $base['id']; $successEvent = CloudAgentCompletionEvent::where( 'event_id', $base['id'] )->first(); $assert($successEvent !== null && !CloudOperationCompletionService::settledTaskWithEvent( $failedFinalizerJob, $successEvent ), 'A reserved completion_event_id concealed a failed receipt finalizer.'); // Every registered Agent leaf keeps the exact receipt available when its PHP // finalizer degraded success into a terminal unknown effect. The recovery // worker, rather than completion ingestion, must converge its business ledger. foreach (\App\Services\Cloud\CloudOperationRecoveryPolicy::kinds() as $kind) { $failedFinalizerJob->kind = $kind; $assert(!CloudOperationCompletionService::settledTaskWithEvent( $failedFinalizerJob, $successEvent ), 'A recoverable ' . $kind . ' receipt was acknowledged before its ledger converged.'); } $failedFinalizerJob->finalization_incident = 1; $assert(!CloudOperationCompletionService::settledTaskWithEvent( $failedFinalizerJob, $successEvent ), 'A local finalization incident was concealed by an exact event id.'); echo "Cloud completion receipt contract passed.\n";