diff --git a/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/context.py b/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/context.py index e1957932e..223a460cb 100644 --- a/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/context.py +++ b/packages/aws-durable-execution-sdk-python/src/aws_durable_execution_sdk_python/context.py @@ -609,7 +609,20 @@ def _replay_aware( # - brand-new next op (no checkpoint): always flip to NEW. # - non-terminal STEP-type op (brand-new or retrying): the user function # is about to run real work, so flip to NEW before it. - if was_replaying and (not next_exists or (next_is_step and not next_terminal)): + # A virtual child never checkpoints itself, so its own checkpoint being + # absent says nothing: it is new only when its first inner operation + # has no history either. + brand_new: bool = not next_exists and not ( + is_virtual + and operation_identifier is not None + and self.state.get_checkpoint_result( + OperationIdNamespace( + operation_identifier.operation_id + ).create_id_for_step(1), + claim_end_hook=False, + ).is_existent() + ) + if was_replaying and (brand_new or (next_is_step and not next_terminal)): self._set_replay_status_new() try: yield diff --git a/packages/aws-durable-execution-sdk-python/tests/context_test.py b/packages/aws-durable-execution-sdk-python/tests/context_test.py index beb9df4ad..b11bd749b 100644 --- a/packages/aws-durable-execution-sdk-python/tests/context_test.py +++ b/packages/aws-durable-execution-sdk-python/tests/context_test.py @@ -3303,6 +3303,48 @@ def on_operation_end(self, info): assert child_body_calls == [] +def test_virtual_child_keeps_replay_status_while_inner_history_exists(): + """A virtual child has no checkpoint of its own; its inner history keeps REPLAY.""" + ctx = DurableContext( + state=_replay_state({}), + execution_context=ExecutionContext(durable_execution_arn="arn"), + replay_status=ReplayStatus.REPLAY, + ) + container_id = ctx._peek_next_operation_id() # noqa: SLF001 + probe = ctx.create_child_context(container_id, is_virtual=True) + inner_id = probe._create_step_id() # noqa: SLF001 + ctx.state._operations[inner_id] = _wait_op( # noqa: SLF001 + inner_id, OperationStatus.SUCCEEDED + ) + + seen: list[bool] = [] + ctx.run_in_child_context( + lambda child: seen.append(child.is_replaying()), + name="virtual", + config=ChildConfig(is_virtual=True), + ) + + assert seen == [True] + + +def test_virtual_child_without_inner_history_starts_new(): + """A virtual child with no history for its first inner operation is new work.""" + ctx = DurableContext( + state=_replay_state({}), + execution_context=ExecutionContext(durable_execution_arn="arn"), + replay_status=ReplayStatus.REPLAY, + ) + + seen: list[bool] = [] + ctx.run_in_child_context( + lambda child: seen.append(child.is_replaying()), + name="virtual", + config=ChildConfig(is_virtual=True), + ) + + assert seen == [False] + + def test_replay_aware_emits_update_hook_for_operation_updated_since_last_invocation(): """Updated terminal operations emit operation_end, not replay start+end.""" captured: list[tuple[str, str, bool, OperationStatus]] = []