tools.scheduled_prompt module
Cron-based scheduled prompt tools (v3)
Schedule prompts to fire at specified times or on cron
intervals. Prompt metadata is persisted in Redis. When a prompt
comes due it is handed to a runner registered via
set_bot_runner (runner.handle_webhook); that legacy hook
predates the Phase T3 microservices split and is no longer wired
up, so delivery is a no-op unless a runner is supplied.
Module-level tick_scheduled_prompts and
cleanup_expired_prompts are used by background_tasks.py
and accept a redis client directly.
- tools.scheduled_prompt.set_bot_runner(runner)[source]
Store a runner reference used by
_execute_promptto deliver a prompt viarunner.handle_webhook.This hook predates the Phase T3 split into separate gateway, inference, agents, consolidation, and web services. Nothing in the current microservices layout calls it, so
_bot_runnernormally staysNoneand_execute_promptshort-circuits with an error until a runner is supplied.
- async tools.scheduled_prompt.tick_scheduled_prompts(redis)[source]
Scan all scheduled prompts and fire any that have come due.
The polling sweep that complements the per-prompt asyncio timers: it iterates every active/scheduled record and dispatches the ones whose time has arrived, so prompts still fire even if their dedicated timer task was never spawned or was lost (for example across a restart before hydration). Each due delivery is launched fire-and-forget via
asyncio.create_taskso a slow send does not stall the sweep.For one-time prompts it compares
schedule_timeto now and, on firing, flips the record toexecuted. For cron prompts it usescroniterto find the previous fire boundary and fires when that boundary is newer than the storedlast_execution(or, on first run, thecreated_attime), updatinglast_executionafterward. Reads via_list_alland persists status changes via_save; all errors are logged and swallowed so one bad record cannot abort the tick.Called by
background_tasks.scheduled_prompt_tick, which the agents service’s periodic scheduler runs roughly once a minute (registered inagents_main.py).- Parameters:
redis – An async Redis client.
- Return type:
- async tools.scheduled_prompt.hydrate_scheduled_prompts(redis, active_platforms, limit=500)[source]
Re-spawn in-memory timer tasks for surviving scheduled prompts.
Recovery routine run at startup: the per-prompt
_run_cron/_run_one_timeasyncio tasks live only in process memory and are lost on restart, so this walks the persisted index and re-instantiates a timer for each still-pending prompt. Without it, after a restart prompts would only fire via the slowertick_scheduled_promptspolling fallback.Reads the
_INDEXset and each record via_load, skipping prompts that are notactive/scheduled, that target a platform not in active_platforms, that already have a live task in_running_tasks, or (for one-time prompts) whose time has already passed. For each eligible prompt it adds the id to_running_tasksand launches_run_cronor_run_one_timeviaasyncio.create_task. Processing stops once limit prompts have been hydrated. All exceptions are caught and logged.Called at startup by the agents service wiring (see callers of
hydrate_scheduled_prompts); also exercised directly bytests/test_scheduled_prompt_hydration.py.- Parameters:
- Returns:
The number of prompts for which a timer task was re-spawned (
0on an empty index or on error).- Return type:
- async tools.scheduled_prompt.cleanup_expired_prompts(redis, *, days_to_keep=30)[source]
Purge old executed and cancelled prompts past a retention window.
Retention sweep that keeps the
scheduled_promptsnamespace from growing unbounded: it deletes terminal records (statusexecutedorcancelled) whosecreated_atis older than days_to_keep. Active and scheduled prompts are always left untouched, as are records with an unparseablecreated_at(those are logged and skipped).Reads candidates via
_list_alland removes each match via_delete(which clears both the per-prompt key and its index entry). Errors are caught and reported in the returned JSON rather than raised.Called by
background_tasks.scheduled_prompt_cleanup(which passesdays_to_keep=7) on the agents service’s periodic scheduler, and by thecleanup_expired_promptstool handler_cleanup_expired_toolin this module.