Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
docs(pixel-draw): the safe area - prepare's margin, the floor row, G8, G6 inside the safe area, and what seven drawings under the new prompt did
Soohong Kima8bca1a3176b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c20d08c..f321753 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -4,7 +4,7 @@ All notable public changes to `sprite-gen` are recorded here. Versions track the## Unreleased-- **New (experimental): `sprite-gen pixel-draw` — Claude Opus draws pixel art of 8–48 px directly, as a pixel grid.** The model writes a palette and one line of keys per pixel row (a character grid, or JSON with `--format json`); code checks it and renders it. `--subject` (or `--spec`), `--size` 8–48 (32), `--kind sprite|icon|tile`, `--frames` (up to four, drawn in one call; `--walk-cycle` for contact, passing, contact, passing), `--palette free[:N]|pico8|#hex,…|file`, `--candidates` (2), `--effort medium|high|xhigh` (high), `--batch` (Message Batches, half price), `--max-tokens` (32000 for one frame, 64000 for more), `--pick vision|human`. G1 reads every answer strictly; a row one key too long or short, a key outside the palette or rows missing at the end go back to the model on their own with a column ruler and are spliced in (two rounds), and an answer still broken — or broken beyond its rows — is drawn once more; a single frame written without its `FRAME` line is recovered and recorded. G2 palette, G3 alpha and G6 frames (one ground line, heights within a pixel, no two frames the same) fail a candidate; G4 tile seam, G5 stray pixels and a walk whose legs barely change sides warn. Of two or more candidates that pass, one vision call at effort low picks (G7). An answer that spent every token thinking is drawn once more one effort lower; a refusal is reported as it came (`refused by the provider's content policy (stop_reason=refusal category=…)`); nothing falls back to another model. The run folder holds `grid.txt` (the truth: edit it and re-render with `--grid`), `palette.json`, `frame-<name>.png` written by an encoder whose bytes depend on the pixels alone, `preview-x<s>.png`, `tiled-3x3.png` for a tile, `anim.gif` for several frames, every call under `calls/` and `report.json` with each call's model, effort, usage (thinking tokens included), price, seconds and request id, the gate results and the repair history. A batch is recorded the moment it is submitted, and `--resume` collects it — and reads every answered call back — instead of buying it twice. It runs on `ANTHROPIC_API_KEY`, billed per call and said so on stderr; the transport is urllib, so the dependency surface stays Pillow and NumPy — [docs/pixel-draw.md](docs/pixel-draw.md).+- **New (experimental): `sprite-gen pixel-draw` — Claude Opus draws pixel art of 8–48 px directly, as a pixel grid.** The model writes a palette and one line of keys per pixel row (a character grid, or JSON with `--format json`); code checks it and renders it. `--subject` (or `--spec`), `--size` 8–48 (32), `--kind sprite|icon|tile`, `--frames` (up to four, drawn in one call; `--walk-cycle` for contact, passing, contact, passing), `--palette free[:N]|pico8|#hex,…|file`, `--candidates` (2), `--effort medium|high|xhigh` (high), `--batch` (Message Batches, half price), `--max-tokens` (32000 for one frame, 64000 for more), `--pick vision|human`. G1 reads every answer strictly; a row one key too long or short, a key outside the palette or rows missing at the end go back to the model on their own with a column ruler and are spliced in (two rounds), and an answer still broken — or broken beyond its rows — is drawn once more; a single frame written without its `FRAME` line is recovered and recorded. A sprite or icon keeps the engine's safe area — `prepare`'s margin, 9.4% of the side, floored, taken from `prepare` rather than copied: at 32 px the body is drawn inside the central 26×26 (16 → 14, 24 → 20, 48 → 40), only a thin detail such as a weapon tip, a strand of hair or an effect reaches into the margin, the canvas's outermost rows and columns stay clear, and a sprite's feet stand on the safe area's bottom row, the row `prepare`'s fit grounds a frame on, in every frame; the prompt says it in rows and columns counted from 1. G2 palette, G3 alpha, G6 frames (one ground line, heights within a pixel, no two frames the same — a sprite's or icon's measured inside the safe area, so a staff or a spark that reaches into the margin in one frame is not a taller figure) and G8 safe area (no pixel on the canvas edge) fail a candidate; G4 tile seam, G5 stray pixels, a walk whose legs barely change sides, a body more than a pixel past the safe area and feet off its bottom row warn. Of two or more candidates that pass, one vision call at effort low picks (G7). An answer that spent every token thinking is drawn once more one effort lower; a refusal is reported as it came (`refused by the provider's content policy (stop_reason=refusal category=…)`); nothing falls back to another model. The run folder holds `grid.txt` (the truth: edit it and re-render with `--grid`), `palette.json`, `frame-<name>.png` written by an encoder whose bytes depend on the pixels alone, `preview-x<s>.png`, `tiled-3x3.png` for a tile, `anim.gif` for several frames, every call under `calls/` and `report.json` with each call's model, effort, usage (thinking tokens included), price, seconds and request id, the gate results and the repair history. A batch is recorded the moment it is submitted, and `--resume` collects it — and reads every answered call back — instead of buying it twice. It runs on `ANTHROPIC_API_KEY`, billed per call and said so on stderr; the transport is urllib, so the dependency surface stays Pillow and NumPy — [docs/pixel-draw.md](docs/pixel-draw.md).- **Fix: a poll xAI answers 429 or 5xx asks the same request again — the clip is bought once.** `video`, `video-extend` and `video-edit` ended the run on any non-2xx poll, after xAI had accepted the request and may bill it, and the line read `(HTTP 429)` — what `video-set`, and any caller that backs off on a refused submission, answers by submitting the clip again: a second clip paid for, the first never published. A poll answered `429`, `500`, `502`, `503` or `504` is now asked again on the same `request_id`, after a wait that doubles from 8 s up to 60 s while the polls are refused and goes back to 4 s on an answer; a stderr note says each, the status in plain digits. The poll timeout (`SPRITE_GEN_VIDEO_TIMEOUT_SECONDS`) still bounds the whole poll, a pending poll's waits included (and, below, the downloads). A refused POST is still never sent again, and a `400` or `404` poll still ends the run with its status (a `401` or `403` poll is unfinished, below).- **Output change: a generation the poll timeout cut short is unfinished, named by its `request_id`.** Polls still pending or still refused at the timeout end the run with a line that names `request_id=<id>`, says the generation may still finish and be billed, and carries no `(HTTP <n>)`, a refusal line's status — a caller that reads `(HTTP 429)` there sends the request again, unbilled only when it was refused. The pending line keeps its front (`still 'pending' after N polls — poll timeout;`). With `--report` the run leaves a `sprite-gen-video-unfinished` report there (`verb`, `auth_source`, `model`, `request_id`, `out`, `polls`, `polls_refused`, `last_poll`, `elapsed_seconds`) and prints nothing on stdout; Python callers catch `GenerationUnfinished`, a `SystemExit`. The success report gains `polls_refused`, how many of its `polls` xAI answered 429 or 5xx (docs/video.md, An unfinished generation).- **`tests/gen/test_video.py`** scripts the polls: each of the five statuses refused twice, then pending, then done, is asked on the one request with waits 8, 16, 4 and published with `polls_refused` 2; polls refused until a 300 s timeout wait 8, 16, 32, 60 × 4, ask no poll whose wait would reach the timeout and end unfinished after eight with no `(HTTP n)` and no `HTTP 429` on stderr; a pending timeout names the request; a 400 or 404 poll ends at once; a refused POST is sent once; the CLI leaves the unfinished report and an empty stdout. The 500-poll case of the failure table is a 404 now, the status that still ends a run.diff --git a/docs/README.md b/docs/README.mdindex 7894212..19ba962 100644--- a/docs/README.md+++ b/docs/README.md@@ -78,7 +78,7 @@ grouping is derived from `sprite_gen/_modules.py`, the one taxonomy table.| Doc | Owns ||---|---|| [gen.md](gen.md) | `sprite-gen gen` / `gen-set`: providers, default resolution, transparency strategy per provider, row usage |-| [pixel-draw.md](pixel-draw.md) | `sprite-gen pixel-draw` (experimental): Claude Opus draws 8–48 px pixel art as a pixel grid — the inputs, the run folder and its report, gates G1–G7, the row repair and the one redraw, the pick between candidates, Message Batches and `--resume`, re-rendering an edited `grid.txt` |+| [pixel-draw.md](pixel-draw.md) | `sprite-gen pixel-draw` (experimental): Claude Opus draws 8–48 px pixel art as a pixel grid — the inputs, the safe area, the run folder and its report, gates G1–G8, the row repair and the one redraw, the pick between candidates, Message Batches and `--resume`, re-rendering an edited `grid.txt` || [prompt-assembly.md](prompt-assembly.md) | What the engine adds to a still or clip prompt and when: the pieces, the 2.22.0 freeze without `--handed`, what counts as already said, and the notes on the caller's own words || [video.md](video.md) | `sprite-gen video` / `video-extend` / `video-edit` / `video-resume`: stills and clips to mp4 through Grok Imagine (image-to-video, last-frame pin, references, extension, editing, asking again for an unfinished generation) with the user's own credential || [video-pipeline.md](video-pipeline.md) | Pipeline B engine contract: state canvas, keyed frames, true-period and one-shot cycles, strip/GIF/WebP, the batch, a set's directions on one sheet |diff --git a/docs/pixel-draw.md b/docs/pixel-draw.mdindex 0e2395f..c145db8 100644--- a/docs/pixel-draw.md+++ b/docs/pixel-draw.md@@ -4,7 +4,7 @@> varies from run to run: expect to compare candidates, re-draw, or touch up `grid.txt` by hand. The numbers below come from a> small set of measured runs, not from a benchmark.-> Owns: `sprite-gen pixel-draw`: the inputs, the run folder and `report.json`, gates G1–G7, the row repair, the one redraw, the pick between candidates, Message Batches and `--resume`, re-rendering an edited `grid.txt` · Index: [docs/README.md](README.md)+> Owns: `sprite-gen pixel-draw`: the inputs, the safe area of a sprite or icon, the run folder and `report.json`, gates G1–G8, the row repair, the one redraw, the pick between candidates, Message Batches and `--resume`, re-rendering an edited `grid.txt` · Index: [docs/README.md](README.md)The model does not paint an image that is then shrunk to a grid. It writes the grid itself: apalette of keys and, per frame, one line of keys per pixel row. Code checks every answer, asks for@@ -68,8 +68,8 @@ candidate's files are at the top of the run folder.|---|---|---|| `--subject TEXT` | words | What to draw. For several frames, say what each one is (the frame names are listed for the model, their meaning comes from your words). || `--spec FILE` | JSON object | `subject`, `size`, `kind`, `frames`, `walk_cycle`, `palette` instead of the flags. A key given both in the file and as a flag is refused. |-| `--size N` | 8–48 (32) | Canvas side in pixels; every frame is N×N. Measured best at 32 or less. |-| `--kind` | `sprite`, `icon`, `tile` (`sprite`) | Sprites and icons have a transparent background and a dark outline; a tile fills every pixel and repeats seamlessly. |+| `--size N` | 8–48 (32) | Canvas side in pixels; every frame is N×N, a sprite's or icon's body inside the [safe area](#the-safe-area) (26×26 at 32). Measured best at 32 or less. |+| `--kind` | `sprite`, `icon`, `tile` (`sprite`) | Sprites and icons have a transparent background, a dark outline and a margin around the body; a tile fills every pixel and repeats seamlessly. || `--frames a,b,…` | 1–4 names (one frame named after `--kind`) | Frames drawn **in one call**, so they share one palette and one design. Names are letters, digits, `_` and `-`. || `--walk-cycle` | flag | The four frames are contact, passing, contact, passing: the prompt says so and G6 checks the legs change sides. || `--palette` | `free[:N]` (`free:16`), `pico8`, `#RRGGBB,…`, or a file | A free palette allows at most N colours; a fixed one allows only its colours. A file is JSON (a list, or `{"colors": [...]}`) or one colour per line (`RRGGBB` or `#RRGGBB`, `;` comments — Lospec's `.hex`). |@@ -89,8 +89,8 @@ candidate's files are at the top of the run folder.For each candidate, independently (in parallel, or in one batch):1. **Draw** at `--effort`, with the prompt built from the inputs: the subject, the canvas, the frames- (and the walk cycle), the palette rule, the kind's background and style rules, and the exact- output format.+ (and the walk cycle), the palette rule, the kind's background, [safe area](#the-safe-area) and+ style rules, and the exact output format.2. **G1 parse.** The answer is read strictly ([below](#gates)). A row one key too long or short, akey that is not in the palette, rows missing at the end — those rows go back to the model ontheir own, each with the rows above and below and a column ruler, and it answers@@ -100,7 +100,8 @@ For each candidate, independently (in parallel, or in one batch):a broken palette, a frame missing, too many rows), is drawn again at the same effort. An answerthat spent all `--max-tokens` thinking and wrote nothing is drawn again **one effort lower**(xhigh → high → medium → low). A candidate is drawn at most twice in all.-4. **G2–G6** on the pixels. G2, G3 and G6 fail the candidate; G4, G5 and the walk check warn.+4. **G2–G6 and G8** on the pixels. G2, G3, G6 and G8 fail the candidate; G4, G5, the walk check and+ G8's body and floor checks warn.5. **G7 pick.** Of two or more candidates that passed, one vision call (`claude-opus-5-5`, effortlow, structured output) looks at all of them on one sheet, shuffled under letters, scores each andnames the best; with `--pick human` nobody picks and the candidates wait for a person. One that@@ -109,6 +110,45 @@ For each candidate, independently (in parallel, or in one batch):A refusal is reported as it came and that candidate is not drawn again. Nothing falls back toanother model, provider or effort beyond the one lowered redraw.+## The safe area++A pixel artist drawing a 32×32 sprite does not fill the cell: the body sits in about 26×26, and a+sword tip or a strand of hair may stray past it. sprite-gen keeps the same reserve around every cell it+lays out — [`prepare`](run-contract.md)'s safe margin, 9.4% of the side, floored — and `pixel-draw` takes+its numbers from there instead of keeping its own:++| Canvas | Margin | Safe area | Floor row (counted from 1) | A thin detail may reach past it |+|---|---|---|---|---|+| 8–10 | 0 | the whole canvas | the last row | — |+| 16 | 1 | 14×14 | 15 | not at all: the margin is the canvas edge |+| 24 | 2 | 20×20 | 22 | 1 px |+| 32 | 3 | 26×26 | 29 | 2 px |+| 48 | 4 | 40×40 | 44 | 3 px |++The prompt of a sprite or an icon says these in rows and columns the model can count: the body inside+the safe area; only a thin detail — a weapon tip, a strand of hair, an effect — in the margin; the+canvas's outermost rows and columns transparent; and for a sprite, the feet on the floor row, the safe+area's bottom row, in every frame. That is the row `prepare`'s fit grounds a frame on, so a sprite+drawn here stands where sprite-gen's other sprites of the same cell size stand. (`gen --layout-guide`+draws its floor line one margin further in: that is room for an image model's still to bob into once+a video moves it, and a grid drawn here is the frame itself.)++G8 checks it ([below](#gates)). A pixel on the canvas edge fails. The body box — the rows and columns+holding at least a quarter as many opaque pixels as the fullest one, which a blade, a strand or a+spark does not — more than a pixel past the safe area warns, and so do feet off the floor row (a+sprite that flies or floats is told the same floor and gets the same warning). G6 compares the frames+inside the safe area for the same reason: a weapon raised or an effect thrown into the margin in one+frame leaves the figure's height and floor as they were. `--grid` measures a grid by its own size,+each side on its own when it is not square. A tile has no margin: it fills every pixel and repeats.++Measured on `claude-opus-5-5` at effort high, one candidate, on 2026-10-13 — seven drawings: a knight+and a wizard standing, walking and attacking at 32 px, the knight standing at 16 px. Every one kept its+body inside the safe area, stood on the floor row in every frame and left the canvas edge clear; a+blade, a staff and a spark reached one or two pixels into the margin. Under the earlier prompt, which+asked to fill the canvas, the same knight (2026-10-12) stood 29–31 px tall with its boots two rows+below the floor row, and one of its walks put the plume on the canvas edge. One wizard walk drew its+two passing frames alike and failed G6, as walk drafts do.+## Gates| Gate | Checks | On failure |@@ -118,7 +158,8 @@ another model, provider or effort beyond the one lowered redraw.| G3 alpha | Sprite/icon: something drawn, four transparent corners, border at least half transparent. Tile: every pixel opaque | fails || G4 tile seam | Colour step across the wrap edges ÷ the mean step inside the tile ≤ 1.5 | warns — mortar lines that meet the edge raise it on tiles that repeat cleanly; look at `tiled-3x3.png` || G5 stray pixels | Opaque pixels of a sprite/icon with no opaque 4-neighbour | warns |-| G6 frames | Same bottom row in every frame, heights within one pixel, no two frames the same. With `--walk-cycle`: the contact frames differ by at least 40% of the mean contact↔passing difference | fails; the walk check warns ("weak leg alternation — touch up the legs") |+| G6 frames | Same bottom row in every frame, heights within one pixel, no two frames the same; a sprite's or icon's rows are measured inside the [safe area](#the-safe-area), so a staff raised or a spark into the margin in one frame is not a taller figure (the margin is G8's). With `--walk-cycle`: the contact frames differ by at least 40% of the mean contact↔passing difference | fails; the walk check warns ("weak leg alternation — touch up the legs") |+| G8 safe area | Sprite/icon: no opaque pixel on the canvas's outermost row or column; the body box no more than 1 px past the [safe area](#the-safe-area); a sprite's body box ending on the floor row | a pixel on the edge fails; the body past the safe area, and feet off the floor row, warn. Below 11 px the margin is 0 and there is no edge to keep || G7 pick | Two or more passed: the vision pick (or a person) | a pick that fails ends the run `pick_failed`, candidates kept |## The run folder@@ -154,7 +195,7 @@ zlib build. The previews, the tiling and the GIF are views for the eye.| `request` | the resolved inputs (`palette` with its rule and colours) || `chosen`, `files`, `warnings` | the picked candidate, its top-level files, its warnings || `pick` | `by` (`vision` / `only` / `human`), `best`, and for the vision pick its `labels`, `sheet` and `verdict` |-| `candidates[]` | per candidate: `status`, `reason`, `effort` of the answer kept, `generations` (call ids), `redraws` (after which call, why, at which effort), `repairs` (rows asked, answered, still broken), `G1` (issues and what was recovered), `gates` (G2–G6 with their measures), `warnings`, `files` |+| `candidates[]` | per candidate: `status`, `reason`, `effort` of the answer kept, `generations` (call ids), `redraws` (after which call, why, at which effort), `repairs` (rows asked, answered, still broken), `G1` (issues and what was recovered), `gates` (G2–G6 and G8 with their measures), `warnings`, `files` || `calls[]` | per call: `id` (`c1-gen1`, `c1-gen1-fix1`, `pick`), `purpose`, `mode` (`realtime` / `batch`), `model`, `effort`, `max_tokens`, `request_id`, `message_id`, `batch_id`, `stop_reason`, `stop_details`, `usage` (as the API reported it), `thinking_tokens`, `cost_usd`, `seconds`, and for a failed call `error` and `billed` || `batches[]` | per batch: `id`, `custom_ids`, `submitted_at`, `collected`, `ended_at`, `waited_seconds` || `usage`, `cost_usd`, `seconds` | totals over the calls, and the run's wall time |@@ -203,7 +244,8 @@ sprite-gen pixel-draw --grid runs/slime/grid.txt --out-dir runs/slime-edit [--ki```Reads a character grid (the frames and size are what it holds), runs G1 (no repair: an issue is-reported with its frame and row) and G2–G6, and writes the same files and a report with no calls.+reported with its frame and row) and G2–G6 and G8 — the safe area of the grid's own size — and writes+the same files and a report with no calls.Pass the `--kind`, `--palette` and `--walk-cycle` the grid was drawn under; the run's`request.json` has them.