model2code

A code-generation command that translates a checked Z specification—a formal description of a system’s data and behavior—into implementation code and unit tests.

In plain words
What is it for?
Use it with Swift, TypeScript, Python, or Kotlin projects to generate code from a .tex specification.
Why use it?
It connects a precise system model with code, while checking the model first so errors are found before generation.

Command

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add commands/punt-labs/z-spec/model2code
Clone the repo
git clone --depth 1 https://github.com/punt-labs/z-spec
Per session 6 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 2,153 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin 98% copy Near-identical to another mod in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00006 $0.02153
Opus 5 $0.00003 $0.01077
Sonnet 5 $0.00001 $0.00431
Haiku 4.5 $0.00001 $0.00215

Measured 2d ago against content hash 082b37a102d3, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

model2code scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

Origin

This is a copy

98% identical to model2code-dev — 6 lines differ, which has more behind it and is treated as the original. This page carries a canonical link to it rather than competing with it.

plugin/commands/model2code.md · 350 lines

How it starts

The opening of the file, as written. The whole thing — 350 lines — stays where its author put it; the contents beside it link to each section on GitHub.

/z-spec:model2code - Model to Code

Generate implementation code and unit tests from a Z specification.

Usage

/z-spec:model2code [spec.tex] [language]

Arguments:

  • spec.tex - Z specification file (default: search in docs/)
  • language - Target: swift, typescript, python, kotlin (default: detect from project)

Workflow

Step 1: Validate the Specification

Before generating code, type-check the specification with fuzz:

fuzz -t <spec.tex>

If fuzz reports errors, fix them first. Code generation from invalid specs produces invalid code.

Step 2: Detect Target Language

If no language specified, detect from project files:

File Present Language
Package.swift, *.xcodeproj Swift
package.json, tsconfig.json TypeScript
pyproject.toml, setup.py, requirements.txt Python
build.gradle.kts, pom.xml Kotlin

Step 3: Generate Code

Read the Z specification and translate each construct to idiomatic code for the target language.

Translation Principles

State Schemas → Classes/Structs

Z state schemas become data types with properties:

\begin{schema}{Account}
balance : \num \\
status : Status
\where
balance \geq 0 \lor status = suspended
\end{schema}

Swift:

struct Account {
    var balance: Int
    var status: Status

    var isValid: Bool {
        balance >= 0 || status == .suspended
    }
}

TypeScript:

interface Account {
    balance: number;
    status: Status;
}

function isValidAccount(a: Account): boolean {
    return a.balance >= 0 || a.status === Status.Suspended;
}

Free Types → Enums

\begin{zed}
Status ::= pending | active | suspended | closed
\end{zed}

Swift:

enum Status {
    case pending, active, suspended, closed
}

TypeScript:

enum Status {
    Pending = "pending",
    Active = "active",
    Suspended = "suspended",
    Closed = "closed"
}

Read the full file on GitHub · 350 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 350 lines · 6 tokens per session scan A 082b37a102d3

Subscribe to this mod's changes

model2code is a command published in the GitHub repository punt-labs/z-spec (5 stars, last pushed 2d ago), licensed MIT. It adds 6 tokens to every session and 2,153 once invoked, about $0.0000 per session on Opus 5. A static security scan graded it A with 0 findings. It is 98% identical to model2code-dev, differing in 6 lines, and is treated as a copy.