From f7411b692461579d5e99b72c074b01b0d24fd348 Mon Sep 17 00:00:00 2001 From: Ym <173550318+MaxonT@users.noreply.github.com> Date: Fri, 27 Nov 2026 19:47:24 -0600 Subject: [PATCH] fix(testing): match ReplayChildren lifecycle and wire form of the local service Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com> --- .../checkpoint/processors/base.py | 15 +- .../invoker.py | 13 +- .../model.py | 31 ++++- .../tests/replay_children_wire_test.py | 129 ++++++++++++++++++ 4 files changed, 181 insertions(+), 7 deletions(-) create mode 100644 packages/aws-durable-execution-sdk-python-testing/tests/replay_children_wire_test.py diff --git a/packages/aws-durable-execution-sdk-python-testing/src/aws_durable_execution_sdk_python_testing/checkpoint/processors/base.py b/packages/aws-durable-execution-sdk-python-testing/src/aws_durable_execution_sdk_python_testing/checkpoint/processors/base.py index d2eaa4ec4..4fe5251ff 100644 --- a/packages/aws-durable-execution-sdk-python-testing/src/aws_durable_execution_sdk_python_testing/checkpoint/processors/base.py +++ b/packages/aws-durable-execution-sdk-python-testing/src/aws_durable_execution_sdk_python_testing/checkpoint/processors/base.py @@ -95,14 +95,21 @@ def _create_execution_details( ) def _create_context_details(self, update: OperationUpdate) -> ContextDetails | None: - """Create ContextDetails from OperationUpdate.""" + """Create ContextDetails from OperationUpdate. + + ReplayChildren is the closing choice: it is recorded as true only when a + SUCCEED or FAIL update explicitly asks for it. START never retains it. + """ + closing = update.action in (OperationAction.SUCCEED, OperationAction.FAIL) return ( ContextDetails( result=update.payload, error=update.error, - replay_children=update.context_options.replay_children - if update.context_options - else False, + replay_children=bool( + closing + and update.context_options + and update.context_options.replay_children is True + ), ) if update.operation_type == OperationType.CONTEXT else None diff --git a/packages/aws-durable-execution-sdk-python-testing/src/aws_durable_execution_sdk_python_testing/invoker.py b/packages/aws-durable-execution-sdk-python-testing/src/aws_durable_execution_sdk_python_testing/invoker.py index 4992212f4..75a969509 100644 --- a/packages/aws-durable-execution-sdk-python-testing/src/aws_durable_execution_sdk_python_testing/invoker.py +++ b/packages/aws-durable-execution-sdk-python-testing/src/aws_durable_execution_sdk_python_testing/invoker.py @@ -30,6 +30,7 @@ from aws_durable_execution_sdk_python_testing.model import ( LambdaContext, executed_version, + project_operation_dict, ) @@ -40,6 +41,16 @@ from aws_durable_execution_sdk_python_testing.execution import Execution +def _invocation_input_payload(input: DurableExecutionInvocationInput) -> dict[str, Any]: + """Serialize the invocation input with the ReplayChildren projection applied.""" + payload: dict[str, Any] = dict(input.to_json_dict()) + state = input.initial_execution_state + operations = payload["InitialExecutionState"]["Operations"] + for operation, data in zip(state.operations, operations, strict=True): + project_operation_dict(operation, data) + return payload + + # Every Invoke the runner sends is one Lambda invocation: a handler # invocation, or a chained invoke of a non-durable target. The client # waits for it to return, so the read timeout exceeds the emulated @@ -514,7 +525,7 @@ def invoke( invoke_kwargs: dict[str, Any] = { "FunctionName": function_name, "InvocationType": "RequestResponse", # Synchronous invocation - "Payload": json.dumps(input.to_json_dict()), + "Payload": json.dumps(_invocation_input_payload(input)), } if tenant_id is not None: invoke_kwargs["TenantId"] = tenant_id diff --git a/packages/aws-durable-execution-sdk-python-testing/src/aws_durable_execution_sdk_python_testing/model.py b/packages/aws-durable-execution-sdk-python-testing/src/aws_durable_execution_sdk_python_testing/model.py index 052399a1c..ee836b58f 100644 --- a/packages/aws-durable-execution-sdk-python-testing/src/aws_durable_execution_sdk_python_testing/model.py +++ b/packages/aws-durable-execution-sdk-python-testing/src/aws_durable_execution_sdk_python_testing/model.py @@ -6,6 +6,7 @@ from aws_durable_execution_sdk_python_testing.clock import real_now import json +from collections.abc import MutableMapping from dataclasses import dataclass, replace from enum import Enum from typing import Any @@ -548,6 +549,28 @@ def to_dict(self) -> dict[str, Any]: return result +def project_operation_dict( + operation: Operation, data: MutableMapping[str, Any] +) -> MutableMapping[str, Any]: + """Apply the local service's ReplayChildren wire projection to one operation. + + Core serialization omits a false ``ReplayChildren``. The service omits the + field from a STARTED context and sends an explicit boolean once the context + is completed. ``data`` is the serialized form of ``operation``; the + operation itself is not changed. + """ + if operation.operation_type is not OperationType.CONTEXT: + return data + context = data.get("ContextDetails") + if context is None or operation.context_details is None: + return data + if operation.status is OperationStatus.STARTED: + context.pop("ReplayChildren", None) + else: + context["ReplayChildren"] = operation.context_details.replay_children is True + return data + + @dataclass(frozen=True) class GetDurableExecutionStateResponse: """Response containing durable execution state operations.""" @@ -567,7 +590,9 @@ def from_dict(cls, data: dict) -> GetDurableExecutionStateResponse: def to_dict(self) -> dict[str, Any]: result: dict[str, Any] = { - "Operations": [op.to_dict() for op in self.operations] + "Operations": [ + project_operation_dict(op, op.to_dict()) for op in self.operations + ] } if self.next_marker is not None: result["NextMarker"] = self.next_marker @@ -3146,7 +3171,9 @@ def from_dict(cls, data: dict) -> CheckpointUpdatedExecutionState: def to_dict(self) -> dict[str, Any]: result: dict[str, Any] = { - "Operations": [op.to_dict() for op in self.operations] + "Operations": [ + project_operation_dict(op, op.to_dict()) for op in self.operations + ] } if self.next_marker is not None: result["NextMarker"] = self.next_marker diff --git a/packages/aws-durable-execution-sdk-python-testing/tests/replay_children_wire_test.py b/packages/aws-durable-execution-sdk-python-testing/tests/replay_children_wire_test.py new file mode 100644 index 000000000..c00f3d933 --- /dev/null +++ b/packages/aws-durable-execution-sdk-python-testing/tests/replay_children_wire_test.py @@ -0,0 +1,129 @@ +"""ReplayChildren lifecycle and wire representation of the local service.""" + +import json +from datetime import UTC, datetime + +import pytest +from aws_durable_execution_sdk_python.execution import ( + DurableExecutionInvocationInput, + InitialExecutionState, +) +from aws_durable_execution_sdk_python.lambda_service import ( + ContextOptions, + Operation, + OperationAction, + OperationType, + OperationUpdate, +) + +from aws_durable_execution_sdk_python_testing.checkpoint.processors.context import ( + ContextProcessor, +) +from aws_durable_execution_sdk_python_testing.invoker import ( + _invocation_input_payload, +) +from aws_durable_execution_sdk_python_testing.model import ( + CheckpointUpdatedExecutionState, + GetDurableExecutionStateResponse, +) +from aws_durable_execution_sdk_python_testing.observer import ExecutionNotifier + +NOW = datetime(2026, 1, 1, tzinfo=UTC) + + +def _process(action, options): + update = OperationUpdate( + operation_id="ctx", + operation_type=OperationType.CONTEXT, + action=action, + payload="p", + context_options=options, + ) + return ContextProcessor().process(update, None, ExecutionNotifier(), "arn", NOW) + + +def _context_details(wire): + return wire["Operations"][0]["ContextDetails"] + + +@pytest.mark.parametrize("action", [OperationAction.SUCCEED, OperationAction.FAIL]) +@pytest.mark.parametrize( + ("options", "expected"), + [ + (None, False), + (ContextOptions(), False), + (ContextOptions(replay_children=False), False), + (ContextOptions(replay_children=True), True), + ], +) +def test_replay_children_is_recorded_only_by_closing_update(action, options, expected): + operation = _process(action, options) + + assert operation.context_details.replay_children is expected + + +def test_start_does_not_retain_replay_children(): + operation = _process(OperationAction.START, ContextOptions(replay_children=True)) + + assert operation.context_details.replay_children is False + + +@pytest.mark.parametrize("retained", [True, False]) +def test_state_and_checkpoint_responses_send_explicit_boolean_when_completed(retained): + operation = _process( + OperationAction.SUCCEED, ContextOptions(replay_children=retained) + ) + + for response in ( + GetDurableExecutionStateResponse(operations=[operation]), + CheckpointUpdatedExecutionState(operations=[operation]), + ): + assert _context_details(response.to_dict())["ReplayChildren"] is retained + + +def test_started_context_omits_replay_children_on_every_path(): + started = _process(OperationAction.START, ContextOptions(replay_children=True)) + + state = GetDurableExecutionStateResponse(operations=[started]).to_dict() + checkpoint = CheckpointUpdatedExecutionState(operations=[started]).to_dict() + invocation = _invocation_input_payload( + DurableExecutionInvocationInput( + durable_execution_arn="arn", + checkpoint_token="t", + initial_execution_state=InitialExecutionState( + operations=[started], next_marker="" + ), + ) + ) + + assert "ReplayChildren" not in _context_details(state) + assert "ReplayChildren" not in _context_details(checkpoint) + assert ( + "ReplayChildren" + not in invocation["InitialExecutionState"]["Operations"][0]["ContextDetails"] + ) + + +def test_invocation_payload_has_false_for_completed_context_and_keeps_others(): + closed = _process(OperationAction.SUCCEED, None) + step = Operation.from_dict( + {"Id": "s", "Type": "STEP", "Status": "STARTED", "StepDetails": {"Attempt": 1}} + ) + original = closed.to_dict() + + payload = _invocation_input_payload( + DurableExecutionInvocationInput( + durable_execution_arn="arn", + checkpoint_token="t", + initial_execution_state=InitialExecutionState( + operations=[closed, step], next_marker="" + ), + ) + ) + operations = json.loads(json.dumps(payload))["InitialExecutionState"]["Operations"] + + assert operations[0]["ContextDetails"]["ReplayChildren"] is False + assert operations[1] == json.loads(json.dumps(step.to_json_dict())) + # The supplied operation is not mutated by the projection. + assert closed.to_dict() == original + assert "ReplayChildren" not in closed.to_dict()["ContextDetails"]