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:
@@ -14,12 +14,17 @@ class CronTool(Tool):
|
|||||||
self._cron = cron_service
|
self._cron = cron_service
|
||||||
self._channel = ""
|
self._channel = ""
|
||||||
self._chat_id = ""
|
self._chat_id = ""
|
||||||
|
self._in_cron_context = False
|
||||||
|
|
||||||
def set_context(self, channel: str, chat_id: str) -> None:
|
def set_context(self, channel: str, chat_id: str) -> None:
|
||||||
"""Set the current session context for delivery."""
|
"""Set the current session context for delivery."""
|
||||||
self._channel = channel
|
self._channel = channel
|
||||||
self._chat_id = chat_id
|
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
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
return "cron"
|
return "cron"
|
||||||
@@ -72,6 +77,8 @@ class CronTool(Tool):
|
|||||||
**kwargs: Any,
|
**kwargs: Any,
|
||||||
) -> str:
|
) -> str:
|
||||||
if action == "add":
|
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)
|
return self._add_job(message, every_seconds, cron_expr, tz, at)
|
||||||
elif action == "list":
|
elif action == "list":
|
||||||
return self._list_jobs()
|
return self._list_jobs()
|
||||||
|
|||||||
@@ -296,6 +296,7 @@ def gateway(
|
|||||||
# Set cron callback (needs agent)
|
# Set cron callback (needs agent)
|
||||||
async def on_cron_job(job: CronJob) -> str | None:
|
async def on_cron_job(job: CronJob) -> str | None:
|
||||||
"""Execute a cron job through the agent."""
|
"""Execute a cron job through the agent."""
|
||||||
|
from nanobot.agent.tools.cron import CronTool
|
||||||
from nanobot.agent.tools.message import MessageTool
|
from nanobot.agent.tools.message import MessageTool
|
||||||
reminder_note = (
|
reminder_note = (
|
||||||
"[Scheduled Task] Timer finished.\n\n"
|
"[Scheduled Task] Timer finished.\n\n"
|
||||||
@@ -303,12 +304,20 @@ def gateway(
|
|||||||
f"Scheduled instruction: {job.payload.message}"
|
f"Scheduled instruction: {job.payload.message}"
|
||||||
)
|
)
|
||||||
|
|
||||||
response = await agent.process_direct(
|
# Prevent the agent from scheduling new cron jobs during execution
|
||||||
reminder_note,
|
cron_tool = agent.tools.get("cron")
|
||||||
session_key=f"cron:{job.id}",
|
if isinstance(cron_tool, CronTool):
|
||||||
channel=job.payload.channel or "cli",
|
cron_tool.set_cron_context(True)
|
||||||
chat_id=job.payload.to or "direct",
|
try:
|
||||||
)
|
response = await agent.process_direct(
|
||||||
|
reminder_note,
|
||||||
|
session_key=f"cron:{job.id}",
|
||||||
|
channel=job.payload.channel or "cli",
|
||||||
|
chat_id=job.payload.to or "direct",
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
if isinstance(cron_tool, CronTool):
|
||||||
|
cron_tool.set_cron_context(False)
|
||||||
|
|
||||||
message_tool = agent.tools.get("message")
|
message_tool = agent.tools.get("message")
|
||||||
if isinstance(message_tool, MessageTool) and message_tool._sent_in_turn:
|
if isinstance(message_tool, MessageTool) and message_tool._sent_in_turn:
|
||||||
|
|||||||
Reference in New Issue
Block a user