mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
codex: enforce queue timer limit (#17380)
This commit is contained in:
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<bool> {
|
||||
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<Vec<ThreadTimer>> {
|
||||
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<_>>(),
|
||||
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<_>>(),
|
||||
vec!["timer-4".to_string()]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn one_shot_claim_consumes_pending_timer_once() {
|
||||
let runtime = test_runtime().await;
|
||||
|
||||
Reference in New Issue
Block a user