-
Notifications
You must be signed in to change notification settings - Fork 11.4k
feat: skip memory startup when Codex rate limits are low #19990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+214
−2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| use codex_backend_client::Client as BackendClient; | ||
| use codex_core::config::Config; | ||
| use codex_login::AuthManager; | ||
| use codex_protocol::protocol::RateLimitSnapshot; | ||
| use codex_protocol::protocol::RateLimitWindow; | ||
| use tracing::info; | ||
| use tracing::warn; | ||
|
|
||
| const CODEX_LIMIT_ID: &str = "codex"; | ||
|
|
||
| pub(crate) async fn rate_limits_ok(auth_manager: &AuthManager, config: &Config) -> bool { | ||
| rate_limits_check(auth_manager, config) | ||
| .await | ||
| .unwrap_or(true) | ||
| } | ||
|
|
||
| async fn rate_limits_check(auth_manager: &AuthManager, config: &Config) -> Option<bool> { | ||
| let auth = auth_manager.auth().await?; | ||
| if !auth.uses_codex_backend() { | ||
| return None; | ||
| } | ||
|
|
||
| let client = BackendClient::from_auth(config.chatgpt_base_url.clone(), &auth) | ||
| .map_err(|err| warn!(%err, "failed to construct backend client")) | ||
| .ok()?; | ||
|
|
||
| let snapshots = client | ||
| .get_rate_limits_many() | ||
| .await | ||
| .map_err(|err| warn!(%err, "failed to fetch rate limits")) | ||
| .ok()?; | ||
|
|
||
| let snapshot = snapshots | ||
| .iter() | ||
| .find(|s| s.limit_id.as_deref() == Some(CODEX_LIMIT_ID)) | ||
| .or_else(|| snapshots.first())?; | ||
|
|
||
| let min_remaining_percent = config.memories.min_rate_limit_remaining_percent; | ||
| let allowed = snapshot_allows_startup(snapshot, min_remaining_percent); | ||
|
|
||
| if !allowed { | ||
| info!( | ||
| min_remaining_percent, | ||
| "skipping memories startup because Codex rate limits are below the configured threshold" | ||
| ); | ||
| } | ||
|
|
||
| Some(allowed) | ||
| } | ||
|
|
||
| fn snapshot_allows_startup(snapshot: &RateLimitSnapshot, min_remaining_percent: i64) -> bool { | ||
| if snapshot.rate_limit_reached_type.is_some() { | ||
| return false; | ||
| } | ||
|
|
||
| let max_used_percent = 100.0 - min_remaining_percent.clamp(0, 100) as f64; | ||
| window_allows_startup(snapshot.primary.as_ref(), max_used_percent) | ||
| && window_allows_startup(snapshot.secondary.as_ref(), max_used_percent) | ||
| } | ||
|
|
||
| fn window_allows_startup(window: Option<&RateLimitWindow>, max_used_percent: f64) -> bool { | ||
| match window { | ||
| Some(window) => window.used_percent <= max_used_percent, | ||
| None => true, | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[path = "guard_tests.rs"] | ||
| mod tests; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| use super::*; | ||
| use codex_protocol::protocol::RateLimitReachedType; | ||
|
|
||
| fn snapshot( | ||
| primary_used_percent: Option<f64>, | ||
| secondary_used_percent: Option<f64>, | ||
| ) -> RateLimitSnapshot { | ||
| RateLimitSnapshot { | ||
| limit_id: Some(CODEX_LIMIT_ID.to_string()), | ||
| limit_name: None, | ||
| primary: primary_used_percent.map(window), | ||
| secondary: secondary_used_percent.map(window), | ||
| credits: None, | ||
| plan_type: None, | ||
| rate_limit_reached_type: None, | ||
| } | ||
| } | ||
|
|
||
| fn window(used_percent: f64) -> RateLimitWindow { | ||
| RateLimitWindow { | ||
| used_percent, | ||
| window_minutes: None, | ||
| resets_at: None, | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn startup_check_uses_configured_remaining_threshold() { | ||
| let snapshot = snapshot( | ||
| /*primary_used_percent*/ Some(89.9), | ||
| /*secondary_used_percent*/ Some(50.0), | ||
| ); | ||
|
|
||
| assert!(snapshot_allows_startup( | ||
| &snapshot, /*min_remaining_percent*/ 10 | ||
| )); | ||
| assert!(!snapshot_allows_startup( | ||
| &snapshot, /*min_remaining_percent*/ 11 | ||
| )); | ||
| } | ||
|
|
||
| #[test] | ||
| fn startup_check_skips_when_primary_or_secondary_is_too_low() { | ||
| assert!(!snapshot_allows_startup( | ||
| &snapshot( | ||
| /*primary_used_percent*/ Some(75.1), | ||
| /*secondary_used_percent*/ Some(10.0), | ||
| ), | ||
| /*min_remaining_percent*/ 25, | ||
| )); | ||
| assert!(!snapshot_allows_startup( | ||
| &snapshot( | ||
| /*primary_used_percent*/ Some(10.0), | ||
| /*secondary_used_percent*/ Some(75.1), | ||
| ), | ||
| /*min_remaining_percent*/ 25, | ||
| )); | ||
| assert!(snapshot_allows_startup( | ||
| &snapshot( | ||
| /*primary_used_percent*/ Some(74.9), | ||
| /*secondary_used_percent*/ Some(74.9), | ||
| ), | ||
| /*min_remaining_percent*/ 25, | ||
| )); | ||
| } | ||
|
|
||
| #[test] | ||
| fn startup_check_skips_when_limit_is_reached() { | ||
| let mut snapshot = snapshot( | ||
| /*primary_used_percent*/ Some(10.0), | ||
| /*secondary_used_percent*/ Some(10.0), | ||
| ); | ||
| snapshot.rate_limit_reached_type = Some(RateLimitReachedType::RateLimitReached); | ||
|
|
||
| assert!(!snapshot_allows_startup( | ||
| &snapshot, /*min_remaining_percent*/ 25, | ||
| )); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| mod control; | ||
| mod extensions; | ||
| mod guard; | ||
| mod phase1; | ||
| mod phase2; | ||
| mod prompts; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.