"""Retry behaviour of the Gemini scoring/detail stage. Prod (22-jul-2026) lost 3 jobs to Gemini answering 200 with an empty body. That raises while parsing, not while calling, so it used to escape the retry loop and kill the job on the first blip. """ import types import pytest import main class _FakeResponse: def __init__(self, parsed=None): self.parsed = parsed self.candidates = [] self.usage_metadata = None @property def text(self): return "" if self.parsed is None else "{}" class _FakeModels: """Returns an empty body for the first `blips` calls, then a good one.""" def __init__(self, blips, payload=None): self.blips = blips self.calls = 0 self.payload = payload if payload is not None else {"windows": [{"id": "w0", "score": 90}]} def generate_content(self, **kwargs): self.calls += 1 if self.calls <= self.blips: return _FakeResponse(parsed=None) return _FakeResponse(parsed=self.payload) def _client(models): return types.SimpleNamespace(models=models) @pytest.fixture(autouse=True) def _no_sleep(monkeypatch): monkeypatch.setattr(main.time, "sleep", lambda *_: None) def test_recovers_from_a_single_empty_body(monkeypatch): models = _FakeModels(blips=1) parsed, _cost = main._run_gemini_stage(_client(models), "m", "prompt", object) assert models.calls == 2 assert parsed["windows"][0]["score"] == 90 def test_recovers_from_two_consecutive_blips(): models = _FakeModels(blips=2) parsed, _cost = main._run_gemini_stage(_client(models), "m", "prompt", object) assert models.calls == 3 assert parsed["windows"] def test_gives_up_after_three_attempts(): models = _FakeModels(blips=99) with pytest.raises(Exception) as exc: main._run_gemini_stage(_client(models), "m", "prompt", object) assert models.calls == 3 assert "empty response body" in str(exc.value) def test_non_transient_errors_are_not_retried(): class _Boom: calls = 0 def generate_content(self, **kwargs): _Boom.calls += 1 raise ValueError("400 INVALID_ARGUMENT: bad request") with pytest.raises(ValueError): main._run_gemini_stage(_client(_Boom()), "m", "prompt", object) assert _Boom.calls == 1 def test_succeeds_without_retrying_when_the_first_call_is_fine(): models = _FakeModels(blips=0) main._run_gemini_stage(_client(models), "m", "prompt", object) assert models.calls == 1