mirror of
https://github.com/openai/codex.git
synced 2026-11-23 23:10:47 +00:00
Serialize goal accounting updates
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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<GoalAccountingInner>,
|
||||
}
|
||||
|
||||
@@ -61,6 +64,13 @@ pub(crate) struct RecordedTokenDelta {
|
||||
}
|
||||
|
||||
impl GoalAccountingState {
|
||||
pub(crate) async fn accounting_permit(&self) -> Result<SemaphorePermit<'_>, String> {
|
||||
self.accounting_lock
|
||||
.acquire()
|
||||
.await
|
||||
.map_err(|_| "goal accounting semaphore closed".to_string())
|
||||
}
|
||||
|
||||
pub(crate) fn start_turn(
|
||||
&self,
|
||||
turn_id: impl Into<String>,
|
||||
@@ -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
|
||||
|
||||
@@ -306,6 +306,7 @@ impl GoalRuntimeHandle {
|
||||
budget_limited_goal_disposition: BudgetLimitedGoalDisposition,
|
||||
) -> Result<Option<AccountedGoalProgress>, 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<Option<AccountedGoalProgress>, String> {
|
||||
let accounting = self.accounting_state();
|
||||
let _accounting_permit = accounting.accounting_permit().await?;
|
||||
let Some(snapshot) = accounting.idle_progress_snapshot() else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user