pi-verify

A pi package that folds verification into the agent loop: deterministic project gates that run themselves, and TLA+ tooling for design-level model checking. Project-local gates and jars follow Pi's Project Trust; trusting a project is not a command sandbox.

Install

pi install git:git.rwiesner.com/alex/pi-verify         # from git
pi install https://git.rwiesner.com/alex/pi-verify   # same, via URL
pi install ./path/to/pi-verify                        # from a local path

Not published to npm: the pi-verify name there belongs to an unrelated package. Distribution is via the git sources above.

The TLA+ tools require Java 11+ (the tla2tools.jar ships with the package). Every TLA+ operation probes java -version directly and reports missing, unsupported, or unparseable Java as a setup error; the probe is not cached across operations.

sudo apt-get install default-jre

Then /reload (or restart pi).

What you get

Tools (callable by the agent)

  • tla_check — TLA+ Specification Check with SANY: fast parse and basic semantic checks; re-translates PlusCal algorithms with pcal.trans -nocfg. Outcomes distinguish pass, invalid input, setup errors, translation/parse/semantic failures, timeout/abort, output limit, and unexpected tool failures.
  • tlc — TLA+ Model Check with TLC. Needs a <Spec>.cfg with the finite model (INIT, NEXT, INVARIANTs, PROPERTY, or SPECIFICATION). Reports pass, deadlock, invariant/temporal violation, specification error, model-config error, timeout/abort, output limit, or tool failure, with the trace or diagnostic.
  • run_gates — run the trusted project's gates from .pi/verify.json, with one explicit outcome per gate — pass, exit, timeout, aborted, output-limit, spawn-error, or stream-error — plus output tails. Untrusted projects cannot run project-supplied gates.

Specification Check vs Model Check

tla_check runs SANY. A pass means SANY found no parse or semantic errors; it does not prove that a model satisfies its properties. Parse errors take precedence, semantic errors fail even when SANY exits 0, and PlusCal is re-translated whenever an algorithm is present (-nocfg, so the model config is never mutated).

tlc exhaustively checks the finite model in the .cfg and uses an isolated metadir per run, removed on every exit path, so parallel model checks cannot collide. Every SANY, PlusCal, and TLC JVM invocation also gets its own java.io.tmpdir, created and removed per invocation, because the tools extract jar-embedded standard modules under fixed names; this isolates concurrent invocations rather than changing the upstream tools. Outcomes distinguish deadlocks, invariant violations, temporal violations, specification errors, model-config errors, timeouts/aborts, output limits, and unexpected tool failures instead of treating every Error: as a property violation. Set ignore_deadlock when terminal states are expected. A pass means TLC completed the search and found no error; it is only as meaningful as the model in the config.

Both runners bound each stream at 20 MB. On overflow the JVM path keeps the head of each truncated stream (execFile maxBuffer retention) and reports output-limit evidence from the tail of that capture; gates keep the tail of their streams. The asymmetry is deliberate: JVM diagnostics need the start of the tool's output, while gate noise is usually at the end.

Hooks (deterministic enforcement)

  • Auto-SANY (tool_result): every edit/write of a *.tla file is parsed immediately and the verdict is appended to the tool result, so parse errors land in the agent's context without it asking. A trusted malformed or unreadable configuration is appended as a concise configuration error and SANY is not run; missing toolchain stays silent. Untrusted projects use safe TLA+ settings without reading the project configuration.
  • Gate-before-idle (agent_before_settle): when the agent finishes a run, gates marked "on_settle": true run automatically from the session project root only when a mutation-capable tool has completed successfully since the last completed settle attempt. The dirty set is edit, write, tla_check, tlc, plus the opt-in extension tools codemode, subagent, and subagent_async; error results and other tools do not arm it. A completed attempt consumes the cycle, so gates re-run only after a new mutation; an agent that re-settles without changing anything settles silently. If any gate is red, the failures are injected into the conversation with one requested continuation — the agent must fix before settling; trusted malformed or unreadable configuration also fails closed as a red settle result with no gate execution. Missing configuration, zero On-settle Gates, and untrusted projects are normal skips. The continuation budget is capped at 3; only session_start and runs started by a human (messageType: "user") reset it, so programmatic "custom" runs never grant a fresh budget. An aborted attempt burns no budget, requests no continuation, and leaves the dirty cycle armed; an abort observed at settle entry executes no gates. Bash-driven mutations never arm the cycle — an accepted limitation, since command strings are not inspected; run_gates and CI remain the explicit verification contract.
  • Test/spec deletion guard (tool_call): in-scope bash commands that can delete files are rm, git rm, git clean, and a piped zero-operand rm (… | xargs rm). Recognition happens at command start, after unquoted separators (;, &, |, parentheses and command substitution, backticks, newlines), after sudo (including its flags), after a leading xargs, and inside bash -c/sh -c bodies. Test/spec targets are /-separated segments test, tests, __tests__, spec, or specs, or basenames with a delimited marker (Spec.tla, foo.test.ts, utils_test.py, foo_spec.rb, test-utils.ts; not contest.ts or latest.ts). Quoted operands stay one path and are judged by that content (rm -rf "tests" targets tests; 'tests dir' is a directory named tests dir). TUI mode asks for confirmation before deletion; headless blocks because there is no approver. Uncertainty also confirms: pathless git clean, globs that do not match the vocabulary (rm -rf *, rm -rf *.log), wholesale operands (., ./, ~, .., ../, /), $ expansion (rm -rf $TESTS), xargs-led rm (which always appends upstream paths), and unparseable input. git clean -n/--dry-run is allowed. Not covered: mv, find -delete/-exec, truncate, git checkout --/git reset --hard, rsync --delete, other interpreters (zsh -c, python -c, node -e), deep quoting, cross-segment cd tracking, filesystem state, and command words supplied by expansion ($(echo rm) -rf tests).

