|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Workflow\V2\Support; |
| 6 | + |
| 7 | +use Workflow\V2\Enums\HistoryEventType; |
| 8 | +use Workflow\V2\Exceptions\ActivityRetryPolicyMismatchException; |
| 9 | +use Workflow\V2\Models\WorkflowRun; |
| 10 | + |
| 11 | +final class ActivityReplayIdentity |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @param callable(int): int|null $historySequence |
| 15 | + */ |
| 16 | + public static function assertCall( |
| 17 | + WorkflowRun $run, |
| 18 | + mixed $call, |
| 19 | + int $sequence, |
| 20 | + ?callable $historySequence = null, |
| 21 | + ): void { |
| 22 | + if ($call instanceof DurableOperationHandle) { |
| 23 | + self::assertCall($run, $call->call, $call->baseSequence); |
| 24 | + |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + if ($call instanceof AllCall) { |
| 29 | + foreach ($call->leafDescriptors($sequence) as $descriptor) { |
| 30 | + self::assertCall($run, $descriptor['call'], $sequence + $descriptor['offset'], $historySequence); |
| 31 | + } |
| 32 | + |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + if (! $call instanceof ActivityCall) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + $sequence = $historySequence === null ? $sequence : $historySequence($sequence); |
| 41 | + if (! $run->relationLoaded('historyEvents')) { |
| 42 | + $run->load('historyEvents'); |
| 43 | + } |
| 44 | + foreach ($run->historyEvents as $event) { |
| 45 | + $recordedSequence = $event->payload['sequence'] ?? null; |
| 46 | + if (is_string($recordedSequence) && is_numeric($recordedSequence)) { |
| 47 | + $recordedSequence = (int) $recordedSequence; |
| 48 | + } |
| 49 | + if ($event->event_type !== HistoryEventType::ActivityScheduled |
| 50 | + || $recordedSequence !== $sequence |
| 51 | + || ($event->payload['execution_mode'] ?? null) === LocalActivityRuntime::EXECUTION_MODE |
| 52 | + || ($event->payload['activity']['execution_mode'] ?? null) === LocalActivityRuntime::EXECUTION_MODE) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + $snapshot = $event->payload['activity']['retry_policy'] ?? $event->payload['retry_policy'] ?? null; |
| 57 | + if ($snapshot === null) { |
| 58 | + // Legacy absence is not an invitation to invent today's class defaults. |
| 59 | + return; |
| 60 | + } |
| 61 | + if (! is_array($snapshot)) { |
| 62 | + throw new ActivityRetryPolicyMismatchException($sequence, 'retry_policy'); |
| 63 | + } |
| 64 | + |
| 65 | + self::assertPolicy($snapshot, $call->options, $sequence); |
| 66 | + |
| 67 | + return; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Compare explicit authored overrides only; never instantiate an activity |
| 73 | + * or recompute its class-derived budget during replay. |
| 74 | + * |
| 75 | + * @param array<string, mixed> $snapshot |
| 76 | + */ |
| 77 | + public static function assertPolicy(array $snapshot, ?ActivityOptions $options, int $sequence): void |
| 78 | + { |
| 79 | + $authored = [ |
| 80 | + 'max_attempts' => $options?->maxAttempts, |
| 81 | + 'backoff_seconds' => $options?->backoff, |
| 82 | + 'start_to_close_timeout' => $options?->startToCloseTimeout, |
| 83 | + 'schedule_to_start_timeout' => $options?->scheduleToStartTimeout, |
| 84 | + 'schedule_to_close_timeout' => $options?->scheduleToCloseTimeout, |
| 85 | + 'heartbeat_timeout' => $options?->heartbeatTimeout, |
| 86 | + 'non_retryable_error_types' => $options?->nonRetryableErrorTypes ?: null, |
| 87 | + ]; |
| 88 | + |
| 89 | + $normalized = ActivityRetryPolicy::snapshotExternal(null, $options) ?? [ |
| 90 | + 'non_retryable_error_types' => [], |
| 91 | + ]; |
| 92 | + foreach ($snapshot as $field => $recorded) { |
| 93 | + $valid = match ($field) { |
| 94 | + 'snapshot_version' => $recorded === ActivityRetryPolicy::SNAPSHOT_VERSION, |
| 95 | + 'max_attempts' => $recorded === null || (is_int($recorded) && $recorded > 0), |
| 96 | + 'backoff_seconds' => is_array($recorded) && array_is_list($recorded) |
| 97 | + && array_filter($recorded, static fn (mixed $value): bool => ! is_int($value) || $value < 0) === [], |
| 98 | + 'non_retryable_error_types' => is_array($recorded) && array_is_list($recorded) |
| 99 | + && array_filter( |
| 100 | + $recorded, |
| 101 | + static fn (mixed $value): bool => ! is_string($value) || trim($value) === '' |
| 102 | + ) === [], |
| 103 | + 'start_to_close_timeout', 'schedule_to_start_timeout', 'schedule_to_close_timeout', 'heartbeat_timeout' |
| 104 | + => $recorded === null || (is_int($recorded) && $recorded >= 0), |
| 105 | + default => true, |
| 106 | + }; |
| 107 | + if (! $valid) { |
| 108 | + throw new ActivityRetryPolicyMismatchException($sequence, $field); |
| 109 | + } |
| 110 | + |
| 111 | + $current = $authored[$field] ?? null; |
| 112 | + if (in_array($field, ['max_attempts', 'backoff_seconds'], true) && $current === null) { |
| 113 | + // These two fields can originate from activity-class defaults. |
| 114 | + continue; |
| 115 | + } |
| 116 | + if (! array_key_exists($field, $authored)) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + // Apply exactly the same normalization as initial scheduling. |
| 120 | + if ($recorded !== ($normalized[$field] ?? null)) { |
| 121 | + throw new ActivityRetryPolicyMismatchException($sequence, $field); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments