Skip to content

Commit c1b5d75

Browse files
committed
test(message_history): pin the async write, drop and lifecycle paths
Seed the other session through astore, block every sync round trip by patching execute_command and pipeline on the sync client, and pin a tool message's tool_call_id and metadata through an async write, adrop(id), the reused async index and adisconnect closing the converted client. adrop now goes through AsyncSearchIndex.drop_keys rather than the private client, and the connection caveat drops SSL and unix sockets, which convert since #792.
1 parent 93657d7 commit c1b5d75

2 files changed

Lines changed: 47 additions & 17 deletions

File tree

‎redisvl/extensions/message_history/message_history.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ async def adrop(self, id: str | None = None) -> None:
151151
recent = await self.aget_recent(top_k=1, raw=True)
152152
id = recent[0][ID_FIELD_NAME] # type: ignore
153153

154-
client = await aindex._get_client()
155-
await client.delete(aindex.key(id))
154+
await aindex.drop_keys(aindex.key(id))
156155

157156
def _count_query(self, session_tag: str | None) -> CountQuery:
158157
return CountQuery(
@@ -249,8 +248,8 @@ async def aget_recent(
249248
:class:`~redisvl.index.AsyncSearchIndex`, without blocking the loop.
250249
251250
Pass ``redis_url`` rather than a client to the history if you need the
252-
async methods over SSL, a unix socket, Sentinel or Redis Cluster: those
253-
clients cannot be converted to their async form. The async client is
251+
async methods over Sentinel or Redis Cluster: those clients cannot be
252+
converted to their async form. The async client is
254253
bound to the event loop that made the first call, so call
255254
:meth:`adisconnect` before switching loops.
256255
"""

‎tests/integration/test_message_history.py‎

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

9891020
async def test_async_message_history_with_redis_url(app_name, redis_url):

0 commit comments

Comments
 (0)