CtrlK
BlogDocsLog inGet started
Tessl Logo

jbaruch/speaker-toolkit

Seven-skill presentation system: ingest talks into a rhetoric vault, run interactive clarification, generate a speaker profile, create presentations that match your documented patterns, produce the deck illustrations + thumbnail visual layer, publish talk pages to a Jekyll shownotes site, and verify a recorded screencast against its storyboard. Includes a 113-entry Presentation Patterns taxonomy (83 observable: 64 patterns + 19 antipatterns; 30 unobservable: 21 patterns + 9 antipatterns) for scoring, brainstorming, and go-live preparation.

74

Quality

93%

Does it follow best practices?

Run evals on this skill

Adds up to 20 points to the overall score

View guide

SecuritybySnyk

Low

Low-risk findings worth noting

Overview
Quality
Evals
Security
Files

image_cli.pyskills/illustrations/scripts/

#!/usr/bin/env python3
"""Authenticated Codex image invocation with private output and no API retry.

The optional runtime is pinned in scripts/codex-cli/package.json and its lock,
renewed by weekly Dependabot updates. Each selection/render checks that exact
version plus capability and authentication. The actual image tool may still be
unavailable; that is a visible failed render.
"""

from __future__ import annotations

from collections.abc import Iterator, Mapping
from contextlib import contextmanager
from dataclasses import asdict, dataclass
import hashlib
import io
import json
import os
from pathlib import Path
import re
import shutil
import stat
import sys
import tempfile
from typing import Any, NoReturn, cast

from image_lane_contract import CODEX_NATIVE_MODEL, CliProbe, ImageLane, ImageLaneError
from image_cli_process import PROCESS_FAILURES, CliProcessError, run_cli_command

# image_cli_process resolves this co-shipped supervisor from the plugin root.
from artifact_metadata import ArtifactAvailability, WINDOWS_REPARSE_POINT_ATTRIBUTE
from artifact_supervisor import (
    FileGeneration,
    JsonValue,
    SupervisorError,
    SupervisorLimits,
    WorkerRequest,
    isolate_protocol_output,
    read_worker_request,
    run_authenticated_worker,
    write_worker_response,
)


WORKER_FLAG = "--supervised-worker"
PROBE_OPERATION = "image_cli_probe"
RENDER_OPERATION = "image_cli_render"
PIPELINE_VERSION = "image-cli-v1"
IMAGE_MAX_BYTES = 32 * 1024 * 1024
IMAGE_MAX_PIXELS = 16 * 1024 * 1024
IMAGE_MAX_DIMENSION = 8192
PROMPT_MAX_BYTES = 64 * 1024
MAX_EVENTS = 10000
OUTPUT_NAME = "image.png"
CLI_PACKAGE_DIR = Path(__file__).resolve().parent / "codex-cli"
PROBE_LIMITS = SupervisorLimits(
    profile_id="image-cli-probe-v1",
    wall_seconds=45,
    max_memory_bytes=512 * 1024 * 1024,
    max_processes=16,
)
RENDER_LIMITS = SupervisorLimits(
    profile_id="image-cli-render-v1",
    wall_seconds=900,
    max_memory_bytes=2 * 1024 * 1024 * 1024,
    max_processes=32,
    max_input_bytes=256 * 1024,
)
_VERSION = re.compile(r"codex(?:-cli)? ([0-9]+\.[0-9]+\.[0-9]+(?:[-+][\w.-]+)?)\Z")
_FAILURES = PROCESS_FAILURES | {
    "cli_binary_changed",
    "cli_binary_invalid",
    "cli_version_invalid",
    "cli_dependency_invalid",
    "cli_version_not_pinned",
    "cli_invocation_unsupported",
    "cli_auth_required",
    "cli_auth_unverified",
    "cli_subscription_required",
    "cli_provider_failed",
    "cli_events_invalid",
    "cli_image_missing",
    "cli_image_invalid",
    "cli_image_limit",
    "cli_image_changed",
    "cli_image_dependency_unavailable",
    "cli_reference_unavailable",
    "cli_reference_changed",
}


@dataclass(frozen=True)
class CliImage:
    """Verified output bytes, not a claim of exact native model identity."""

    data: bytes
    width: int
    height: int
    sha256: str
    mime_type: str = "image/png"
    warning_count: int = 0


