mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
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
This commit is contained in:
committed by
copyberry
parent
e40b0e62e2
commit
4aac2eacc5
@@ -278,12 +278,11 @@ impl CodexThread {
|
||||
}
|
||||
|
||||
/// Reads this thread's configured clock.
|
||||
pub async fn current_time(&self) -> CodexResult<DateTime<Utc>> {
|
||||
pub fn current_time(&self) -> CodexResult<DateTime<Utc>> {
|
||||
self.session
|
||||
.services
|
||||
.time_provider
|
||||
.current_time(self.session.thread_id)
|
||||
.await
|
||||
.map_err(|error| CodexErr::Io(std::io::Error::other(error)))
|
||||
}
|
||||
|
||||
|
||||
@@ -12,12 +12,12 @@ use codex_protocol::ThreadId;
|
||||
|
||||
use crate::config::CurrentTimeReminderConfig;
|
||||
|
||||
pub type TimeFuture<'a> = Pin<Box<dyn Future<Output = Result<DateTime<Utc>>> + Send + 'a>>;
|
||||
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 {
|
||||
fn current_time(&self, thread_id: ThreadId) -> TimeFuture<'_>;
|
||||
/// 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.
|
||||
///
|
||||
@@ -28,8 +28,8 @@ pub trait TimeProvider: Send + Sync {
|
||||
pub(crate) struct SystemTimeProvider;
|
||||
|
||||
impl TimeProvider for SystemTimeProvider {
|
||||
fn current_time(&self, _thread_id: ThreadId) -> TimeFuture<'_> {
|
||||
Box::pin(async { Ok(Utc::now()) })
|
||||
fn current_time(&self, _thread_id: ThreadId) -> Result<DateTime<Utc>> {
|
||||
Ok(Utc::now())
|
||||
}
|
||||
|
||||
fn sleep(&self, _thread_id: ThreadId, duration: Duration) -> SleepFuture<'_> {
|
||||
@@ -53,3 +53,7 @@ pub(crate) fn resolve_time_provider(
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "current_time_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#![cfg(test)]
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn explicit_external_time_requires_a_provider() {
|
||||
let config = CurrentTimeReminderConfig {
|
||||
clock_source: CurrentTimeSource::External,
|
||||
..Default::default()
|
||||
};
|
||||
let err = resolve_time_provider(Some(&config), /*external_provider*/ None)
|
||||
.err()
|
||||
.expect("missing provider");
|
||||
assert!(
|
||||
err.to_string()
|
||||
.contains("no external current-time provider")
|
||||
);
|
||||
let provider: Arc<dyn TimeProvider> = Arc::new(SystemTimeProvider);
|
||||
let selected = resolve_time_provider(Some(&config), Some(Arc::clone(&provider))).unwrap();
|
||||
assert!(Arc::ptr_eq(&selected, &provider));
|
||||
assert!(resolve_time_provider(/*config*/ None, /*external_provider*/ None).is_ok());
|
||||
}
|
||||
@@ -281,7 +281,6 @@ pub use codex_prompts::REVIEW_PROMPT;
|
||||
pub use codex_runtime_agent_instance::resolve_installation_id;
|
||||
pub use compact::content_items_to_text;
|
||||
pub use current_time::SleepFuture;
|
||||
pub use current_time::TimeFuture;
|
||||
pub use current_time::TimeProvider;
|
||||
pub use event_mapping::parse_turn_item;
|
||||
pub use exec_policy::ExecPolicyError;
|
||||
|
||||
@@ -102,12 +102,7 @@ impl Session {
|
||||
turn_context: &TurnContext,
|
||||
clock_read: &'static str,
|
||||
) -> CodexResult<Option<DateTime<Utc>>> {
|
||||
let error = match self
|
||||
.services
|
||||
.time_provider
|
||||
.current_time(self.thread_id)
|
||||
.await
|
||||
{
|
||||
let error = match self.services.time_provider.current_time(self.thread_id) {
|
||||
Ok(time) => {
|
||||
let mut state = self.state.lock().await;
|
||||
state.current_time_reminder.last_clock_failure = None;
|
||||
|
||||
@@ -103,7 +103,6 @@ impl ToolExecutor<ToolInvocation> for CurrentTimeHandler {
|
||||
.services
|
||||
.time_provider
|
||||
.current_time(invocation.session.thread_id)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
if invocation
|
||||
.turn
|
||||
|
||||
Reference in New Issue
Block a user