feat(cloud): add service compensation ledger schema

go
codex 2 weeks ago
parent 711bcb9a5d
commit 072fb4b1ba

@ -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-v34-maintenance-notices';
private const SCHEMA_VERSION = 'go-agent-wss-v35-service-compensations';
private const MAIN_TABLES = [
'cloud_regions', 'cloud_traffic_pool_groups',
'cloud_network_lines', 'cloud_resource_pools',
@ -53,6 +53,9 @@ final class CloudSetup
'cloud_mail_outbox', 'cloud_maintenance_notices',
'cloud_maintenance_notice_instances',
'cloud_maintenance_notice_recipients',
'cloud_service_compensations',
'cloud_service_compensation_items',
'cloud_service_compensation_recipients',
'cloud_jobs', 'cloud_agent_completion_events',
'cloud_ssh_keys',
'cloud_sessions', 'cloud_console_tickets',
@ -343,6 +346,38 @@ final class CloudSetup
'sent_at', 'last_error_code', 'last_error', 'created_at',
'updated_at',
],
'cloud_service_compensations' => [
'id', 'compensation_key', 'incident_key',
'requested_by_user_id', 'request_idempotency_key',
'request_fingerprint', 'scope_type', 'scope_id',
'scope_snapshot_json', 'reason_text', 'incident_starts_at',
'incident_ends_at', 'compensation_seconds', 'notify_users',
'audience_digest', 'instance_count', 'recipient_count',
'applied_count', 'deferred_count', 'rejected_count',
'attention_count', 'state', 'last_error_code', 'terminal_at',
'created_at', 'updated_at',
],
'cloud_service_compensation_items' => [
'id', 'item_key', 'batch_id', 'incident_key', 'instance_id',
'user_id', 'host_id', 'instance_name', 'requested_seconds',
'service_period_id', 'traffic_entitlement_id',
'backup_entitlement_id', 'source_invoice_id',
'replacement_invoice_id', 'audit_job_id', 'before_expires_at',
'after_expires_at', 'before_period_ends_at',
'after_period_ends_at', 'before_traffic_ends_at',
'after_traffic_ends_at', 'before_backup_ends_at',
'after_backup_ends_at', 'before_snapshot_json',
'after_snapshot_json', 'state', 'attempts', 'available_at',
'lease_token', 'lease_until', 'applied_at', 'terminal_at',
'last_error_code', 'last_error', 'created_at', 'updated_at',
],
'cloud_service_compensation_recipients' => [
'id', 'batch_id', 'user_id', 'email_snapshot',
'instance_count', 'payload_json', 'payload_sha256', 'status',
'attempts', 'available_at', 'lease_token', 'lease_until',
'sent_at', 'last_error_code', 'last_error', 'created_at',
'updated_at',
],
'cloud_instance_migrations' => [
'id', 'migration_key', 'instance_id', 'vpc_port_id', 'restore_id',
'source_host_id', 'source_uuid', 'source_reservation_id',
@ -623,6 +658,27 @@ final class CloudSetup
'chk_cloud_maintenance_notice_recipient_status',
'chk_cloud_maintenance_notice_recipient_shape',
],
'cloud_service_compensations' => [
'chk_cloud_service_compensation_scope',
'chk_cloud_service_compensation_request',
'chk_cloud_service_compensation_window',
'chk_cloud_service_compensation_counts',
'chk_cloud_service_compensation_state',
'chk_cloud_service_compensation_terminal',
],
'cloud_service_compensation_items' => [
'chk_cloud_service_compensation_item_state',
'chk_cloud_service_compensation_item_window',
'chk_cloud_service_compensation_item_lease',
'chk_cloud_service_compensation_item_terminal',
'chk_cloud_service_compensation_item_applied',
'chk_cloud_service_compensation_item_error',
],
'cloud_service_compensation_recipients' => [
'chk_cloud_service_compensation_recipient_status',
'chk_cloud_service_compensation_recipient_payload',
'chk_cloud_service_compensation_recipient_shape',
],
'cloud_jobs' => [
'chk_cloud_job_dispatch_once', 'chk_cloud_job_attempt_shape',
'chk_cloud_job_phase', 'chk_cloud_job_outcome',
@ -823,6 +879,31 @@ final class CloudSetup
'cloud_maintenance_notice_recipient_lease',
'cloud_maintenance_notice_recipient_owner',
],
'cloud_service_compensations' => [
'PRIMARY', 'cloud_service_compensation_key',
'cloud_service_compensation_request',
'cloud_service_compensation_incident_identity',
'cloud_service_compensation_incident',
'cloud_service_compensation_scope',
'cloud_service_compensation_state',
],
'cloud_service_compensation_items' => [
'PRIMARY', 'cloud_service_compensation_item_key',
'cloud_service_compensation_item_instance',
'cloud_service_compensation_item_incident',
'cloud_service_compensation_item_batch_state',
'cloud_service_compensation_item_due',
'cloud_service_compensation_item_lease',
'cloud_service_compensation_item_owner',
'cloud_service_compensation_item_instance_history',
'cloud_service_compensation_item_audit_job',
],
'cloud_service_compensation_recipients' => [
'PRIMARY', 'cloud_service_compensation_recipient',
'cloud_service_compensation_recipient_due',
'cloud_service_compensation_recipient_lease',
'cloud_service_compensation_recipient_owner',
],
'cloud_jobs' => [
'PRIMARY', 'uniq_cloud_operation', 'uniq_cloud_job_request',
'uniq_cloud_job_completion_event', 'uniq_cloud_provision_order',
@ -1003,6 +1084,54 @@ final class CloudSetup
'columns' => ['status', 'lease_until', 'id'],
],
],
'cloud_service_compensations' => [
'cloud_service_compensation_key' => [
'non_unique' => 0,
'columns' => ['compensation_key'],
],
'cloud_service_compensation_request' => [
'non_unique' => 0,
'columns' => [
'requested_by_user_id', 'request_idempotency_key',
],
],
'cloud_service_compensation_incident_identity' => [
'non_unique' => 0,
'columns' => ['id', 'incident_key'],
],
],
'cloud_service_compensation_items' => [
'cloud_service_compensation_item_instance' => [
'non_unique' => 0,
'columns' => ['batch_id', 'instance_id'],
],
'cloud_service_compensation_item_incident' => [
'non_unique' => 0,
'columns' => ['incident_key', 'instance_id'],
],
'cloud_service_compensation_item_due' => [
'non_unique' => 1,
'columns' => ['state', 'available_at', 'id'],
],
'cloud_service_compensation_item_lease' => [
'non_unique' => 1,
'columns' => ['state', 'lease_until', 'id'],
],
],
'cloud_service_compensation_recipients' => [
'cloud_service_compensation_recipient' => [
'non_unique' => 0,
'columns' => ['batch_id', 'user_id'],
],
'cloud_service_compensation_recipient_due' => [
'non_unique' => 1,
'columns' => ['status', 'available_at', 'id'],
],
'cloud_service_compensation_recipient_lease' => [
'non_unique' => 1,
'columns' => ['status', 'lease_until', 'id'],
],
],
'cloud_backup_restores' => [
'cloud_backup_restore_request' => [
'non_unique' => 0,
@ -1143,6 +1272,12 @@ final class CloudSetup
'cloud_maintenance_notice_recipients' => [
'fk_cloud_maintenance_notice_recipient_notice',
],
'cloud_service_compensation_items' => [
'fk_cloud_service_compensation_item_batch',
],
'cloud_service_compensation_recipients' => [
'fk_cloud_service_compensation_recipient_batch',
],
'cloud_jobs' => ['fk_cloud_job_user'],
];

@ -0,0 +1,27 @@
<?php
namespace App\Models;
final class CloudServiceCompensation extends Model
{
protected $connection = 'default';
protected $table = 'cloud_service_compensations';
protected $casts = [
'id' => 'int',
'requested_by_user_id' => 'int',
'scope_id' => 'int',
'incident_starts_at' => 'int',
'incident_ends_at' => 'int',
'compensation_seconds' => 'int',
'notify_users' => 'bool',
'instance_count' => 'int',
'recipient_count' => 'int',
'applied_count' => 'int',
'deferred_count' => 'int',
'rejected_count' => 'int',
'attention_count' => 'int',
'terminal_at' => 'int',
'created_at' => 'int',
'updated_at' => 'int',
];
}

@ -0,0 +1,38 @@
<?php
namespace App\Models;
final class CloudServiceCompensationItem extends Model
{
protected $connection = 'default';
protected $table = 'cloud_service_compensation_items';
protected $casts = [
'id' => 'int',
'batch_id' => 'int',
'instance_id' => 'int',
'user_id' => 'int',
'host_id' => 'int',
'requested_seconds' => 'int',
'service_period_id' => 'int',
'traffic_entitlement_id' => 'int',
'backup_entitlement_id' => 'int',
'source_invoice_id' => 'int',
'replacement_invoice_id' => 'int',
'audit_job_id' => 'int',
'before_expires_at' => 'int',
'after_expires_at' => 'int',
'before_period_ends_at' => 'int',
'after_period_ends_at' => 'int',
'before_traffic_ends_at' => 'int',
'after_traffic_ends_at' => 'int',
'before_backup_ends_at' => 'int',
'after_backup_ends_at' => 'int',
'attempts' => 'int',
'available_at' => 'int',
'lease_until' => 'int',
'applied_at' => 'int',
'terminal_at' => 'int',
'created_at' => 'int',
'updated_at' => 'int',
];
}

@ -0,0 +1,21 @@
<?php
namespace App\Models;
final class CloudServiceCompensationRecipient extends Model
{
protected $connection = 'default';
protected $table = 'cloud_service_compensation_recipients';
protected $casts = [
'id' => 'int',
'batch_id' => 'int',
'user_id' => 'int',
'instance_count' => 'int',
'attempts' => 'int',
'available_at' => 'int',
'lease_until' => 'int',
'sent_at' => 'int',
'created_at' => 'int',
'updated_at' => 'int',
];
}

@ -24,6 +24,8 @@ class UserDataDeletionService
// remove. cloud_jobs may contain encrypted provisioning credentials.
['cloud_vpc_membership_batch_items', 'user_id'],
['cloud_vpc_membership_batches', 'user_id'],
['cloud_service_compensation_recipients', 'user_id'],
['cloud_service_compensation_items', 'user_id'],
['cloud_maintenance_notice_recipients', 'user_id'],
['cloud_maintenance_notice_instances', 'user_id'],
['cloud_jobs', 'user_id'],
@ -100,6 +102,38 @@ class UserDataDeletionService
'用户仍有支付安全栅栏,请先完成网关或链上状态处理');
}
}
$applyingCompensationItems = collect();
if ($this->hasColumns(
$connection,
'cloud_service_compensation_items',
['id', 'user_id', 'state']
)) {
$applyingCompensationItems = $connection->table(
'cloud_service_compensation_items'
)->whereIn('user_id', $ids)->where('state', 'applying')
->orderBy('id')->lockForUpdate()->get(['id']);
}
if (!$applyingCompensationItems->isEmpty()) {
throw new \RuntimeException(
'用户仍有应用中的实例补偿,请等待补偿结果核对后再删除账户'
);
}
$sendingCompensationRecipients = collect();
if ($this->hasColumns(
$connection,
'cloud_service_compensation_recipients',
['id', 'user_id', 'status']
)) {
$sendingCompensationRecipients = $connection->table(
'cloud_service_compensation_recipients'
)->whereIn('user_id', $ids)->where('status', 'sending')
->orderBy('id')->lockForUpdate()->get(['id']);
}
if (!$sendingCompensationRecipients->isEmpty()) {
throw new \RuntimeException(
'用户仍有投递中的补偿通知,请等待投递结果核对后再删除账户'
);
}
$maintenanceNoticeIds = [];
if ($this->hasColumns(
$connection,

@ -2296,6 +2296,232 @@ CREATE TABLE cloud_maintenance_notice_recipients (
ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- One administrator service-compensation request freezes an affected scope and
-- its exact per-instance evidence. The batch is retained as aggregate audit
-- after tenant PII in item and recipient rows is removed during account
-- deletion.
CREATE TABLE cloud_service_compensations (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
compensation_key CHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
incident_key VARCHAR(128) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
requested_by_user_id BIGINT UNSIGNED NOT NULL,
request_idempotency_key VARCHAR(128) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
request_fingerprint CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
scope_type VARCHAR(24) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
scope_id BIGINT UNSIGNED NOT NULL,
scope_snapshot_json LONGTEXT NOT NULL,
reason_text VARCHAR(500) NOT NULL,
incident_starts_at BIGINT UNSIGNED DEFAULT NULL,
incident_ends_at BIGINT UNSIGNED DEFAULT NULL,
compensation_seconds BIGINT UNSIGNED NOT NULL,
notify_users TINYINT(1) NOT NULL DEFAULT 1,
audience_digest CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
instance_count INT UNSIGNED NOT NULL,
recipient_count INT UNSIGNED NOT NULL DEFAULT 0,
applied_count INT UNSIGNED NOT NULL DEFAULT 0,
deferred_count INT UNSIGNED NOT NULL DEFAULT 0,
rejected_count INT UNSIGNED NOT NULL DEFAULT 0,
attention_count INT UNSIGNED NOT NULL DEFAULT 0,
state VARCHAR(24) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'pending',
last_error_code VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '',
terminal_at BIGINT UNSIGNED DEFAULT NULL,
created_at BIGINT UNSIGNED NOT NULL,
updated_at BIGINT UNSIGNED NOT NULL,
UNIQUE KEY cloud_service_compensation_key (compensation_key),
UNIQUE KEY cloud_service_compensation_request (
requested_by_user_id, request_idempotency_key
),
UNIQUE KEY cloud_service_compensation_incident_identity (id, incident_key),
KEY cloud_service_compensation_incident (incident_key, id),
KEY cloud_service_compensation_scope (scope_type, scope_id, created_at, id),
KEY cloud_service_compensation_state (state, created_at, id),
CONSTRAINT chk_cloud_service_compensation_scope CHECK (
scope_type IN ('instance','host','public_ip_pool') AND scope_id > 0
),
CONSTRAINT chk_cloud_service_compensation_request CHECK (
CHAR_LENGTH(request_idempotency_key) BETWEEN 16 AND 128
AND CHAR_LENGTH(request_fingerprint) = 64
AND CHAR_LENGTH(incident_key) BETWEEN 1 AND 128
AND CHAR_LENGTH(audience_digest) = 64
AND CHAR_LENGTH(reason_text) BETWEEN 1 AND 500
),
CONSTRAINT chk_cloud_service_compensation_window CHECK (
compensation_seconds BETWEEN 1 AND 31622400
AND (
(incident_starts_at IS NULL AND incident_ends_at IS NULL)
OR (incident_starts_at IS NOT NULL AND incident_ends_at IS NOT NULL
AND incident_ends_at > incident_starts_at)
)
),
CONSTRAINT chk_cloud_service_compensation_counts CHECK (
notify_users IN (0,1)
AND instance_count > 0
AND recipient_count <= instance_count
AND applied_count + deferred_count + rejected_count + attention_count
<= instance_count
),
CONSTRAINT chk_cloud_service_compensation_state CHECK (
state IN ('pending','applying','completed','partial','attention')
),
CONSTRAINT chk_cloud_service_compensation_terminal CHECK (
(state IN ('pending','applying') AND terminal_at IS NULL)
OR (state IN ('completed','partial','attention') AND terminal_at IS NOT NULL)
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Per-instance rows are both the durable work queue and the immutable before /
-- after audit. The incident key is repeated under a composite foreign key so
-- an item can never be attached to a batch for another incident.
CREATE TABLE cloud_service_compensation_items (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
item_key CHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
batch_id BIGINT UNSIGNED NOT NULL,
incident_key VARCHAR(128) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
instance_id BIGINT UNSIGNED NOT NULL,
user_id BIGINT UNSIGNED NOT NULL,
host_id BIGINT UNSIGNED NOT NULL,
instance_name VARCHAR(63) NOT NULL,
requested_seconds BIGINT UNSIGNED NOT NULL,
service_period_id BIGINT UNSIGNED DEFAULT NULL,
traffic_entitlement_id BIGINT UNSIGNED DEFAULT NULL,
backup_entitlement_id BIGINT UNSIGNED DEFAULT NULL,
source_invoice_id BIGINT UNSIGNED DEFAULT NULL,
replacement_invoice_id BIGINT UNSIGNED DEFAULT NULL,
audit_job_id BIGINT UNSIGNED DEFAULT NULL,
before_expires_at BIGINT UNSIGNED DEFAULT NULL,
after_expires_at BIGINT UNSIGNED DEFAULT NULL,
before_period_ends_at BIGINT UNSIGNED DEFAULT NULL,
after_period_ends_at BIGINT UNSIGNED DEFAULT NULL,
before_traffic_ends_at BIGINT UNSIGNED DEFAULT NULL,
after_traffic_ends_at BIGINT UNSIGNED DEFAULT NULL,
before_backup_ends_at BIGINT UNSIGNED DEFAULT NULL,
after_backup_ends_at BIGINT UNSIGNED DEFAULT NULL,
before_snapshot_json LONGTEXT NOT NULL,
after_snapshot_json LONGTEXT DEFAULT NULL,
state VARCHAR(24) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'pending',
attempts INT UNSIGNED NOT NULL DEFAULT 0,
available_at BIGINT UNSIGNED NOT NULL,
lease_token CHAR(32) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL,
lease_until BIGINT UNSIGNED NOT NULL DEFAULT 0,
applied_at BIGINT UNSIGNED DEFAULT NULL,
terminal_at BIGINT UNSIGNED DEFAULT NULL,
last_error_code VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '',
last_error VARCHAR(500) NOT NULL DEFAULT '',
created_at BIGINT UNSIGNED NOT NULL,
updated_at BIGINT UNSIGNED NOT NULL,
UNIQUE KEY cloud_service_compensation_item_key (item_key),
UNIQUE KEY cloud_service_compensation_item_instance (batch_id, instance_id),
UNIQUE KEY cloud_service_compensation_item_incident (incident_key, instance_id),
KEY cloud_service_compensation_item_batch_state (batch_id, state, id),
KEY cloud_service_compensation_item_due (state, available_at, id),
KEY cloud_service_compensation_item_lease (state, lease_until, id),
KEY cloud_service_compensation_item_owner (user_id, created_at, id),
KEY cloud_service_compensation_item_instance_history (instance_id, created_at, id),
KEY cloud_service_compensation_item_audit_job (audit_job_id, id),
CONSTRAINT chk_cloud_service_compensation_item_state CHECK (
state IN (
'pending','applying','deferred','applied',
'rejected','attention','cancelled'
)
),
CONSTRAINT chk_cloud_service_compensation_item_window CHECK (
requested_seconds BETWEEN 1 AND 31622400 AND available_at > 0
),
CONSTRAINT chk_cloud_service_compensation_item_lease CHECK (
(state = 'applying' AND attempts > 0
AND lease_token IS NOT NULL AND lease_until > 0)
OR (state <> 'applying' AND lease_token IS NULL AND lease_until = 0)
),
CONSTRAINT chk_cloud_service_compensation_item_terminal CHECK (
(state IN ('pending','applying','deferred') AND terminal_at IS NULL)
OR (state IN ('applied','rejected','attention','cancelled')
AND terminal_at IS NOT NULL)
),
CONSTRAINT chk_cloud_service_compensation_item_applied CHECK (
state <> 'applied'
OR (
applied_at IS NOT NULL
AND before_expires_at IS NOT NULL AND after_expires_at IS NOT NULL
AND after_expires_at = before_expires_at + requested_seconds
AND before_period_ends_at IS NOT NULL
AND after_period_ends_at = before_period_ends_at + requested_seconds
AND (
(before_traffic_ends_at IS NULL AND after_traffic_ends_at IS NULL)
OR (before_traffic_ends_at IS NOT NULL
AND after_traffic_ends_at = before_traffic_ends_at + requested_seconds)
)
AND (
(before_backup_ends_at IS NULL AND after_backup_ends_at IS NULL)
OR (before_backup_ends_at IS NOT NULL
AND after_backup_ends_at = before_backup_ends_at + requested_seconds)
)
AND after_snapshot_json IS NOT NULL
AND last_error_code = '' AND last_error = ''
)
),
CONSTRAINT chk_cloud_service_compensation_item_error CHECK (
state NOT IN ('deferred','rejected','attention','cancelled')
OR last_error_code <> ''
),
CONSTRAINT fk_cloud_service_compensation_item_batch
FOREIGN KEY (batch_id, incident_key)
REFERENCES cloud_service_compensations (id, incident_key)
ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Compensation mail uses the same no-blind-replay lease contract as maintenance
-- notices. The exact payload is frozen so an unknown SMTP effect can be
-- inspected without recomposing changed instance data.
CREATE TABLE cloud_service_compensation_recipients (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
batch_id BIGINT UNSIGNED NOT NULL,
user_id BIGINT UNSIGNED NOT NULL,
email_snapshot VARCHAR(254) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
instance_count INT UNSIGNED NOT NULL,
payload_json LONGTEXT NOT NULL,
payload_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
status VARCHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'pending',
attempts INT UNSIGNED NOT NULL DEFAULT 0,
available_at BIGINT UNSIGNED NOT NULL,
lease_token CHAR(32) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL,
lease_until BIGINT UNSIGNED NOT NULL DEFAULT 0,
sent_at BIGINT UNSIGNED DEFAULT NULL,
last_error_code VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '',
last_error VARCHAR(500) NOT NULL DEFAULT '',
created_at BIGINT UNSIGNED NOT NULL,
updated_at BIGINT UNSIGNED NOT NULL,
UNIQUE KEY cloud_service_compensation_recipient (batch_id, user_id),
KEY cloud_service_compensation_recipient_due (status, available_at, id),
KEY cloud_service_compensation_recipient_lease (status, lease_until, id),
KEY cloud_service_compensation_recipient_owner (user_id, created_at, id),
CONSTRAINT chk_cloud_service_compensation_recipient_status CHECK (
status IN (
'pending','sending','sent','failed','unknown','skipped','cancelled'
)
),
CONSTRAINT chk_cloud_service_compensation_recipient_payload CHECK (
instance_count > 0 AND CHAR_LENGTH(payload_sha256) = 64
),
CONSTRAINT chk_cloud_service_compensation_recipient_shape CHECK (
available_at > 0
AND (
(status = 'pending' AND lease_token IS NULL AND lease_until = 0
AND sent_at IS NULL AND last_error_code = '' AND last_error = '')
OR (status = 'sending' AND attempts > 0
AND lease_token IS NOT NULL AND lease_until > 0 AND sent_at IS NULL)
OR (status = 'sent' AND lease_token IS NULL AND lease_until = 0
AND sent_at IS NOT NULL AND last_error_code = '' AND last_error = '')
OR (status IN ('failed','unknown') AND lease_token IS NULL
AND lease_until = 0 AND sent_at IS NULL AND last_error_code <> '')
OR (status IN ('skipped','cancelled') AND lease_token IS NULL
AND lease_until = 0 AND sent_at IS NULL AND last_error_code <> '')
)
),
CONSTRAINT fk_cloud_service_compensation_recipient_batch
FOREIGN KEY (batch_id) REFERENCES cloud_service_compensations (id)
ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE cloud_jobs (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
operation_id CHAR(32) NOT NULL,

@ -22,7 +22,7 @@ $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-v34-maintenance-notices'")
"SCHEMA_VERSION = 'go-agent-wss-v35-service-compensations'")
&& !str_contains($setup, "SCHEMA_VERSION = 'go-v1'"),
'Outbound Agent schema must reject the incompatible earlier Go database.');
$assert(str_contains($schema,

@ -56,7 +56,7 @@ foreach ([
}
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v34-maintenance-notices'"),
"SCHEMA_VERSION = 'go-agent-wss-v35-service-compensations'"),
'CloudSetup schema version was not advanced for maintenance notices.');
echo "Maintenance notice schema contracts passed.\n";

@ -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-v34-maintenance-notices'")
"SCHEMA_VERSION = 'go-agent-wss-v35-service-compensations'")
&& str_contains($setup,
"'cloud_public_ip_pool_replacement_batches'")
&& str_contains($setup,

@ -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-v34-maintenance-notices'")
"SCHEMA_VERSION = 'go-agent-wss-v35-service-compensations'")
&& str_contains($setup, "'cloud_public_network_transitions'")
&& str_contains($setup, "'cloud_public_network_transition_ips'")
&& str_contains($setup, "'reservation_ip_claim_id'")

@ -56,7 +56,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-v34-maintenance-notices'")
"SCHEMA_VERSION = 'go-agent-wss-v35-service-compensations'")
&& str_contains($setup, "'cloud_rescue_artifact_delete_due'")
&& str_contains($setup, "'cloud_rescue_replica_cleanup'"),
'CloudSetup version or rescue catalog indexes are incomplete.');

@ -218,7 +218,7 @@ foreach ([
'CloudSetup omits restore/migration ledger contract: ' . $required);
}
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v34-maintenance-notices'")
"SCHEMA_VERSION = 'go-agent-wss-v35-service-compensations'")
&& str_contains($attemptModel,
"protected \$table = 'cloud_backup_restore_attempts';")
&& str_contains($attemptModel, "'active_attempt' => 'int'")

@ -0,0 +1,30 @@
<?php
$root = dirname(__DIR__);
$deletion = file_get_contents(
$root . '/app/Services/UserDataDeletionService.php'
);
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$assert(str_contains($deletion,
"['cloud_service_compensation_recipients', 'user_id']")
&& str_contains($deletion,
"['cloud_service_compensation_items', 'user_id']"),
'Account deletion does not remove compensation tenant PII.');
$assert(!str_contains($deletion,
"['cloud_service_compensations', 'requested_by_user_id']"),
'Account deletion must retain compensation batch audit.');
$assert(str_contains($deletion,
"->whereIn('user_id', \$ids)->where('state', 'applying')")
&& str_contains($deletion, '应用中的实例补偿'),
'Account deletion can erase an applying compensation item.');
$assert(str_contains($deletion,
"->whereIn('user_id', \$ids)->where('status', 'sending')")
&& str_contains($deletion, '投递中的补偿通知'),
'Account deletion can erase an in-flight compensation delivery.');
echo "Service compensation account-deletion contracts passed.\n";

@ -0,0 +1,64 @@
<?php
$root = dirname(__DIR__);
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$models = [
'CloudServiceCompensation.php' => [
'cloud_service_compensations',
[
'requested_by_user_id', 'scope_id', 'incident_starts_at',
'incident_ends_at', 'compensation_seconds', 'notify_users',
'instance_count', 'recipient_count', 'applied_count',
'deferred_count', 'rejected_count', 'attention_count',
'terminal_at', 'created_at', 'updated_at',
],
],
'CloudServiceCompensationItem.php' => [
'cloud_service_compensation_items',
[
'batch_id', 'instance_id', 'user_id', 'host_id',
'requested_seconds', 'service_period_id',
'traffic_entitlement_id', 'backup_entitlement_id',
'source_invoice_id', 'replacement_invoice_id', 'audit_job_id',
'before_expires_at', 'after_expires_at',
'before_period_ends_at', 'after_period_ends_at',
'before_traffic_ends_at', 'after_traffic_ends_at',
'before_backup_ends_at', 'after_backup_ends_at', 'attempts',
'available_at', 'lease_until', 'applied_at', 'terminal_at',
],
],
'CloudServiceCompensationRecipient.php' => [
'cloud_service_compensation_recipients',
[
'batch_id', 'user_id', 'instance_count', 'attempts',
'available_at', 'lease_until', 'sent_at', 'created_at',
'updated_at',
],
],
];
foreach ($models as $file => [$table, $casts]) {
$source = file_get_contents($root . '/app/Models/' . $file);
$assert(str_contains($source, "protected \$connection = 'default';"),
$file . ' does not use the default business connection.');
$assert(str_contains($source, "protected \$table = '" . $table . "';"),
$file . ' does not bind the expected table.');
foreach ($casts as $cast) {
$assert(str_contains($source, "'" . $cast . "' =>"),
$file . ' is missing cast ' . $cast . '.');
}
foreach ([
'scope_snapshot_json', 'before_snapshot_json',
'after_snapshot_json', 'payload_json',
] as $jsonColumn) {
$assert(!str_contains($source, "'" . $jsonColumn . "' => 'array'"),
$file . ' must retain canonical JSON string ' . $jsonColumn . '.');
}
}
echo "Service compensation model contracts passed.\n";

@ -0,0 +1,82 @@
<?php
$root = dirname(__DIR__);
$schema = file_get_contents($root . '/sql/cloud.sql');
$setup = file_get_contents($root . '/app/Command/CloudSetup.php');
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
foreach ([
'cloud_service_compensations',
'cloud_service_compensation_items',
'cloud_service_compensation_recipients',
] as $table) {
$assert(str_contains($schema, 'CREATE TABLE ' . $table . ' ('),
'Service compensation schema is missing ' . $table . '.');
$assert(str_contains($setup, "'" . $table . "'"),
'CloudSetup does not require ' . $table . '.');
}
foreach ([
'UNIQUE KEY cloud_service_compensation_request',
'UNIQUE KEY cloud_service_compensation_incident_identity (id, incident_key)',
'UNIQUE KEY cloud_service_compensation_item_incident (incident_key, instance_id)',
'UNIQUE KEY cloud_service_compensation_recipient (batch_id, user_id)',
'KEY cloud_service_compensation_item_due (state, available_at, id)',
'KEY cloud_service_compensation_item_lease (state, lease_until, id)',
'KEY cloud_service_compensation_recipient_due (status, available_at, id)',
'KEY cloud_service_compensation_recipient_lease (status, lease_until, id)',
'fk_cloud_service_compensation_item_batch',
'FOREIGN KEY (batch_id, incident_key)',
'fk_cloud_service_compensation_recipient_batch',
"scope_type IN ('instance','host','public_ip_pool')",
'compensation_seconds BETWEEN 1 AND 31622400',
] as $contract) {
$assert(str_contains($schema, $contract),
'Service compensation schema lost contract: ' . $contract);
}
foreach ([
'chk_cloud_service_compensation_scope',
'chk_cloud_service_compensation_request',
'chk_cloud_service_compensation_window',
'chk_cloud_service_compensation_counts',
'chk_cloud_service_compensation_state',
'chk_cloud_service_compensation_terminal',
'chk_cloud_service_compensation_item_state',
'chk_cloud_service_compensation_item_window',
'chk_cloud_service_compensation_item_lease',
'chk_cloud_service_compensation_item_terminal',
'chk_cloud_service_compensation_item_applied',
'chk_cloud_service_compensation_item_error',
'chk_cloud_service_compensation_recipient_status',
'chk_cloud_service_compensation_recipient_payload',
'chk_cloud_service_compensation_recipient_shape',
] as $check) {
$assert(str_contains($setup, "'" . $check . "'"),
'CloudSetup lost service compensation check: ' . $check);
}
foreach ([
"'columns' => ['id', 'incident_key']",
"'columns' => ['incident_key', 'instance_id']",
"'columns' => ['state', 'available_at', 'id']",
"'columns' => ['state', 'lease_until', 'id']",
"'columns' => ['batch_id', 'user_id']",
"'columns' => ['status', 'available_at', 'id']",
"'columns' => ['status', 'lease_until', 'id']",
"'fk_cloud_service_compensation_item_batch'",
"'fk_cloud_service_compensation_recipient_batch'",
] as $setupContract) {
$assert(str_contains($setup, $setupContract),
'CloudSetup lost service compensation contract: ' . $setupContract);
}
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v35-service-compensations'"),
'CloudSetup schema version was not advanced for service compensation.');
echo "Service compensation schema contracts passed.\n";

@ -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-v34-maintenance-notices'"),
"SCHEMA_VERSION = 'go-agent-wss-v35-service-compensations'"),
'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}'")

@ -71,7 +71,7 @@ $assert(str_contains($schema,
'Service-period traffic-pool identity is not frozen or indexed.');
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v34-maintenance-notices'")
"SCHEMA_VERSION = 'go-agent-wss-v35-service-compensations'")
&& str_contains($setup, "'chk_cloud_traffic_entitlement_balance'")
&& str_contains($setup, "'cloud_traffic_ledger_sequence'"),
'CloudSetup schema version or named-object validation is incomplete.');

