clipwright/tests/test_gemini_retry.py
pedro-bento 83ad1feaba
All checks were successful
CI / backend-tests (pull_request) Successful in 30s
CI / frontend-build (pull_request) Successful in 29s
refactor: move backend into the clipwright package
Restructure the 37 root-level Python modules into a proper package:
  clipwright/{core,api,pipeline,layouts,media,integrations,tools}

- Rewrite every local import (module-level and deferred) to absolute
  clipwright.<group>.<mod> form; no legacy root shims or re-exports.
- Add clipwright/core/paths.py and route fonts/assets/cookies through it.
- Entry points: uvicorn clipwright.api.app:app; jobs run as
  python -m clipwright.pipeline.main; probe as -m ...quality_probe.
- child_env() appends REPO_ROOT to PYTHONPATH so the -m workers work.
- transcribe_backends: sys.modules key now clipwright.media.scene_detection.
- Tooling: root pyproject.toml (pytest pythonpath=[.], ruff), shared
  requirements-test.txt, CI installs it, Docker PYTHONPATH=/app.
- Tests: all imports/fakes/source-scans repointed; new tests/test_package_layout.py
  enforces no root modules, docstring-only __init__, and no legacy imports.
- Docs/comments updated for the new layout.

Backend: 744 passed, 19 skipped. Frontend lint+build green.
2026-10-04 20:40:38 +01:00

146 lines
4.2 KiB
Python

"""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
# main pulls in cv2/torch/mediapipe at import time; the minimal CI env lacks
# them, so skip there. Runs fully in the container/local where deps exist.
main = pytest.importorskip("clipwright.pipeline.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
class _BlockedResponse:
"""Mimics the real prod shape: 200, empty candidates, block_reason set."""
text = ""
candidates = []
usage_metadata = None
class _PF:
class _Reason:
name = "PROHIBITED_CONTENT"
block_reason = _Reason()
prompt_feedback = _PF()
def test_policy_block_fails_fast_without_retrying():
# Prod 23-jul-2026: PROHIBITED_CONTENT is deterministic — 3 retries just
# burned quota and reported a misleading "empty response body".
class _Models:
calls = 0
def generate_content(self, **kwargs):
_Models.calls += 1
return _BlockedResponse()
from clipwright.pipeline import gemini_worker
with pytest.raises(gemini_worker.GeminiBlockedError) as exc:
main._run_gemini_stage(_client(_Models()), "m", "prompt", object)
assert _Models.calls == 1
assert "PROHIBITED_CONTENT" in str(exc.value)
def test_blocked_finish_reason_also_raises():
from clipwright.pipeline import gemini_worker
class _Cand:
class _FR:
name = "SAFETY"
finish_reason = _FR()
content = None
class _Resp:
prompt_feedback = None
candidates = [_Cand()]
with pytest.raises(gemini_worker.GeminiBlockedError):
gemini_worker.raise_if_blocked(_Resp())
def test_clean_response_is_not_flagged_as_blocked():
from clipwright.pipeline import gemini_worker
class _Resp:
prompt_feedback = None
candidates = []
gemini_worker.raise_if_blocked(_Resp()) # must not raise