Add opt-in nonfatal handling for clock read failures (#45825)

## Why

Clock provider failures can abort a turn while preparing time context or running clock tools. Allow turns to continue with an explicit indication that the current time is unavailable.

## What changed

- Add `features.nonfatal_clock_read_errors`, disabled by default, to report clock failures to the model without failing the turn.
- Emit a generic `failed to read current time` notice for context reads and tool errors, without exposing provider error details. Deduplicate context notices per turn and compaction window, and remove inherited notices from forked subagent context.
- Omit unavailable environment dates and explicitly clear previously visible dates with `<current_date status="unavailable" />`.
- Return external sleep clock failures to the model when the feature is enabled, preserving sleep item completion notifications.

## Testing

Add coverage for continued inference after clock failures, notice deduplication across compaction, subagent notice filtering, environment date removal and recovery, and sleep failures during initial and polling reads.

GitOrigin-RevId: a39c3723c06a6f786d8ad59d667e8b9f626c973e
This commit is contained in:
Vivian Fang
2026-09-16 01:16:35 +00:00
committed by copyberry
parent fac58c1153
commit 5bf132cd52
14 changed files with 367 additions and 48 deletions
@@ -41,3 +41,31 @@ impl ContextualUserFragment for CurrentTimeReminder {
format!("It is {}.", self.formatted_time())
}
}
pub(crate) struct CurrentTimeUnavailable;
impl CurrentTimeUnavailable {
pub(crate) const MESSAGE: &str = "failed to read current time";
}
impl ContextualUserFragment for CurrentTimeUnavailable {
fn content_kind(&self) -> ContentItemKind {
ContentItemKind("current_time.unavailable".to_string())
}
fn role(&self) -> &'static str {
"developer"
}
fn markers(&self) -> (&'static str, &'static str) {
Self::type_markers()
}
fn type_markers() -> (&'static str, &'static str) {
("<current_time_unavailable>", "</current_time_unavailable>")
}
fn body(&self) -> String {
Self::MESSAGE.to_string()
}
}
+1
View File
@@ -66,6 +66,7 @@ pub(crate) use contextual_user_message::is_contextual_user_fragment;
pub(crate) use contextual_user_message::is_user_authorization_message;
pub(crate) use contextual_user_message::parse_visible_hook_prompt_message;
pub(crate) use current_time_reminder::CurrentTimeReminder;
pub(crate) use current_time_reminder::CurrentTimeUnavailable;
pub(crate) use developer_instructions::DeveloperInstructions;
pub(crate) use environments_instructions::EnvironmentsInstructions;
pub(crate) use guardian_approved_action::GuardianApprovedAction;
@@ -92,6 +92,7 @@ impl EnvironmentsState {
shell_version: self.shell_version.clone(),
shell_version_removed: false,
current_date: self.current_date.clone(),
current_date_removed: false,
timezone: self.timezone.clone(),
network: self.network.clone(),
filesystem: self.filesystem.clone(),
@@ -183,6 +184,8 @@ impl WorldStateSection for EnvironmentsState {
shell_version_removed: self.shell_version.is_none()
&& previous.shell_version.is_some(),
current_date: self.current_date.clone(),
current_date_removed: self.current_date.is_none()
&& previous.current_date.is_some(),
timezone: self.timezone.clone(),
network: self.network.clone(),
filesystem: self.filesystem.clone(),
@@ -221,6 +224,7 @@ struct RenderedEnvironments {
shell_version: Option<String>,
shell_version_removed: bool,
current_date: Option<String>,
current_date_removed: bool,
timezone: Option<String>,
network: Option<NetworkContext>,
filesystem: Option<FileSystemContext>,
@@ -289,7 +293,11 @@ impl ContextualUserFragment for RenderedEnvironments {
let shell_version = self.shell_version.as_deref();
push_optional_element(&mut rendered, "shell_version", shell_version);
}
push_optional_element(&mut rendered, "current_date", self.current_date.as_deref());
if self.current_date_removed {
rendered.push_str(" <current_date status=\"unavailable\" />\n");
} else {
push_optional_element(&mut rendered, "current_date", self.current_date.as_deref());
}
push_optional_element(&mut rendered, "timezone", self.timezone.as_deref());
if let Some(network) = &self.network {
rendered.push_str(" ");
@@ -396,3 +396,31 @@ fn shell_version_diff_clears_previously_visible_version() {
"<environment_context>\n <shell_version status=\"unavailable\" />\n</environment_context>"
);
}
#[test]
fn current_date_diff_clears_once_and_recovers() {
let available = EnvironmentsState {
current_date: Some("2026-06-17".to_string()),
..Default::default()
};
let unavailable = EnvironmentsState::default();
assert_eq!(
unavailable
.render_diff(PreviousSectionState::Known(&available.snapshot()))
.expect("removed current date")
.render(),
"<environment_context>\n <current_date status=\"unavailable\" />\n</environment_context>"
);
assert!(
unavailable
.render_diff(PreviousSectionState::Known(&unavailable.snapshot()))
.is_none()
);
assert_eq!(
available
.render_diff(PreviousSectionState::Known(&unavailable.snapshot()))
.expect("restored current date")
.render(),
"<environment_context>\n <current_date>2026-06-17</current_date>\n</environment_context>"
);
}