fix: prevent cron job execution from scheduling new jobs

When a cron job fires, the agent processes the scheduled message and
has access to the cron tool. If the original message resembles a
scheduling instruction (e.g. "remind me in 10 seconds"), the agent
would call cron.add again, creating an infinite feedback loop.

Add a cron-context flag to CronTool that blocks add operations during
cron job execution. The flag is set before process_direct() and cleared
in a finally block to ensure cleanup even on errors.

Fixes #1441
This commit is contained in:
Nikolas de Hor
2026-03-03 01:02:33 -03:00
parent 3c79404194
commit da8a4fc68c
2 changed files with 22 additions and 6 deletions

View File

@@ -14,12 +14,17 @@ class CronTool(Tool):
self._cron = cron_service
self._channel = ""
self._chat_id = ""
self._in_cron_context = False
def set_context(self, channel: str, chat_id: str) -> None:
"""Set the current session context for delivery."""
self._channel = channel
self._chat_id = chat_id
def set_cron_context(self, active: bool) -> None:
"""Mark whether the tool is executing inside a cron job callback."""
self._in_cron_context = active
@property
def name(self) -> str:
return "cron"
@@ -72,6 +77,8 @@ class CronTool(Tool):
**kwargs: Any,
) -> str:
if action == "add":
if self._in_cron_context:
return "Error: cannot schedule new jobs from within a cron job execution"
return self._add_job(message, every_seconds, cron_expr, tz, at)
elif action == "list":
return self._list_jobs()