Skip to content
Stuart Jackson edited this page Nov 25, 2026 · 8 revisions

Proofs

Code here is proved, not only tested: every decision is a small TypeScript function annotated with LemmaScript, translated to Dafny, and proved to match a hand-written model of how the marketplace must behave. This is the platform's way of working, led by atlas, and the standard for all SimpleApps development. The augur-build plugin's lemma-driven-design skill teaches the pattern itself, test-driven design with proofs, with LemmaScript and Dafny as the TypeScript tooling (plugins/augur-build/skills/lemma-driven-design/); when a lesson here applies beyond this repo, add it to that skill too.

Sources:

  • LemmaScript (lsc, kept on its latest release, 0.6.11 here; on each upgrade re-check atlas's gap list and run pnpm proofs): TypeScript to Dafny, the //@ annotations, .dfy.gen vs .dfy
  • Dafny reference: the language and verifier (dafny verify, 4.11 here)
  • atlas (clones/atlas): .claude/rules/proofs.md for the rules, models/PROCESS.md for the process, and models/LSC-GAPS.md for where lsc's translation differs from JavaScript. Read the gaps list before using a construct for the first time

The pieces

Piece Path What it is
Shared layer models/Text.dfy The lowest model layer, which every model includes: the list scan ListEnd, the text scans ScanEnd (a case of it) and RunStart, each with its general lemmas (…Is, …Reaches, …Agree, …Ext)
Model models/Marketplace.dfy Hand-written Dafny: what each rule means (CalVer, skill keys and names, the description limit, the token budget, connector URLs), with lemmas about shipped values. Written before the code
Proved code scripts/rules.ts Each rule as a pure function with //@ verify and //@ ensures, listed in LemmaScript-files.txt
Generated Dafny scripts/rules.dfy.gen, scripts/rules.dfy lsc writes .dfy.gen; .dfy is the same plus hand proof steps, additions only
Link models/links/scripts-rules.dfy <function>MatchesModel lemmas: the code decides exactly what the model says
Runtime pin scripts/rules.test.ts Each rule agrees with the regex it replaced on thousands of generated inputs, so the proof is about the code that actually runs
Shell scripts/validate.ts Reads files and reports; calls the proved rules for every decision about a value

A mod's decisions are proved the same way, with the proved file inside the plugin, since an installed plugin has only its own folder. The Work pane has two: models/WorkPane.dfy for the pane (rules plugins/simpleapps-work-pane/scripts/pane-rules.ts, link models/links/work-pane-rules.dfy) and models/WorkData.dfy for the work scripts (work-rules.ts, work-data-rules.dfy). The status line has models/StatusLine.dfy (what /plan-usage <args> answers; rules plugins/simpleapps-statusline/scripts/usage-rules.ts, link models/links/usage-rules.dfy). A skill's decisions live in the skill's own folder, plugins/<plugin>/skills/<skill>/scripts/: the repo skill's cleanup has models/Repo.dfy (which branch to delete, whether to move), rules plugins/augur-build/skills/repo/scripts/cleanup-rules.ts, link models/links/cleanup-rules.dfy, and a JSON-in, JSON-out wrapper cleanup-plan.ts the skill runs (see Skill-Authoring#bundled-files). The proof covers the decisions, not the facts the agent gathers for them. validate.ts, the tests, lint and typecheck cover plugins/*/scripts/ and plugins/*/skills/*/scripts/ as they do scripts/. pnpm proofs verifies every model and link as one Dafny program, so a top-level name in one proved file MUST NOT repeat in another (a plugin's copy of a helper takes its own name).

A regex is a decision, and lsc cannot translate one: write it as a code-unit scanner that makes the same leftmost, greedy match, state the regex in the model, and pin the scanner to the original regex in the tests over thousands of generated inputs (work-rules.test.ts is the example).

lemmascript.json sets JavaScript string semantics (UTF-16 code units), as atlas does, so the proofs count characters the way the code does.

Rules

  • Code is TypeScript. LemmaScript proves only TypeScript, so all code in this repo MUST be TypeScript (.ts, or .tsx in a mod): no Python, and no new JavaScript. Scripts run with Node's type stripping (node scripts/validate.ts), so there is no build step. The only exceptions are tool configs that their tools load as JavaScript (eslint.config.mjs, prettier.config.mjs); scripts/check-banned-strings.sh, a shell wrapper around git grep with no decisions of its own; and the cld launcher (plugins/augur-build/skills/launcher/scripts/cld), a shell command users install that runs claude update and then claude with their arguments.
  • Proofs always come before code (owner, 2026-11-22). A new rule starts in the model (models/<Area>.dfy), then its proved functions and their link, and only then the code that uses them. Code MUST NOT be written ahead of its proof, and proofs MUST NOT be deferred to a later issue.
  • Every decision is proved. File reading, JSON, regex, await, and classes stay in thin unproved shells that only call proved functions (lsc can't translate them; atlas models/LSC-GAPS.md).
  • No escapes. Hand-written Dafny (the model, links, and additions to a generated .dfy) MUST NOT use assume, {:axiom}, {:verify false}, {:trusted}, or expect. scripts/validate.ts fails one.
  • Tests still pin the runtime at 100% coverage: a proof is about the Dafny translation, and a test shows JavaScript agrees.
  • No recursion per element (GH #19). A proved function MUST NOT call itself once per character, list item, line or edge of its input: JavaScript has no tail calls, and V8 overflows its stack at about 10,000 frames, which a long issue body reaches. Dafny proves termination, not stack depth. Write the walk as a while loop marked //@ pure, with //@ invariant bounds and //@ decreases; lsc emits it as a Dafny function f(...) { f_fun_spec(...) } by method { <the loop> }. Then, in the .dfy (additions only), give f_fun_spec the recursive definition (the body a recursive f would have had, calling f_fun_spec), its decreases, and any ensures, and add the loop the invariant that ties it to the spec: f_fun_spec(s, j) == f_fun_spec(s, i), or acc + f_fun_spec(s, j) == f_fun_spec(s, i) when the loop builds a result. Callers and links keep calling f, which is its spec. Bounded recursion (a fixed depth such as digitsAt's 2 or 4) is fine but written as a loop too, so the rule has no exceptions. Test each walk at 100,000 elements (tests/work-pane/long-inputs.test.ts).
  • General before specific. Before writing a second proof of the same shape, make it one proof over a parameter and reuse it (augur-build skill lemma-driven-design, references/writing-the-model.md "General before specific"). Here, every scan and list search is one of three general loops, proved once per shipped file (scanEnd in rules.ts; runEnd, runStart and listEnd in work-rules.ts; their pane… copies in pane-rules.ts) and linked once to Text.dfy. A named scan (digitRunEnd, refIndex, holdsFrom, …) is one call with its own test, and its link is two lemma calls with no induction. The code's lambda ((r: Ref) => r.id !== id) is a different function value from the model's, even with the same body, so a link states the general loop's lemma for every test (ensures forall ok :: runEnd(s, i, ok) == Text.ScanEnd(s, i, ok)) and meets the two tests with Text.ScanEndExt, RunStartExt or ListEndExt. ListEndExt also takes two lists, which links code records to the model's (refs and AsRefs(refs)).
  • After changing a proved .ts: pnpm exec lsc regen --backend=dafny scripts/<file>.ts, then re-add any hand proof steps.

lsc's translation, as found here (0.6.11; check again on each upgrade):

  • An //@ ensures becomes a separate <f>_ensures lemma, so callers never see it. When a caller needs a bound (a run's end, an index), copy the lemma's ensures lines into the function's own header in the .dfy (an addition); Dafny then proves it by the function's induction and every caller gets it.
  • On a //@ pure loop (function-by-method) lsc drops the //@ ensures entirely: no lemma, nothing checked. Put those ensures on f_fun_spec in the .dfy; keep them in the .ts as the contract a reader sees.
  • lsc gen rewrites .dfy.gen but leaves an existing .dfy alone: after a change to a proved .ts, rebuild the .dfy from the new .dfy.gen and re-add the additions (git diff --no-index <file>.dfy.gen <file>.dfy lists them before the change).
  • A function-typed parameter (ok: (c: number) => boolean) becomes ok: (int) -> bool, and a named function or a lambda can be passed for it, so one proved loop can serve many rules. A lambda passed to a generic function (listEnd<T>) MUST type its parameter ((r: Ref) => …): untyped, lsc types it T, which Dafny rejects as unbound.
  • A wrapper calling a general loop sees the loop's bound only if the loop's own header says it: add ensures i <= f(s, i, ok) <= |s| to the function-by-method itself (an addition), not only to f_fun_spec. Without it, a caller two calls up (closesBefore through spaceStartBack) timed out.
  • arr.push(x) on a local becomes arr := arr + [x]: build a loop's list with it (linear in JavaScript), not [...arr, x] (a copy each time).
  • toLowerCase translates to an {:axiom} function: an escape in generated code, which validate.ts does not see. Lower ASCII with a proved function instead (lower in work-rules.ts).
  • const x = arr[k] becomes an Option; read fields in place, and write arr[k]! where TypeScript's strict index checks need it (lsc keeps the bound as a proof obligation).
  • A parameter named after a Dafny keyword (assigned, to) breaks the generated file or its checks: rename it.
  • A branch no input reaches still counts against 100% branch coverage: restructure it away (a requires or an ensures that rules it out) rather than leaving it to the tests.

Running

Command Does
pnpm proofs lsc check (regenerate, check additions-only, verify the code), then dafny verify the model and links
pnpm quality Includes the escape check and the runtime pins

pnpm proofs MUST pass before /submit (Deployment#submit). Like atlas, it is not in CI or the hooks: it needs Dafny installed locally (brew install dafny).

Not yet proved

  • The status line mod's other decisions (meter colours, cache ages, wrapping): next, by pulling them out of hooks/register.tsx into proved functions (Mods).
  • scripts/check-banned-strings.sh: shell; git grep does the matching.
  • The Work pane's shells (hooks/register.tsx, scripts/lib/) keep what is plumbing rather than a rule: Map/Set lookups, defaults for JSON fields that are missing (??), file framing (the --- lines, paths), message and Markdown templates, and matching the connector's "no connected MCP tool" error text. Every rule about a value they read is proved.

Clone this wiki locally