Files
codex/codex-rs/core/src/current_time.rs
T
Ruslan Nigmatullin 4aac2eacc5 Make clock reads synchronous and remove client clock requests (#61382)
## What changed

- Make `TimeProvider::current_time` and `CodexThread::current_time` synchronous, with clock reads required to avoid yielding or calling back into the session.
- Remove the experimental `currentTime/read` app-server request and its client-backed clock and sleep polling implementation.
- Require a host-provided `TimeProvider` for external clocks. App-server thread startup now fails when `features.current_time_reminder.clock_source = "external"` has no provider.

## Testing

Add coverage for external provider selection and rejection of app-server thread startup without an external provider.

GitOrigin-RevId: 09479d6eb28453b343dee78b3673092e73ae5d73
2026-11-13 21:51:53 +00:00

60 lines
1.8 KiB
Rust

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use anyhow::Result;
use anyhow::anyhow;
use chrono::DateTime;
use chrono::Utc;
use codex_features::CurrentTimeSource;
use codex_protocol::ThreadId;
use crate::config::CurrentTimeReminderConfig;
pub type SleepFuture<'a> = Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>>;
/// Host integration boundary for reading and waiting on the current time.
pub trait TimeProvider: Send + Sync {
/// Reads the clock without yielding or calling back into the session.
fn current_time(&self, thread_id: ThreadId) -> Result<DateTime<Utc>>;
/// Waits for the given duration on this provider's clock.
///
/// Dropping the returned future cancels the wait.
fn sleep(&self, thread_id: ThreadId, duration: Duration) -> SleepFuture<'_>;
}
pub(crate) struct SystemTimeProvider;
impl TimeProvider for SystemTimeProvider {
fn current_time(&self, _thread_id: ThreadId) -> Result<DateTime<Utc>> {
Ok(Utc::now())
}
fn sleep(&self, _thread_id: ThreadId, duration: Duration) -> SleepFuture<'_> {
Box::pin(async move {
tokio::time::sleep(duration).await;
Ok(())
})
}
}
pub(crate) fn resolve_time_provider(
config: Option<&CurrentTimeReminderConfig>,
external_provider: Option<Arc<dyn TimeProvider>>,
) -> Result<Arc<dyn TimeProvider>> {
match config.map(|config| config.clock_source).unwrap_or_default() {
CurrentTimeSource::System => Ok(Arc::new(SystemTimeProvider)),
CurrentTimeSource::External => external_provider.ok_or_else(|| {
anyhow!(
"features.current_time_reminder.clock_source is external, but no external current-time provider is available"
)
}),
}
}
#[cfg(test)]
#[path = "current_time_tests.rs"]
mod tests;