From 9367aeafaafe29267ce8f2ce4848834ec1b4c7a3 Mon Sep 17 00:00:00 2001 From: Richard McDaniel <1130888+rmcdaniel@users.noreply.github.com> Date: Thu, 19 Nov 2026 06:07:28 +0000 Subject: [PATCH 1/3] Show workflow code only applied signals (#434) WorkflowContext::signals() returned every SignalReceived in the history, including signals the Server had received but not yet applied. Workflow code could act on a signal the Server later rejected, for example closing the run before a queued update was applied. A signal is now visible once it is applied (a SignalApplied event, or a SignalReceived carrying the workflow_sequence of the wait that consumed it), or when the current workflow task delivers it (resume_source_kind workflow_signal). Identity-less SignalReceived records stay visible. Message streams keep their own cursor over every message. Replay compatibility: when hiding an unapplied signal contradicts the recorded history, the Replayer replays again showing each signal received before a later recorded workflow decision; signals received after the last recorded decision stay hidden until applied. Replay fixtures cover the new path and a legacy history. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 16 ++ docs/sdk-reference.md | 11 + scripts/ci/run-replay-regression-fixture.php | 29 ++- src/Worker/AppliedSignals.php | 183 ++++++++++++++++ src/Worker/Replayer.php | 57 ++++- src/Worker/WorkflowContext.php | 28 ++- tests/AppliedSignalsTest.php | 196 ++++++++++++++++++ tests/Support/ReplayRegressionFixture.php | 19 ++ .../signals-observe-only-applied.json | 70 +++++++ .../signals-unapplied-legacy-history.json | 60 ++++++ 10 files changed, 664 insertions(+), 5 deletions(-) create mode 100644 src/Worker/AppliedSignals.php create mode 100644 tests/AppliedSignalsTest.php create mode 100644 tests/fixtures/replay-regressions/signals-observe-only-applied.json create mode 100644 tests/fixtures/replay-regressions/signals-unapplied-legacy-history.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ebc43a1..31dc1f84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,22 @@ project follows [Semantic Versioning](https://semver.org/). (durable-workflow/.github#216). Nothing was written and no receipt was selected, so the refusal is never resent automatically. +### Fixed + +- `WorkflowContext::signals()` returns only signals the Server has applied to + the run (a `SignalApplied` event, or a `SignalReceived` carrying the + `workflow_sequence` of the wait that consumed it), plus the signal the current + workflow task delivers. It previously returned every received signal, so + workflow code could act on a signal the Server had not applied yet and later + rejected, for example closing the run before a queued update was applied. + Signals now reach workflow code in message order with updates, as in embedded + Workflow, Python and Rust. `SignalReceived` records that name neither a + signal ID nor a command ID are still returned. Replay compatibility: a history + recorded by an earlier release still replays unchanged. When hiding an + unapplied signal contradicts that history, the replay shows again each signal + received before a later recorded workflow decision; signals received after the + last recorded decision stay hidden until applied (#434). + ## [3.20.0] - 2026-11-18 ### Added diff --git a/docs/sdk-reference.md b/docs/sdk-reference.md index 83ff1b1f..d1574795 100644 --- a/docs/sdk-reference.md +++ b/docs/sdk-reference.md @@ -713,6 +713,17 @@ invoked. The workflow reads their committed values with `$context->signals('set-language')` during replay. Query and update methods are executed with immutable `QueryContext` state. +`signals()` returns only signals the Server has applied to the run, plus the +signal the current workflow task delivers, in the order the Server received +them. The Server applies signals in message order with updates, so an update +sent before a signal is applied first. A signal that has been received but not +yet applied is not returned, and one the Server rejects (for example because +the run closed first) never is. Histories recorded by earlier SDK releases, +which returned every received signal, still replay: if hiding an unapplied +signal would contradict the recorded history, the signals received before that +history's last recorded workflow decision are returned again. Signals received +after it stay hidden until applied. + The PHP SDK does not expose update-validator authoring. Worker registration therefore declares an empty `update_validators` list for every workflow type; capability discovery and Server admission must not infer validator parity from diff --git a/scripts/ci/run-replay-regression-fixture.php b/scripts/ci/run-replay-regression-fixture.php index 55f0986d..1d17c88b 100644 --- a/scripts/ci/run-replay-regression-fixture.php +++ b/scripts/ci/run-replay-regression-fixture.php @@ -679,7 +679,7 @@ private static function executeLayout( 'arguments' => $codec->envelope($input), 'history_events' => $layout === 'inline' ? $history : [], 'next_history_page_token' => $layout === 'paginated' ? 'regression-page-1' : '', - ], self::taskAttributes($workflowType)); + ], self::taskAttributes($workflowType), self::workerTaskAttributes($identity, $fixture)); $pagedHistory[$taskId] = $layout === 'paginated' ? $history : []; } $transport = new ReplayRegressionTransport( @@ -934,6 +934,15 @@ private static function workflow(string $workflowType): callable return $result; }, + // sdk-php#434: only applied signals, and the one this task delivers. + 'golden.signal-applied' => static fn (WorkflowContext $context): array => ['signals' => $context->signals('approve')], + // sdk-php#434: a decision taken from the signals visible to this task. + 'golden.signal-decision' => static function (WorkflowContext $context): array { + $approved = $context->signals('approve') !== []; + $result = $context->activity($approved ? 'golden.approved' : 'golden.waiting'); + + return ['result' => $result, 'signals' => $context->signals('approve')]; + }, 'golden.immediate-condition' => static function (WorkflowContext $context): array { $first = $context->activity('golden.first'); $satisfied = $context->waitCondition(static fn (): bool => true, key: 'already-ready'); @@ -1393,6 +1402,24 @@ private static function selectionHandleIsolationWorkflow(): callable }; } + /** + * The resume context a signal-delivering workflow task carries (sdk-php#434). + * + * @param array $fixture + * @return array + */ + private static function workerTaskAttributes(string $identity, array $fixture): array + { + $workerTask = $fixture['worker_task'] ?? []; + if (!is_array($workerTask)) { + throw new RuntimeException("{$identity}.worker_task must be an object."); + } + + return array_intersect_key($workerTask, array_flip([ + 'resume_source_kind', 'resume_source_id', 'workflow_signal_id', 'workflow_command_id', 'signal_name', + ])); + } + /** @return array */ private static function taskAttributes(string $workflowType): array { diff --git a/src/Worker/AppliedSignals.php b/src/Worker/AppliedSignals.php new file mode 100644 index 00000000..5c243823 --- /dev/null +++ b/src/Worker/AppliedSignals.php @@ -0,0 +1,183 @@ + $signalIds + * @param array $commandIds + * @param array $observedOrders history positions of signals shown for compatibility + */ + private function __construct( + private readonly array $signalIds, + private readonly array $commandIds, + private readonly array $observedOrders = [], + ) { + } + + /** + * @param list> $history + * @param array $task + */ + public static function fromHistory(array $history, array $task): self + { + $signalIds = []; + $commandIds = []; + foreach ($history as $event) { + $type = $event['event_type'] ?? $event['type'] ?? null; + $payload = is_array($event['payload'] ?? null) ? $event['payload'] : []; + $applied = $type === 'SignalApplied' + || ($type === 'SignalReceived' && self::recordedSequence($payload['workflow_sequence'] ?? null)); + if (!$applied) { + continue; + } + if (($signalId = self::identity($payload['signal_id'] ?? null)) !== null) { + $signalIds[$signalId] = true; + } + if (($commandId = self::identity($payload['workflow_command_id'] ?? $event['workflow_command_id'] ?? null)) !== null) { + $commandIds[$commandId] = true; + } + } + if (($task['resume_source_kind'] ?? null) === 'workflow_signal') { + if (($signalId = self::identity($task['workflow_signal_id'] ?? $task['resume_source_id'] ?? null)) !== null) { + $signalIds[$signalId] = true; + } + if (($commandId = self::identity($task['workflow_command_id'] ?? null)) !== null) { + $commandIds[$commandId] = true; + } + } + + return new self($signalIds, $commandIds); + } + + /** + * The compatibility view for a history recorded while every received signal + * was visible: also show each signal received before a later recorded + * workflow-task decision. + * + * @param list> $history + */ + public function withRecordedObservations(array $history): self + { + $observed = []; + $pending = []; + foreach ($history as $order => $event) { + $type = $event['event_type'] ?? $event['type'] ?? null; + if ($type === 'SignalReceived') { + $pending[] = $order; + } elseif (in_array($type, self::DECISION_EVENTS, true)) { + foreach ($pending as $received) { + $observed[$received] = true; + } + $pending = []; + } + } + + return new self($this->signalIds, $this->commandIds, $observed); + } + + /** + * @param array $event a `SignalReceived` event + * @param int|null $order its position in the history + */ + public function observes(array $event, ?int $order = null): bool + { + if ($order !== null && isset($this->observedOrders[$order])) { + return true; + } + $payload = is_array($event['payload'] ?? null) ? $event['payload'] : []; + if (self::recordedSequence($payload['workflow_sequence'] ?? null)) { + return true; + } + $signalId = self::identity($payload['signal_id'] ?? null); + $commandId = self::identity($payload['workflow_command_id'] ?? $event['workflow_command_id'] ?? null); + if ($signalId === null && $commandId === null) { + return true; + } + + return ($signalId !== null && isset($this->signalIds[$signalId])) + || ($commandId !== null && isset($this->commandIds[$commandId])); + } + + /** + * Whether any received signal in the history is hidden from workflow code. + * + * @param list> $history + */ + public function hidesAny(array $history): bool + { + foreach ($history as $order => $event) { + if (($event['event_type'] ?? $event['type'] ?? null) === 'SignalReceived' && !$this->observes($event, $order)) { + return true; + } + } + + return false; + } + + /** + * Whether this view shows a received signal the other view hides. + * + * @param list> $history + */ + public function showsMoreThan(self $other, array $history): bool + { + foreach ($history as $order => $event) { + if (($event['event_type'] ?? $event['type'] ?? null) === 'SignalReceived' + && $this->observes($event, $order) && !$other->observes($event, $order)) { + return true; + } + } + + return false; + } + + private static function identity(mixed $value): ?string + { + return (is_string($value) && $value !== '') || is_int($value) ? (string) $value : null; + } + + private static function recordedSequence(mixed $value): bool + { + return (is_int($value) && $value > 0) || (is_string($value) && preg_match('/\A[1-9][0-9]*\z/', $value) === 1); + } +} diff --git a/src/Worker/Replayer.php b/src/Worker/Replayer.php index b3359f18..1fc8a2f7 100644 --- a/src/Worker/Replayer.php +++ b/src/Worker/Replayer.php @@ -148,6 +148,60 @@ public function replay( bool $replayCommittedCancellationScopes = false, bool $prepareCancellationScopeDelivery = false, bool $allowScopedPreparedLocalActivities = false, + ): ReplayResult { + // sdk-php#434: workflow code observes a signal only once the Server has + // applied it (or this task delivers it). Histories recorded while the SDK + // showed every received signal stay replayable: when hiding a received + // but unapplied signal contradicts the recorded history, the signals an + // earlier task could see (those received before a later recorded + // decision) are shown again. Signals received after the last recorded + // decision stay hidden until applied. + $applied = AppliedSignals::fromHistory($history, $task); + $replay = fn (?AppliedSignals $visibility): ReplayResult => $this->replayWithSignals( + $visibility, $handler, $history, $input, $taskQueue, $task, $localActivityExecutor, $prepareLocalActivities, + $prepareLocalActivityGroups, $localActivityCancellationPolicies, $allowCancellationScopeAuthoring, + $replayCommittedCancellationScopes, $prepareCancellationScopeDelivery, $allowScopedPreparedLocalActivities, + ); + if (!$applied->hidesAny($history)) { + return $replay($applied); + } + try { + return $replay($applied); + } catch (NonDeterministicWorkflow $appliedOnly) { + $recorded = $applied->withRecordedObservations($history); + if (!$recorded->showsMoreThan($applied, $history)) { + throw $appliedOnly; + } + try { + return $replay($recorded); + } catch (NonDeterministicWorkflow) { + throw $appliedOnly; + } + } + } + + /** + * @param callable(WorkflowContext, mixed ...$input): mixed $handler + * @param list> $history + * @param list $input + * @param array $task + * @param list $localActivityCancellationPolicies + */ + private function replayWithSignals( + ?AppliedSignals $appliedSignals, + callable $handler, + array $history, + array $input, + string $taskQueue, + array $task, + ?callable $localActivityExecutor, + bool $prepareLocalActivities, + bool $prepareLocalActivityGroups, + array $localActivityCancellationPolicies, + bool $allowCancellationScopeAuthoring, + bool $replayCommittedCancellationScopes, + bool $prepareCancellationScopeDelivery, + bool $allowScopedPreparedLocalActivities, ): ReplayResult { if ($prepareLocalActivityGroups && !$prepareLocalActivities) { throw new LogicException('Prepared local groups require prepared local activity admission.'); @@ -226,7 +280,7 @@ public function replay( $context = null; $businessReplayClock = new BusinessReplayClock($history, (string) ($task['workflow_id'] ?? ''), (string) ($task['run_id'] ?? '')); $this->lastBusinessReplayClock = $businessReplayClock; - $execution = new Fiber(ExecutionTelemetry::bind(function () use ($handler, $history, $input, $task, $businessReplayClock, $cancellation, $scopes, $localActivityExecutor, $prepareLocalActivities, $prepareLocalActivityGroups, $localActivityCancellationPolicies, $allowScopeAuthoring, $allowScopedPreparedLocalActivities, &$context): mixed { + $execution = new Fiber(ExecutionTelemetry::bind(function () use ($handler, $history, $input, $task, $businessReplayClock, $appliedSignals, $cancellation, $scopes, $localActivityExecutor, $prepareLocalActivities, $prepareLocalActivityGroups, $localActivityCancellationPolicies, $allowScopeAuthoring, $allowScopedPreparedLocalActivities, &$context): mixed { $current = Fiber::getCurrent(); if ($current === null) { throw new LogicException('Workflow execution did not start inside its Fiber.'); @@ -249,6 +303,7 @@ public function replay( $scopes->openings !== [], $allowScopedPreparedLocalActivities, $businessReplayClock, + $appliedSignals, ); try { diff --git a/src/Worker/WorkflowContext.php b/src/Worker/WorkflowContext.php index efc0abac..aaa6549a 100644 --- a/src/Worker/WorkflowContext.php +++ b/src/Worker/WorkflowContext.php @@ -174,6 +174,7 @@ public function __construct( private bool $hasAuthoredCancellationScopes = false, private readonly bool $allowScopedPreparedLocalActivities = false, private readonly ?BusinessReplayClock $businessReplayClock = null, + private readonly ?AppliedSignals $appliedSignals = null, ) { $this->execution = $execution; $this->loadMessageStreamMessages(); @@ -883,14 +884,34 @@ public function observeCancellationReplayTime(?array $event): void ($this->cancellationReplayClock ??= new CancellationReplayClock())->observe($event); } - /** @return list> */ + /** + * The arguments of each signal with this name applied to the run, in the + * order the Server received them. + * + * A signal the Server has received but not yet applied is not included: + * the Server applies signals in message order with updates, when the + * workflow task that delivers the signal completes, and can still reject + * one it has not applied (sdk-php#434). The signal the current workflow + * task delivers is included. + * + * @return list> + */ public function signals(string $signalName): array + { + return $this->receivedSignals($signalName, $this->appliedSignals); + } + + /** @return list> */ + private function receivedSignals(string $signalName, ?AppliedSignals $applied): array { $signals = []; - foreach ($this->history as $event) { + foreach ($this->history as $order => $event) { if (($event['event_type'] ?? $event['type'] ?? null) !== 'SignalReceived') { continue; } + if ($applied !== null && !$applied->observes($event, $order)) { + continue; + } $payload = isset($event['payload']) && is_array($event['payload']) ? $event['payload'] : []; if (($payload['signal_name'] ?? null) !== $signalName) { continue; @@ -935,7 +956,8 @@ private function loadMessageStreamMessages(): void { $seenPositions = []; $seenMessageIds = []; - foreach ($this->signals(self::MESSAGE_STREAM_SIGNAL) as $arguments) { + // Message streams keep their own cursor over every received message. + foreach ($this->receivedSignals(self::MESSAGE_STREAM_SIGNAL, null) as $arguments) { if (count($arguments) !== 1 || !is_array($arguments[0])) { continue; } diff --git a/tests/AppliedSignalsTest.php b/tests/AppliedSignalsTest.php new file mode 100644 index 00000000..942580de --- /dev/null +++ b/tests/AppliedSignalsTest.php @@ -0,0 +1,196 @@ +codec = new AvroPayloadCodec(); + } + + public function testAQueuedUpdateIsAppliedBeforeAReceivedSignalClosesTheRun(): void + { + // The update was queued before the release signal. The update's task + // must not see the release signal, so the run stays open for it. + $history = [ + $this->conditionOpened(), + ['event_type' => 'UpdateAccepted', 'payload' => ['update_id' => 'update-1', 'update_name' => 'change', + 'arguments' => $this->codec->envelope([1])]], + $this->received('signal-release', 'command-release', 'release'), + ]; + + $updateTask = $this->replay($history, ['workflow_update_id' => 'update-1', 'update_name' => 'change']); + self::assertNotContains('complete_workflow', array_column($updateTask->commands, 'type')); + + // The signal's own task delivers it: now the condition is satisfied. + $signalTask = $this->replay($history, ['resume_source_kind' => 'workflow_signal', + 'workflow_signal_id' => 'signal-release', 'workflow_command_id' => 'command-release', 'signal_name' => 'release']); + self::assertSame('complete_workflow', $signalTask->commands[0]['type']); + self::assertSame(['released' => [['go']]], $this->codec->decodeEnvelope($signalTask->commands[0]['result'])); + } + + public function testARejectedSignalIsNeverObserved(): void + { + $history = [ + $this->conditionOpened(), + $this->received('signal-release', 'command-release', 'release'), + ['event_type' => 'SignalRejected', 'payload' => ['signal_id' => 'signal-release', + 'workflow_command_id' => 'command-release', 'signal_name' => 'release', + 'rejection_reason' => 'run_closed_before_signal_applied']], + ]; + + self::assertNotContains('complete_workflow', array_column($this->replay($history)->commands, 'type')); + } + + public function testAppliedSignalsAreObservedInReceiptOrder(): void + { + $history = [ + $this->received('signal-1', 'command-1', 'release', 'first'), + $this->received('signal-2', 'command-2', 'release', 'second'), + $this->received('signal-3', 'command-3', 'release', 'third', workflowSequence: 4), + $this->received('signal-4', 'command-4', 'release', 'fourth'), + ['event_type' => 'SignalApplied', 'payload' => ['signal_id' => 'signal-2', 'workflow_command_id' => 'command-2', + 'signal_name' => 'release', 'value' => $this->codec->envelope(['second'])]], + ['event_type' => 'SignalApplied', 'payload' => ['workflow_command_id' => 'command-1', 'signal_name' => 'release', + 'value' => $this->codec->envelope(['first'])]], + ['event_type' => 'SignalReceived', 'payload' => ['signal_name' => 'release', + 'arguments' => $this->codec->envelope(['imported'])]], + ]; + $workflow = static fn (WorkflowContext $context): array => $context->signals('release'); + + $result = (new Replayer($this->codec))->replay($workflow, $history, [], 'php-workers'); + + // Applied by event (by signal or command identity), consumed with a + // sequence, and an identity-less imported record; never the unapplied one. + self::assertSame([['first'], ['second'], ['third'], ['imported']], + $this->codec->decodeEnvelope($result->commands[0]['result'])); + } + + public function testALegacyHistoryKeepsTheSignalsItsTasksObservedButHidesLaterOnes(): void + { + // Recorded by an SDK that showed every received signal: the activity was + // chosen while signal-observed was received but not yet applied. + $history = [ + $this->received('signal-observed', 'command-observed', 'approve', 'first'), + ['event_type' => 'ActivityScheduled', 'payload' => ['sequence' => 1, 'activity_type' => 'approved']], + ['event_type' => 'ActivityCompleted', 'payload' => ['sequence' => 1, 'activity_type' => 'approved', + 'result' => $this->codec->envelope('ok')]], + $this->received('signal-later', 'command-later', 'approve', 'later'), + ]; + $workflow = static function (WorkflowContext $context): array { + $result = $context->activity($context->signals('approve') !== [] ? 'approved' : 'waiting'); + + return ['result' => $result, 'signals' => $context->signals('approve')]; + }; + + $result = (new Replayer($this->codec))->replay($workflow, $history, [], 'php-workers'); + + self::assertSame(['result' => 'ok', 'signals' => [['first']]], + $this->codec->decodeEnvelope($result->commands[0]['result'])); + } + + public function testAGenuinelyChangedWorkflowStillReportsTheAppliedOnlyDivergence(): void + { + $history = [ + $this->received('signal-observed', 'command-observed', 'approve'), + ['event_type' => 'ActivityScheduled', 'payload' => ['sequence' => 1, 'activity_type' => 'removed']], + ]; + $workflow = static function (WorkflowContext $context): mixed { + return $context->activity($context->signals('approve') !== [] ? 'approved' : 'waiting'); + }; + + try { + (new Replayer($this->codec))->replay($workflow, $history, [], 'php-workers'); + self::fail('A changed workflow must remain nondeterministic.'); + } catch (NonDeterministicWorkflow $error) { + self::assertSame(1, $error->sequence); + self::assertStringContainsString('waiting', $error->getMessage()); + } + } + + public function testFreshDecisionsWithoutContradictionUseOnlyAppliedSignals(): void + { + $history = [ + $this->received('signal-pending', 'command-pending', 'approve'), + ]; + $workflow = static fn (WorkflowContext $context): mixed + => $context->activity($context->signals('approve') !== [] ? 'approved' : 'waiting'); + + $result = (new Replayer($this->codec))->replay($workflow, $history, [], 'php-workers'); + + self::assertSame('schedule_activity', $result->commands[0]['type']); + self::assertSame('waiting', $result->commands[0]['activity_type']); + } + + public function testRecordedObservationsCoverOnlySignalsBeforeTheLastDecision(): void + { + $history = [ + $this->received('signal-1', 'command-1', 'approve'), + ['event_type' => 'TimerScheduled', 'payload' => ['sequence' => 1]], + $this->received('signal-2', 'command-2', 'approve'), + ['event_type' => 'TimerFired', 'payload' => ['sequence' => 1]], + $this->received('signal-3', 'command-3', 'approve'), + ]; + $applied = AppliedSignals::fromHistory($history, []); + $recorded = $applied->withRecordedObservations($history); + + self::assertFalse($applied->observes($history[0], 0)); + self::assertTrue($recorded->observes($history[0], 0)); + self::assertFalse($recorded->observes($history[2], 2)); + self::assertFalse($recorded->observes($history[4], 4)); + self::assertTrue($recorded->showsMoreThan($applied, $history)); + self::assertTrue($recorded->hidesAny($history)); + } + + /** @return array */ + private function conditionOpened(): array + { + return ['event_type' => 'ConditionWaitOpened', 'payload' => ['sequence' => 1, + 'condition_wait_id' => 'wait-release', 'condition_key' => 'release', 'timeout_seconds' => 300]]; + } + + /** @return array */ + private function received(string $signalId, string $commandId, string $name, string $value = 'go', ?int $workflowSequence = null): array + { + $payload = ['signal_id' => $signalId, 'workflow_command_id' => $commandId, 'signal_name' => $name, + 'arguments' => $this->codec->envelope([$value])]; + if ($workflowSequence !== null) { + $payload['workflow_sequence'] = $workflowSequence; + } + + return ['event_type' => 'SignalReceived', 'payload' => $payload]; + } + + /** + * @param list> $history + * @param array $task + */ + private function replay(array $history, array $task = []): \DurableWorkflow\Worker\ReplayResult + { + $workflow = static function (WorkflowContext $context): array { + $context->waitCondition(static fn (): bool => $context->signals('release') !== [], key: 'release', timeout: 300); + + return ['released' => $context->signals('release')]; + }; + + return (new Replayer($this->codec))->replay($workflow, $history, [], 'php-workers', + ['workflow_id' => 'workflow-434', 'run_id' => 'run-434'] + $task); + } +} diff --git a/tests/Support/ReplayRegressionFixture.php b/tests/Support/ReplayRegressionFixture.php index f370645a..ed7ac8da 100644 --- a/tests/Support/ReplayRegressionFixture.php +++ b/tests/Support/ReplayRegressionFixture.php @@ -199,6 +199,16 @@ public static function execute(array $fixture): array $codec = new AvroPayloadCodec(); $scopedCleanup = $workflowType === 'golden.prepared-local-scoped-cleanup'; $task = self::taskAttributes($workflowType); + $workerTask = $fixture['worker_task'] ?? []; + if (!is_array($workerTask)) { + throw new RuntimeException("{$identity}.worker_task must be an object."); + } + // The resume context a signal-delivering task carries (sdk-php#434). + foreach (['resume_source_kind', 'resume_source_id', 'workflow_signal_id', 'workflow_command_id', 'signal_name'] as $field) { + if (array_key_exists($field, $workerTask)) { + $task[$field] = $workerTask[$field]; + } + } if ($scopedCleanup) { foreach ($history as $event) { if (($event['event_type'] ?? $event['type'] ?? null) === 'WorkflowStarted') { @@ -525,6 +535,15 @@ private static function workflow(string $workflowType): callable return $result; }, + // sdk-php#434: only applied signals, and the one this task delivers. + 'golden.signal-applied' => static fn (WorkflowContext $context): array => ['signals' => $context->signals('approve')], + // sdk-php#434: a decision taken from the signals visible to this task. + 'golden.signal-decision' => static function (WorkflowContext $context): array { + $approved = $context->signals('approve') !== []; + $result = $context->activity($approved ? 'golden.approved' : 'golden.waiting'); + + return ['result' => $result, 'signals' => $context->signals('approve')]; + }, 'golden.immediate-condition' => static function (WorkflowContext $context): array { $first = $context->activity('golden.first'); $satisfied = $context->waitCondition(static fn (): bool => true, key: 'already-ready'); diff --git a/tests/fixtures/replay-regressions/signals-observe-only-applied.json b/tests/fixtures/replay-regressions/signals-observe-only-applied.json new file mode 100644 index 00000000..8988ff06 --- /dev/null +++ b/tests/fixtures/replay-regressions/signals-observe-only-applied.json @@ -0,0 +1,70 @@ +{ + "$schema": "https://raw.githubusercontent.com/durable-workflow/.github/main/regression-corpus/evidence-schema.json", + "fixture_schema": "durable-workflow.replay-regression/v1", + "id": "php-signals-observe-only-applied", + "protocol_version": "1.20", + "bindings": ["php"], + "workflow": { + "type": "golden.signal-applied", + "input": [] + }, + "worker_task": { + "resume_source_kind": "workflow_signal", + "resume_source_id": "signal-current", + "workflow_signal_id": "signal-current", + "workflow_command_id": "command-current", + "signal_name": "approve" + }, + "history": [ + { + "event_type": "SignalReceived", + "payload": { + "signal_id": "signal-first", + "workflow_command_id": "command-first", + "signal_name": "approve", + "arguments": {"codec": "avro", "blob": "wwHioz3/VYAiNwwCCgpmaXJzdAA="} + } + }, + { + "event_type": "SignalApplied", + "payload": { + "signal_id": "signal-first", + "workflow_command_id": "command-first", + "signal_name": "approve", + "value": {"codec": "avro", "blob": "wwHioz3/VYAiNwwCCgpmaXJzdAA="} + } + }, + { + "event_type": "SignalReceived", + "payload": { + "signal_id": "signal-current", + "workflow_command_id": "command-current", + "signal_name": "approve", + "arguments": {"codec": "avro", "blob": "wwHioz3/VYAiNwwCCg5jdXJyZW50AA=="} + } + }, + { + "event_type": "SignalReceived", + "payload": { + "signal_id": "signal-unapplied", + "workflow_command_id": "command-unapplied", + "signal_name": "approve", + "arguments": {"codec": "avro", "blob": "wwHioz3/VYAiNwwCCgxzZWNvbmQA"} + } + } + ], + "command_sequence": [ + { + "type": "complete_workflow", + "result": {"signals": [["first"], ["current"]]} + } + ], + "expected": { + "command_sequence": [ + { + "type": "complete_workflow", + "result": {"signals": [["first"], ["current"]]} + } + ] + } +} diff --git a/tests/fixtures/replay-regressions/signals-unapplied-legacy-history.json b/tests/fixtures/replay-regressions/signals-unapplied-legacy-history.json new file mode 100644 index 00000000..a4224b5a --- /dev/null +++ b/tests/fixtures/replay-regressions/signals-unapplied-legacy-history.json @@ -0,0 +1,60 @@ +{ + "$schema": "https://raw.githubusercontent.com/durable-workflow/.github/main/regression-corpus/evidence-schema.json", + "fixture_schema": "durable-workflow.replay-regression/v1", + "id": "php-signals-unapplied-legacy-history", + "protocol_version": "1.20", + "bindings": ["php"], + "workflow": { + "type": "golden.signal-decision", + "input": [] + }, + "history": [ + { + "event_type": "SignalReceived", + "payload": { + "signal_id": "signal-observed", + "workflow_command_id": "command-observed", + "signal_name": "approve", + "arguments": {"codec": "avro", "blob": "wwHioz3/VYAiNwwCCgpmaXJzdAA="} + } + }, + { + "event_type": "ActivityScheduled", + "payload": { + "sequence": 1, + "activity_type": "golden.approved" + } + }, + { + "event_type": "ActivityCompleted", + "payload": { + "sequence": 1, + "activity_type": "golden.approved", + "result": {"codec": "avro", "blob": "wwHioz3/VYAiNwoeYXBwcm92ZWQtcmVzdWx0"} + } + }, + { + "event_type": "SignalReceived", + "payload": { + "signal_id": "signal-later", + "workflow_command_id": "command-later", + "signal_name": "approve", + "arguments": {"codec": "avro", "blob": "wwHioz3/VYAiNwwCCgxzZWNvbmQA"} + } + } + ], + "command_sequence": [ + { + "type": "complete_workflow", + "result": {"result": "approved-result", "signals": [["first"]]} + } + ], + "expected": { + "command_sequence": [ + { + "type": "complete_workflow", + "result": {"result": "approved-result", "signals": [["first"]]} + } + ] + } +} From 48b14a3db75b353c8aae2661fe65443b15d3dca1 Mon Sep 17 00:00:00 2001 From: Richard McDaniel <1130888+rmcdaniel@users.noreply.github.com> Date: Thu, 19 Nov 2026 06:13:37 +0000 Subject: [PATCH 2/3] Keep the replay entry point and split the signal view helpers Route the signal-compatibility retry through the existing replay() entry instead of renaming its body, and split the applied-signal checks into smaller functions. Behaviour is unchanged. Co-Authored-By: Claude Opus 5.5 --- scripts/ci/run-replay-regression-fixture.php | 9 +- src/Worker/AppliedSignals.php | 52 +++++---- src/Worker/Replayer.php | 105 ++++++++++--------- src/Worker/WorkflowContext.php | 10 +- 4 files changed, 87 insertions(+), 89 deletions(-) diff --git a/scripts/ci/run-replay-regression-fixture.php b/scripts/ci/run-replay-regression-fixture.php index 1d17c88b..0565822d 100644 --- a/scripts/ci/run-replay-regression-fixture.php +++ b/scripts/ci/run-replay-regression-fixture.php @@ -679,7 +679,7 @@ private static function executeLayout( 'arguments' => $codec->envelope($input), 'history_events' => $layout === 'inline' ? $history : [], 'next_history_page_token' => $layout === 'paginated' ? 'regression-page-1' : '', - ], self::taskAttributes($workflowType), self::workerTaskAttributes($identity, $fixture)); + ], self::taskAttributes($workflowType), self::workerTaskAttributes($fixture)); $pagedHistory[$taskId] = $layout === 'paginated' ? $history : []; } $transport = new ReplayRegressionTransport( @@ -1408,12 +1408,9 @@ private static function selectionHandleIsolationWorkflow(): callable * @param array $fixture * @return array */ - private static function workerTaskAttributes(string $identity, array $fixture): array + private static function workerTaskAttributes(array $fixture): array { - $workerTask = $fixture['worker_task'] ?? []; - if (!is_array($workerTask)) { - throw new RuntimeException("{$identity}.worker_task must be an object."); - } + $workerTask = is_array($fixture['worker_task'] ?? null) ? $fixture['worker_task'] : []; return array_intersect_key($workerTask, array_flip([ 'resume_source_kind', 'resume_source_id', 'workflow_signal_id', 'workflow_command_id', 'signal_name', diff --git a/src/Worker/AppliedSignals.php b/src/Worker/AppliedSignals.php index 5c243823..22eca84f 100644 --- a/src/Worker/AppliedSignals.php +++ b/src/Worker/AppliedSignals.php @@ -65,30 +65,34 @@ public static function fromHistory(array $history, array $task): self foreach ($history as $event) { $type = $event['event_type'] ?? $event['type'] ?? null; $payload = is_array($event['payload'] ?? null) ? $event['payload'] : []; - $applied = $type === 'SignalApplied' - || ($type === 'SignalReceived' && self::recordedSequence($payload['workflow_sequence'] ?? null)); - if (!$applied) { - continue; - } - if (($signalId = self::identity($payload['signal_id'] ?? null)) !== null) { - $signalIds[$signalId] = true; - } - if (($commandId = self::identity($payload['workflow_command_id'] ?? $event['workflow_command_id'] ?? null)) !== null) { - $commandIds[$commandId] = true; + if ($type === 'SignalApplied' + || ($type === 'SignalReceived' && self::recordedSequence($payload['workflow_sequence'] ?? null))) { + self::remember($signalIds, $commandIds, $payload['signal_id'] ?? null, + $payload['workflow_command_id'] ?? $event['workflow_command_id'] ?? null); } } if (($task['resume_source_kind'] ?? null) === 'workflow_signal') { - if (($signalId = self::identity($task['workflow_signal_id'] ?? $task['resume_source_id'] ?? null)) !== null) { - $signalIds[$signalId] = true; - } - if (($commandId = self::identity($task['workflow_command_id'] ?? null)) !== null) { - $commandIds[$commandId] = true; - } + self::remember($signalIds, $commandIds, $task['workflow_signal_id'] ?? $task['resume_source_id'] ?? null, + $task['workflow_command_id'] ?? null); } return new self($signalIds, $commandIds); } + /** + * @param array $signalIds + * @param array $commandIds + */ + private static function remember(array &$signalIds, array &$commandIds, mixed $signalId, mixed $commandId): void + { + if (($signalId = self::identity($signalId)) !== null) { + $signalIds[$signalId] = true; + } + if (($commandId = self::identity($commandId)) !== null) { + $commandIds[$commandId] = true; + } + } + /** * The compatibility view for a history recorded while every received signal * was visible: also show each signal received before a later recorded @@ -121,20 +125,14 @@ public function withRecordedObservations(array $history): self */ public function observes(array $event, ?int $order = null): bool { - if ($order !== null && isset($this->observedOrders[$order])) { - return true; - } $payload = is_array($event['payload'] ?? null) ? $event['payload'] : []; - if (self::recordedSequence($payload['workflow_sequence'] ?? null)) { - return true; - } $signalId = self::identity($payload['signal_id'] ?? null); $commandId = self::identity($payload['workflow_command_id'] ?? $event['workflow_command_id'] ?? null); - if ($signalId === null && $commandId === null) { - return true; - } - return ($signalId !== null && isset($this->signalIds[$signalId])) + return ($order !== null && isset($this->observedOrders[$order])) + || self::recordedSequence($payload['workflow_sequence'] ?? null) + || ($signalId === null && $commandId === null) + || ($signalId !== null && isset($this->signalIds[$signalId])) || ($commandId !== null && isset($this->commandIds[$commandId])); } @@ -178,6 +176,6 @@ private static function identity(mixed $value): ?string private static function recordedSequence(mixed $value): bool { - return (is_int($value) && $value > 0) || (is_string($value) && preg_match('/\A[1-9][0-9]*\z/', $value) === 1); + return (is_int($value) && $value > 0) || (is_string($value) && preg_match('/\A[1-9]\d*\z/', $value) === 1); } } diff --git a/src/Worker/Replayer.php b/src/Worker/Replayer.php index 1fc8a2f7..02b51a87 100644 --- a/src/Worker/Replayer.php +++ b/src/Worker/Replayer.php @@ -121,65 +121,65 @@ public function refuseNewGroupedConditionWaits(?Closure $refusal): void private ?BusinessReplayClock $lastBusinessReplayClock = null; - /** @internal Whether the most recent replay asked for business time it could not have. */ - public function lastReplayReadUnavailableTime(): bool - { - return ($this->lastBusinessReplayClock?->unavailableReads() ?? 0) > 0; - } + /** The signals the replay in progress shows workflow code (sdk-php#434). */ + private ?AppliedSignals $signalView = null; + + private bool $replayingSignalView = false; /** - * @param callable(WorkflowContext, mixed ...$input): mixed $handler + * sdk-php#434: workflow code observes a signal only once the Server has + * applied it (or this task delivers it). Histories recorded while the SDK + * showed every received signal stay replayable: when hiding a received but + * unapplied signal contradicts the recorded history, the signals an earlier + * task could see (those received before a later recorded decision) are + * shown again. Signals received after the last recorded decision stay + * hidden until applied. + * * @param list> $history - * @param list $input * @param array $task - * @param list $localActivityCancellationPolicies + * @param Closure(): ReplayResult $replay */ - public function replay( - callable $handler, - array $history, - array $input, - string $taskQueue, - array $task = [], - ?callable $localActivityExecutor = null, - bool $prepareLocalActivities = false, - bool $prepareLocalActivityGroups = false, - array $localActivityCancellationPolicies = [], - bool $allowCancellationScopeAuthoring = false, - bool $replayCommittedCancellationScopes = false, - bool $prepareCancellationScopeDelivery = false, - bool $allowScopedPreparedLocalActivities = false, - ): ReplayResult { - // sdk-php#434: workflow code observes a signal only once the Server has - // applied it (or this task delivers it). Histories recorded while the SDK - // showed every received signal stay replayable: when hiding a received - // but unapplied signal contradicts the recorded history, the signals an - // earlier task could see (those received before a later recorded - // decision) are shown again. Signals received after the last recorded - // decision stay hidden until applied. + private function replayWithSignalCompatibility(array $history, array $task, Closure $replay): ReplayResult + { $applied = AppliedSignals::fromHistory($history, $task); - $replay = fn (?AppliedSignals $visibility): ReplayResult => $this->replayWithSignals( - $visibility, $handler, $history, $input, $taskQueue, $task, $localActivityExecutor, $prepareLocalActivities, - $prepareLocalActivityGroups, $localActivityCancellationPolicies, $allowCancellationScopeAuthoring, - $replayCommittedCancellationScopes, $prepareCancellationScopeDelivery, $allowScopedPreparedLocalActivities, - ); + $view = fn (AppliedSignals $signals): ReplayResult => $this->replayWithSignalView($signals, $replay); if (!$applied->hidesAny($history)) { - return $replay($applied); + return $view($applied); } try { - return $replay($applied); + return $view($applied); } catch (NonDeterministicWorkflow $appliedOnly) { $recorded = $applied->withRecordedObservations($history); if (!$recorded->showsMoreThan($applied, $history)) { throw $appliedOnly; } try { - return $replay($recorded); + return $view($recorded); } catch (NonDeterministicWorkflow) { throw $appliedOnly; } } } + /** @param Closure(): ReplayResult $replay */ + private function replayWithSignalView(AppliedSignals $signals, Closure $replay): ReplayResult + { + $this->replayingSignalView = true; + $this->signalView = $signals; + try { + return $replay(); + } finally { + $this->replayingSignalView = false; + $this->signalView = null; + } + } + + /** @internal Whether the most recent replay asked for business time it could not have. */ + public function lastReplayReadUnavailableTime(): bool + { + return ($this->lastBusinessReplayClock?->unavailableReads() ?? 0) > 0; + } + /** * @param callable(WorkflowContext, mixed ...$input): mixed $handler * @param list> $history @@ -187,22 +187,29 @@ public function replay( * @param array $task * @param list $localActivityCancellationPolicies */ - private function replayWithSignals( - ?AppliedSignals $appliedSignals, + public function replay( callable $handler, array $history, array $input, string $taskQueue, - array $task, - ?callable $localActivityExecutor, - bool $prepareLocalActivities, - bool $prepareLocalActivityGroups, - array $localActivityCancellationPolicies, - bool $allowCancellationScopeAuthoring, - bool $replayCommittedCancellationScopes, - bool $prepareCancellationScopeDelivery, - bool $allowScopedPreparedLocalActivities, + array $task = [], + ?callable $localActivityExecutor = null, + bool $prepareLocalActivities = false, + bool $prepareLocalActivityGroups = false, + array $localActivityCancellationPolicies = [], + bool $allowCancellationScopeAuthoring = false, + bool $replayCommittedCancellationScopes = false, + bool $prepareCancellationScopeDelivery = false, + bool $allowScopedPreparedLocalActivities = false, ): ReplayResult { + if (!$this->replayingSignalView) { + return $this->replayWithSignalCompatibility($history, $task, fn (): ReplayResult => $this->replay( + $handler, $history, $input, $taskQueue, $task, $localActivityExecutor, $prepareLocalActivities, + $prepareLocalActivityGroups, $localActivityCancellationPolicies, $allowCancellationScopeAuthoring, + $replayCommittedCancellationScopes, $prepareCancellationScopeDelivery, $allowScopedPreparedLocalActivities, + )); + } + $appliedSignals = $this->signalView; if ($prepareLocalActivityGroups && !$prepareLocalActivities) { throw new LogicException('Prepared local groups require prepared local activity admission.'); } diff --git a/src/Worker/WorkflowContext.php b/src/Worker/WorkflowContext.php index aaa6549a..bffbec38 100644 --- a/src/Worker/WorkflowContext.php +++ b/src/Worker/WorkflowContext.php @@ -906,14 +906,10 @@ private function receivedSignals(string $signalName, ?AppliedSignals $applied): { $signals = []; foreach ($this->history as $order => $event) { - if (($event['event_type'] ?? $event['type'] ?? null) !== 'SignalReceived') { - continue; - } - if ($applied !== null && !$applied->observes($event, $order)) { - continue; - } $payload = isset($event['payload']) && is_array($event['payload']) ? $event['payload'] : []; - if (($payload['signal_name'] ?? null) !== $signalName) { + if (($event['event_type'] ?? $event['type'] ?? null) !== 'SignalReceived' + || ($payload['signal_name'] ?? null) !== $signalName + || ($applied !== null && !$applied->observes($event, $order))) { continue; } $raw = $payload['value'] ?? $payload['input'] ?? $payload['arguments'] ?? null; From 2d8416a4e27926cf09c3353e6d52c49664b35287 Mon Sep 17 00:00:00 2001 From: Richard McDaniel <1130888+rmcdaniel@users.noreply.github.com> Date: Thu, 19 Nov 2026 06:47:20 +0000 Subject: [PATCH 3/3] Gate the applied-only signal view with a version marker (#434) Replace the two-pass replay fallback with the standard version marker durable-workflow.sdk-php.signals-applied-only. The worker records it the first time hiding an unapplied signal changes what signals() returns to a live decision: a call at the end of the recorded history, or the predicate of a condition wait whose outcome is not recorded. A history without the marker replays with every received signal visible. With it, live decisions and calls replayed at or after the marker's sequence use the applied-only view, and calls replayed before it keep the earlier view. The marker step is transparent to the authored command sequence, and runs that never meet an unapplied signal record nothing. Replay fixtures cover a fresh run recording the marker, a history without it, and a history with it. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 15 +- docs/sdk-reference.md | 16 +- src/Worker/AppliedSignals.php | 195 +++++++++++------- src/Worker/Replayer.php | 112 +++++----- src/Worker/WorkflowContext.php | 21 +- tests/AppliedSignalsTest.php | 155 +++++++++----- .../signals-applied-only-marker-recorded.json | 87 ++++++++ .../signals-observe-only-applied.json | 60 +++++- .../signals-unapplied-legacy-history.json | 43 +++- 9 files changed, 492 insertions(+), 212 deletions(-) create mode 100644 tests/fixtures/replay-regressions/signals-applied-only-marker-recorded.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 22d4e956..0b09e17c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,11 +43,16 @@ project follows [Semantic Versioning](https://semver.org/). rejected, for example closing the run before a queued update was applied. Signals now reach workflow code in message order with updates, as in embedded Workflow, Python and Rust. `SignalReceived` records that name neither a - signal ID nor a command ID are still returned. Replay compatibility: a history - recorded by an earlier release still replays unchanged. When hiding an - unapplied signal contradicts that history, the replay shows again each signal - received before a later recorded workflow decision; signals received after the - last recorded decision stay hidden until applied (#434). + signal ID nor a command ID are still returned. Replay compatibility: the + change is gated by the version marker + `durable-workflow.sdk-php.signals-applied-only`. The worker records it the + first time hiding an unapplied signal changes what `signals()` returns to a + live decision (a call at the end of the recorded history, or the predicate of + a condition wait whose outcome is not recorded). A history without the marker + replays with every received signal visible; with it, live decisions and calls + replayed at or after the marker's sequence use the applied-only view, and + calls replayed before it keep the earlier view. Runs that never meet an + unapplied signal record no marker (#434). ## [3.20.0] - 2026-11-18 diff --git a/docs/sdk-reference.md b/docs/sdk-reference.md index 63ad42f8..c65df3d7 100644 --- a/docs/sdk-reference.md +++ b/docs/sdk-reference.md @@ -718,11 +718,17 @@ signal the current workflow task delivers, in the order the Server received them. The Server applies signals in message order with updates, so an update sent before a signal is applied first. A signal that has been received but not yet applied is not returned, and one the Server rejects (for example because -the run closed first) never is. Histories recorded by earlier SDK releases, -which returned every received signal, still replay: if hiding an unapplied -signal would contradict the recorded history, the signals received before that -history's last recorded workflow decision are returned again. Signals received -after it stay hidden until applied. +the run closed first) never is. + +Earlier SDK releases returned every received signal, so the applied-only view +is versioned with the marker `durable-workflow.sdk-php.signals-applied-only`. +The worker records it the first time hiding an unapplied signal changes what +`signals()` returns to a live decision: a call at the end of the recorded +history, or the predicate of a condition wait whose outcome is not recorded. +A history without the marker replays with every received signal visible. With +the marker, live decisions and calls replayed at or after the marker's sequence +use the applied-only view, and calls replayed before it keep the earlier view. +A run that never meets an unapplied signal records no marker. The PHP SDK does not expose update-validator authoring. Worker registration therefore declares an empty `update_validators` list for every workflow type; diff --git a/src/Worker/AppliedSignals.php b/src/Worker/AppliedSignals.php index 22eca84f..0e05ded2 100644 --- a/src/Worker/AppliedSignals.php +++ b/src/Worker/AppliedSignals.php @@ -4,14 +4,16 @@ namespace DurableWorkflow\Worker; +use Closure; + /** - * @internal Which received signals workflow code may observe (sdk-php#434). + * @internal Which received signals workflow code observes (sdk-php#434). * * The Server records `SignalReceived` when it accepts a signal, and applies the * signal to the run later, in message order with updates, when the workflow * task that delivers it completes. Until then the signal can still be rejected, - * for example when the run closes first (`SignalRejected`). Workflow code sees - * a signal only once it is applied, as embedded Workflow, Python and Rust do: + * for example when the run closes first (`SignalRejected`). A signal is applied + * when: * * - a `SignalApplied` event names its signal ID or command ID; * - its `SignalReceived` carries a `workflow_sequence`, which the Server adds @@ -21,36 +23,47 @@ * which the task's completion applies; or * - its `SignalReceived` names neither a signal ID nor a command ID, so the * history cannot say whether it was applied (imported or synthetic - * histories, and test fixtures). These remain visible as before. + * histories, and test fixtures). + * + * Earlier releases showed every received signal. The applied-only view is a + * versioned change recorded with the standard version marker + * {@see self::CHANGE_ID}: * - * Histories recorded before this rule let earlier tasks see received but - * unapplied signals. The Replayer keeps them replayable: when hiding such a - * signal contradicts the recorded history, it replays again with - * withRecordedObservations(), which also shows every signal received before a - * later workflow-task decision was recorded. An earlier SDK showed those - * signals to the task that recorded that decision. A signal received after the - * last recorded decision was seen by no earlier task and stays hidden until it - * is applied (see Replayer::replay()). + * - The marker is recorded the first time the applied-only view changes what + * `signals()` returns to a live decision: a call at the end of the recorded + * history, or the predicate of a condition wait whose outcome is not + * recorded. The applied-only view applies from that call on. + * - With the marker in the history, live decisions and replayed calls at or + * after the marker's sequence use the applied-only view. Replayed calls + * before it keep the earlier view, so the decisions recorded before the + * marker replay unchanged. + * - Without the marker, every replayed call keeps the earlier view, and a run + * that never meets an unapplied signal records nothing. + * + * The marker step is transparent to the authored command sequence. */ final class AppliedSignals { - /** Events only a workflow-task completion records: decisions taken by workflow code. */ - private const DECISION_EVENTS = [ - 'ActivityScheduled', 'TimerScheduled', 'ChildWorkflowScheduled', 'ConditionWaitOpened', 'SignalWaitOpened', - 'SideEffectRecorded', 'VersionMarkerRecorded', 'MemoUpserted', 'SearchAttributesUpserted', - 'ServiceOperationScheduled', 'CancellationScopeOpened', 'UpdateCompleted', 'WorkflowCompleted', - 'WorkflowFailed', 'WorkflowContinuedAsNew', - ]; + /** The version-marker change ID that selects the applied-only view. */ + public const CHANGE_ID = 'durable-workflow.sdk-php.signals-applied-only'; + + /** @var (Closure(): array{int, bool})|null the replay position and whether it is the live end */ + private ?Closure $position = null; + + private bool $liveEvaluation = false; + + private bool $markerRequested = false; + + private bool $markerFlushed = false; /** * @param array $signalIds * @param array $commandIds - * @param array $observedOrders history positions of signals shown for compatibility */ private function __construct( private readonly array $signalIds, private readonly array $commandIds, - private readonly array $observedOrders = [], + private readonly ?int $markerSequence, ) { } @@ -62,6 +75,7 @@ public static function fromHistory(array $history, array $task): self { $signalIds = []; $commandIds = []; + $markerSequence = null; foreach ($history as $event) { $type = $event['event_type'] ?? $event['type'] ?? null; $payload = is_array($event['payload'] ?? null) ? $event['payload'] : []; @@ -69,6 +83,8 @@ public static function fromHistory(array $history, array $task): self || ($type === 'SignalReceived' && self::recordedSequence($payload['workflow_sequence'] ?? null))) { self::remember($signalIds, $commandIds, $payload['signal_id'] ?? null, $payload['workflow_command_id'] ?? $event['workflow_command_id'] ?? null); + } elseif ($markerSequence === null && self::isMarker($type, $payload)) { + $markerSequence = $payload['sequence']; } } if (($task['resume_source_kind'] ?? null) === 'workflow_signal') { @@ -76,97 +92,128 @@ public static function fromHistory(array $history, array $task): self $task['workflow_command_id'] ?? null); } - return new self($signalIds, $commandIds); + return new self($signalIds, $commandIds, $markerSequence); + } + + /** @param array $payload */ + public static function isMarker(mixed $type, array $payload): bool + { + return $type === 'VersionMarkerRecorded' && ($payload['change_id'] ?? null) === self::CHANGE_ID + && is_int($payload['sequence'] ?? null) && $payload['sequence'] > 0; + } + + /** @internal The Replayer reports its position: the next authored sequence and whether it is the live end. */ + public function bind(Closure $position): void + { + $this->position = $position; } /** - * @param array $signalIds - * @param array $commandIds + * @internal Evaluate a condition predicate whose outcome is not recorded. + * + * @template T + * @param Closure(): T $evaluation + * @return T */ - private static function remember(array &$signalIds, array &$commandIds, mixed $signalId, mixed $commandId): void + public function live(Closure $evaluation): mixed { - if (($signalId = self::identity($signalId)) !== null) { - $signalIds[$signalId] = true; - } - if (($commandId = self::identity($commandId)) !== null) { - $commandIds[$commandId] = true; + $previous = $this->liveEvaluation; + $this->liveEvaluation = true; + try { + return $evaluation(); + } finally { + $this->liveEvaluation = $previous; } } /** - * The compatibility view for a history recorded while every received signal - * was visible: also show each signal received before a later recorded - * workflow-task decision. + * Keep the received signals workflow code observes, in history order. * - * @param list> $history + * @param array> $received `SignalReceived` events of one name by history position + * @return array> */ - public function withRecordedObservations(array $history): self + public function select(array $received): array { - $observed = []; - $pending = []; - foreach ($history as $order => $event) { - $type = $event['event_type'] ?? $event['type'] ?? null; - if ($type === 'SignalReceived') { - $pending[] = $order; - } elseif (in_array($type, self::DECISION_EVENTS, true)) { - foreach ($pending as $received) { - $observed[$received] = true; - } - $pending = []; - } + $applied = array_filter($received, fn (array $event): bool => $this->observes($event)); + if (count($applied) === count($received)) { + return $received; } - return new self($this->signalIds, $this->commandIds, $observed); + return $this->appliedOnly() || $this->requestMarker() ? $applied : $received; } - /** - * @param array $event a `SignalReceived` event - * @param int|null $order its position in the history - */ - public function observes(array $event, ?int $order = null): bool + /** @param array $event a `SignalReceived` event */ + public function observes(array $event): bool { $payload = is_array($event['payload'] ?? null) ? $event['payload'] : []; $signalId = self::identity($payload['signal_id'] ?? null); $commandId = self::identity($payload['workflow_command_id'] ?? $event['workflow_command_id'] ?? null); - return ($order !== null && isset($this->observedOrders[$order])) - || self::recordedSequence($payload['workflow_sequence'] ?? null) + return self::recordedSequence($payload['workflow_sequence'] ?? null) || ($signalId === null && $commandId === null) || ($signalId !== null && isset($this->signalIds[$signalId])) || ($commandId !== null && isset($this->commandIds[$commandId])); } /** - * Whether any received signal in the history is hidden from workflow code. + * @internal Whether this replay must record the marker it has not sent yet. * - * @param list> $history + * @phpstan-impure */ - public function hidesAny(array $history): bool + public function takeMarker(): bool { - foreach ($history as $order => $event) { - if (($event['event_type'] ?? $event['type'] ?? null) === 'SignalReceived' && !$this->observes($event, $order)) { - return true; - } + if (!$this->markerRequested || $this->markerFlushed) { + return false; + } + $this->markerFlushed = true; + + return true; + } + + private function appliedOnly(): bool + { + if ($this->markerRequested) { + return true; + } + if ($this->markerSequence === null) { + return false; } + [$sequence, $live] = $this->position(); + + return $live || $sequence >= $this->markerSequence; + } + + /** Record the marker at a live decision of a history without one. */ + private function requestMarker(): bool + { + if ($this->markerSequence !== null || !$this->position()[1]) { + return false; + } + $this->markerRequested = true; + + return true; + } - return false; + /** @return array{int, bool} */ + private function position(): array + { + [$sequence, $end] = $this->position === null ? [PHP_INT_MAX, false] : ($this->position)(); + + return [$sequence, $end || ($this->liveEvaluation && $this->position !== null)]; } /** - * Whether this view shows a received signal the other view hides. - * - * @param list> $history + * @param array $signalIds + * @param array $commandIds */ - public function showsMoreThan(self $other, array $history): bool + private static function remember(array &$signalIds, array &$commandIds, mixed $signalId, mixed $commandId): void { - foreach ($history as $order => $event) { - if (($event['event_type'] ?? $event['type'] ?? null) === 'SignalReceived' - && $this->observes($event, $order) && !$other->observes($event, $order)) { - return true; - } + if (($signalId = self::identity($signalId)) !== null) { + $signalIds[$signalId] = true; + } + if (($commandId = self::identity($commandId)) !== null) { + $commandIds[$commandId] = true; } - - return false; } private static function identity(mixed $value): ?string diff --git a/src/Worker/Replayer.php b/src/Worker/Replayer.php index 02b51a87..31316f5f 100644 --- a/src/Worker/Replayer.php +++ b/src/Worker/Replayer.php @@ -121,57 +121,28 @@ public function refuseNewGroupedConditionWaits(?Closure $refusal): void private ?BusinessReplayClock $lastBusinessReplayClock = null; - /** The signals the replay in progress shows workflow code (sdk-php#434). */ - private ?AppliedSignals $signalView = null; - - private bool $replayingSignalView = false; - /** - * sdk-php#434: workflow code observes a signal only once the Server has - * applied it (or this task delivers it). Histories recorded while the SDK - * showed every received signal stay replayable: when hiding a received but - * unapplied signal contradicts the recorded history, the signals an earlier - * task could see (those received before a later recorded decision) are - * shown again. Signals received after the last recorded decision stay - * hidden until applied. + * Send the applied-only signal marker once, the first time that view + * changed a live decision of this task (sdk-php#434). * - * @param list> $history - * @param array $task - * @param Closure(): ReplayResult $replay + * @param list> $commands + * @phpstan-impure */ - private function replayWithSignalCompatibility(array $history, array $task, Closure $replay): ReplayResult - { - $applied = AppliedSignals::fromHistory($history, $task); - $view = fn (AppliedSignals $signals): ReplayResult => $this->replayWithSignalView($signals, $replay); - if (!$applied->hidesAny($history)) { - return $view($applied); - } - try { - return $view($applied); - } catch (NonDeterministicWorkflow $appliedOnly) { - $recorded = $applied->withRecordedObservations($history); - if (!$recorded->showsMoreThan($applied, $history)) { - throw $appliedOnly; - } - try { - return $view($recorded); - } catch (NonDeterministicWorkflow) { - throw $appliedOnly; - } + private function recordAppliedSignalMarker( + AppliedSignals $appliedSignals, + array &$commands, + int &$nextSequence, + string $taskQueue, + ?WorkflowContext $context, + ): ?ReplayResult { + if (!$appliedSignals->takeMarker()) { + return null; } - } + $failure = $this->appendWorkflowCommand($commands, + WorkflowCommand::versionMarker(AppliedSignals::CHANGE_ID, -1, 1, 'patched'), $taskQueue, $context); + ++$nextSequence; - /** @param Closure(): ReplayResult $replay */ - private function replayWithSignalView(AppliedSignals $signals, Closure $replay): ReplayResult - { - $this->replayingSignalView = true; - $this->signalView = $signals; - try { - return $replay(); - } finally { - $this->replayingSignalView = false; - $this->signalView = null; - } + return $failure; } /** @internal Whether the most recent replay asked for business time it could not have. */ @@ -202,14 +173,6 @@ public function replay( bool $prepareCancellationScopeDelivery = false, bool $allowScopedPreparedLocalActivities = false, ): ReplayResult { - if (!$this->replayingSignalView) { - return $this->replayWithSignalCompatibility($history, $task, fn (): ReplayResult => $this->replay( - $handler, $history, $input, $taskQueue, $task, $localActivityExecutor, $prepareLocalActivities, - $prepareLocalActivityGroups, $localActivityCancellationPolicies, $allowCancellationScopeAuthoring, - $replayCommittedCancellationScopes, $prepareCancellationScopeDelivery, $allowScopedPreparedLocalActivities, - )); - } - $appliedSignals = $this->signalView; if ($prepareLocalActivityGroups && !$prepareLocalActivities) { throw new LogicException('Prepared local groups require prepared local activity admission.'); } @@ -259,6 +222,19 @@ public function replay( } $strictSequences = $cancellation->request !== null || $scopes->openings !== []; $steps = $this->recordedSteps($history, $strictSequences, $scopes); + // sdk-php#434: the applied-only signal marker is transparent to the + // authored command sequence; only its position selects the view. + $appliedSignals = AppliedSignals::fromHistory($history, $task); + $signalMarkerSequences = []; + $steps = array_values(array_filter($steps, static function (array $step) use (&$signalMarkerSequences): bool { + if ($step['shape'] === 'version_marker' && $step['detail'] === AppliedSignals::CHANGE_ID) { + $signalMarkerSequences[$step['sequence']] = true; + + return false; + } + + return true; + })); $stepsBySequence = []; foreach ($steps as $step) { $stepsBySequence[$step['sequence']] = $step; @@ -323,15 +299,24 @@ public function replay( $stepCursor = 0; $commands = []; + $nextSequence = 1; + $appliedSignals->bind(static function () use (&$nextSequence, &$stepCursor, &$steps, $completedHistory): array { + return [$nextSequence, $stepCursor >= count($steps) && !$completedHistory]; + }); /** @var array $versionDecisions */ $versionDecisions = []; $suspended = $businessReplayClock->invoke($execution->start(...)); - $nextSequence = 1; $cancellationConsumed = false; $consumedScopeDeliveries = []; $consumedConditionTail = null; while (!$execution->isTerminated()) { + while (isset($signalMarkerSequences[$nextSequence])) { + ++$nextSequence; + } + if (($encodingFailure = $this->recordAppliedSignalMarker($appliedSignals, $commands, $nextSequence, $taskQueue, $context)) !== null) { + return $encodingFailure; + } if ($consumedConditionTail !== null) { $remaining = $this->restoreConsumedConditionTail($suspended, $consumedConditionTail); if ($remaining !== []) { @@ -339,8 +324,12 @@ public function replay( } $consumedConditionTail = null; } - if ($suspended instanceof WorkflowCommand && !isset($cancelledConditionSequences[$nextSequence]) - && ($conditionResult = $this->unrecordedConditionResult($suspended, $steps[$stepCursor] ?? null)) !== null) { + $conditionResult = $suspended instanceof WorkflowCommand && !isset($cancelledConditionSequences[$nextSequence]) + ? $this->unrecordedConditionResult($suspended, $steps[$stepCursor] ?? null) : null; + if (($encodingFailure = $this->recordAppliedSignalMarker($appliedSignals, $commands, $nextSequence, $taskQueue, $context)) !== null) { + return $encodingFailure; + } + if ($conditionResult !== null) { $suspended = $this->resumeExecution($execution, $businessReplayClock, $conditionResult); continue; } @@ -927,7 +916,11 @@ public function replay( $suspended = $this->resumeExecution($execution, $businessReplayClock, $step['value'], $step); continue; } - if ($suspended->conditionSatisfied()) { + $satisfied = $appliedSignals->live(static fn (): bool => $suspended->conditionSatisfied()); + if (($encodingFailure = $this->recordAppliedSignalMarker($appliedSignals, $commands, $nextSequence, $taskQueue, $context)) !== null) { + return $encodingFailure; + } + if ($satisfied) { $consumedConditionTail = $this->conditionTailAfterConsumption($step, $suspended, true); $suspended = $this->resumeExecution($execution, $businessReplayClock, true); continue; @@ -1050,6 +1043,9 @@ public function replay( } } $this->assertNoRemainingSteps($steps, $stepCursor, 'complete_workflow'); + if (($encodingFailure = $this->recordAppliedSignalMarker($appliedSignals, $commands, $nextSequence, $taskQueue, $context)) !== null) { + return $encodingFailure; + } $result = $execution->getReturn(); if ($result instanceof \Generator) { throw new LogicException( diff --git a/src/Worker/WorkflowContext.php b/src/Worker/WorkflowContext.php index bffbec38..f6ca93cc 100644 --- a/src/Worker/WorkflowContext.php +++ b/src/Worker/WorkflowContext.php @@ -892,7 +892,10 @@ public function observeCancellationReplayTime(?array $event): void * the Server applies signals in message order with updates, when the * workflow task that delivers the signal completes, and can still reject * one it has not applied (sdk-php#434). The signal the current workflow - * task delivers is included. + * task delivers is included. Runs recorded before this rule keep their + * earlier view: the first live call that hides a received signal records + * the version marker `durable-workflow.sdk-php.signals-applied-only` + * (see {@see AppliedSignals}). * * @return list> */ @@ -904,14 +907,20 @@ public function signals(string $signalName): array /** @return list> */ private function receivedSignals(string $signalName, ?AppliedSignals $applied): array { - $signals = []; + $received = []; foreach ($this->history as $order => $event) { $payload = isset($event['payload']) && is_array($event['payload']) ? $event['payload'] : []; - if (($event['event_type'] ?? $event['type'] ?? null) !== 'SignalReceived' - || ($payload['signal_name'] ?? null) !== $signalName - || ($applied !== null && !$applied->observes($event, $order))) { - continue; + if (($event['event_type'] ?? $event['type'] ?? null) === 'SignalReceived' + && ($payload['signal_name'] ?? null) === $signalName) { + $received[$order] = $payload; } + } + if ($applied !== null) { + $received = array_intersect_key($received, $applied->select(array_map( + static fn (array $payload): array => ['event_type' => 'SignalReceived', 'payload' => $payload], $received))); + } + $signals = []; + foreach ($received as $payload) { $raw = $payload['value'] ?? $payload['input'] ?? $payload['arguments'] ?? null; $decoded = (is_array($raw) || is_string($raw)) ? $this->codec->decodeEnvelope($raw) : null; $signals[] = is_array($decoded) && array_is_list($decoded) ? $decoded : [$decoded]; diff --git a/tests/AppliedSignalsTest.php b/tests/AppliedSignalsTest.php index 942580de..8b13c6bb 100644 --- a/tests/AppliedSignalsTest.php +++ b/tests/AppliedSignalsTest.php @@ -8,13 +8,15 @@ use DurableWorkflow\Exception\NonDeterministicWorkflow; use DurableWorkflow\Worker\AppliedSignals; use DurableWorkflow\Worker\Replayer; +use DurableWorkflow\Worker\ReplayResult; use DurableWorkflow\Worker\WorkflowContext; use PHPUnit\Framework\TestCase; /** * sdk-php#434: workflow code observes a signal only once the Server has applied - * it, or when the current workflow task delivers it, so a received signal the - * Server later rejects never changes a workflow decision. + * it, or when the current workflow task delivers it. The applied-only view is a + * versioned change: the first live decision it changes records a version + * marker, and histories without the marker keep the earlier view. */ final class AppliedSignalsTest extends TestCase { @@ -36,27 +38,32 @@ public function testAQueuedUpdateIsAppliedBeforeAReceivedSignalClosesTheRun(): v $this->received('signal-release', 'command-release', 'release'), ]; - $updateTask = $this->replay($history, ['workflow_update_id' => 'update-1', 'update_name' => 'change']); + $updateTask = $this->replayCondition($history, ['workflow_update_id' => 'update-1', 'update_name' => 'change']); + self::assertSame('record_version_marker', $updateTask->commands[0]['type']); + self::assertSame(AppliedSignals::CHANGE_ID, $updateTask->commands[0]['change_id']); + self::assertSame(1, $updateTask->commands[0]['version']); self::assertNotContains('complete_workflow', array_column($updateTask->commands, 'type')); - // The signal's own task delivers it: now the condition is satisfied. - $signalTask = $this->replay($history, ['resume_source_kind' => 'workflow_signal', + // The update task recorded the marker; the signal's own task delivers it. + $history[] = $this->marker(2); + $signalTask = $this->replayCondition($history, ['resume_source_kind' => 'workflow_signal', 'workflow_signal_id' => 'signal-release', 'workflow_command_id' => 'command-release', 'signal_name' => 'release']); - self::assertSame('complete_workflow', $signalTask->commands[0]['type']); + self::assertSame(['complete_workflow'], array_column($signalTask->commands, 'type')); self::assertSame(['released' => [['go']]], $this->codec->decodeEnvelope($signalTask->commands[0]['result'])); } - public function testARejectedSignalIsNeverObserved(): void + public function testARejectedSignalIsNeverObservedByANewDecision(): void { $history = [ $this->conditionOpened(), + $this->marker(2), $this->received('signal-release', 'command-release', 'release'), ['event_type' => 'SignalRejected', 'payload' => ['signal_id' => 'signal-release', 'workflow_command_id' => 'command-release', 'signal_name' => 'release', 'rejection_reason' => 'run_closed_before_signal_applied']], ]; - self::assertNotContains('complete_workflow', array_column($this->replay($history)->commands, 'type')); + self::assertNotContains('complete_workflow', array_column($this->replayCondition($history)->commands, 'type')); } public function testAppliedSignalsAreObservedInReceiptOrder(): void @@ -79,84 +86,123 @@ public function testAppliedSignalsAreObservedInReceiptOrder(): void // Applied by event (by signal or command identity), consumed with a // sequence, and an identity-less imported record; never the unapplied one. + self::assertSame(['record_version_marker', 'complete_workflow'], array_column($result->commands, 'type')); self::assertSame([['first'], ['second'], ['third'], ['imported']], - $this->codec->decodeEnvelope($result->commands[0]['result'])); + $this->codec->decodeEnvelope($result->commands[1]['result'])); } - public function testALegacyHistoryKeepsTheSignalsItsTasksObservedButHidesLaterOnes(): void + public function testAHistoryThatNeverReadsSignalsReplaysUnchanged(): void { - // Recorded by an SDK that showed every received signal: the activity was - // chosen while signal-observed was received but not yet applied. + $history = [ + $this->received('signal-pending', 'command-pending', 'approve'), + ['event_type' => 'ActivityScheduled', 'payload' => ['sequence' => 1, 'activity_type' => 'work']], + ['event_type' => 'ActivityCompleted', 'payload' => ['sequence' => 1, 'activity_type' => 'work', + 'result' => $this->codec->envelope('done')]], + $this->received('signal-later', 'command-later', 'approve'), + ]; + $workflow = static fn (WorkflowContext $context): mixed => $context->activity('work'); + + $result = (new Replayer($this->codec))->replay($workflow, $history, [], 'php-workers'); + + self::assertCount(1, $result->commands); + self::assertSame('complete_workflow', $result->commands[0]['type']); + self::assertSame('done', $this->codec->decodeEnvelope($result->commands[0]['result'])); + } + + public function testReadingSignalsThatAreAllAppliedRecordsNoMarker(): void + { + $history = [ + $this->received('signal-1', 'command-1', 'approve', 'first', workflowSequence: 1), + ['event_type' => 'SignalReceived', 'payload' => ['signal_name' => 'approve', + 'arguments' => $this->codec->envelope(['imported'])]], + ]; + $workflow = static fn (WorkflowContext $context): array => $context->signals('approve'); + + $result = (new Replayer($this->codec))->replay($workflow, $history, [], 'php-workers'); + + self::assertSame(['complete_workflow'], array_column($result->commands, 'type')); + self::assertSame([['first'], ['imported']], $this->codec->decodeEnvelope($result->commands[0]['result'])); + } + + public function testAHistoryWithoutTheMarkerKeepsTheEarlierViewForRecordedDecisions(): void + { + // Recorded by an earlier release: the activity was chosen while + // signal-observed was received but not yet applied. $history = [ $this->received('signal-observed', 'command-observed', 'approve', 'first'), ['event_type' => 'ActivityScheduled', 'payload' => ['sequence' => 1, 'activity_type' => 'approved']], ['event_type' => 'ActivityCompleted', 'payload' => ['sequence' => 1, 'activity_type' => 'approved', 'result' => $this->codec->envelope('ok')]], - $this->received('signal-later', 'command-later', 'approve', 'later'), ]; - $workflow = static function (WorkflowContext $context): array { - $result = $context->activity($context->signals('approve') !== [] ? 'approved' : 'waiting'); - return ['result' => $result, 'signals' => $context->signals('approve')]; - }; + $result = (new Replayer($this->codec))->replay(self::decision(), $history, [], 'php-workers'); - $result = (new Replayer($this->codec))->replay($workflow, $history, [], 'php-workers'); + // The recorded decision replays unchanged; the first live decision the + // applied-only view changes records the marker before it. + self::assertSame(['record_version_marker', 'schedule_activity'], array_column($result->commands, 'type')); + self::assertSame('second-waiting', $result->commands[1]['activity_type']); + } - self::assertSame(['result' => 'ok', 'signals' => [['first']]], - $this->codec->decodeEnvelope($result->commands[0]['result'])); + public function testTheMarkerSelectsTheAppliedViewFromItsPositionOn(): void + { + $history = [ + $this->received('signal-observed', 'command-observed', 'approve', 'first'), + ['event_type' => 'ActivityScheduled', 'payload' => ['sequence' => 1, 'activity_type' => 'approved']], + ['event_type' => 'ActivityCompleted', 'payload' => ['sequence' => 1, 'activity_type' => 'approved', + 'result' => $this->codec->envelope('ok')]], + $this->marker(2), + ['event_type' => 'ActivityScheduled', 'payload' => ['sequence' => 3, 'activity_type' => 'second-waiting']], + ['event_type' => 'ActivityCompleted', 'payload' => ['sequence' => 3, 'activity_type' => 'second-waiting', + 'result' => $this->codec->envelope('later')]], + ]; + + $result = (new Replayer($this->codec))->replay(self::decision(), $history, [], 'php-workers'); + + self::assertSame(['complete_workflow'], array_column($result->commands, 'type')); + self::assertSame(['ok', 'later', []], $this->codec->decodeEnvelope($result->commands[0]['result'])); } - public function testAGenuinelyChangedWorkflowStillReportsTheAppliedOnlyDivergence(): void + public function testAChangedWorkflowIsReportedWithoutTryingTheEarlierView(): void { + // The marker selects the applied view, so the recorded "approved" choice + // cannot be explained by the unapplied signal: it is nondeterministic. $history = [ + $this->marker(1), $this->received('signal-observed', 'command-observed', 'approve'), - ['event_type' => 'ActivityScheduled', 'payload' => ['sequence' => 1, 'activity_type' => 'removed']], + ['event_type' => 'ActivityScheduled', 'payload' => ['sequence' => 2, 'activity_type' => 'approved']], ]; - $workflow = static function (WorkflowContext $context): mixed { - return $context->activity($context->signals('approve') !== [] ? 'approved' : 'waiting'); - }; try { - (new Replayer($this->codec))->replay($workflow, $history, [], 'php-workers'); + (new Replayer($this->codec))->replay(self::decision(), $history, [], 'php-workers'); self::fail('A changed workflow must remain nondeterministic.'); } catch (NonDeterministicWorkflow $error) { - self::assertSame(1, $error->sequence); + self::assertSame(2, $error->sequence); self::assertStringContainsString('waiting', $error->getMessage()); } } - public function testFreshDecisionsWithoutContradictionUseOnlyAppliedSignals(): void + public function testACompletedHistoryRecordsNoMarker(): void { $history = [ - $this->received('signal-pending', 'command-pending', 'approve'), + $this->received('signal-observed', 'command-observed', 'approve', 'first'), + ['event_type' => 'WorkflowCompleted', 'payload' => []], ]; - $workflow = static fn (WorkflowContext $context): mixed - => $context->activity($context->signals('approve') !== [] ? 'approved' : 'waiting'); + $workflow = static fn (WorkflowContext $context): array => $context->signals('approve'); $result = (new Replayer($this->codec))->replay($workflow, $history, [], 'php-workers'); - self::assertSame('schedule_activity', $result->commands[0]['type']); - self::assertSame('waiting', $result->commands[0]['activity_type']); + self::assertSame(['complete_workflow'], array_column($result->commands, 'type')); + self::assertSame([['first']], $this->codec->decodeEnvelope($result->commands[0]['result'])); } - public function testRecordedObservationsCoverOnlySignalsBeforeTheLastDecision(): void + private static function decision(): \Closure { - $history = [ - $this->received('signal-1', 'command-1', 'approve'), - ['event_type' => 'TimerScheduled', 'payload' => ['sequence' => 1]], - $this->received('signal-2', 'command-2', 'approve'), - ['event_type' => 'TimerFired', 'payload' => ['sequence' => 1]], - $this->received('signal-3', 'command-3', 'approve'), - ]; - $applied = AppliedSignals::fromHistory($history, []); - $recorded = $applied->withRecordedObservations($history); - - self::assertFalse($applied->observes($history[0], 0)); - self::assertTrue($recorded->observes($history[0], 0)); - self::assertFalse($recorded->observes($history[2], 2)); - self::assertFalse($recorded->observes($history[4], 4)); - self::assertTrue($recorded->showsMoreThan($applied, $history)); - self::assertTrue($recorded->hidesAny($history)); + return static function (WorkflowContext $context): array { + $first = $context->activity($context->signals('approve') !== [] ? 'approved' : 'waiting'); + $second = $context->activity($context->signals('approve') !== [] ? 'second-approved' : 'second-waiting'); + + return [$first, $second, $context->signals('approve')]; + }; } /** @return array */ @@ -166,6 +212,13 @@ private function conditionOpened(): array 'condition_wait_id' => 'wait-release', 'condition_key' => 'release', 'timeout_seconds' => 300]]; } + /** @return array */ + private function marker(int $sequence): array + { + return ['event_type' => 'VersionMarkerRecorded', 'payload' => ['sequence' => $sequence, + 'change_id' => AppliedSignals::CHANGE_ID, 'version' => 1, 'min_supported' => -1, 'max_supported' => 1]]; + } + /** @return array */ private function received(string $signalId, string $commandId, string $name, string $value = 'go', ?int $workflowSequence = null): array { @@ -182,7 +235,7 @@ private function received(string $signalId, string $commandId, string $name, str * @param list> $history * @param array $task */ - private function replay(array $history, array $task = []): \DurableWorkflow\Worker\ReplayResult + private function replayCondition(array $history, array $task = []): ReplayResult { $workflow = static function (WorkflowContext $context): array { $context->waitCondition(static fn (): bool => $context->signals('release') !== [], key: 'release', timeout: 300); diff --git a/tests/fixtures/replay-regressions/signals-applied-only-marker-recorded.json b/tests/fixtures/replay-regressions/signals-applied-only-marker-recorded.json new file mode 100644 index 00000000..161bba27 --- /dev/null +++ b/tests/fixtures/replay-regressions/signals-applied-only-marker-recorded.json @@ -0,0 +1,87 @@ +{ + "$schema": "https://raw.githubusercontent.com/durable-workflow/.github/main/regression-corpus/evidence-schema.json", + "fixture_schema": "durable-workflow.replay-regression/v1", + "id": "php-signals-applied-only-marker-recorded", + "protocol_version": "1.20", + "bindings": [ + "php" + ], + "workflow": { + "type": "golden.signal-decision", + "input": [] + }, + "history": [ + { + "event_type": "SignalReceived", + "payload": { + "signal_id": "signal-pending", + "workflow_command_id": "command-pending", + "signal_name": "approve", + "arguments": { + "codec": "avro", + "blob": "wwHioz3/VYAiNwwCCgpmaXJzdAA=" + } + } + }, + { + "event_type": "VersionMarkerRecorded", + "payload": { + "sequence": 1, + "change_id": "durable-workflow.sdk-php.signals-applied-only", + "version": 1, + "min_supported": -1, + "max_supported": 1 + } + }, + { + "event_type": "ActivityScheduled", + "payload": { + "sequence": 2, + "activity_type": "golden.waiting" + } + }, + { + "event_type": "ActivityCompleted", + "payload": { + "sequence": 2, + "activity_type": "golden.waiting", + "result": { + "codec": "avro", + "blob": "wwHioz3/VYAiNwocd2FpdGluZy1yZXN1bHQ=" + } + } + }, + { + "event_type": "SignalReceived", + "payload": { + "signal_id": "signal-later", + "workflow_command_id": "command-later", + "signal_name": "approve", + "arguments": { + "codec": "avro", + "blob": "wwHioz3/VYAiNwwCCgxzZWNvbmQA" + } + } + } + ], + "command_sequence": [ + { + "type": "complete_workflow", + "result": { + "result": "waiting-result", + "signals": [] + } + } + ], + "expected": { + "command_sequence": [ + { + "type": "complete_workflow", + "result": { + "result": "waiting-result", + "signals": [] + } + } + ] + } +} diff --git a/tests/fixtures/replay-regressions/signals-observe-only-applied.json b/tests/fixtures/replay-regressions/signals-observe-only-applied.json index 8988ff06..193c2f03 100644 --- a/tests/fixtures/replay-regressions/signals-observe-only-applied.json +++ b/tests/fixtures/replay-regressions/signals-observe-only-applied.json @@ -3,7 +3,9 @@ "fixture_schema": "durable-workflow.replay-regression/v1", "id": "php-signals-observe-only-applied", "protocol_version": "1.20", - "bindings": ["php"], + "bindings": [ + "php" + ], "workflow": { "type": "golden.signal-applied", "input": [] @@ -22,7 +24,10 @@ "signal_id": "signal-first", "workflow_command_id": "command-first", "signal_name": "approve", - "arguments": {"codec": "avro", "blob": "wwHioz3/VYAiNwwCCgpmaXJzdAA="} + "arguments": { + "codec": "avro", + "blob": "wwHioz3/VYAiNwwCCgpmaXJzdAA=" + } } }, { @@ -31,7 +36,10 @@ "signal_id": "signal-first", "workflow_command_id": "command-first", "signal_name": "approve", - "value": {"codec": "avro", "blob": "wwHioz3/VYAiNwwCCgpmaXJzdAA="} + "value": { + "codec": "avro", + "blob": "wwHioz3/VYAiNwwCCgpmaXJzdAA=" + } } }, { @@ -40,7 +48,10 @@ "signal_id": "signal-current", "workflow_command_id": "command-current", "signal_name": "approve", - "arguments": {"codec": "avro", "blob": "wwHioz3/VYAiNwwCCg5jdXJyZW50AA=="} + "arguments": { + "codec": "avro", + "blob": "wwHioz3/VYAiNwwCCg5jdXJyZW50AA==" + } } }, { @@ -49,21 +60,56 @@ "signal_id": "signal-unapplied", "workflow_command_id": "command-unapplied", "signal_name": "approve", - "arguments": {"codec": "avro", "blob": "wwHioz3/VYAiNwwCCgxzZWNvbmQA"} + "arguments": { + "codec": "avro", + "blob": "wwHioz3/VYAiNwwCCgxzZWNvbmQA" + } } } ], "command_sequence": [ + { + "type": "record_version_marker", + "change_id": "durable-workflow.sdk-php.signals-applied-only", + "version": 1, + "min_supported": -1, + "max_supported": 1 + }, { "type": "complete_workflow", - "result": {"signals": [["first"], ["current"]]} + "result": { + "signals": [ + [ + "first" + ], + [ + "current" + ] + ] + } } ], "expected": { "command_sequence": [ + { + "type": "record_version_marker", + "change_id": "durable-workflow.sdk-php.signals-applied-only", + "version": 1, + "min_supported": -1, + "max_supported": 1 + }, { "type": "complete_workflow", - "result": {"signals": [["first"], ["current"]]} + "result": { + "signals": [ + [ + "first" + ], + [ + "current" + ] + ] + } } ] } diff --git a/tests/fixtures/replay-regressions/signals-unapplied-legacy-history.json b/tests/fixtures/replay-regressions/signals-unapplied-legacy-history.json index a4224b5a..ddb85a88 100644 --- a/tests/fixtures/replay-regressions/signals-unapplied-legacy-history.json +++ b/tests/fixtures/replay-regressions/signals-unapplied-legacy-history.json @@ -3,7 +3,9 @@ "fixture_schema": "durable-workflow.replay-regression/v1", "id": "php-signals-unapplied-legacy-history", "protocol_version": "1.20", - "bindings": ["php"], + "bindings": [ + "php" + ], "workflow": { "type": "golden.signal-decision", "input": [] @@ -15,7 +17,10 @@ "signal_id": "signal-observed", "workflow_command_id": "command-observed", "signal_name": "approve", - "arguments": {"codec": "avro", "blob": "wwHioz3/VYAiNwwCCgpmaXJzdAA="} + "arguments": { + "codec": "avro", + "blob": "wwHioz3/VYAiNwwCCgpmaXJzdAA=" + } } }, { @@ -30,7 +35,10 @@ "payload": { "sequence": 1, "activity_type": "golden.approved", - "result": {"codec": "avro", "blob": "wwHioz3/VYAiNwoeYXBwcm92ZWQtcmVzdWx0"} + "result": { + "codec": "avro", + "blob": "wwHioz3/VYAiNwoeYXBwcm92ZWQtcmVzdWx0" + } } }, { @@ -39,21 +47,44 @@ "signal_id": "signal-later", "workflow_command_id": "command-later", "signal_name": "approve", - "arguments": {"codec": "avro", "blob": "wwHioz3/VYAiNwwCCgxzZWNvbmQA"} + "arguments": { + "codec": "avro", + "blob": "wwHioz3/VYAiNwwCCgxzZWNvbmQA" + } } } ], "command_sequence": [ + { + "type": "record_version_marker", + "change_id": "durable-workflow.sdk-php.signals-applied-only", + "version": 1, + "min_supported": -1, + "max_supported": 1 + }, { "type": "complete_workflow", - "result": {"result": "approved-result", "signals": [["first"]]} + "result": { + "result": "approved-result", + "signals": [] + } } ], "expected": { "command_sequence": [ + { + "type": "record_version_marker", + "change_id": "durable-workflow.sdk-php.signals-applied-only", + "version": 1, + "min_supported": -1, + "max_supported": 1 + }, { "type": "complete_workflow", - "result": {"result": "approved-result", "signals": [["first"]]} + "result": { + "result": "approved-result", + "signals": [] + } } ] }