From 2874c0fdacb66ce115a21c36c69e94d23981b02e Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Sat, 11 Apr 2026 09:03:15 -0700 Subject: [PATCH] codex: enforce queue timer limit (#17380) --- codex-rs/cli/src/queue_cmd.rs | 79 ++++++++++++++++++- codex-rs/state/src/runtime/timers.rs | 113 +++++++++++++++++++++++++++ 2 files changed, 191 insertions(+), 1 deletion(-) diff --git a/codex-rs/cli/src/queue_cmd.rs b/codex-rs/cli/src/queue_cmd.rs index 8bcce96f1b..122dbedfe5 100644 --- a/codex-rs/cli/src/queue_cmd.rs +++ b/codex-rs/cli/src/queue_cmd.rs @@ -8,6 +8,7 @@ use clap::Parser; use codex_core::config::Config; use codex_core::config::ConfigOverrides; use codex_core::messages::MessagePayload; +use codex_core::timers::MAX_ACTIVE_TIMERS_PER_THREAD; use codex_core::timers::ThreadTimerStorageCreateParams; use codex_core::timers::ThreadTimerTrigger; use codex_core::timers::TimerDelivery; @@ -80,7 +81,7 @@ pub(crate) async fn run_queue_command( delivery, }) .map_err(anyhow::Error::msg)?; - state_db.create_thread_timer(&timer_params).await?; + create_thread_timer_for_queue(&state_db, &timer_params).await?; remove_queued_work_if_thread_missing( config.codex_home.as_path(), &state_db, @@ -124,6 +125,24 @@ pub(crate) async fn run_queue_command( Ok(()) } +async fn create_thread_timer_for_queue( + state_db: &StateRuntime, + timer_params: &codex_state::ThreadTimerCreateParams, +) -> anyhow::Result<()> { + if state_db + .create_thread_timer_if_below_limit(timer_params, MAX_ACTIVE_TIMERS_PER_THREAD) + .await? + { + return Ok(()); + } + + anyhow::bail!( + "thread `{}` already has the maximum of {} active timers", + timer_params.thread_id, + MAX_ACTIVE_TIMERS_PER_THREAD + ) +} + enum QueuedWork<'a> { Timer(&'a str), Message(&'a str), @@ -553,4 +572,62 @@ mod tests { Vec::new() ); } + + #[tokio::test] + async fn queued_timer_rejects_thread_at_timer_limit() { + let sqlite_home = TempDir::new().expect("sqlite home tempdir"); + let runtime = StateRuntime::init( + sqlite_home.path().to_path_buf(), + "test-provider".to_string(), + ) + .await + .expect("initialize state runtime"); + let thread_id = ThreadId::new().to_string(); + for index in 0..MAX_ACTIVE_TIMERS_PER_THREAD { + runtime + .create_thread_timer(&test_timer_params(&thread_id, &format!("timer-{index}"))) + .await + .expect("seed timer"); + } + + let err = create_thread_timer_for_queue( + &runtime, + &test_timer_params(&thread_id, "timer-over-limit"), + ) + .await + .expect_err("thread at timer limit should reject queued timer"); + + assert_eq!( + err.to_string(), + format!( + "thread `{thread_id}` already has the maximum of {MAX_ACTIVE_TIMERS_PER_THREAD} active timers" + ) + ); + assert_eq!( + runtime + .list_thread_timers(&thread_id) + .await + .expect("list timers") + .len(), + MAX_ACTIVE_TIMERS_PER_THREAD + ); + } + + fn test_timer_params(thread_id: &str, id: &str) -> codex_state::ThreadTimerCreateParams { + codex_state::ThreadTimerCreateParams { + id: id.to_string(), + thread_id: thread_id.to_string(), + source: "external".to_string(), + client_id: "codex-cli".to_string(), + trigger_json: r#"{"kind":"schedule","dtstart":"2026-04-10T12:00:00"}"#.to_string(), + content: "do work".to_string(), + instructions: None, + meta_json: "{}".to_string(), + delivery: TimerDelivery::AfterTurn.as_str().to_string(), + created_at: 100, + next_run_at: Some(200), + last_run_at: None, + pending_run: false, + } + } } diff --git a/codex-rs/state/src/runtime/timers.rs b/codex-rs/state/src/runtime/timers.rs index 94a81104fc..9d48ba69d4 100644 --- a/codex-rs/state/src/runtime/timers.rs +++ b/codex-rs/state/src/runtime/timers.rs @@ -74,6 +74,57 @@ INSERT INTO thread_timers ( Ok(()) } + pub async fn create_thread_timer_if_below_limit( + &self, + params: &ThreadTimerCreateParams, + max_thread_timers: usize, + ) -> anyhow::Result { + let max_thread_timers = i64::try_from(max_thread_timers)?; + let result = sqlx::query( + r#" +INSERT INTO thread_timers ( + id, + thread_id, + source, + client_id, + trigger_json, + content, + instructions, + meta_json, + delivery, + created_at, + next_run_at, + last_run_at, + pending_run +) +SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? +WHERE ( + SELECT COUNT(*) + FROM thread_timers + WHERE thread_id = ? +) < ? + "#, + ) + .bind(params.id.as_str()) + .bind(params.thread_id.as_str()) + .bind(params.source.as_str()) + .bind(params.client_id.as_str()) + .bind(params.trigger_json.as_str()) + .bind(params.content.as_str()) + .bind(params.instructions.as_deref()) + .bind(params.meta_json.as_str()) + .bind(params.delivery.as_str()) + .bind(params.created_at) + .bind(params.next_run_at) + .bind(params.last_run_at) + .bind(i64::from(params.pending_run)) + .bind(params.thread_id.as_str()) + .bind(max_thread_timers) + .execute(self.pool.as_ref()) + .await?; + Ok(result.rows_affected() > 0) + } + pub async fn list_thread_timers(&self, thread_id: &str) -> anyhow::Result> { let rows = sqlx::query_as::<_, ThreadTimerRow>( r#" @@ -345,6 +396,68 @@ ORDER BY name ); } + #[tokio::test] + async fn create_thread_timer_if_below_limit_rejects_full_thread() { + let runtime = test_runtime().await; + assert!( + runtime + .create_thread_timer_if_below_limit( + &timer_params("timer-1", "thread-1"), + /*max_thread_timers*/ 2, + ) + .await + .expect("create first timer") + ); + assert!( + runtime + .create_thread_timer_if_below_limit( + &timer_params("timer-2", "thread-1"), + /*max_thread_timers*/ 2, + ) + .await + .expect("create second timer") + ); + assert!( + !runtime + .create_thread_timer_if_below_limit( + &timer_params("timer-3", "thread-1"), + /*max_thread_timers*/ 2, + ) + .await + .expect("reject third timer") + ); + assert!( + runtime + .create_thread_timer_if_below_limit( + &timer_params("timer-4", "thread-2"), + /*max_thread_timers*/ 2, + ) + .await + .expect("create timer for different thread") + ); + + assert_eq!( + runtime + .list_thread_timers("thread-1") + .await + .expect("list thread-1 timers") + .into_iter() + .map(|timer| timer.id) + .collect::>(), + vec!["timer-1".to_string(), "timer-2".to_string()] + ); + assert_eq!( + runtime + .list_thread_timers("thread-2") + .await + .expect("list thread-2 timers") + .into_iter() + .map(|timer| timer.id) + .collect::>(), + vec!["timer-4".to_string()] + ); + } + #[tokio::test] async fn one_shot_claim_consumes_pending_timer_once() { let runtime = test_runtime().await;