diff --git a/pkgs/id/src/commands/world.rs b/pkgs/id/src/commands/world.rs index 8dffabaf43..60de198219 100644 --- a/pkgs/id/src/commands/world.rs +++ b/pkgs/id/src/commands/world.rs @@ -517,6 +517,13 @@ pub async fn cmd_world(command: WorldCommand) -> Result<()> { client.close(); endpoint.close().await; print!("{}", crate::world_manifest::diff(&manifest, &state)); + // Diff stays informational: report drift an apply would refuse. + if let Err(error) = crate::world_manifest::check_schedule_horizon( + &crate::world_manifest::plan(&manifest, &state), + crate::time::unix_ms(), + ) { + eprintln!("warning: `world apply` would refuse this manifest: {error:#}"); + } Ok(()) } WorldCommand::Apply(args) => { @@ -535,6 +542,15 @@ pub async fn cmd_world(command: WorldCommand) -> Result<()> { let (endpoint, client) = connect(&node, &addrs, no_relay, world.as_deref()).await?; let state = current_world_state(&client, &capability, Some(&admin_token)).await?; let steps = crate::world_manifest::plan(&manifest, &state); + // Refuse before any step runs: schedules apply last, so the + // host's horizon refusal would otherwise abort a partial apply (#6486). + if let Err(error) = + crate::world_manifest::check_schedule_horizon(&steps, crate::time::unix_ms()) + { + client.close(); + endpoint.close().await; + return Err(error); + } if steps.is_empty() { println!("no drift"); } else { diff --git a/pkgs/id/src/world_manifest.rs b/pkgs/id/src/world_manifest.rs index 0e16557be7..0141f5e17b 100644 --- a/pkgs/id/src/world_manifest.rs +++ b/pkgs/id/src/world_manifest.rs @@ -18,7 +18,8 @@ use anyhow::{Result, ensure}; use serde::{Deserialize, Serialize}; use crate::world_caps::{ - MAX_NAME_BYTES, MAX_PENDING_SCHEDULES, validate_ceiling_names, validate_grant_names, + MAX_NAME_BYTES, MAX_PENDING_SCHEDULES, MAX_SCHEDULE_HORIZON_DAYS, MAX_SCHEDULE_HORIZON_MS, + validate_ceiling_names, validate_grant_names, }; use crate::world_limits::WorldExecutionProfile; @@ -446,6 +447,31 @@ pub fn apply(steps: &[ApplyStep], state: &mut WorldState) -> Result { Ok(changed) } +/// Refuse a plan whose `ScheduleAt` steps fire past the host's schedule +/// horizon (`now_ms` plus [`MAX_SCHEDULE_HORIZON_DAYS`] days) — the rule +/// the host's `check_schedule` enforces. `validate()` and [`apply`] are pure +/// and clockless, and schedule steps run last in a live apply, so without +/// this pre-flight the host's refusal would abort the apply after earlier +/// steps had already mutated the world (#6486). Past fire times are legal +/// (they fire on the next wakeup). `now_ms` is the caller's clock; the host +/// re-checks against its own. +/// +/// # Errors +/// +/// Names the first schedule id and fire time beyond the horizon. +pub fn check_schedule_horizon(steps: &[ApplyStep], now_ms: u64) -> Result<()> { + let horizon = now_ms.saturating_add(MAX_SCHEDULE_HORIZON_MS); + for step in steps { + if let ApplyStep::ScheduleAt { id, at_ms } = step { + ensure!( + *at_ms <= horizon, + "schedule {id:?} fires at {at_ms} ms, more than {MAX_SCHEDULE_HORIZON_DAYS} days past the host clock ({now_ms} ms; limit {horizon} ms)" + ); + } + } + Ok(()) +} + /// Render the steps as `id world diff` drift lines. #[must_use] pub fn diff(desired: &WorldManifest, current: &WorldState) -> String { @@ -735,6 +761,40 @@ native_memory_bytes = 16777216", assert!(plan(&manifest, &state).is_empty()); } + #[test] + fn schedule_horizon_boundary_is_accepted_and_one_past_is_refused() { + let now = 1_000_000_u64; + let at = |at_ms| { + [ApplyStep::ScheduleAt { + id: "round".to_owned(), + at_ms, + }] + }; + let horizon = now + crate::world_caps::MAX_SCHEDULE_HORIZON_MS; + check_schedule_horizon(&at(horizon), now).unwrap(); + let err = check_schedule_horizon(&at(horizon + 1), now).unwrap_err(); + let text = format!("{err}"); + assert!(text.contains("round"), "{text}"); + assert!(text.contains(&(horizon + 1).to_string()), "{text}"); + assert!(text.contains("30 days"), "{text}"); + assert!(check_schedule_horizon(&at(u64::MAX), now).is_err()); + // Past and near-future fire times stay legal. + check_schedule_horizon(&at(1), now).unwrap(); + check_schedule_horizon(&at(now + 86_400_000), now).unwrap(); + // A clock near u64::MAX saturates instead of overflowing. + check_schedule_horizon(&at(u64::MAX), u64::MAX).unwrap(); + } + + #[test] + fn schedule_horizon_check_ignores_non_schedule_steps_and_plans_manifests() { + let now = 1_000_000_u64; + let manifest = parse("schedules = { far = 99999999999999 }").unwrap(); + let steps = plan(&manifest, &WorldState::default()); + assert!(check_schedule_horizon(&steps, now).is_err()); + let ok = parse("seed = 1\nschedules = { soon = 2000000 }").unwrap(); + check_schedule_horizon(&plan(&ok, &WorldState::default()), now).unwrap(); + } + #[test] fn apply_refuses_schedule_at_time_zero() { // #4769: validate() refuses at_ms == 0; apply()'s re-check must too,