main
md 112 lines 8.25 KB
Rendered Raw
1 # task_scheduler.py DOX
2
3 ## Purpose
4
5 - Own the `task_scheduler.py` helper module.
6 - This module models, serializes, schedules, runs, and persists scheduled tasks.
7 - Keep this file-level DOX profile synchronized with `task_scheduler.py` because this directory is intentionally flat.
8
9 ## Ownership
10
11 - `task_scheduler.py` owns the runtime implementation.
12 - `task_scheduler.py.dox.md` owns durable notes about responsibilities, contracts, side effects, and verification for that implementation.
13 - Classes:
14 - `TaskState` (`str`, `Enum`)
15 - `TaskType` (`str`, `Enum`)
16 - `TaskSchedule` (`BaseModel`)
17 - `to_crontab(self) -> str`
18 - `TaskPlan` (`BaseModel`)
19 - `create(cls, todo: list[datetime] | None=..., in_progress: datetime | None=..., done: list[datetime] | None=...)`
20 - `add_todo(self, launch_time: datetime)`
21 - `set_in_progress(self, launch_time: datetime)`
22 - `set_done(self, launch_time: datetime)`
23 - `get_next_launch_time(self) -> datetime | None`
24 - `should_launch(self) -> datetime | None`
25 - `BaseTask` (`BaseModel`)
26 - `update(self, name: str | None=..., state: TaskState | None=..., system_prompt: str | None=..., prompt: str | None=..., attachments: list[str] | None=..., last_run: datetime | None=..., last_result: str | None=..., context_id: str | None=..., **kwargs)`
27 - `check_schedule(self, frequency_seconds: float=...) -> bool`
28 - `get_next_run(self) -> datetime | None`
29 - `is_dedicated(self) -> bool`
30 - `get_next_run_minutes(self) -> int | None`
31 - `async on_run(self)`
32 - `async on_finish(self)`
33 - `async on_error(self, error: str)`
34 - `AdHocTask` (`BaseTask`)
35 - `create(cls, name: str, system_prompt: str, prompt: str, token: str, attachments: list[str] | None=..., context_id: str | None=..., project_name: str | None=..., project_color: str | None=...)`
36 - `update(self, name: str | None=..., state: TaskState | None=..., system_prompt: str | None=..., prompt: str | None=..., attachments: list[str] | None=..., last_run: datetime | None=..., last_result: str | None=..., context_id: str | None=..., token: str | None=..., **kwargs)`
37 - `ScheduledTask` (`BaseTask`)
38 - `create(cls, name: str, system_prompt: str, prompt: str, schedule: TaskSchedule, attachments: list[str] | None=..., context_id: str | None=..., timezone: str | None=..., project_name: str | None=..., project_color: str | None=...)`
39 - `update(self, name: str | None=..., state: TaskState | None=..., system_prompt: str | None=..., prompt: str | None=..., attachments: list[str] | None=..., last_run: datetime | None=..., last_result: str | None=..., context_id: str | None=..., schedule: TaskSchedule | None=..., **kwargs)`
40 - `check_schedule(self, frequency_seconds: float=...) -> bool`
41 - `get_next_run(self) -> datetime | None`
42 - `PlannedTask` (`BaseTask`)
43 - `create(cls, name: str, system_prompt: str, prompt: str, plan: TaskPlan, attachments: list[str] | None=..., context_id: str | None=..., project_name: str | None=..., project_color: str | None=...)`
44 - `update(self, name: str | None=..., state: TaskState | None=..., system_prompt: str | None=..., prompt: str | None=..., attachments: list[str] | None=..., last_run: datetime | None=..., last_result: str | None=..., context_id: str | None=..., plan: TaskPlan | None=..., **kwargs)`
45 - `check_schedule(self, frequency_seconds: float=...) -> bool`
46 - `get_next_run(self) -> datetime | None`
47 - `async on_run(self)`
48 - `async on_finish(self)`
49 - `async on_success(self, result: str)`
50 - `async on_error(self, error: str)`
51 - `SchedulerTaskList` (`BaseModel`)
52 - `get(cls) -> 'SchedulerTaskList'`
53 - `async reload(self) -> 'SchedulerTaskList'`
54 - `async add_task(self, task: Union[ScheduledTask, AdHocTask, PlannedTask]) -> 'SchedulerTaskList'`
55 - `async save(self) -> 'SchedulerTaskList'`
56 - `async update_task_by_uuid(self, task_uuid: str, updater_func: Callable[[Union[ScheduledTask, AdHocTask, PlannedTask]], None], verify_func: Callable[[Union[ScheduledTask, AdHocTask, PlannedTask]], bool]=...) -> Union[ScheduledTask, AdHocTask, PlannedTask] | None`
57 - `get_tasks(self) -> list[Union[ScheduledTask, AdHocTask, PlannedTask]]`
58 - `get_tasks_by_context_id(self, context_id: str, only_running: bool=...) -> list[Union[ScheduledTask, AdHocTask, PlannedTask]]`
59 - `async get_due_tasks(self) -> list[Union[ScheduledTask, AdHocTask, PlannedTask]]`
60 - `TaskScheduler` (no explicit base class)
61 - `get(cls) -> 'TaskScheduler'`
62 - `cancel_running_task(self, task_uuid: str, terminate_thread: bool=...) -> bool`
63 - `cancel_tasks_by_context(self, context_id: str, terminate_thread: bool=...) -> bool`
64 - `async reload(self)`
65 - `get_tasks(self) -> list[Union[ScheduledTask, AdHocTask, PlannedTask]]`
66 - `get_tasks_by_context_id(self, context_id: str, only_running: bool=...) -> list[Union[ScheduledTask, AdHocTask, PlannedTask]]`
67 - `async add_task(self, task: Union[ScheduledTask, AdHocTask, PlannedTask]) -> 'TaskScheduler'`
68 - `async remove_task_by_uuid(self, task_uuid: str) -> 'TaskScheduler'`
69 - Top-level functions:
70 - `normalize_schedule_timezone(timezone_name: str | None) -> str`
71 - `_now() -> datetime`
72 - `_localize_task_datetime(dt: datetime) -> datetime`
73 - `serialize_datetime(dt: Optional[datetime]) -> Optional[str]`: Serialize a datetime object to ISO format string in the user's timezone.
74 - `parse_datetime(dt_str: Optional[str]) -> Optional[datetime]`: Parse ISO format datetime string with timezone awareness.
75 - `serialize_task_schedule(schedule: TaskSchedule) -> Dict[str, str]`: Convert TaskSchedule to a standardized dictionary format.
76 - `parse_task_schedule(schedule_data: Dict[str, str]) -> TaskSchedule`: Parse dictionary into TaskSchedule with validation.
77 - `serialize_task_plan(plan: TaskPlan) -> Dict[str, Any]`: Convert TaskPlan to a standardized dictionary format.
78 - `parse_task_plan(plan_data: Dict[str, Any]) -> TaskPlan`: Parse dictionary into TaskPlan with validation.
79 - `serialize_task(task: Union[ScheduledTask, AdHocTask, PlannedTask]) -> Dict[str, Any]`: Standardized serialization for task objects with proper handling of all complex types.
80 - `serialize_tasks(tasks: list[Union[ScheduledTask, AdHocTask, PlannedTask]]) -> list[Dict[str, Any]]`: Serialize a list of tasks to a list of dictionaries.
81 - `deserialize_task(task_data: Dict[str, Any], task_class: Optional[Type[T]]=...) -> T`: Deserialize dictionary into appropriate task object with validation.
82 - Notable constants/configuration names: `SCHEDULER_FOLDER`, `LOCAL_TIMEZONE_ALIASES`, `T`.
83
84 ## Runtime Contracts
85
86 - Helper modules own reusable framework APIs and must preserve public callers unless all callers, tests, and docs are updated together.
87 - Update this file whenever public functions, classes, persistence behavior, path/security assumptions, side effects, or cross-module contracts change.
88 - Observed side-effect areas: filesystem reads, filesystem writes, filesystem deletion, network calls, settings/state persistence, secret handling, scheduler state.
89 - Imported dependency areas include: `agent`, `asyncio`, `crontab`, `datetime`, `enum`, `helpers`, `helpers.defer`, `helpers.files`, `helpers.localization`, `helpers.persist_chat`, `helpers.print_style`, `initialize`, `nest_asyncio`, `os`, `os.path`, `pydantic`.
90
91 ## Key Concepts
92
93 - Important called helpers/classes observed in the source: `nest_asyncio.apply`, `TypeVar`, `str.strip`, `callable`, `datetime.now`, `tzinfo.localize`, `Field`, `PrivateAttr`, `Localization.get.serialize_datetime`, `normalize_schedule_timezone`, `Localization.get.get_timezone`, `pytz.timezone`, `now`, `localize`, `cls`, `_localize_task_datetime`, `self.todo.remove`, `self.get_next_launch_time`, `super.__init__`, `threading.RLock`.
94 - Keep request/response, tool, or helper semantics documented here at the same time as source changes.
95
96 ## Work Guidance
97
98 - Preserve public helper APIs used by core code and plugins unless every caller is updated.
99 - Keep path, auth, secret, persistence, network, and subprocess behavior explicit and bounded.
100 - Prefer adding cohesive helper functions here only when behavior is reused across modules.
101
102 ## Verification
103
104 - Run targeted tests for changed helper behavior; run security regressions for auth, filesystem, WebSocket, tunnel, upload, or secret-handling helpers.
105 - Related tests observed by source search:
106 - `tests/test_task_scheduler_timezone.py`
107 - `tests/test_timezone_regressions.py`
108 - `tests/test_tool_action_contracts.py`
109
110 ## Child DOX Index
111
112 No child DOX files.