Skip to content

Commit 0091992

Browse files
committed
fixup: code style and convention alignment
- hoist a2a_push_config_store init outside per-agent loop, matching the task_store pattern (both now use if-None guard at the top) - remove redundant local push_config_store variable; use a2a_push_config_store directly in DefaultRequestHandler call - trim docstring: drop inline Example block, shorten to match the one-liner style used by the other params in the same function - rename test to test_a2a_custom_task_store_is_used (shorter, matches existing naming style) - fix test mock setup to use an explicit mock_a2a_app_instance like the test_app_with_a2a fixture does - simplify test assertion: drop intermediate call_kwargs variable
1 parent 70ba852 commit 0091992

File tree

2 files changed

+18
-33
lines changed

2 files changed

+18
-33
lines changed

src/google/adk/cli/fast_api.py

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -128,22 +128,11 @@ def get_fast_api_app(
128128
web: Whether to enable the web UI and serve its assets.
129129
a2a: Whether to enable Agent-to-Agent (A2A) protocol support.
130130
a2a_task_store: Optional A2A TaskStore instance. Defaults to
131-
InMemoryTaskStore when a2a=True. Pass a DatabaseTaskStore (from the
132-
a2a-sdk) for persistence across server restarts and horizontal replicas.
133-
Example::
134-
135-
from a2a.server.tasks import DatabaseTaskStore
136-
from sqlalchemy.ext.asyncio import create_async_engine
137-
138-
engine = create_async_engine("postgresql+asyncpg://user:pw@host/db")
139-
app = get_fast_api_app(
140-
agents_dir="agents/", web=True, a2a=True,
141-
a2a_task_store=DatabaseTaskStore(engine),
142-
)
143-
144-
a2a_push_config_store: Optional A2A PushNotificationConfigStore instance.
145-
Defaults to InMemoryPushNotificationConfigStore when a2a=True. Pass a
146-
DatabasePushNotificationConfigStore for persistence across restarts.
131+
InMemoryTaskStore. Pass a custom store (e.g. DatabaseTaskStore) to
132+
persist task state across restarts or share it across replicas.
133+
a2a_push_config_store: Optional A2A PushNotificationConfigStore.
134+
Defaults to InMemoryPushNotificationConfigStore. Pass a custom store
135+
for persistence across restarts.
147136
host: Host address for the server (defaults to 127.0.0.1).
148137
port: Port number for the server (defaults to 8000).
149138
url_prefix: Optional prefix for all URL routes.
@@ -582,6 +571,8 @@ async def get_agent_builder(
582571
if base_path.exists() and base_path.is_dir():
583572
if a2a_task_store is None:
584573
a2a_task_store = InMemoryTaskStore()
574+
if a2a_push_config_store is None:
575+
a2a_push_config_store = InMemoryPushNotificationConfigStore()
585576

586577
def create_a2a_runner_loader(captured_app_name: str):
587578
"""Factory function to create A2A runner with proper closure."""
@@ -609,16 +600,10 @@ async def _get_a2a_runner_async() -> Runner:
609600
runner=create_a2a_runner_loader(app_name),
610601
)
611602

612-
push_config_store = (
613-
a2a_push_config_store
614-
if a2a_push_config_store is not None
615-
else InMemoryPushNotificationConfigStore()
616-
)
617-
618603
request_handler = DefaultRequestHandler(
619604
agent_executor=agent_executor,
620605
task_store=a2a_task_store,
621-
push_config_store=push_config_store,
606+
push_config_store=a2a_push_config_store,
622607
)
623608

624609
with (p / "agent.json").open("r", encoding="utf-8") as f:

tests/unittests/cli/test_fast_api.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,7 @@ def test_a2a_uses_in_memory_task_store_by_default(
15571557
temp_agents_dir_with_a2a,
15581558
monkeypatch,
15591559
):
1560-
"""Test that InMemoryTaskStore is created when no task_store is provided."""
1560+
"""Test that InMemoryTaskStore is used when no task store is provided."""
15611561
with (
15621562
patch("signal.signal", return_value=None),
15631563
patch(
@@ -1590,7 +1590,9 @@ def test_a2a_uses_in_memory_task_store_by_default(
15901590
patch("a2a.server.request_handlers.DefaultRequestHandler"),
15911591
patch("a2a.server.apps.A2AStarletteApplication") as mock_a2a_app,
15921592
):
1593-
mock_a2a_app.return_value.routes.return_value = []
1593+
mock_a2a_app_instance = MagicMock()
1594+
mock_a2a_app_instance.routes.return_value = []
1595+
mock_a2a_app.return_value = mock_a2a_app_instance
15941596
monkeypatch.chdir(temp_agents_dir_with_a2a)
15951597

15961598
_ = get_fast_api_app(
@@ -1608,7 +1610,7 @@ def test_a2a_uses_in_memory_task_store_by_default(
16081610
mock_task_store_class.assert_called_once()
16091611

16101612

1611-
def test_a2a_custom_task_store_bypasses_in_memory_default(
1613+
def test_a2a_custom_task_store_is_used(
16121614
mock_session_service,
16131615
mock_artifact_service,
16141616
mock_memory_service,
@@ -1618,7 +1620,7 @@ def test_a2a_custom_task_store_bypasses_in_memory_default(
16181620
temp_agents_dir_with_a2a,
16191621
monkeypatch,
16201622
):
1621-
"""Test that a custom task_store is forwarded and InMemoryTaskStore is not created."""
1623+
"""Test that a custom task store is forwarded to DefaultRequestHandler."""
16221624
custom_task_store = MagicMock()
16231625

16241626
with (
@@ -1655,7 +1657,9 @@ def test_a2a_custom_task_store_bypasses_in_memory_default(
16551657
) as mock_handler,
16561658
patch("a2a.server.apps.A2AStarletteApplication") as mock_a2a_app,
16571659
):
1658-
mock_a2a_app.return_value.routes.return_value = []
1660+
mock_a2a_app_instance = MagicMock()
1661+
mock_a2a_app_instance.routes.return_value = []
1662+
mock_a2a_app.return_value = mock_a2a_app_instance
16591663
monkeypatch.chdir(temp_agents_dir_with_a2a)
16601664

16611665
_ = get_fast_api_app(
@@ -1671,12 +1675,8 @@ def test_a2a_custom_task_store_bypasses_in_memory_default(
16711675
port=8000,
16721676
)
16731677

1674-
# InMemoryTaskStore must NOT be instantiated when a custom store is supplied
16751678
mock_task_store_class.assert_not_called()
1676-
1677-
# The custom store must be passed through to DefaultRequestHandler
1678-
call_kwargs = mock_handler.call_args.kwargs
1679-
assert call_kwargs["task_store"] is custom_task_store
1679+
assert mock_handler.call_args.kwargs["task_store"] is custom_task_store
16801680

16811681

16821682
def test_patch_memory(test_app, create_test_session, mock_memory_service):

0 commit comments

Comments
 (0)