addConnection([ 'driver' => 'sqlite', 'database' => ':memory:', ], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema('default'); $schema->create('cloud_traffic_pool_groups', static function (Blueprint $t): void { $t->increments('id'); $t->string('mode'); }); $schema->create('cloud_traffic_pools', static function (Blueprint $t): void { $t->increments('id'); $t->integer('user_id'); $t->integer('traffic_pool_group_id'); $t->string('state'); $t->integer('available_bytes')->default(0); $t->integer('overage_bytes')->default(0); $t->integer('version')->default(1); $t->integer('last_sequence')->default(0); $t->integer('last_event_id')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_traffic_ledger_events', static function (Blueprint $t): void { $t->increments('id'); $t->integer('pool_id'); $t->integer('user_id'); $t->integer('pool_sequence'); $t->string('event_key')->unique(); $t->string('event_type'); $t->string('source_type'); $t->string('source_id'); $t->integer('source_revision'); $t->string('payload_hash'); $t->integer('instance_id')->nullable(); $t->integer('service_period_id')->nullable(); $t->integer('entitlement_id')->nullable(); $t->integer('amount_bytes'); $t->integer('effective_at'); $t->integer('observed_at')->nullable(); $t->text('payload_json'); $t->integer('created_at'); $t->unique(['pool_id', 'pool_sequence']); }); $schema->create('cloud_traffic_usage_settlement_buckets', static function (Blueprint $t): void { $t->increments('id'); $t->integer('pool_id'); $t->integer('user_id'); $t->integer('instance_id'); $t->integer('service_period_id'); $t->integer('entitlement_id')->nullable(); $t->string('allocation_kind'); $t->integer('allocation_target_id'); $t->integer('bucket_start'); $t->integer('seal_at'); $t->integer('bucket_revision'); $t->binary('event_identity')->unique(); $t->integer('amount_bytes'); $t->integer('download_bytes'); $t->integer('upload_bytes'); $t->integer('sample_count'); $t->integer('first_observed_at'); $t->integer('last_observed_at'); $t->string('state'); $t->integer('sealed_event_id')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_traffic_instance_cursors', static function (Blueprint $t): void { $t->integer('instance_id')->primary(); $t->integer('user_id'); $t->integer('pool_id'); $t->integer('accounted_download_bytes'); $t->integer('accounted_upload_bytes'); $t->integer('last_observed_at'); $t->integer('last_event_id')->nullable(); $t->string('state'); $t->string('attention_code'); $t->integer('version'); $t->integer('updated_at'); }); $schema->create('cloud_instances', static function (Blueprint $t): void { $t->increments('id'); $t->integer('user_id'); $t->integer('current_period_id')->nullable(); $t->integer('lifetime_download_bytes')->default(0); $t->integer('lifetime_upload_bytes')->default(0); $t->integer('traffic_synced_at')->default(0); $t->string('status')->default('active'); $t->integer('created_at')->default(0); $t->integer('updated_at')->default(0); }); $schema->create('cloud_service_periods', static function (Blueprint $t): void { $t->increments('id'); $t->integer('instance_id'); $t->integer('user_id'); $t->integer('traffic_pool_group_id')->nullable(); $t->string('traffic_quota_mode')->default('finite'); $t->integer('traffic_quota_bytes')->default(0); $t->integer('starts_at'); $t->integer('ends_at'); $t->string('status'); $t->integer('updated_at')->default(0); }); $schema->create('cloud_traffic_entitlements', static function (Blueprint $t): void { $t->increments('id'); $t->integer('pool_id'); $t->integer('user_id'); $t->integer('instance_id'); $t->integer('service_period_id'); $t->integer('starts_at'); $t->integer('ends_at'); $t->string('quota_mode'); $t->integer('granted_bytes'); $t->integer('consumed_bytes')->default(0); $t->integer('forfeited_bytes')->default(0); $t->string('state'); $t->integer('activated_at')->nullable(); $t->integer('closed_at')->nullable(); $t->integer('version')->default(1); $t->integer('created_at')->default(0); $t->integer('updated_at')->default(0); }); $now = time(); Capsule::table('cloud_traffic_pool_groups')->insert([ 'id' => 1, 'mode' => 'enforced', ]); Capsule::table('cloud_traffic_pools')->insert([ 'id' => 1, 'user_id' => 7, 'traffic_pool_group_id' => 1, 'state' => 'available', 'available_bytes' => 0, 'overage_bytes' => 10, 'version' => 1, 'last_sequence' => 0, 'last_event_id' => null, 'created_at' => $now - 100, 'updated_at' => $now - 100, ]); $identity = random_bytes(16); $bucket = [ 'id' => 1, 'pool_id' => 1, 'user_id' => 7, 'instance_id' => 11, 'service_period_id' => 101, 'entitlement_id' => null, 'allocation_kind' => 'overage', 'allocation_target_id' => 0, 'bucket_start' => $now - 86_500, 'seal_at' => $now - 100, 'bucket_revision' => 1, 'event_identity' => $identity, 'amount_bytes' => 10, 'download_bytes' => 7, 'upload_bytes' => 3, 'sample_count' => 1, 'first_observed_at' => $now - 86_490, 'last_observed_at' => $now - 86_490, 'state' => 'open', 'sealed_event_id' => null, 'created_at' => $now - 100, 'updated_at' => $now - 100, ]; Capsule::table('cloud_traffic_usage_settlement_buckets')->insert($bucket); $plan = CloudTrafficSettlementRecovery::inspect(); $assert($plan['actionable'] === true && $plan['counts']['due_buckets'] === 1 && preg_match('/^[a-f0-9]{64}$/D', $plan['plan_hash']) === 1, 'Inspect did not produce a bounded actionable settlement plan: ' . json_encode($plan, JSON_THROW_ON_ERROR)); Capsule::table('cloud_traffic_usage_settlement_buckets')->where('id', 1) ->update(['amount_bytes' => 11, 'download_bytes' => 8]); try { CloudTrafficSettlementRecovery::apply($plan['plan_hash']); throw new RuntimeException('A changed recovery plan was applied.'); } catch (RuntimeException $error) { $assert($error->getMessage() === 'traffic_settlement_recovery_plan_changed', 'Changed plan failed with the wrong safety code.'); } Capsule::table('cloud_traffic_usage_settlement_buckets')->where('id', 1) ->update(['amount_bytes' => 10, 'download_bytes' => 7]); $fresh = CloudTrafficSettlementRecovery::inspect(); $applied = CloudTrafficSettlementRecovery::apply($fresh['plan_hash']); $assert($applied['sealed_buckets'] === 1 && Capsule::table('cloud_traffic_usage_settlement_buckets') ->where('id', 1)->value('state') === 'sealed' && Capsule::table('cloud_traffic_ledger_events') ->where('event_key', 'traffic-settlement:v2:' . bin2hex($identity)) ->count() === 1, 'The exact approved plan did not use the normal immutable seal path.'); $empty = CloudTrafficSettlementRecovery::inspect(); $repeat = CloudTrafficSettlementRecovery::apply($empty['plan_hash']); $assert($repeat['sealed_buckets'] === 0 && $repeat['reconciled_periods'] === 0 && $repeat['settled_instances'] === 0, 'A repeated empty recovery plan was not idempotent.'); Capsule::table('cloud_traffic_usage_settlement_buckets')->insert(array_merge( $bucket, [ 'id' => 2, 'event_identity' => '', 'state' => 'open', 'sealed_event_id' => null, ] )); $blocked = CloudTrafficSettlementRecovery::inspect(); $assert($blocked['actionable'] === false && $blocked['blockers'][0]['code'] === 'settlement_event_identity_missing', 'A malformed persistent event identity did not block recovery.'); try { CloudTrafficSettlementRecovery::apply($blocked['plan_hash']); throw new RuntimeException('A blocked recovery plan was applied.'); } catch (RuntimeException $error) { $assert($error->getMessage() === 'traffic_settlement_recovery_blocked', 'Blocked plan failed with the wrong safety code.'); } $parse = new ReflectionMethod( CloudTrafficSettlementRecoveryCommand::class, 'parse' ); $assert($parse->invoke(null, [ 'xcat', 'cloudTrafficSettlementRecovery', 'inspect', ]) === ['action' => 'inspect'], 'CLI inspect arguments drifted.'); $hash = str_repeat('a', 64); $assert($parse->invoke(null, [ 'xcat', 'cloudTrafficSettlementRecovery', 'apply', '--plan-hash=' . $hash, '--apply', ]) === ['action' => 'apply', 'plan_hash' => $hash], 'CLI apply did not bind the exact reviewed plan hash.'); foreach ([ ['apply', '--plan-hash=' . $hash], ['apply', '--plan-hash=bad', '--apply'], ['apply', '--plan-hash=' . $hash, '--apply', '--force'], ['inspect', '--apply'], ] as $arguments) { try { $parse->invoke(null, array_merge([ 'xcat', 'cloudTrafficSettlementRecovery', ], $arguments)); throw new RuntimeException('Unsafe CLI arguments were accepted.'); } catch (ReflectionException $error) { throw $error; } catch (Throwable $error) { $actual = $error->getPrevious() ?? $error; $assert($actual instanceof InvalidArgumentException, 'Unsafe CLI arguments failed with the wrong exception.'); } } $service = file_get_contents(dirname(__DIR__) . '/app/Services/Cloud/CloudTrafficSettlementRecovery.php'); $xcat = file_get_contents(dirname(__DIR__) . '/app/Command/XCat.php'); $assert(is_string($service) && is_string($xcat) && !str_contains($service, '->delete(') && !str_contains($service, 'CloudAgentClient') && !str_contains($service, 'CloudJob') && str_contains($service, 'CloudTrafficPoolAccounting::sealPlannedUsageBuckets(') && str_contains($service, 'CloudTrafficPoolAccounting::reconcileServicePeriod(') && str_contains($service, 'CloudTrafficPoolAccounting::settleInstanceUsage(') && str_contains($xcat, "'cloudTrafficSettlementRecovery'") && str_contains($xcat, 'cloudTrafficSettlementRecovery apply --plan-hash=HASH --apply'), 'Recovery acquired a destructive or remote bypass path.'); echo "Traffic settlement recovery plan/apply checks passed.\n";