Skip to content
Draft
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 @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# 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

from enum import StrEnum


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