@@ -939,15 +939,17 @@ async def test_async_message_history_matches_sync(standard_history, monkeypatch)
939939 "count" : standard_history .count (),
940940 }
941941 other_tag = "other_session"
942- standard_history .store ("q" , "a" , session_tag = other_tag )
943942
944943 def blocking (* args , ** kwargs ):
945944 raise AssertionError ("the async path made a blocking call" )
946945
947- # The async methods must read and write through the async index only.
948- monkeypatch .setattr (standard_history ._index , "query" , blocking )
949- monkeypatch .setattr (standard_history ._index , "load" , blocking )
950- monkeypatch .setattr (standard_history ._index , "clear" , blocking )
946+ # Every sync round trip goes through execute_command or a pipeline, so
947+ # blocking both on the sync client catches any sync call the async
948+ # methods make.
949+ sync_client = standard_history ._index ._redis_client
950+ monkeypatch .setattr (sync_client , "execute_command" , blocking )
951+ monkeypatch .setattr (sync_client , "pipeline" , blocking )
952+ aindex = None
951953 try :
952954 actual = {
953955 "recent" : await standard_history .aget_recent (top_k = 4 ),
@@ -961,29 +963,58 @@ def blocking(*args, **kwargs):
961963 "count" : await standard_history .acount (),
962964 }
963965 assert actual == expected
966+ aindex = standard_history .aindex
967+ assert aindex is not None
968+
969+ # astore must tag its entries with the session it is given.
970+ await standard_history .astore ("q" , "a" , session_tag = other_tag )
964971 assert await standard_history .acount (session_tag = other_tag ) == 2
965972
966- # Writes land where the sync reader would find them, and adrop with no
967- # id removes the latest entry .
973+ # Writes land where the sync reader would find them, and an async
974+ # write keeps tool_call_id and metadata .
968975 await standard_history .astore ("async prompt" , "async response" )
976+ tool_message = {
977+ "role" : "tool" ,
978+ "content" : "async lookup" ,
979+ "tool_call_id" : "t2" ,
980+ "metadata" : {"params" : "abc 789" },
981+ }
982+ await standard_history .aadd_message (tool_message )
983+ assert await standard_history .aget_recent (top_k = 1 ) == [tool_message ]
969984 await standard_history .aadd_message ({"role" : "user" , "content" : "last" })
970- assert await standard_history .acount () == expected ["count" ] + 3
971- assert (await standard_history .aget_recent (top_k = 1 )) == [
972- {"role" : "user" , "content" : "last" }
973- ]
985+ assert await standard_history .acount () == expected ["count" ] + 4
986+
987+ # adrop with no id removes the latest entry ...
974988 await standard_history .adrop ()
975- assert (await standard_history .aget_recent (top_k = 1 )) == [
976- {"role" : "llm" , "content" : "async response" }
989+ assert await standard_history .aget_recent (top_k = 1 ) == [tool_message ]
990+ # ... and adrop(id) removes that entry, not the latest.
991+ prompt_id = next (
992+ m [ID_FIELD_NAME ]
993+ for m in await standard_history .aget_recent (top_k = 3 , raw = True )
994+ if m ["content" ] == "async prompt"
995+ )
996+ await standard_history .adrop (prompt_id )
997+ assert await standard_history .aget_recent (top_k = 2 ) == [
998+ {"role" : "llm" , "content" : "async response" },
999+ tool_message ,
9771000 ]
9781001 with pytest .raises (ValueError ):
9791002 await standard_history .aget_recent (top_k = - 1 )
9801003
9811004 await standard_history .aclear ()
9821005 assert await standard_history .acount () == 0
9831006 assert await standard_history .acount (session_tag = other_tag ) == 0
1007+
1008+ # One async index serves every call.
1009+ assert standard_history .aindex is aindex
9841010 finally :
1011+ pool = aindex ._redis_client .connection_pool if aindex else None
9851012 await standard_history .adisconnect ()
9861013 assert standard_history .aindex is None
1014+ # adisconnect closed the converted client's connections.
1015+ assert pool is not None
1016+ connections = pool ._available_connections + list (pool ._in_use_connections )
1017+ assert connections and not any (c .is_connected for c in connections )
9871018
9881019
9891020async def test_async_message_history_with_redis_url (app_name , redis_url ):
0 commit comments