From 83d8a8c9a5726b989f74ee0f864506cb2979ab36 Mon Sep 17 00:00:00 2001 From: Pooya Paridel Date: Sat, 21 Nov 2026 00:44:41 +0000 Subject: [PATCH 1/2] feat(extensions): label the agentcore_session context The agentcore_session child context now records the subtype AgentcoreSession, the same value as the JavaScript SDK. The steps and the job operations keep their subtypes. A job's own child context keeps the default subtype, as in microvm_session. This changes the recorded history of agentcore_session. The API is experimental and not released. Closes #945 --- .../compute/agentcore/session.py | 15 +++++- .../compute/agentcore/subtypes.py | 8 ++- .../agentcore/agentcore_operation_test.py | 7 +-- .../agentcore/agentcore_session_test.py | 50 +++++++++++++++++++ 4 files changed, 74 insertions(+), 6 deletions(-) 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..b33ef8a69 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: @@ -216,6 +220,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``. The + session's steps and each job's callback and request record the + subtypes of :func:`agentcore`. A job's own child context keeps the + default subtype of ``run_in_child_context``. + 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 +285,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..ed42ca4c7 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,8 @@ class AgentcoreOperationSubType(StrEnum): AGENTCORE = "Agentcore" """The child context of an ``agentcore`` call.""" + SESSION = "AgentcoreSession" + """The child context of an ``agentcore_session`` call.""" 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..280e6015f 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,10 @@ 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 + } == 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..e28030439 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": "RunInChildContext", + "build.callback": "AgentcoreCallback", + "build.request": "AgentcoreRequest", + "test": "RunInChildContext", + "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"), [ From ad611a93906caa3f6d1e739b84c969232cdab3ca Mon Sep 17 00:00:00 2001 From: Pooya Paridel Date: Sat, 21 Nov 2026 01:15:52 +0000 Subject: [PATCH 2/2] feat(extensions): label agentcore session jobs Each AgentcoreSession.invoke job now records the subtype AgentcoreSessionJob on its child context. The JavaScript SDK records the same subtype, so both SDKs write the same execution history. --- .../compute/agentcore/session.py | 14 +++++++++----- .../compute/agentcore/subtypes.py | 2 ++ .../compute/agentcore/agentcore_operation_test.py | 3 ++- .../compute/agentcore/agentcore_session_test.py | 4 ++-- 4 files changed, 15 insertions(+), 8 deletions(-) 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 b33ef8a69..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 @@ -160,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 @@ -220,10 +224,10 @@ 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``. The - session's steps and each job's callback and request record the - subtypes of :func:`agentcore`. A job's own child context keeps the - default subtype of ``run_in_child_context``. + 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. 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 ed42ca4c7..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 @@ -25,6 +25,8 @@ class AgentcoreOperationSubType(StrEnum): """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 280e6015f..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 @@ -798,7 +798,8 @@ def test_labels_every_operation_with_its_subtype(fake_agentcore_client: Any) -> assert recorded == {name: {sub_type} for name, sub_type in expected.items()} # Every other subtype belongs to agentcore_session. assert {member.value for member in AgentcoreOperationSubType} - { - AgentcoreOperationSubType.SESSION + AgentcoreOperationSubType.SESSION, + AgentcoreOperationSubType.SESSION_JOB, } == set(expected.values()) 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 e28030439..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 @@ -451,10 +451,10 @@ def body(session: AgentcoreSession, ctx: DurableContext) -> Any: expected = { "review": "AgentcoreSession", "review.session": "AgentcoreSessionId", - "build": "RunInChildContext", + "build": "AgentcoreSessionJob", "build.callback": "AgentcoreCallback", "build.request": "AgentcoreRequest", - "test": "RunInChildContext", + "test": "AgentcoreSessionJob", "test.callback": "AgentcoreCallback", "test.request": "AgentcoreRequest", "review.stop": "AgentcoreStop",