Agent Zero AI framework
Scheduler: use convenience methods for logging of special messages
Rafael Uzarowski51c8451696ac
diff --git a/python/helpers/task_scheduler.py b/python/helpers/task_scheduler.pyindex 249945bb2..5f9321754 100644--- a/python/helpers/task_scheduler.py+++ b/python/helpers/task_scheduler.py@@ -215,7 +215,7 @@ class BaseTask(BaseModel):last_result=f"ERROR: {error}")if not updated_task:- PrintStyle(italic=True, font_color="red", padding=False).print(+ PrintStyle.error(f"Failed to update task {self.uuid} state to ERROR after error: {error}")await scheduler.save() # Force save after update@@ -231,7 +231,7 @@ class BaseTask(BaseModel):last_result=result)if not updated_task:- PrintStyle(italic=True, font_color="red", padding=False).print(+ PrintStyle.error(f"Failed to update task {self.uuid} state to IDLE after success")await scheduler.save() # Force save after update@@ -504,12 +504,12 @@ class SchedulerTaskList(BaseModel):for task in self.tasks:if isinstance(task, AdHocTask):if task.token is None or task.token == "":- PrintStyle(italic=True, font_color="red", padding=False).print(+ PrintStyle.warning(f"WARNING: AdHocTask {task.name} ({task.uuid}) has a null or empty token before saving: '{task.token}'")# Generate a new token to prevent errorstask.token = str(random.randint(1000000000000000000, 9999999999999999999))- PrintStyle(italic=True, font_color="red", padding=False).print(+ PrintStyle.info(f"Fixed: Generated new token '{task.token}' for task {task.name}")@@ -522,7 +522,7 @@ class SchedulerTaskList(BaseModel):# Debug: check if 'null' appears as token value in JSONif '"type": "adhoc"' in json_data and '"token": null' in json_data:- PrintStyle(italic=True, font_color="red", padding=False).print(+ PrintStyle.error("ERROR: Found null token in JSON output for an adhoc task")@@ -532,7 +532,7 @@ class SchedulerTaskList(BaseModel):if exists(path):loaded_json = read_file(path)if '"type": "adhoc"' in loaded_json and '"token": null' in loaded_json:- PrintStyle(italic=True, font_color="red", padding=False).print(+ PrintStyle.error("ERROR: Null token persisted in JSON file for an adhoc task")@@ -650,7 +650,7 @@ class TaskScheduler:deferred_task = self._running_deferred_tasks.get(task_uuid)if not deferred_task:return False- self._printer.print(f"Scheduler cancelling task {task_uuid}")+ PrintStyle.info(f"Scheduler cancelling task {task_uuid}")deferred_task.kill(terminate_thread=terminate_thread)return True@@ -719,7 +719,7 @@ class TaskScheduler:# If the task is in error state, reset it to IDLE firstif task.state == TaskState.ERROR:- self._printer.print(f"Resetting task '{task.name}' from ERROR to IDLE state before running")+ PrintStyle.info(f"Resetting task '{task.name}' from ERROR to IDLE state before running")await self.update_task(task_uuid, state=TaskState.IDLE)# Force a reload to ensure we have the updated stateawait self._tasks.reload()@@ -782,13 +782,13 @@ class TaskScheduler:if context:assert isinstance(context, AgentContext)- self._printer.print(+ PrintStyle.info(f"Scheduler Task {task.name} loaded from task {task.uuid}, context ok")save_tmp_chat(context)return contextelse:- self._printer.print(+ PrintStyle.warning(f"Scheduler Task {task.name} loaded from task {task.uuid} but context not found")return await self.__new_context(task)@@ -805,23 +805,23 @@ class TaskScheduler:# preflight checks with a snapshot of the tasktask_snapshot: Union[ScheduledTask, AdHocTask, PlannedTask] | None = self.get_task_by_uuid(task_uuid)if task_snapshot is None:- self._printer.print(f"Scheduler Task with UUID '{task_uuid}' not found")+ PrintStyle.error(f"Scheduler Task with UUID '{task_uuid}' not found")self._unregister_running_task(task_uuid)returnif task_snapshot.state == TaskState.RUNNING:- self._printer.print(f"Scheduler Task '{task_snapshot.name}' already running, skipping")+ PrintStyle.warning(f"Scheduler Task '{task_snapshot.name}' already running, skipping")self._unregister_running_task(task_uuid)return# Atomically fetch and check the task's current statecurrent_task = await self.update_task_checked(task_uuid, lambda task: task.state != TaskState.RUNNING, state=TaskState.RUNNING)if not current_task:- self._printer.print(f"Scheduler Task with UUID '{task_uuid}' not found or updated by another process")+ PrintStyle.error(f"Scheduler Task with UUID '{task_uuid}' not found or updated by another process")self._unregister_running_task(task_uuid)returnif current_task.state != TaskState.RUNNING:# This means the update failed due to state conflict- self._printer.print(f"Scheduler Task '{current_task.name}' state is '{current_task.state}', skipping")+ PrintStyle.warning(f"Scheduler Task '{current_task.name}' state is '{current_task.state}', skipping")self._unregister_running_task(task_uuid)return@@ -831,7 +831,7 @@ class TaskScheduler:agent = Nonetry:- self._printer.print(f"Scheduler Task '{current_task.name}' started")+ PrintStyle.info(f"Scheduler Task '{current_task.name}' started")context = await self._get_chat_context(current_task)AgentContext.use(context.id)@@ -854,9 +854,9 @@ class TaskScheduler:if url.scheme in ["http", "https", "ftp", "ftps", "sftp"]:attachment_filenames.append(attachment)else:- self._printer.print(f"Skipping attachment: [{attachment}]")+ PrintStyle.warning(f"Skipping attachment: [{attachment}]")except Exception:- self._printer.print(f"Skipping attachment: [{attachment}]")+ PrintStyle.warning(f"Skipping attachment: [{attachment}]")self._printer.print("User message:")self._printer.print(f"> {current_task.prompt}")@@ -893,7 +893,7 @@ class TaskScheduler:result = await agent.monologue()# Success- self._printer.print(f"Scheduler Task '{current_task.name}' completed: {result}")+ PrintStyle.success(f"Scheduler Task '{current_task.name}' completed: {result}")await self._persist_chat(current_task, context)await current_task.on_success(result)@@ -901,11 +901,11 @@ class TaskScheduler:await self._tasks.reload()updated_task = self.get_task_by_uuid(task_uuid)if updated_task and updated_task.state != TaskState.IDLE:- self._printer.print(f"Fixing task state consistency: '{current_task.name}' state is not IDLE after success")+ PrintStyle.warning(f"Fixing task state consistency: '{current_task.name}' state is not IDLE after success")await self.update_task(task_uuid, state=TaskState.IDLE)except asyncio.CancelledError:- self._printer.print(f"Scheduler Task '{current_task.name}' cancelled by user")+ PrintStyle.warning(f"Scheduler Task '{current_task.name}' cancelled by user")try:await asyncio.shield(self.update_task(task_uuid, state=TaskState.IDLE))except Exception:@@ -913,14 +913,14 @@ class TaskScheduler:raiseexcept Exception as e:# Error- self._printer.print(f"Scheduler Task '{current_task.name}' failed: {e}")+ PrintStyle.error(f"Scheduler Task '{current_task.name}' failed: {e}")await current_task.on_error(str(e))# Explicitly verify task was updated in storage after errorawait self._tasks.reload()updated_task = self.get_task_by_uuid(task_uuid)if updated_task and updated_task.state != TaskState.ERROR:- self._printer.print(f"Fixing task state consistency: '{current_task.name}' state is not ERROR after failure")+ PrintStyle.warning(f"Fixing task state consistency: '{current_task.name}' state is not ERROR after failure")await self.update_task(task_uuid, state=TaskState.ERROR)if agent:@@ -1094,7 +1094,7 @@ def parse_task_plan(plan_data: Dict[str, Any]) -> TaskPlan:done=done_dates_cast)except Exception as e:- PrintStyle(italic=True, font_color="red", padding=False).print(+ PrintStyle.error(f"Error parsing task plan: {e}")# Return empty plan instead of failing