httpStatus === $status, $message . ': HTTP ' . $error->httpStatus); return; } throw new RuntimeException($message . ': request was accepted'); }; $ids = static fn (array $items): array => array_map( 'intval', array_column($items, 'id') ); $capsule = new Capsule(); $capsule->addConnection([ 'driver' => 'sqlite', 'database' => ':memory:', ], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema('default'); $schema->create('user', static function (Blueprint $table): void { $table->increments('id'); $table->string('user_name'); $table->string('email'); }); $schema->create('cloud_hosts', static function (Blueprint $table): void { $table->increments('id'); $table->string('name'); $table->string('region'); }); $schema->create('cloud_instances', static function (Blueprint $table): void { $table->increments('id'); $table->string('uuid'); $table->string('name'); $table->integer('host_id'); $table->integer('user_id'); $table->string('status'); }); $schema->create('cloud_orders', static function (Blueprint $table): void { $table->increments('id'); $table->integer('host_id'); $table->integer('user_id'); $table->integer('instance_id')->nullable(); }); $schema->create('cloud_jobs', static function (Blueprint $table): void { $table->increments('id'); $table->string('operation_id'); $table->string('request_idempotency_key'); $table->string('request_fingerprint'); $table->integer('parent_job_id')->nullable(); $table->integer('order_id')->nullable(); $table->integer('invoice_id')->nullable(); $table->integer('instance_id')->nullable(); $table->integer('snapshot_id')->nullable(); $table->integer('user_id'); $table->integer('host_id')->nullable(); $table->string('kind'); $table->string('execution_mode')->default('agent'); $table->string('phase'); $table->string('outcome')->nullable(); $table->string('status'); $table->integer('attempts')->default(0); $table->integer('dispatch_attempts')->nullable()->default(0); $table->integer('dispatch_deadline_at'); $table->integer('dispatch_started_at')->nullable(); $table->integer('agent_accepted_at')->nullable(); $table->integer('receipt_deadline_at')->nullable(); $table->integer('dispatched_at')->nullable(); $table->integer('terminal_at')->nullable(); $table->string('completion_event_id')->nullable(); $table->boolean('effect_may_have_applied')->nullable(); $table->boolean('safe_to_resubmit')->nullable(); $table->string('effect_resolution')->default('not_applicable'); $table->integer('effect_resolved_at')->nullable(); $table->integer('effect_resolved_by')->nullable(); $table->string('effect_resolution_reason')->default(''); $table->string('lease_token')->nullable(); $table->integer('lease_until')->default(0); $table->integer('available_at')->default(0); $table->string('error_code')->nullable(); $table->boolean('finalization_incident')->default(0); $table->string('finalization_error_code')->nullable(); $table->text('result_json')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); }); Capsule::table('user')->insert([ ['id' => 1, 'user_name' => 'Instance Owner', 'email' => 'owner@example.test'], ['id' => 99, 'user_name' => 'Root Operator', 'email' => 'root@example.test'], ]); Capsule::table('cloud_hosts')->insert([ 'id' => 7, 'name' => 'Host Seven', 'region' => 'HongKong', ]); Capsule::table('cloud_instances')->insert([ 'id' => 10, 'uuid' => '11111111-1111-4111-8111-111111111110', 'name' => "tenant's 100%_vm", 'host_id' => 7, 'user_id' => 1, 'status' => 'active', ]); Capsule::table('cloud_orders')->insert([ 'id' => 100, 'host_id' => 7, 'user_id' => 1, 'instance_id' => 10, ]); $now = time(); $insert = static function (int $id, array $values = []) use ($now): void { Capsule::table('cloud_jobs')->insert(array_replace([ 'id' => $id, 'operation_id' => sprintf('%032x', $id), 'request_idempotency_key' => sprintf('%032x', $id), 'request_fingerprint' => hash('sha256', (string) $id), 'order_id' => 100, 'instance_id' => 10, 'snapshot_id' => null, 'user_id' => 99, 'host_id' => 7, 'kind' => 'action', 'phase' => 'terminal', 'outcome' => 'succeeded', 'status' => 'succeeded', 'attempts' => 1, 'dispatch_attempts' => 1, 'dispatch_deadline_at' => $now - 100, 'dispatch_started_at' => $now - 95, 'agent_accepted_at' => $now - 92, 'receipt_deadline_at' => $now - 50, 'dispatched_at' => $now - 90, 'terminal_at' => $now - 40, 'completion_event_id' => null, 'effect_may_have_applied' => false, 'safe_to_resubmit' => false, 'effect_resolution' => 'not_applicable', 'lease_until' => 0, 'available_at' => 0, 'result_json' => json_encode(['cause_code' => ''], JSON_THROW_ON_ERROR), 'created_at' => $now - 120, 'updated_at' => $now - 40, ], $values)); }; $insert(1, [ 'phase' => 'accepted', 'outcome' => null, 'status' => 'queued', 'attempts' => 0, 'dispatch_attempts' => 0, 'dispatch_deadline_at' => $now + 100, 'dispatched_at' => null, 'receipt_deadline_at' => null, 'terminal_at' => null, 'effect_may_have_applied' => null, 'safe_to_resubmit' => null, ]); $insert(2); $insert(3, [ 'outcome' => 'failed', 'status' => 'failed', 'effect_may_have_applied' => true, 'safe_to_resubmit' => false, 'effect_resolution' => 'unknown', 'error_code' => 'transport_lost', 'finalization_incident' => true, 'finalization_error_code' => 'recovery_evidence_conflict', 'available_at' => $now + 45, 'result_json' => json_encode(['cause_code' => 'transport_lost'], JSON_THROW_ON_ERROR), ]); $insert(4, [ 'outcome' => 'failed', 'status' => 'failed', 'error_code' => 'operation_failed', 'finalization_incident' => true, 'finalization_error_code' => 'PDOException: password=must-not-expose-server-error', ]); $insert(5, [ 'outcome' => 'failed', 'status' => 'failed', 'effect_may_have_applied' => false, 'safe_to_resubmit' => false, 'error_code' => 'validation_failed', ]); $insert(6, [ 'order_id' => null, 'instance_id' => null, 'kind' => 'host_storage_configure', 'outcome' => 'failed', 'status' => 'failed', 'effect_may_have_applied' => true, 'safe_to_resubmit' => false, 'effect_resolution' => 'unknown', 'error_code' => 'operation_failed', ]); $admin = ['user_id' => 99, 'is_admin' => true, 'is_super_admin' => true, 'can_manage' => true]; $readonly = ['user_id' => 99, 'is_admin' => true, 'is_super_admin' => false, 'can_manage' => false]; $customer = ['user_id' => 1, 'is_admin' => false, 'is_super_admin' => false, 'can_manage' => false]; $default = Operations::page($admin); $all = Operations::page($admin, ['status' => 'all']); $assert($default['filters']['status'] === 'all' && $ids($default['items']) === $ids($all['items']) && $default['summary'] === $all['summary'], 'The default task inventory did not preserve the all-status view.'); $assert($ids($all['items']) === [6, 5, 4, 3, 2, 1] && $all['summary'] === [ 'total' => 6, 'active' => 1, 'succeeded' => 1, 'failed' => 4, 'unresolved_effect' => 1, ], 'Operations inventory did not expose active and terminal states exactly.'); foreach ([ 'active' => [1], 'succeeded' => [2], 'failed' => [6, 5, 4, 3], 'attention' => [6, 5, 4, 3], ] as $status => $expected) { $page = Operations::page($admin, ['status' => $status]); $assert($ids($page['items']) === $expected, 'Status filter diverged from task receipt state: ' . $status); } $unsafeIncident = array_values(array_filter( $all['items'], static fn (array $item): bool => $item['id'] === '4' ))[0]; $assert($unsafeIncident['finalization_error_code'] === 'finalization_failed' && !str_contains(json_encode($unsafeIncident, JSON_UNESCAPED_UNICODE), 'must-not-expose-server-error'), 'Administrator DTO leaked an unreviewed server finalization error.'); foreach (["tenant's", '100%_vm', '11111111-1111-4111-8111-111111111110'] as $search) { $assert(Operations::page($admin, ['status' => 'all', 'q' => $search])['summary']['total'] === 5, 'Bound task search failed for ' . $search); } $assert(Operations::page($admin, ['status' => 'all', 'q' => "' OR 1=1 --"])['items'] === [], 'Task search accepted SQL injection text as a predicate.'); $detail = Operations::detail($admin, '3')['job']; $assert($detail['completed'] === true && $detail['status'] === 'failed' && $detail['dispatch_attempts'] === 1 && $detail['effect_may_have_applied'] === true && $detail['safe_to_resubmit'] === false && $detail['effect_resolution'] === 'unknown' && $detail['can_inspect'] === true && $detail['can_resolve_effect'] === false && ($detail['recovery']['pending'] ?? null) === true && ($detail['recovery']['code'] ?? null) === 'recovery_evidence_conflict' && ($detail['recovery']['next_check_at'] ?? null) === $now + 45 && str_contains((string) ($detail['recovery']['message'] ?? ''), '仍会继续自动核对') && str_contains($detail['resolve_effect_reason'], '尚无可原子收敛业务账本'), 'Unknown-effect terminal failure lost its immutable outcome or safety fence.'); $detailPayload = Operations::detail($admin, '3'); $assert(str_contains(implode(' ', $detailPayload['notes']), '绝不会重放原操作') && str_contains(implode(' ', $detailPayload['notes']), '仍会继续自动核对') && !str_contains(implode(' ', $detailPayload['notes']), '失败后不会自动核对'), 'Administrator detail still described recoverable work as permanently manual.'); $active = Operations::detail($admin, '1')['job']; $assert($active['completed'] === false && $active['status'] === 'active', 'Accepted task was exposed as a fabricated terminal result.'); $assert(Operations::detail($readonly, '3')['job']['can_resolve_effect'] === false, 'Read-only administrator received effect-resolution authority.'); $storageDetail = Operations::detail($admin, '6'); $assert($storageDetail['job']['can_resolve_effect'] === false && str_contains($storageDetail['job']['resolve_effect_reason'], '不保留跨任务资源栅栏') && str_contains($storageDetail['job']['display']['effect'], '不阻断新的独立任务') && str_contains(implode(' ', $storageDetail['notes']), '旧 operation ID 仍永久终止且绝不重放') && str_contains(implode(' ', $storageDetail['notes']), '不会更换已有池路径'), 'Storage failure was still presented as a durable resource blocker.'); $insert(7, [ 'outcome' => 'succeeded', 'status' => 'succeeded', 'dispatch_attempts' => 1, 'effect_may_have_applied' => false, 'safe_to_resubmit' => false, 'effect_resolution' => 'applied', 'finalization_incident' => true, 'finalization_error_code' => 'recovery_dispatch_ledger_conflict', ]); $insert(8, [ 'outcome' => 'failed', 'status' => 'failed', 'attempts' => 0, 'dispatch_attempts' => 0, 'dispatch_started_at' => null, 'agent_accepted_at' => null, 'dispatched_at' => null, 'receipt_deadline_at' => null, 'effect_may_have_applied' => false, 'safe_to_resubmit' => false, 'effect_resolution' => 'not_applied', 'finalization_incident' => true, 'finalization_error_code' => 'recovery_ledger_conflict', ]); $insert(9, [ 'outcome' => 'succeeded', 'status' => 'succeeded', 'dispatch_attempts' => null, 'effect_may_have_applied' => false, 'safe_to_resubmit' => false, 'effect_resolution' => 'applied', 'finalization_incident' => false, 'finalization_error_code' => null, ]); $insert(10, [ 'outcome' => 'succeeded', 'status' => 'succeeded', 'dispatch_attempts' => 1, 'effect_may_have_applied' => false, 'safe_to_resubmit' => false, 'effect_resolution' => 'not_applied', 'finalization_incident' => false, 'finalization_error_code' => null, ]); $insert(11, [ 'outcome' => 'succeeded', 'status' => 'succeeded', 'dispatch_attempts' => 1, 'receipt_deadline_at' => null, 'effect_may_have_applied' => false, 'safe_to_resubmit' => false, 'effect_resolution' => 'applied', 'finalization_incident' => false, 'finalization_error_code' => null, ]); $insert(12, [ 'order_id' => null, 'instance_id' => null, 'host_id' => null, 'kind' => 'template_import', 'execution_mode' => 'aggregate', 'outcome' => 'succeeded', 'status' => 'succeeded', 'attempts' => 0, 'dispatch_attempts' => 0, 'dispatch_started_at' => null, 'agent_accepted_at' => null, 'dispatched_at' => null, 'receipt_deadline_at' => null, 'effect_may_have_applied' => false, 'safe_to_resubmit' => false, 'effect_resolution' => 'applied', 'finalization_incident' => false, 'finalization_error_code' => null, ]); $recoveryInventory = Operations::page($admin, ['status' => 'all']); $assert($recoveryInventory['summary'] === [ 'total' => 12, 'active' => 1, 'succeeded' => 6, 'failed' => 5, 'unresolved_effect' => 7, ] && $ids(Operations::page($admin, ['status' => 'attention'])['items']) === [12, 11, 10, 9, 8, 7, 6, 5, 4, 3] && $ids(Operations::page($admin, ['status' => 'succeeded'])['items']) === [12, 11, 10, 9, 7, 2], 'Administrator summary/filtering omitted succeeded recovery incidents or local finalizers.'); $stickyRepair = Operations::detail($admin, '7')['job']; $assert($stickyRepair['status'] === 'succeeded' && $stickyRepair['display']['status'] === '恢复处理中' && str_contains($stickyRepair['display']['effect'], '终态保持不变') && ($stickyRepair['recovery']['code'] ?? null) === 'recovery_dispatch_ledger_conflict' && $stickyRepair['can_inspect'] === false && $stickyRepair['can_resolve_effect'] === false && str_contains($stickyRepair['inspect_reason'], '恢复账本'), 'A repaired succeeded task appeared successful or exposed controls before sticky validation.'); $localFinalizer = Operations::detail($admin, '8')['job']; $assert($localFinalizer['dispatch_state'] === 'not_dispatched' && $localFinalizer['display']['status'] === '恢复处理中' && ($localFinalizer['recovery']['code'] ?? null) === 'recovery_ledger_conflict' && str_contains($localFinalizer['display']['effect'], 'PHP') && str_contains($localFinalizer['display']['effect'], '不会联系 Agent') && $localFinalizer['can_inspect'] === false && $localFinalizer['can_resolve_effect'] === false, 'A zero-dispatch PHP finalizer recovery was described as an Agent operation or unlocked.'); $nullLedger = Operations::detail($admin, '9')['job']; $assert($nullLedger['status'] === 'succeeded' && $nullLedger['dispatch_attempts'] === null && $nullLedger['dispatch_state'] === 'invalid' && $nullLedger['display']['status'] === '恢复处理中' && ($nullLedger['recovery']['code'] ?? null) === 'recovery_dispatch_ledger_conflict' && $nullLedger['can_inspect'] === false, 'A NULL dispatch ledger was normalized to zero or shown as ordinary success.'); $resolvedConflict = Operations::detail($admin, '10')['job']; $assert($resolvedConflict['status'] === 'succeeded' && $resolvedConflict['display']['status'] === '恢复处理中' && ($resolvedConflict['recovery']['code'] ?? null) === 'recovery_instance_state_conflict' && $resolvedConflict['can_inspect'] === false, 'A succeeded/not_applied terminal contradiction escaped administrator attention.'); $envelopeConflict = Operations::detail($admin, '11')['job']; $assert($envelopeConflict['status'] === 'succeeded' && $envelopeConflict['display']['status'] === '恢复处理中' && ($envelopeConflict['recovery']['code'] ?? null) === 'recovery_instance_state_conflict' && $envelopeConflict['can_inspect'] === false, 'A resolved Agent task with an invalid receipt deadline escaped administrator attention.'); $aggregateConflict = Operations::detail($admin, '12')['job']; $assert($aggregateConflict['status'] === 'succeeded' && $aggregateConflict['display']['status'] === '恢复处理中' && ($aggregateConflict['recovery']['code'] ?? null) === 'recovery_aggregate_state_conflict' && $aggregateConflict['can_inspect'] === false, 'A published aggregate with a missing receipt deadline escaped administrator attention.'); $insert(13, [ 'kind' => 'delete', 'outcome' => 'failed', 'status' => 'failed', 'effect_may_have_applied' => false, 'safe_to_resubmit' => false, 'effect_resolution' => 'not_applied', 'error_code' => 'force_required', ]); $insert(14, [ 'kind' => 'delete', 'outcome' => 'failed', 'status' => 'failed', 'effect_may_have_applied' => false, 'safe_to_resubmit' => false, 'effect_resolution' => 'not_applied', 'error_code' => 'force_required', 'finalization_incident' => true, 'finalization_error_code' => 'recovery_ledger_conflict', ]); $forceReady = Operations::detail($admin, '13')['job']; $forceBlocked = Operations::detail($admin, '14')['job']; $assert($forceReady['can_force_delete'] === true && $forceBlocked['can_force_delete'] === false && str_contains( (string) $forceBlocked['force_delete_reason'], '恢复账本异常' ), 'A sticky recovery incident exposed the destructive force-delete control.'); $insert(15, [ 'kind' => 'instance_settings', 'execution_mode' => 'local', 'phase' => 'accepted', 'outcome' => null, 'status' => 'queued', 'dispatch_deadline_at' => $now + 100, 'dispatch_attempts' => 0, 'attempts' => 0, 'dispatch_started_at' => null, 'agent_accepted_at' => null, 'dispatched_at' => null, 'receipt_deadline_at' => null, 'terminal_at' => null, 'effect_may_have_applied' => null, 'safe_to_resubmit' => null, ]); $wrongModeActive = Operations::detail($admin, '15')['job']; $assert($wrongModeActive['completed'] === false && $wrongModeActive['can_inspect'] === false && str_contains( (string) $wrongModeActive['inspect_reason'], 'PHP 本地恢复' ), 'A nonterminal supported kind with a damaged mode exposed Agent inspection.'); $insert(16, [ 'order_id' => null, 'instance_id' => null, 'host_id' => null, 'kind' => 'user_iso_delete', 'execution_mode' => 'aggregate', 'outcome' => 'failed', 'status' => 'failed', 'attempts' => 0, 'dispatch_attempts' => 0, 'dispatch_started_at' => null, 'agent_accepted_at' => null, 'dispatched_at' => null, 'effect_may_have_applied' => true, 'safe_to_resubmit' => false, 'effect_resolution' => 'applied', 'effect_resolved_at' => $now - 30, 'effect_resolution_reason' => 'legacy_child_effects_resolved:160:mixed', 'finalization_incident' => false, 'finalization_error_code' => null, ]); $partialAggregate = Operations::detail($admin, '16')['job']; $assert(($partialAggregate['effect_scope'] ?? null) === 'partial' && $partialAggregate['display']['status'] === '失败' && str_contains( (string) $partialAggregate['display']['effect'], '部分宿主机已应用、部分未应用' ) && !str_contains( (string) $partialAggregate['display']['effect'], '确认操作已应用' ), 'Administrator display mislabeled a mixed aggregate as all-applied.'); $reason = '已在宿主机只读核对虚拟机与磁盘,确认原操作效果已经发生。'; $operationId = sprintf('%032x', 3); $freshInspection = [ 'operation_id' => $operationId, 'checked_at' => $now, 'status' => 'failed', ]; // A generic host action has no business-ledger finalizer. Even a valid // historical Agent fence receipt must not clear its safety fence. $persistedFenceReceipt = [ 'operation_id' => $operationId, 'resolution_id' => str_repeat('a', 32), 'resolution' => 'applied', 'inspection_checked_at' => $now - 60, 'reason_sha256' => hash('sha256', $reason), 'resolved_at' => $now - 50, 'resolved_by' => 7, 'replayed' => false, ]; $reject(static fn () => Operations::resolveEffect($admin, '3', [ 'resolution' => 'applied', 'reason' => $reason, ], $freshInspection, $persistedFenceReceipt), 409, 'Unsupported action effect resolution cleared a safety fence'); $row = CloudJob::find(3); $assert($row !== null && (string) $row->phase === 'terminal' && (string) $row->outcome === 'failed' && (string) $row->effect_resolution === 'unknown' && $row->effect_resolved_by === null, 'Rejected generic effect resolution changed the immutable task receipt.'); $reject(static fn () => Operations::resolveEffect($readonly, '5', [ 'resolution' => 'not_applied', 'reason' => $reason, ], $freshInspection, $persistedFenceReceipt), 403, 'Read-only administrator resolved a fence'); foreach ([ ['status' => 'invented'], ['page' => '0'], ['per_page' => '30'], ['q' => str_repeat('x', 81)], ['unknown' => 'value'], ] as $invalid) { $reject(static fn () => Operations::page($admin, $invalid), 400, 'Malformed task filter was accepted'); } $reject(static fn () => Operations::page($customer), 403, 'Customer accessed administrator operations'); $reject(static fn () => Operations::detail($admin, '999'), 404, 'Missing task detail was fabricated'); echo "Administrator task inventory, terminal receipt, and audited fence-resolution tests passed.\n"; }