@@ -1206,6 +1206,58 @@ def test_list_messages_with_limit_calculates_max_results(self, session_manager,
12061206 call_kwargs = mock_memory_client .list_events .call_args [1 ]
12071207 assert call_kwargs ["max_results" ] == 550 # limit + offset
12081208
1209+ def test_append_message_handles_none_from_create_message (self , session_manager , test_agent ):
1210+ """Test that append_message gracefully handles None return from create_message."""
1211+ # Create a tool use message (no text content, only toolUse block)
1212+ tool_use_message = {
1213+ "role" : "assistant" ,
1214+ "content" : [
1215+ {
1216+ "toolUse" : {
1217+ "toolUseId" : "tooluse_abc123" ,
1218+ "name" : "calculator" ,
1219+ "input" : {"operation" : "add" , "a" : 5 , "b" : 3 },
1220+ }
1221+ }
1222+ ],
1223+ }
1224+
1225+ # Mock create_message to return None (simulating the behavior for messages with no text)
1226+ session_manager .create_message = Mock (return_value = None )
1227+ session_manager ._latest_agent_message = {}
1228+
1229+ # This should NOT crash - it should handle None gracefully
1230+ session_manager .append_message (tool_use_message , test_agent )
1231+
1232+ # Verify create_message was called
1233+ session_manager .create_message .assert_called_once ()
1234+
1235+ # Verify that _latest_agent_message was NOT updated (since message was skipped)
1236+ assert test_agent .agent_id not in session_manager ._latest_agent_message
1237+
1238+ def test_append_message_normal_message_still_works (self , session_manager , test_agent ):
1239+ """Test that append_message still works correctly for normal messages with text."""
1240+ # Create a normal message with text content
1241+ normal_message = {
1242+ "role" : "assistant" ,
1243+ "content" : [{"text" : "The answer is 8." }],
1244+ }
1245+
1246+ # Mock create_message to return a valid event (normal behavior)
1247+ mock_event = {"eventId" : "event_123456" , "memoryId" : "test-memory" }
1248+ session_manager .create_message = Mock (return_value = mock_event )
1249+ session_manager ._latest_agent_message = {}
1250+
1251+ # This should work normally
1252+ session_manager .append_message (normal_message , test_agent )
1253+
1254+ # Verify create_message was called
1255+ session_manager .create_message .assert_called_once ()
1256+
1257+ # Verify that _latest_agent_message WAS updated
1258+ assert test_agent .agent_id in session_manager ._latest_agent_message
1259+ assert session_manager ._latest_agent_message [test_agent .agent_id ].message_id == "event_123456"
1260+
12091261
12101262class TestBatchingConfig :
12111263 """Test batch_size configuration validation."""
0 commit comments