@ -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-v34-maintenance-notices'")
"private const SCHEMA_VERSION = 'go-agent-wss-v35-service-compensations'")
&& str_contains($setup, "'name' => 'cloud_vpc_enabled'")
&& str_contains($setup, "'value' => 'false'")
&& str_contains($setup,

@ -124,6 +124,17 @@ $schema->create('cloud_invoice_payments', static function (Blueprint $table) {
$schema->create('cloud_mail_outbox', static function (Blueprint $table) {
$table->increments('id'); $table->integer('invoice_id');
});
$schema->create('cloud_service_compensations', static function (Blueprint $table) {
$table->increments('id'); $table->integer('requested_by_user_id');
});
$schema->create('cloud_service_compensation_items', static function (Blueprint $table) {
$table->increments('id'); $table->integer('batch_id');
$table->integer('user_id'); $table->string('state');
});
$schema->create('cloud_service_compensation_recipients', static function (Blueprint $table) {
$table->increments('id'); $table->integer('batch_id');
$table->integer('user_id'); $table->string('status');
});
$schema->create('email_verify', static function (Blueprint $table) {
$table->increments('id');
$table->string('email');
@ -237,6 +248,17 @@ Capsule::table('cloud_invoice_payments')->insert([
Capsule::table('cloud_mail_outbox')->insert([
['invoice_id' => 103], ['invoice_id' => 303],
]);
Capsule::table('cloud_service_compensations')->insert([
'id' => 1, 'requested_by_user_id' => 2,
]);
Capsule::table('cloud_service_compensation_items')->insert([
['id' => 1, 'batch_id' => 1, 'user_id' => 1, 'state' => 'applied'],
['id' => 2, 'batch_id' => 1, 'user_id' => 3, 'state' => 'applied'],
]);
Capsule::table('cloud_service_compensation_recipients')->insert([
['id' => 1, 'batch_id' => 1, 'user_id' => 1, 'status' => 'sent'],
['id' => 2, 'batch_id' => 1, 'user_id' => 3, 'status' => 'sent'],
]);
Capsule::table('email_verify')->insert([
['email' => 'deleted@example.com'], ['email' => 'other@example.com'],
]);
@ -283,6 +305,13 @@ $assert(Capsule::table('cloud_jobs')->orderBy('id')->pluck('id')->all()
'Tenant deletion retained delegated jobs or removed unrelated administrator jobs.');
$assert(Capsule::table('cloud_mail_outbox')->pluck('invoice_id')->all() === [303],
'Cloud invoice mail history deletion was not isolated.');
$assert(Capsule::table('cloud_service_compensations')->pluck('id')->all() === [1],
'Account deletion removed the immutable compensation batch audit.');
$assert(Capsule::table('cloud_service_compensation_items')->pluck('user_id')->all()
=== [3]
&& Capsule::table('cloud_service_compensation_recipients')
->pluck('user_id')->all() === [3],
'Compensation tenant details were not deleted in isolation.');
$assert(Capsule::table('email_verify')->pluck('email')->all() === ['other@example.com'], 'Email verification cleanup was not isolated.');
$assert(Capsule::table('configurations')->where('name', 'email_blacklist')->value('value') === "deleted@example.com\n@blocked.example", 'Manual blacklist was changed.');
$assert($result['cache_error'] === false && $result['log_rows_deleted'] === 0, 'Disabled optional services were accessed.');
@ -361,6 +390,51 @@ $assert(User::find(6) !== null
&& $fencedJob->sealed_payload === 'unknown-effect-create-audit',
'Unknown-effect terminal provisioning fence lost its order or job audit.');
Capsule::table('user')->insert([
'id' => 8, 'email' => 'compensation-applying@example.com',
'is_admin' => 0, 'ref_by' => 0,
]);
Capsule::table('cloud_service_compensations')->insert([
'id' => 2, 'requested_by_user_id' => 2,
]);
Capsule::table('cloud_service_compensation_items')->insert([
'id' => 8, 'batch_id' => 2, 'user_id' => 8, 'state' => 'applying',
]);
try {
(new UserDataDeletionService())->deleteUsers([8], false);
throw new RuntimeException('Account with an applying compensation was deleted.');
} catch (RuntimeException $error) {
$assert(str_contains($error->getMessage(), '应用中的实例补偿'),
'Applying-compensation deletion returned the wrong error.');
}
$assert(User::find(8) !== null
&& Capsule::table('cloud_service_compensation_items')->where('id', 8)->exists()
&& Capsule::table('cloud_service_compensations')->where('id', 2)->exists(),
'Applying-compensation deletion fence did not preserve its audit rows.');
Capsule::table('user')->insert([
'id' => 9, 'email' => 'compensation-sending@example.com',
'is_admin' => 0, 'ref_by' => 0,
]);
Capsule::table('cloud_service_compensations')->insert([
'id' => 3, 'requested_by_user_id' => 2,
]);
Capsule::table('cloud_service_compensation_recipients')->insert([
'id' => 9, 'batch_id' => 3, 'user_id' => 9, 'status' => 'sending',
]);
try {
(new UserDataDeletionService())->deleteUsers([9], false);
throw new RuntimeException('Account with a sending compensation notice was deleted.');
} catch (RuntimeException $error) {
$assert(str_contains($error->getMessage(), '投递中的补偿通知'),
'Sending-compensation deletion returned the wrong error.');
}
$assert(User::find(9) !== null
&& Capsule::table('cloud_service_compensation_recipients')
->where('id', 9)->exists()
&& Capsule::table('cloud_service_compensations')->where('id', 3)->exists(),
'Sending-compensation deletion fence did not preserve its audit rows.');
Capsule::table('user')->insert([
'id' => 7, 'email' => 'rollback-attachment@example.com',
'is_admin' => 0, 'ref_by' => 0,

Loading…
Cancel
Save