Skip to content

Your lightweight open-source personal agent.

fix(cron): reject non-positive every_seconds intervals

bobb5f539292dd9
diff --git a/nanobot/agent/tools/cron.py b/nanobot/agent/tools/cron.py
index 7a9fa4929..f5566ae30 100644
--- a/nanobot/agent/tools/cron.py
+++ b/nanobot/agent/tools/cron.py
@@ -31,7 +31,9 @@ _CRON_PARAMETERS = tool_parameters_schema(
"(e.g., 'Send a reminder to WeChat: xxx' or 'Check system status and report'). "
"Not used for action='list' or action='remove'."
),
- every_seconds=IntegerSchema(description="Interval in seconds (for recurring tasks)"),
+ every_seconds=IntegerSchema(
+ description="Interval in seconds (for recurring tasks)", minimum=1
+ ),
cron_expr=StringSchema("Cron expression like '0 9 * * *' (for scheduled tasks)"),
tz=StringSchema(
"Optional IANA timezone for cron expressions (e.g. 'America/Vancouver'). "
@@ -183,7 +185,9 @@ class CronTool(Tool):
# Build schedule
delete_after = False
- if every_seconds:
+ if every_seconds is not None:
+ if every_seconds <= 0:
+ return ToolResult.error("Error: every_seconds must be a positive integer")
schedule = CronSchedule(kind="every", every_ms=every_seconds * 1000)
elif cron_expr:
effective_tz = tz or self._default_timezone
diff --git a/nanobot/cron/service.py b/nanobot/cron/service.py
index 3e703033f..73b0609e0 100644
--- a/nanobot/cron/service.py
+++ b/nanobot/cron/service.py
@@ -73,6 +73,8 @@ def _validate_schedule_for_add(schedule: CronSchedule) -> None:
"""Validate schedule fields that would otherwise create non-runnable jobs."""
if schedule.tz and schedule.kind != "cron":
raise ValueError("tz can only be used with cron schedules")
+ if schedule.kind == "every" and (schedule.every_ms is None or schedule.every_ms <= 0):
+ raise ValueError("every schedule requires a positive 'every_ms'")
if schedule.kind == "cron":
if not schedule.expr or not schedule.expr.strip():
diff --git a/tests/cron/test_cron_service.py b/tests/cron/test_cron_service.py
index 2d7429db1..a3b4d414f 100644
--- a/tests/cron/test_cron_service.py
+++ b/tests/cron/test_cron_service.py
@@ -157,6 +157,21 @@ def test_add_job_rejects_missing_cron_expression(tmp_path, expr: str | None) ->
assert service.list_jobs(include_disabled=True) == []
[email protected]("every_ms", [None, 0, -60_000])
+def test_add_job_rejects_non_positive_interval(tmp_path, every_ms: int | None) -> None:
+ service = CronService(tmp_path / "cron" / "jobs.json")
+
+ with pytest.raises(ValueError, match="requires a positive 'every_ms'"):
+ service.add_job(
+ name="never runs",
+ schedule=CronSchedule(kind="every", every_ms=every_ms),
+ message="hello",
+ **_bound_chat(),
+ )
+
+ assert service.list_jobs(include_disabled=True) == []
+
+
def test_add_job_rejects_invalid_cron_expression_before_persisting(tmp_path) -> None:
service = CronService(tmp_path / "cron" / "jobs.json")
diff --git a/tests/cron/test_cron_tool_list.py b/tests/cron/test_cron_tool_list.py
index cc3799ee0..5c968f622 100644
--- a/tests/cron/test_cron_tool_list.py
+++ b/tests/cron/test_cron_tool_list.py
@@ -343,6 +343,29 @@ def test_add_job_rejects_multiple_schedule_fields(tmp_path) -> None:
assert tool._cron.list_jobs() == []
[email protected]("every_seconds", [0, -60])
+def test_add_job_rejects_non_positive_interval(tmp_path, every_seconds: int) -> None:
+ tool = _make_tool(tmp_path)
+ with request_context(
+ RequestContext(channel="telegram", chat_id="chat-1", session_key="telegram:chat-1")
+ ):
+ result = tool._add_job(None, "Morning standup", every_seconds, None, None, None)
+
+ assert result == "Error: every_seconds must be a positive integer"
+ assert tool._cron.list_jobs(include_disabled=True) == []
+
+
[email protected]("every_seconds", [0, -60])
+def test_validate_params_rejects_non_positive_interval(tmp_path, every_seconds: int) -> None:
+ tool = _make_tool(tmp_path)
+
+ errors = tool.validate_params(
+ {"action": "add", "message": "Morning standup", "every_seconds": every_seconds}
+ )
+
+ assert any("every_seconds" in error for error in errors)
+
+
def test_add_job_binds_current_session_key(tmp_path) -> None:
tool = _make_tool(tmp_path)
with request_context(