Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mesa_llm/memory/st_lt_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def _process_step_core(self, pre_step: bool):
> self.capacity + (self.consolidation_capacity or 0)
and self.consolidation_capacity
):
self.short_term_memory.popleft()
# Defer popleft() to the caller so _build_consolidation_prompt can see it.
should_consolidate = True

elif (
Expand All @@ -155,6 +155,7 @@ def process_step(self, pre_step: bool = False):

if should_consolidate:
self._update_long_term_memory()
self.short_term_memory.popleft()

if new_entry and self.display:
new_entry.display()
Expand All @@ -167,6 +168,7 @@ async def aprocess_step(self, pre_step: bool = False):

if should_consolidate:
await self._aupdate_long_term_memory()
self.short_term_memory.popleft()

if new_entry and self.display:
new_entry.display()
Expand Down
36 changes: 36 additions & 0 deletions tests/test_memory/test_STLT_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,39 @@ def test_get_prompt_ready_returns_str_when_empty(self, mock_agent):
)
assert "Short term memory:" in result
assert "Long term memory:" in result

def test_memory_does_not_drop_oldest_during_consolidation(
self, mock_agent, mock_llm, llm_response_factory
):
"""
Verify that the oldest entry in short-term memory is still present when
building the consolidation prompt. Fixes Issue #186.
"""
mock_llm.generate.return_value = llm_response_factory(
"Consolidated memory summary"
)

memory = STLTMemory(
agent=mock_agent,
short_term_capacity=2,
consolidation_capacity=1,
llm_model="provider/test_model",
)
memory.llm = mock_llm

# Fill to capacity + consolidation_capacity + 1 to trigger consolidation
with patch("rich.console.Console"):
for i in range(4):
memory.step_content = {"observation": f"critical event at step {i}"}
memory.process_step(pre_step=True)
mock_agent.model.steps = i + 1
memory.step_content = {"action": f"response to step {i}"}
memory.process_step()

# Get the arguments passed to llm.generate during consolidation
call_args = mock_llm.generate.call_args[0][0]

# The oldest entry MUST be in the prompt sent to the LLM
assert "critical event at step 0" in call_args, (
"Oldest memory entry was silently dropped before consolidation!"
)