Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

A rate-limited AI service is waited for as long as it asks (#148)

BFlataae724f14b2c
diff --git a/crates/ai/src/rebuild/mod.rs b/crates/ai/src/rebuild/mod.rs
index a49ecd7b..e41a9ada 100644
--- a/crates/ai/src/rebuild/mod.rs
+++ b/crates/ai/src/rebuild/mod.rs
@@ -789,9 +789,20 @@ impl Worker {
Err(e) if e.code != ErrorCode::Cancelled && tries < 3 && agent.session_id().is_some() && worth_retrying(&e.message) => {
tries += 1;
let why = e.message.chars().take(160).collect::<String>();
- self.step(ws, Step::Build, Status::Building, &format!("The builder stopped ({why}). Picking up where it left off…"))?;
- // Give a dropped connection or a busy service a moment, longer each time.
- for _ in 0..30 * tries {
+ // A service that says how long to wait is waited for; otherwise a dropped
+ // connection or a busy service gets a moment, longer each time.
+ let wait = match retry_after(&e.message) {
+ Some(secs) => {
+ let note = format!("The AI service is busy or its rate limit was reached, and asked to wait {secs} s. Trying again then…");
+ self.step(ws, Step::Build, Status::Building, &note)?;
+ secs + 1
+ }
+ None => {
+ self.step(ws, Step::Build, Status::Building, &format!("The builder stopped ({why}). Picking up where it left off…"))?;
+ 30 * tries
+ }
+ };
+ for _ in 0..wait {
self.stop.check()?;
std::thread::sleep(Duration::from_secs(1));
}
@@ -915,3 +926,27 @@ fn worth_retrying(message: &str) -> bool {
let m = message.to_ascii_lowercase();
!["sign in", "log in", "login", "logged in", "auth", "credit", "billing", "usage limit", "not installed", "install "].iter().any(|k| m.contains(k))
}
+
+/// The longest wait a service's "retry in …" is followed for.
+const MAX_RETRY_WAIT: u64 = 15 * 60;
+
+/// How long a busy or rate-limited service asked to wait ("Please retry in 19.05s", "retry after
+/// 2 minutes"), in whole seconds rounded up and at most [`MAX_RETRY_WAIT`] (#142).
+fn retry_after(message: &str) -> Option<u64> {
+ let m = message.to_ascii_lowercase();
+ let at = ["retry in ", "retry after ", "try again in "].iter().find_map(|p| m.find(p).map(|i| i + p.len()))?;
+ let rest = m.get(at..)?;
+ let number: String = rest.chars().take_while(|c| c.is_ascii_digit() || *c == '.').collect();
+ let value: f64 = number.parse().ok()?;
+ let unit = rest.get(number.len()..).unwrap_or_default().trim_start();
+ let seconds = if unit.starts_with("ms") || unit.starts_with("millisecond") {
+ value / 1000.0
+ } else if unit.starts_with("min") || (unit.starts_with('m') && !unit.starts_with("ms")) {
+ value * 60.0
+ } else if unit.starts_with('h') {
+ value * 3600.0
+ } else {
+ value
+ };
+ Some((seconds.ceil().max(1.0) as u64).min(MAX_RETRY_WAIT))
+}
diff --git a/crates/ai/src/rebuild/tests.rs b/crates/ai/src/rebuild/tests.rs
index c777b485..69698f3a 100644
--- a/crates/ai/src/rebuild/tests.rs
+++ b/crates/ai/src/rebuild/tests.rs
@@ -333,6 +333,20 @@ fn a_cut_off_turn_is_resumed_unless_the_person_must_act() {
assert_eq!(RunOptions::default().max_rounds, 6, "room to keep fixing until it works");
}
+/// #142: a rate-limited service's own wait is followed (rounded up, capped), not a fixed one.
+#[test]
+fn a_services_requested_wait_is_read() {
+ assert_eq!(retry_after("OpenCode stopped with exit code 1: Please retry in 19.052906498s."), Some(20));
+ assert_eq!(retry_after("Rate limit reached. Retry after 2 minutes"), Some(120));
+ assert_eq!(retry_after("try again in 500ms"), Some(1));
+ assert_eq!(retry_after("Please try again in 3h"), Some(MAX_RETRY_WAIT));
+ assert_eq!(retry_after("Retry in 45 seconds"), Some(45));
+ for no_wait in ["API Error: 529 overloaded", "please retry in a moment", "retry in ."] {
+ assert_eq!(retry_after(no_wait), None, "{no_wait}");
+ }
+ assert!(worth_retrying("Please retry in 19.05s"), "a rate limit is waited out, not handed to the person");
+}
+
#[test]
fn games_get_the_mechanics_sheet_and_decompile_only_in_deep_mode() {
let d = tempfile::tempdir().unwrap();