Skip to content
Stuart Jackson edited this page Nov 22, 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, taken from atlas, and the direction for all SimpleApps development.

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
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). validate.ts, the tests, lint and typecheck cover plugins/*/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) and scripts/check-banned-strings.sh, a shell wrapper around git grep with no decisions of its own.
  • 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).
  • 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).
  • 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 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