diff --git a/crates/cli/src/schedule.rs b/crates/cli/src/schedule.rs index dc4ca666..77bffe69 100644 --- a/crates/cli/src/schedule.rs +++ b/crates/cli/src/schedule.rs @@ -89,12 +89,13 @@ fn parse_every(rest: &str) -> Result { (amount, unit) } }; - let secs = match unit { - "m" | "min" | "mins" | "minute" | "minutes" => amount * 60, - "h" | "hr" | "hrs" | "hour" | "hours" => amount * 3600, - "d" | "day" | "days" => amount * 86_400, + let multiplier = match unit { + "m" | "min" | "mins" | "minute" | "minutes" => 60, + "h" | "hr" | "hrs" | "hour" | "hours" => 3600, + "d" | "day" | "days" => 86_400, _ => return Err(format!("Could not read the interval {rest:?}. Use every 30m, every 2h, or every 1d.")), }; + let secs = amount.checked_mul(multiplier).ok_or("That interval is longer than a year.")?; if secs < MIN_INTERVAL_SECS { return Err("That runs too often. A routine runs at most every five minutes.".into()); } @@ -154,10 +155,11 @@ fn parse_value(text: &str, min: u32, max: u32, names: &[&str]) -> Result Option { match self { - Schedule::Every(secs) => Some(after + secs), + Schedule::Every(secs) => after.checked_add(*secs), Schedule::Cron(cron) => cron.next_after(after), } } @@ -353,6 +355,26 @@ mod tests { local_unix(date, Some(clock)).unwrap() } + #[test] + fn oversized_intervals_reject_without_overflow_or_wraparound() { + for input in [ + "every 9223372036854775807m", "every 9223372036854775807h", + "every 9223372036854775807d", "every 144115188075855873d", + ] { + assert!(parse(input).is_err(), "accepted {input}"); + } + assert_eq!(parse("every 366d").unwrap(), Schedule::Every(366 * 86_400)); + assert!(parse("every 367d").is_err()); + } + + #[test] + fn interval_next_firing_does_not_wrap_timestamp() { + let schedule = parse("every 5m").unwrap(); + assert_eq!(schedule.next_after(i64::MAX - 300), Some(i64::MAX)); + assert_eq!(schedule.next_after(i64::MAX - 299), None); + assert_eq!(schedule.next_after(i64::MIN), Some(i64::MIN + 300)); + } + #[test] fn intervals_parse_and_read_back() { assert_eq!(parse("every 30m").unwrap(), Schedule::Every(1800)); @@ -366,6 +388,9 @@ mod tests { assert_eq!(parse("every 120m").unwrap().canonical(), "every 2h"); assert!(parse("every 2m").unwrap_err().contains("too often")); assert!(parse("every fortnight").is_err()); + for unit in ["m", "h", "d"] { + assert!(parse(&format!("every {}{unit}", i64::MAX)).unwrap_err().contains("longer than a year")); + } assert_eq!(parse("every 30m").unwrap().next_after(1000), Some(2800)); } diff --git a/docs/architecture/bots.md b/docs/architecture/bots.md index 50d723e7..41754582 100644 --- a/docs/architecture/bots.md +++ b/docs/architecture/bots.md @@ -47,7 +47,7 @@ A routine is a task a bot runs on a schedule in its direct chat with the user, a The roster also carries Auto-review (see [Plugins](plugins.md#plugins)). Routines live in the roster (`Routine { id, bot_id, name, prompt, schedule, is_enabled, enabled_at, last_run_at?, last_outcome?, paused_reason?, check?, created_at }`), so every paired Device lists them and can pause, resume, run, or delete one (`routines.create` / `routines.update` / `routines.delete` / `routines.run`, and `routines.describe` to read a schedule back). The bot's Runner runs them (`crates/cli/src/routines.rs`): every half minute it starts the routines of its bots whose next run is due, or their checks, and starts none while a run or check of the routine is going on. -A schedule (`crates/cli/src/schedule.rs`) is `every 30m`, `every 2h`, `every 1d`, or five cron fields read in the Runner's local time (`0 9 * * 1-5`), never more often than every five minutes. An interval counts from the last run, or from when the routine was created or resumed; a cron fires at the next matching minute after that. The CLI says a schedule in words on the wire (`schedule_text`: "Weekdays at 9:00 AM", "Every 2 hours", "On the 1st of every month at 8:30 AM", else "Cron 5 4 * * 1-3") and gives `next_run_at` and `is_running` beside every routine. +A schedule (`crates/cli/src/schedule.rs`) is an interval (`every 30m`, `every 2h`, `every 1d`) or five cron fields in the Runner's local time (`0 9 * * 1-5`), at most every five minutes. Intervals use checked arithmetic and range from five minutes to 366 days. They count from the last run, creation or resume; an overflowing next timestamp returns no firing. Cron fires at the next matching minute. The wire provides `schedule_text` ("Weekdays at 9:00 AM", "Every 2 hours", "On the 1st of every month at 8:30 AM", else "Cron 5 4 * * 1-3"), `next_run_at` and `is_running` for each routine. A run is a `routine` Job in the bot's DM. It opens with a "Routine · Name" marker (a system notice carrying the routine id), then the turn runs with the routine's prompt as the task and a system prompt that says nobody is typing: the bot does the work, replies with what the user should know, or answers `PASS`, which leaves only the marker. Later turns rebuild the marker as `[Routine "Name" ran on its schedule. Task: …]`; the daily log names the routine as the turn's source. `job.started` / `job.finished` carry `routine_id`, so the apps show the routine as running and the bot at work. A run's outcome (`sent`, `pass`, `error`) is kept on the routine. Run Now from a Device that is not the Runner seals the job to the Runner like any turn.