Skip to content

Commit dac7d9d

Browse files
authored
fix(server): return the stored id when creating a push notification config (a2aproject#1236)
## Problem The create handler returned the caller's request instead of what the store saved, so on the database store the response had no `id`. Neither store set `task_id`, so configs registered inline via `SendMessage` came back with `task_id=""`. ## Fix `set_info` now stores a copy with `task_id` set and an empty `id` defaulted to the task id, and returns it without mutating the input. Both handlers return that. `set_info` now returns `TaskPushNotificationConfig` instead of `None`. ## Tests Create-handler tests over both real stores (database skipped without the sqlite extras), an inline `SendMessage` test, and a `set_info` test per store. All fail with the source change reverted. Fixes a2aproject#1237
1 parent ddce6b8 commit dac7d9d

9 files changed

Lines changed: 321 additions & 35 deletions

‎src/a2a/server/request_handlers/default_request_handler.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
TaskManager,
3333
TaskStore,
3434
)
35+
from a2a.server.tasks.push_notification_config_store import (
36+
normalize_push_notification_config,
37+
)
3538
from a2a.types.a2a_pb2 import (
3639
AgentCard,
3740
CancelTaskRequest,
@@ -560,13 +563,15 @@ async def on_create_task_push_notification_config(
560563

561564
await self._reject_unsafe_push_url(params.url)
562565

563-
await self._push_config_store.set_info(
566+
stored = await self._push_config_store.set_info(
564567
task_id,
565568
params,
566569
context,
567570
)
568-
569-
return params
571+
if stored is not None:
572+
return stored
573+
# Custom stores written before set_info returned the stored config.
574+
return normalize_push_notification_config(task_id, params)
570575

571576
@validate_request_params
572577
@validate(

‎src/a2a/server/request_handlers/default_request_handler_v2.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
validate,
3030
validate_request_params,
3131
)
32+
from a2a.server.tasks.push_notification_config_store import (
33+
normalize_push_notification_config,
34+
)
3235
from a2a.types.a2a_pb2 import (
3336
AgentCard,
3437
CancelTaskRequest,
@@ -497,13 +500,15 @@ async def on_create_task_push_notification_config( # noqa: D102
497500

498501
await self._reject_unsafe_push_url(params.url)
499502

500-
await self._push_config_store.set_info(
503+
stored = await self._push_config_store.set_info(
501504
task_id,
502505
params,
503506
context,
504507
)
505-
506-
return params
508+
if stored is not None:
509+
return stored
510+
# Custom stores written before set_info returned the stored config.
511+
return normalize_push_notification_config(task_id, params)
507512

508513
@validate_request_params
509514
@validate(

‎src/a2a/server/tasks/database_push_notification_config_store.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from a2a.server.owner_resolver import OwnerResolver, resolve_user_scope
3939
from a2a.server.tasks.push_notification_config_store import (
4040
PushNotificationConfigStore,
41+
normalize_push_notification_config,
4142
)
4243
from a2a.types.a2a_pb2 import TaskPushNotificationConfig
4344

@@ -283,16 +284,14 @@ async def set_info(
283284
task_id: str,
284285
notification_config: TaskPushNotificationConfig,
285286
context: ServerCallContext,
286-
) -> None:
287+
) -> TaskPushNotificationConfig:
287288
"""Sets or updates the push notification configuration for a task."""
288289
await self._ensure_initialized()
289290
owner = self.owner_resolver(context)
290291

291-
# Create a copy of the config using proto CopyFrom
292-
config_to_save = TaskPushNotificationConfig()
293-
config_to_save.CopyFrom(notification_config)
294-
if not config_to_save.id:
295-
config_to_save.id = task_id
292+
config_to_save = normalize_push_notification_config(
293+
task_id, notification_config
294+
)
296295

297296
db_config = self._to_orm(task_id, config_to_save, owner)
298297
async with self.async_session_maker.begin() as session:
@@ -303,6 +302,7 @@ async def set_info(
303302
config_to_save.id,
304303
owner,
305304
)
305+
return config_to_save
306306

307307
async def _select_configs(
308308
self,

‎src/a2a/server/tasks/inmemory_push_notification_config_store.py‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from a2a.server.owner_resolver import OwnerResolver, resolve_user_scope
66
from a2a.server.tasks.push_notification_config_store import (
77
PushNotificationConfigStore,
8+
normalize_push_notification_config,
89
)
910
from a2a.types.a2a_pb2 import TaskPushNotificationConfig
1011

@@ -40,31 +41,36 @@ async def set_info(
4041
task_id: str,
4142
notification_config: TaskPushNotificationConfig,
4243
context: ServerCallContext,
43-
) -> None:
44+
) -> TaskPushNotificationConfig:
4445
"""Sets or updates the push notification configuration for a task in memory."""
4546
owner = self.owner_resolver(context)
47+
stored = normalize_push_notification_config(
48+
task_id, notification_config
49+
)
50+
4651
with self.lock:
4752
owner_infos = self._push_notification_infos.setdefault(owner, {})
4853
if task_id not in owner_infos:
4954
owner_infos[task_id] = []
5055

51-
if not notification_config.id:
52-
notification_config.id = task_id
53-
5456
# Remove existing config with the same ID
5557
for config in owner_infos[task_id]:
56-
if config.id == notification_config.id:
58+
if config.id == stored.id:
5759
owner_infos[task_id].remove(config)
5860
break
5961

60-
owner_infos[task_id].append(notification_config)
62+
owner_infos[task_id].append(stored)
6163
logger.debug(
6264
'Push notification config for task %s with config id %s for owner %s saved/updated.',
6365
task_id,
64-
notification_config.id,
66+
stored.id,
6567
owner,
6668
)
6769

70+
result = TaskPushNotificationConfig()
71+
result.CopyFrom(stored)
72+
return result
73+
6874
async def get_info(
6975
self,
7076
task_id: str,

‎src/a2a/server/tasks/push_notification_config_store.py‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99
logger = logging.getLogger(__name__)
1010

1111

12+
def normalize_push_notification_config(
13+
task_id: str, notification_config: TaskPushNotificationConfig
14+
) -> TaskPushNotificationConfig:
15+
"""Returns a copy with task_id set and an empty id defaulted to the task id."""
16+
normalized = TaskPushNotificationConfig()
17+
normalized.CopyFrom(notification_config)
18+
normalized.task_id = task_id
19+
if not normalized.id:
20+
normalized.id = task_id
21+
return normalized
22+
23+
1224
class PushNotificationConfigStore(ABC):
1325
"""Interface for storing and retrieving push notification configurations for tasks."""
1426

@@ -18,8 +30,14 @@ async def set_info(
1830
task_id: str,
1931
notification_config: TaskPushNotificationConfig,
2032
context: ServerCallContext,
21-
) -> None:
22-
"""Sets or updates the push notification configuration for a task."""
33+
) -> TaskPushNotificationConfig | None:
34+
"""Sets or updates the push notification configuration for a task.
35+
36+
Implementations should not mutate notification_config. They store a
37+
copy normalized by normalize_push_notification_config and return it.
38+
Returning None is still accepted for existing implementations; the
39+
request handlers then normalize the request themselves.
40+
"""
2341

2442
@abstractmethod
2543
async def get_info(

‎tests/server/request_handlers/test_default_request_handler.py‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,6 +1977,87 @@ async def test_set_task_push_notification_config_task_not_found(agent_card):
19771977
mock_push_store.set_info.assert_not_awaited()
19781978

19791979

1980+
@pytest.mark.asyncio
1981+
@pytest.mark.parametrize('store_kind', ['inmemory', 'database'])
1982+
async def test_create_task_push_notification_config_returns_stored_id(
1983+
agent_card, store_kind
1984+
):
1985+
"""Test on_create_task_push_notification_config returns the id that was stored."""
1986+
if store_kind == 'database':
1987+
pytest.importorskip('sqlalchemy')
1988+
pytest.importorskip('aiosqlite')
1989+
from a2a.server.tasks.database_push_notification_config_store import (
1990+
DatabasePushNotificationConfigStore,
1991+
)
1992+
from sqlalchemy.ext.asyncio import create_async_engine
1993+
1994+
engine = create_async_engine(
1995+
'sqlite+aiosqlite:///file:pushid?mode=memory&cache=shared&uri=true'
1996+
)
1997+
push_config_store = DatabasePushNotificationConfigStore(engine=engine)
1998+
else:
1999+
push_config_store = InMemoryPushNotificationConfigStore()
2000+
2001+
task = create_sample_task()
2002+
task_store = InMemoryTaskStore()
2003+
context = create_server_call_context()
2004+
await task_store.save(task, context)
2005+
2006+
request_handler = DefaultRequestHandler(
2007+
agent_executor=MockAgentExecutor(),
2008+
task_store=task_store,
2009+
push_config_store=push_config_store,
2010+
agent_card=agent_card,
2011+
)
2012+
params = TaskPushNotificationConfig(
2013+
task_id=task.id,
2014+
url='http://example.com',
2015+
)
2016+
2017+
response = await request_handler.on_create_task_push_notification_config(
2018+
params, context
2019+
)
2020+
2021+
stored = await push_config_store.get_info(task.id, context)
2022+
if store_kind == 'database':
2023+
await engine.dispose()
2024+
2025+
assert response.id == task.id
2026+
assert list(stored) == [response]
2027+
assert params.id == '', 'the request object must not be mutated'
2028+
2029+
2030+
@pytest.mark.asyncio
2031+
async def test_create_task_push_notification_config_normalizes_when_store_returns_none(
2032+
agent_card,
2033+
):
2034+
"""A custom store that still returns None from set_info keeps working."""
2035+
push_config_store = AsyncMock(spec=PushNotificationConfigStore)
2036+
push_config_store.set_info.return_value = None
2037+
2038+
task = create_sample_task()
2039+
task_store = InMemoryTaskStore()
2040+
context = create_server_call_context()
2041+
await task_store.save(task, context)
2042+
2043+
request_handler = DefaultRequestHandler(
2044+
agent_executor=MockAgentExecutor(),
2045+
task_store=task_store,
2046+
push_config_store=push_config_store,
2047+
agent_card=agent_card,
2048+
)
2049+
params = TaskPushNotificationConfig(
2050+
task_id=task.id, url='http://example.com'
2051+
)
2052+
2053+
response = await request_handler.on_create_task_push_notification_config(
2054+
params, context
2055+
)
2056+
2057+
assert (response.task_id, response.id) == (task.id, task.id)
2058+
assert params.id == '', 'the request object must not be mutated'
2059+
2060+
19802061
@pytest.mark.asyncio
19812062
async def test_get_task_push_notification_config_no_store(agent_card):
19822063
"""Test on_get_task_push_notification_config when _push_config_store is None."""

‎tests/server/request_handlers/test_default_request_handler_v2.py‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,84 @@ async def test_set_task_push_notification_config_task_not_found():
513513
mock_push_store.set_info.assert_not_awaited()
514514

515515

516+
@pytest.mark.asyncio
517+
@pytest.mark.parametrize('store_kind', ['inmemory', 'database'])
518+
async def test_create_task_push_notification_config_returns_stored_id(
519+
store_kind,
520+
):
521+
"""Test on_create_task_push_notification_config returns the id that was stored."""
522+
if store_kind == 'database':
523+
pytest.importorskip('sqlalchemy')
524+
pytest.importorskip('aiosqlite')
525+
from a2a.server.tasks.database_push_notification_config_store import (
526+
DatabasePushNotificationConfigStore,
527+
)
528+
from sqlalchemy.ext.asyncio import create_async_engine
529+
530+
engine = create_async_engine(
531+
'sqlite+aiosqlite:///file:pushidv2?mode=memory&cache=shared&uri=true'
532+
)
533+
push_config_store = DatabasePushNotificationConfigStore(engine=engine)
534+
else:
535+
push_config_store = InMemoryPushNotificationConfigStore()
536+
537+
task = create_sample_task()
538+
task_store = InMemoryTaskStore()
539+
context = create_server_call_context()
540+
await task_store.save(task, context)
541+
542+
request_handler = DefaultRequestHandlerV2(
543+
agent_executor=MockAgentExecutor(),
544+
task_store=task_store,
545+
push_config_store=push_config_store,
546+
agent_card=create_default_agent_card(),
547+
)
548+
params = TaskPushNotificationConfig(
549+
task_id=task.id, url='http://example.com'
550+
)
551+
552+
response = await request_handler.on_create_task_push_notification_config(
553+
params, context
554+
)
555+
556+
stored = await push_config_store.get_info(task.id, context)
557+
if store_kind == 'database':
558+
await engine.dispose()
559+
560+
assert response.id == task.id
561+
assert list(stored) == [response]
562+
assert params.id == '', 'the request object must not be mutated'
563+
564+
565+
@pytest.mark.asyncio
566+
async def test_create_task_push_notification_config_normalizes_when_store_returns_none():
567+
"""A custom store that still returns None from set_info keeps working."""
568+
push_config_store = AsyncMock(spec=PushNotificationConfigStore)
569+
push_config_store.set_info.return_value = None
570+
571+
task = create_sample_task()
572+
task_store = InMemoryTaskStore()
573+
context = create_server_call_context()
574+
await task_store.save(task, context)
575+
576+
request_handler = DefaultRequestHandlerV2(
577+
agent_executor=MockAgentExecutor(),
578+
task_store=task_store,
579+
push_config_store=push_config_store,
580+
agent_card=create_default_agent_card(),
581+
)
582+
params = TaskPushNotificationConfig(
583+
task_id=task.id, url='http://example.com'
584+
)
585+
586+
response = await request_handler.on_create_task_push_notification_config(
587+
params, context
588+
)
589+
590+
assert (response.task_id, response.id) == (task.id, task.id)
591+
assert params.id == '', 'the request object must not be mutated'
592+
593+
516594
@pytest.mark.asyncio
517595
async def test_get_task_push_notification_config_no_store():
518596
"""Test on_get_task_push_notification_config when _push_config_store is None."""
@@ -1710,6 +1788,40 @@ async def test_on_message_send_with_push_notification():
17101788
)
17111789

17121790

1791+
@pytest.mark.asyncio
1792+
async def test_on_message_send_stores_inline_push_config_under_its_task():
1793+
task_store = InMemoryTaskStore()
1794+
push_store = InMemoryPushNotificationConfigStore()
1795+
1796+
request_handler = DefaultRequestHandlerV2(
1797+
agent_executor=HelloAgentExecutor(),
1798+
task_store=task_store,
1799+
push_config_store=push_store,
1800+
agent_card=create_default_agent_card(),
1801+
)
1802+
# SendMessageConfiguration carries neither task_id nor id for the config.
1803+
params = SendMessageRequest(
1804+
message=Message(
1805+
role=Role.ROLE_USER,
1806+
message_id='msg_push_inline',
1807+
parts=[Part(text='Hi')],
1808+
),
1809+
configuration=SendMessageConfiguration(
1810+
task_push_notification_config=TaskPushNotificationConfig(
1811+
url='http://example.com/webhook'
1812+
)
1813+
),
1814+
)
1815+
1816+
context = create_server_call_context()
1817+
result = await request_handler.on_message_send(params, context)
1818+
1819+
stored = await push_store.get_info(result.id, context)
1820+
assert [(config.task_id, config.id) for config in stored] == [
1821+
(result.id, result.id)
1822+
]
1823+
1824+
17131825
@pytest.mark.asyncio
17141826
async def test_on_message_send_with_empty_push_notification_config_does_not_call_set_info():
17151827
task_store = InMemoryTaskStore()

0 commit comments

Comments
 (0)