Skip to content

Commit 261c074

Browse files
fix(server): apply history_length to legacy streaming sends (a2aproject#1364)
## Problem The legacy `on_message_send_stream` ignored `history_length`. Task events kept full history and negative values weren't rejected. ## Fix Validate at the top and apply it to yielded Task events, as `on_message_send` and the v2 stream handler do. Push notifications still get the full task. ## Tests `test_on_message_send_stream_limit_history` and `test_on_message_send_stream_negative_history_length_error` fail on main. 2289 passed. Fixes a2aproject#1363 --------- Co-authored-by: Mateusz Iwaniuk <45974414+Iwaniukooo11@users.noreply.github.com>
1 parent 79a6f45 commit 261c074

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ async def on_message_send_stream(
478478
Starts the agent execution and yields events as they are produced
479479
by the agent.
480480
"""
481+
validate_history_length(params.configuration)
482+
481483
(
482484
_task_manager,
483485
task_id,
@@ -490,11 +492,13 @@ async def on_message_send_stream(
490492

491493
try:
492494
async for event in result_aggregator.consume_and_emit(consumer):
495+
response = event
493496
if isinstance(event, Task):
494497
self._validate_task_id_match(task_id, event.id)
498+
response = apply_history_length(event, params.configuration)
495499

496500
await self._send_push_notification_if_needed(task_id, event)
497-
yield event
501+
yield response
498502
except (asyncio.CancelledError, GeneratorExit):
499503
# Client disconnected: continue consuming and persisting events in the background
500504
bg_task = asyncio.create_task(

‎tests/server/request_handlers/test_default_request_handler.py‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,46 @@ async def test_on_message_send_limit_history(agent_card):
11421142
assert task.history is not None and len(task.history) > 1
11431143

11441144

1145+
@pytest.mark.asyncio
1146+
async def test_on_message_send_stream_limit_history(agent_card):
1147+
task_store = InMemoryTaskStore()
1148+
1149+
request_handler = DefaultRequestHandler(
1150+
agent_executor=HelloAgentExecutor(),
1151+
task_store=task_store,
1152+
agent_card=agent_card,
1153+
)
1154+
params = SendMessageRequest(
1155+
message=Message(
1156+
role=Role.ROLE_USER,
1157+
message_id='msg_stream',
1158+
parts=[Part(text='Hi')],
1159+
),
1160+
configuration=SendMessageConfiguration(
1161+
accepted_output_modes=['text/plain'],
1162+
history_length=0,
1163+
),
1164+
)
1165+
1166+
context = create_server_call_context()
1167+
tasks = [
1168+
event
1169+
async for event in request_handler.on_message_send_stream(
1170+
params, context
1171+
)
1172+
if isinstance(event, Task)
1173+
]
1174+
1175+
# verify that history_length is honored
1176+
assert tasks
1177+
assert all(len(task.history) == 0 for task in tasks)
1178+
1179+
# verify that history is still persisted to the store
1180+
task = await task_store.get(tasks[0].id, context)
1181+
assert task is not None
1182+
assert len(task.history) > 0
1183+
1184+
11451185
@pytest.mark.asyncio
11461186
async def test_on_get_task_limit_history(agent_card):
11471187
task_store = InMemoryTaskStore()
@@ -3014,6 +3054,38 @@ async def test_on_message_send_negative_history_length_error(agent_card):
30143054
assert 'history length must be non-negative' in exc_info.value.message
30153055

30163056

3057+
@pytest.mark.asyncio
3058+
async def test_on_message_send_stream_negative_history_length_error(
3059+
agent_card,
3060+
):
3061+
"""Test on_message_send_stream raises error for negative history length in configuration."""
3062+
mock_task_store = AsyncMock(spec=TaskStore)
3063+
mock_agent_executor = AsyncMock(spec=AgentExecutor)
3064+
request_handler = DefaultRequestHandler(
3065+
agent_executor=mock_agent_executor,
3066+
task_store=mock_task_store,
3067+
agent_card=agent_card,
3068+
)
3069+
3070+
message_config = SendMessageConfiguration(
3071+
history_length=-1,
3072+
accepted_output_modes=['text/plain'],
3073+
)
3074+
params = SendMessageRequest(
3075+
message=Message(
3076+
role=Role.ROLE_USER, message_id='msg1', parts=[Part(text='Test')]
3077+
),
3078+
configuration=message_config,
3079+
)
3080+
context = create_server_call_context()
3081+
3082+
with pytest.raises(InvalidParamsError) as exc_info:
3083+
async for _ in request_handler.on_message_send_stream(params, context):
3084+
pass
3085+
3086+
assert 'history length must be non-negative' in exc_info.value.message
3087+
3088+
30173089
@pytest.mark.asyncio
30183090
async def test_on_get_extended_agent_card_success(agent_card):
30193091
"""Test on_get_extended_agent_card when extended_agent_card is supported."""

0 commit comments

Comments
 (0)