diff --git a/docs/DEPLOYMENT.md b/docs/DEPLOYMENT.md index 9d33702b..56fb2314 100644 --- a/docs/DEPLOYMENT.md +++ b/docs/DEPLOYMENT.md @@ -160,7 +160,7 @@ libvirt 的持久 pool 定义就是 Agent 后续使用的映射,不另写存 公网网络接入在宿主机的“网络接入”中按资源池线路独立配置,可使用 `bridge:br0`、其他 Linux 网桥或 `network:default` 这样的 libvirt network。名称以字母或数字开头,后续只能包含字母、数字、点、下划线和短横线,bridge 名称为 1~15 字符,libvirt network 名称为 1~64 字符。宿主机上的公网网桥或 libvirt network 必须由运维预先准备。Agent 只发现、验证和使用现有公网网络,不创建公网网桥、不启动 libvirt network,也不修改公网地址、上游网关或挂载。`cloud_hosts.default_network` 仅保留为兼容展示值,不参与 Agent 就绪、调度、恢复、迁移或换 IP 决策;真正的运行时权威是资源池线路、当前 Agent 代次已验证的网络接入,以及绑定该接入的公网地址池。VPC 使用下述独立的 Agent 受管 bridge 和 L3 路由,不改变这一公网边界。 -当前完整 schema 版本为 `go-agent-wss-v52-daily-traffic-settlement`。v48 将滥用监测拆分为 72 小时原始证据、短期终态投影、7 天去重凭据以及 5 分钟/小时趋势汇总;v49 为实例性能图表增加 5 分钟和小时汇总,并将一分钟原始点限制为 24 小时;v50 保持分钟级 FEFO 扣减和游标更新不变,将同一小时、实例和额度的用量累积到持久结算桶,小时结束、额度耗尽、到期、债务结算或异常栅栏时再生成不可变账本事件。迟到采样使用新的桶修订,绝不改写已封存事件;v51 将性能历史改为逻辑实例主键,删除每行重复的宿主机 hash 和文本 UUID,将永久流量总数/检查点的 UUID 与 hash 改为二进制并删除未被读取路径使用的宽索引;v52 保持实时 FEFO 扣减不变,将新用量累积到 UTC 每日结算窗口,并通过 `bucket_seconds` 与 `seal_at` 继续识别和封存升级前的小时窗口。部署既有数据库时必须先停止生命周期与滥用监测 worker,按当前 `cloud.sql`、`cloud_log.sql` 的新增表和字段制作一次性迁移并完成回填,再启动新代码;v51 涉及日志表主键与二进制身份重建,不能在采集 worker 运行时原地改列,也不能让 v50 代码与 v51 日志表混跑;v52 必须先为既有结算桶回填 `bucket_seconds = 3600` 与 `seal_at = bucket_start + 3600`,再替换唯一索引、到期索引和检查约束。不得让缺少汇总表、结算桶窗口字段或 `instance_id` 的旧数据库直接进入采集或流量结算。v47 为 `cloud_offers` 增加季度、年度及对应自动备份价格;既有方案升级时必须在恢复销售前显式回填 90/365 天价格,不能让数据库默认零价进入购买目录。从 v44 升级时,必须先停止滥用观测与投影 worker,为既有 finding 回填 `occurrence_generation = 1` 与 `transition_revision = 1`,再创建 `cloud_abuse_alert_events` 和 `cloud_abuse_alert_event_items`;从 v45 升级还必须停止通知 worker,创建 `cloud_abuse_alert_deliveries`、`administrator_telegram_delivery_gate` 并写入唯一 gate 行。历史 finding 不反向合成事件;事件只从升级后主库事务内真实发生的 `opened`、`escalated` 和 `evidence_clear` 解决转移产生。完成列、约束、索引、外键和 gate 核对后,先恢复滥用观测,再恢复通知 worker。 +当前完整 schema 版本为 `go-agent-wss-v53-traffic-settlement-receipts`。v48 将滥用监测拆分为 72 小时原始证据、短期终态投影、7 天去重凭据以及 5 分钟/小时趋势汇总;v49 为实例性能图表增加 5 分钟和小时汇总,并将一分钟原始点限制为 24 小时;v50 保持分钟级 FEFO 扣减和游标更新不变,将同一小时、实例和额度的用量累积到持久结算桶,小时结束、额度耗尽、到期、债务结算或异常栅栏时再生成不可变账本事件。迟到采样使用新的桶修订,绝不改写已封存事件;v51 将性能历史改为逻辑实例主键,删除每行重复的宿主机 hash 和文本 UUID,将永久流量总数/检查点的 UUID 与 hash 改为二进制并删除未被读取路径使用的宽索引;v52 保持实时 FEFO 扣减不变,将新用量累积到 UTC 每日结算窗口,并通过 `bucket_seconds` 与 `seal_at` 继续识别和封存升级前的小时窗口;v53 增加紧凑结算凭据,冻结已经安全压缩的窗口身份、最大修订、账本事件和载荷哈希,让迟到采样无需依赖完整旧桶也能从下一修订继续。部署既有数据库时必须先停止生命周期与滥用监测 worker,按当前 `cloud.sql`、`cloud_log.sql` 的新增表和字段制作一次性迁移并完成回填,再启动新代码;v51 涉及日志表主键与二进制身份重建,不能在采集 worker 运行时原地改列,也不能让 v50 代码与 v51 日志表混跑;v52 必须先为既有结算桶回填 `bucket_seconds = 3600` 与 `seal_at = bucket_start + 3600`,再替换唯一索引、到期索引和检查约束;v53 必须先创建 `cloud_traffic_settlement_receipts`,本阶段不删除既有完整桶。不得让缺少汇总表、结算桶窗口字段、紧凑凭据表或 `instance_id` 的旧数据库直接进入采集或流量结算。v47 为 `cloud_offers` 增加季度、年度及对应自动备份价格;既有方案升级时必须在恢复销售前显式回填 90/365 天价格,不能让数据库默认零价进入购买目录。从 v44 升级时,必须先停止滥用观测与投影 worker,为既有 finding 回填 `occurrence_generation = 1` 与 `transition_revision = 1`,再创建 `cloud_abuse_alert_events` 和 `cloud_abuse_alert_event_items`;从 v45 升级还必须停止通知 worker,创建 `cloud_abuse_alert_deliveries`、`administrator_telegram_delivery_gate` 并写入唯一 gate 行。历史 finding 不反向合成事件;事件只从升级后主库事务内真实发生的 `opened`、`escalated` 和 `evidence_clear` 解决转移产生。完成列、约束、索引、外键和 gate 核对后,先恢复滥用观测,再恢复通知 worker。 事件 header 与 projection receipt 在同一个主库事务中写入,且事件先于 receipt;同一实例同一采样桶的多规则、双协议族转移合并为一个 header 和多个 item。`continued` 不产生事件,`policy_replaced` 与 `owner_changed` 只关闭 finding,不能伪造“恢复”事件。item 已冻结当时的聚合 evidence、规则身份和 finding 代次,后续投递不得回读会变化的 finding。v46 为每个事件建立至多一条 Telegram 投递决策;远端调用开始后的中断或回执不确定统一进入 `unknown`,禁止自动重放。删除用户时按事件冻结的 `user_id` 显式删除 header,item 与投递账本按当前外键和账户清理规则收敛,避免实例转交后遗留原租户数据。 diff --git a/docs/SERVICE_COMPENSATION.md b/docs/SERVICE_COMPENSATION.md index 9792f782..412268f4 100644 --- a/docs/SERVICE_COMPENSATION.md +++ b/docs/SERVICE_COMPENSATION.md @@ -47,7 +47,7 @@ ## 部署 -全新数据库必须由当前 `CloudSetup` 创建并验证 `go-agent-wss-v52-daily-traffic-settlement` 完整 schema;其中补偿模块包括: +全新数据库必须由当前 `CloudSetup` 创建并验证 `go-agent-wss-v53-traffic-settlement-receipts` 完整 schema;其中补偿模块包括: - `cloud_service_compensations`; - `cloud_service_compensation_items`; diff --git a/web/app/Command/CloudSetup.php b/web/app/Command/CloudSetup.php index a993e97f..557fa159 100644 --- a/web/app/Command/CloudSetup.php +++ b/web/app/Command/CloudSetup.php @@ -8,7 +8,7 @@ use App\Services\Config; /** Initializes only the clean-break PHP business and Agent log schemas. */ final class CloudSetup { - private const SCHEMA_VERSION = 'go-agent-wss-v52-daily-traffic-settlement'; + private const SCHEMA_VERSION = 'go-agent-wss-v53-traffic-settlement-receipts'; /** Tables installed by ss.sql before cloudInit and owned by this schema. */ private const PREINSTALLED_MAIN_TABLES = [ 'ui_copy_locales', 'ui_copy_entries', 'ui_copy_drafts', @@ -55,6 +55,7 @@ final class CloudSetup 'cloud_traffic_pools', 'cloud_traffic_entitlements', 'cloud_traffic_ledger_events', 'cloud_traffic_usage_settlement_buckets', + 'cloud_traffic_settlement_receipts', 'cloud_traffic_instance_cursors', 'cloud_traffic_scan_failures', 'cloud_instance_network_restrictions', @@ -586,6 +587,14 @@ final class CloudSetup 'sample_count', 'first_observed_at', 'last_observed_at', 'state', 'sealed_event_id', 'created_at', 'updated_at', ], + 'cloud_traffic_settlement_receipts' => [ + 'id', 'pool_id', 'user_id', 'instance_id', 'service_period_id', + 'entitlement_id', 'allocation_kind', 'allocation_target_id', + 'bucket_start', 'bucket_seconds', 'max_revision', + 'last_event_id', 'compacted_amount_bytes', + 'compacted_event_count', 'last_payload_hash', 'compacted_at', + 'updated_at', + ], 'cloud_traffic_instance_cursors' => [ 'instance_id', 'user_id', 'pool_id', 'accounted_download_bytes', 'accounted_upload_bytes', @@ -839,6 +848,11 @@ final class CloudSetup 'chk_cloud_traffic_settlement_amount', 'chk_cloud_traffic_settlement_state', ], + 'cloud_traffic_settlement_receipts' => [ + 'chk_cloud_traffic_settlement_receipt_identity', + 'chk_cloud_traffic_settlement_receipt_window', + 'chk_cloud_traffic_settlement_receipt_totals', + ], 'cloud_traffic_instance_cursors' => [ 'chk_cloud_traffic_cursor_state', ], @@ -1191,6 +1205,12 @@ final class CloudSetup 'cloud_traffic_settlement_owner', 'cloud_traffic_settlement_event', ], + 'cloud_traffic_settlement_receipts' => [ + 'PRIMARY', 'cloud_traffic_settlement_receipt_identity', + 'cloud_traffic_settlement_receipt_owner', + 'cloud_traffic_settlement_receipt_event', + 'cloud_traffic_settlement_receipt_age', + ], 'cloud_traffic_instance_cursors' => [ 'PRIMARY', 'cloud_traffic_cursor_pool', 'cloud_traffic_cursor_owner', @@ -1634,6 +1654,16 @@ final class CloudSetup 'columns' => ['state', 'seal_at', 'id'], ], ], + 'cloud_traffic_settlement_receipts' => [ + 'cloud_traffic_settlement_receipt_identity' => [ + 'non_unique' => 0, + 'columns' => [ + 'pool_id', 'instance_id', 'service_period_id', + 'allocation_kind', 'allocation_target_id', + 'bucket_start', 'bucket_seconds', + ], + ], + ], 'cloud_instance_backups' => [ 'cloud_instance_backup_create_request' => [ 'non_unique' => 0, diff --git a/web/app/Models/CloudTrafficSettlementReceipt.php b/web/app/Models/CloudTrafficSettlementReceipt.php new file mode 100644 index 00000000..070c1de5 --- /dev/null +++ b/web/app/Models/CloudTrafficSettlementReceipt.php @@ -0,0 +1,27 @@ + 'int', + 'pool_id' => 'int', + 'user_id' => 'int', + 'instance_id' => 'int', + 'service_period_id' => 'int', + 'entitlement_id' => 'int', + 'allocation_target_id' => 'int', + 'bucket_start' => 'int', + 'bucket_seconds' => 'int', + 'max_revision' => 'int', + 'last_event_id' => 'int', + 'compacted_amount_bytes' => 'int', + 'compacted_event_count' => 'int', + 'compacted_at' => 'int', + 'updated_at' => 'int', + ]; +} diff --git a/web/app/Services/Cloud/CloudTrafficPoolAccounting.php b/web/app/Services/Cloud/CloudTrafficPoolAccounting.php index ffe734a9..a4e1f1d0 100644 --- a/web/app/Services/Cloud/CloudTrafficPoolAccounting.php +++ b/web/app/Services/Cloud/CloudTrafficPoolAccounting.php @@ -10,6 +10,7 @@ use App\Models\CloudTrafficLedgerEvent; use App\Models\CloudTrafficPool; use App\Models\CloudTrafficPoolGroup; use App\Models\CloudTrafficScanFailure; +use App\Models\CloudTrafficSettlementReceipt; use App\Models\CloudTrafficUsageSettlementBucket; /** @@ -1482,6 +1483,16 @@ final class CloudTrafficPoolAccounting // revision for the remaining delta. $lastRevision = (int) (clone $identity) ->lockForUpdate()->max('bucket_revision'); + $receiptRevision = (int) CloudTrafficSettlementReceipt::where( + 'pool_id', (int) $pool->id + )->where('instance_id', $instanceId) + ->where('service_period_id', $servicePeriodId) + ->where('allocation_kind', $kind) + ->where('allocation_target_id', $targetId) + ->where('bucket_start', $bucketStart) + ->where('bucket_seconds', $bucketSeconds) + ->lockForUpdate()->value('max_revision'); + $lastRevision = max($lastRevision, $receiptRevision); $bucket = new CloudTrafficUsageSettlementBucket(); $bucket->pool_id = (int) $pool->id; $bucket->user_id = (int) $pool->user_id; diff --git a/web/app/Services/UserDataDeletionService.php b/web/app/Services/UserDataDeletionService.php index 629d8f7b..b12a4cc6 100644 --- a/web/app/Services/UserDataDeletionService.php +++ b/web/app/Services/UserDataDeletionService.php @@ -41,6 +41,7 @@ class UserDataDeletionService ['cloud_abuse_findings', 'user_id'], ['cloud_abuse_instance_states', 'user_id'], ['cloud_traffic_usage_settlement_buckets', 'user_id'], + ['cloud_traffic_settlement_receipts', 'user_id'], ['cloud_traffic_instance_cursors', 'user_id'], ['cloud_traffic_ledger_events', 'user_id'], ['cloud_traffic_entitlements', 'user_id'], diff --git a/web/sql/cloud.sql b/web/sql/cloud.sql index 28081647..ac8d3a09 100644 --- a/web/sql/cloud.sql +++ b/web/sql/cloud.sql @@ -2374,6 +2374,52 @@ CREATE TABLE cloud_traffic_usage_settlement_buckets ( ) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +-- Compact idempotency evidence for settlement buckets whose complete mutable +-- row has passed the safety horizon. Immutable ledger events remain canonical; +-- this receipt only preserves the base identity and highest sealed revision. +CREATE TABLE cloud_traffic_settlement_receipts ( + id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, + pool_id BIGINT UNSIGNED NOT NULL, + user_id BIGINT UNSIGNED NOT NULL, + instance_id BIGINT UNSIGNED NOT NULL, + service_period_id BIGINT UNSIGNED NOT NULL, + entitlement_id BIGINT UNSIGNED DEFAULT NULL, + allocation_kind VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + allocation_target_id BIGINT UNSIGNED NOT NULL, + bucket_start BIGINT UNSIGNED NOT NULL, + bucket_seconds INT UNSIGNED NOT NULL, + max_revision INT UNSIGNED NOT NULL, + last_event_id BIGINT UNSIGNED NOT NULL, + compacted_amount_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0, + compacted_event_count INT UNSIGNED NOT NULL DEFAULT 0, + last_payload_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + compacted_at BIGINT UNSIGNED NOT NULL, + updated_at BIGINT UNSIGNED NOT NULL, + UNIQUE KEY cloud_traffic_settlement_receipt_identity ( + pool_id, instance_id, service_period_id, + allocation_kind, allocation_target_id, bucket_start, bucket_seconds + ), + KEY cloud_traffic_settlement_receipt_owner (user_id, instance_id, id), + KEY cloud_traffic_settlement_receipt_event (last_event_id), + KEY cloud_traffic_settlement_receipt_age (compacted_at, id), + CONSTRAINT chk_cloud_traffic_settlement_receipt_identity CHECK ( + (allocation_kind = 'entitlement' + AND entitlement_id IS NOT NULL + AND allocation_target_id = entitlement_id) + OR (allocation_kind = 'overage' + AND entitlement_id IS NULL AND allocation_target_id = 0) + ), + CONSTRAINT chk_cloud_traffic_settlement_receipt_window CHECK ( + bucket_seconds IN (3600, 86400) + AND bucket_start % bucket_seconds = 0 + AND max_revision > 0 + ), + CONSTRAINT chk_cloud_traffic_settlement_receipt_totals CHECK ( + last_event_id > 0 AND compacted_amount_bytes > 0 + AND compacted_event_count > 0 + ) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + CREATE TABLE cloud_traffic_instance_cursors ( instance_id BIGINT UNSIGNED NOT NULL PRIMARY KEY, user_id BIGINT UNSIGNED NOT NULL, diff --git a/web/tests/CloudAbuseMonitoringSchemaContractTest.php b/web/tests/CloudAbuseMonitoringSchemaContractTest.php index 154b01bb..d84e3db9 100644 --- a/web/tests/CloudAbuseMonitoringSchemaContractTest.php +++ b/web/tests/CloudAbuseMonitoringSchemaContractTest.php @@ -415,7 +415,7 @@ $assert(str_contains( $assert(str_contains( $setup, - "SCHEMA_VERSION = 'go-agent-wss-v52-daily-traffic-settlement'" + "SCHEMA_VERSION = 'go-agent-wss-v53-traffic-settlement-receipts'" ), 'CloudSetup schema version was not advanced for abuse observation.'); foreach (array_keys($mainTables + $logTables) as $table) { diff --git a/web/tests/CloudAbusePolicySchemaContractTest.php b/web/tests/CloudAbusePolicySchemaContractTest.php index 37da1a51..8e858e26 100644 --- a/web/tests/CloudAbusePolicySchemaContractTest.php +++ b/web/tests/CloudAbusePolicySchemaContractTest.php @@ -96,7 +96,7 @@ foreach ($defaults->rules() as $code => $rule) { 'The fresh schema rule seed drifted from PHP: ' . $code); } $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v52-daily-traffic-settlement'") + "SCHEMA_VERSION = 'go-agent-wss-v53-traffic-settlement-receipts'") && str_contains($service, "->where('active_slot', 1)->lockForUpdate()") && str_contains($service, "->where('active_slot', 1)->update") && str_contains($service, 'insertGetId'), diff --git a/web/tests/CloudAbuseProjectionLedgerContractTest.php b/web/tests/CloudAbuseProjectionLedgerContractTest.php index e375d820..7eb200ef 100644 --- a/web/tests/CloudAbuseProjectionLedgerContractTest.php +++ b/web/tests/CloudAbuseProjectionLedgerContractTest.php @@ -260,7 +260,7 @@ $assert(str_contains($projection, '->snapshotByIdentity($revision, $hash)') && !str_contains($projection, '->activeSnapshot()'), 'Projection can drift from the outbox identity to the current policy.'); $assert(str_contains($deployment, - '`go-agent-wss-v52-daily-traffic-settlement`') + '`go-agent-wss-v53-traffic-settlement-receipts`') && str_contains($deployment, '`occurrence_generation = 1` 与 `transition_revision = 1`') && str_contains($deployment, diff --git a/web/tests/CloudAdminTrafficPoolServiceTest.php b/web/tests/CloudAdminTrafficPoolServiceTest.php index e3e3e0aa..e7f55c17 100644 --- a/web/tests/CloudAdminTrafficPoolServiceTest.php +++ b/web/tests/CloudAdminTrafficPoolServiceTest.php @@ -165,6 +165,25 @@ $schema->create('cloud_traffic_usage_settlement_buckets', static function (Bluep 'bucket_revision', ], 'traffic_settlement_identity'); }); +$schema->create('cloud_traffic_settlement_receipts', 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('bucket_seconds'); + $t->integer('max_revision'); + $t->integer('last_event_id'); + $t->integer('compacted_amount_bytes'); + $t->integer('compacted_event_count'); + $t->string('last_payload_hash'); + $t->integer('compacted_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'); diff --git a/web/tests/CloudBackupPublicationPreparationSchemaContractTest.php b/web/tests/CloudBackupPublicationPreparationSchemaContractTest.php index a5bbf195..e88701e8 100644 --- a/web/tests/CloudBackupPublicationPreparationSchemaContractTest.php +++ b/web/tests/CloudBackupPublicationPreparationSchemaContractTest.php @@ -64,7 +64,7 @@ foreach ([ 'CloudSetup omits backup preparation contract: ' . $required); } $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v52-daily-traffic-settlement'") + "SCHEMA_VERSION = 'go-agent-wss-v53-traffic-settlement-receipts'") && str_contains($setup, "'columns' => [\n 'create_request_actor_id',\n 'create_request_idempotency_key',") && str_contains($setup, "'non_unique' => 0"), diff --git a/web/tests/CloudFreshSchemaContractTest.php b/web/tests/CloudFreshSchemaContractTest.php index b62eac91..11e81cc9 100644 --- a/web/tests/CloudFreshSchemaContractTest.php +++ b/web/tests/CloudFreshSchemaContractTest.php @@ -27,10 +27,10 @@ $assert(stripos($schema, 'compatibility import') === false $assert(!preg_match('/\bengine_[A-Za-z0-9_]*\b/', $schema), 'cloud.sql retained an engine-prefixed identifier.'); $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v52-daily-traffic-settlement'") + "SCHEMA_VERSION = 'go-agent-wss-v53-traffic-settlement-receipts'") && !str_contains($setup, "SCHEMA_VERSION = 'go-v1'") - && str_contains($deployment, '`go-agent-wss-v52-daily-traffic-settlement`') - && str_contains($compensation, '`go-agent-wss-v52-daily-traffic-settlement`'), + && str_contains($deployment, '`go-agent-wss-v53-traffic-settlement-receipts`') + && str_contains($compensation, '`go-agent-wss-v53-traffic-settlement-receipts`'), 'Outbound Agent schema must reject the incompatible earlier Go database.'); $assert(str_contains($schema, "timezone VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'UTC'") diff --git a/web/tests/CloudMaintenanceNoticeSchemaContractTest.php b/web/tests/CloudMaintenanceNoticeSchemaContractTest.php index 6edc6419..14c12df3 100644 --- a/web/tests/CloudMaintenanceNoticeSchemaContractTest.php +++ b/web/tests/CloudMaintenanceNoticeSchemaContractTest.php @@ -56,7 +56,7 @@ foreach ([ } $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v52-daily-traffic-settlement'"), + "SCHEMA_VERSION = 'go-agent-wss-v53-traffic-settlement-receipts'"), 'CloudSetup schema version was not advanced for maintenance notices.'); echo "Maintenance notice schema contracts passed.\n"; diff --git a/web/tests/CloudPublicIpPoolReplacementSchemaContractTest.php b/web/tests/CloudPublicIpPoolReplacementSchemaContractTest.php index 1995b59a..7badf2cf 100644 --- a/web/tests/CloudPublicIpPoolReplacementSchemaContractTest.php +++ b/web/tests/CloudPublicIpPoolReplacementSchemaContractTest.php @@ -53,7 +53,7 @@ $assert(is_string($schema) 'Public-pool replacement lacks durable batch, child, or claim schema.'); $assert(is_string($setup) && str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v52-daily-traffic-settlement'") + "SCHEMA_VERSION = 'go-agent-wss-v53-traffic-settlement-receipts'") && str_contains($setup, "'cloud_public_ip_pool_replacement_batches'") && str_contains($setup, diff --git a/web/tests/CloudPublicNetworkTransitionSchemaContractTest.php b/web/tests/CloudPublicNetworkTransitionSchemaContractTest.php index 6e91c78b..33e54227 100644 --- a/web/tests/CloudPublicNetworkTransitionSchemaContractTest.php +++ b/web/tests/CloudPublicNetworkTransitionSchemaContractTest.php @@ -170,7 +170,7 @@ $assert(str_contains($setup, 'CloudSetup does not verify index uniqueness and ordered columns.'); $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v52-daily-traffic-settlement'") + "SCHEMA_VERSION = 'go-agent-wss-v53-traffic-settlement-receipts'") && str_contains($setup, "'cloud_public_network_transitions'") && str_contains($setup, "'cloud_public_network_transition_ips'") && str_contains($setup, "'reservation_ip_claim_id'") diff --git a/web/tests/CloudRescueCatalogSchemaContractTest.php b/web/tests/CloudRescueCatalogSchemaContractTest.php index 91a809ae..586f83b8 100644 --- a/web/tests/CloudRescueCatalogSchemaContractTest.php +++ b/web/tests/CloudRescueCatalogSchemaContractTest.php @@ -57,7 +57,7 @@ $assert($replica !== '' && str_contains($replica, 'cleanup_resolved_at BIGINT UNSIGNED'), 'Rescue replica cannot prove exact identity or retain cleanup debt.'); $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v52-daily-traffic-settlement'") + "SCHEMA_VERSION = 'go-agent-wss-v53-traffic-settlement-receipts'") && str_contains($setup, "'cloud_rescue_artifact_delete_due'") && str_contains($setup, "'cloud_rescue_replica_cleanup'"), 'CloudSetup version or rescue catalog indexes are incomplete.'); diff --git a/web/tests/CloudRestoreMigrationLedgerContractTest.php b/web/tests/CloudRestoreMigrationLedgerContractTest.php index 7f827600..2bb7e1ae 100644 --- a/web/tests/CloudRestoreMigrationLedgerContractTest.php +++ b/web/tests/CloudRestoreMigrationLedgerContractTest.php @@ -218,7 +218,7 @@ foreach ([ 'CloudSetup omits restore/migration ledger contract: ' . $required); } $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v52-daily-traffic-settlement'") + "SCHEMA_VERSION = 'go-agent-wss-v53-traffic-settlement-receipts'") && str_contains($attemptModel, "protected \$table = 'cloud_backup_restore_attempts';") && str_contains($attemptModel, "'active_attempt' => 'int'") diff --git a/web/tests/CloudServiceCompensationSchemaContractTest.php b/web/tests/CloudServiceCompensationSchemaContractTest.php index 0126743b..77fafde3 100644 --- a/web/tests/CloudServiceCompensationSchemaContractTest.php +++ b/web/tests/CloudServiceCompensationSchemaContractTest.php @@ -105,7 +105,7 @@ $assert(str_contains($setup, 'CloudSetup lost the open-compensation fence index contract.'); $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v52-daily-traffic-settlement'"), + "SCHEMA_VERSION = 'go-agent-wss-v53-traffic-settlement-receipts'"), 'CloudSetup schema version was not advanced for service compensation.'); $appliedShape = preg_match( diff --git a/web/tests/CloudStoragePublicationSchemaContractTest.php b/web/tests/CloudStoragePublicationSchemaContractTest.php index bd5c29ba..747fc8c3 100644 --- a/web/tests/CloudStoragePublicationSchemaContractTest.php +++ b/web/tests/CloudStoragePublicationSchemaContractTest.php @@ -138,7 +138,7 @@ $assert(is_string($publicationModel) && str_contains($artifactModel, "'maximum_size_bytes' => 'int'"), 'Storage publication/artifact model contracts are incomplete.'); $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v52-daily-traffic-settlement'"), + "SCHEMA_VERSION = 'go-agent-wss-v53-traffic-settlement-receipts'"), 'CloudSetup schema version was not advanced for durable publications.'); echo "Storage publication schema contracts passed.\n"; diff --git a/web/tests/CloudTemplateImportIntentContractTest.php b/web/tests/CloudTemplateImportIntentContractTest.php index 2d062084..8b79ed72 100644 --- a/web/tests/CloudTemplateImportIntentContractTest.php +++ b/web/tests/CloudTemplateImportIntentContractTest.php @@ -63,7 +63,7 @@ $assert(str_contains($schema, && str_contains($schema, 'CONSTRAINT chk_cloud_template_import_completion CHECK (') && str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v52-daily-traffic-settlement'"), + "SCHEMA_VERSION = 'go-agent-wss-v53-traffic-settlement-receipts'"), 'Template imports are missing durable identity or terminal-state constraints.'); $assert(str_contains($routes, "'/admin/cloud-template-imports'") && str_contains($routes, "'/admin/cloud-template-imports/{id}'") diff --git a/web/tests/CloudTrafficPoolAccountingTest.php b/web/tests/CloudTrafficPoolAccountingTest.php index a29db9f3..39e52cfc 100644 --- a/web/tests/CloudTrafficPoolAccountingTest.php +++ b/web/tests/CloudTrafficPoolAccountingTest.php @@ -153,6 +153,29 @@ $schema->create('cloud_traffic_usage_settlement_buckets', static function (Bluep 'bucket_revision', ], 'traffic_settlement_identity'); }); +$schema->create('cloud_traffic_settlement_receipts', static function (Blueprint $table): void { + $table->increments('id'); + $table->integer('pool_id'); + $table->integer('user_id'); + $table->integer('instance_id'); + $table->integer('service_period_id'); + $table->integer('entitlement_id')->nullable(); + $table->string('allocation_kind'); + $table->integer('allocation_target_id'); + $table->integer('bucket_start'); + $table->integer('bucket_seconds'); + $table->integer('max_revision'); + $table->integer('last_event_id'); + $table->integer('compacted_amount_bytes'); + $table->integer('compacted_event_count'); + $table->string('last_payload_hash'); + $table->integer('compacted_at'); + $table->integer('updated_at'); + $table->unique([ + 'pool_id', 'instance_id', 'service_period_id', 'allocation_kind', + 'allocation_target_id', 'bucket_start', 'bucket_seconds', + ], 'traffic_settlement_receipt_identity'); +}); $schema->create('cloud_traffic_instance_cursors', static function (Blueprint $table): void { $table->integer('instance_id')->primary(); $table->integer('user_id'); @@ -795,6 +818,32 @@ $assert($sealedBucketCount >= 1 && CloudTrafficLedgerEvent::where('instance_id', 72) ->where('event_type', 'usage_allocated')->count() === 1, 'Completed settlement buckets were not sealed exactly once.'); +$sealedBucket = Capsule::table('cloud_traffic_usage_settlement_buckets') + ->where('instance_id', 72)->where('state', 'sealed')->first(); +$sealedEvent = $sealedBucket === null ? null + : CloudTrafficLedgerEvent::find((int) $sealedBucket->sealed_event_id); +$assert($sealedBucket !== null && $sealedEvent !== null, + 'Sealed settlement fixture is incomplete.'); +Capsule::table('cloud_traffic_settlement_receipts')->insert([ + 'pool_id' => (int) $sealedBucket->pool_id, + 'user_id' => (int) $sealedBucket->user_id, + 'instance_id' => (int) $sealedBucket->instance_id, + 'service_period_id' => (int) $sealedBucket->service_period_id, + 'entitlement_id' => (int) $sealedBucket->entitlement_id, + 'allocation_kind' => (string) $sealedBucket->allocation_kind, + 'allocation_target_id' => (int) $sealedBucket->allocation_target_id, + 'bucket_start' => (int) $sealedBucket->bucket_start, + 'bucket_seconds' => (int) $sealedBucket->bucket_seconds, + 'max_revision' => (int) $sealedBucket->bucket_revision, + 'last_event_id' => (int) $sealedEvent->id, + 'compacted_amount_bytes' => (int) $sealedBucket->amount_bytes, + 'compacted_event_count' => 1, + 'last_payload_hash' => (string) $sealedEvent->payload_hash, + 'compacted_at' => $settlementDay + 86_400, + 'updated_at' => $settlementDay + 86_400, +]); +Capsule::table('cloud_traffic_usage_settlement_buckets') + ->where('id', (int) $sealedBucket->id)->delete(); $bucketedInstance->lifetime_download_bytes = 30; $bucketedInstance->lifetime_upload_bytes = 10; $bucketedInstance->traffic_synced_at = $settlementDay + 7_230; diff --git a/web/tests/CloudTrafficPoolAttentionIntegrationTest.php b/web/tests/CloudTrafficPoolAttentionIntegrationTest.php index 09a99994..ee459cf8 100644 --- a/web/tests/CloudTrafficPoolAttentionIntegrationTest.php +++ b/web/tests/CloudTrafficPoolAttentionIntegrationTest.php @@ -149,6 +149,25 @@ $schema->create('cloud_traffic_usage_settlement_buckets', static function (Bluep 'bucket_revision', ], 'traffic_settlement_identity'); }); +$schema->create('cloud_traffic_settlement_receipts', static function (Blueprint $table): void { + $table->increments('id'); + $table->integer('pool_id'); + $table->integer('user_id'); + $table->integer('instance_id'); + $table->integer('service_period_id'); + $table->integer('entitlement_id')->nullable(); + $table->string('allocation_kind'); + $table->integer('allocation_target_id'); + $table->integer('bucket_start'); + $table->integer('bucket_seconds'); + $table->integer('max_revision'); + $table->integer('last_event_id'); + $table->integer('compacted_amount_bytes'); + $table->integer('compacted_event_count'); + $table->string('last_payload_hash'); + $table->integer('compacted_at'); + $table->integer('updated_at'); +}); $schema->create('cloud_traffic_instance_cursors', static function (Blueprint $table): void { $table->integer('instance_id')->primary(); $table->integer('user_id'); diff --git a/web/tests/CloudTrafficPoolSchemaContractTest.php b/web/tests/CloudTrafficPoolSchemaContractTest.php index 97d944ba..f89982a6 100644 --- a/web/tests/CloudTrafficPoolSchemaContractTest.php +++ b/web/tests/CloudTrafficPoolSchemaContractTest.php @@ -24,6 +24,7 @@ foreach ([ 'cloud_traffic_entitlements', 'cloud_traffic_ledger_events', 'cloud_traffic_usage_settlement_buckets', + 'cloud_traffic_settlement_receipts', 'cloud_traffic_instance_cursors', 'cloud_traffic_scan_failures', 'cloud_instance_network_restrictions', @@ -51,6 +52,8 @@ $assert(str_contains($schema, 'allocation_kind, allocation_target_id, bucket_start, bucket_seconds,') && str_contains($schema, 'KEY cloud_traffic_settlement_due (state, seal_at, id)') + && str_contains($schema, + 'UNIQUE KEY cloud_traffic_settlement_receipt_identity (') && str_contains($schema, 'UNIQUE KEY cloud_traffic_scan_failure_subject (scan_kind, subject_id)'), 'Traffic-pool identity, FEFO or immutable ledger indexes are incomplete.'); @@ -78,7 +81,7 @@ $assert(str_contains($schema, 'Service-period traffic-pool identity is not frozen or indexed.'); $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v52-daily-traffic-settlement'") + "SCHEMA_VERSION = 'go-agent-wss-v53-traffic-settlement-receipts'") && str_contains($setup, "'chk_cloud_traffic_entitlement_balance'") && str_contains($setup, "'cloud_traffic_ledger_sequence'") && str_contains($setup, "'chk_cloud_traffic_settlement_state'") @@ -89,6 +92,7 @@ $expectedDeletionOrder = [ "['cloud_instance_network_policy_states', 'user_id']", "['cloud_instance_network_restrictions', 'user_id']", "['cloud_traffic_usage_settlement_buckets', 'user_id']", + "['cloud_traffic_settlement_receipts', 'user_id']", "['cloud_traffic_instance_cursors', 'user_id']", "['cloud_traffic_ledger_events', 'user_id']", "['cloud_traffic_entitlements', 'user_id']", @@ -115,6 +119,7 @@ foreach ([ 'CloudTrafficPoolGroup', 'CloudTrafficPool', 'CloudTrafficEntitlement', 'CloudTrafficLedgerEvent', 'CloudTrafficInstanceCursor', 'CloudTrafficUsageSettlementBucket', + 'CloudTrafficSettlementReceipt', 'CloudTrafficScanFailure', 'CloudInstanceNetworkRestriction', 'CloudInstanceNetworkPolicyState', diff --git a/web/tests/CloudVpcSchemaContractTest.php b/web/tests/CloudVpcSchemaContractTest.php index ca2d1680..f28a7658 100644 --- a/web/tests/CloudVpcSchemaContractTest.php +++ b/web/tests/CloudVpcSchemaContractTest.php @@ -101,7 +101,7 @@ $assert(str_contains($jobs, 'vpc_id BIGINT UNSIGNED DEFAULT NULL') 'Cloud jobs cannot be correlated to VPC or VPC-port work.'); $assert(str_contains($setup, - "private const SCHEMA_VERSION = 'go-agent-wss-v52-daily-traffic-settlement'") + "private const SCHEMA_VERSION = 'go-agent-wss-v53-traffic-settlement-receipts'") && str_contains($setup, "'name' => 'cloud_vpc_enabled'") && str_contains($setup, "'value' => 'false'") && str_contains($setup, diff --git a/web/tests/ManualAccountDeletionSqliteTest.php b/web/tests/ManualAccountDeletionSqliteTest.php index fd4cd3f0..cb540f2b 100644 --- a/web/tests/ManualAccountDeletionSqliteTest.php +++ b/web/tests/ManualAccountDeletionSqliteTest.php @@ -196,6 +196,7 @@ $schema->create('cloud_backup_period_entitlements', static function (Blueprint $ }); foreach (['cloud_instance_network_restrictions', 'cloud_traffic_usage_settlement_buckets', + 'cloud_traffic_settlement_receipts', 'cloud_traffic_instance_cursors', 'cloud_traffic_ledger_events', 'cloud_traffic_entitlements', 'cloud_traffic_pools'] as $tableName) { $schema->create($tableName, static function (Blueprint $table) { @@ -358,6 +359,7 @@ Capsule::table('cloud_backup_period_entitlements')->insert([ ]); foreach (['cloud_instance_network_restrictions', 'cloud_traffic_usage_settlement_buckets', + 'cloud_traffic_settlement_receipts', 'cloud_traffic_instance_cursors', 'cloud_traffic_ledger_events', 'cloud_traffic_entitlements', 'cloud_traffic_pools'] as $tableName) { Capsule::table($tableName)->insert([ @@ -457,6 +459,7 @@ foreach (['cloud_instances', 'cloud_orders', 'cloud_instance_snapshots', 'cloud_backup_period_entitlements', 'cloud_invoices', 'cloud_invoice_payments', 'cloud_instance_network_restrictions', 'cloud_traffic_usage_settlement_buckets', + 'cloud_traffic_settlement_receipts', 'cloud_traffic_instance_cursors', 'cloud_traffic_ledger_events', 'cloud_traffic_entitlements', 'cloud_traffic_pools'] as $tableName) { $assert(Capsule::table($tableName)->pluck('user_id')->all() === [3], diff --git a/web/tests/UiCopyCatalogSchemaContractTest.php b/web/tests/UiCopyCatalogSchemaContractTest.php index b989564a..d2b77c10 100644 --- a/web/tests/UiCopyCatalogSchemaContractTest.php +++ b/web/tests/UiCopyCatalogSchemaContractTest.php @@ -104,7 +104,7 @@ foreach ($models as $class => $modelTable) { } $assert(str_contains($setup, - "SCHEMA_VERSION = 'go-agent-wss-v52-daily-traffic-settlement'") + "SCHEMA_VERSION = 'go-agent-wss-v53-traffic-settlement-receipts'") && str_contains($setup, "self::schema('ss.sql') . \"\\0\" . self::schema('cloud.sql')") && str_contains($setup,