mirror of
https://github.com/openai/codex.git
synced 2026-11-23 23:19:04 +00:00
Bump SQLx to pick up newer bundled SQLite (#24728)
## Why Codex stores thread, log, goal, and memory state in bundled SQLite databases through SQLx. We have a suspected SQLite WAL-reset corruption issue under heavy concurrent writer load, especially when multiple subagents are active. The existing `sqlx 0.8.6` dependency kept us on an older `libsqlite3-sys` / bundled SQLite, so this PR moves the SQLx stack far enough forward to pick up the newer bundled SQLite library. ## What changed - Bump the workspace `sqlx` dependency to `0.9.0`. - Use the SQLx 0.9 feature names explicitly: `runtime-tokio`, `tls-rustls`, and `sqlite-bundled`. - Update `Cargo.lock` so `sqlx-sqlite` resolves through `libsqlite3-sys 0.37.0`. - Refresh `MODULE.bazel.lock` for the dependency changes. - Adapt `codex-state` to SQLx 0.9: - build dynamic state queries with `QueryBuilder<Sqlite>` instead of passing dynamic `String`s to `sqlx::query`; - remove the old `QueryBuilder` lifetime parameter from helper signatures; - preserve SQLx's new `Migrator` fields when constructing runtime migrators. ## Verification - `just test -p codex-state` - `just bazel-lock-check` - `cargo check -p codex-state --tests`
This commit is contained in:
1 parent
aa184548b1
commit
379511dcea
7 files changed
+591
-342
No files matched your search
@@ -399,44 +399,78 @@ WHERE thread_id = ?
|
||||
}
|
||||
|
||||
let now_ms = datetime_to_epoch_millis(Utc::now());
|
||||
let active_or_stopped_status_filter =
|
||||
"status IN ('active', 'paused', 'blocked', 'usage_limited', 'budget_limited')";
|
||||
let status_filter = match mode {
|
||||
GoalAccountingMode::ActiveStatusOnly => "status = 'active'",
|
||||
GoalAccountingMode::ActiveOnly => "status IN ('active', 'budget_limited')",
|
||||
GoalAccountingMode::ActiveOrComplete => {
|
||||
"status IN ('active', 'budget_limited', 'complete')"
|
||||
}
|
||||
GoalAccountingMode::ActiveOrStopped => {
|
||||
"status IN ('active', 'paused', 'blocked', 'usage_limited', 'budget_limited')"
|
||||
}
|
||||
GoalAccountingMode::ActiveOrStopped => active_or_stopped_status_filter,
|
||||
};
|
||||
let budget_limit_status_filter = match mode {
|
||||
GoalAccountingMode::ActiveStatusOnly
|
||||
| GoalAccountingMode::ActiveOnly
|
||||
| GoalAccountingMode::ActiveOrComplete => "status = 'active'",
|
||||
GoalAccountingMode::ActiveOrStopped => {
|
||||
"status IN ('active', 'paused', 'blocked', 'usage_limited', 'budget_limited')"
|
||||
}
|
||||
GoalAccountingMode::ActiveOrStopped => active_or_stopped_status_filter,
|
||||
};
|
||||
let goal_id_filter = if expected_goal_id.is_some() {
|
||||
"goal_id = ?"
|
||||
} else {
|
||||
"1 = 1"
|
||||
};
|
||||
let query = format!(
|
||||
let mut builder = QueryBuilder::<Sqlite>::new(
|
||||
r#"
|
||||
UPDATE thread_goals
|
||||
SET
|
||||
time_used_seconds = time_used_seconds + ?,
|
||||
tokens_used = tokens_used + ?,
|
||||
time_used_seconds = time_used_seconds +
|
||||
"#,
|
||||
);
|
||||
builder.push_bind(time_delta_seconds);
|
||||
builder.push(
|
||||
r#",
|
||||
tokens_used = tokens_used +
|
||||
"#,
|
||||
);
|
||||
builder.push_bind(token_delta);
|
||||
builder.push(
|
||||
r#",
|
||||
status = CASE
|
||||
WHEN {budget_limit_status_filter} AND token_budget IS NOT NULL AND tokens_used + ? >= token_budget
|
||||
THEN ?
|
||||
WHEN
|
||||
"#,
|
||||
);
|
||||
builder.push(budget_limit_status_filter);
|
||||
builder.push(
|
||||
r#"
|
||||
AND token_budget IS NOT NULL
|
||||
AND tokens_used +
|
||||
"#,
|
||||
);
|
||||
builder.push_bind(token_delta);
|
||||
builder.push(
|
||||
r#"
|
||||
>= token_budget
|
||||
THEN
|
||||
"#,
|
||||
);
|
||||
builder.push_bind(crate::ThreadGoalStatus::BudgetLimited.as_str());
|
||||
builder.push(
|
||||
r#"
|
||||
ELSE status
|
||||
END,
|
||||
updated_at_ms = ?
|
||||
WHERE thread_id = ?
|
||||
AND {status_filter}
|
||||
AND {goal_id_filter}
|
||||
updated_at_ms =
|
||||
"#,
|
||||
);
|
||||
builder.push_bind(now_ms);
|
||||
builder.push(
|
||||
r#"
|
||||
WHERE thread_id =
|
||||
"#,
|
||||
);
|
||||
builder.push_bind(thread_id.to_string());
|
||||
builder.push(" AND ");
|
||||
builder.push(status_filter);
|
||||
if let Some(expected_goal_id) = expected_goal_id {
|
||||
builder.push(" AND goal_id = ").push_bind(expected_goal_id);
|
||||
}
|
||||
builder.push(
|
||||
r#"
|
||||
RETURNING
|
||||
thread_id,
|
||||
goal_id,
|
||||
@@ -450,18 +484,7 @@ RETURNING
|
||||
"#,
|
||||
);
|
||||
|
||||
let mut query = sqlx::query(&query)
|
||||
.bind(time_delta_seconds)
|
||||
.bind(token_delta)
|
||||
.bind(token_delta)
|
||||
.bind(crate::ThreadGoalStatus::BudgetLimited.as_str())
|
||||
.bind(now_ms)
|
||||
.bind(thread_id.to_string());
|
||||
if let Some(expected_goal_id) = expected_goal_id {
|
||||
query = query.bind(expected_goal_id);
|
||||
}
|
||||
|
||||
let row = query.fetch_optional(self.pool.as_ref()).await?;
|
||||
let row = builder.build().fetch_optional(self.pool.as_ref()).await?;
|
||||
|
||||
let Some(row) = row else {
|
||||
return Ok(GoalAccountingOutcome::Unchanged(
|
||||
|
||||
Reference in new issue
Block a user