Skip to content

Commit 7064666

Browse files
committed
fix(voice): release the run while a tool reply waits for a paused activity
A reply task that waits for an AgentTask to return stayed watched by the current RunResult. When the AgentTask needs another user turn, that run never completed, and the next session.run() raised "nested runs are not supported". The reply task now unwatches itself while it waits and joins the run that is active on resume, as AgentTask does with its parent's handles.
1 parent a0345f1 commit 7064666

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

‎livekit-agents/livekit/agents/voice/tool_executor.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,22 @@ async def _deliver_reply(self, session: AgentSession) -> None:
560560
if not activity.scheduling_paused:
561561
break
562562

563-
# idle but paused: it resumes once an AgentTask returns, or closes at a handoff
563+
# idle but paused: it resumes once an AgentTask returns, or closes at a handoff.
564+
# the AgentTask may need another run to return, so release the current one
565+
reply_task = asyncio.current_task()
566+
run_state = session._global_run_state
567+
suspended = False
568+
if reply_task and run_state and run_state._unwatch_handle(reply_task):
569+
suspended = True
570+
run_state._mark_done_if_needed(None)
571+
564572
await activity._scheduling_resumed.wait()
573+
574+
# rejoin whichever run is active on resume, as AgentTask does with its parent
575+
run_state = session._global_run_state
576+
if suspended and reply_task and run_state and not run_state.done():
577+
run_state._watch_handle(reply_task)
578+
565579
if activity._closed:
566580
raise ActivityClosedError(f"activity {activity.agent.label} is closing")
567581
except ActivityClosedError:

‎tests/test_deferred_tool_reply.py‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,30 @@ async def collect(self) -> str:
6363
return "collected"
6464

6565

66+
class _AskTask(AgentTask[None]):
67+
def __init__(self) -> None:
68+
super().__init__(instructions="ask task")
69+
70+
async def on_enter(self) -> None:
71+
await asyncio.sleep(TOOL_WORK * 2) # keeps the first run open past send_link's work
72+
73+
@function_tool
74+
async def finish(self) -> None:
75+
"""Finish the task."""
76+
self.complete(None)
77+
78+
79+
class _Asker(Agent):
80+
def __init__(self) -> None:
81+
super().__init__(instructions="asker agent", tools=[send_link])
82+
83+
@function_tool
84+
async def ask(self) -> str:
85+
"""Ask the user."""
86+
await _AskTask()
87+
return "asked"
88+
89+
6690
def _outputs(agent: Agent) -> list[str]:
6791
return [i.output for i in agent.chat_ctx.items if i.type == "function_call_output"]
6892

@@ -130,6 +154,47 @@ async def test_reply_during_agent_task_waits_for_resume(
130154
assert "link sent" in _outputs(parent)
131155

132156

157+
async def test_reply_under_agent_task_does_not_block_run(
158+
caplog: pytest.LogCaptureFixture,
159+
) -> None:
160+
"""A reply waiting for an AgentTask that needs another user turn must not hold the
161+
current run open, or the next run can never start."""
162+
calls = [
163+
FunctionToolCall(name=name, arguments="{}", call_id=f"call_{name}")
164+
for name in ("send_link", "ask")
165+
]
166+
finish = [FunctionToolCall(name="finish", arguments="{}", call_id="call_finish")]
167+
llm = FakeLLM(
168+
fake_responses=[
169+
FakeLLMResponse(
170+
input="go", content="one moment", ttft=0.1, duration=0.1, tool_calls=calls
171+
),
172+
FakeLLMResponse(input="done", content="", ttft=0.1, duration=0.1, tool_calls=finish),
173+
]
174+
)
175+
asker = _Asker()
176+
async with AgentSession(llm=llm) as sess:
177+
speakers: list[str] = []
178+
179+
def _on_speech_created(ev: SpeechCreatedEvent) -> None:
180+
if ev.source == "generate_reply":
181+
speakers.append(type(sess.current_agent).__name__)
182+
183+
sess.on("speech_created", _on_speech_created)
184+
await sess.start(asker)
185+
186+
with caplog.at_level(logging.DEBUG, logger="livekit.agents"):
187+
await asyncio.wait_for(sess.run(user_input="go"), timeout=5.0)
188+
assert isinstance(sess.current_agent, _AskTask)
189+
await asyncio.wait_for(sess.run(user_input="done"), timeout=5.0)
190+
191+
errors = [r.getMessage() for r in caplog.records if r.levelno >= logging.ERROR]
192+
assert not errors, errors
193+
assert sess.current_agent is asker
194+
assert speakers[-1] == "_Asker" # the deferred reply, after the task returns
195+
assert "link sent" in _outputs(asker)
196+
197+
133198
async def test_session_scoped_reply_follows_handoff(caplog: pytest.LogCaptureFixture) -> None:
134199
"""A session-scoped async tool that finishes during a handoff is announced by the
135200
next agent."""

0 commit comments

Comments
 (0)