feat: task cancellation (wip)

This commit is contained in:
Shivering Ocelot committed 2026-05-30 17:51:49 -05:00
1 parent 25b5fbd22a
commit 77addb889d
10 files changed
+319 -60

No files matched your search

+35
View File
@@ -137,6 +137,11 @@ class AgentRun:
self.events = []
self.event_listeners = {}
self.output = None
# Cancellation tracking
self.task = None # For asyncio tasks
self.thread = None # For threads
self.cancelled = False
app.tmp.runs[self.id] = self
@@ -216,6 +221,36 @@ class AgentRun:
"output": self.output,
}
def cancel(self):
"""Cancel the agent run and stop any ongoing inference."""
if self.cancelled or self.done:
return
self.cancelled = True
print(f"Cancelling agent run {self.id}")
# Cancel asyncio task if exists
if self.task and not self.task.done():
self.task.cancel()
# Thread cannot be forcefully cancelled, but we can signal
if self.thread and self.thread.is_alive():
# The thread should be checking self.cancelled in its loop
print(f"Signalled thread to stop for run {self.id}")
# Close the stream and mark as done
self.close()
# Emit cancellation event
self.emit_event("cancelled", {"runId": self.id, "agentId": self.agent_id})
# Broadcast cancellation event
self.app.emit_event("run-cancelled", {
"runId": self.id,
"agentId": self.agent_id,
"endTime": int(time.time()),
})
def close(self):
if self.done:
return