@@ -257,6 +257,9 @@ def prepare_agent_loop(config, bus, session_manager, cron):
257257 sandbox_manager = sandbox_manager ,
258258 config = config ,
259259 )
260+ # Set the agent reference in cron if it uses the holder pattern
261+ if hasattr (cron , '_agent_holder' ):
262+ cron ._agent_holder ['agent' ] = agent
260263 return agent
261264
262265
@@ -265,12 +268,35 @@ def prepare_cron(bus) -> CronService:
265268 cron_store_path = get_data_dir () / "cron" / "jobs.json"
266269 cron = CronService (cron_store_path )
267270
271+ # Use a mutable holder for the agent reference
272+ agent_holder = {"agent" : None }
273+
268274 # Set cron callback (needs agent)
269275 async def on_cron_job (job : CronJob ) -> str | None :
270276 """Execute a cron job through the agent."""
271277 session_key = SessionKey (** json .loads (job .payload .session_key_str ))
272- response = await agent .process_direct (
273- job .payload .message ,
278+ message = job .payload .message
279+
280+ if agent_holder ["agent" ] is None :
281+ raise RuntimeError ("Agent not initialized yet" )
282+
283+ # Clear instructions: let agent know this is a cron task to deliver
284+ cron_instruction = f"""[CRON TASK]
285+ This is a scheduled task triggered by cron job: '{ job .name } '
286+ Your task is to deliver the following reminder message to the user.
287+
288+ IMPORTANT:
289+ - This is NOT a user message - it's a scheduled reminder you need to send
290+ - You should acknowledge/confirm the reminder and send it in a friendly way
291+ - DO NOT treat this as a question from the user
292+ - Simply deliver the reminder message as requested
293+
294+ Reminder message to deliver:
295+ \" \" \" { message } \" \" \"
296+ """
297+
298+ response = await agent_holder ["agent" ].process_direct (
299+ cron_instruction ,
274300 session_key = session_key ,
275301 )
276302 if job .payload .deliver :
@@ -285,6 +311,7 @@ async def on_cron_job(job: CronJob) -> str | None:
285311 return response
286312
287313 cron .on_job = on_cron_job
314+ cron ._agent_holder = agent_holder
288315
289316 cron_status = cron .status ()
290317 if cron_status ["jobs" ] > 0 :
0 commit comments