Skip to content

Commit a016002

Browse files
fix(server): run follow-up messages in the sender's contextvars (a2aproject#1317)
# Description `ActiveTask` creates its producer once, during the first request for a task. Later messages on the same task are queued to that producer, so `AgentExecutor.execute()` ran with the **first** request's contextvars. Per-request values set by ASGI middleware or a `ServerCallContextBuilder` (auth tokens, tenant identity, tracing) were stale on every follow-up turn, for example a reply after `input-required`. This change captures `contextvars.copy_context()` in `enqueue_request()`, which runs in the sender's request, and runs `execute()` in that context as a child task. Awaiting the child keeps cancellation working, because cancelling the producer cancels the child. `Context.run(asyncio.create_task, ...)` works on Python 3.10. Only `execute()` changes context. Task-store writes and push notifications still run in the producer's context, as before. Tested: - New `test_follow_up_message_runs_in_sender_contextvars`, for both `on_message_send` and `on_message_send_stream`. It fails on `main` and passes with this change. - The reproduction in a2aproject#1316 prints `request-2` for the follow-up. - `./scripts/lint.sh` passes. `uv run pytest --cov=src` gives 2223 passed, 93% coverage. `tests/integration/cross_version/client_server/test_client_server.py` was deselected because its server fails to start in my local environment on `main` too. - [x] Follow the [`CONTRIBUTING` Guide](https://github.com/a2aproject/a2a-python/blob/main/CONTRIBUTING.md). - [x] Make your Pull Request title in the <https://www.conventionalcommits.org/> specification. - [x] Ensure the tests and linter pass (Run `bash scripts/format.sh` from the repository root to format) - [x] Appropriate docs were updated (if necessary) Fixes a2aproject#1316 🦕 Co-authored-by: mykytanetipa <mykytanetipa@google.com>
1 parent 59dcbfb commit a016002

2 files changed

Lines changed: 105 additions & 7 deletions

File tree

‎src/a2a/server/agent_execution/active_task.py‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from __future__ import annotations
3737

3838
import asyncio
39+
import contextvars
3940
import logging
4041
import uuid
4142

@@ -468,9 +469,9 @@ def __init__( # noqa: PLR0913
468469
self._reference_count = 0
469470

470471
# Queue for incoming requests
471-
self._request_queue: AsyncQueue[tuple[RequestContext, uuid.UUID]] = (
472-
create_async_queue()
473-
)
472+
self._request_queue: AsyncQueue[
473+
tuple[RequestContext, uuid.UUID, contextvars.Context]
474+
] = create_async_queue()
474475

475476
@property
476477
def task_id(self) -> str:
@@ -480,9 +481,16 @@ def task_id(self) -> str:
480481
async def enqueue_request(
481482
self, request_context: RequestContext
482483
) -> uuid.UUID:
483-
"""Enqueues a request for the active task to process."""
484+
"""Enqueues a request for the active task to process.
485+
486+
The caller's contextvars are captured so the producer runs
487+
`AgentExecutor.execute` in the context of the request that sent the
488+
message, not the request that started the producer.
489+
"""
484490
request_id = uuid.uuid4()
485-
await self._request_queue.put((request_context, request_id))
491+
await self._request_queue.put(
492+
(request_context, request_id, contextvars.copy_context())
493+
)
486494
return request_id
487495

488496
async def start(
@@ -573,6 +581,7 @@ async def _run_producer(self) -> None:
573581
(
574582
request_context,
575583
request_id,
584+
sender_context,
576585
) = await self._request_queue.get()
577586
await self._request_lock.acquire()
578587
# TODO: Should we create task manager every time?
@@ -607,8 +616,12 @@ async def _run_producer(self) -> None:
607616
_RequestStarted(request_id, request_context),
608617
)
609618
)
610-
await self._agent_executor.execute(
611-
request_context, self._event_queue_agent
619+
# Awaiting the child task propagates producer cancellation.
620+
await sender_context.run(
621+
asyncio.create_task,
622+
self._agent_executor.execute(
623+
request_context, self._event_queue_agent
624+
),
612625
)
613626
logger.debug(
614627
'Producer[%s]: Execution finished successfully',

‎tests/server/request_handlers/test_default_request_handler_v2.py‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import contextvars
23
import logging
34
import time
45
import uuid
@@ -2443,3 +2444,87 @@ async def test_cancel_of_input_required_task_cannot_be_undone():
24432444
assert stored.status.state == TaskState.TASK_STATE_CANCELED
24442445
assert agent.execute_calls == 1
24452446
await handler.aclose()
2447+
2448+
2449+
_REQUEST_TAG: contextvars.ContextVar[str] = contextvars.ContextVar(
2450+
'request_tag', default='unset'
2451+
)
2452+
2453+
2454+
class _InputRequiredThenCompleteAgent(AgentExecutor):
2455+
"""Asks for input on the first message of a task and completes on the next."""
2456+
2457+
def __init__(self) -> None:
2458+
self.seen_tags: list[str] = []
2459+
self.task: Task | None = None
2460+
2461+
async def execute(
2462+
self, context: RequestContext, event_queue: EventQueue
2463+
) -> None:
2464+
self.seen_tags.append(_REQUEST_TAG.get())
2465+
if context.current_task:
2466+
updater = TaskUpdater(
2467+
event_queue, context.task_id, context.context_id
2468+
)
2469+
await updater.complete()
2470+
return
2471+
self.task = new_task_from_user_message(context.message)
2472+
await event_queue.enqueue_event(self.task)
2473+
updater = TaskUpdater(event_queue, self.task.id, self.task.context_id)
2474+
await updater.update_status(TaskState.TASK_STATE_INPUT_REQUIRED)
2475+
2476+
async def cancel(
2477+
self, context: RequestContext, event_queue: EventQueue
2478+
) -> None:
2479+
pass
2480+
2481+
2482+
@pytest.mark.timeout(10)
2483+
@pytest.mark.asyncio
2484+
@pytest.mark.parametrize('streaming', [False, True])
2485+
async def test_follow_up_message_runs_in_sender_contextvars(streaming):
2486+
"""A follow-up message on a live task must see its own request's contextvars."""
2487+
agent = _InputRequiredThenCompleteAgent()
2488+
handler = DefaultRequestHandlerV2(
2489+
agent_executor=agent,
2490+
task_store=InMemoryTaskStore(),
2491+
agent_card=create_default_agent_card(),
2492+
)
2493+
2494+
async def send(tag: str, message: Message) -> None:
2495+
_REQUEST_TAG.set(tag)
2496+
params = SendMessageRequest(message=message)
2497+
if streaming:
2498+
async for _ in handler.on_message_send_stream(
2499+
params, create_server_call_context()
2500+
):
2501+
pass
2502+
else:
2503+
await handler.on_message_send(params, create_server_call_context())
2504+
2505+
await asyncio.create_task(
2506+
send(
2507+
'request-1',
2508+
Message(
2509+
message_id='msg-1',
2510+
role=Role.ROLE_USER,
2511+
parts=[Part(text='book a flight')],
2512+
),
2513+
)
2514+
)
2515+
assert agent.task is not None
2516+
await asyncio.create_task(
2517+
send(
2518+
'request-2',
2519+
Message(
2520+
message_id='msg-2',
2521+
role=Role.ROLE_USER,
2522+
parts=[Part(text='Friday')],
2523+
task_id=agent.task.id,
2524+
context_id=agent.task.context_id,
2525+
),
2526+
)
2527+
)
2528+
2529+
assert agent.seen_tags == ['request-1', 'request-2']
2530+
await handler.aclose()

0 commit comments

Comments
 (0)