diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.json index b5930b9d94..3312f0bfcb 100644 --- a/codex-rs/core/config.schema.json +++ b/codex-rs/core/config.schema.json @@ -1493,6 +1493,9 @@ "use_xaa": { "type": "boolean" }, + "utc_environment_date": { + "type": "boolean" + }, "view_image": { "type": "boolean" }, @@ -8742,6 +8745,9 @@ "use_xaa": { "type": "boolean" }, + "utc_environment_date": { + "type": "boolean" + }, "view_image": { "type": "boolean" }, diff --git a/codex-rs/core/src/context/world_state/environment.rs b/codex-rs/core/src/context/world_state/environment.rs index df3069b6a6..188ec7723f 100644 --- a/codex-rs/core/src/context/world_state/environment.rs +++ b/codex-rs/core/src/context/world_state/environment.rs @@ -41,6 +41,7 @@ pub(crate) struct EnvironmentsState { per_environment: bool, shell_version: Option, current_date: Option, + current_date_is_utc: bool, timezone: Option, network: Option, filesystem: Option, @@ -73,6 +74,11 @@ impl EnvironmentsState { environments: environment_states(environments, per_environment), per_environment, shell_version, + current_date_is_utc: current_date.is_some() + && turn_context + .config + .features + .enabled(Feature::UtcEnvironmentDate), current_date, timezone: turn_context .timezone @@ -126,6 +132,7 @@ impl EnvironmentsState { shell_version_removed: false, current_date: self.current_date.clone(), current_date_removed: false, + current_date_is_utc: self.current_date_is_utc, timezone: self.timezone.clone(), network: self.rendered_network(), filesystem: self.filesystem.clone(), @@ -164,6 +171,7 @@ impl WorldStateSection for EnvironmentsState { .collect(), shell_version: self.shell_version.clone(), current_date: self.current_date.clone(), + current_date_is_utc: self.current_date_is_utc, timezone: self.timezone.clone(), network: self.rendered_network(), filesystem: self.filesystem.as_ref().map(FileSystemContext::render), @@ -180,6 +188,7 @@ impl WorldStateSection for EnvironmentsState { let turn_context_values_changed = format_changed || current.shell_version != previous.shell_version || current.current_date != previous.current_date + || current.current_date_is_utc != previous.current_date_is_utc || current.timezone != previous.timezone || current.network != previous.network || current.filesystem != previous.filesystem; @@ -222,6 +231,11 @@ impl WorldStateSection for EnvironmentsState { current_date: self.current_date.clone(), current_date_removed: self.current_date.is_none() && previous.current_date.is_some(), + current_date_is_utc: if self.current_date.is_some() { + self.current_date_is_utc + } else { + previous.current_date_is_utc + }, timezone: self.timezone.clone(), network: self.rendered_network(), filesystem: self.filesystem.clone(), @@ -266,6 +280,7 @@ pub(crate) struct RenderedEnvironments { shell_version_removed: bool, current_date: Option, current_date_removed: bool, + current_date_is_utc: bool, timezone: Option, network: Option, filesystem: Option, @@ -334,10 +349,15 @@ impl ContextualUserFragment for RenderedEnvironments { let shell_version = self.shell_version.as_deref(); push_optional_element(&mut rendered, "shell_version", shell_version); } - if self.current_date_removed { - rendered.push_str(" \n"); + let date_element = if self.current_date_is_utc { + "current_date_utc" } else { - push_optional_element(&mut rendered, "current_date", self.current_date.as_deref()); + "current_date" + }; + if self.current_date_removed { + rendered.push_str(&format!(" <{date_element} status=\"unavailable\" />\n")); + } else { + push_optional_element(&mut rendered, date_element, self.current_date.as_deref()); } push_optional_element(&mut rendered, "timezone", self.timezone.as_deref()); if let Some(network) = &self.network { @@ -431,6 +451,9 @@ pub(crate) struct EnvironmentsSnapshot { #[serde(default, skip_serializing_if = "Option::is_none")] shell_version: Option, current_date: Option, + // Changing the time basis must emit an update even when the date stays the same. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + current_date_is_utc: bool, timezone: Option, network: Option, filesystem: Option, diff --git a/codex-rs/core/src/context/world_state/environment_render_tests.rs b/codex-rs/core/src/context/world_state/environment_render_tests.rs index ec16d3cc28..3fa482bd41 100644 --- a/codex-rs/core/src/context/world_state/environment_render_tests.rs +++ b/codex-rs/core/src/context/world_state/environment_render_tests.rs @@ -65,6 +65,7 @@ fn environment_state( per_environment: false, shell_version: None, current_date, + current_date_is_utc: false, timezone, network, filesystem: None, @@ -425,12 +426,19 @@ fn shell_version_diff_clears_previously_visible_version() { ); } -#[test] -fn current_date_diff_clears_once_and_recovers() { +#[test_case::test_case(false; "local")] +#[test_case::test_case(true; "utc")] +fn current_date_diff_clears_once_and_recovers(current_date_is_utc: bool) { let available = EnvironmentsState { current_date: Some("2026-06-17".to_string()), + current_date_is_utc, ..Default::default() }; + let tag = if current_date_is_utc { + "current_date_utc" + } else { + "current_date" + }; let unavailable = EnvironmentsState::default(); assert_eq!( unavailable @@ -444,7 +452,9 @@ fn current_date_diff_clears_once_and_recovers() { .expect("removed current date") .render() .expect_text(), - "\n \n" + format!( + "\n <{tag} status=\"unavailable\" />\n" + ) ); assert!( unavailable @@ -469,6 +479,6 @@ fn current_date_diff_clears_once_and_recovers() { .expect("restored current date") .render() .expect_text(), - "\n 2026-06-17\n" + format!("\n <{tag}>2026-06-17\n") ); } diff --git a/codex-rs/core/src/context/world_state/environment_tests.rs b/codex-rs/core/src/context/world_state/environment_tests.rs index 9eee9d4ad5..598a141438 100644 --- a/codex-rs/core/src/context/world_state/environment_tests.rs +++ b/codex-rs/core/src/context/world_state/environment_tests.rs @@ -221,14 +221,14 @@ fn changing_primary_environment_updates_model_context_and_persisted_state() -> R } #[test] -fn legacy_single_environment_snapshot_does_not_change() -> Result<()> { +fn legacy_single_environment_snapshot_preserves_environment_and_refreshes_date() -> Result<()> { let environment = EnvironmentsState { environments: [("local".to_string(), primary("file:///repo", "bash")?)] .into_iter() .collect(), ..Default::default() }; - let legacy_snapshot = serde_json::from_value::(json!({ + let mut legacy_value = json!({ "environments": { "local": { "cwd": PathUri::parse("file:///repo")?.inferred_native_path_string(), @@ -236,7 +236,8 @@ fn legacy_single_environment_snapshot_does_not_change() -> Result<()> { "shell": "bash", }, }, - }))?; + }); + let legacy_snapshot = serde_json::from_value::(legacy_value.clone())?; assert!( environment @@ -258,6 +259,39 @@ fn legacy_single_environment_snapshot_does_not_change() -> Result<()> { }) ); + let environment = EnvironmentsState { + current_date: Some("2026-06-19".to_string()), + ..environment + }; + legacy_value["current_date"] = json!("2026-06-19"); + let legacy_snapshot = serde_json::from_value(legacy_value)?; + assert!( + environment + .render_fragment_diff(PreviousSectionState::Known(&legacy_snapshot)) + .1 + .is_none() + ); + let environment = EnvironmentsState { + current_date_is_utc: true, + ..environment + }; + let (snapshot, update) = + environment.render_fragment_diff(PreviousSectionState::Known(&legacy_snapshot)); + assert_eq!( + update + .expect("legacy date should be refreshed") + .render() + .expect_text(), + "\n 2026-06-19\n" + ); + let snapshot = serde_json::from_value(serde_json::to_value(snapshot.unwrap())?)?; + assert!( + environment + .render_fragment_diff(PreviousSectionState::Known(&snapshot)) + .1 + .is_none() + ); + Ok(()) } diff --git a/codex-rs/core/src/session/world_state.rs b/codex-rs/core/src/session/world_state.rs index 766e132a09..028212f0d0 100644 --- a/codex-rs/core/src/session/world_state.rs +++ b/codex-rs/core/src/session/world_state.rs @@ -371,10 +371,18 @@ impl Session { self.read_clock_for_context(turn_context, "environment_date") .await? .map(|current_time| { - current_time - .with_timezone(&chrono::Local) - .format("%Y-%m-%d") - .to_string() + if turn_context + .config + .features + .enabled(Feature::UtcEnvironmentDate) + { + current_time.format("%Y-%m-%d").to_string() + } else { + current_time + .with_timezone(&chrono::Local) + .format("%Y-%m-%d") + .to_string() + } }) } else { None diff --git a/codex-rs/core/tests/common/context_snapshot/normalize.rs b/codex-rs/core/tests/common/context_snapshot/normalize.rs index 0851bdd4d8..31b394b1f8 100644 --- a/codex-rs/core/tests/common/context_snapshot/normalize.rs +++ b/codex-rs/core/tests/common/context_snapshot/normalize.rs @@ -155,7 +155,7 @@ impl Normalizer { Regex::new(r"(<(cwd|root|path|glob)>)([^<]*)").expect("environment path regex") }); let host = HOST.get_or_init(|| { - Regex::new(r"(<(shell|shell_version|current_date|timezone)>)[^<]*") + Regex::new(r"(<(shell|shell_version|current_date|current_date_utc|timezone)>)[^<]*") .expect("host context regex") }); for captures in cwd.captures_iter(text) { @@ -198,7 +198,7 @@ impl Normalizer { let label = match &captures[2] { "shell" => "HOST_SHELL", "shell_version" => "HOST_SHELL_VERSION", - "current_date" => "CURRENT_DATE", + "current_date" | "current_date_utc" => "CURRENT_DATE", "timezone" => "HOST_TIMEZONE", _ => unreachable!(), }; diff --git a/codex-rs/core/tests/suite/current_time_reminder.rs b/codex-rs/core/tests/suite/current_time_reminder.rs index 3699ea25fd..68d0868aec 100644 --- a/codex-rs/core/tests/suite/current_time_reminder.rs +++ b/codex-rs/core/tests/suite/current_time_reminder.rs @@ -10,7 +10,6 @@ use std::time::Duration; use anyhow::Result; use anyhow::anyhow; use chrono::DateTime; -use chrono::Local; use chrono::Utc; use codex_config::test_support::CloudConfigBundleFixture; use codex_core::SleepFuture; @@ -160,6 +159,7 @@ async fn environment_context_time_follows_config( vec![ sse(vec![ev_response_created("resp-1"), ev_completed("resp-1")]), sse(vec![ev_response_created("resp-2"), ev_completed("resp-2")]), + sse(vec![ev_response_created("resp-3"), ev_completed("resp-3")]), ], ) .await; @@ -168,6 +168,10 @@ async fn environment_context_time_follows_config( .with_config(move |config| { enable_current_time_reminder(config, /*interval*/ 0, CurrentTimeSource::External); config.include_environment_context = true; + config + .features + .enable(Feature::UtcEnvironmentDate) + .expect("enable UTC environment date"); if !include_time { config.include_environment_context_time = false; } @@ -177,33 +181,43 @@ async fn environment_context_time_follows_config( .await?; test.submit_turn("first simulated day").await?; + let next_day = DateTime::parse_from_rfc3339("2026-06-18T00:00:00Z")?.timestamp(); time_provider .current_time - .store(FIRST_TIME_UNIX_SECONDS + 86_400, Ordering::Relaxed); + .store(next_day, Ordering::Relaxed); test.submit_turn("second simulated day").await?; + time_provider + .current_time + .store(next_day + 3_600, Ordering::Relaxed); + test.submit_turn("same simulated day").await?; let requests = responses.requests(); - assert_eq!(requests.len(), 2); + assert_eq!(requests.len(), 3); + for pair in requests.windows(2) { + let previous = pair[0].input(); + assert_eq!(&pair[1].input()[..previous.len()], previous.as_slice()); + } for (index, (request, timestamp)) in requests .iter() - .zip([FIRST_TIME_UNIX_SECONDS, FIRST_TIME_UNIX_SECONDS + 86_400]) + .zip([FIRST_TIME_UNIX_SECONDS, next_day, next_day + 3_600]) .enumerate() { assert!(request.has_content_kinds(&["environments.environment_context"])); let text = request.message_input_texts("user").join("\n"); assert_eq!( text.matches("").count(), - if include_time { index + 1 } else { 1 } + if include_time { index.min(1) + 1 } else { 1 } ); - assert_eq!(text.contains("::from_timestamp(timestamp, 0) .expect("test timestamp should be valid") - .with_timezone(&Local) .format("%Y-%m-%d") .to_string(); - assert!(text.contains(&format!("{current_date}"))); + assert!(text.contains(&format!( + "{current_date}" + ))); } } assert_eq!( @@ -753,7 +767,7 @@ async fn opted_in_clock_failures_reach_the_model_without_aborting() -> Result<() !request .message_input_texts("user") .iter() - .any(|text| text.contains("")) + .any(|text| text.contains(" Result .with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing()) .with_config(move |config| { configure_scenario_catalog(config); + config + .features + .enable(Feature::UtcEnvironmentDate) + .expect("enable UTC environment date"); config.workspace_roots = vec![config.cwd.clone()]; if local_compaction { config.model_provider.name = "Local compaction test".to_string(); diff --git a/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__world_state_prefix__extension_window_prefix_local.snap b/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__world_state_prefix__extension_window_prefix_local.snap index c75507935c..6261386acf 100644 --- a/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__world_state_prefix__extension_window_prefix_local.snap +++ b/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__world_state_prefix__extension_window_prefix_local.snap @@ -96,7 +96,7 @@ Settings: - + <...e="unrestricted" /> [hash=d40b2f45cb028888] @@ -216,7 +216,7 @@ Settings: unavailable (items only) - + <...e="unrestricted" /> [hash=d40b2f45cb028888] @@ -313,7 +313,7 @@ Settings: same as window 1 - + <...e="unrestricted" /> [hash=d40b2f45cb028888] diff --git a/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__world_state_prefix__extension_window_prefix_remote.snap b/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__world_state_prefix__extension_window_prefix_remote.snap index 3a4c4372c4..36cf628d25 100644 --- a/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__world_state_prefix__extension_window_prefix_remote.snap +++ b/codex-rs/core/tests/suite/snapshots/all__suite__scenarios__world_state_prefix__extension_window_prefix_remote.snap @@ -96,7 +96,7 @@ Settings: - + <...e="unrestricted" /> [hash=d40b2f45cb028888] @@ -207,7 +207,7 @@ Settings: unavailable (items only) - + <...e="unrestricted" /> [hash=d40b2f45cb028888] @@ -302,7 +302,7 @@ Settings: same as window 1 - + <...e="unrestricted" /> [hash=d40b2f45cb028888] diff --git a/codex-rs/features/src/lib.rs b/codex-rs/features/src/lib.rs index c37ba6f9db..a9a506eafc 100644 --- a/codex-rs/features/src/lib.rs +++ b/codex-rs/features/src/lib.rs @@ -485,6 +485,8 @@ pub enum Feature { ReasoningEffortOverride, /// Add current-time reminders to model-visible context. CurrentTimeReminder, + /// Use UTC for the date in model-visible environment context. + UtcEnvironmentDate, /// Report failed clock reads to the model without failing the turn. NonfatalClockReadErrors, /// Route MCP tool approval prompts through the MCP elicitation request path. @@ -2300,6 +2302,12 @@ pub const FEATURES: &[FeatureSpec] = &[ stage: Stage::UnderDevelopment, default_enabled: false, }, + FeatureSpec { + id: Feature::UtcEnvironmentDate, + key: "utc_environment_date", + stage: Stage::UnderDevelopment, + default_enabled: false, + }, FeatureSpec { id: Feature::NonfatalClockReadErrors, key: "nonfatal_clock_read_errors",