Files
roboco/tests/unit/runtime/test_grok_rate_limit.py
T
01e10ad693 Feat/provider overload break (#242)
* feat(conventions): standard schema models + effective-map merge

* feat(orchestrator): park provider on persistent server overload (529/500)

A 429 rate limit already parks a provider — queue its spawns, probe until it
recovers — but a persistent 529/500/503 overload had no such break: the run
died and the orchestrator crash-retried straight back into the overload,
burning tokens in a respawn loop.

Generalize the park to provider-unavailability. On a non-graceful Anthropic
agent exit, match the API's overload markers (overloaded_error /
internal_server_error / "API Error: 5xx") against the dead container's own
output and park the provider with kind="overloaded"; the existing spawn gate
already queues any parked provider, and the probe-resume loop revives the task
when it recovers. Grok keeps its exit-75 path; both now route through one
_park_provider_unavailable helper. Markers are kept specific so an agent that
merely writes about HTTP 500/529 can't trip the break.

Fix the recovery probe to require a 2xx: it treated any non-429 as recovered,
so a probe that itself got a 529 would have resumed agents straight back into
the overload — wrong for the new path and for a 429 that lifts into a 5xx.

Gated by ROBOCO_OVERLOAD_BREAK_ENABLED (default on; off => crash-retry).

---------

Co-authored-by: Renn F <rennf93@users.noreply.github.com>
2026-06-22 03:49:45 +02:00

145 lines
4.9 KiB
Python

"""GROK 429 parking: break the 429 -> exit -> respawn cost loop (B4).
A one-shot grok run that hits an xAI 429 exits 75; the orchestrator parks the
grok provider rate-limited instead of crash-retrying, and the spawn guard
suppresses re-spawns until the probe-resume loop clears the park. These tests
exercise the decision points deterministically (tracker + finalize stubbed).
"""
from __future__ import annotations
from unittest.mock import AsyncMock
import pytest
from roboco.models.runtime import AgentInstance
from roboco.runtime.orchestrator import (
_GROK_RATE_LIMIT_EXIT_CODE,
AgentOrchestrator,
AgentState,
)
def _grok_instance(provider_type: str = "grok") -> AgentInstance:
cfg = type("C", (), {"provider_type": provider_type, "model": "grok-build-0.1"})()
inst = AgentInstance(agent_id="be-dev-1", state=AgentState.ACTIVE, config=cfg)
inst.current_task_id = "task-1"
inst.container_id = "cid"
return inst
class _FakeTracker:
def __init__(self, *, limited: bool = False) -> None:
self._limited = limited
self.activated_with: dict[str, object] | None = None
async def is_rate_limited(self) -> bool:
return self._limited
async def activate(
self,
*,
retry_after: float,
affected_agents: list[str],
kind: str = "rate_limited",
) -> None:
self.activated_with = {
"retry_after": retry_after,
"affected_agents": affected_agents,
"kind": kind,
}
def test_is_grok_rate_limit_exit() -> None:
inst = _grok_instance()
assert AgentOrchestrator._is_grok_rate_limit_exit(inst, _GROK_RATE_LIMIT_EXIT_CODE)
# Wrong exit code, or a non-grok provider, is not a grok-429 exit.
assert not AgentOrchestrator._is_grok_rate_limit_exit(inst, 0)
assert not AgentOrchestrator._is_grok_rate_limit_exit(inst, 1)
assert not AgentOrchestrator._is_grok_rate_limit_exit(
_grok_instance(provider_type="anthropic"), _GROK_RATE_LIMIT_EXIT_CODE
)
@pytest.mark.asyncio
async def test_grok_spawn_parked_true_when_limited(
monkeypatch: pytest.MonkeyPatch,
) -> None:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
monkeypatch.setattr(orch, "_make_tracker", lambda _p: _FakeTracker(limited=True))
assert await orch._grok_spawn_parked("grok") is True
@pytest.mark.asyncio
async def test_grok_spawn_parked_false_when_not_limited(
monkeypatch: pytest.MonkeyPatch,
) -> None:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
monkeypatch.setattr(orch, "_make_tracker", lambda _p: _FakeTracker(limited=False))
assert await orch._grok_spawn_parked("grok") is False
@pytest.mark.asyncio
async def test_grok_spawn_parked_false_for_non_grok(
monkeypatch: pytest.MonkeyPatch,
) -> None:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
# Non-grok never consults the tracker (a tracker call would error here).
monkeypatch.setattr(
orch, "_make_tracker", lambda _p: (_ for _ in ()).throw(AssertionError)
)
assert await orch._grok_spawn_parked("anthropic") is False
@pytest.mark.asyncio
async def test_grok_spawn_parked_fails_open(monkeypatch: pytest.MonkeyPatch) -> None:
# A tracker error must never block spawning (fail-open -> False).
def _boom(_p: str) -> object:
raise RuntimeError("redis down")
orch = AgentOrchestrator.__new__(AgentOrchestrator)
monkeypatch.setattr(orch, "_make_tracker", _boom)
assert await orch._grok_spawn_parked("grok") is False
@pytest.mark.asyncio
async def test_park_grok_rate_limited_activates_and_offlines(
monkeypatch: pytest.MonkeyPatch,
) -> None:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
inst = _grok_instance()
inst.error_count = 2 # pretend prior crashes — parking must NOT count one
tracker = _FakeTracker()
monkeypatch.setattr(orch, "_make_tracker", lambda _p: tracker)
finalize = AsyncMock()
monkeypatch.setattr(orch, "_finalize_spawn_session", finalize)
await orch._park_grok_rate_limited("be-dev-1", inst)
finalize.assert_awaited_once()
assert inst.state == AgentState.OFFLINE
assert inst.container_id is None
assert inst.error_count == 0 # a 429 is not a crash
assert tracker.activated_with == {
"retry_after": pytest.approx(60.0),
"affected_agents": ["be-dev-1"],
"kind": "rate_limited",
}
@pytest.mark.asyncio
async def test_handle_stopped_container_parks_on_grok_429(
monkeypatch: pytest.MonkeyPatch,
) -> None:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
inst = _grok_instance()
park = AsyncMock()
finalize = AsyncMock()
monkeypatch.setattr(orch, "_park_grok_rate_limited", park)
monkeypatch.setattr(orch, "_finalize_spawn_session", finalize)
await orch._handle_stopped_container("be-dev-1", inst, _GROK_RATE_LIMIT_EXIT_CODE)
park.assert_awaited_once_with("be-dev-1", inst)
# Early-return: the normal crash/graceful finalize path never runs.
finalize.assert_not_awaited()