AGENT SKILL

tsverify

Prove TypeScript functions with tsverify: write a spec's laws before the code, run the verifier while you write, and read its verdicts. Load it before you write or change a spec (`spec`, `contract`, `Implementation<S>`, `assume`), and before you change a function a spec proves.

License 许可
MIT
所属仓库
emyann/nemia
最近更新
2026-11-15
Stars
★ 3
Forks
0

01 前言 Front Matter

name
tsverify
description
Prove TypeScript functions with tsverify: write a spec's laws before the code, run the verifier while you write, and read its verdicts. Load it before you write or change a spec (`spec`, `contract`, `Implementation<S>`, `assume`), and before you change a function a spec proves.

03 SKILL.md 正文

tsverify

tsverify proves that a TypeScript function keeps the laws of its spec for every input its spec allows, or gives the input that breaks one. It reads the spec and the code as they are, and asks the Z3 solver law by law. The pages behind this skill are installed with the package, in node_modules/@emyann/tsverify/docs/: specs.md (writing a spec), verdicts.md (reading a report), reads.md (what it reads, refuses and trusts), effect.md and proofs.md (loops). Read the one you need before you guess.

The loop: the laws first

  1. Say what must hold, one law per fact, in the words of the domain the function serves: "a refused withdrawal writes nothing", never the steps the code takes. A law that restates the code proves only that the code is the code.
  2. Write the laws into the spec before you change the code, and run the verifier: each new law should come back counterexample on the code as it is. A pure function's laws are first run on 1,000 generated inputs, so a law the code breaks there comes back in milliseconds, its e.g. line ending "found by running": the real function on that input. The run loads the code's modules in a child process, so their top-level code runs: for code whose modules must not be loaded, pass --no-run, and Z3 alone answers. A new law the old code already proves says nothing your change adds; keep it only if it says something the spec lacked.
  3. Change the code until every law is proved. A counterexample is an input the real code fails on: run the function on it before you decide whether the code, the law or the requires is wrong.

Writing a law

// shipping.contract.ts
import { type Implementation, spec } from "@emyann/tsverify"

const shippingSpec = spec<(total: number) => number>({
  requires: ({ input: [total] }) => total >= 0,
  ensures: {
    freeFrom50: {
      description: "An order of 50 or more ships free",
      condition: ({ input: [total], output }) => (total >= 50 ? output === 0 : true),
    },
    smallOrdersPay: ({ input: [total], output }) => (total < 50 ? output === 5 : true),
  },
})
// The function's type, which the code takes: it then imports nothing of tsverify.
export type Shipping = Implementation<typeof shippingSpec>

// shipping.ts
import type { Shipping } from "./shipping.contract.ts"
export const shipping: Shipping = (total) => (total >= 50 ? 0 : 5)
  • The spec's module exports the function's type, Implementation<typeof shippingSpec>, under the function's name: the code takes it and imports nothing of tsverify, and the verifier finds the code through that import.
  • The spec's module imports nothing from the code that implements it. Declare the types its signature needs in the spec's module and let the code take them from it; a value both need (an error class, a service) goes in a module of its own that both import. A spec that imports its implementation's types cannot be written, or type-checked, before the code exists.
  • A law reads one run: ({ input: [a, , c], output }) => boolean, the arguments as a tuple (a hole for one it does not read). A law of async code or an Effect program reads its outcome as output and the calls it made as calls.
  • Give each law its sentence, { description, condition }: the report prints the description on the law's line, so write it as a statement a reader can check against the domain.
  • requires is what every caller promises, and the laws are proved only where it holds. Read the function's real callers: an input they pass that requires rules out is an input nothing proves. A default parameter is proved only as requires lets it vary.
  • Say both sides. Most laws forbid ("refused when …"); a function that refuses everything keeps every such law. Say when it succeeds too: a law of the form "exactly when", or one for the accepting side. The verifier asks it of every spec: where two results of different kinds, a refusal and none, or two strings of a union, both keep every law at some input, the contract is undecided. A choice the code makes on purpose is named in the spec, open: ["output.reason"], or open: true for the whole result.
  • Name the domain's words once, as small pure helpers beside the spec, and read them in the laws. A law may call a function of the code that has its own spec: it then says that the two agree, and leans on that spec's proof. A law that calls code with no spec leans on code nothing states.
  • A law speaks of one run. "Calling it twice gives the same answer", "this field changes nothing" are about two runs: a test holds them, not a law.
  • vacuous means the law says nothing: no result could break it, usually because its premise never holds under requires. Rewrite it; never keep one.

