From f4904c4bdfec22667c7d96b5a6884cb44b21836b Mon Sep 17 00:00:00 2001 From: 04cb <0x04cb@gmail.com> Date: Mon, 6 Apr 2026 22:47:48 +0800 Subject: [PATCH] fix(cron): add optional name parameter to separate job label from message (#2680) --- nanobot/agent/tools/cron.py | 10 ++++++++-- tests/cron/test_cron_tool_list.py | 8 ++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/nanobot/agent/tools/cron.py b/nanobot/agent/tools/cron.py index 064b6e4c9..f0d3ddab9 100644 --- a/nanobot/agent/tools/cron.py +++ b/nanobot/agent/tools/cron.py @@ -13,6 +13,10 @@ from nanobot.cron.types import CronJob, CronJobState, CronSchedule @tool_parameters( tool_parameters_schema( action=StringSchema("Action to perform", enum=["add", "list", "remove"]), + name=StringSchema( + "Optional short human-readable label for the job " + "(e.g., 'weather-monitor', 'daily-standup'). Defaults to first 30 chars of message." + ), message=StringSchema( "Instruction for the agent to execute when the job triggers " "(e.g., 'Send a reminder to WeChat: xxx' or 'Check system status and report')" @@ -93,6 +97,7 @@ class CronTool(Tool): async def execute( self, action: str, + name: str | None = None, message: str = "", every_seconds: int | None = None, cron_expr: str | None = None, @@ -105,7 +110,7 @@ class CronTool(Tool): if action == "add": if self._in_cron_context.get(): return "Error: cannot schedule new jobs from within a cron job execution" - return self._add_job(message, every_seconds, cron_expr, tz, at, deliver) + return self._add_job(name, message, every_seconds, cron_expr, tz, at, deliver) elif action == "list": return self._list_jobs() elif action == "remove": @@ -114,6 +119,7 @@ class CronTool(Tool): def _add_job( self, + name: str | None, message: str, every_seconds: int | None, cron_expr: str | None, @@ -158,7 +164,7 @@ class CronTool(Tool): return "Error: either every_seconds, cron_expr, or at is required" job = self._cron.add_job( - name=message[:30], + name=name or message[:30], schedule=schedule, message=message, deliver=deliver, diff --git a/tests/cron/test_cron_tool_list.py b/tests/cron/test_cron_tool_list.py index 5da3f4891..e57ab26bd 100644 --- a/tests/cron/test_cron_tool_list.py +++ b/tests/cron/test_cron_tool_list.py @@ -299,7 +299,7 @@ def test_add_cron_job_defaults_to_tool_timezone(tmp_path) -> None: tool = _make_tool_with_tz(tmp_path, "Asia/Shanghai") tool.set_context("telegram", "chat-1") - result = tool._add_job("Morning standup", None, "0 8 * * *", None, None) + result = tool._add_job(None, "Morning standup", None, "0 8 * * *", None, None) assert result.startswith("Created job") job = tool._cron.list_jobs()[0] @@ -310,7 +310,7 @@ def test_add_at_job_uses_default_timezone_for_naive_datetime(tmp_path) -> None: tool = _make_tool_with_tz(tmp_path, "Asia/Shanghai") tool.set_context("telegram", "chat-1") - result = tool._add_job("Morning reminder", None, None, None, "2026-03-25T08:00:00") + result = tool._add_job(None, "Morning reminder", None, None, None, "2026-03-25T08:00:00") assert result.startswith("Created job") job = tool._cron.list_jobs()[0] @@ -322,7 +322,7 @@ def test_add_job_delivers_by_default(tmp_path) -> None: tool = _make_tool(tmp_path) tool.set_context("telegram", "chat-1") - result = tool._add_job("Morning standup", 60, None, None, None) + result = tool._add_job(None, "Morning standup", 60, None, None, None) assert result.startswith("Created job") job = tool._cron.list_jobs()[0] @@ -333,7 +333,7 @@ def test_add_job_can_disable_delivery(tmp_path) -> None: tool = _make_tool(tmp_path) tool.set_context("telegram", "chat-1") - result = tool._add_job("Background refresh", 60, None, None, None, deliver=False) + result = tool._add_job(None, "Background refresh", 60, None, None, None, deliver=False) assert result.startswith("Created job") job = tool._cron.list_jobs()[0]