Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: Change reset messages to always preserve the original system message (#2813)
Matthew Zhou093f65e8914d
diff --git a/letta/services/agent_manager.py b/letta/services/agent_manager.pyindex c7f8628d5..554ea0abf 100644--- a/letta/services/agent_manager.py+++ b/letta/services/agent_manager.py@@ -1574,10 +1574,11 @@ class AgentManager:self, agent_id: str, actor: PydanticUser, add_default_initial_messages: bool = False) -> PydanticAgentState:"""- Removes all in-context messages for the specified agent by:- 1) Clearing the agent.messages relationship (which cascades delete-orphans).- 2) Resetting the message_ids list to empty.- 3) Committing the transaction.+ Removes all in-context messages for the specified agent except the original system message by:+ 1) Preserving the first message ID (original system message).+ 2) Deleting all other messages for the agent.+ 3) Updating the agent's message_ids to only contain the system message.+ 4) Optionally adding default initial messages after the system message.This action is destructive and cannot be undone once committed.@@ -1587,35 +1588,49 @@ class AgentManager:actor (PydanticUser): The user performing this action.Returns:- PydanticAgentState: The updated agent state with no linked messages.+ PydanticAgentState: The updated agent state with only the original system message preserved."""async with db_registry.async_session() as session:# Retrieve the existing agent (will raise NoResultFound if invalid)agent = await AgentModel.read_async(db_session=session, identifier=agent_id, actor=actor)- # Also clear out the message_ids field to keep in-context memory consistent- agent.message_ids = []+ # Ensure agent has message_ids with at least one message+ if not agent.message_ids or len(agent.message_ids) == 0:+ logger.error(+ f"Agent {agent_id} has no message_ids. Agent details: "+ f"name={agent.name}, created_at={agent.created_at}, "+ f"message_ids={agent.message_ids}, organization_id={agent.organization_id}"+ )+ raise ValueError(f"Agent {agent_id} has no message_ids - cannot preserve system message")- # Commit the update- await agent.update_async(db_session=session, actor=actor)+ # Get the system message ID (first message)+ system_message_id = agent.message_ids[0]- agent_state = await agent.to_pydantic_async()+ # Delete all messages for the agent except the system message+ await self.message_manager.delete_all_messages_for_agent_async(agent_id=agent_id, actor=actor, exclude_ids=[system_message_id])- await self.message_manager.delete_all_messages_for_agent_async(agent_id=agent_id, actor=actor)+ # Update agent to only keep the system message+ agent.message_ids = [system_message_id]+ await agent.update_async(db_session=session, actor=actor)+ agent_state = await agent.to_pydantic_async()+ # Optionally add default initial messages after the system messageif add_default_initial_messages:- return await self.append_initial_message_sequence_to_in_context_messages_async(actor, agent_state)- else:- # We still want to always have a system messageinit_messages = initialize_message_sequence(agent_state=agent_state, memory_edit_timestamp=get_utc_time(), include_initial_boot_message=True)- system_message = PydanticMessage.dict_to_message(- agent_id=agent_state.id,- model=agent_state.llm_config.model,- openai_message_dict=init_messages[0],- )- return await self.append_to_in_context_messages_async([system_message], agent_id=agent_state.id, actor=actor)+ # Skip index 0 (system message) since we preserved the original+ non_system_messages = [+ PydanticMessage.dict_to_message(+ agent_id=agent_state.id,+ model=agent_state.llm_config.model,+ openai_message_dict=msg,+ )+ for msg in init_messages[1:]+ ]+ return await self.append_to_in_context_messages_async(non_system_messages, agent_id=agent_state.id, actor=actor)+ else:+ return agent_state@trace_method@enforce_typesdiff --git a/letta/services/message_manager.py b/letta/services/message_manager.pyindex 34a2e32f2..5e1620cf3 100644--- a/letta/services/message_manager.py+++ b/letta/services/message_manager.py@@ -603,10 +603,11 @@ class MessageManager:@enforce_types@trace_method- async def delete_all_messages_for_agent_async(self, agent_id: str, actor: PydanticUser) -> int:+ async def delete_all_messages_for_agent_async(self, agent_id: str, actor: PydanticUser, exclude_ids: Optional[List[str]] = None) -> int:"""Efficiently deletes all messages associated with a given agent_id,while enforcing permission checks and avoiding any ORM‑level loads.+ Optionally excludes specific message IDs from deletion."""async with db_registry.async_session() as session:# 1) verify the agent exists and the actor has access@@ -616,10 +617,36 @@ class MessageManager:stmt = (delete(MessageModel).where(MessageModel.agent_id == agent_id).where(MessageModel.organization_id == actor.organization_id))++ # 3) exclude specific message IDs if provided+ if exclude_ids:+ stmt = stmt.where(~MessageModel.id.in_(exclude_ids))++ result = await session.execute(stmt)++ # 4) commit once+ await session.commit()++ # 5) return the number of rows deleted+ return result.rowcount++ @enforce_types+ @trace_method+ async def delete_messages_by_ids_async(self, message_ids: List[str], actor: PydanticUser) -> int:+ """+ Efficiently deletes messages by their specific IDs,+ while enforcing permission checks.+ """+ if not message_ids:+ return 0++ async with db_registry.async_session() as session:+ # issue a CORE DELETE against the mapped class for specific message IDs+ stmt = delete(MessageModel).where(MessageModel.id.in_(message_ids)).where(MessageModel.organization_id == actor.organization_id)result = await session.execute(stmt)- # 3) commit once+ # commit onceawait session.commit()- # 4) return the number of rows deleted+ # return the number of rows deletedreturn result.rowcountdiff --git a/tests/test_managers.py b/tests/test_managers.pyindex 147aa4024..a345b0e06 100644--- a/tests/test_managers.py+++ b/tests/test_managers.py@@ -1592,14 +1592,13 @@ async def test_reset_messages_no_messages(server: SyncServer, sarah_agent, defauTest that resetting messages on an agent that has zero messagesdoes not fail and clears out message_ids if somehow it's non-empty."""- # Force a weird scenario: Suppose the message_ids field was set non-empty (without actual messages).- await server.agent_manager.update_agent_async(sarah_agent.id, UpdateAgent(message_ids=["ghost-message-id"]), actor=default_user)- updated_agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, default_user)- assert updated_agent.message_ids == ["ghost-message-id"]+ assert len(sarah_agent.message_ids) == 4+ og_message_ids = sarah_agent.message_ids# Reset messagesreset_agent = await server.agent_manager.reset_messages_async(agent_id=sarah_agent.id, actor=default_user)assert len(reset_agent.message_ids) == 1+ assert og_message_ids[0] == reset_agent.message_ids[0]# Double check that physically no messages existassert await server.message_manager.size_async(agent_id=sarah_agent.id, actor=default_user) == 1@@ -1610,16 +1609,18 @@ async def test_reset_messages_default_messages(server: SyncServer, sarah_agent,Test that resetting messages on an agent that has zero messagesdoes not fail and clears out message_ids if somehow it's non-empty."""- # Force a weird scenario: Suppose the message_ids field was set non-empty (without actual messages).- await server.agent_manager.update_agent_async(sarah_agent.id, UpdateAgent(message_ids=["ghost-message-id"]), actor=default_user)- updated_agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, default_user)- assert updated_agent.message_ids == ["ghost-message-id"]+ assert len(sarah_agent.message_ids) == 4+ og_message_ids = sarah_agent.message_ids# Reset messagesreset_agent = await server.agent_manager.reset_messages_async(agent_id=sarah_agent.id, actor=default_user, add_default_initial_messages=True)assert len(reset_agent.message_ids) == 4+ assert og_message_ids[0] == reset_agent.message_ids[0]+ assert og_message_ids[1] != reset_agent.message_ids[1]+ assert og_message_ids[2] != reset_agent.message_ids[2]+ assert og_message_ids[3] != reset_agent.message_ids[3]# Double check that physically no messages existassert await server.message_manager.size_async(agent_id=sarah_agent.id, actor=default_user) == 4@@ -1671,6 +1672,9 @@ async def test_reset_messages_idempotency(server: SyncServer, sarah_agent, defau"""Test that calling reset_messages multiple times has no adverse effect."""+ # Clear messages first+ await server.message_manager.delete_messages_by_ids_async(message_ids=sarah_agent.message_ids[1:], actor=default_user)+# Create a single messageserver.message_manager.create_message(PydanticMessage(@@ -1692,6 +1696,72 @@ async def test_reset_messages_idempotency(server: SyncServer, sarah_agent, defauassert await server.message_manager.size_async(agent_id=sarah_agent.id, actor=default_user) == 1+async def test_reset_messages_preserves_system_message_id(server: SyncServer, sarah_agent, default_user, event_loop):+ """+ Test that resetting messages preserves the original system message ID.+ """+ # Get the original system message ID+ original_agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, default_user)+ original_system_message_id = original_agent.message_ids[0]++ # Add some user messages+ server.message_manager.create_message(+ PydanticMessage(+ agent_id=sarah_agent.id,+ organization_id=default_user.organization_id,+ role="user",+ content=[TextContent(text="Hello!")],+ ),+ actor=default_user,+ )++ # Reset messages+ reset_agent = await server.agent_manager.reset_messages_async(agent_id=sarah_agent.id, actor=default_user)++ # Verify the system message ID is preserved+ assert len(reset_agent.message_ids) == 1+ assert reset_agent.message_ids[0] == original_system_message_id++ # Verify the system message still exists in the database+ system_message = await server.message_manager.get_message_by_id_async(message_id=original_system_message_id, actor=default_user)+ assert system_message.role == "system"+++async def test_reset_messages_preserves_system_message_content(server: SyncServer, sarah_agent, default_user, event_loop):+ """+ Test that resetting messages preserves the original system message content.+ """+ # Get the original system message+ original_agent = await server.agent_manager.get_agent_by_id_async(sarah_agent.id, default_user)+ original_system_message = await server.message_manager.get_message_by_id_async(+ message_id=original_agent.message_ids[0], actor=default_user+ )++ # Add some messages and reset+ server.message_manager.create_message(+ PydanticMessage(+ agent_id=sarah_agent.id,+ organization_id=default_user.organization_id,+ role="user",+ content=[TextContent(text="Hello!")],+ ),+ actor=default_user,+ )++ reset_agent = await server.agent_manager.reset_messages_async(agent_id=sarah_agent.id, actor=default_user)++ # Verify the system message content is unchanged+ preserved_system_message = await server.message_manager.get_message_by_id_async(+ message_id=reset_agent.message_ids[0], actor=default_user+ )++ assert preserved_system_message.content == original_system_message.content+ assert preserved_system_message.role == "system"+ assert preserved_system_message.id == original_system_message.id++@pytest.mark.asyncioasync def test_modify_letta_message(server: SyncServer, sarah_agent, default_user, event_loop):"""