Calls, and what is taken on trust

  • A call of a function typed Implementation<S> takes what S proves of it, where S's requires holds at the call, and nothing of its body. What a caller needs of it is a law of S. When a counterexample "leans on S for f", either the caller does not meet S's requires there, or S lacks the law: meet the one or add the other.
  • assume(f, { pure, ensures }) declares a function with no spec that the verifier does not or should not read: a package's, a recursive one, one too large to read at every call. Its ensures are trusted, never proved: test them on the real function with brokenOn(assumption, inputs). pure: true with no laws makes it some pure function: what is proved then holds whatever it returns. An assume of a function that implements a spec is refused: its spec declares it.
  • Every report lists what it leaned on: the declarations it trusted, the specs it took, and what it assumed of JavaScript (arguments have their types, code throws only where it says so, a purity check trusts that a getter changes nothing). Read that list before you call a law proved.

Reading a verdict

Verdict What to do
proved Nothing, for every input requires allows.
counterexample Run the function on e.g.'s input; the where line says what each call returned. Fix the code, the law or requires.
unmeetable No code could keep the law at e.g.'s input: the laws it names leave no result together there. Change the spec, not the code: loosen one of them, or narrow requires.
vacuous The law says nothing of the function: rewrite it.
undecided Of the contract: at e.g.'s input, both results it names keep every law. Add the law that picks one; where the choice is the code's on purpose, say open: ["<the part>"] in the spec, or open: true.
bounded No proof, and no counterexample with arrays of up to 4 elements. The reason says what stands in the way, often a loop's invariant (docs/proofs.md).
unsupported The code or the law uses what tsverify does not read. The reason says what to write instead.
unknown The solver gave no answer: split the law, or narrow what it ranges over.
inconsistent A bug of tsverify, not a verdict on your code.

The command exits 0 only when every law is proved and no contract is undecided.

Running it

  • One contract: npx tsverify <file> <name>, the file the function name is written in, typed by its spec's type (or the one that exports contract(...)), never the spec's own module, which holds no contract to run. Every contract of a file: npx tsverify <file>. Of a folder: npx tsverify <folder>. --verbose prints every law and what each leaned on; --json is the same for a program.
  • While you write: npx tsverify <file> --watch --cache runs again on every change to the project's TypeScript, each run a process of its own, and stops a run a newer change makes stale. Run it in the background, edit, then read the newest report: piped, each report follows the last under a line tsverify --watch · <time> · <why>, and ends with all proved or not all proved. Watch the file you are working in, not the whole project, so each run stays short. Stop it, by the process you started, before you finish: it never ends on its own.
  • --cache keeps each contract's verdict, keyed by the text of every file its program reads: a run after a change verifies only the contracts that change reaches.
  • Several roots: tsverify.config.json ({ "roots": [...], "workers": 2 }) lists what npx tsverify with no root verifies, each through its own tsconfig.json; --workers N verifies files in N processes, the same report as one.

