Skip to content

Commit 79e1dcc

Browse files
authored
feat(extras): validate the agentcore_session config (#963)
* feat(extras): validate the agentcore_session config agentcore_session must reject a bad config before any durable operation. It uses the same rules as agentcore(). The runtime ARN is a required field. The timeout must be at least 1 second. Move the timeout check of agentcore() into _validate_timeout. Add _validate_session_config, which the agentcore_session entry function will call first. Add one test per rule and one for a valid config. * fix(extras): check the agentcore_session name agentcore() rejects an empty name. _validate_session_config did not check the name, so it accepted an empty name. Move the name check and the timeout check of agentcore() into _validate_base, as in the microvm operation. The rules and the messages do not change. agentcore() and _validate_session_config both call _validate_base. Add a test for an empty name. * test(extras): cover the name type check Every session test passed a string name, so removing the isinstance check in _validate_base left all tests passing. A new test passes the integer 1 as the name and expects the same ValidationError as the empty-name test.
1 parent a5f6d88 commit 79e1dcc

3 files changed

Lines changed: 110 additions & 7 deletions

File tree

‎packages/aws-durable-execution-sdk-python-extras/src/aws_durable_execution_sdk_python_extras/compute/agentcore/operation.py‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,7 @@ def create(
221221
TypeError: When ``job_input`` is not JSON-serializable.
222222
ValueError: When the JSON of ``job_input`` is too large.
223223
"""
224-
if not isinstance(name, str) or not name:
225-
msg = "an AgentCore operation requires a non-empty name"
226-
raise ValidationError(msg)
227-
timeout_seconds = config.timeout.to_seconds()
228-
if timeout_seconds < 1:
229-
msg = f'AgentCore "{name}": timeout must be at least 1 second'
230-
raise ValidationError(msg)
224+
timeout_seconds = _validate_base(name, config.timeout)
231225
heartbeat_timeout_seconds = (
232226
None
233227
if config.heartbeat_timeout is None
@@ -543,6 +537,23 @@ def _reported_error_type(callback: Callback) -> str | None:
543537
return reported
544538

545539

540+
def _validate_base(name: str, timeout: Duration) -> int:
541+
"""Validate the name and the timeout, and return the timeout in seconds.
542+
543+
Raises:
544+
ValidationError: When ``name`` is empty, or the timeout is less
545+
than 1 second.
546+
"""
547+
if not isinstance(name, str) or not name:
548+
msg = "an AgentCore operation requires a non-empty name"
549+
raise ValidationError(msg)
550+
timeout_seconds = timeout.to_seconds()
551+
if timeout_seconds < 1:
552+
msg = f'AgentCore "{name}": timeout must be at least 1 second'
553+
raise ValidationError(msg)
554+
return timeout_seconds
555+
556+
546557
def _validate_input(name: str, job_input: Any) -> None:
547558
"""Check that the job input is JSON-serializable, and fits in a job body.
548559

‎packages/aws-durable-execution-sdk-python-extras/src/aws_durable_execution_sdk_python_extras/compute/agentcore/session.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88

99
from typing import TYPE_CHECKING, Any, Protocol
1010

11+
from aws_durable_execution_sdk_python_extras.compute.agentcore.operation import (
12+
_validate_base,
13+
)
14+
1115

1216
if TYPE_CHECKING:
1317
from aws_durable_execution_sdk_python.context import DurableContext
1418

1519
from aws_durable_execution_sdk_python_extras.compute.agentcore.config import (
1620
AgentcoreInvokeOptions,
21+
AgentcoreSessionConfig,
1722
)
1823

1924

@@ -59,3 +64,20 @@ def invoke(
5964
retries, or the agent does not accept the job.
6065
"""
6166
...
67+
68+
69+
def _validate_session_config(name: str, config: AgentcoreSessionConfig) -> int:
70+
"""Validate the session config, before any durable operation starts.
71+
72+
It uses the rules of :func:`agentcore`. ``agent_runtime_arn`` is a
73+
required field, and its value is not checked here. The boto3 client and
74+
the service reject a value that is not valid.
75+
76+
Returns:
77+
The session timeout in seconds.
78+
79+
Raises:
80+
ValidationError: When ``name`` is empty, or ``timeout`` is less
81+
than 1 second.
82+
"""
83+
return _validate_base(name, config.timeout)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# SPDX-FileCopyrightText: 2026-present Amazon.com, Inc. or its affiliates.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
"""Tests for the validation of the agentcore_session config."""
5+
6+
from __future__ import annotations
7+
8+
from typing import Any
9+
10+
import pytest
11+
from aws_durable_execution_sdk_python.config import Duration
12+
from aws_durable_execution_sdk_python.exceptions import ValidationError
13+
14+
from aws_durable_execution_sdk_python_extras.compute.agentcore import (
15+
AgentcoreSessionConfig,
16+
)
17+
from aws_durable_execution_sdk_python_extras.compute.agentcore.session import (
18+
_validate_session_config,
19+
)
20+
21+
22+
ARN = "arn:aws:bedrock-agentcore:us-west-2:123456789012:runtime/reviewer-abc123"
23+
24+
25+
def test_accepts_a_valid_config() -> None:
26+
config = AgentcoreSessionConfig(
27+
agent_runtime_arn=ARN, timeout=Duration.from_minutes(30)
28+
)
29+
30+
assert _validate_session_config("review", config) == 1800
31+
32+
33+
def test_config_has_no_default_runtime_arn() -> None:
34+
fields: dict[str, Any] = {"timeout": Duration.from_minutes(30)}
35+
36+
with pytest.raises(TypeError, match="agent_runtime_arn"):
37+
AgentcoreSessionConfig(**fields)
38+
39+
40+
def test_rejects_an_empty_name() -> None:
41+
config = AgentcoreSessionConfig(
42+
agent_runtime_arn=ARN, timeout=Duration.from_minutes(30)
43+
)
44+
45+
with pytest.raises(
46+
ValidationError, match="an AgentCore operation requires a non-empty name"
47+
):
48+
_validate_session_config("", config)
49+
50+
51+
def test_rejects_a_non_string_name() -> None:
52+
config = AgentcoreSessionConfig(
53+
agent_runtime_arn=ARN, timeout=Duration.from_minutes(30)
54+
)
55+
56+
with pytest.raises(
57+
ValidationError, match="an AgentCore operation requires a non-empty name"
58+
):
59+
_validate_session_config(1, config) # type: ignore[arg-type]
60+
61+
62+
def test_rejects_a_timeout_under_1_second() -> None:
63+
config = AgentcoreSessionConfig(
64+
agent_runtime_arn=ARN, timeout=Duration.from_seconds(0)
65+
)
66+
67+
with pytest.raises(
68+
ValidationError, match='AgentCore "review": timeout must be at least 1 second'
69+
):
70+
_validate_session_config("review", config)

0 commit comments

Comments
 (0)