feat: add time-based agent scheduling with APScheduler

Add comprehensive scheduling system that allows agents to automatically
start and stop during configured time windows, helping users manage
Claude API token limits by running agents during off-hours.

Backend Changes:
- Add Schedule and ScheduleOverride database models for persistent storage
- Implement APScheduler-based SchedulerService with UTC timezone support
- Add schedule CRUD API endpoints (/api/projects/{name}/schedules)
- Add manual override tracking to prevent unwanted auto-start/stop
- Integrate scheduler lifecycle with FastAPI startup/shutdown
- Fix timezone bug: explicitly set timezone=timezone.utc on CronTrigger
  to ensure correct UTC scheduling (critical fix)

Frontend Changes:
- Add ScheduleModal component for creating and managing schedules
- Add clock button and schedule status display to AgentControl
- Add timezone utilities for converting between UTC and local time
- Add React Query hooks for schedule data fetching
- Fix 204 No Content handling in fetchJSON for delete operations
- Invalidate nextRun cache when manually stopping agent during window
- Add TypeScript type annotations to Terminal component callbacks

Features:
- Multiple overlapping schedules per project supported
- Auto-start at scheduled time via APScheduler cron jobs
- Auto-stop after configured duration
- Manual start/stop creates persistent overrides in database
- Crash recovery with exponential backoff (max 3 retries)
- Server restart preserves schedules and active overrides
- Times displayed in user's local timezone, stored as UTC
- Immediate start if schedule created during active window

Dependencies:
- Add APScheduler for reliable cron-like scheduling

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Marian Paul
2026-01-15 16:57:46 +01:00
parent fbe4c399ac
commit 0bab585630
16 changed files with 2140 additions and 76 deletions

View File

@@ -472,3 +472,92 @@ class WSDevServerStatusMessage(BaseModel):
type: Literal["dev_server_status"] = "dev_server_status"
status: Literal["stopped", "running", "crashed"]
url: str | None = None
# ============================================================================
# Schedule Schemas
# ============================================================================
class ScheduleCreate(BaseModel):
"""Request schema for creating a schedule."""
start_time: str = Field(
...,
pattern=r'^([0-1][0-9]|2[0-3]):[0-5][0-9]$',
description="Start time in HH:MM format (local time, will be stored as UTC)"
)
duration_minutes: int = Field(
...,
ge=1,
le=1440,
description="Duration in minutes (1-1440)"
)
days_of_week: int = Field(
default=127,
ge=0,
le=127,
description="Bitfield: Mon=1, Tue=2, Wed=4, Thu=8, Fri=16, Sat=32, Sun=64"
)
enabled: bool = True
yolo_mode: bool = False
model: str | None = None
@field_validator('model')
@classmethod
def validate_model(cls, v: str | None) -> str | None:
"""Validate model is in the allowed list."""
if v is not None and v not in VALID_MODELS:
raise ValueError(f"Invalid model. Must be one of: {VALID_MODELS}")
return v
class ScheduleUpdate(BaseModel):
"""Request schema for updating a schedule (partial updates allowed)."""
start_time: str | None = Field(
None,
pattern=r'^([0-1][0-9]|2[0-3]):[0-5][0-9]$'
)
duration_minutes: int | None = Field(None, ge=1, le=1440)
days_of_week: int | None = Field(None, ge=0, le=127)
enabled: bool | None = None
yolo_mode: bool | None = None
model: str | None = None
@field_validator('model')
@classmethod
def validate_model(cls, v: str | None) -> str | None:
"""Validate model is in the allowed list."""
if v is not None and v not in VALID_MODELS:
raise ValueError(f"Invalid model. Must be one of: {VALID_MODELS}")
return v
class ScheduleResponse(BaseModel):
"""Response schema for a schedule."""
id: int
project_name: str
start_time: str # UTC, frontend converts to local
duration_minutes: int
days_of_week: int
enabled: bool
yolo_mode: bool
model: str | None
crash_count: int
created_at: datetime
class Config:
from_attributes = True
class ScheduleListResponse(BaseModel):
"""Response containing list of schedules."""
schedules: list[ScheduleResponse]
class NextRunResponse(BaseModel):
"""Response for next scheduled run calculation."""
has_schedules: bool
next_start: datetime | None # UTC
next_end: datetime | None # UTC (latest end if overlapping)
is_currently_running: bool
active_schedule_count: int