Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(agent): give a throttled model call a retry schedule that outlasts the throttle

M3gA-Mindfd2cc61e4002
diff --git a/crates/openhuman-core/src/agent/tinyagents/tinyagents_tests.rs b/crates/openhuman-core/src/agent/tinyagents/tinyagents_tests.rs
index 7e52d451a5..1d31c04cfa 100644
--- a/crates/openhuman-core/src/agent/tinyagents/tinyagents_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/tinyagents_tests.rs
@@ -144,3 +144,75 @@ fn default_per_call_ceiling_sits_under_the_default_turn_ceiling() {
"per-call ceiling ({per_call} ms) must be under the turn ceiling ({turn} ms)"
);
}
+
+/// #6413: the retry schedule must outlast a throttled upstream.
+///
+/// These pin the two numbers that were wrong — the attempt count and the total
+/// time spent waiting — rather than "it retried", which passed on the old
+/// 2-retry/1.5 s schedule and would pass again if someone shortened it.
+#[test]
+fn run_policy_retry_schedule_outlasts_a_throttled_upstream() {
+ use tinyagents_harness::retry::JITTER_FRACTION;
+
+ let policy = run_policy_for(10, false);
+ let retry = &policy.retry;
+
+ // 4 retries (5 attempts). The issue asks for "at least 3".
+ assert_eq!(retry.max_attempts, 5, "attempts (first try + retries)");
+ assert!(
+ retry.backoff_sleep,
+ "a retry that does not wait is not a backoff"
+ );
+
+ // The harness applies `max_attempts_capped_at(limits.max_retries_per_call)`
+ // at the model call, and `RunLimits` defaults that to 3 — so without this the
+ // policy above is silently clamped to 4 attempts and the change is a no-op.
+ // This is the assertion that catches a "fix" that raised only `max_attempts`.
+ assert!(
+ policy.limits.max_retries_per_call + 1 >= retry.max_attempts,
+ "max_attempts {} is clamped by max_retries_per_call {} — the schedule would \
+ silently lose an attempt",
+ retry.max_attempts,
+ policy.limits.max_retries_per_call,
+ );
+
+ // Total time actually spent waiting, on the WORST jitter draw. `rand01 = 0.0`
+ // is the bottom of the band (`base * (1 - JITTER_FRACTION)`), which is the
+ // figure that has to clear the issue's floor — the nominal schedule clearing
+ // it while the floor does not would be an intermittent failure to retry long
+ // enough, and those are the ones nobody reproduces.
+ let retries = retry.max_attempts - 1;
+ let worst: std::time::Duration = (0..retries)
+ .map(|attempt| retry.backoff_for_attempt_with(attempt, 0.0))
+ .sum();
+ assert!(
+ worst >= std::time::Duration::from_secs(30),
+ "worst-case backoff across {retries} retries is {:?}; #6413 requires >= 30s",
+ worst,
+ );
+
+ // And the nominal (mid-band) schedule, so a future edit that satisfies the
+ // floor by widening jitter rather than lengthening the curve still fails.
+ let nominal: std::time::Duration = (0..retries)
+ .map(|attempt| retry.backoff_for_attempt_with(attempt, 0.5))
+ .sum();
+ assert!(
+ nominal >= std::time::Duration::from_secs(40),
+ "nominal backoff is {nominal:?}; expected >= 40s so the jittered floor has margin",
+ );
+
+ // Jitter on: a throttled fleet must not retry in lockstep on one curve.
+ assert!(
+ retry.jitter,
+ "identical curves keep a saturated upstream saturated"
+ );
+ assert!(JITTER_FRACTION > 0.0, "jitter must actually widen the band");
+
+ // Both ceilings stay bounded — this must not become an unbounded retry
+ // (cf. #6412, the opposite failure in another crate).
+ assert!(retry.max_attempts < 10, "attempts must stay bounded");
+ assert!(
+ retry.max_retry_after_ms > 0 && retry.max_retry_after_ms <= 300_000,
+ "a server Retry-After must be honoured but capped, not obeyed without limit"
+ );
+}
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs b/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs
index da62e8e437..9818625973 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs
@@ -113,9 +113,10 @@ pub(super) fn parse_model_call_wall_clock_ms(env_value: Option<&str>) -> Option<
/// Retry is now owned by the crate [`RetryPolicy`] (issue #4249, Phase 3a): the
/// turn path no longer wraps its provider in `ReliableProvider` (removed in
/// `session/builder/factory.rs`), so the single retry layer is here, at the
-/// harness model call. The schedule mirrors the former `ReliableProvider`
-/// defaults — 2 retries (3 attempts) with 500 ms exponential backoff — so
-/// transient 429/5xx behavior is preserved. Retryability is decided by the crate
+/// harness model call. The schedule is **4 retries over ~45 s** (issue #6413);
+/// it used to mirror the former `ReliableProvider` defaults — 2 retries with
+/// 500 ms backoff, about 1.5 s in total — which was far too short to outlast a
+/// throttled upstream. Retryability is decided by the crate
/// `is_retryable`, which the [`native model adapter`](super::model) adapter feeds
/// correctly: a permanent config/auth/quota/context error is mapped to a
/// non-retryable `TinyAgentsError::Validation`, a transient blip to a retryable
@@ -162,15 +163,42 @@ pub(crate) fn run_policy_for(max_iterations: usize, response_cache_enabled: bool
// delegations) are exempt in the harness and keep the remainder-only
// budget. Env-overridable, `0` disables.
policy.limits.max_model_call_ms = model_call_wall_clock_ms();
- // Crate-owned retry (Phase 3a): mirror the former `ReliableProvider` schedule
- // (2 retries, 500 ms exponential backoff). `backoff_sleep` is on so a
- // transient 429/5xx actually waits before retrying, as it did before.
+ // Crate-owned retry (Phase 3a), lengthened for #6413.
+ //
+ // The former schedule — 2 retries at 500 ms and 1 s — spent about 1.5 s in
+ // total, which is nothing against a throttled upstream: a managed 429 killed
+ // the turn while the provider was still rate-limiting. 4 retries at 3/6/12/24 s
+ // spend ~45 s, and ~33.75 s even on the worst jitter draw (`JITTER_FRACTION`
+ // is 0.25, so each delay lands in `[base * 0.75, base * 1.25]`) — the floor is
+ // what has to clear the issue's "at least 3 retries over >=30 s", not the
+ // nominal figure.
+ //
+ // `max_retries_per_call` is raised alongside it and that line is load-bearing:
+ // `RunLimits` defaults it to 3, and the harness applies
+ // `max_attempts_capped_at(max_retries_per_call)` at the model call
+ // (`agent_loop/model_call.rs`), so `max_attempts` above 4 is **silently
+ // clamped** without it. Raising `max_attempts` alone would have looked like a
+ // fix and changed nothing.
+ //
+ // The first retry moves 500 ms -> 3 s, which is the deliberate cost: one curve
+ // serves every retryable class, so a transient 5xx now waits 3 s too. Buying a
+ // fast first retry *and* a 30 s tail needs per-class backoff, which lives in
+ // the crate's `backoff_for_error` rather than here.
+ //
+ // Both ceilings stay bounded — `max_attempts` and `max_retry_after_ms` — so
+ // this adds no unbounded retry (cf. #6412, which is the opposite failure in
+ // `vendor/tinymcp`: a different crate and loop, untouched by this).
+ //
+ // `jitter` is now on: a fleet throttled together previously retried in
+ // lockstep on an identical curve, which is the shape that keeps an upstream
+ // saturated. Jitter only widens the band; it never removes the delay.
+ policy.limits.max_retries_per_call = 4;
policy.retry = RetryPolicy {
- max_attempts: 3,
- initial_backoff_ms: 500,
+ max_attempts: 5,
+ initial_backoff_ms: 3_000,
max_backoff_ms: 30_000,
multiplier: 2.0,
- jitter: false,
+ jitter: true,
backoff_sleep: true,
max_retry_after_ms: RetryPolicy::DEFAULT_MAX_RETRY_AFTER_MS,
retry_on: None,