|
46 | 46 | from google.adk.a2a.agent.utils import execute_after_request_interceptors |
47 | 47 | from google.adk.a2a.agent.utils import execute_before_card_request_interceptors |
48 | 48 | from google.adk.a2a.agent.utils import execute_before_request_interceptors |
| 49 | +from google.adk.a2a.converters import event_converter |
| 50 | +from google.adk.a2a.converters.from_adk_event import convert_event_to_a2a_events |
49 | 51 | from google.adk.a2a.converters.part_converter import convert_genai_part_to_a2a_part |
| 52 | +from google.adk.a2a.converters.utils import _get_adk_metadata_key |
50 | 53 | from google.adk.agents.invocation_context import InvocationContext |
51 | 54 | from google.adk.agents.llm.task._finish_task_tool import FINISH_TASK_ERROR_RESULT |
52 | 55 | from google.adk.agents.llm.task._finish_task_tool import FINISH_TASK_SUCCESS_RESULT |
|
59 | 62 | from google.adk.auth.auth_credential import OAuth2Auth |
60 | 63 | from google.adk.auth.auth_preprocessor import TOOLSET_AUTH_CREDENTIAL_ID_PREFIX |
61 | 64 | from google.adk.events.event import Event |
| 65 | +from google.adk.events.event_actions import EventActions |
62 | 66 | from google.adk.flows.llm_flows.context._fencing import QUOTED_CONTENT_BEGIN |
63 | 67 | from google.adk.flows.llm_flows.context._fencing import QUOTED_CONTENT_END |
64 | 68 | from google.adk.flows.llm_flows.tools._functions import REQUEST_EUC_FUNCTION_CALL_NAME |
@@ -1600,6 +1604,24 @@ def test_construct_message_parts_from_session_user_input_metadata(self): |
1600 | 1604 | assert len(parts) == 1 |
1601 | 1605 | assert _compat.part_metadata(parts[0]).get("is_user_input") is True |
1602 | 1606 |
|
| 1607 | + def test_construct_message_parts_warns_for_state_only_handoff(self, caplog): |
| 1608 | + """A state-only hand-off warns before the remote receives stale content.""" |
| 1609 | + self.mock_session.events = [ |
| 1610 | + Event( |
| 1611 | + author="local_agent", |
| 1612 | + actions=EventActions(state_delta={"routing": "priority"}), |
| 1613 | + ) |
| 1614 | + ] |
| 1615 | + |
| 1616 | + with caplog.at_level("WARNING"): |
| 1617 | + parts, context_id = self.agent._construct_message_parts_from_session( |
| 1618 | + self.mock_context |
| 1619 | + ) |
| 1620 | + |
| 1621 | + assert parts == [] |
| 1622 | + assert context_id is None |
| 1623 | + assert "cannot forward the preceding state-only event" in caplog.text |
| 1624 | + |
1603 | 1625 | def test_construct_message_parts_from_session_success_multiple_parts(self): |
1604 | 1626 | """Test successful message parts construction from session.""" |
1605 | 1627 | # Mock event with text content |
@@ -3182,6 +3204,51 @@ def test_construct_message_parts_from_session_non_foreign_fr_not_converted_when_ |
3182 | 3204 | assert parts[0] == mock_a2a_part |
3183 | 3205 | self.mock_genai_part_converter.assert_called_once_with(user_part) |
3184 | 3206 |
|
| 3207 | + def test_task_mode_state_warning_uses_latest_applicable_event(self, caplog): |
| 3208 | + """An unrelated state-only event outside the task scope does not warn.""" |
| 3209 | + task_scope = self.mock_context.isolation_scope |
| 3210 | + self.mock_genai_part_converter.return_value = _compat.make_text_part( |
| 3211 | + "converted" |
| 3212 | + ) |
| 3213 | + trigger = _make_dummy_task_trigger_event(task_scope, self.agent.name) |
| 3214 | + applicable = Event( |
| 3215 | + author="user", |
| 3216 | + isolation_scope=task_scope, |
| 3217 | + content=genai_types.Content(parts=[genai_types.Part(text="hello")]), |
| 3218 | + ) |
| 3219 | + unrelated_state_only = Event( |
| 3220 | + author="other", |
| 3221 | + isolation_scope="different-task", |
| 3222 | + actions=EventActions(state_delta={"routing": "priority"}), |
| 3223 | + ) |
| 3224 | + self.mock_session.events = [trigger, applicable, unrelated_state_only] |
| 3225 | + |
| 3226 | + with caplog.at_level("WARNING"): |
| 3227 | + self.agent._construct_message_parts_from_session(self.mock_context) |
| 3228 | + |
| 3229 | + assert "cannot forward the preceding state-only event" not in caplog.text |
| 3230 | + |
| 3231 | + def test_task_mode_state_warning_logs_for_state_only_event_in_scope( |
| 3232 | + self, caplog |
| 3233 | + ): |
| 3234 | + """A state-only event at the end of the task scope logs a warning.""" |
| 3235 | + task_scope = self.mock_context.isolation_scope |
| 3236 | + self.mock_genai_part_converter.return_value = _compat.make_text_part( |
| 3237 | + "converted" |
| 3238 | + ) |
| 3239 | + trigger = _make_dummy_task_trigger_event(task_scope, self.agent.name) |
| 3240 | + state_only_in_scope = Event( |
| 3241 | + author="local_agent", |
| 3242 | + isolation_scope=task_scope, |
| 3243 | + actions=EventActions(state_delta={"routing": "priority"}), |
| 3244 | + ) |
| 3245 | + self.mock_session.events = [trigger, state_only_in_scope] |
| 3246 | + |
| 3247 | + with caplog.at_level("WARNING"): |
| 3248 | + self.agent._construct_message_parts_from_session(self.mock_context) |
| 3249 | + |
| 3250 | + assert "cannot forward the preceding state-only event" in caplog.text |
| 3251 | + |
3185 | 3252 |
|
3186 | 3253 | class TestRemoteA2aAgentStreamingArtifactChunks: |
3187 | 3254 | """Regression tests for chunked artifact streams.""" |
@@ -3239,6 +3306,150 @@ async def test_artifact_update_without_parts_is_ignored(self): |
3239 | 3306 | assert result is None |
3240 | 3307 |
|
3241 | 3308 |
|
| 3309 | +_PEER_STATE_DELTA_WARNING = ( |
| 3310 | + "Ignoring a session state delta from a remote A2A peer" |
| 3311 | +) |
| 3312 | + |
| 3313 | + |
| 3314 | +class TestRemoteA2aAgentPeerStateDelta: |
| 3315 | + """A remote peer's state delta is never applied, but dropping it is logged.""" |
| 3316 | + |
| 3317 | + def setup_method(self): |
| 3318 | + """Setup test fixtures.""" |
| 3319 | + self.agent = RemoteA2aAgent( |
| 3320 | + name="test_agent", |
| 3321 | + agent_card=create_test_agent_card(), |
| 3322 | + ) |
| 3323 | + self.mock_context = Mock(spec=InvocationContext) |
| 3324 | + self.mock_context.invocation_id = "invocation-123" |
| 3325 | + self.mock_context.branch = "main" |
| 3326 | + |
| 3327 | + def _remote_event(self, state_delta): |
| 3328 | + """Build the final event of a remote LlmAgent(output_key="findings").""" |
| 3329 | + return Event( |
| 3330 | + author="remote_agent", |
| 3331 | + invocation_id="remote-invocation", |
| 3332 | + content=genai_types.Content( |
| 3333 | + role="model", parts=[genai_types.Part(text="the findings")] |
| 3334 | + ), |
| 3335 | + actions=EventActions(state_delta=state_delta), |
| 3336 | + ) |
| 3337 | + |
| 3338 | + def _remote_invocation_context(self): |
| 3339 | + remote_context = Mock(spec=InvocationContext) |
| 3340 | + remote_context.app_name = "remote_app" |
| 3341 | + remote_context.user_id = "user" |
| 3342 | + remote_context.session = Mock(spec=Session) |
| 3343 | + remote_context.session.id = "remote-session" |
| 3344 | + return remote_context |
| 3345 | + |
| 3346 | + def _working_task(self): |
| 3347 | + return _compat.make_task( |
| 3348 | + id="task-123", |
| 3349 | + status=_compat.make_task_status(_compat.TS_WORKING), |
| 3350 | + context_id="context-123", |
| 3351 | + ) |
| 3352 | + |
| 3353 | + async def _round_trip_legacy(self, state_delta): |
| 3354 | + """Send a remote event through the default (legacy) server and client.""" |
| 3355 | + updates = event_converter.convert_event_to_a2a_events( |
| 3356 | + self._remote_event(state_delta), |
| 3357 | + self._remote_invocation_context(), |
| 3358 | + "task-123", |
| 3359 | + "context-123", |
| 3360 | + ) |
| 3361 | + return [ |
| 3362 | + await self.agent._handle_a2a_response( |
| 3363 | + (self._working_task(), update), self.mock_context |
| 3364 | + ) |
| 3365 | + for update in updates |
| 3366 | + ] |
| 3367 | + |
| 3368 | + @pytest.mark.asyncio |
| 3369 | + async def test_legacy_round_trip_warns_on_peer_state_delta(self, caplog): |
| 3370 | + """The default client path logs a remote output_key it cannot apply.""" |
| 3371 | + with caplog.at_level("WARNING"): |
| 3372 | + events = await self._round_trip_legacy({"findings": "the findings"}) |
| 3373 | + |
| 3374 | + assert _PEER_STATE_DELTA_WARNING in caplog.text |
| 3375 | + assert [event.content.parts[0].text for event in events] == ["the findings"] |
| 3376 | + assert all(event.actions.state_delta == {} for event in events) |
| 3377 | + |
| 3378 | + @pytest.mark.asyncio |
| 3379 | + async def test_legacy_round_trip_without_state_delta_does_not_warn( |
| 3380 | + self, caplog |
| 3381 | + ): |
| 3382 | + """The legacy server sends stateDelta: {} on every event; ignore it.""" |
| 3383 | + with caplog.at_level("WARNING"): |
| 3384 | + await self._round_trip_legacy({}) |
| 3385 | + |
| 3386 | + assert _PEER_STATE_DELTA_WARNING not in caplog.text |
| 3387 | + |
| 3388 | + @pytest.mark.asyncio |
| 3389 | + async def test_legacy_full_task_warns_on_peer_state_delta(self, caplog): |
| 3390 | + """A non-streaming client sees the delta merged into the task metadata.""" |
| 3391 | + task = _compat.make_task( |
| 3392 | + id="task-123", |
| 3393 | + status=_compat.make_task_status(_compat.TS_COMPLETED), |
| 3394 | + context_id="context-123", |
| 3395 | + metadata={ |
| 3396 | + _get_adk_metadata_key("actions"): { |
| 3397 | + "stateDelta": {"findings": "the findings"} |
| 3398 | + } |
| 3399 | + }, |
| 3400 | + ) |
| 3401 | + |
| 3402 | + with caplog.at_level("WARNING"): |
| 3403 | + await self.agent._handle_a2a_response((task, None), self.mock_context) |
| 3404 | + |
| 3405 | + assert _PEER_STATE_DELTA_WARNING in caplog.text |
| 3406 | + |
| 3407 | + @pytest.mark.asyncio |
| 3408 | + async def test_legacy_message_warns_on_peer_state_delta(self, caplog): |
| 3409 | + """A direct message response carrying a state delta also warns.""" |
| 3410 | + message = _compat.make_message( |
| 3411 | + message_id="msg-1", |
| 3412 | + role=_compat.ROLE_AGENT, |
| 3413 | + parts=[_compat.make_text_part("the findings")], |
| 3414 | + metadata={ |
| 3415 | + _get_adk_metadata_key("actions"): { |
| 3416 | + "stateDelta": {"findings": "the findings"} |
| 3417 | + } |
| 3418 | + }, |
| 3419 | + ) |
| 3420 | + |
| 3421 | + with caplog.at_level("WARNING"): |
| 3422 | + event = await self.agent._handle_a2a_response(message, self.mock_context) |
| 3423 | + |
| 3424 | + assert _PEER_STATE_DELTA_WARNING in caplog.text |
| 3425 | + assert event.actions.state_delta == {} |
| 3426 | + |
| 3427 | + @pytest.mark.asyncio |
| 3428 | + async def test_new_integration_round_trip_warns_on_peer_state_delta( |
| 3429 | + self, caplog |
| 3430 | + ): |
| 3431 | + """The new integration path logs a remote output_key it cannot apply.""" |
| 3432 | + updates = convert_event_to_a2a_events( |
| 3433 | + self._remote_event({"findings": "the findings"}), |
| 3434 | + {}, |
| 3435 | + "task-123", |
| 3436 | + "context-123", |
| 3437 | + ) |
| 3438 | + |
| 3439 | + with caplog.at_level("WARNING"): |
| 3440 | + events = [ |
| 3441 | + await self.agent._handle_a2a_response_v2( |
| 3442 | + (self._working_task(), update), self.mock_context |
| 3443 | + ) |
| 3444 | + for update in updates |
| 3445 | + ] |
| 3446 | + |
| 3447 | + assert _PEER_STATE_DELTA_WARNING in caplog.text |
| 3448 | + events = [event for event in events if event] |
| 3449 | + assert [event.content.parts[0].text for event in events] == ["the findings"] |
| 3450 | + assert all(event.actions.state_delta == {} for event in events) |
| 3451 | + |
| 3452 | + |
3242 | 3453 | class TestRemoteA2aAgentMessageHandlingFromFactory: |
3243 | 3454 | """Test message handling functionality.""" |
3244 | 3455 |
|
|
0 commit comments