Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(model-hub): end at once when a retry falls due at the recovery window's end (#2834)

Call Whitedd4341804cf3
diff --git a/core/handlers/model_hub/retry.py b/core/handlers/model_hub/retry.py
index 9b28b2614..36df644f3 100644
--- a/core/handlers/model_hub/retry.py
+++ b/core/handlers/model_hub/retry.py
@@ -19,6 +19,11 @@ from .resolver import SourceRecoveryAnnotation, parse_model_hub_timestamp
logger = logging.getLogger(__name__)
RECOVERY_WINDOW_SECONDS = 120.0
+# A wait must end with time left to attempt inside the window. Retry-After has one-second resolution, and the window
+# opens at the refusal, after the response its advice counts from: advice equal to the window lands a few
+# milliseconds inside it, and the attempt it was waited for would find the window closed. So a retry due within the
+# window's last second is one the window does not admit.
+RECOVERY_ADMISSION_MARGIN_SECONDS = 1.0
RECOVERY_EXHAUSTED_CODE = "model_hub_recovery_exhausted"
RECOVERY_EXHAUSTED_MESSAGE = "Automatic recovery has ended. Try again or choose another model."
RETRY_DELAYS = {
diff --git a/core/handlers/model_hub/service.py b/core/handlers/model_hub/service.py
index 6824af931..78f268457 100644
--- a/core/handlers/model_hub/service.py
+++ b/core/handlers/model_hub/service.py
@@ -237,7 +237,14 @@ from .resolver import (
source_runnable,
)
from .revocations import CredentialRevocationJournal
-from .retry import RECOVERY_EXHAUSTED_CODE, RecoveryPolicy, RecoveryRequest, RETRY_DELAYS, source_identity
+from .retry import (
+ RECOVERY_ADMISSION_MARGIN_SECONDS,
+ RECOVERY_EXHAUSTED_CODE,
+ RecoveryPolicy,
+ RecoveryRequest,
+ RETRY_DELAYS,
+ source_identity,
+)
from .usage import (
USAGE_DEFAULT_WINDOW_DAYS,
USAGE_WINDOW_KEYS,
@@ -10956,7 +10963,7 @@ class ModelHubService:
pending.source_id = hop.source_id
annotation = annotations.get(hop.source_id)
pending.reason = annotation.reason if annotation else pending.reason
- if delay >= pending.remaining:
+ if delay >= pending.remaining - RECOVERY_ADMISSION_MARGIN_SECONDS:
# Another admitted owner can still wake this request early.
if not any(hop.recovery == "in_flight" for hop in waitable):
raise self._recovery_exhausted(exc, pending.reason) from exc
diff --git a/tests/test_model_hub_retry_policy.py b/tests/test_model_hub_retry_policy.py
index bba1450a5..e81512d69 100644
--- a/tests/test_model_hub_retry_policy.py
+++ b/tests/test_model_hub_retry_policy.py
@@ -4,6 +4,7 @@ from __future__ import annotations
import asyncio
import copy
+from dataclasses import replace
from datetime import datetime, timedelta
from email.utils import format_datetime
from types import SimpleNamespace
@@ -231,6 +232,39 @@ def test_fallback_success_does_not_clear_primary_and_eligibility_is_not_recovery
asyncio.run(run())
[email protected](("advice", "waited"), [("120", []), ("119", [118.95])], ids=["at-the-window-end", "inside"])
+def test_a_retry_the_window_cannot_admit_ends_the_request_at_once(tmp_path, advice, waited):
+ """MH-RETRY-WINDOW-001: the window opens at the refusal, after the response its Retry-After counts from, so advice
+ equal to the window lands just inside it. Waiting for it would end the window as the retry fell due, with no
+ retry; the request ends at once instead, keeping the cooldown. Advice a second shorter is waited for and tried."""
+
+ async def run():
+ refused = replace(
+ _outcome(RawOutcomeKind.HTTP_ERROR, status=429, source_id="src_recovery01"),
+ retry_after=advice, response_received_at=NOW - timedelta(seconds=0.05),
+ )
+ service, clock = clock_service(tmp_path, outcomes=[
+ refused, _outcome(RawOutcomeKind.SUCCESS, source_id="src_recovery01", stream_started=True),
+ ])
+
+ async def late_sleep(seconds):
+ # A real wake lands after its timer: the loop, then the lock, before the attempt is admitted.
+ await clock.sleep(seconds)
+ clock.advance(0.1)
+
+ service.recovery.sleep = late_sleep
+ if waited:
+ result = await service.resolve_with_recovery(backend="codex", model_id="shared-model", request={})
+ assert result.source_id == "src_recovery01"
+ else:
+ with pytest.raises(ModelHubError) as raised:
+ await service.resolve_with_recovery(backend="codex", model_id="shared-model", request={})
+ assert raised.value.code == RECOVERY_EXHAUSTED_CODE
+ assert clock.delays == pytest.approx(waited)
+ assert len(service.adapter.invocations) == 1 + len(waited)
+ asyncio.run(run())
+
+
def test_one_window_covers_all_fallback_passes_without_replaying_after_output(tmp_path):
"""MH-RETRY-WINDOW-001: admission shares one clock across failed attempts."""
async def run():