From 8f288ef573eebf87243554f2bd3e6b246b3bb087 Mon Sep 17 00:00:00 2001 From: cthomas Date: Thu, 24 Apr 2025 00:09:22 -0700 Subject: [PATCH] fix: sleeptime input locking (#1874) --- letta/groups/sleeptime_multi_agent.py | 31 ++++++++++------------- tests/integration_test_sleeptime_agent.py | 5 +++- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/letta/groups/sleeptime_multi_agent.py b/letta/groups/sleeptime_multi_agent.py index 1eb1e8e3..6114c552 100644 --- a/letta/groups/sleeptime_multi_agent.py +++ b/letta/groups/sleeptime_multi_agent.py @@ -43,24 +43,21 @@ class SleeptimeMultiAgent(Agent): def _run_async_in_new_thread(self, coro): """Run an async coroutine in a new thread with its own event loop""" - result = None def run_async(): - nonlocal result loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) try: - result = loop.run_until_complete(coro) + loop.run_until_complete(coro) finally: loop.close() asyncio.set_event_loop(None) thread = threading.Thread(target=run_async) + thread.daemon = True thread.start() - thread.join() - return result - async def _issue_background_task( + def _issue_background_task( self, participant_agent_id: str, messages: List[Message], @@ -81,7 +78,7 @@ class SleeptimeMultiAgent(Agent): ) run = self.job_manager.create_job(pydantic_job=run, actor=self.user) - asyncio.create_task( + self._run_async_in_new_thread( self._perform_background_agent_step( participant_agent_id=participant_agent_id, messages=messages, @@ -239,17 +236,15 @@ class SleeptimeMultiAgent(Agent): ) for participant_agent_id in self.agent_ids: try: - run_id = self._run_async_in_new_thread( - self._issue_background_task( - participant_agent_id, - last_response_messages, - chaining, - max_chaining_steps, - token_streaming, - metadata, - put_inner_thoughts_first, - last_processed_message_id, - ) + run_id = self._issue_background_task( + participant_agent_id, + last_response_messages, + chaining, + max_chaining_steps, + token_streaming, + metadata, + put_inner_thoughts_first, + last_processed_message_id, ) run_ids.append(run_id) diff --git a/tests/integration_test_sleeptime_agent.py b/tests/integration_test_sleeptime_agent.py index f5c3977d..6b373d04 100644 --- a/tests/integration_test_sleeptime_agent.py +++ b/tests/integration_test_sleeptime_agent.py @@ -150,11 +150,14 @@ async def test_sleeptime_group_chat(server, actor): runs = [Run.from_job(job) for job in jobs] agent_runs = [run for run in runs if "agent_id" in run.metadata and run.metadata["agent_id"] == sleeptime_agent_id] assert len(agent_runs) == len(run_ids) + + # 6. Verify run status after sleep + time.sleep(8) for run_id in run_ids: job = server.job_manager.get_job_by_id(job_id=run_id, actor=actor) assert job.status == JobStatus.completed - # 6. Delete agent + # 7. Delete agent server.agent_manager.delete_agent(agent_id=main_agent.id, actor=actor) with pytest.raises(NoResultFound):