Skip to content

xonika9/agent-skills / skills/x9-codex-imagegen

x9-codex-imagegen

Verified

Use when an agent outside Codex, such as Claude Code or OpenCode on any model, should generate or edit a raster image through the user's Codex subscription — «сгенерируй картинку через Codex», «нарисуй иллюстрацию», "generate an image with Codex". Do not use inside a Codex session, where Codex's own imagegen skill applies, or for editable diagrams, crops, resizes, or other edits made locally without generation, or delegating non-image work to Codex.

FileKindSize
SKILL.mdskill md5.1 KB
references/onboarding.jsonconfig1.2 KB
scripts/codex_image.pyscript16.0 KB
scripts/test_codex_image.pyscript12.0 KB

scripts/test_codex_image.py

View on GitHub
#!/usr/bin/env python3
"""Regression checks for codex_image.py against a fake codex executable."""

from __future__ import annotations

import json
import os
import signal
import socket
import struct
import subprocess
import sys
import tempfile
import time
import zlib
from pathlib import Path

SCRIPT = Path(__file__).with_name("codex_image.py")
sys.path.insert(0, str(SCRIPT.parent))
from codex_image import classify  # noqa: E402

THREAD = "01a11ac5-b216-7f52-97b8-4d5fa1683b73"


def png_bytes(width: int, height: int, color_type: int = 0) -> bytes:
    def chunk(kind: bytes, data: bytes) -> bytes:
        return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data))
    channels = {0: 1, 2: 3, 3: 1, 4: 2, 6: 4}[color_type]
    raw = b"".join(b"\x00" + b"\x00" * width * channels for _ in range(height))
    return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, color_type, 0, 0, 0))
            + chunk(b"IDAT", zlib.compress(raw)) + chunk(b"IEND", b""))


FAKE = f"""#!{sys.executable}
import json, os, re, sys
from pathlib import Path
task = sys.stdin.read()
out = re.search(r"Save the final image as a PNG at (.+?)\\. ", task).group(1)
png = bytes.fromhex({png_bytes(30, 20).hex()!r})
alpha_png = bytes.fromhex({png_bytes(30, 20, 6).hex()!r})
print(json.dumps({{"type": "thread.started", "thread_id": {THREAD!r}}}))
mode = os.environ["FAKE_MODE"]
if mode == "copy":
    Path(out).write_bytes(png)
    kind = "verbatim" if "verbatim" in task.splitlines()[0] else "brief"
    proxy = os.environ.get("HTTPS_PROXY", "")
    reply = f"Saved ({{kind}}) proxy=[{{proxy}}]."
    if kind == "brief":  # like Codex: only the brief task asks for a FINAL_PROMPT line
        reply += "\\nFINAL_PROMPT: a red square, landscape 3:2"
    print(json.dumps({{"type": "item.completed", "item": {{"type": "agent_message", "text": reply}}}}))
elif mode == "alpha":
    Path(out).write_bytes(alpha_png)
elif mode in ("recover", "recover_no_prompt"):
    folder = Path(os.environ["CODEX_HOME"]) / "generated_images" / {THREAD!r}
    folder.mkdir(parents=True)
    (folder / "exec-1.png").write_bytes(png)
elif mode in ("missing_prompt", "empty_prompt"):
    Path(out).write_bytes(png)
    if mode == "empty_prompt":
        print(json.dumps({{"type": "item.completed", "item": {{"type": "agent_message", "text": "FINAL_PROMPT:   "}}}}))
elif mode == "truncated":
    Path(out).write_bytes(png[:26])
elif mode == "nonpng":
    Path(out).write_text("not an image")
elif mode == "network":
    text = "FAILED=network error: error sending request"
    print(json.dumps({{"type": "item.completed", "item": {{"type": "agent_message", "text": text}}}}))
elif mode == "policy":
    text = "FAILED=request rejected by the safety system"
    print(json.dumps({{"type": "item.completed", "item": {{"type": "agent_message", "text": text}}}}))
elif mode == "stderr_network":
    text = "Using the imagegen skill."
    print(json.dumps({{"type": "item.completed", "item": {{"type": "agent_message", "text": text}}}}))
    print("ERROR codex_core::tools::router: error=image generation failed: network error: error sending request",
          file=sys.stderr)
elif mode == "policy_noise":
    text = "FAILED=request rejected by the safety system"
    print(json.dumps({{"type": "item.completed", "item": {{"type": "agent_message", "text": text}}}}))
    print("ERROR rmcp::transport::worker: error sending request for url (https://mcp.example/mcp)", file=sys.stderr)
elif mode == "hang":
    # Like the npm wrapper: a child keeps running if only this process is killed.
    import subprocess, time
    late = "import time, pathlib; time.sleep(4); pathlib.Path(%r).write_bytes(%r)" % (out, png)
    subprocess.Popen([sys.executable, "-c", late])
    sys.stdout.flush()
    time.sleep(60)
if mode in ("alpha", "recover"):
    print(json.dumps({{"type": "item.completed", "item": {{"type": "agent_message", "text": "FINAL_PROMPT: a red square, landscape 3:2"}}}}))
"""


