Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(bootstrap): wait for the provider's Retry-After before retrying a chunk

Carlos Felipe730f0f97c919
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 12202b0f..dda71dca 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,11 @@ version in [`docs/changelog/`](docs/changelog/):
### Fixed
+- `bootstrap` now waits for a provider's `Retry-After` before retrying a
+ chunk, capped at 32 s like the remote embedders (#1397). It retried after a
+ fixed 2 s and gave up after three attempts, so a rate-limited hosted API
+ (DeepSeek, OpenAI) failed the run while it was still asking the client to
+ wait. Without a hint the fixed 2 s delay is unchanged (#PR).
- Project search links now use the project's current stored name after
resolving a canonical or legacy key. Results, breadcrumbs, and the scope
selector stay on that project without renaming it (#1875).
diff --git a/crates/ai-memory-consolidate/src/bootstrap.rs b/crates/ai-memory-consolidate/src/bootstrap.rs
index 46337c1c..f30e3ae7 100644
--- a/crates/ai-memory-consolidate/src/bootstrap.rs
+++ b/crates/ai-memory-consolidate/src/bootstrap.rs
@@ -329,6 +329,18 @@ const BOOTSTRAP_CHUNK_MAX_ATTEMPTS: u32 = 3;
/// Fixed, short delay between chunk retries. Deliberately not tenacity-style
/// escalating backoff — see the cognee #2840 lesson in `ai-memory-llm`.
const BOOTSTRAP_CHUNK_RETRY_DELAY: Duration = Duration::from_secs(2);
+/// Ceiling on a provider-requested wait, the same one the remote embedders
+/// apply to `Retry-After` (#1397), so one bad hint cannot park a run.
+const BOOTSTRAP_CHUNK_MAX_RETRY_AFTER: Duration = Duration::from_secs(32);
+
+/// Wait before retrying a chunk after `err`. A provider that names its own
+/// delay is waited for, up to [`BOOTSTRAP_CHUNK_MAX_RETRY_AFTER`]: retrying
+/// sooner than a rate-limited provider asked only spends the attempt on
+/// another `429`. Without a hint the fixed `fallback` applies, as before.
+fn chunk_retry_wait(err: &LlmError, fallback: Duration) -> Duration {
+ err.retry_after()
+ .map_or(fallback, |hint| hint.min(BOOTSTRAP_CHUNK_MAX_RETRY_AFTER))
+}
/// Run one chunk's structured LLM call with a short, bounded retry.
///
@@ -357,13 +369,15 @@ async fn complete_chunk_with_retry(
{
Ok(batch) => return Ok(batch),
Err(e) if attempt < BOOTSTRAP_CHUNK_MAX_ATTEMPTS && e.is_transient() => {
+ let wait = chunk_retry_wait(&e, retry_delay);
warn!(
attempt,
max = BOOTSTRAP_CHUNK_MAX_ATTEMPTS,
+ wait_ms = u64::try_from(wait.as_millis()).unwrap_or(u64::MAX),
error = %e,
"bootstrap chunk hit a transient LLM error; retrying shortly",
);
- tokio::time::sleep(retry_delay).await;
+ tokio::time::sleep(wait).await;
attempt += 1;
}
Err(e) => return Err(e),
@@ -1718,6 +1732,7 @@ mod tests {
ids: std::sync::Mutex<Vec<LlmOperationId>>,
transient_failures: usize,
permanent: bool,
+ retry_after: Option<Duration>,
}
#[async_trait::async_trait]
@@ -1747,7 +1762,7 @@ mod tests {
return Err(LlmError::Provider {
status: 503,
body: "busy".into(),
- retry_after: None,
+ retry_after: self.retry_after,
});
}
Ok(serde_json::json!({ "pages": [], "rationale": "ok" }))
@@ -1780,6 +1795,7 @@ mod tests {
ids: std::sync::Mutex::new(Vec::new()),
transient_failures: 2, // fails twice, succeeds on the 3rd (= MAX)
permanent: false,
+ retry_after: None,
};
let batch = complete_chunk_with_retry(&llm, chunk_request(), Duration::ZERO)
.await
@@ -1803,6 +1819,7 @@ mod tests {
ids: std::sync::Mutex::new(Vec::new()),
transient_failures: usize::MAX, // never clears
permanent: false,
+ retry_after: None,
};
let err = complete_chunk_with_retry(&llm, chunk_request(), Duration::ZERO)
.await
@@ -1822,6 +1839,7 @@ mod tests {
ids: std::sync::Mutex::new(Vec::new()),
transient_failures: 0,
permanent: true, // returns Auth — not transient
+ retry_after: None,
};
let err = complete_chunk_with_retry(&llm, chunk_request(), Duration::ZERO)
.await
@@ -1834,6 +1852,52 @@ mod tests {
);
}
+ /// A provider hint replaces the fixed delay, clamped to the same ceiling
+ /// the remote embedders use; without one the fixed delay is unchanged.
+ #[test]
+ fn chunk_retry_wait_honours_a_capped_provider_hint() {
+ let hinted = |hint| LlmError::Provider {
+ status: 429,
+ body: "slow down".into(),
+ retry_after: Some(hint),
+ };
+ let fixed = Duration::from_secs(2);
+ assert_eq!(chunk_retry_wait(&LlmError::Auth("x".into()), fixed), fixed);
+ assert_eq!(
+ chunk_retry_wait(&hinted(Duration::from_secs(10)), fixed),
+ Duration::from_secs(10)
+ );
+ assert_eq!(
+ chunk_retry_wait(&hinted(Duration::from_secs(3600)), fixed),
+ BOOTSTRAP_CHUNK_MAX_RETRY_AFTER,
+ "a huge hint must not park the run"
+ );
+ }
+
+ /// A rate-limited provider that says when to come back is not retried
+ /// before then, even when the fixed delay is zero: retrying earlier only
+ /// spends the attempt on another 429.
+ #[tokio::test]
+ async fn chunk_retry_waits_for_the_providers_retry_after() {
+ let llm = FlakyLlm {
+ calls: AtomicUsize::new(0),
+ ids: std::sync::Mutex::new(Vec::new()),
+ transient_failures: 1,
+ permanent: false,
+ retry_after: Some(Duration::from_millis(150)),
+ };
+ let started = std::time::Instant::now();
+ complete_chunk_with_retry(&llm, chunk_request(), Duration::ZERO)
+ .await
+ .expect("the retry after the hinted wait succeeds");
+ assert_eq!(llm.calls.load(Ordering::SeqCst), 2);
+ assert!(
+ started.elapsed() >= Duration::from_millis(150),
+ "retried after {:?}, before the provider's Retry-After",
+ started.elapsed()
+ );
+ }
+
fn make_repo(tmp: &Path) -> Result<(), Box<dyn std::error::Error>> {
// Use system git via std::process::Command instead of git2's
// signature/config plumbing, which would force us to set a