Background tasks run now. Sometimes an agent needs to schedule work for later: reminders, periodic checks, delayed follow-ups, or recurring maintenance.
This chapter adds a simple cron scheduler.
cargo run -p s14_cron_scheduler- Add
CronScheduler. - Add
cron_create. - Add
cron_list. - Add
cron_delete. - Persist scheduled tasks.
- Match schedules using cron expressions.
- Re-inject due tasks into the agent loop.
An agent harness is not only a request-response loop. It can also react to time.
Cron scheduling lets the agent express:
Do this later
or:
Check this periodically
The scheduler turns time into another event source for the agent.
s14_cron_scheduler/
├── src/
│ ├── main.rs
│ ├── lib.rs
│ ├── cron.rs
│ └── tool/
│ ├── cron_create.rs
│ ├── cron_list.rs
│ ├── cron_delete.rs
│ ├── bash.rs
│ ├── read_file.rs
│ ├── write_file.rs
│ └── edit_file.rs
├── s14.md
└── cron_rs_explained.md
Scheduler logic is in src/cron.rs.
A scheduled task contains:
- id
- schedule expression
- prompt or message to inject
- enabled state
- last run time
- next run time
This gives the scheduler enough durable state to list, delete, and trigger tasks.
Example:
{
"schedule": "0/5 * * * * *",
"prompt": "Check whether the background test job has finished."
}The exact cron syntax depends on the cron parser used by the chapter.
Lists known scheduled tasks and their next run times.
Removes a scheduled task by id.
When a task becomes due, the scheduler injects a new user message into the agent context.
This keeps the core model loop unchanged:
time event -> user-like message -> model -> tool use -> result
The scheduler is an event source, not a second agent loop.
- Keep scheduled tasks durable.
- Use a scheduler manager rather than direct file access from tools.
- Convert due scheduled work into normal agent messages.
- Keep cron operations as separate typed tools.
- No distributed scheduler.
- No time zone UI beyond the chosen implementation.
- No missed-run recovery beyond the teaching version.
- No complex recurrence management.
- No calendar-style scheduling.
s15 introduces agent teams. Instead of one agent doing everything, named teammates can communicate through inboxes.