fix: cancellation button

This commit is contained in:
Timothy
2026-03-09 12:48:20 -07:00
parent 2111c808a9
commit ad1a4ef0c3
2 changed files with 16 additions and 7 deletions
+14 -5
View File
@@ -347,6 +347,7 @@ class EventLoopNode(NodeProtocol):
self._awaiting_input = False
self._shutdown = False
self._stream_task: asyncio.Task | None = None
self._tool_task: asyncio.Task | None = None # gather task while tools run
# Track which nodes already have an action plan emitted (skip on revisit)
self._action_plan_emitted: set[str] = set()
# Monotonic counter for spillover file naming (web_search_1.txt, etc.)
@@ -1711,14 +1712,16 @@ class EventLoopNode(NodeProtocol):
self._input_ready.set()
def cancel_current_turn(self) -> None:
"""Cancel the current LLM streaming turn instantly.
"""Cancel the current LLM streaming turn or in-progress tool calls instantly.
Unlike signal_shutdown() which permanently stops the event loop,
this only kills the in-progress HTTP stream via task.cancel().
this only kills the in-progress HTTP stream or tool gather task.
The queen stays alive for the next user message.
"""
if self._stream_task and not self._stream_task.done():
self._stream_task.cancel()
if self._tool_task and not self._tool_task.done():
self._tool_task.cancel()
async def _await_user_input(
self,
@@ -2259,10 +2262,16 @@ class EventLoopNode(NodeProtocol):
_dur = round(time.time() - _s, 3)
return _r, _iso, _dur
timed_results = await asyncio.gather(
*(_timed_execute(tc) for tc in pending_real),
return_exceptions=True,
self._tool_task = asyncio.ensure_future(
asyncio.gather(
*(_timed_execute(tc) for tc in pending_real),
return_exceptions=True,
)
)
try:
timed_results = await self._tool_task
finally:
self._tool_task = None
# gather(return_exceptions=True) captures CancelledError
# as a return value instead of propagating it. Re-raise
# so stop_worker actually stops the execution.
@@ -204,8 +204,8 @@ class DeepResearchAgent:
"""Set up the executor with all components."""
from pathlib import Path
storage_path = Path.home() / ".hive" / "agents" / "deep_research_agent"
storage_path.mkdir(parents=True, exist_ok=True)
self._storage_path = Path.home() / ".hive" / "agents" / "deep_research_agent"
self._storage_path.mkdir(parents=True, exist_ok=True)
self._tool_registry = ToolRegistry()