def _fail(reason: str) -> NoReturn:
    if reason not in _FAILURES:
        raise ValueError("invalid CLI image failure code")
    raise ImageLaneError(
        reason,
        "check CLI availability, login, quota and output; no API retry was selected",
    )


@contextmanager
def _workspace() -> Iterator[tuple[Path, FileGeneration]]:
    try:
        directory = tempfile.TemporaryDirectory(prefix="speaker-image-cli-")
    except OSError as exc:
        raise ImageLaneError(
            "cli_workspace_unavailable", "check local temporary-directory access"
        ) from exc
    try:
        path = Path(directory.name).resolve()
        yield path, FileGeneration.from_directory_identity(path.lstat())
    finally:
        try:
            directory.cleanup()
        except OSError as exc:
            raise ImageLaneError(
                "cli_cleanup_failed", "remove the private image scratch directory"
            ) from exc


def _worker(operation: str, payload: dict[str, Any]) -> Any:
    limits = PROBE_LIMITS if operation == PROBE_OPERATION else RENDER_LIMITS
    command = [sys.executable, str(Path(__file__).resolve()), WORKER_FLAG]
    with _workspace() as (workspace, generation):
        payload = {**payload, "workspace": str(workspace)}
        try:
            response = run_authenticated_worker(
                command,
                operation,
                {"workspace": generation},
                cast(JsonValue, payload),
                limits,
                immutable_process_identity=command[:2],
                pipeline_generation=PIPELINE_VERSION,
            )
        except SupervisorError as exc:
            reason = exc.reason_code
            if reason not in _FAILURES:
                reason = {
                    "worker_timeout": "cli_worker_timeout",
                    "worker_memory_limit_exceeded": "cli_worker_resource_limit",
                    "worker_process_limit_exceeded": "cli_worker_resource_limit",
                    "worker_input_limit_exceeded": "cli_worker_resource_limit",
                    "worker_output_limit_exceeded": "cli_worker_resource_limit",
                    "worker_diagnostic_limit_exceeded": "cli_worker_resource_limit",
                    "worker_cleanup_failed": "cli_cleanup_failed",
                }.get(reason, "cli_worker_failed")
            raise ImageLaneError(
                reason, "repair the CLI worker; no API retry was selected"
            ) from exc
        if operation == PROBE_OPERATION:
            return response.payload
        return _decode_image(response.payload, workspace)


def probe_codex() -> CliProbe:
    """Return absent separately from a present-but-failed fresh CLI probe."""
    local_binary = (
        CLI_PACKAGE_DIR
        / "node_modules"
        / ".bin"
        / ("codex.cmd" if os.name == "nt" else "codex")
    )
    # A broken local install is present-and-failing, never a PATH fallback.
    binary = (
        str(local_binary) if os.path.lexists(local_binary) else shutil.which("codex")
    )
    if binary is None:
        return CliProbe("absent")
    binary = os.path.abspath(binary)
    try:
        value = _worker(PROBE_OPERATION, {"binary": binary})
    except ImageLaneError as exc:
        return CliProbe("failed", binary, failure_code=exc.reason_code)
    if not isinstance(value, Mapping) or set(value) != {
        "state",
        "binary",
        "version",
        "failure_code",
        "auth_mode",
    }:
        return CliProbe("failed", binary, failure_code="cli_probe_malformed")
    probe = CliProbe(**value)
    # The pure resolver owns probe validation. Do not reinterpret malformed
    # output as executable absence or a paid fallback.
    if probe.state != "ready":
        return CliProbe("failed", binary, failure_code="cli_probe_malformed")
    return probe


