fix: sleeptime input locking (#1874)

This commit is contained in:
cthomas
2025-04-24 00:09:22 -07:00
committed by GitHub
parent 8e327295ec
commit 8f288ef573
2 changed files with 17 additions and 19 deletions

View File

@@ -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)

View File

@@ -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):