Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

fix: summarization causing desync for conversations [LET-7014] (#8734)

cthomas487bb4223140
diff --git a/letta/agents/letta_agent_v3.py b/letta/agents/letta_agent_v3.py
index f14456188..bc2d031e7 100644
--- a/letta/agents/letta_agent_v3.py
+++ b/letta/agents/letta_agent_v3.py
@@ -508,6 +508,7 @@ class LettaAgentV3(LettaAgentV2):
)
# Update which messages are in context
+ # Note: update_in_context_messages also updates positions to preserve order
await ConversationManager().update_in_context_messages(
conversation_id=self.conversation_id,
in_context_message_ids=[m.id for m in in_context_messages],
diff --git a/letta/services/conversation_manager.py b/letta/services/conversation_manager.py
index efe7c5f3f..db8e7e73a 100644
--- a/letta/services/conversation_manager.py
+++ b/letta/services/conversation_manager.py
@@ -302,10 +302,15 @@ class ConversationManager:
Update which messages are in context for a conversation.
Sets in_context=True for messages in the list, False for others.
+ Also updates positions to preserve the order specified in in_context_message_ids.
+
+ This is critical for correctness: when summarization inserts a summary message
+ that needs to appear before an approval request, the positions must reflect
+ the intended order, not the insertion order.
Args:
conversation_id: The conversation to update
- in_context_message_ids: List of message IDs that should be in context
+ in_context_message_ids: List of message IDs in the desired order
actor: The user performing the action
"""
async with db_registry.async_session() as session:
@@ -318,11 +323,20 @@ class ConversationManager:
result = await session.execute(query)
conv_messages = result.scalars().all()
- # Update in_context status
+ # Build lookup dict
+ conv_msg_dict = {cm.message_id: cm for cm in conv_messages}
+
+ # Update in_context status AND positions
in_context_set = set(in_context_message_ids)
for conv_msg in conv_messages:
conv_msg.in_context = conv_msg.message_id in in_context_set
+ # Update positions to match the order in in_context_message_ids
+ # This ensures ORDER BY position returns messages in the correct order
+ for position, message_id in enumerate(in_context_message_ids):
+ if message_id in conv_msg_dict:
+ conv_msg_dict[message_id].position = position
+
await session.commit()
@enforce_types