Skip to content

Commit a719dac

Browse files
authored
Merge pull request #157 from Harsh-617/fix-send-message-json
fix: store unique_ids instead of Agent objects in send_message memory…
2 parents 8db52c1 + 0f35684 commit a719dac

File tree

2 files changed

+96
-4
lines changed

2 files changed

+96
-4
lines changed

mesa_llm/llm_agent.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ async def asend_message(self, message: str, recipients: list[Agent]) -> str:
267267
type="message",
268268
content={
269269
"message": message,
270-
"sender": self,
271-
"recipients": recipients,
270+
"sender": self.unique_id,
271+
"recipients": [r.unique_id for r in recipients],
272272
},
273273
)
274274

@@ -283,8 +283,8 @@ def send_message(self, message: str, recipients: list[Agent]) -> str:
283283
type="message",
284284
content={
285285
"message": message,
286-
"sender": self,
287-
"recipients": recipients,
286+
"sender": self.unique_id,
287+
"recipients": [r.unique_id for r in recipients],
288288
},
289289
)
290290

tests/test_llm_agent.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# tests/test_llm_agent.py
22

3+
import json
34
import re
45

56
import pytest
@@ -591,3 +592,94 @@ def __init__(self):
591592
obs = agent.generate_obs()
592593

593594
assert len(obs.local_state) == 0
595+
596+
597+
# ---------------------------------------------------------------------------
598+
# send_message / asend_message - store unique_ids, not Agent objects (#156)
599+
# ---------------------------------------------------------------------------
600+
601+
602+
def _make_send_message_model(monkeypatch):
603+
"""Shared setup: two-agent MultiGrid model with ShortTermMemory."""
604+
monkeypatch.setenv("GEMINI_API_KEY", "dummy")
605+
606+
class DummyModel(Model):
607+
def __init__(self):
608+
super().__init__(seed=45)
609+
self.grid = MultiGrid(3, 3, torus=False)
610+
611+
def add_agent(self, pos):
612+
agents = LLMAgent.create_agents(
613+
self,
614+
n=1,
615+
reasoning=lambda agent: None,
616+
system_prompt="Test",
617+
vision=-1,
618+
internal_state=[],
619+
)
620+
agent = agents.to_list()[0]
621+
self.grid.place_agent(agent, pos)
622+
return agent
623+
624+
model = DummyModel()
625+
626+
sender = model.add_agent((0, 0))
627+
sender.memory = ShortTermMemory(agent=sender, n=5, display=True)
628+
sender.unique_id = 10
629+
630+
recipient = model.add_agent((1, 1))
631+
recipient.memory = ShortTermMemory(agent=recipient, n=5, display=True)
632+
recipient.unique_id = 20
633+
634+
return sender, recipient
635+
636+
637+
def test_send_message_stores_serializable_ids(monkeypatch):
638+
"""send_message stores sender/recipients as unique_ids, not Agent objects."""
639+
sender, recipient = _make_send_message_model(monkeypatch)
640+
641+
captured = {}
642+
643+
def capture_content(type, content):
644+
captured.update(content)
645+
646+
monkeypatch.setattr(recipient.memory, "add_to_memory", capture_content)
647+
monkeypatch.setattr(sender.memory, "add_to_memory", lambda *a, **kw: None)
648+
649+
sender.send_message("hello", recipients=[recipient])
650+
651+
assert captured["sender"] == 10
652+
assert captured["recipients"] == [20]
653+
assert captured["message"] == "hello"
654+
655+
# Must not raise TypeError when serializing
656+
data = json.loads(json.dumps(captured))
657+
assert data["sender"] == 10
658+
assert data["recipients"] == [20]
659+
660+
661+
@pytest.mark.asyncio
662+
async def test_asend_message_stores_serializable_ids(monkeypatch):
663+
"""asend_message stores sender/recipients as unique_ids, not Agent objects."""
664+
sender, recipient = _make_send_message_model(monkeypatch)
665+
666+
captured = {}
667+
668+
async def capture_content(type, content):
669+
captured.update(content)
670+
671+
async def noop(*a, **kw):
672+
pass
673+
674+
monkeypatch.setattr(recipient.memory, "aadd_to_memory", capture_content)
675+
monkeypatch.setattr(sender.memory, "aadd_to_memory", noop)
676+
677+
await sender.asend_message("hello", recipients=[recipient])
678+
679+
assert captured["sender"] == 10
680+
assert captured["recipients"] == [20]
681+
assert captured["message"] == "hello"
682+
683+
data = json.loads(json.dumps(captured))
684+
assert data["sender"] == 10
685+
assert data["recipients"] == [20]

0 commit comments

Comments
 (0)