Skip to content

Build and run agents you can see, understand and trust.

fix(scheduler): normalize standard cron weekday numbering (#2770)

dafyy321-pixeld2e8258e5fec
diff --git a/src/agentscope/app/_manager/_scheduler/_scheduler_manager.py b/src/agentscope/app/_manager/_scheduler/_scheduler_manager.py
index 7648f52a8..2c3f64124 100644
--- a/src/agentscope/app/_manager/_scheduler/_scheduler_manager.py
+++ b/src/agentscope/app/_manager/_scheduler/_scheduler_manager.py
@@ -369,13 +369,31 @@ class SchedulerManager:
# than raising, which is not what the caller asked for.
raise ValueError("timezone must be a non-empty IANA name")
+ # Standard cron counts weekdays from Sunday (0 or 7) while
+ # APScheduler counts from Monday, so map numeric terms to names.
+ weekdays = []
+ for term in day_of_week.split(","):
+ bounds, _, step = term.replace("*", "0-7").partition("/")
+ if term == "*" or not bounds.replace("-", "").isdigit():
+ weekdays.append(term)
+ continue
+ first, _, last = bounds.partition("-")
+ last = last or ("7" if step else first)
+ days = range(int(first), int(last) + 1, int(step or 1))
+ if int(last) > 7 or not days:
+ raise ValueError(f"Invalid day of week: {term!r}")
+ weekdays.extend(
+ ("sun", "mon", "tue", "wed", "thu", "fri", "sat")[d % 7]
+ for d in days
+ )
+
try:
trigger = CronTrigger(
minute=minute,
hour=hour,
day=day,
month=month,
- day_of_week=day_of_week,
+ day_of_week=",".join(weekdays),
timezone=record.data.timezone,
start_date=record.data.started_at,
end_date=record.data.ended_at,
diff --git a/tests/schedule_router_test.py b/tests/schedule_router_test.py
index fb410985f..2b28712bb 100644
--- a/tests/schedule_router_test.py
+++ b/tests/schedule_router_test.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""Tests for schedule validation before persistence and registration."""
from datetime import datetime
-from unittest import IsolatedAsyncioTestCase
+from unittest import IsolatedAsyncioTestCase, TestCase
from fastapi import HTTPException
@@ -121,6 +121,34 @@ def _record() -> ScheduleRecord:
)
+class ScheduleWeekdayTest(TestCase):
+ """Standard cron weekdays are translated for APScheduler."""
+
+ def test_standard_weekdays(self) -> None:
+ """Numeric weekdays count from Sunday; names pass through."""
+ record = _record()
+ actual = {}
+ for expression in ("1", "0", "7", "1-5", "0-7/3", "mon-fri"):
+ record.data.cron_expression = f"0 9 * * {expression}"
+ trigger = SchedulerManager.validate_schedule(record)
+ actual[expression] = str(trigger.fields[4])
+ self.assertDictEqual(
+ actual,
+ {
+ "1": "mon",
+ "0": "sun",
+ "7": "sun",
+ "1-5": "mon,tue,wed,thu,fri",
+ "0-7/3": "sun,wed,sat",
+ "mon-fri": "mon-fri",
+ },
+ )
+
+ record.data.cron_expression = "0 9 * * 8"
+ with self.assertRaises(ValueError):
+ SchedulerManager.validate_schedule(record)
+
+
class ScheduleValidationTest(IsolatedAsyncioTestCase):
"""Invalid schedules must fail before any state mutation."""