async def _achat(...):
...
while True:
...
if cur_step_output.should_defer:
spun_off_task = self.create_task(
task.input,
extra_state=task.extra_state,
)
# Run the spun-off task after waiting for 4 seconds.
promise_to_run_task_after_wait = self._achat_from_task_after_wait(
spun_off_task
)
# Fulfill this promise in the separate thread we prepared.
asyncio.run_coroutine_threadsafe(
promise_to_run_task_after_wait, self.deferral_loop
)
Demo: Here's the complete code:
https://github.com/tslmy/agent/pull/2/files. Run
tool_for_backburner.py
to see a proof-of-concept. The user asks the agent to walk the dog, the agent sees that the dog doesn't want to go outside because of the rain, and then the agent defers the dog-walking task to the sub-thread, returning the chat loop to the user.
Symptom: After the 4-sec sleep, while handling the deferred task, the code hangs at this line in
agent.runner.base.AgentRunner._arun_step
:
task = self.state.get_task(task_id)
I know it hangs on this line because I tapped a debugger into it, but -- due to the nature of debugger -- there is a slight chance that it is pointing me at the wrong line of code.
I'm not very familiar with
async
programming in Python, so my knowledge could only carry me this far. I'd appreciate any help on this front. Thanks!