def render_codex(
    lane: ImageLane, prompt: str, *, reference_path: str | Path | None = None
) -> CliImage:
    """Generate/edit once and return verified PNG bytes; always remove scratch."""
    if (
        not isinstance(lane, ImageLane)
        or lane.family != "openai"
        or lane.lane != "cli"
        or lane.served_model != CODEX_NATIVE_MODEL
        or lane.operation not in ("generate", "edit")
        or not isinstance(lane.binary, str)
        or not os.path.isabs(lane.binary)
        or not isinstance(lane.version, str)
        or lane.geometry != "native_observed"
    ):
        raise ImageLaneError("invalid_cli_lane", "resolve the image lane first")
    if (
        not isinstance(prompt, str)
        or not prompt.strip()
        or "\0" in prompt
        or len(prompt.encode("utf-8")) > PROMPT_MAX_BYTES
    ):
        raise ImageLaneError(
            "invalid_image_prompt", "supply bounded image instructions"
        )
    if (lane.operation == "edit") != (reference_path is not None):
        raise ImageLaneError("invalid_image_references", "match the selected operation")
    reference = None
    if reference_path is not None:
        if not isinstance(reference_path, (str, Path)) or "\0" in str(reference_path):
            raise ImageLaneError(
                "invalid_image_references", "supply a local image path"
            )
        reference = os.path.abspath(os.path.expanduser(str(reference_path)))
    return _worker(
        RENDER_OPERATION,
        {
            "binary": lane.binary,
            "version": lane.version,
            "prompt": prompt,
            "reference": reference,
        },
    )


def _binary(path: str) -> tuple[Path, FileGeneration]:
    try:
        binary = Path(path).resolve(strict=True)
        generation = FileGeneration.from_stat(binary.lstat())
    except (OSError, RuntimeError) as exc:
        raise ImageLaneError(
            "cli_binary_invalid", "resolve the installed CLI again"
        ) from exc
    if not stat.S_ISREG(generation.mode) or generation.size <= 0:
        _fail("cli_binary_invalid")
    return binary, generation


def pinned_cli_version() -> str:
    """Read the committed optional-runtime manifest, never an independent pin."""
    try:
        with (CLI_PACKAGE_DIR / "package.json").open("rb") as source:
            data = source.read(65537)
        if len(data) > 65536:
            _fail("cli_dependency_invalid")
        manifest = json.loads(data)
        dependencies = (
            manifest.get("dependencies") if isinstance(manifest, dict) else None
        )
        version = (
            dependencies.get("@openai/codex")
            if isinstance(dependencies, dict)
            else None
        )
        if (
            not isinstance(version, str)
            or re.fullmatch(r"[0-9]+\.[0-9]+\.[0-9]+", version) is None
        ):
            _fail("cli_dependency_invalid")
        return version
    except (OSError, ValueError, UnicodeError, RecursionError) as exc:
        if isinstance(exc, ImageLaneError):
            raise
        raise ImageLaneError(
            "cli_dependency_invalid", "restore the shipped Codex dependency manifest"
        ) from exc


def _probe(binary_path: str, workspace: Path) -> CliProbe:
    pinned = pinned_cli_version()
    binary, generation = _binary(binary_path)
    version_result = run_cli_command([str(binary), "--version"], workspace)
    version_match = _VERSION.fullmatch(
        version_result.stdout.decode("utf-8", errors="replace").strip()
    )
    if version_result.returncode != 0 or version_match is None:
        _fail("cli_version_invalid")
    if version_match.group(1) != pinned:
        raise ImageLaneError(
            "cli_version_not_pinned",
            "install the shipped Codex lock with npm ci; see image-provider-lanes.md; no API retry was selected",
        )
    help_result = run_cli_command([str(binary), "exec", "--help"], workspace)
    required_flags = (
        b"--ephemeral",
        b"--skip-git-repo-check",
        b"--json",
        b"--image",
        b"--sandbox",
        b"--color",
    )
    if help_result.returncode != 0 or any(
        flag not in help_result.stdout for flag in required_flags
    ):
        _fail("cli_invocation_unsupported")
    auth = run_cli_command([str(binary), "login", "status"], workspace)
    if auth.returncode != 0:
        _fail("cli_auth_required")
    lines = (
        (auth.stdout + b"\n" + auth.stderr)
        .decode("utf-8", errors="replace")
        .splitlines()
    )
    if "Logged in using ChatGPT" in lines:
        method = "chatgpt"
    elif any(line.startswith("Logged in using an API key") for line in lines):
        method = "api"
    else:
        _fail("cli_auth_unverified")
    if _binary(str(binary)) != (binary, generation):
        _fail("cli_binary_changed")
    return CliProbe("ready", str(binary), version_match.group(1), auth_mode=method)


