From 7e68348368420166e2179135b1b8324e212c2108 Mon Sep 17 00:00:00 2001 From: Shivering Ocelot Date: Sun, 20 Sep 2026 17:29:50 -0500 Subject: [PATCH] fix: bug when cancelling tasks, tool call message subkind --- backend/src/agent.py | 5 ++++- backend/src/inference.py | 5 +++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/backend/src/agent.py b/backend/src/agent.py index 18dc38c..b928676 100644 --- a/backend/src/agent.py +++ b/backend/src/agent.py @@ -148,7 +148,7 @@ class Agent: thread_id=message.thread_id, content=stream, kind="log", - subkind="reasoning", + subkind="tool-call", ) run.emit_event('chat-message', { 'messageId': new_message['id'], @@ -220,7 +220,10 @@ class AgentRun: self.reasoning = None # Cancellation tracking + # You should use 'cancelled', task and thread are for + # external tasks that cannot be cancelled reliably self.task = None # For asyncio tasks + self.thread = None self.cancelled = False app.tmp.runs[self.id] = self diff --git a/backend/src/inference.py b/backend/src/inference.py index 99c7b13..ba7a4b7 100644 --- a/backend/src/inference.py +++ b/backend/src/inference.py @@ -358,8 +358,7 @@ async def openrouter_chat_completion(app, messages, model, tools=[], opts={}, ag async for event in stream_response: if agent_run and agent_run.cancelled: - print(f"Cancelling openrouter inference for run {agent_run.id}") - # TODO: Need to cancell? + stream_response.cancel() break if event.choices and len(event.choices) > 0: @@ -402,6 +401,8 @@ async def openrouter_chat_completion(app, messages, model, tools=[], opts={}, ag def handle_done(task): try: task.result() + except asyncio.CancelledError: + print("[openrouter] inference cancelled") except Exception as e: print(f"[openrouter] Worker error: {e}") print(traceback.format_exc())