Skip to content

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

fix(gen): a video error is masked before it is cut, and a key after _ or - is a key

Soohong Kimd1b6e1e67b49
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ba492d0..38e8064 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,7 +5,7 @@ All notable public changes to `sprite-gen` are recorded here. Versions track the
## Unreleased (v2.12.0) - A refusal names the provider's code, and says when it was the content policy
- A provider refusal line now carries the provider's own code after the status: `openai-gen: image request failed (HTTP 400) code=invalid_value; no retry or provider fallback`. The code is `error.code` (or, for xAI's synchronous errors, a top-level `code`) and is printed only when it matches `^[A-Za-z0-9_.:-]{1,64}$`. The video verbs read the code out of a failed poll's `error` object too.
-- The image paths now print the provider's error body too, as received, at the end of the line: `openai-gen: image request failed (HTTP 400) code=invalid_value; no retry or provider fallback: {"error": {...}}` (JSON on one line, other text with its line breaks as `\n`, nothing for an empty body; this includes 401/403). Before, the body was dropped and a failure was hard to diagnose. Two things are masked with `[redacted]`: strings shaped like an API key (`sk-…`, `xai-…`, a `Bearer` value, the credential the request carried; a key the provider already masked is left alone) and URL query strings, where signatures live. The video verbs, which already printed what xAI said, now go through the same mask. The body can repeat the prompt: a product that shows sprite-gen's stderr to its own users decides what to keep. Everything in front of the body is unchanged.
+- The image paths now print the provider's error body too, as received, at the end of the line: `openai-gen: image request failed (HTTP 400) code=invalid_value; no retry or provider fallback: {"error": {...}}` (JSON on one line, other text with its line breaks as `\n`, nothing for an empty body; this includes 401/403). Before, the body was dropped and a failure was hard to diagnose. Two things are masked with `[redacted]`: strings shaped like an API key (`sk-…`, `xai-…`, a `Bearer` value, the credential the request carried; a key the provider already masked is left alone) and URL query strings, where signatures live. The video verbs, which already printed what xAI said, now go through the same mask, applied before their 300-character cut so a key the cut would split is still masked. A key right after `_` or `-` (`api_key_sk-…`) is masked too; only a letter or digit in front makes `sk-`/`xai-` part of a word. The body can repeat the prompt: a product that shows sprite-gen's stderr to its own users decides what to keep. Everything in front of the body is unchanged.
- A code the provider documents as a content-policy block turns the line into `<verb>: refused by the provider's content policy (HTTP <n>) code=<code>; no retry or provider fallback`, so a caller can tell "change the request" from an auth, quota or parameter error by one fixed phrase. OpenAI's `moderation_blocked` is the only such code (the image guide calls `error.code` the stable discriminator); its `moderation_details.moderation_stage` follows as `moderation_stage=input|output|unknown`. xAI's `invalid_argument` is not one: it covers moderation and parameter errors alike.
- An xAI clip that finishes with `video.respect_moderation: false` is now `<verb>: generation <id> refused by the provider's content policy (HTTP 200) respect_moderation=false; nothing was written` instead of "done but reported no video url". The xAI REST image answer has no moderation field, so a grok image refusal carries its code only.
- `(HTTP <n>)` is unchanged, and the body comes after it and everything else, so anything that finds the status with `\(HTTP (\d{3})\)` reads these lines as before, whatever the body says. The policy table is `sprite_gen/gen/refusal.py` `POLICY_CODES`. Line shape: [docs/gen.md](docs/gen.md).
diff --git a/sprite_gen/gen/refusal.py b/sprite_gen/gen/refusal.py
index eb47d2a..cf5504a 100644
--- a/sprite_gen/gen/refusal.py
+++ b/sprite_gen/gen/refusal.py
@@ -32,8 +32,10 @@ CODE_PATTERN = re.compile(r"[A-Za-z0-9_.:-]{1,64}")
CONTENT_POLICY = "refused by the provider's content policy"
# A key is a known prefix and a long run of key characters. One the provider
-# already masked (`sk-proj-****…abcd`) carries `*` and is left as it came.
-_KEY = re.compile(r"(?<![A-Za-z0-9_-])(sk|xai)-([A-Za-z0-9_*-]+)")
+# already masked (`sk-proj-****…abcd`) carries `*` and is left as it came. Only a
+# letter or digit before the prefix makes it part of a word (`task-…`); after
+# `_` or `-` (`api_key_sk-…`) the key starts there.
+_KEY = re.compile(r"(?<![A-Za-z0-9])(sk|xai)-([A-Za-z0-9_*-]+)")
_KEY_MIN_LENGTH = 16
_BEARER = re.compile(r"(?i)\b(bearer\s+)[A-Za-z0-9._~+/=-]{16,}")
_URL_QUERY = re.compile(r"(?i)\b(https?://[^\s?#\"'<>\\]+)\?[^\s#\"'<>\\]*")
diff --git a/sprite_gen/gen/video.py b/sprite_gen/gen/video.py
index feec578..1ebca98 100644
--- a/sprite_gen/gen/video.py
+++ b/sprite_gen/gen/video.py
@@ -125,11 +125,12 @@ def _data_url(path: Path) -> str:
def _error_detail(body: Any, secret: str | None = None) -> str:
# What the provider said, with keys and URL query strings masked — the same
- # mask the image lines use (refusal.redact).
+ # mask the image lines use (refusal.redact). Masked before it is cut, so a
+ # key the cut would split is still whole when the mask looks for it.
if not isinstance(body, dict):
- return refusal.redact(str(body)[:300], secret)
+ return refusal.redact(str(body), secret)[:300]
parts = [f"{key}={body[key]!r}" for key in ("code", "error", "message", "status", "raw") if body.get(key)]
- return refusal.redact(", ".join(parts) if parts else json.dumps(body)[:300], secret)
+ return refusal.redact(", ".join(parts), secret) if parts else refusal.redact(json.dumps(body), secret)[:300]
def _redact_host(url: str) -> str | None:
diff --git a/tests/gen/test_video.py b/tests/gen/test_video.py
index deb27e1..6b88b33 100644
--- a/tests/gen/test_video.py
+++ b/tests/gen/test_video.py
@@ -494,6 +494,26 @@ def test_what_xai_said_is_shown_with_keys_and_signatures_masked(tmp_path: Path,
assert HTTP_STATUS.search(message).group(1) == ("400" if scripted == "post" else "200")
+ "x" * 285 + " xai-" + "K" * 60,
+ {"detail": "x" * 270 + " xai-" + "K" * 60},
+], ids=["text", "json"])
+def test_a_key_the_300_character_cut_would_split_is_masked_before_the_cut(body) -> None:
+ """Masked, then cut: cut first, the key's tail was shorter than a key and slipped through."""
+ detail = video._error_detail(body)
+ assert "KKKK" not in detail
+ assert "xai-[redacted]" in detail and len(detail) <= 300
+
+
[email protected](("said", "masked"), [
+ ("api_key_sk-" + "Synthetic0Key1For2Tests" * 2, "api_key_sk-[redacted]"),
+ ("token-xai-" + "Synthetic0Key1For2Tests" * 2, "token-xai-[redacted]"),
+ ("task-" + "Synthetic0Key1For2Tests" * 2, "task-" + "Synthetic0Key1For2Tests" * 2),
+], ids=["underscore", "hyphen", "word"])
+def test_a_key_after_an_underscore_or_hyphen_is_masked_and_a_word_ending_in_sk_is_not(said, masked) -> None:
+ assert video._error_detail({"message": said}) == f"message={masked!r}"
+
+
@pytest.mark.parametrize(("post", "prefix"), [
((400, {"code": "invalid-argument", "error": "synthetic"}), "video: generation request refused (HTTP 400) code=invalid-argument: "),
((400, {"error": {"code": "invalid_argument", "message": "synthetic"}}), "video: generation request refused (HTTP 400) code=invalid_argument: "),