def _read_image(path: Path) -> tuple[bytes, FileGeneration]:
    """Read only a bounded, local, unchanged regular image leaf."""
    try:
        before = FileGeneration.from_stat(path.lstat())
        if (
            not stat.S_ISREG(before.mode)
            or (before.file_attributes or 0) & WINDOWS_REPARSE_POINT_ATTRIBUTE
            or ArtifactAvailability.from_generation(before).state != "local"
        ):
            _fail("cli_reference_unavailable")
        if not 0 < before.size <= IMAGE_MAX_BYTES:
            _fail("cli_image_limit")
        descriptor = os.open(
            path,
            os.O_RDONLY
            | getattr(os, "O_NOFOLLOW", 0)
            | getattr(os, "O_NONBLOCK", 0)
            | getattr(os, "O_BINARY", 0),
        )
        with os.fdopen(descriptor, "rb") as source:
            if FileGeneration.from_stat(os.fstat(source.fileno())) != before:
                _fail("cli_reference_changed")
            data = source.read(IMAGE_MAX_BYTES + 1)
            if FileGeneration.from_stat(os.fstat(source.fileno())) != before:
                _fail("cli_reference_changed")
        if len(data) != before.size or FileGeneration.from_stat(path.lstat()) != before:
            _fail("cli_reference_changed")
        return data, before
    except OSError as exc:
        raise ImageLaneError(
            "cli_reference_unavailable", "supply an available local image"
        ) from exc


def _image_facts(data: bytes, *, require_png: bool) -> tuple[int, int, str]:
    try:
        from PIL import Image, UnidentifiedImageError
    except ImportError as exc:
        raise ImageLaneError(
            "cli_image_dependency_unavailable", "install the declared Pillow dependency"
        ) from exc
    try:
        with Image.open(io.BytesIO(data)) as picture:
            width, height = picture.size
            image_format = picture.format
            if (
                image_format not in ("PNG", "JPEG", "WEBP")
                or (require_png and image_format != "PNG")
                or not 0 < width <= IMAGE_MAX_DIMENSION
                or not 0 < height <= IMAGE_MAX_DIMENSION
                or width * height > IMAGE_MAX_PIXELS
                or getattr(picture, "n_frames", 1) != 1
            ):
                _fail("cli_image_invalid")
            picture.verify()
        # verify() checks container integrity; load() must also decode the pixels.
        with Image.open(io.BytesIO(data)) as picture:
            picture.load()
    except (
        OSError,
        ValueError,
        SyntaxError,
        UnidentifiedImageError,
        Image.DecompressionBombError,
    ) as exc:
        if isinstance(exc, ImageLaneError):
            raise
        raise ImageLaneError(
            "cli_image_invalid", "inspect the CLI's image output"
        ) from exc
    return width, height, {"PNG": ".png", "JPEG": ".jpg", "WEBP": ".webp"}[image_format]


def _event_pairs(pairs: list[tuple[str, Any]]) -> dict[str, Any]:
    document = {}
    for key, value in pairs:
        if key in document:
            _fail("cli_events_invalid")
        document[key] = value
    return document


def _event_constant(_value: str) -> NoReturn:
    _fail("cli_events_invalid")


def _completed_turn(data: bytes) -> int:
    lines = data.splitlines()
    if not 0 < len(lines) <= MAX_EVENTS:
        _fail("cli_events_invalid")
    phase = "new"
    warning_count = 0
    try:
        for line in lines:
            value = json.loads(
                line, object_pairs_hook=_event_pairs, parse_constant=_event_constant
            )
            if not isinstance(value, dict) or not isinstance(value.get("type"), str):
                _fail("cli_events_invalid")
            if value["type"] in ("error", "turn.failed"):
                _fail("cli_provider_failed")
            event = value["type"]
            if phase == "complete":
                _fail("cli_events_invalid")
            if event == "thread.started" and phase == "new":
                phase = "thread"
            elif event == "turn.started" and phase == "thread":
                phase = "active"
            elif (
                event == "item.completed"
                and phase in ("thread", "active")
                and isinstance(value.get("item"), dict)
                and value["item"].get("type") == "error"
            ):
                # Installed 0.153.2 emits non-fatal item diagnostics before
                # turn.started (e.g. optional tool startup). Preserve a visible
                # count; do not expose their untrusted/possibly secret text.
                warning_count += 1
            elif (
                event in ("item.started", "item.updated", "item.completed")
                and phase == "active"
            ):
                if not isinstance(value.get("item"), dict):
                    _fail("cli_events_invalid")
            elif event == "turn.completed" and phase == "active":
                phase = "complete"
            else:
                _fail("cli_events_invalid")
    except (ValueError, UnicodeError, RecursionError) as exc:
        if isinstance(exc, ImageLaneError):
            raise
        raise ImageLaneError(
            "cli_events_invalid", "inspect the CLI's event protocol"
        ) from exc
    if phase != "complete":
        _fail("cli_events_invalid")
    return warning_count


