Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
fix(gen): a refused request is sent again only within the time limit of its call
Soohong Kim472655edff81
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 061d372..e9ea865 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -4,6 +4,8 @@ All notable public changes to `sprite-gen` are recorded here. Versions track the## Unreleased+- **Behavior change: a refused provider request is sent again only within the time limit of its call, and the waits count in that limit.** Previously a request its provider documents as not taken (a 429, an overload) was sent again after 8 s and 16 s, or after the answer's `retry-after` (at most 120 s), twice, whatever time the call had left, and the time limits did not count those waits: `SPRITE_GEN_GEN_TIMEOUT_SECONDS` bounded each send of an OpenAI or Grok image and of the OpenAI facing check on its own, the Grok facing check's 120 s each read, the clip's poll timeout counted from the send xAI accepted, and a Message Batch's status and results reads waited outside `--batch-wait`. A caller that stops the run at its own ceiling, set above those limits, could kill a request taken after two refusals and hear nothing from the engine. Now each limit counts from the first send and holds the waits: the image calls and the OpenAI facing check count `SPRITE_GEN_GEN_TIMEOUT_SECONDS`, the Grok facing check its 120 s, each of their sends reading only what is left of it; the clip its poll timeout (`SPRITE_GEN_VIDEO_TIMEOUT_SECONDS`; the POST itself is still not cut, its answer being the one way to the request id); a batch's reads `--batch-wait`. A wait that would reach the limit is not waited: the refusal ends the call as any refusal does, after one stderr line, `[gen] openai refused the request (429 rate_limit_exceeded); nothing was made — not asking again: the 16s wait would reach the time limit (12s left)`. A realtime `pixel-draw` call has no time limit of its own and is unchanged.+- **Tests** — `tests/gen/test_retry.py` (a request sent again only while its wait ends before the time limit; the answer's `retry-after` counted the same; a wait that ends at the limit not waited; every direct provider call hands `retry` its limit), `tests/gen/test_openai_provider.py` and `tests/gen/test_grok_provider.py` (with 20 s, the second send waits only the 12 s left and the second 429 ends the call `sent 2 times`), `tests/gen/test_video.py` (the poll timeout counts from the first send; a 429 whose next wait would reach it ends the run with its status), `tests/gen/test_pixel_draw.py` (each batch read is handed the end of `--batch-wait`). `tests/http_answer.py` is one offline HTTP answer the provider tests share, read whole or a chunk at a time under a deadline.- **New verb: `video-stop` cuts a walk or run that stops from the clip's own start played backwards.** Given the clip's keyed frames and a walk or run loop (`--frames-dir`, `--loop`), it writes a one-shot strip (`<name>.strip.png`), sidecar (`loop: false`, `hold_last: true`), GIF, WebP, and report (`<name>.stop.report.json`). The stop leaves the loop from the cell nearest the entry (`from_cell`), the first frame after the opening rest within `SAME_POSE` (0.35 steps) of a loop cell, and ends on the clip's first frame, the still as filmed. A seam past the jump line (1.4 steps, `--seam-max`) is refused by `video-stop` with exit code 1 and `status: "failed"` in the report, and so is another motion in the run-up between the rest and the entry, however short: a run-up frame back within `SAME_POSE` (0.35 steps) of a pose the clip had left by `TURN_BACK_AWAY` (one step) or more (the still, or the run-up's own), refused with "the run-up turns back — another motion, that the stop would play backwards", naming the three frames (`turn_back`, the nearest such comeback, is reported either way). A run-up longer than `RUN_UP_MAX_CYCLES` (1.5 cycles) — `source_cut.length` frames, overridden by `--run-up-max` — which plays strides unlike the loop's, is refused with "strides unlike the loop's, that the stop would play backwards"; `--run-up-max` bounds only that length and never lets another motion through. The seam refusal, the another-motion refusal and the run-up refusal can all be named in one message. Drift is taken out by laying the entry on its cell and the first frame on the loop's ground and mean coverage, moving frames between by a straight share of the difference per step; a run leaning away moves the still a few pixels, and drift not straight leaves a little of itself. Strips wider than 32,000 px are refused, `--speed 2` plays every other frame (still kept last), `--max-frames N` spaces frames evenly, and frames shown pass the ghost screen (`interpolation_quality`) as filmed. The pose thumbnails (`gait_fallback.poses`, 48 px cut about the feet) are now public, as the verb reads poses with them ([video-pipeline](docs/video-pipeline.md#8-a-walk-or-run-that-stops--video-stop)).- **New: a motion plays as its request says — `--play loop|once-return|once-hold|lifetime` — and `video-set --motion` gives each state or item its own motion text.** A state's name used to fix its sentence, its end-frame pin, its cut and whether its strip loops, so every new motion (a punch, typing at a desk, a block, a tool swing) was a change to the engine. `video-set --play STATE=MODE` or `ITEM=MODE` and `video-prompt --play MODE` take the mode as a value of the request, and `video-set --motion STATE=TEXT` or `ITEM=TEXT` the caller's words in place of the state's sentence (an item's own value wins over its state's; a key that names neither, a key given twice or a mode the state cannot play is refused before any clip). A word the engine does not name is filmed under that name in that mode: a once-return is pinned to end on its first frame, asked for as one action that returns to the first pose, and cut whole by the new `video-loop --cycle once-return` (every frame, the last too, gated by the pin), its `strip.json` saying `loop: false`, `hold_last: false`, `returns: true` with the first pose in its first and last cells; a once-hold is cut at its held pose (`once`), the first settled pose whose drawing is not its first — a shield raised inside a body outline that keeps its size and floor is one, the first drawing come back, in place or slid sideways, is not — while the six terminal states keep their body-geometry screen (any other state cut `--cycle once` is screened by its drawing too); a lifetime is framed and filmed as an effect whatever its name; a loop asked for is searched as a loop (`periodic`) and never failed over to a single play. Named states are presets: without `--play` every state is filmed, prompted and cut as before, and an attack played once-return is the same prompt and the same pinned clip, cut whole and played once. `effect`, the six terminal states and the cyclic states refuse any other mode. `set.report.json` records each item's `play` (and `motion`); `video-prompt --json` records `play`. Not measured on a clip model: the sentences for a motion of your own ([video-pipeline](docs/video-pipeline.md#play-modes)).- **Tests** — `tests/video/test_play.py` (every named state given no mode is pinned, prompted and cut as before, and the same when its preset is named, save that a loop asked for is never failed over; a mode a state cannot play is refused; a once-return asked for pinned with its return sentence and cut whole, or searched where the source cannot pin; the attack played once-return is the attack's prompt; a named state played another way takes the mode's sentence; a once-hold with its end frame; a once-hold of your own ending on a new drawing inside the same body box cut at its held pose, the same clip as `cast` still refused by its body geometry, and one that comes back to its first drawing, in place or slid sideways, refused; a lifetime filmed as an effect whatever its name; a loop of your own; `video-set` modes and texts per state and per item, refused before any clip when a key or mode is wrong; a once-return that does not come back refused; a once-return cut subsampled with both ends kept and played once; the CLIs) and the once-return item filmed end to end in `tests/video/test_clip_prompt.py`, its strip's first and last cells the first pose. Synthetic fixtures only.diff --git a/docs/gen.md b/docs/gen.mdindex 7f85c45..7333b3a 100644--- a/docs/gen.md+++ b/docs/gen.md@@ -211,6 +211,14 @@ Anthropic transport of `pixel-draw` (docs/pixel-draw.md) — in `sprite_gen/gen/`[gen] openai refused the request (429 rate_limit_exceeded); nothing was made — asking again in 8s (1 of 2)`.A refusal still there after that ends the call as any refusal does, its line saying`sent 3 times, no provider fallback` where it says `no retry or provider fallback`.+- **The waits are inside the call's time limit**, counted from its first send:+ `SPRITE_GEN_GEN_TIMEOUT_SECONDS` (180) for an OpenAI or Grok image and the OpenAI+ facing check, 120 s for the Grok facing check — each send reading only what is left+ of it — the poll timeout for a clip (docs/video.md), `--batch-wait` for the reads of+ a Message Batch (docs/pixel-draw.md). A wait that would reach the limit is not+ waited: the refusal ends the call after one line,+ `[gen] openai refused the request (429 rate_limit_exceeded); nothing was made — not asking again: the 16s wait would reach the time limit (12s left)`.+ A realtime `pixel-draw` call has no time limit of its own.- **A request that got no answer it could read is never sent again** — the connectionbroke, it timed out, or the answer broke on the way, a refusal whose body brokeincluded (`xai.error_body`: a refusal's status alone is not an answer read whole).diff --git a/docs/pixel-draw.md b/docs/pixel-draw.mdindex 62e4b7f..677e5a9 100644--- a/docs/pixel-draw.md+++ b/docs/pixel-draw.md@@ -107,7 +107,7 @@ candidate's files are at the top of the run folder.| `--effort` | `medium`, `high`, `xhigh` (`high`) | Thinking effort of each drawing. || `--format` | `chargrid`, `json` (`chargrid`) | How the model writes the grid. `grid.txt` is a character grid either way. || `--batch` | flag | Draw through Message Batches at half price and wait for the batch (below). Batches exist only on the API, so the whole run uses `ANTHROPIC_API_KEY` even when a subscription is signed in. |-| `--batch-wait S` | seconds (3600) | How long to wait for one batch before the run ends unfinished; `--resume` collects it later. `0` submits and returns. |+| `--batch-wait S` | seconds (3600) | How long to wait for one batch before the run ends unfinished; `--resume` collects it later. `0` submits and returns. A refused status or results read is asked again only while its wait ends within it. || `--max-tokens N` | 1024–128000 (32000 for one frame, 64000 for more) | Output cap of each drawing, thinking included. || `--pick` | `vision`, `human` (`vision`) | Who picks among candidates that passed. || `--out-dir DIR` | a new or empty folder | The run folder. |diff --git a/docs/video.md b/docs/video.mdindex e2dd04c..1961636 100644--- a/docs/video.md+++ b/docs/video.md@@ -144,7 +144,9 @@ What happens, in order:2. `POST /v1/videos/generations` with `model`, `prompt`, `duration`, `resolution`,then `image.url` / `last_frame.url` / `reference_images[].url` as base64 dataURLs, optional `aspect_ratio` / `generate_audio`. A `429` — xAI's rate limit, a- request it did not take — is sent again, twice at most, after 8 s and 16 s+ request it did not take — is sent again, twice at most, after 8 s and 16 s, while+ the wait leaves some of the poll timeout (step 3); a wait that would reach it is not+ waited and the run ends on that `429`([a refused request sent again](gen.md#a-refused-request-sent-again)). A `401/403`names the credential source and its fix; any other reply without a `request_id`fails with the API's own error text.@@ -163,8 +165,8 @@ What happens, in order:valid JSON` for an answer it could not read — goes to stderr each time.The poll timeout (`SPRITE_GEN_VIDEO_TIMEOUT_SECONDS`, default 600) is the one time- limit of the accepted request. It counts from the send of the POST xAI accepted and bounds the polls,- their waits and the clip's downloads (step 4) alike:+ limit of the request. It counts from the POST's first send and bounds the waits before a refused+ POST goes again (step 2), the polls, their waits and the clip's downloads (step 4) alike:- Each poll reads only what is left of it. One whose answer stalls, or comes tooslowly for its 120 s read timeout ever to fire, is cut when the timeout comes anddiff --git a/sprite_gen/gen/anthropic_api.py b/sprite_gen/gen/anthropic_api.pyindex b7b482f..30bcbc2 100644--- a/sprite_gen/gen/anthropic_api.py+++ b/sprite_gen/gen/anthropic_api.py@@ -174,8 +174,9 @@ def _no_answer(exc: BaseException) -> ApiError:def _open(request_for: Callable[[], urllib.request.Request], token: str, *, timeout: float, sleep: Sleep,- label: str, method: str = "POST") -> http.client.HTTPResponse:- """Send a request, and again after a refusal `retry` sends again; any other refusal raises ApiError."""+ label: str, method: str = "POST", deadline: float | None = None) -> http.client.HTTPResponse:+ """Send a request, and again after a refusal `retry` sends again within `deadline`, the time limit of the+ call (None: none); any other refusal raises ApiError."""def send() -> tuple[int, Any, Any]:try:response = urllib.request.urlopen(request_for(), timeout=timeout)@@ -189,7 +190,7 @@ def _open(request_for: Callable[[], urllib.request.Request], token: str, *, timereturn response.status, response, response.headersstatus, answer, headers, _asks = retry.ask(send, provider="anthropic", who="claude", label=label, method=method,- sleep=sleep, kind=_error_type)+ sleep=sleep, kind=_error_type, deadline=deadline)if 200 <= status < 300:return answerraise ApiError(refused_line(status, answer, token), status=status,@@ -301,10 +302,10 @@ def stream_message(token: str, body: dict[str, Any], *, sleep: Sleep = time.sleedef _json_call(method: str, url: str, token: str, body: dict[str, Any] | None, *, sleep: Sleep,- label: str, timeout: float = 120.0) -> Any:+ label: str, timeout: float = 120.0, deadline: float | None = None) -> Any:data = None if body is None else json.dumps(body).encode("utf-8")response = _open(lambda: _request(method, url, token, data, accept="application/json"),- token, timeout=timeout, sleep=sleep, label=label, method=method)+ token, timeout=timeout, sleep=sleep, label=label, method=method, deadline=deadline)try:with response:raw = response.read().decode("utf-8")@@ -326,8 +327,11 @@ def submit_batch(token: str, requests: list[tuple[str, dict[str, Any]]], *, sleereturn batch-def get_batch(token: str, batch_id: str, *, sleep: Sleep = time.sleep, label: str = "pixel-draw") -> dict[str, Any]:- batch = _json_call("GET", f"{API_BASE}/messages/batches/{batch_id}", token, None, sleep=sleep, label=label)+def get_batch(token: str, batch_id: str, *, sleep: Sleep = time.sleep, label: str = "pixel-draw",+ deadline: float | None = None) -> dict[str, Any]:+ """The batch object; a refused read is asked again within `deadline` (the wait for the batch)."""+ batch = _json_call("GET", f"{API_BASE}/messages/batches/{batch_id}", token, None, sleep=sleep, label=label,+ deadline=deadline)if not isinstance(batch, dict):raise ApiError("pixel-draw: the batch status answer is not an object")return batch@@ -349,14 +353,15 @@ def _message_reply(message: dict[str, Any], batch_id: str) -> Reply:)-def batch_results(token: str, batch: dict[str, Any], *, sleep: Sleep = time.sleep,- label: str = "pixel-draw") -> dict[str, Reply | ApiError]:- """Every result of an ended batch by custom_id; an item that was not answered is an ApiError."""+def batch_results(token: str, batch: dict[str, Any], *, sleep: Sleep = time.sleep, label: str = "pixel-draw",+ deadline: float | None = None) -> dict[str, Reply | ApiError]:+ """Every result of an ended batch by custom_id; an item that was not answered is an ApiError. A refused+ read is asked again within `deadline` (the wait for the batch)."""url = batch.get("results_url")if not isinstance(url, str) or not url:raise ApiError(f"pixel-draw: batch {batch.get('id')} has ended but names no results_url")response = _open(lambda: _request("GET", url, token, None, accept="application/binary"),- token, timeout=120.0, sleep=sleep, label=label, method="GET")+ token, timeout=120.0, sleep=sleep, label=label, method="GET", deadline=deadline)try:with response:lines = response.read().decode("utf-8").splitlines()diff --git a/sprite_gen/gen/base.py b/sprite_gen/gen/base.pyindex 361b724..180ab14 100644--- a/sprite_gen/gen/base.py+++ b/sprite_gen/gen/base.py@@ -71,6 +71,7 @@ _ORCHESTRATOR_SESSION_ENV_SUFFIXES = (# (회귀 2026-07-19: 15행 배치 중 1행의 codex exec 이 1시간 38분 무출력 — 같은 env# 로 14행이 성공했으니 세션/훅 충돌이 아니라 provider 측 산발 스톨. 킬 외엔 답이# 없으므로 바운드가 유일한 방어). 기본 180초 (maintainer 확정 2026-07-19; 실측 정상 최장 129초).+# OpenAI·Grok 직접 호출(이미지, OpenAI 방향 검사)에는 첫 전송부터 센 호출 기한 — 다시 보내기 전 대기 포함 (gen.retry).GEN_TIMEOUT_SECONDS = int(os.environ.get("SPRITE_GEN_GEN_TIMEOUT_SECONDS", "180"))diff --git a/sprite_gen/gen/facing_vision.py b/sprite_gen/gen/facing_vision.pyindex f440b56..1340434 100644--- a/sprite_gen/gen/facing_vision.py+++ b/sprite_gen/gen/facing_vision.py@@ -12,6 +12,7 @@ import base64import jsonimport subprocessimport tempfile+import timefrom pathlib import Pathfrom .base import GEN_TIMEOUT_SECONDS, provider_binary, provider_subprocess_env@@ -77,8 +78,11 @@ def grok_inspect(path: Path, *, credential=None, call=None, prompt: str = INSPECcredential = credential or xai.resolve_credential()if credential.source == xai.AUTH_SOURCE_API_KEY:announce_api_billing("grok", xai.AUTH_ENV, f" ({label}).")- status, reply, _sends = xai.post(call or xai.http_json, xai.API_BASE + "/responses", credential.token,- request_body(path, GROK_MODEL, prompt=prompt, detail=detail),- who="grok", label=_tag(label))+ # The question's time limit is the transport's read timeout, from its first send: the waits before a+ # refused one goes again count in it (`retry`), and each send reads only what is left of it.+ send, deadline = call or xai.http_json, time.monotonic() + xai.HTTP_TIMEOUT_SECONDS+ status, reply, _sends = xai.post(lambda *a: send(*a, deadline=deadline), xai.API_BASE + "/responses",+ credential.token, request_body(path, GROK_MODEL, prompt=prompt, detail=detail),+ who="grok", label=_tag(label), deadline=deadline)text, metadata = response_text(status, reply, label=label)return text, {**metadata, "auth_source": credential.source, "transport": "xai-api"}diff --git a/sprite_gen/gen/grok_provider.py b/sprite_gen/gen/grok_provider.pyindex 67a462f..cbfb99e 100644--- a/sprite_gen/gen/grok_provider.py+++ b/sprite_gen/gen/grok_provider.py@@ -145,8 +145,12 @@ class GrokProvider:f"{f' ({priced})' if priced else ''} — "f"`{xai.GROK_LOGIN_COMMAND}` signs your Grok subscription in.")started = time.monotonic()- status, reply, sends = xai.post(lambda *a: xai.http_json(*a, timeout=GEN_TIMEOUT_SECONDS),- xai.API_BASE + endpoint, credential.token, body, who="grok", label="gen")+ # SPRITE_GEN_GEN_TIMEOUT_SECONDS is the image's time limit from its first send: the waits before a+ # refused request goes again count in it (`retry`), and each send reads only what is left of it.+ deadline = started + GEN_TIMEOUT_SECONDS+ status, reply, sends = xai.post(lambda *a: xai.http_json(*a, timeout=GEN_TIMEOUT_SECONDS, deadline=deadline),+ xai.API_BASE + endpoint, credential.token, body, who="grok", label="gen",+ deadline=deadline)# The provider's code goes in front, format-checked; the body it sent goes# last, as received, with keys and URL query strings masked (refusal.said).# The REST image answer has no moderation field (docs.x.ai reference; adiff --git a/sprite_gen/gen/openai_provider.py b/sprite_gen/gen/openai_provider.pyindex ff36b18..02c3f12 100644--- a/sprite_gen/gen/openai_provider.py+++ b/sprite_gen/gen/openai_provider.py@@ -41,7 +41,7 @@ from typing import Anyfrom PIL import Image, UnidentifiedImageErrorfrom . import refusal, retry-from .xai import NO_ANSWER, error_body+from .xai import NO_ANSWER, error_body, read_whole, time_leftfrom .base import (GEN_TIMEOUT_SECONDS,QUALITIES,@@ -216,34 +216,41 @@ def _multipart_request(fields: dict[str, str], refs: list[Path]) -> tuple[bytes,TRANSPORT_FAILED = "openai-gen: request failed or timed out; no automatic retry (the server may have accepted it)"-def http_image(url: str, token: str, data: bytes, content_type: str, *, timeout: float) -> tuple[int, Any, Any]:+def http_image(url: str, token: str, data: bytes, content_type: str, *, timeout: float,+ deadline: float | None = None) -> tuple[int, Any, Any]:"""POST an images request and return (status, parsed JSON, response headers).A transport error is terminal: the server may already have accepted and billedthe request, so it is never retried here. A refusal whose body broke on the way- is one too (`xai.error_body`).+ is one too (`xai.error_body`). A `deadline` bounds the waits as it does in+ `xai.http_json`: an answer that has not come, or not whole, by then is none."""request = urllib.request.Request(url, data=data, method="POST")request.add_header("Authorization", f"Bearer {token}")request.add_header("Accept", "application/json")request.add_header("Content-Type", content_type)try:- with urllib.request.urlopen(request, timeout=timeout) as response:- raw = response.read().decode("utf-8")+ with urllib.request.urlopen(request, timeout=time_left(timeout, deadline)) as response:+ raw = read_whole(response, timeout, deadline).decode("utf-8")return response.status, (json.loads(raw) if raw else {}), response.headersexcept (json.JSONDecodeError, UnicodeDecodeError) as exc:raise SystemExit("openai-gen: response was not valid JSON") from excexcept urllib.error.HTTPError as exc:try:- return exc.code, error_body(exc, timeout), exc.headers+ return exc.code, error_body(exc, timeout, deadline), exc.headersexcept NO_ANSWER as cut:raise SystemExit(TRANSPORT_FAILED) from cutexcept NO_ANSWER as exc:raise SystemExit(TRANSPORT_FAILED) from exc-def _ask(send: retry.Send) -> tuple[int, Any, Any, int]:- return retry.ask(send, provider="openai", who="openai", label="gen", kind=refusal.provider_code)+def _ask(url: str, token: str, data: bytes, content_type: str) -> tuple[int, Any, Any, int]:+ """POST one request, sent again as `retry` says. SPRITE_GEN_GEN_TIMEOUT_SECONDS is its time limit from+ the first send: the waits before a refused one goes again count in it, and each send reads only what is+ left of it."""+ deadline = time.monotonic() + GEN_TIMEOUT_SECONDS+ return retry.ask(lambda: http_image(url, token, data, content_type, timeout=GEN_TIMEOUT_SECONDS, deadline=deadline),+ provider="openai", who="openai", label="gen", kind=refusal.provider_code, deadline=deadline)def rate_limit_headers(headers: Any) -> dict[str, str]:@@ -291,8 +298,7 @@ class OpenAIProvider:token = resolve_credential()announce_api_billing(self.name, AUTH_ENV, f" ({label}).")data = json.dumps(body).encode("utf-8")- status, reply, _headers, _asks = _ask(lambda: http_image(API_BASE + "/responses", token, data, "application/json",- timeout=GEN_TIMEOUT_SECONDS))+ status, reply, _headers, _asks = _ask(API_BASE + "/responses", token, data, "application/json")text, metadata = vision.response_text(status, reply, label=label)return text, {**metadata, "auth_source": AUTH_SOURCE_API_KEY, "transport": "openai-api"}@@ -312,8 +318,7 @@ class OpenAIProvider:f" (model={fields['model']}, quality={fields['quality']}, size={fields['size']}).",)started = time.monotonic()- status, reply, headers, asks = _ask(lambda: http_image(API_BASE + endpoint, token, data, content_type,- timeout=GEN_TIMEOUT_SECONDS))+ status, reply, headers, asks = _ask(API_BASE + endpoint, token, data, content_type)# The provider's code (and the moderation stage) go in front, format-checked;# the body the provider sent goes last, as received, with keys and URL# query strings masked (refusal.said).diff --git a/sprite_gen/gen/pixel_draw.py b/sprite_gen/gen/pixel_draw.pyindex 5e57b5e..2f53cba 100644--- a/sprite_gen/gen/pixel_draw.py+++ b/sprite_gen/gen/pixel_draw.py@@ -725,9 +725,11 @@ class Calls:def _collect(self, record: dict[str, Any], asks: dict[str, Ask], wait: float) -> dict[str, Reply | ApiError]:started = time.monotonic()+ # --batch-wait holds the reads too: a refused one is asked again only while its wait ends before it.+ deadline = started + waitwhile True:try:- batch = api.get_batch(self.token, record["id"], sleep=self.sleep, label=VERB)+ batch = api.get_batch(self.token, record["id"], sleep=self.sleep, label=VERB, deadline=deadline)except ApiError as exc:self.note(f"batch {record['id']}: status not read ({exc.line}); asking again")batch = {}@@ -737,7 +739,7 @@ class Calls:raise Unfinished(record["id"], time.monotonic() - started, "has not ended")self.sleep(BATCH_POLL_SECONDS)try:- results = api.batch_results(self.token, batch, sleep=self.sleep, label=VERB)+ results = api.batch_results(self.token, batch, sleep=self.sleep, label=VERB, deadline=deadline)except ApiError as exc:raise Unfinished(record["id"], time.monotonic() - started, f"has ended but its results were not read ({exc.line})") from excseconds = round(time.monotonic() - started, 3)diff --git a/sprite_gen/gen/retry.py b/sprite_gen/gen/retry.pyindex 911d871..be2bc17 100644--- a/sprite_gen/gen/retry.py+++ b/sprite_gen/gen/retry.py@@ -38,6 +38,12 @@ again, as OpenAI's rate-limit guide asks), else `backoff` — the doubling the `downloads wait by, from START_SECONDS up to MAX_SECONDS. A POST is sent again ASKS_AGAIN times atmost, each said on stderr in one line.+The waits are inside the time limit of the call the request belongs to (`ask(deadline=)`): the gen+image calls and the facing check count theirs from the first send, the clip request its poll+timeout, a Message Batch's reads `--batch-wait`. A request is sent again only when its wait ends+before that limit; one whose wait would reach it is returned as it came, and its stderr line says+why — so a caller whose own ceiling sits above the limit hears the refusal, not a kill.+A GET buys nothing, so one answered with any of GET_AGAIN is asked again; the `video` poll keepsasking for as long as its poll timeout lets it, the Anthropic batch reads ASKS_AGAIN times."""@@ -117,13 +123,23 @@ def again_after(provider: str, method: str, status: int, body: Any, headers: Anyreturn said if said <= RETRY_AFTER_MAX_SECONDS else None-def asking_again(label: str, who: str, method: str, status: int, kind: str | None, wait: float, failures: int) -> str:- """The stderr line of one ask again. The status goes in plain digits: `(HTTP 429)` on this stream- reads as a refusal line, which a caller may answer by sending a second request."""+def _refused(label: str, who: str, method: str, status: int, kind: str | None) -> str:+ # The status goes in plain digits: `(HTTP 429)` on this stream reads as a refusal line, which a caller+ # may answer by sending a second request.why = ("a read buys nothing" if method == "GET"else "its docs say to ask again" if status == 500 else "nothing was made")- return (f"[{label}] {who} refused the request ({status}{' ' + kind if kind else ''}); {why} — "- f"asking again in {wait:g}s ({failures} of {ASKS_AGAIN})")+ return f"[{label}] {who} refused the request ({status}{' ' + kind if kind else ''}); {why}"+++def asking_again(label: str, who: str, method: str, status: int, kind: str | None, wait: float, failures: int) -> str:+ """The stderr line of one ask again."""+ return f"{_refused(label, who, method, status, kind)} — asking again in {wait:g}s ({failures} of {ASKS_AGAIN})"+++def out_of_time(label: str, who: str, method: str, status: int, kind: str | None, wait: float, left: float) -> str:+ """The stderr line of a request not sent again because its wait would reach the time limit of its call."""+ return (f"{_refused(label, who, method, status, kind)} — not asking again: "+ f"the {wait:g}s wait would reach the time limit ({max(left, 0.0):.0f}s left)")def tail(asks: int) -> str:@@ -136,12 +152,14 @@ def _sleep(seconds: float) -> None:def ask(send: Send, *, provider: str, who: str, label: str, method: str = "POST", sleep: Sleep | None = None,- kind: Callable[[Any], str | None] | None = None) -> tuple[int, Any, Any, int]:+ kind: Callable[[Any], str | None] | None = None, deadline: float | None = None) -> tuple[int, Any, Any, int]:"""`send()` until an answer is not one to send again: `(status, body, headers, asks)`.`send` returns `(status, body, headers)` — `headers` None when the transport keeps none — andraises when no answer came; that is never asked again here. `kind` reads the provider's word for- the refusal off the body for the stderr line."""+ the refusal off the body for the stderr line. `deadline`, a `time.monotonic()` instant (None:+ none), is the time limit of the call: a wait that would reach it is not waited, and the refusal+ is returned as it came."""asks = 0while True:status, body, headers = send()@@ -149,5 +167,10 @@ def ask(send: Send, *, provider: str, who: str, label: str, method: str = "POST"wait = None if 200 <= status < 300 else again_after(provider, method, status, body, headers, asks)if wait is None:return status, body, headers, asks- print(asking_again(label, who, method, status, kind(body) if kind else None, wait, asks), file=sys.stderr)+ said = kind(body) if kind else None+ left = None if deadline is None else deadline - time.monotonic()+ if left is not None and wait >= left:+ print(out_of_time(label, who, method, status, said, wait, left), file=sys.stderr)+ return status, body, headers, asks+ print(asking_again(label, who, method, status, said, wait, asks), file=sys.stderr)(sleep or _sleep)(wait)diff --git a/sprite_gen/gen/video.py b/sprite_gen/gen/video.pyindex 2afb4e6..45dd27f 100644--- a/sprite_gen/gen/video.py+++ b/sprite_gen/gen/video.py@@ -102,12 +102,13 @@ MODE_RESUME = "resume"# What `video-resume` takes for an id: it goes into the `/videos/{id}` path, so no `/`, `?`, `#` or `%`.REQUEST_ID = re.compile(r"[A-Za-z0-9][A-Za-z0-9._-]*")POLL_INTERVAL_SECONDS = 4.0-# The poll timeout is the one time limit of an accepted request: it counts from the moment the request is-# sent (a resume: its first poll) and bounds its polls, their waits and the clip's downloads alike. Each-# poll and download reads only what is left of it (xai.read_whole), and one whose wait would reach it is-# not asked: the run ends unfinished on the request_id instead. So a caller that stops the run at a hard-# limit gets the id back when it sets the timeout under that limit. The POST is not cut by it — its-# answer is the one way to the id; an answer that comes after the whole timeout ends the run at once.+# The poll timeout is the one time limit of a request: it counts from the POST's first send (a resume: its+# first poll) and bounds the waits before a refused POST goes again (`retry`), the polls, their waits and+# the clip's downloads alike. Each poll and download reads only what is left of it (xai.read_whole), and a+# send, poll or download whose wait would reach it is not asked: the run ends on the refusal, or unfinished+# on the request_id. So a caller that stops the run at a hard limit gets its line back when it sets the+# timeout under that limit. The POST is not cut by it — its answer is the one way to the id; an answer+# that comes after the whole timeout ends the run at once.POLL_TIMEOUT_SECONDS = int(os.environ.get("SPRITE_GEN_VIDEO_TIMEOUT_SECONDS", "600"))# A poll xAI throttles (429) or fails on its own side (5xx, `retry.GET_AGAIN`), or one that gets no answer# it can read (the connection failed or timed out, the answer broke on the way or was not JSON —@@ -518,17 +519,12 @@ def _submit_poll_publish(announce_api_billing(verb, AUTH_ENV,f"{f' ({priced})' if priced else ''} — "f"`{GROK_LOGIN_COMMAND}` signs your Grok subscription in.")- sent: list[float] = []-- def send(*args: Any) -> tuple[int, Any]:- sent.append(time.monotonic())- return call(*args)-- # A refusal xAI documents as a request not taken (429) is sent again (`retry`); the poll timeout counts- # from the send it answered.- status, reply, _sends = post(send, f"{API_BASE}/videos/{endpoint}", credential.token, body, who="xai", label=verb,- sleep=sleep)- started = sent[-1]+ # A refusal xAI documents as a request not taken (429) is sent again (`retry`) while its wait leaves+ # some of the poll timeout, which counts from the first send.+ started = time.monotonic()+ timeout = POLL_TIMEOUT_SECONDS if poll_timeout is None else poll_timeout+ status, reply, _sends = post(call, f"{API_BASE}/videos/{endpoint}", credential.token, body, who="xai", label=verb,+ sleep=sleep, deadline=started + timeout)if status in (401, 403):raise SystemExit(f"{verb}: xAI rejected the credential ({credential.source}) with HTTP {status}: {_error_detail(reply, credential.token)}\n"@@ -542,7 +538,7 @@ def _submit_poll_publish()return _poll_publish(verb=verb, request_id=str(request_id), model=body.get("model"), out=out, credential=credential, call=call,- download=download, sleep=sleep, poll_timeout=poll_timeout, started=started, accept=accept,+ download=download, sleep=sleep, poll_timeout=timeout, started=started, accept=accept,)@@ -566,7 +562,7 @@ def _poll_publish(still the `.part` file — `video-extend` measures the staged bytes with ffprobe; thestaged file is removed and `out` is never written.- The poll timeout counts from `started`, when the request was sent (a resume: its first poll), and+ The poll timeout counts from `started`, the POST's first send (a resume: its first poll), andis one deadline for the polls and the downloads: `call` (as http_json) and `download` take it as`deadline=` and read only what is left of it, and a poll or download whose wait would reach it isnot asked. A run that ends before the request does — the deadline came, xAI rejected the credential,diff --git a/sprite_gen/gen/xai.py b/sprite_gen/gen/xai.pyindex 83071e6..48dc39d 100644--- a/sprite_gen/gen/xai.py+++ b/sprite_gen/gen/xai.py@@ -141,11 +141,13 @@ HttpCall = Callable[[str, str, str, dict | None], tuple[int, Any]]def post(call: HttpCall, url: str, token: str, body: dict, *, who: str, label: str,- sleep: Callable[[float], None] | None = None) -> tuple[int, Any, int]:- """POST `body` with `call` (as http_json), and again after a refusal `retry` sends again: (status, answer,- sends). xAI documents 429 alone as a request not taken, and no retry-after, so http_json keeps no headers."""+ sleep: Callable[[float], None] | None = None, deadline: float | None = None) -> tuple[int, Any, int]:+ """POST `body` with `call` (as http_json), and again after a refusal `retry` sends again within `deadline`,+ the call's time limit (which `call` reads by only when the caller hands it too): (status, answer, sends).+ xAI documents 429 alone as a request not taken, and no retry-after, so http_json keeps no headers."""status, reply, _headers, sends = retry.ask(lambda: (*call("POST", url, token, body), None), provider="xai",- who=who, label=label, sleep=sleep, kind=refusal.provider_code)+ who=who, label=label, sleep=sleep, kind=refusal.provider_code,+ deadline=deadline)return status, reply, sends