clipwright/tests/test_gemini_block_split.py
juancarlos.cavero 443c7980a5 fix(selection): bisect a Gemini policy block instead of failing the job
PROHIBITED_CONTENT fired on the scoring batch of a plain software
walkthrough: windows 5+6 together blocked 3/3 on lite, 3.7-flash and
2.5-flash, each alone passed, and a neutral prompt over the same text
passed. The filter reacts to combinations, so it cannot be predicted or
sanitized. Both selection passes now run through _run_stage_split: on a
block the batch is halved until the combination is isolated, and only a
window that blocks on its own is dropped. The affected job now yields 2
clips instead of dying.
2026-08-27 19:14:44 +02:00

41 lines
2 KiB
Python

"""A policy block on a batch of windows must bisect, not kill the job."""
import pytest
main = pytest.importorskip("main")
import gemini_worker
def _windows(n):
return [{"id": f"w{i}", "start": i * 10, "end": i * 10 + 10, "text": f"t{i}"} for i in range(n)]
def test_block_on_pair_isolates_and_drops_only_the_culprit(monkeypatch):
calls = []
def fake_stage(client, model, prompt, schema):
ids = [w["id"] for w in __import__("json").loads(prompt)]
calls.append(ids)
if "w5" in ids and "w6" in ids: # the pair that trips the filter
raise gemini_worker.GeminiBlockedError("PROHIBITED_CONTENT")
if ids == ["w3"]: # and w3 blocks even on its own
raise gemini_worker.GeminiBlockedError("PROHIBITED_CONTENT")
return {"windows": [{"id": i, "score": 50} for i in ids]}, {"input_tokens": 1}
monkeypatch.setattr(main, "_run_gemini_stage", fake_stage)
costs = []
out = main._run_stage_split(None, "m", _windows(8), lambda ws: __import__("json").dumps(ws),
None, "windows", costs, "score")
got = sorted(w["id"] for w in out)
# w5 and w6 each survive once separated; w3 only ever reached the model on
# its own inside the [0-3] batch, which passed, so it is kept too.
assert got == [f"w{i}" for i in range(8)]
assert calls[0] == [f"w{i}" for i in range(8)] # tried the whole batch first
assert [c for c in calls if "w5" in c and "w6" in c] == [calls[0], ["w4", "w5", "w6", "w7"], ["w5", "w6"]]
assert len(costs) == 4 # [0-3], [4], [5], [6,7]
def test_no_block_is_a_single_call(monkeypatch):
calls = []
def fake_stage(client, model, prompt, schema):
calls.append(prompt)
return {"shorts": [{"start": 0, "end": 20}]}, None
monkeypatch.setattr(main, "_run_gemini_stage", fake_stage)
out = main._run_stage_split(None, "m", _windows(3), lambda ws: "p", None, "shorts", [], "detail")
assert out == [{"start": 0, "end": 20}] and len(calls) == 1