polygen

A code-authoring command that turns a feature description into a written contract and JavaScript or TypeScript code checked against that contract. It can also create example and regression traces, which are recorded behavior scenarios used to catch future mistakes.

In plain words
What is it for?
Use it to author verifiable state-machine modules, check their rules locally, and produce demonstrations or regression cases.
Why use it?
It brings the specification and its checks into the code-writing process, then can repair reachable violations before the result is delivered.

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/cognitive-fab/polygraph/polygen
Clone the repo
git clone --depth 1 https://github.com/cognitive-fab/polygraph
Per session 43 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 1,120 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found 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.00043 $0.01120
Opus 5 $0.00022 $0.00560
Sonnet 5 $0.00009 $0.00224
Haiku 4.5 $0.00004 $0.00112

Measured yesterday against content hash 7bcd1593e788, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

polygen 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 yesterday.

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.

commands/polygen.md · 78 lines

How it starts

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

Run polygen over the arguments in $ARGUMENTS. This is the AUTHOR side of the method (companion to /polygraph:verify, which AUDITS existing code): instead of deriving a spec from code that already exists, it writes new code that is verifiable from the moment it's written.

This drives ${CLAUDE_PLUGIN_ROOT}/scripts/polygen.mjs:

node ${CLAUDE_PLUGIN_ROOT}/scripts/polygen.mjs \
  --intent "<feature description>" --model <id> \
  [--contract <c.json>] [--lang javascript] [--out out/] \
  [--repair-max 3] [--max-tokens 32000]

The scripted run needs ANTHROPIC_API_KEY and an explicit --model (no default — pass the exact Anthropic model id if not using a known alias). Recommend opus-5 with the repair loop on (the default); fable-5 only for one-shot runs with --repair-max 0; on an API policy refusal retry with opus-4.8 (per-step source of truth: RECOMMENDED_MODELS in scripts/models.mjs). v1 is JS/TS only.

No ANTHROPIC_API_KEY in the environment? Do not fail — ASK the user, with the tradeoffs. Only the authoring model call needs the key; every gate is keyless local execution. Put the choice to them per the polygen skill's Step 0: "I don't have an API key — (a) continue keyless: I author the artifacts in this session and run every mechanical gate locally (same checking strength, zero API cost; you give up a pinned model id and a scripted, re-runnable authoring step), or (b) supply a key for the scripted CI-grade run (pinned model, automated repair loop, standard report)?" If they choose keyless: author in-session in the same artifact style, run the same gates (check.mjs, corpus synthesis + validate_corpus.mjs, separate-process replay), fix code at counterexamples until clean, and record the provenance ("authored in-session, keyless") in the handoff.

What it does, in order:

  1. Drafts a contract.json from the feature description (or uses one you supply with --contract) — the observable state, action alphabet, dataDomain (concrete enumerable values — required for model checking to see parameterized actions at all), terminal states, and special rules.
  2. Authors the module against that contract — by default a SAM v2 strict-profile module (named intents/schemas/domains, keyed acceptors, reject(reason), sealed model; must load strict-clean through the validate() gate).
  3. Proposes invariants.mjs — rules encoding intent, not just behavior.
  4. Self-repairs: model-checks the code against its own invariants (exhaustive reachability, same engine as check.mjs), and on a reachable violation, patches the code and re-checks — capped at --repair-max (default 3). A run that does not converge within budget is reported as NOT converged, never silently presented as clean.
  5. Synthesizes a demo/regression trace corpus by driving the final code through model-proposed scenarios, validates it, and independently replays it in a separate process as a sanity check.

Steps to perform:

  1. Run the command above with the parsed arguments.
  2. Read <out>/polygen-report.md and walk the user through it: the contract (flag if model-drafted — review before use), the code, the invariants (flag as proposed, not authoritative), the repair-loop outcome (converged or not), and the corpus/replay results.
  3. Tell the user the next steps explicitly: review the contract and invariants by hand, wire the module into the real handler/reducer (v2: dispatch actions[name](data) and read getState(); legacy: call next() — either way call it, don't reimplement the logic inline), then run /polygraph:verify against REAL captured traces after integration to catch drift between this pure model and the glue code around it.

Always state that this is a consistency check, not a proof — the code has been model-checked against its OWN stated invariants, over its OWN declared finite action/data domains, and independently replayed, which is not the same as being correct. The contract and invariants are the model's reading of intent; they need human review.

Read the full file on GitHub · 78 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. yesterday First seen · 78 lines · 43 tokens per session scan A 7bcd1593e788

Subscribe to this mod's changes

polygen is a command published in the GitHub repository cognitive-fab/polygraph (11 stars, last pushed 5d ago), licensed Apache-2.0. It adds 43 tokens to every session and 1,120 once invoked, about $0.0002 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.