From 42b1e741dc6999f14d09852f9f3580a51bb840d4 Mon Sep 17 00:00:00 2001 From: Kian Jones <11655409+kianjones9@users.noreply.github.com> Date: Tue, 27 Jan 2026 23:05:24 -0800 Subject: [PATCH] fix: prevent duplicate block attachment in sleeptime agents (#9150) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Check if a block with the same label already exists before attaching to sleeptime agents. This prevents UniqueConstraintViolationError on the (agent_id, block_label) constraint when the same block is attached multiple times due to race conditions. 🤖 Generated with [Letta Code](https://letta.com) Co-authored-by: Letta --- letta/services/agent_manager.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/letta/services/agent_manager.py b/letta/services/agent_manager.py index f7874475..c8c656a8 100644 --- a/letta/services/agent_manager.py +++ b/letta/services/agent_manager.py @@ -2042,10 +2042,12 @@ class AgentManager: if other_agent_id != agent_id: try: other_agent = await AgentModel.read_async(db_session=session, identifier=other_agent_id, actor=actor) - if other_agent.agent_type == AgentType.sleeptime_agent and block not in other_agent.core_memory: - other_agent.core_memory.append(block) - # await other_agent.update_async(session, actor=actor, no_commit=True) - await other_agent.update_async(session, actor=actor) + if other_agent.agent_type == AgentType.sleeptime_agent: + # Check if block with same label already exists + existing_block = next((b for b in other_agent.core_memory if b.label == block.label), None) + if not existing_block: + other_agent.core_memory.append(block) + await other_agent.update_async(session, actor=actor) except NoResultFound: # Agent might not exist anymore, skip continue