Command

  • /verify [filter] — run gates on demand, report a green/red summary. A filter that matches no configured gates is an error, not a green zero-gate result.

Config: .pi/verify.json (session project root)

pi-verify reads .pi/verify.json under the session project root (ctx.cwd, which can differ from the directory that launched pi for resumed or imported sessions). A missing file is valid and receives defaults; only ENOENT means absent configuration. Malformed JSON, invalid shapes, unknown fields, and unreadable files are errors.

{
  "gates": [
    { "name": "typecheck", "command": "tsc --noEmit", "on_settle": true },
    { "name": "lint", "command": "npm run lint", "on_settle": true },
    { "name": "tests", "command": "npm test" },
    { "name": "kani", "command": "cargo kani --output-format terse" }
  ],
  "tla2toolsJar": "vendor/tla2tools.jar",
  "tlcTimeoutMs": 300000
}
  • on_settle: true gates run automatically at agent-settle — they must be fast (seconds).
  • timeoutMs — per-gate timeout in ms (default 120000).
  • tla2toolsJar — path to tla2tools.jar; relative resolves against the project root.
  • tlcTimeoutMs — TLC timeout in ms (default 300000); timeout_sec takes precedence when given and is clamped to 10–900 seconds.

Project Trust

Pi's Project Trust controls whether project-local configuration and resources may be honored; it is not a command sandbox. Gates run as plain shell commands in the project root.

  • Untrusted projects: .pi/verify.json is not read. run_gates, /verify, and settle execute no project gates. TLA+ checks use safe defaults: no configured jar, tlcTimeoutMs 300000, and no project-local jars. Environment, packaged, loose-layout, and user-level jars remain available.
  • Trusted projects: gates and tla2toolsJar from .pi/verify.json are honored, including project-local jars.

Strict validation and defaults

  • The document must be a JSON object. Recognized top-level fields are gates, tla2toolsJar, and tlcTimeoutMs; unknown fields are errors.
  • gates is optional and must be an array when present. An absent or empty array is valid and means no gates.
  • Each gate must be an object with recognized fields name, command, on_settle, and timeoutMs; unknown gate fields are errors.
  • name and command are required non-whitespace strings. on_settle defaults to false and must be boolean when present. timeoutMs defaults to 120000.
  • tla2toolsJar is optional and must be a non-whitespace string when present. tlcTimeoutMs defaults to 300000.
  • All timeout values must be integers from 1 through 2,147,483,647 ms.
  • Validation errors name the configuration file and the precise field, such as gates[0].command or tlcTimeoutMs. One error prevents all gates and TLA+ tools from running against a partially valid document.

Gate selection and failure behavior

  • Every gate result carries one explicit outcome — pass, exit with the real exit code, timeout, aborted, output-limit, spawn-error, or stream-error. Formatting derives from the outcome; a gate that could not run is never reported as a synthetic exit 1 failure.
  • run_gates and /verify with no configured gates report the existing no-gates guidance.
  • A filter matching zero configured gates is an error in both run_gates and /verify.
  • A settle cycle with zero On-settle Gates is a normal skip, as are missing configuration and untrusted projects.
  • Trusted malformed or unreadable configuration at settle consumes the dirty cycle, sets a red status when UI is available, injects an actionable error, and requests one continuation only while the per-prompt cap of 3 allows it and the context is not aborted. No gate executes. A successful mutation-capable tool after the fix re-arms the next settle check.
  • Each POSIX gate runs in its own process group. Timeout, abort, and output overflow terminate that group, including bash-spawned children still in it; /verify forwards the session abort signal and never reports an aborted run green. Output is bounded at 20 MB per stream, so overflow on stdout or stderr is red with an actionable diagnostic even when the command exits 0. Returned tails are bounded and Unicode-safe.
  • A deliberately detached descendant that leaves the gate's process group (for example via setsid) is outside the group-termination guarantee and is not claimed as killed. It cannot delay the result indefinitely because finalization is bounded.

