mirror of
https://github.com/openai/codex.git
synced 2026-11-17 22:10:37 +00:00
Add opt-in UTC dates to environment context (#61897)
## What changed Add the disabled-by-default `utc_environment_date` feature to compute the model-visible environment date in UTC and render it as `<current_date_utc>`. Preserve local dates when the feature is disabled. Track the date's time basis in environment snapshots so switching to UTC emits an update even when the date is unchanged. Use the matching date tag when reporting an unavailable clock. ## Testing Extend coverage for UTC midnight rollover, unchanged dates within a day, legacy snapshot refresh, and date removal and recovery. Update local and remote compaction snapshots to exercise the UTC date tag. GitOrigin-RevId: 6e983d05ca2914629c9f8575e2efe3ed274a2f77
This commit is contained in:
@@ -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"
|
||||
},
|
||||
|
||||
@@ -41,6 +41,7 @@ pub(crate) struct EnvironmentsState {
|
||||
per_environment: bool,
|
||||
shell_version: Option<String>,
|
||||
current_date: Option<String>,
|
||||
current_date_is_utc: bool,
|
||||
timezone: Option<String>,
|
||||
network: Option<NetworkContext>,
|
||||
filesystem: Option<FileSystemContext>,
|
||||
@@ -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<String>,
|
||||
current_date_removed: bool,
|
||||
current_date_is_utc: bool,
|
||||
timezone: Option<String>,
|
||||
network: Option<String>,
|
||||
filesystem: Option<FileSystemContext>,
|
||||
@@ -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(" <current_date status=\"unavailable\" />\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<String>,
|
||||
current_date: Option<String>,
|
||||
// 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<String>,
|
||||
network: Option<String>,
|
||||
filesystem: Option<String>,
|
||||
|
||||
@@ -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(),
|
||||
"<environment_context>\n <current_date status=\"unavailable\" />\n</environment_context>"
|
||||
format!(
|
||||
"<environment_context>\n <{tag} status=\"unavailable\" />\n</environment_context>"
|
||||
)
|
||||
);
|
||||
assert!(
|
||||
unavailable
|
||||
@@ -469,6 +479,6 @@ fn current_date_diff_clears_once_and_recovers() {
|
||||
.expect("restored current date")
|
||||
.render()
|
||||
.expect_text(),
|
||||
"<environment_context>\n <current_date>2026-06-17</current_date>\n</environment_context>"
|
||||
format!("<environment_context>\n <{tag}>2026-06-17</{tag}>\n</environment_context>")
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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::<EnvironmentsSnapshot>(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::<EnvironmentsSnapshot>(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(),
|
||||
"<environment_context>\n <current_date_utc>2026-06-19</current_date_utc>\n</environment_context>"
|
||||
);
|
||||
let snapshot = serde_json::from_value(serde_json::to_value(snapshot.unwrap())?)?;
|
||||
assert!(
|
||||
environment
|
||||
.render_fragment_diff(PreviousSectionState::Known(&snapshot))
|
||||
.1
|
||||
.is_none()
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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!(),
|
||||
};
|
||||
|
||||
@@ -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("<environment_context>").count(),
|
||||
if include_time { index + 1 } else { 1 }
|
||||
if include_time { index.min(1) + 1 } else { 1 }
|
||||
);
|
||||
assert_eq!(text.contains("<current_date"), include_time);
|
||||
assert_eq!(text.contains("<current_date_utc"), include_time);
|
||||
assert_eq!(text.contains("<timezone"), include_time);
|
||||
if include_time {
|
||||
let current_date = DateTime::<Utc>::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>{current_date}</current_date>")));
|
||||
assert!(text.contains(&format!(
|
||||
"<current_date_utc>{current_date}</current_date_utc>"
|
||||
)));
|
||||
}
|
||||
}
|
||||
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("<current_date>"))
|
||||
.any(|text| text.contains("<current_date"))
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -105,6 +105,10 @@ async fn extension_window_prefix_survives_compaction(compaction: &str) -> 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();
|
||||
|
||||
+3
-3
@@ -96,7 +96,7 @@ Settings:
|
||||
<environment_context>
|
||||
<cwd><CWD></cwd>
|
||||
<shell><HOST_SHELL></shell>
|
||||
<current_date><CURRENT_DATE></current_date>
|
||||
<current_date_utc><CURRENT_DATE></current_date_utc>
|
||||
<timezone><HOST_TIMEZONE></timezone>
|
||||
<filesystem><workspace_roots><root><CWD></root></workspace_roots><permission_profile type="disabled"><...e="unrestricted" /></permission_profile></filesystem> [hash=d40b2f45cb028888]
|
||||
</environment_context>
|
||||
@@ -216,7 +216,7 @@ Settings: unavailable (items only)
|
||||
<environment_context>
|
||||
<cwd><CWD></cwd>
|
||||
<shell><HOST_SHELL></shell>
|
||||
<current_date><CURRENT_DATE></current_date>
|
||||
<current_date_utc><CURRENT_DATE></current_date_utc>
|
||||
<timezone><HOST_TIMEZONE></timezone>
|
||||
<filesystem><workspace_roots><root><CWD></root></workspace_roots><permission_profile type="disabled"><...e="unrestricted" /></permission_profile></filesystem> [hash=d40b2f45cb028888]
|
||||
</environment_context>
|
||||
@@ -313,7 +313,7 @@ Settings: same as window 1
|
||||
<environment_context>
|
||||
<cwd><CWD></cwd>
|
||||
<shell><HOST_SHELL></shell>
|
||||
<current_date><CURRENT_DATE></current_date>
|
||||
<current_date_utc><CURRENT_DATE></current_date_utc>
|
||||
<timezone><HOST_TIMEZONE></timezone>
|
||||
<filesystem><workspace_roots><root><CWD></root></workspace_roots><permission_profile type="disabled"><...e="unrestricted" /></permission_profile></filesystem> [hash=d40b2f45cb028888]
|
||||
</environment_context>
|
||||
|
||||
+3
-3
@@ -96,7 +96,7 @@ Settings:
|
||||
<environment_context>
|
||||
<cwd><CWD></cwd>
|
||||
<shell><HOST_SHELL></shell>
|
||||
<current_date><CURRENT_DATE></current_date>
|
||||
<current_date_utc><CURRENT_DATE></current_date_utc>
|
||||
<timezone><HOST_TIMEZONE></timezone>
|
||||
<filesystem><workspace_roots><root><CWD></root></workspace_roots><permission_profile type="disabled"><...e="unrestricted" /></permission_profile></filesystem> [hash=d40b2f45cb028888]
|
||||
</environment_context>
|
||||
@@ -207,7 +207,7 @@ Settings: unavailable (items only)
|
||||
<environment_context>
|
||||
<cwd><CWD></cwd>
|
||||
<shell><HOST_SHELL></shell>
|
||||
<current_date><CURRENT_DATE></current_date>
|
||||
<current_date_utc><CURRENT_DATE></current_date_utc>
|
||||
<timezone><HOST_TIMEZONE></timezone>
|
||||
<filesystem><workspace_roots><root><CWD></root></workspace_roots><permission_profile type="disabled"><...e="unrestricted" /></permission_profile></filesystem> [hash=d40b2f45cb028888]
|
||||
</environment_context>
|
||||
@@ -302,7 +302,7 @@ Settings: same as window 1
|
||||
<environment_context>
|
||||
<cwd><CWD></cwd>
|
||||
<shell><HOST_SHELL></shell>
|
||||
<current_date><CURRENT_DATE></current_date>
|
||||
<current_date_utc><CURRENT_DATE></current_date_utc>
|
||||
<timezone><HOST_TIMEZONE></timezone>
|
||||
<filesystem><workspace_roots><root><CWD></root></workspace_roots><permission_profile type="disabled"><...e="unrestricted" /></permission_profile></filesystem> [hash=d40b2f45cb028888]
|
||||
</environment_context>
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user