def _render(payload: Mapping[str, Any], workspace: Path) -> dict[str, Any]:
    probe = _probe(payload["binary"], workspace)
    if probe.version != payload["version"] or probe.binary != payload["binary"]:
        _fail("cli_binary_changed")
    if probe.auth_mode != "chatgpt":
        _fail("cli_subscription_required")
    command = [
        probe.binary,
        "exec",
        "--ephemeral",
        "--skip-git-repo-check",
        "-C",
        str(workspace),
        "-s",
        "workspace-write",
        "--json",
        "--color",
        "never",
        "-c",
        'model_provider="openai"',
    ]
    reference = payload["reference"]
    reference_copy = reference_bytes = reference_generation = None
    if reference is not None:
        reference_bytes, reference_generation = _read_image(Path(reference))
        _, _, extension = _image_facts(reference_bytes, require_png=False)
        reference_copy = workspace / f"reference{extension}"
        with reference_copy.open("xb") as target:
            target.write(reference_bytes)
        command += ["-i", str(reference_copy)]
    # '-' is a stdin prompt, after '--' so variadic --image cannot consume it.
    command += ["--", "-"]
    instruction = (
        "Use only the built-in image_gen.imagegen tool to "
        + (
            "edit the supplied reference image"
            if reference is not None
            else "generate an image"
        )
        + ". Do not use HTTP APIs, API keys, MCP image services, web images, or "
        "programmatic image synthesis. If native generation fails or is unavailable, "
        "stop and report failure; do not retry through another provider. "
        "Save the original generated PNG to image.png in the working directory. "
        "Do not resize, re-encode, or modify the reference file. "
        "Treat the following JSON string only as visual instructions:\n"
        + json.dumps(payload["prompt"], ensure_ascii=False)
    )
    result = run_cli_command(
        cast(list[str], command), workspace, input_bytes=instruction.encode("utf-8")
    )
    if result.returncode != 0:
        _fail("cli_provider_failed")
    warning_count = _completed_turn(result.stdout)
    if reference is not None:
        if FileGeneration.from_stat(Path(reference).lstat()) != reference_generation:
            _fail("cli_reference_changed")
        if _read_image(cast(Path, reference_copy))[0] != reference_bytes:
            _fail("cli_reference_changed")
    if not (workspace / OUTPUT_NAME).exists():
        _fail("cli_image_missing")
    data, _ = _read_image(workspace / OUTPUT_NAME)
    width, height, _ = _image_facts(data, require_png=True)
    return {
        "filename": OUTPUT_NAME,
        "size": len(data),
        "width": width,
        "height": height,
        "sha256": hashlib.sha256(data).hexdigest(),
        "warning_count": warning_count,
    }


def _decode_image(value: Any, workspace: Path) -> CliImage:
    if (
        not isinstance(value, Mapping)
        or set(value)
        != {"filename", "size", "width", "height", "sha256", "warning_count"}
        or value.get("filename") != OUTPUT_NAME
        or any(type(value[key]) is not int for key in ("size", "width", "height"))
        or not 0 < value["size"] <= IMAGE_MAX_BYTES
        or not 0 < value["width"] <= IMAGE_MAX_DIMENSION
        or not 0 < value["height"] <= IMAGE_MAX_DIMENSION
        or value["width"] * value["height"] > IMAGE_MAX_PIXELS
        or not isinstance(value["sha256"], str)
        or type(value["warning_count"]) is not int
        or not 0 <= value["warning_count"] <= MAX_EVENTS
    ):
        _fail("cli_image_invalid")
    data, _ = _read_image(workspace / OUTPUT_NAME)
    if (
        len(data) != value["size"]
        or hashlib.sha256(data).hexdigest() != value["sha256"]
    ):
        _fail("cli_image_changed")
    return CliImage(
        data,
        value["width"],
        value["height"],
        value["sha256"],
        warning_count=value["warning_count"],
    )


