diff --git a/packages/aws-durable-execution-sdk-python-extensions/src/aws_durable_execution_sdk_python_extensions/compute/agentcore/session.py b/packages/aws-durable-execution-sdk-python-extensions/src/aws_durable_execution_sdk_python_extensions/compute/agentcore/session.py index 2521c7f47..ed038bdd2 100644 --- a/packages/aws-durable-execution-sdk-python-extensions/src/aws_durable_execution_sdk_python_extensions/compute/agentcore/session.py +++ b/packages/aws-durable-execution-sdk-python-extensions/src/aws_durable_execution_sdk_python_extensions/compute/agentcore/session.py @@ -9,6 +9,7 @@ from collections.abc import Callable from typing import TYPE_CHECKING, Any, TypeVar +from aws_durable_execution_sdk_python.config import ChildConfig from aws_durable_execution_sdk_python.exceptions import ( ChildContextError, ExecutionError, @@ -31,6 +32,9 @@ from aws_durable_execution_sdk_python_extensions.compute.agentcore.retry import ( default_agentcore_retry_strategy, ) +from aws_durable_execution_sdk_python_extensions.compute.agentcore.subtypes import ( + AgentcoreOperationSubType, +) if TYPE_CHECKING: @@ -156,7 +160,11 @@ def run(job_context: DurableContext) -> Any: return _run_job(job_context, scope, session_id, job_input, lambda: None) try: - return context.run_in_child_context(run, name=name) + return context.run_in_child_context( + run, + name=name, + config=ChildConfig(sub_type=AgentcoreOperationSubType.SESSION_JOB), + ) except ChildContextError as error: raise AgentcoreError.from_child_context_error(name, error) from error @@ -216,6 +224,11 @@ def agentcore_session( A stop for one of them would end a session that the replay still uses, or add an operation that the replay does not expect. + The child context records the subtype ``AgentcoreSession``. Each + job's own child context records the subtype ``AgentcoreSessionJob``. + The session's steps and each job's callback and request record the + subtypes of :func:`agentcore`. + A failed :meth:`AgentcoreSession.invoke` raises inside the handler. The handler can catch the error and send the next job to the same session. An error that leaves the handler reaches the caller as follows: @@ -276,7 +289,11 @@ def stop() -> None: return result try: - return context.run_in_child_context(run, name=name) + return context.run_in_child_context( + run, + name=name, + config=ChildConfig(sub_type=AgentcoreOperationSubType.SESSION), + ) except ChildContextError as error: # The handler is caller code. So only an AgentCore error is restored # to its class. Every other error keeps the SDK's ChildContextError. diff --git a/packages/aws-durable-execution-sdk-python-extensions/src/aws_durable_execution_sdk_python_extensions/compute/agentcore/subtypes.py b/packages/aws-durable-execution-sdk-python-extensions/src/aws_durable_execution_sdk_python_extensions/compute/agentcore/subtypes.py index 3387d0566..1ca3e58f9 100644 --- a/packages/aws-durable-execution-sdk-python-extensions/src/aws_durable_execution_sdk_python_extensions/compute/agentcore/subtypes.py +++ b/packages/aws-durable-execution-sdk-python-extensions/src/aws_durable_execution_sdk_python_extensions/compute/agentcore/subtypes.py @@ -1,7 +1,8 @@ # SPDX-FileCopyrightText: 2026-present Amazon.com, Inc. or its affiliates. # # SPDX-License-Identifier: Apache-2.0 -"""The subtypes that :func:`agentcore` records on its durable operations.""" +"""The subtypes that :func:`agentcore` and :func:`agentcore_session` +record on their durable operations.""" from __future__ import annotations @@ -9,7 +10,8 @@ class AgentcoreOperationSubType(StrEnum): - """The subtypes that ``agentcore`` records on its durable operations. + """The subtypes that ``agentcore`` and ``agentcore_session`` record on + their durable operations. Execution history and plugins can filter on them without parsing operation names. The values equal ``AgentcoreOperationSubType`` in the @@ -21,6 +23,10 @@ class AgentcoreOperationSubType(StrEnum): AGENTCORE = "Agentcore" """The child context of an ``agentcore`` call.""" + SESSION = "AgentcoreSession" + """The child context of an ``agentcore_session`` call.""" + SESSION_JOB = "AgentcoreSessionJob" + """The child context of one :meth:`AgentcoreSession.invoke` job.""" SESSION_ID = "AgentcoreSessionId" """The step that generates and checkpoints the runtime session ID.""" CALLBACK = "AgentcoreCallback" diff --git a/packages/aws-durable-execution-sdk-python-extensions/tests/compute/agentcore/agentcore_operation_test.py b/packages/aws-durable-execution-sdk-python-extensions/tests/compute/agentcore/agentcore_operation_test.py index 31ca37f17..39d92f7a7 100644 --- a/packages/aws-durable-execution-sdk-python-extensions/tests/compute/agentcore/agentcore_operation_test.py +++ b/packages/aws-durable-execution-sdk-python-extensions/tests/compute/agentcore/agentcore_operation_test.py @@ -796,9 +796,11 @@ def test_labels_every_operation_with_its_subtype(fake_agentcore_client: Any) -> if event.name in expected: recorded.setdefault(event.name, set()).add(event.sub_type) assert recorded == {name: {sub_type} for name, sub_type in expected.items()} - assert {member.value for member in AgentcoreOperationSubType} == set( - expected.values() - ) + # Every other subtype belongs to agentcore_session. + assert {member.value for member in AgentcoreOperationSubType} - { + AgentcoreOperationSubType.SESSION, + AgentcoreOperationSubType.SESSION_JOB, + } == set(expected.values()) def stop_failure_logs(caplog: pytest.LogCaptureFixture) -> list[str]: diff --git a/packages/aws-durable-execution-sdk-python-extensions/tests/compute/agentcore/agentcore_session_test.py b/packages/aws-durable-execution-sdk-python-extensions/tests/compute/agentcore/agentcore_session_test.py index 8bc7824f6..84c0791e3 100644 --- a/packages/aws-durable-execution-sdk-python-extensions/tests/compute/agentcore/agentcore_session_test.py +++ b/packages/aws-durable-execution-sdk-python-extensions/tests/compute/agentcore/agentcore_session_test.py @@ -427,6 +427,56 @@ def body(session: AgentcoreSession, ctx: DurableContext) -> Any: ] +def test_labels_every_operation_with_its_subtype(fake_agentcore_client: Any) -> None: + client = fake_agentcore_client() + + def body(session: AgentcoreSession, ctx: DurableContext) -> Any: + session.invoke(ctx, "build", job_input={}, options=OPTIONS) + return session.invoke(ctx, "test", job_input={}, options=OPTIONS) + + runner = DurableFunctionTestRunner( + handler=session_handler(session_config(client), body), poll_interval=0.05 + ) + try: + arn = runner.run_async(input="{}", execution_timeout=WAIT) + for name in ("build.callback", "test.callback"): + callback_id = runner.wait_for_callback(arn, name=name, timeout=WAIT) + runner.send_callback_success(callback_id, b'"done"') + result = runner.wait_for_result(arn, timeout=WAIT) + history = runner.get_execution_history(arn) + finally: + runner.close() + + assert outcome(result) == {"result": "done"} + expected = { + "review": "AgentcoreSession", + "review.session": "AgentcoreSessionId", + "build": "AgentcoreSessionJob", + "build.callback": "AgentcoreCallback", + "build.request": "AgentcoreRequest", + "test": "AgentcoreSessionJob", + "test.callback": "AgentcoreCallback", + "test.request": "AgentcoreRequest", + "review.stop": "AgentcoreStop", + } + context = result.get_context("review") + recorded_tree: dict[str | None, str | None] = {"review": context.sub_type} + for child in context.child_operations: + recorded_tree[child.name] = child.sub_type + if child.name in ("build", "test"): + job = context.get_context(child.name) + for step in job.child_operations: + recorded_tree[step.name] = step.sub_type + assert recorded_tree == expected + # The execution history carries the subtype on every event of each + # operation. + recorded: dict[str, set[str | None]] = {} + for event in history.events: + if event.name in expected: + recorded.setdefault(event.name, set()).add(event.sub_type) + assert recorded == {name: {sub_type} for name, sub_type in expected.items()} + + @pytest.mark.parametrize( ("fields", "invokes", "complete", "expected"), [