fix(cron): reject past one-time schedules in the cron tool

The webui automation path already rejects one-time schedules that are
not in the future, but the agent cron tool silently accepted them: a
past ISO datetime produced an enabled job with next_run_at_ms=None
that never fires and never honors delete_after_run, while the tool
reported success. Reject the request at the tool boundary with an
actionable error instead.

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
FanouZeng-TTandClaude Code authored and Xubin Ren committed 2026-09-17 20:57:51 +08:00
1 parent 2fb1659398
commit b1d54b2ca2
2 files changed
+49 -5

No files matched your search

+6
View File
@@ -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:
+43 -5
View File
@@ -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)