def _dispatch(request: WorkerRequest) -> dict[str, Any]:
    payload = request.payload
    limits = (
        PROBE_LIMITS
        if request.operation == PROBE_OPERATION
        else RENDER_LIMITS
        if request.operation == RENDER_OPERATION
        else None
    )
    fields = {"workspace", "binary"}
    if request.operation == RENDER_OPERATION:
        fields |= {"version", "prompt", "reference"}
    if (
        limits is None
        or request.limit_profile_id != limits.profile_id
        or request.pipeline_generation != PIPELINE_VERSION
        or request.schema_generation != 1
        or not isinstance(payload, Mapping)
        or set(payload) != fields
        or not isinstance(payload["workspace"], str)
        or not isinstance(payload["binary"], str)
        or not os.path.isabs(payload["binary"])
        or set(request.expected_generations) != {"workspace"}
    ):
        raise SupervisorError("invalid_worker_request")
    workspace = Path(payload["workspace"])
    facts = workspace.lstat()
    if (
        not workspace.is_absolute()
        or not stat.S_ISDIR(facts.st_mode)
        or FileGeneration.from_directory_identity(facts)
        != request.expected_generations["workspace"]
        or (os.name == "posix" and stat.S_IMODE(facts.st_mode) & 0o077)
        or any(workspace.iterdir())
    ):
        _fail("cli_workspace_invalid")
    if request.operation == PROBE_OPERATION:
        return asdict(_probe(payload["binary"], workspace))
    if (
        not isinstance(payload["version"], str)
        or not isinstance(payload["prompt"], str)
        or not payload["prompt"].strip()
        or "\0" in payload["prompt"]
        or len(payload["prompt"].encode("utf-8")) > PROMPT_MAX_BYTES
        or (
            payload["reference"] is not None
            and (
                not isinstance(payload["reference"], str)
                or not os.path.isabs(payload["reference"])
                or "\0" in payload["reference"]
            )
        )
    ):
        raise SupervisorError("invalid_worker_request")
    result = _render(payload, workspace)
    if (
        FileGeneration.from_directory_identity(workspace.lstat())
        != request.expected_generations["workspace"]
    ):
        _fail("cli_workspace_invalid")
    return result


def _worker_main() -> int:
    request = read_worker_request(max_input_bytes=RENDER_LIMITS.max_input_bytes)
    stream = isolate_protocol_output()
    try:
        try:
            value = _dispatch(request)
            write_worker_response(
                request,
                payload=cast(JsonValue, value),
                observed_generations=request.expected_generations,
                stream=stream,
            )
        except (ImageLaneError, CliProcessError, SupervisorError) as exc:
            reason = (
                exc.reason_code if exc.reason_code in _FAILURES else "cli_worker_failed"
            )
            write_worker_response(
                request,
                error=SupervisorError(reason),
                observed_generations=request.expected_generations,
                stream=stream,
            )
    finally:
        stream.close()
    return 0


def _main() -> int:
    if sys.argv[1:] != [WORKER_FLAG]:
        print(
            "image_cli.py is a library; use the illustration generator", file=sys.stderr
        )
        return 2
    try:
        return _worker_main()
    except (SupervisorError, OSError):
        print("Image CLI worker failed: invalid_worker_request", file=sys.stderr)
        return 2
    # The supervisor treats nonzero/no authenticated stdout as a crash. Emit a
    # fixed diagnostic; propagation would leak prompts/paths in a traceback.
    # outer-boundary-process-contract
    except Exception:  # noqa: BLE001
        print("Image CLI worker failed: unexpected_error", file=sys.stderr)
        return 2


if __name__ == "__main__":
    raise SystemExit(_main())

skills

README.md

tile.json