From 6065400899df64b2c052571e0be380e44633f7d8 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Wed, 27 May 2026 09:43:41 -0700 Subject: [PATCH] Serialize goal accounting updates --- codex-rs/ext/goal/Cargo.toml | 1 + codex-rs/ext/goal/src/accounting.rs | 21 ++++++++++++++++++++- codex-rs/ext/goal/src/runtime.rs | 2 ++ codex-rs/ext/goal/src/tool.rs | 5 +++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/codex-rs/ext/goal/Cargo.toml b/codex-rs/ext/goal/Cargo.toml index 667ed5d14d..6116383243 100644 --- a/codex-rs/ext/goal/Cargo.toml +++ b/codex-rs/ext/goal/Cargo.toml @@ -24,6 +24,7 @@ codex-tools = { workspace = true } codex-utils-template = { workspace = true } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } +tokio = { workspace = true, features = ["sync"] } tracing = { workspace = true } [dev-dependencies] diff --git a/codex-rs/ext/goal/src/accounting.rs b/codex-rs/ext/goal/src/accounting.rs index e0a84f5f8b..b5b14748de 100644 --- a/codex-rs/ext/goal/src/accounting.rs +++ b/codex-rs/ext/goal/src/accounting.rs @@ -6,9 +6,12 @@ use std::sync::Mutex; use std::sync::PoisonError; use std::time::Duration; use std::time::Instant; +use tokio::sync::Semaphore; +use tokio::sync::SemaphorePermit; -#[derive(Debug, Default)] +#[derive(Debug)] pub(crate) struct GoalAccountingState { + accounting_lock: Semaphore, inner: Mutex, } @@ -61,6 +64,13 @@ pub(crate) struct RecordedTokenDelta { } impl GoalAccountingState { + pub(crate) async fn accounting_permit(&self) -> Result, String> { + self.accounting_lock + .acquire() + .await + .map_err(|_| "goal accounting semaphore closed".to_string()) + } + pub(crate) fn start_turn( &self, turn_id: impl Into, @@ -320,6 +330,15 @@ impl Default for GoalAccountingInner { } } +impl Default for GoalAccountingState { + fn default() -> Self { + Self { + accounting_lock: Semaphore::new(/*permits*/ 1), + inner: Mutex::new(GoalAccountingInner::default()), + } + } +} + impl GoalAccountingInner { fn thread_unflushed_token_delta(&self) -> i64 { self.turns diff --git a/codex-rs/ext/goal/src/runtime.rs b/codex-rs/ext/goal/src/runtime.rs index 8609c2d7c2..8d4c932cff 100644 --- a/codex-rs/ext/goal/src/runtime.rs +++ b/codex-rs/ext/goal/src/runtime.rs @@ -306,6 +306,7 @@ impl GoalRuntimeHandle { budget_limited_goal_disposition: BudgetLimitedGoalDisposition, ) -> Result, String> { let accounting = self.accounting_state(); + let _accounting_permit = accounting.accounting_permit().await?; let Some(snapshot) = accounting.progress_snapshot(turn_id) else { return Ok(None); }; @@ -356,6 +357,7 @@ impl GoalRuntimeHandle { budget_limited_goal_disposition: BudgetLimitedGoalDisposition, ) -> Result, String> { let accounting = self.accounting_state(); + let _accounting_permit = accounting.accounting_permit().await?; let Some(snapshot) = accounting.idle_progress_snapshot() else { return Ok(None); }; diff --git a/codex-rs/ext/goal/src/tool.rs b/codex-rs/ext/goal/src/tool.rs index 90ea0eb44b..5dc547e521 100644 --- a/codex-rs/ext/goal/src/tool.rs +++ b/codex-rs/ext/goal/src/tool.rs @@ -291,6 +291,11 @@ impl GoalToolExecutor { let Some(turn_id) = self.accounting_state.current_turn_id() else { return Ok(None); }; + let _accounting_permit = self + .accounting_state + .accounting_permit() + .await + .map_err(FunctionCallError::RespondToModel)?; let Some(snapshot) = self.accounting_state.progress_snapshot(turn_id.as_str()) else { return Ok(None); };