|
| 1 | +import httpx |
| 2 | +import pytest |
| 3 | +from typing import NamedTuple |
| 4 | + |
| 5 | +from a2a.client.client import Client, ClientConfig |
| 6 | +from a2a.client.client_factory import ClientFactory |
| 7 | +from a2a.server.agent_execution import AgentExecutor, RequestContext |
| 8 | +from a2a.server.apps import A2AFastAPIApplication |
| 9 | +from a2a.server.events import EventQueue |
| 10 | +from a2a.server.events.in_memory_queue_manager import InMemoryQueueManager |
| 11 | +from a2a.server.request_handlers import DefaultRequestHandler |
| 12 | +from a2a.server.tasks import TaskUpdater |
| 13 | +from a2a.server.tasks.inmemory_task_store import InMemoryTaskStore |
| 14 | +from a2a.types import ( |
| 15 | + AgentCapabilities, |
| 16 | + AgentCard, |
| 17 | + AgentInterface, |
| 18 | + Artifact, |
| 19 | + GetTaskRequest, |
| 20 | + Message, |
| 21 | + Part, |
| 22 | + Role, |
| 23 | + SendMessageRequest, |
| 24 | + TaskState, |
| 25 | +) |
| 26 | +from a2a.utils import TransportProtocol |
| 27 | + |
| 28 | + |
| 29 | +class MockMutatingAgentExecutor(AgentExecutor): |
| 30 | + async def execute(self, context: RequestContext, event_queue: EventQueue): |
| 31 | + assert context.task_id is not None |
| 32 | + assert context.context_id is not None |
| 33 | + task_updater = TaskUpdater( |
| 34 | + event_queue, |
| 35 | + context.task_id, |
| 36 | + context.context_id, |
| 37 | + ) |
| 38 | + |
| 39 | + user_input = context.get_user_input() |
| 40 | + |
| 41 | + if user_input == 'Init task': |
| 42 | + # Explicitly save status change to ensure task exists with some state |
| 43 | + await task_updater.update_status( |
| 44 | + TaskState.TASK_STATE_WORKING, |
| 45 | + message=task_updater.new_agent_message( |
| 46 | + [Part(text='task working')] |
| 47 | + ), |
| 48 | + ) |
| 49 | + else: |
| 50 | + # Mutate the task WITHOUT saving it properly |
| 51 | + context.current_task.artifacts.append( |
| 52 | + Artifact( |
| 53 | + name='leaked-artifact', |
| 54 | + parts=[Part(text='leaked artifact')], |
| 55 | + ) |
| 56 | + ) |
| 57 | + |
| 58 | + async def cancel(self, context: RequestContext, event_queue: EventQueue): |
| 59 | + raise NotImplementedError('Cancellation is not supported') |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture |
| 63 | +def agent_card() -> AgentCard: |
| 64 | + return AgentCard( |
| 65 | + name='Mutating Agent', |
| 66 | + description='Real in-memory integration testing.', |
| 67 | + version='1.0.0', |
| 68 | + capabilities=AgentCapabilities( |
| 69 | + streaming=True, push_notifications=False |
| 70 | + ), |
| 71 | + skills=[], |
| 72 | + default_input_modes=['text/plain'], |
| 73 | + default_output_modes=['text/plain'], |
| 74 | + supported_interfaces=[ |
| 75 | + AgentInterface( |
| 76 | + protocol_binding=TransportProtocol.JSONRPC, |
| 77 | + url='http://testserver', |
| 78 | + ), |
| 79 | + ], |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +class ClientSetup(NamedTuple): |
| 84 | + client: Client |
| 85 | + task_store: InMemoryTaskStore |
| 86 | + use_copying: bool |
| 87 | + |
| 88 | + |
| 89 | +def setup_client(agent_card: AgentCard, use_copying: bool) -> ClientSetup: |
| 90 | + task_store = InMemoryTaskStore(use_copying=use_copying) |
| 91 | + handler = DefaultRequestHandler( |
| 92 | + agent_executor=MockMutatingAgentExecutor(), |
| 93 | + task_store=task_store, |
| 94 | + queue_manager=InMemoryQueueManager(), |
| 95 | + ) |
| 96 | + app_builder = A2AFastAPIApplication( |
| 97 | + agent_card, handler, extended_agent_card=agent_card |
| 98 | + ) |
| 99 | + app = app_builder.build() |
| 100 | + httpx_client = httpx.AsyncClient( |
| 101 | + transport=httpx.ASGITransport(app=app), base_url='http://testserver' |
| 102 | + ) |
| 103 | + factory = ClientFactory( |
| 104 | + config=ClientConfig( |
| 105 | + httpx_client=httpx_client, |
| 106 | + supported_protocol_bindings=[TransportProtocol.JSONRPC], |
| 107 | + ) |
| 108 | + ) |
| 109 | + client = factory.create(agent_card) |
| 110 | + return ClientSetup( |
| 111 | + client=client, |
| 112 | + task_store=task_store, |
| 113 | + use_copying=use_copying, |
| 114 | + ) |
| 115 | + |
| 116 | + |
| 117 | +@pytest.mark.asyncio |
| 118 | +@pytest.mark.parametrize('use_copying', [True, False]) |
| 119 | +async def test_mutation_observability(agent_card: AgentCard, use_copying: bool): |
| 120 | + """Tests that task mutations are observable when copying is disabled. |
| 121 | +
|
| 122 | + When copying is disabled, the agent mutates the task in-place and the |
| 123 | + changes are observable by the client. When copying is enabled, the agent |
| 124 | + mutates a copy of the task and the changes are not observable by the client. |
| 125 | +
|
| 126 | + It is ok to remove the `use_copying` parameter from the system in the future |
| 127 | + to make InMemoryTaskStore consistent with other task stores. |
| 128 | + """ |
| 129 | + client_setup = setup_client(agent_card, use_copying) |
| 130 | + client = client_setup.client |
| 131 | + |
| 132 | + # 1. First message to create the task |
| 133 | + message_to_send = Message( |
| 134 | + role=Role.ROLE_USER, |
| 135 | + message_id='msg-mut-init', |
| 136 | + parts=[Part(text='Init task')], |
| 137 | + ) |
| 138 | + |
| 139 | + events = [ |
| 140 | + event |
| 141 | + async for event in client.send_message( |
| 142 | + request=SendMessageRequest(message=message_to_send) |
| 143 | + ) |
| 144 | + ] |
| 145 | + |
| 146 | + task = events[-1][1] |
| 147 | + assert task is not None |
| 148 | + task_id = task.id |
| 149 | + |
| 150 | + # 2. Second message to mutate it |
| 151 | + message_to_send_2 = Message( |
| 152 | + role=Role.ROLE_USER, |
| 153 | + message_id='msg-mut-do', |
| 154 | + task_id=task_id, |
| 155 | + parts=[Part(text='Update task without saving it')], |
| 156 | + ) |
| 157 | + |
| 158 | + _ = [ |
| 159 | + event |
| 160 | + async for event in client.send_message( |
| 161 | + request=SendMessageRequest(message=message_to_send_2) |
| 162 | + ) |
| 163 | + ] |
| 164 | + |
| 165 | + # 3. Get task via client |
| 166 | + retrieved_task = await client.get_task(request=GetTaskRequest(id=task_id)) |
| 167 | + |
| 168 | + # 4. Assert behavior based on `use_copying` |
| 169 | + if use_copying: |
| 170 | + # The un-saved artifact IS NOT leaked to the client |
| 171 | + assert len(retrieved_task.artifacts) == 0 |
| 172 | + else: |
| 173 | + # The un-saved artifact IS leaked to the client |
| 174 | + assert len(retrieved_task.artifacts) == 1 |
| 175 | + assert retrieved_task.artifacts[0].name == 'leaked-artifact' |
0 commit comments