diff --git a/nanobot/agent/tools/cron.py b/nanobot/agent/tools/cron.py index 6a4fd653a..72f15a441 100644 --- a/nanobot/agent/tools/cron.py +++ b/nanobot/agent/tools/cron.py @@ -4,6 +4,7 @@ from __future__ import annotations +import time from contextvars import ContextVar, Token from datetime import datetime from typing import Any @@ -201,6 +202,11 @@ class CronTool(Tool): return err dt = dt.replace(tzinfo=ZoneInfo(self._default_timezone)) at_ms = int(dt.timestamp() * 1000) + if at_ms <= int(time.time() * 1000): + return ToolResult.error( + f"Error: one-time job time '{at}' is not in the future. " + "Retry with a future ISO datetime computed from the current time." + ) schedule = CronSchedule(kind="at", at_ms=at_ms) delete_after = True else: diff --git a/tests/cron/test_cron_tool_list.py b/tests/cron/test_cron_tool_list.py index b164532e6..ce2b94991 100644 --- a/tests/cron/test_cron_tool_list.py +++ b/tests/cron/test_cron_tool_list.py @@ -1,6 +1,8 @@ """Tests for CronTool._list_jobs() output formatting.""" -from datetime import datetime, timezone +from datetime import datetime, timedelta, timezone +from types import SimpleNamespace +from zoneinfo import ZoneInfo import pytest @@ -333,18 +335,54 @@ def test_add_cron_job_defaults_to_tool_timezone(tmp_path) -> None: def test_add_at_job_uses_default_timezone_for_naive_datetime(tmp_path) -> None: tool = _make_tool_with_tz(tmp_path, "Asia/Shanghai") + naive = (datetime.now(timezone.utc) + timedelta(days=1)).replace(tzinfo=None) with request_context( RequestContext(channel="telegram", chat_id="chat-1", session_key="telegram:chat-1") ): - result = tool._add_job( - None, "Morning reminder", None, None, None, "2026-03-25T08:00:00" - ) + result = tool._add_job(None, "Morning reminder", None, None, None, naive.isoformat()) assert result.startswith("Created job") job = tool._cron.list_jobs()[0] - expected = int(datetime(2026, 3, 25, 0, 0, 0, tzinfo=timezone.utc).timestamp() * 1000) + expected = int(naive.replace(tzinfo=ZoneInfo("Asia/Shanghai")).timestamp() * 1000) assert job.schedule.at_ms == expected +def test_add_at_job_rejects_past_datetime(tmp_path) -> None: + tool = _make_tool_with_tz(tmp_path, "Asia/Shanghai") + with request_context( + RequestContext(channel="telegram", chat_id="chat-1", session_key="telegram:chat-1") + ): + result = tool._add_job(None, "Old reminder", None, None, None, "2020-01-01T09:00:00") + + assert "not in the future" in result + assert "2020-01-01T09:00:00" in result + assert tool._cron.list_jobs() == [] + +def test_add_at_job_rejects_datetime_equal_to_now(tmp_path, monkeypatch) -> None: + tool = _make_tool(tmp_path) + fixed_now = 1_900_000_000.0 + monkeypatch.setattr("nanobot.agent.tools.cron.time", SimpleNamespace(time=lambda: fixed_now)) + at = datetime.fromtimestamp(fixed_now, tz=timezone.utc).isoformat() + with request_context( + RequestContext(channel="telegram", chat_id="chat-1", session_key="telegram:chat-1") + ): + result = tool._add_job(None, "Deadline reminder", None, None, None, at) + + assert "not in the future" in result + assert tool._cron.list_jobs() == [] + +def test_add_at_job_accepts_future_datetime(tmp_path) -> None: + tool = _make_tool(tmp_path) + future = (datetime.now(timezone.utc) + timedelta(hours=1)).isoformat() + with request_context( + RequestContext(channel="telegram", chat_id="chat-1", session_key="telegram:chat-1") + ): + result = tool._add_job(None, "Future reminder", None, None, None, future) + + assert result.startswith("Created job") + job = tool._cron.list_jobs()[0] + assert job.schedule.kind == "at" + assert job.state.next_run_at_ms is not None + def test_add_job_binds_current_session_key(tmp_path) -> None: tool = _make_tool(tmp_path)