Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(cron): report the closest pair of runs, not the first one under the floor

Shanuc13ca9b32f0c
diff --git a/src/openhuman/cron/schedule.rs b/src/openhuman/cron/schedule.rs
index 263a41a422..35ebcdfa6f 100644
--- a/src/openhuman/cron/schedule.rs
+++ b/src/openhuman/cron/schedule.rs
@@ -132,19 +132,21 @@ impl fmt::Display for TooFrequent {
}
}
-/// Two consecutive runs of `schedule` after `from` that are closer together
-/// than `min_gap`, if there are any.
+/// The two consecutive runs of `schedule` after `from` that are closest
+/// together, if that gap is under `min_gap`.
///
-/// Consecutive occurrences are walked in order, so an irregular expression
-/// such as `1,2,30 * * * *` is judged by its shortest gap and not by whichever
-/// pair happens to follow `from`. The shortest gap is what counts, including
-/// the wrap-around one: `*/7 * * * *` fires at :56 and then at :00, four
-/// minutes apart, and is reported as such.
+/// Consecutive occurrences are walked in order and the smallest gap is kept,
+/// so an irregular expression such as `1,3,4,30 * * * *` is judged by its
+/// :03 → :04 pair — not by the first pair under the floor (:01 → :03) and not
+/// by whichever pair happens to follow `from`. The wrap-around gap counts too:
+/// `*/7 * * * *` fires at :56 and then at :00, four minutes apart, and is
+/// reported as such. Ties keep the earliest pair.
///
/// The walk is bounded ([`RUN_GAP_SCAN_OCCURRENCES`] runs, one shared
/// [`ACTIVE_WINDOW_CANDIDATE_LIMIT`] budget), so a sparse or window-restricted
-/// expression stays cheap. An expression that cannot be parsed, or that has no
-/// second occurrence, is not evidence of anything and yields `None`;
+/// expression stays cheap; if the budget runs out mid-walk, the closest pair
+/// seen so far is still reported. An expression that cannot be parsed, or that
+/// has no second occurrence, is not evidence of anything and yields `None`;
/// [`validate_schedule`] is where a bad expression gets rejected.
pub fn runs_closer_than(
schedule: &Schedule,
@@ -165,17 +167,23 @@ pub fn runs_closer_than(
let plan = CronPlan::parse(expr, tz.as_deref(), active_hours.as_ref()).ok()?;
let mut budget = ACTIVE_WINDOW_CANDIDATE_LIMIT;
let mut previous = plan.next_after(from, &mut budget).ok()?;
+ let mut closest: Option<TooFrequent> = None;
for _ in 1..RUN_GAP_SCAN_OCCURRENCES {
- let next = plan.next_after(previous, &mut budget).ok()?;
- if next - previous < min_gap {
- return Some(TooFrequent::ConsecutiveRuns {
+ // Running out of budget (or of occurrences) ends the walk but
+ // does not discard a pair already found.
+ let Ok(next) = plan.next_after(previous, &mut budget) else {
+ break;
+ };
+ let gap = next - previous;
+ if gap < min_gap && closest.is_none_or(|seen| gap < seen.gap()) {
+ closest = Some(TooFrequent::ConsecutiveRuns {
first: previous,
second: next,
});
}
previous = next;
}
- None
+ closest
}
}
}
diff --git a/src/openhuman/cron/schedule_tests.rs b/src/openhuman/cron/schedule_tests.rs
index 66b59cc7dc..e1aa26c816 100644
--- a/src/openhuman/cron/schedule_tests.rs
+++ b/src/openhuman/cron/schedule_tests.rs
@@ -351,6 +351,32 @@ fn runs_closer_than_does_not_depend_on_the_instant_it_starts_from() {
}
}
+/// The closest pair is reported, not the first one under the floor: `1,3,4,30`
+/// has :01 → :03 (2 min) before :03 → :04 (1 min), and the message must name
+/// the latter. Ties keep the earliest pair.
+#[test]
+fn runs_closer_than_reports_the_closest_pair_not_the_first_one_under_the_floor() {
+ let from = Utc.with_ymd_and_hms(2026, 3, 2, 10, 0, 0).unwrap();
+ let hit =
+ runs_closer_than(&utc_cron("1,3,4,30 * * * *"), from, MIN_AGENT_JOB_INTERVAL).unwrap();
+ assert_eq!(
+ hit,
+ TooFrequent::ConsecutiveRuns {
+ first: Utc.with_ymd_and_hms(2026, 3, 2, 10, 3, 0).unwrap(),
+ second: Utc.with_ymd_and_hms(2026, 3, 2, 10, 4, 0).unwrap(),
+ }
+ );
+ // All gaps equal: the earliest pair wins.
+ let hit = runs_closer_than(&utc_cron("*/2 * * * *"), from, MIN_AGENT_JOB_INTERVAL).unwrap();
+ assert_eq!(
+ hit,
+ TooFrequent::ConsecutiveRuns {
+ first: Utc.with_ymd_and_hms(2026, 3, 2, 10, 2, 0).unwrap(),
+ second: Utc.with_ymd_and_hms(2026, 3, 2, 10, 4, 0).unwrap(),
+ }
+ );
+}
+
/// `*/7` is 0,7,…,56: the :56 → :00 step is four minutes, and it counts.
#[test]
fn runs_closer_than_counts_the_wrap_around_gap() {
@@ -422,7 +448,9 @@ fn runs_closer_than_judges_the_effective_cadence_inside_the_active_window() {
// Every minute, but only during one minute of the day: effectively daily.
let daily = every_minute_during("09:00", "09:00");
assert!(runs_closer_than(&daily, from, MIN_AGENT_JOB_INTERVAL).is_none());
- // A two-minute window lets two adjacent runs through.
+ // A two-minute window lets two adjacent runs through. Skipping ~1,438
+ // out-of-window candidates per day exhausts the shared budget long before
+ // the 1,000-run walk ends; the pair found before that is still reported.
let hit = runs_closer_than(
&every_minute_during("09:00", "09:01"),
from,
diff --git a/src/openhuman/cron/tools/update_tests.rs b/src/openhuman/cron/tools/update_tests.rs
index e674b388be..3464069bee 100644
--- a/src/openhuman/cron/tools/update_tests.rs
+++ b/src/openhuman/cron/tools/update_tests.rs
@@ -139,6 +139,11 @@ async fn rejects_tightening_an_agent_job_below_five_minutes() {
"{:?}",
result.output()
);
+ // A rejected patch must not have been written before the error came back.
+ assert_eq!(
+ cron::get_job(&cfg, &job.id).unwrap().schedule,
+ Schedule::Every { every_ms: 600_000 }
+ );
let result = tool
.execute(json!({