tla2tools.jar resolution order

  1. tla2toolsJar in .pi/verify.json
  2. TLA2_TOOLS_JAR env var
  3. vendor/tla2tools.jar shipped inside this package
  4. loose-layout vendor/tla2tools.jar beside the extension module
  5. ~/.pi/agent/vendor/tla2tools.jar (user-level; survives package updates)
  6. vendor/tla2tools.jar and tools/tla2tools.jar in the project root

Without a jar, tla_check/tlc return setup instructions; everything else still works.

Untrusted projects skip candidates 1 and 6: the configuration is not read and project-local jars are not eligible. Environment, packaged, loose-layout, and user-level jars still work.

Updating the vendored jar

curl -L -o vendor/tla2tools.jar \
  https://github.com/tlaplus/tlaplus/releases/latest/download/tla2tools.jar

Currently vendored: TLA+ Tools 2.0 (2024-08-08). The jar is MIT-licensed (tlaplus/tlaplus).

Design notes

Enforcement belongs in the deterministic layer, never in prose: agents must iterate on red gates before handoff, and deleting tests to get green is the classic agent failure mode — so the hooks enforce both instead of asking nicely. Gates are plain shell commands; TLA+ specs are checked exhaustively by TLC over all interleavings of a small instance, the failure mode example tests cannot enumerate.

Development

Development and CI use Node 24 (the test suites use Node's built-in node:test) and Java 11+ for the JVM smoke tests.

Module map

  • extensions/verify.ts — Pi registration and formatting adapter. The only module that reads Pi context (ctx.cwd, ctx.isProjectTrusted(), ctx.signal), registers tools/hooks/commands, and renders results.
  • extensions/verify/settle.ts — pure Settle Policy module: mutation-capable dirty tracking, reset and re-arm rules, the continuation budget, abort invariants, and the settle decision; no Pi imports or I/O.
  • extensions/verify/config.ts — the single owner of Project Trust, configuration reading, strict validation, defaults, and safe TLA+/gate resolution.
  • extensions/verify/gates.ts — sequential gate selection, process-group execution, timeout/abort/output-overflow termination, per-stream output bounding, and GateResult construction over validated trusted input; it never re-reads configuration.
  • extensions/verify/tla.ts — deep TLA+ toolchain module for SANY, PlusCal, TLC, JVM execution, and jar resolution; it receives resolved safe settings.
npm ci             # clean install from package-lock.json
npm run check      # tsc --noEmit
npm test           # behavioral tests; no Pi runtime or Java required
npm run test:jvm   # real SANY/TLC smoke tests against vendor/tla2tools.jar; requires Java 11+

npm test covers the configuration and gate modules, the TLA+ module, and the Pi adapter with fake JVM adapters. It also drives the full six-entry-point policy matrix — tla_check, tlc, Auto-SANY, run_gates, /verify, and agent_before_settle — across trusted valid, missing, malformed, unreadable, and untrusted configuration. npm run test:jvm copies mutable TLA+/PlusCal fixtures into temporary directories and exercises the production Node/JVM adapter against the vendored jar, including SANY parse/semantic failures, PlusCal re-translation with unchanged .cfg, TLC pass/violation/deadlock behavior, parallel runs with isolated metadirs and per-invocation Java temp dirs, and high-concurrency SANY/TLC stress without false outcomes or temp leaks. Behavioral tests also cover process-group termination, per-stream output limits, abort forwarding, and Unicode-safe output tails.

CI is defined in .gitea/workflows/verify.yml and runs npm ci, npm run check, npm test, and npm run test:jvm on Node 24 with Java 11. Gitea Actions run 8 passed on the feature/verification-deepening integration branch at 3f74c291632d45a7dc69019a1545b121dee0433b. The runner executes as root, so the non-root permission test is skipped there; unreadable configuration is still exercised on CI through the EISDIR case.

This repo dogfoods itself: .pi/verify.json runs the typecheck gate on settle.

Companion skills (kept separate, user-level): verification-doctrine and tla-plus.

S
Description
Verification gates and TLA+ tooling for pi — fold verification into the agent loop
Readme
2.5 MiB
0 Stars 1 Watchers 0 Forks
Languages
TypeScript 99.8%
TLA 0.2%