Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from aws_durable_execution_sdk_python_testing.model import (
LambdaContext,
executed_version,
project_operation_dict,
)


Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"]