Skip to content

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

fix(app): anchor a schedule's default start time to an absolute instant (#3101)

Zhu Leieb796b79f126
diff --git a/src/agentscope/app/_manager/_scheduler/_tools/_schedule_create.py b/src/agentscope/app/_manager/_scheduler/_tools/_schedule_create.py
index b211ad944..f79120557 100644
--- a/src/agentscope/app/_manager/_scheduler/_tools/_schedule_create.py
+++ b/src/agentscope/app/_manager/_scheduler/_tools/_schedule_create.py
@@ -223,7 +223,7 @@ to complete the task independently.
enabled=enabled,
cron_expression=cron_expression,
timezone=timezone,
- started_at=started_at or datetime.now(),
+ started_at=started_at or datetime.now().astimezone(),
ended_at=ended_at,
stateful=stateful,
permission_mode=perm_mode,
diff --git a/src/agentscope/app/_router/_schedule.py b/src/agentscope/app/_router/_schedule.py
index ea2e026ce..380909989 100644
--- a/src/agentscope/app/_router/_schedule.py
+++ b/src/agentscope/app/_router/_schedule.py
@@ -118,7 +118,9 @@ async def create_schedule(
permission_mode=body.permission_mode,
chat_model_config=body.chat_model_config,
source=ScheduleSource.USER,
- started_at=datetime.now(),
+ # Aware on purpose: the trigger reads a naive start time in
+ # the schedule's own timezone, not the server's.
+ started_at=datetime.now().astimezone(),
),
)
diff --git a/src/agentscope/app/storage/_model/_schedule.py b/src/agentscope/app/storage/_model/_schedule.py
index 523b5c1ce..4313a4a86 100644
--- a/src/agentscope/app/storage/_model/_schedule.py
+++ b/src/agentscope/app/storage/_model/_schedule.py
@@ -66,7 +66,7 @@ class ScheduleData(BaseModel):
started_at: datetime = Field(
description="The date and time the schedule was started.",
- default_factory=datetime.now,
+ default_factory=lambda: datetime.now().astimezone(),
)
ended_at: datetime | None = Field(
diff --git a/tests/schedule_router_test.py b/tests/schedule_router_test.py
index 2b28712bb..a9e095f68 100644
--- a/tests/schedule_router_test.py
+++ b/tests/schedule_router_test.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
"""Tests for schedule validation before persistence and registration."""
-from datetime import datetime
+from datetime import datetime, timedelta
from unittest import IsolatedAsyncioTestCase, TestCase
from fastapi import HTTPException
@@ -149,6 +149,67 @@ class ScheduleWeekdayTest(TestCase):
SchedulerManager.validate_schedule(record)
+# UTC-12 sits west of every real server timezone, so a start time taken
+# from the server's naive wall clock lands hours in this zone's future.
+_FAR_WEST_TZ = "Etc/GMT+12"
+
+
+class ScheduleDefaultStartTest(IsolatedAsyncioTestCase):
+ """A schedule created without a start time is active right away."""
+
+ def _assert_fires_within_the_hour(self, record: ScheduleRecord) -> None:
+ trigger = SchedulerManager.validate_schedule(record)
+ now = datetime.now().astimezone()
+ next_fire = trigger.get_next_fire_time(None, now)
+ self.assertIsNotNone(next_fire)
+ self.assertLessEqual(next_fire - now, timedelta(hours=1))
+
+ async def test_router_default_start_is_an_absolute_instant(self) -> None:
+ """An hourly schedule must not skip runs in a far-off timezone."""
+ storage = _Storage()
+
+ await create_schedule(
+ _request("0 * * * *", timezone=_FAR_WEST_TZ),
+ user_id="user-1",
+ storage=storage,
+ access=_Access(),
+ scheduler=_Scheduler(),
+ )
+
+ self._assert_fires_within_the_hour(storage.upserted[0])
+
+ async def test_tool_default_start_is_an_absolute_instant(self) -> None:
+ """The agent-facing tool's default start must not drift either."""
+ storage = _Storage()
+ tool = ScheduleCreate(
+ user_id="user-1",
+ agent_id="agent-1",
+ chat_model_config=_request("0 * * * *").chat_model_config,
+ storage=storage,
+ scheduler_manager=_Scheduler(),
+ )
+
+ await tool(
+ name="hourly",
+ cron_expression="0 * * * *",
+ timezone=_FAR_WEST_TZ,
+ )
+
+ self._assert_fires_within_the_hour(storage.upserted[0])
+
+ def test_model_default_start_is_an_absolute_instant(self) -> None:
+ """Records built without ``started_at`` get the same guarantee."""
+ record = _record()
+ record.data = ScheduleData(
+ name="hourly",
+ cron_expression="0 * * * *",
+ timezone=_FAR_WEST_TZ,
+ chat_model_config=record.data.chat_model_config,
+ )
+
+ self._assert_fires_within_the_hour(record)
+
+
class ScheduleValidationTest(IsolatedAsyncioTestCase):
"""Invalid schedules must fail before any state mutation."""