def run(tmp: Path, mode: str, out: Path, *extra: str, proxy: str = "") -> tuple[int, dict]:
    env = dict(os.environ, FAKE_MODE=mode, CODEX_HOME=str(tmp / f"home-{mode}"), X9_CODEX_IMAGE_PROXY=proxy)
    env.pop("HTTPS_PROXY", None)
    brief = tmp / "brief.txt"
    brief.write_text("a red square, landscape 3:2")
    proc = subprocess.run([sys.executable, str(SCRIPT), "--brief-file", str(brief), "--out", str(out),
                           "--codex", str(tmp / "codex"), "--log-dir", str(tmp / f"logs-{mode}"), *extra],
                          capture_output=True, text=True, env=env)
    return proc.returncode, json.loads(proc.stdout)


def main() -> None:
    assert classify("FAILED=network error: error sending request") == "network_error"
    assert classify("FAILED=content policy") == "failed"

    with tempfile.TemporaryDirectory() as name:
        tmp = Path(name)
        (tmp / "codex").write_text(FAKE)
        (tmp / "codex").chmod(0o755)

        code, result = run(tmp, "copy", tmp / "a.png")
        assert code == 0 and result["status"] == "ok", result
        assert (result["width"], result["height"]) == (30, 20) and result["source"] == "codex-copy", result
        assert result["thread_id"] == THREAD and result["has_alpha"] is False, result
        assert result["final_prompt"] == "a red square, landscape 3:2", result
        assert "(brief)" in result["last_message"], result

        for mode in ("missing_prompt", "empty_prompt", "recover_no_prompt"):
            destination = tmp / f"{mode}.png"
            code, result = run(tmp, mode, destination)
            assert code == 1 and result["status"] == "incomplete", result
            assert not result["final_prompt"] and "prompt" in result["error"], result
            assert destination.read_bytes() == png_bytes(30, 20), "deliver the PNG without generating again"
            assert (result["width"], result["height"]) == (30, 20), result

        code, result = run(tmp, "copy", tmp / "v.png", "--verbatim")
        assert code == 0 and "(verbatim)" in result["last_message"], result
        assert result["final_prompt"] == "a red square, landscape 3:2", "verbatim reports the prompt it sent"

        exact = tmp / "exact.txt"
        exact.write_bytes(b"  a red square\r\n\r\n")
        code, result = run(tmp, "copy", tmp / "exact.png", "--verbatim", "--brief-file", str(exact))
        assert code == 0 and result["final_prompt"] == "  a red square\r\n\r\n", result

        code, result = run(tmp, "alpha", tmp / "alpha.png")
        assert code == 0 and result["has_alpha"] is True, result

        code, result = run(tmp, "copy", tmp / "a.png")
        assert code == 1 and result["status"] == "usage_error", "existing file must not be overwritten"

        code, result = run(tmp, "recover", tmp / "b.png")
        assert code == 0 and result["source"].startswith("recovered:"), result

        code, result = run(tmp, "nonpng", tmp / "c.png")
        assert code == 1 and result["status"] == "failed" and "not a PNG" in result["error"], result

        code, result = run(tmp, "truncated", tmp / "truncated.png")
        assert code == 1 and result["status"] == "failed" and "not a PNG" in result["error"], result

        code, result = run(tmp, "network", tmp / "d.png")
        assert code == 1 and result["status"] == "network_error", result
        assert result["idle_cut_suspected"] is False, "a fast network failure is transient"

        listener = socket.socket()
        listener.bind(("127.0.0.1", 0))
        listener.listen()
        proxy = f"http://127.0.0.1:{listener.getsockname()[1]}"
        code, result = run(tmp, "copy", tmp / "p.png", proxy=proxy)
        assert code == 0 and f"proxy=[{proxy}]" in result["last_message"], result
        listener.close()
        code, result = run(tmp, "copy", tmp / "q.png", proxy=proxy)
        assert code == 1 and result["status"] == "proxy_unreachable", result
        assert not (tmp / "q.png").exists(), "no run may start behind a dead proxy"
        credentialed = proxy.replace("http://", "http://synthetic-user:synthetic-password@")
        code, result = run(tmp, "copy", tmp / "secret-proxy.png", proxy=credentialed)
        assert code == 1 and result["status"] == "proxy_unreachable", result
        assert "synthetic-password" not in json.dumps(result), result
        assert "synthetic-user" not in json.dumps(result), result
        code, result = run(tmp, "copy", tmp / "r.png", proxy="socks5h://127.0.0.1:1")
        assert code == 1 and result["status"] == "proxy_unreachable", result
        code, result = run(tmp, "copy", tmp / "bad-proxy.png", proxy="http://localhost:bad")
        assert code == 1 and result["status"] == "proxy_unreachable", result
        code, result = run(tmp, "copy", tmp / "missing-brief.png", "--brief-file", str(tmp / "missing.txt"))
        assert code == 1 and result["status"] == "usage_error", result
        code, result = run(tmp, "copy", tmp / "s.png")
        assert code == 0 and "proxy=[]" in result["last_message"], "no proxy unless configured"

        code, result = run(tmp, "policy", tmp / "e.png")
        assert code == 1 and result["status"] == "failed" and "safety" in result["error"], result

        code, result = run(tmp, "stderr_network", tmp / "h.png")
        assert code == 1 and result["status"] == "network_error", "image tool errors in stderr count"
        assert "image generation failed" in result["error"], result

        code, result = run(tmp, "policy_noise", tmp / "i.png")
        assert code == 1 and result["status"] == "failed", "other connections' errors must not mask a refusal"

        code, result = run(tmp, "hang", tmp / "j.png", "--timeout", "2")
        assert code == 1 and "timed out" in result["error"], result
        time.sleep(4)
        assert not (tmp / "j.png").exists(), "a timed-out run must not leave a process that writes the image"

        # A stop signal to the script must take the whole Codex run down with it.
        env = dict(os.environ, FAKE_MODE="hang", CODEX_HOME=str(tmp / "home-int"), X9_CODEX_IMAGE_PROXY="")
        (tmp / "brief.txt").write_text("a red square, landscape 3:2")
        proc = subprocess.Popen([sys.executable, str(SCRIPT), "--brief-file", str(tmp / "brief.txt"),
                                 "--out", str(tmp / "k.png"), "--codex", str(tmp / "codex"),
                                 "--log-dir", str(tmp / "logs-int")], stdout=subprocess.PIPE, text=True, env=env)
        time.sleep(1.5)
        proc.send_signal(signal.SIGTERM)
        result = json.loads(proc.communicate(timeout=30)[0])
        assert proc.returncode == 1 and result["status"] == "interrupted", result
        time.sleep(4)
        assert not (tmp / "k.png").exists(), "an interrupted run must not leave a process that writes the image"

        (tmp / "broken").write_text("not a program")
        (tmp / "broken").chmod(0o755)
        brief = tmp / "brief.txt"
        proc = subprocess.run([sys.executable, str(SCRIPT), "--brief-file", str(brief), "--out", str(tmp / "l.png"),
                               "--codex", str(tmp / "broken"), "--log-dir", str(tmp / "logs-broken")],
                              capture_output=True, text=True)
        result = json.loads(proc.stdout)
        assert result["status"] == "failed" and "cannot start" in result["error"], result

        code, result = run(tmp, "copy", tmp / "f.png", "--root", str(tmp / "elsewhere"))
        assert code == 1 and result["status"] == "usage_error", result

        # A recently created existing PNG is not evidence that this run wrote it.
        recent = tmp / "recent.png"
        recent.write_bytes(png_bytes(5, 5))
        before = recent.read_bytes()
        code, result = run(tmp, "network", recent, "--overwrite")
        assert code == 1 and result["status"] == "network_error", result
        assert recent.read_bytes() == before

        # A stale PNG at --out must not count as a new result.
        (tmp / "g.png").write_bytes(png_bytes(5, 5))
        os.utime(tmp / "g.png", (0, 0))
        code, result = run(tmp, "network", tmp / "g.png", "--overwrite")
        assert code == 1 and result["status"] == "network_error", result

    print("ok")


if __name__ == "__main__":
    main()
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall