[BUG] ChatHistoryMemory misses window truncation warning with preserved system/developer message
Required prerequisites
- I searched the existing open issues and pull requests for similar reports.
- I reproduced this on current
master.
What happened?
ChatHistoryMemory.retrieve() warns when the chat history window is reached, but the current guard only checks len(records) == window_size after retrieval.
ChatHistoryBlock.retrieve() preserves the first SYSTEM/DEVELOPER message as a protected prefix and then applies window_size to the remaining sliding messages. When a protected first message exists, the returned context can have window_size + 1 records even though earlier sliding messages were dropped. In that case, no warning is emitted.
Minimal reproduction
import warnings
from unittest.mock import MagicMock
from camel.memories import BaseContextCreator, ChatHistoryMemory, MemoryRecord
from camel.messages import BaseMessage
from camel.types import OpenAIBackendRole, RoleType
memory = ChatHistoryMemory(
context_creator=MagicMock(spec=BaseContextCreator),
window_size=2,
)
records = [
MemoryRecord(
message=BaseMessage("system", RoleType.DEFAULT, None, "System prompt"),
role_at_backend=OpenAIBackendRole.SYSTEM,
),
*[
MemoryRecord(
message=BaseMessage("user", RoleType.USER, None, f"User {i}"),
role_at_backend=OpenAIBackendRole.USER,
)
for i in range(3)
],
]
memory.write_records(records)
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
retrieved = memory.retrieve()
print([r.memory_record.message.content for r in retrieved])
print(len(caught))Current output:
['System prompt', 'User 1', 'User 2']
0Expected behavior
A UserWarning should be emitted because User 0 was dropped from the sliding chat history window. The warning should not be emitted when the protected prefix plus sliding messages exactly fit within the configured window.
Suggested direction
Make the warning condition match ChatHistoryBlock.retrieve() semantics: account for the protected first SYSTEM/DEVELOPER message, and warn only when the number of non-protected sliding messages exceeds window_size.
I can work on a small PR for this.
Source: camel-ai/camel