@@ -735,6 +735,71 @@ async def test_handle_a2a_response_with_task_completed_and_no_update(self):
735735 assert A2A_METADATA_PREFIX + "task_id" in result .custom_metadata
736736 assert A2A_METADATA_PREFIX + "context_id" in result .custom_metadata
737737
738+ def test_construct_message_parts_from_session_preserves_order (self ):
739+ """Test that message parts are in correct order with multi-part messages.
740+
741+ This test verifies the fix for the bug where _present_other_agent_message
742+ creates multi-part messages with "For context:" prefix, and ensures the
743+ parts are in the correct chronological order (not reversed).
744+ """
745+ # Create mock events with multiple parts
746+ # Event 1: User message
747+ user_part = Mock ()
748+ user_part .text = "User question"
749+ user_content = Mock ()
750+ user_content .parts = [user_part ]
751+ user_event = Mock ()
752+ user_event .content = user_content
753+ user_event .author = "user"
754+
755+ # Event 2: Other agent message (will be transformed by
756+ # _present_other_agent_message)
757+ other_agent_part1 = Mock ()
758+ other_agent_part1 .text = "For context:"
759+ other_agent_part2 = Mock ()
760+ other_agent_part2 .text = "[other_agent] said: Response text"
761+ other_agent_content = Mock ()
762+ other_agent_content .parts = [other_agent_part1 , other_agent_part2 ]
763+ other_agent_event = Mock ()
764+ other_agent_event .content = other_agent_content
765+ other_agent_event .author = "other_agent"
766+
767+ self .mock_session .events = [user_event , other_agent_event ]
768+
769+ with patch (
770+ "google.adk.agents.remote_a2a_agent._present_other_agent_message"
771+ ) as mock_present :
772+ # Mock _present_other_agent_message to return the transformed event
773+ mock_present .return_value = other_agent_event
774+
775+ # Mock the converter to track the order of parts
776+ converted_parts = []
777+
778+ def mock_converter (part ):
779+ mock_a2a_part = Mock ()
780+ mock_a2a_part .original_text = part .text
781+ converted_parts .append (mock_a2a_part )
782+ return mock_a2a_part
783+
784+ self .mock_genai_part_converter .side_effect = mock_converter
785+
786+ result = self .agent ._construct_message_parts_from_session (
787+ self .mock_context
788+ )
789+
790+ # Verify the parts are in correct order
791+ assert len (result ) == 2 # Returns tuple of (parts, context_id)
792+ assert len (result [0 ]) == 3 # 1 user part + 2 other agent parts
793+ assert result [1 ] is None # context_id
794+
795+ # Verify order: user part, then "For context:", then agent message
796+ assert converted_parts [0 ].original_text == "User question"
797+ assert converted_parts [1 ].original_text == "For context:"
798+ assert (
799+ converted_parts [2 ].original_text
800+ == "[other_agent] said: Response text"
801+ )
802+
738803 @pytest .mark .asyncio
739804 async def test_handle_a2a_response_with_task_submitted_and_no_update (self ):
740805 """Test successful A2A response handling with streaming task and no update."""
0 commit comments