diff --git a/src/serena/agent.py b/src/serena/agent.py index 7396fc26..61a56de7 100644 --- a/src/serena/agent.py +++ b/src/serena/agent.py @@ -198,15 +198,6 @@ class SerenaAgent: """ return self._task_executor.get_current_tasks() - def cancel_task(self, task_id: int) -> bool: - """ - Cancels a task with the given ID and removes it from the task queue. - - :param task_id: the ID of the task to cancel - :return: True if the task was found and cancelled, False otherwise - """ - return self._task_executor.cancel_task(task_id) - def get_last_executed_task(self) -> TaskExecutor.TaskInfo | None: """ Gets the last executed task. diff --git a/src/serena/dashboard.py b/src/serena/dashboard.py index 6110de76..3be3f3d5 100644 --- a/src/serena/dashboard.py +++ b/src/serena/dashboard.py @@ -262,8 +262,15 @@ class SerenaDashboardAPI: request_data = request.get_json() try: request_cancel_task = RequestCancelTaskExecution.model_validate(request_data) - task_was_cancelled = self._agent.cancel_task(request_cancel_task.task_id) - return {"status": "success", "was_cancelled": task_was_cancelled} + for task in self._agent.get_current_tasks(): + if task.task_id == request_cancel_task.task_id: + task.cancel() + return {"status": "success", "was_cancelled": True} + return { + "status": "success", + "was_cancelled": False, + "message": f"Task with id {request_data.get('task_id')} not found, maybe execution was already finished", + } except Exception as e: return {"status": "error", "message": str(e), "was_cancelled": False} diff --git a/src/serena/task_executor.py b/src/serena/task_executor.py index a66f5c6c..256fbf02 100644 --- a/src/serena/task_executor.py +++ b/src/serena/task_executor.py @@ -216,22 +216,3 @@ class TaskExecutor: """ with self._task_executor_lock: return self._task_executor_last_executed_task_info - - def cancel_task(self, task_id: int) -> bool: - """ - Cancels a task with the given task ID and removes it from the queue if found. - - :param task_id: the unique identifier of the task to cancel - :return: True if the task was found and cancelled, False otherwise - """ - with self._task_executor_lock: - if self._task_executor_current_task is not None and id(self._task_executor_current_task) == task_id: - self._task_executor_current_task.cancel() - self._task_executor_current_task = None - return True - for task in self._task_executor_queue: - if id(task) == task_id: - task.cancel() - self._task_executor_queue.remove(task) - return True - raise KeyError(f"Task with ID {task_id} not found in the queue. {self._task_executor_queue=}, {self._task_executor_current_task=}")