Skip to content

Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.

test(gen): a wait that wakes at the time limit or past it sends nothing

Soohong Kim12d54fb3f827
diff --git a/tests/gen/test_grok_provider.py b/tests/gen/test_grok_provider.py
index 9d54363..0b51a09 100644
--- a/tests/gen/test_grok_provider.py
+++ b/tests/gen/test_grok_provider.py
@@ -151,6 +151,31 @@ def test_a_rate_limit_is_sent_again_only_within_the_time_limit_of_the_image(tmp_
"the 16s wait would reach the time limit (12s left)") in capsys.readouterr().err
+def test_a_rate_limit_whose_wait_woke_past_the_time_limit_is_not_sent_again(tmp_path, api, monkeypatch, capsys):
+ """A sleep may wake later than asked: with 8.01 s, the 8 s wait after a 429 woke 20 ms late, past the
+ image's time limit. Nothing is sent then: the 429 ends the call as it came and its stderr line says why —
+ not a send that finds no time left and ends as a request the server may have taken."""
+ now = [0.0]
+
+ def sleep(seconds):
+ api["slept"].append(seconds)
+ now[0] += seconds + 0.020
+
+ for module in (grok, retry, xai):
+ monkeypatch.setattr(module, "time", SimpleNamespace(monotonic=lambda: now[0]))
+ monkeypatch.setattr(retry, "_sleep", sleep)
+ monkeypatch.setattr(grok, "GEN_TIMEOUT_SECONDS", 8.010)
+ api["script"] = [(429, {"code": "resource-exhausted", "error": "slow down"}), (200, api["body"])]
+ with pytest.raises(SystemExit) as error:
+ gen.generate_image("grok", "x", tmp_path / "out.png")
+ assert len(api["calls"]) == 1 and api["slept"] == [8.0]
+ assert "(HTTP 429) code=resource-exhausted; no retry or provider fallback" in str(error.value)
+ assert capsys.readouterr().err.splitlines()[-1] == (
+ "[gen] grok refused the request (429 resource-exhausted); nothing was made — not asking again: "
+ "the time limit came during the 8s wait")
+ assert not (tmp_path / "out.png").exists()
+
+
@pytest.mark.parametrize("body", [{}, {"data": []}, {"data": [None]}, {"data": [{}, {}]},
{"data": [{"b64_json": "???"}]},
{"data": [{"b64_json": base64.b64encode(b"invalid image").decode()}]},
diff --git a/tests/gen/test_openai_provider.py b/tests/gen/test_openai_provider.py
index a8a6592..06018f3 100644
--- a/tests/gen/test_openai_provider.py
+++ b/tests/gen/test_openai_provider.py
@@ -244,6 +244,38 @@ def test_a_refusal_is_sent_again_only_within_the_time_limit_of_the_image(tmp_pat
assert not (tmp_path / "out.png").exists()
[email protected]("call", ["image", "facing check"])
+def test_a_refusal_whose_wait_woke_past_the_time_limit_is_not_sent_again(tmp_path, api, monkeypatch, capsys, call):
+ """A sleep may wake later than asked: with 8.01 s, the 8 s wait after a 429 woke 20 ms late, past the time
+ limit. The limit is read again right before the send, so nothing is sent and the 429 ends the call as it
+ came, its stderr line saying why — not a send that finds no time left and ends as a request the server may
+ have taken."""
+ now = [0.0]
+
+ def sleep(seconds):
+ api["slept"].append(seconds)
+ now[0] += seconds + 0.020
+
+ for module in (openai, retry, xai):
+ monkeypatch.setattr(module, "time", SimpleNamespace(monotonic=lambda: now[0]))
+ monkeypatch.setattr(retry, "_sleep", sleep)
+ monkeypatch.setattr(openai, "GEN_TIMEOUT_SECONDS", 8.010)
+ limited = (429, {"error": {"type": "rate_limit_error", "code": "rate_limit_exceeded"}}, {})
+ api["script"] = [limited, (200, api["body"], {})]
+ Image.new("RGB", (4, 4), (20, 90, 200)).save(tmp_path / "raw.png")
+ with pytest.raises(SystemExit) as refused:
+ if call == "image":
+ gen.generate_image("openai", "x", tmp_path / "out.png")
+ else:
+ openai.OpenAIProvider().inspect_facing(tmp_path / "raw.png", tmp_path)
+ assert len(api["calls"]) == 1 and api["slept"] == [8.0]
+ assert "HTTP 429" in str(refused.value) and "request failed or timed out" not in str(refused.value)
+ assert capsys.readouterr().err.splitlines()[-1] == (
+ "[gen] openai refused the request (429 rate_limit_exceeded); nothing was made — not asking again: "
+ "the time limit came during the 8s wait")
+ assert not (tmp_path / "out.png").exists()
+
+
@pytest.mark.parametrize("body", [{}, {"data": []}, {"data": [None]}, {"data": [{}, {}]},
{"data": [{"b64_json": "???"}]},
{"data": [{"b64_json": base64.b64encode(b"invalid image").decode()}]},
diff --git a/tests/gen/test_retry.py b/tests/gen/test_retry.py
index c706aa2..c3b4092 100644
--- a/tests/gen/test_retry.py
+++ b/tests/gen/test_retry.py
@@ -4,8 +4,9 @@ provider documents the answer as a request not taken (Anthropic 429 but its spen
OpenAI 429 but its billing codes, 503; xAI 429), a GET on any of 429/5xx it may be answered with;
the wait is the answer's retry-after when this run waits that long, else the doubling `backoff` the
video polls wait by; twice at most, and only while the wait ends before the time limit of the call the
-request belongs to. Every direct provider call asks it — the Anthropic transport, the OpenAI and Grok
-image calls, the facing check and the clip request — and hands it that time limit."""
+request belongs to — read by one judgment, `too_late`, before the wait and again right before the send,
+as a sleep may wake late. Every direct provider call asks it — the Anthropic transport, the OpenAI and
+Grok image calls, the facing check and the clip request — and hands it that time limit."""
from __future__ import annotations
import io
@@ -19,6 +20,9 @@ from http_answer import Answer
from sprite_gen.gen import anthropic_api, facing_vision, grok_provider, openai_provider, retry, video, xai
from sprite_gen.gen.base import GenRequest
+# What a sleep woke after the time it was asked for, in the tests of a wait that woke late.
+LATE = 0.020
+
def test_the_wait_doubles_to_a_ceiling():
assert [retry.backoff(n) for n in (1, 2, 3, 4, 5)] == [8.0, 16.0, 32.0, 60.0, 60.0]
@@ -100,6 +104,13 @@ class _Clock:
self.slept.append(seconds)
self.now += seconds
+ def waking_late(self, late: float = LATE):
+ """A sleep on this clock that wakes `late` seconds after the time it was asked for."""
+ def sleep(seconds: float) -> None:
+ self.sleep(seconds)
+ self.now += late
+ return sleep
+
@pytest.fixture
def clock(monkeypatch) -> _Clock:
@@ -110,6 +121,19 @@ def clock(monkeypatch) -> _Clock:
return clock
+def test_a_send_at_the_time_limit_or_past_it_is_too_late(clock):
+ """`too_late` is the one reading of a call's time limit before a send: a send `wait` seconds from now that
+ would go at the limit or past it is not made (it would have no time left to be answered in); without a
+ limit none is too late."""
+ clock.now = 2.0
+ assert not retry.too_late(None, 1e9)
+ assert not retry.too_late(10.0, 7.5)
+ assert retry.too_late(10.0, 8.0) and retry.too_late(10.0, 9.0)
+ assert not retry.too_late(10.0)
+ clock.now = 10.0
+ assert retry.too_late(10.0)
+
+
def test_a_request_is_sent_again_only_while_its_wait_ends_before_the_time_limit(clock, capsys):
"""The time limit of the call the request belongs to (`deadline`) holds its waits: the 8 s wait ends
before 20 s, the 16 s one would not, so the second refusal is the answer, said on stderr in one line."""
@@ -142,6 +166,28 @@ def test_a_wait_that_would_reach_the_time_limit_is_not_waited(clock, capsys, pro
assert capsys.readouterr().err == f"[gen] {provider} refused the request (429); nothing was made — not asking again: {why}\n"
[email protected]("late", [0.125, 0.25], ids=["at-the-limit", "past-it"])
+def test_a_wait_that_woke_at_the_time_limit_or_past_it_sends_nothing(clock, monkeypatch, capsys, late):
+ """A sleep may wake later than it was asked to: the 8 s wait, asked with 8.125 s left, ends before the limit
+ as asked but wakes at it or past it. The limit is read again right before the send, so nothing is sent: the
+ refusal is the answer, as it came, and a second line says why."""
+ monkeypatch.setattr(retry, "_sleep", clock.waking_late(late))
+ answers = [(429, {"code": "resource-exhausted"}, None), (200, {}, None)]
+ sent = []
+
+ def send():
+ sent.append(clock.now)
+ return answers.pop(0)
+
+ status, _body, _headers, asks = retry.ask(send, provider="xai", who="xai", label="video", deadline=8.125,
+ kind=lambda body: body.get("code"))
+ assert (status, asks, sent, clock.slept) == (429, 1, [0.0], [8.0])
+ assert capsys.readouterr().err.splitlines() == [
+ "[video] xai refused the request (429 resource-exhausted); nothing was made — asking again in 8s (1 of 2)",
+ "[video] xai refused the request (429 resource-exhausted); nothing was made — not asking again: "
+ "the time limit came during the 8s wait"]
+
+
# --- every direct provider call asks it -------------------------------------------------------------
@@ -213,6 +259,51 @@ def test_the_anthropic_transport_asks_it_for_its_posts_and_its_reads_within_the_
assert asked == [("anthropic", "POST", None), ("anthropic", "GET", 30)]
+def test_the_grok_facing_check_sends_nothing_after_a_wait_that_woke_past_its_time_limit(monkeypatch, tmp_path, clock,
+ capsys):
+ """The facing check's time limit (the transport's read timeout from its first send, 8.01 s here) holds its
+ waits as they are slept: the 8 s wait after a 429 woke 20 ms late, past it, so no second question goes and
+ the 429 is the answer."""
+ monkeypatch.setattr(facing_vision, "time", clock)
+ monkeypatch.setattr(xai, "HTTP_TIMEOUT_SECONDS", 8.010)
+ monkeypatch.setattr(retry, "_sleep", clock.waking_late())
+ sent = []
+
+ def call(method, url, token, body, *, deadline):
+ sent.append((method, clock.now))
+ return (429, {"code": "resource-exhausted"}) if len(sent) == 1 else (200, {"output": []})
+
+ Image.new("RGB", (4, 4), (20, 90, 200)).save(tmp_path / "raw.png")
+ with pytest.raises(SystemExit, match="HTTP 429"):
+ facing_vision.grok_inspect(tmp_path / "raw.png", credential=xai.Credential("t", xai.AUTH_SOURCE_GROK_LOGIN),
+ call=call)
+ assert sent == [("POST", 0.0)] and clock.slept == [8.0]
+ assert capsys.readouterr().err.splitlines()[-1] == (
+ "[facing-vision] grok refused the request (429 resource-exhausted); nothing was made — not asking again: "
+ "the time limit came during the 8s wait")
+
+
+def test_a_batch_read_whose_wait_woke_past_the_batch_wait_is_not_asked_again(monkeypatch, clock, capsys):
+ """A Message Batch's reads hold `--batch-wait`, handed as their time limit: the 8 s wait after an overloaded
+ status read woke 20 ms past it, so no read goes after it and the 529 is the answer."""
+ opened = []
+
+ def urlopen(request, timeout):
+ opened.append(clock.now)
+ if len(opened) == 1:
+ raise urllib.error.HTTPError(request.full_url, 529, "Overloaded", {}, Answer(
+ {"type": "error", "error": {"type": "overloaded_error"}}, status=529))
+ return Answer({"id": "msgbatch_1", "processing_status": "ended"})
+
+ monkeypatch.setattr(anthropic_api.urllib.request, "urlopen", urlopen)
+ with pytest.raises(anthropic_api.ApiError, match=r"\(HTTP 529\) code=overloaded_error"):
+ anthropic_api.get_batch("k", "msgbatch_1", sleep=clock.waking_late(), deadline=8.010)
+ assert opened == [0.0] and clock.slept == [8.0]
+ assert capsys.readouterr().err.splitlines()[-1] == (
+ "[pixel-draw] claude refused the request (529 overloaded_error); a read buys nothing — not asking again: "
+ "the time limit came during the 8s wait")
+
+
# --- a refusal's body is read by one function ---------------------------------------------------------
diff --git a/tests/gen/test_video.py b/tests/gen/test_video.py
index 7254ab4..bad8513 100644
--- a/tests/gen/test_video.py
+++ b/tests/gen/test_video.py
@@ -82,6 +82,13 @@ class _Clock:
self.slept.append(seconds)
self.now += seconds
+ def waking_late(self, late: float):
+ """A sleep on this clock that wakes `late` seconds after the time it was asked for, as a real one may."""
+ def sleep(seconds: float) -> None:
+ self.sleep(seconds)
+ self.now += late
+ return sleep
+
@pytest.fixture
def clock(monkeypatch) -> _Clock:
@@ -685,6 +692,34 @@ def test_a_rate_limited_submission_whose_next_wait_would_reach_the_poll_timeout_
assert not request.out.exists() and api.downloads == []
+def test_a_rate_limited_submission_whose_wait_woke_past_the_poll_timeout_is_not_sent_again(
+ tmp_path: Path, clock, capsys) -> None:
+ """The counterexample: with 8.01 s, the 8 s wait after a 429 ends before the poll timeout as asked, but the
+ sleep wakes 20 ms late, past it. The timeout is read again right before the send, so no second POST goes —
+ one sent then could be taken, and billed, after the limit the caller set — and the 429 ends the run with
+ its status, its stderr line saying why."""
+ answers = [(429, {"code": "resource-exhausted", "error": "slow down"}), (200, {"request_id": "req-late"})]
+ sent = []
+
+ def call(method, url, token, body, *, deadline=None):
+ sent.append((method, clock.now))
+ assert method == "POST", "no poll goes after the poll timeout"
+ return answers.pop(0)
+
+ request = _request(tmp_path)
+ with pytest.raises(SystemExit) as refused:
+ video.generate_video(request, credential=video.Credential("t", video.AUTH_SOURCE_GROK_LOGIN), call=call,
+ download=lambda *a, **k: pytest.fail("no download"), sleep=clock.waking_late(0.020),
+ poll_timeout=8.010)
+ assert sent == [("POST", 0.0)] and clock.slept == [8.0]
+ assert not isinstance(refused.value, video.GenerationUnfinished)
+ assert HTTP_STATUS.search(str(refused.value)).group(1) == "429"
+ assert capsys.readouterr().err.splitlines()[-1] == (
+ "[video] xai refused the request (429 resource-exhausted); nothing was made — not asking again: "
+ "the time limit came during the 8s wait")
+ assert not request.out.exists()
+
+
def test_the_cli_leaves_an_unfinished_report_naming_the_request(tmp_path: Path, monkeypatch, clock, capsys) -> None:
api = _FakeApi(polls=[(503, {"error": "unavailable"})])
monkeypatch.setattr(video, "resolve_credential", lambda **_: video.Credential("t", video.AUTH_SOURCE_GROK_LOGIN))
@@ -1446,6 +1481,26 @@ def test_a_download_whose_next_wait_would_reach_the_poll_timeout_is_not_tried_ag
assert not request.out.exists() and not list(tmp_path.glob("*.part"))
+def test_a_download_whose_wait_woke_past_the_poll_timeout_is_not_tried(tmp_path: Path, wire, clock) -> None:
+ """The 8 s wait after a refused download, asked with 8.01 s left, wakes 20 ms late, past the poll timeout:
+ no download is tried then, and the run ends on the refusal it had — not on a download the transport found
+ no time left to send, counted as one that got no answer."""
+ server = _clip_wire(wire, CLIP_REFUSED)
+ request = _request(tmp_path)
+ with pytest.raises(video.GenerationUnfinished) as unfinished:
+ video.generate_video(request, credential=video.Credential("t", video.AUTH_SOURCE_GROK_LOGIN),
+ call=_on_wire, sleep=clock.waking_late(0.020), poll_timeout=8.010)
+ assert server.seen == SUBMITTED + [("GET", "/clips/1.mp4")] and clock.slept == [8.0]
+ assert str(unfinished.value) == (
+ "video: generation req-1 is done, but its download was refused (last answer 503) until the poll timeout "
+ "(8.01s, 1 attempt); the clip is made and may be billed — ask for request_id=req-1 again with `sprite-gen "
+ f"video-resume --request-id req-1 --out {request.out.resolve()}` instead of submitting a new request; "
+ "nothing was written")
+ report = unfinished.value.to_dict()
+ assert (report["downloads"], report["last_download"]) == (1, {"http": 503})
+ assert not request.out.exists() and not list(tmp_path.glob("*.part"))
+
+
@pytest.mark.parametrize("slow", ["stalled", "trickled"])
def test_a_poll_slower_than_the_time_left_ends_unfinished_by_the_poll_timeout(tmp_path: Path, wire, clock, monkeypatch, slow) -> None:
"""A poll's own read is cut by the poll timeout too: one that stalls past it, or whose answer keeps coming
@@ -1470,6 +1525,25 @@ def test_a_poll_slower_than_the_time_left_ends_unfinished_by_the_poll_timeout(tm
assert not request.out.exists()
+def test_a_poll_whose_wait_woke_past_the_poll_timeout_is_not_sent(tmp_path: Path, wire, clock) -> None:
+ """Each wait here wakes 40 ms late: the second, asked at 4.04 s for 4 s with 4.01 s of the 8.05 s poll
+ timeout left, wakes at 8.08 s, past it. No poll goes then, and the run ends on the last one as it came —
+ still pending after 2 polls — not on a third the transport found no time left to send, counted as a poll
+ that got no answer."""
+ server = wire(ACCEPTED, PENDING_ON_WIRE)
+ request = _request(tmp_path)
+ with pytest.raises(video.GenerationUnfinished) as unfinished:
+ video.generate_video(request, credential=video.Credential("t", video.AUTH_SOURCE_GROK_LOGIN), call=_on_wire,
+ download=lambda url, token, **_: MP4, sleep=clock.waking_late(0.040), poll_timeout=8.05)
+ assert server.seen == [("POST", "/v1/videos/generations")] + [("GET", "/v1/videos/req-1")] * 2
+ assert clock.slept == [4.0, 4.0]
+ assert str(unfinished.value).startswith("video: generation req-1 still 'pending' after 2 polls — poll timeout; ")
+ report = unfinished.value.to_dict()
+ assert (report["polls"], report["polls_unanswered"], report["last_poll"]) == (
+ 2, 0, {"http": 200, "status": "pending"})
+ assert not request.out.exists()
+
+
def test_the_poll_timeout_counts_from_the_requests_send(tmp_path: Path, clock) -> None:
"""A caller's limit covers the POST: the polls get what the POST's answer left of the timeout."""
api = _FakeApi(polls=[(200, {"status": "pending"})])