Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Proofs and laws

Tests check the cases someone thought of. A proof checks every case. Troupekit keeps a small set of proofs, written in Bend, next to the code under proofs/, and CI checks them on every pull request. This page says exactly what they prove, because a proof is only worth as much as the claim it makes.

The run state machine

An agent run moves through the states CallModel, CallTools, Check, Repair, Done, Failed and Paused, one step at a time. AgentRun.bend models those states, the model-call count, the turn limit, and every answer the outside world can give: text, tool calls, a paused turn, a fatal error, a retry, a budget pause, a passed check and a failed check. The law, from LAWS.bend:

law step_keeps_model_calls_within_max_turns:
  for r: A.Run
  for a: A.Answer
  for ok: {Nat.is_le(A.Run.model_calls(r), A.Run.max_turns(r)) == True{} : Bool}
  {Nat.is_le(A.Run.model_calls(A.step(r, a)), A.Run.max_turns(A.step(r, a))) == True{} : Bool}

In words: for every run and every answer, if the run has made no more model calls than its turn limit before a step, it has made no more after it. A new run starts at zero calls, so no reachable run ever makes more model calls than max_turns allows. PROOF.bend proves it for every state and every answer. When the guard is removed from the model, the proof fails, so it does test something.

The route planner

The route proof covers the rule that keeps private and offline agents on the device. Route.bend models the plan as a list of targets, each on the device or not, eligible or not. Two laws are proven in route/PROOF.bend:

law private_plan_has_only_on_device_targets:
  for ts: R.Targets
  for offline: Bool
  {R.all_on_device(R.decide(ts, offline, True{})) == True{} : Bool}

law offline_plan_has_no_remote_targets:
  for ts: R.Targets
  for private: Bool
  {R.all_on_device(R.decide(ts, True{}, private)) == True{} : Bool}

For every list of targets, a private plan holds only on-device targets, and so does an offline plan.

What the proofs do not prove

  • They prove a model, not the Rust code. The Bend files are written by hand to follow run.rs and the route planner. A change to the Rust code, such as a new state or a moved guard, does not break the proof. Closing that gap, with a Rust test that drives the real run through every answer and compares each transition with the Bend model, is open work.
  • CI runs bend PROOF.bend without --verdict. The proofs job installs Bend 2.0.40 by checksum and requires the line ALL PROOFS CHECK. Bend itself says to use --verdict for mathematical validity, which rechecks the proof in a Lean kernel. That recheck passed once by hand when the first proof was written, but CI does not run it, because the runner has no Lean toolchain. It is an open backlog item.
  • The index laws are stated, not proven. proofs/index/LAWS.bend states three laws about search: top-k returns min(k, n) results, in sorted order, and reciprocal rank fusion ignores the order of its input lists. There is no proof file for them yet, and CI does not check them.
  • Nothing here proves what a model says. Structure and bounds can be proven; the content of an LLM's answer cannot.

Laws on your agents

Agents and graphs take law files today: .laws(path), .law(text) and .auto_laws(bool) on the builder, and the same keys on #[agent]. In 0.4 they are stored and validated, not enforced. build checks that each law file exists, is a .bend file of at most 1 MiB of UTF-8 with no .. in its path, and that inline laws are not blank and at most 64 KiB (laws.rs). No run proves them. The builder docs say it plainly: "0.2 stores laws; no run proves them yet."

Until laws are enforced, the 0.4 bindings refuse explicit laws at build with an Unsupported error, so a Swift, Kotlin or Python caller cannot believe a law is checked when it is not. troupekit verify, which turns a TOML workflow into a Bend spec and proves it, is planned for 0.7, together with laws generated from an agent's TOML: turn limit, budget, approval gates, tool allowlist and the private route rule.

Proof