When a run is slow

  • Find the law first, then its question. The one-contract form and --verbose print each law's time. The time is Z3's, not the reading of the TypeScript, and a few laws hold most of it. --json says which question took it: each law's asked sums its queries by question (proof, says-anything, search, unmeetable), by side, by what answered (by: run or z3) and by how they ended (answered, out of work, out of time, gave up), and each contract's asked its own (cross-check, cover, undecided, obligation).
  • A proved law asks more than its proof. It also asks whether any result could break it (the vacuous check), a question of the spec alone: a run of generated inputs and results answers it first, else Z3, at a tenth of --work. One it cannot settle in that leaves the law proved, with a note under it.
  • --work N is each query's budget, in Z3's own steps, 15,000,000 by default. A query that runs out spends all of it, from seconds to minutes depending on what it asks, and says "out of work"; "out of time" is a query stopped at 5 minutes, which never counted its work.
  • While you write, lower it: --work 3000000 took one contract from 5 minutes to 40 seconds, every law still proved. A law proved at a lower budget is proved for every input requires allows.
  • In CI, lower it too, and read the notes. What a lower budget loses is the checks around a proof: whether the law says anything, whether the exact search side agrees with it, whether a counterexample's input leaves any result, whether the laws decide every input. Each that ends without an answer is a note under its law or contract, counted on the contract's line and in the totals (totals.unanswered in --json), and the exit code does not change. Where a note matters to you, verify that contract again at the default.
  • --bound N is the longest array the search side tries, 4 by default; maps and loops grow fast with it, so lower it while you write a law over them. A run that exits 3 holding about 2 GB has run Z3 out of memory: lower --bound and --work, or verify that contract alone.
  • --workers and --cache shorten a folder's run, not one contract's: --workers splits the files, never the laws of one contract, and --cache skips only the contracts no change reaches.

What a proof holds, and what it does not

  • A proof covers every input requires allows, and only what the laws say. It says nothing of a message's words, the wiring around the function, another function, a property of two runs, the exact output where the laws only bound it, or whether what an assume declares is true: a test holds those.
  • To see whether a law catches a bug, plant it: change the function as the bug would, run the verifier, see the law come back counterexample, and restore the function. A law that reads as covering a case may leave it open. @emyann/tsverify/mutants plants one-line bugs in bulk and says which the laws miss (docs/specs.md, Gaps).
  • A law loosened or removed so the code passes is a check weakened, as a loosened test is: it needs the same reason.

Traps

  • The project's tsconfig.json must have strict (or strictNullChecks): without it a type leaves out null and undefined, and so would every proof.
  • A read of xs[i] or of a record by any key is read where its type says it may be missing: noUncheckedIndexedAccess on, or xs.at(i).
  • number is IEEE 754: a sum may round, and the verifier proves where it does not (exact obligations). bigint never rounds.
  • A loop needs an invariant to be proved for every length: the verifier guesses some, and you write one where its guess is not enough (docs/proofs.md).

04 同仓库更多 Skills

emyann
emyann/nemia

groom-the-inbox

Read each card in play in the inbox against the repository and say, per card, done (the commit on the base that landed it), replaced (the decision or card that did), stale (each fact that moved, what it said and what it says now) or standing, reading closely only what the period touched. For the janitor's step, which proposes and never acts: a person removes or amends each card by the command the check writes.

★ 3
emyann
emyann/nemia

write-a-spec

Turn an accepted intent into a spec an engineer can build from in this repository: the goal restated, the approach and why, every path it touches, the steps, the tests that prove it, the risks, and what is still open. For a step that designs a change before anyone builds it.

★ 3
emyann
emyann/nemia

review-a-change

Review a change in this repository as a second engineer would: against its intent and spec, by reading the diff and the suite's verdict, with findings that name the file, the line and what goes wrong. For a read-only step that decides whether the work goes round again.

★ 3
emyann
emyann/nemia

read-the-stats

Read a factory's stats since the janitor's last run, against the same length of time before it, write a short report, and propose one correction per card — an allow or deny by name, a line in an agent's body or a skill, or a refusal kept on purpose — each with the numbers behind it, never one already in the inbox. For the janitor's step, which proposes and never acts.

★ 3
emyann
emyann/nemia

land-a-branch

Land a run's result on the base branch in this repository's own checkout, the way the step says it lands: make that one act, read its result, and write the message that records what landed — or change nothing and say why it refused. For a gated ending step (workspace: base).

★ 3
emyann
emyann/nemia

implement-a-change

Build a change from its spec in this repository: tests first where a rule is claimed, small commits, the tests of what changed green for the right reason, and each review finding answered in the next round. For a step that writes the code.

★ 3