Skip to content

Build and run agents you can see, understand and trust.

fix(message-bus): trim the redis replay log exactly when a cursor is given (#3409)

ZhenhaoFuf89837ef561a
diff --git a/src/agentscope/app/message_bus/_redis_message_bus.py b/src/agentscope/app/message_bus/_redis_message_bus.py
index 4990754ab..475b96c8c 100644
--- a/src/agentscope/app/message_bus/_redis_message_bus.py
+++ b/src/agentscope/app/message_bus/_redis_message_bus.py
@@ -476,8 +476,12 @@ class RedisMessageBus(MessageBus): # pylint: disable=too-many-public-methods
if before_id is None:
await self._client.delete(key)
return
- # XTRIM MINID drops entries with id < before_id.
- await self._client.xtrim(key, minid=before_id)
+ # Approximate trimming can leave entries older than the cursor.
+ await self._client.xtrim(
+ key,
+ minid=before_id,
+ approximate=False,
+ )
# ------------------------------------------------------------------
# Mode F — registry map (hash-keyed namespace)
diff --git a/tests/service_message_bus_test.py b/tests/service_message_bus_test.py
index a1f755996..5bdb04e80 100644
--- a/tests/service_message_bus_test.py
+++ b/tests/service_message_bus_test.py
@@ -245,6 +245,17 @@ class TestLogPrimitive(IsolatedAsyncioTestCase):
await self.bus.log_trim("k")
self.assertEqual(await self.bus.log_read("k"), [])
+ async def test_trim_with_before_id_keeps_newer(self) -> None:
+ """Trimming keeps the cursor entry and all newer entries."""
+ await self.bus.log_append("k", {"i": 1})
+ id2 = await self.bus.log_append("k", {"i": 2})
+ await self.bus.log_append("k", {"i": 3})
+ with patch.object(self.fr, "xtrim", wraps=self.fr.xtrim) as xtrim:
+ await self.bus.log_trim("k", before_id=id2)
+ xtrim.assert_called_once_with("k", minid=id2, approximate=False)
+ entries = await self.bus.log_read("k")
+ self.assertEqual([p["i"] for _id, p in entries], [2, 3])
+
class TestPubSubPrimitive(IsolatedAsyncioTestCase):
"""Mode D — transient broadcast: publish / subscribe."""