Skip to content

test(core): behavior-replay conformance against the TLA+ model (MBT) - #1685

Merged
robcohen merged 2 commits into
mainfrom
feat/tla-behavior-replay
Jul 2, 2026
Merged

robcohen merged 2 commits into
mainfrom
feat/tla-behavior-replay

Conversation

@robcohen

@robcohen robcohen commented Jul 2, 2026

Copy link
Copy Markdown
Member

Third formal-verification PR (after #1683/#1684) — the behavior-replay prototype from the model↔implementation gap analysis.

Idea

The specs' bounds are deliberately tiny — Conservation.cfg yields 207 states / 678 transitions — so TLC can enumerate the complete state graph. Instead of sampling properties, replay every model behavior against the real Inventory and check the correspondence at every step. Exhaustive conformance up to the model bound: strictly stronger than tla_proptest.rs, one notch below a refinement proof.

Pieces

  • scripts/tla-behaviors.py — TLC -dump dot,actionlabels → edge-coverage corpus (one behavior per transition: shortest canonical path + the transition). Output is canonical (content-derived ordering, no node ids) so regeneration is byte-stable under TLC's nondeterministic dump order. Self-tested, incl. a reorder-invariance check.
  • spec/tla/behaviors/Conservation.json — committed corpus: 678 behaviors covering all 678 transitions.
  • tests/tla_behavior_replay.rs — replays everything in plain cargo test (hermetic, no Java), asserting per step: abstraction (units == inventory, accumulators == totalAdded/totalReduced) and enabledness (a model-enabled Reduce may not Err — catches over-strict implementations, the dual of test(core): error-path conformance for TLA+ refinement — Err must be a stutter #1683's stutter obligation). Graceful skip when spec/ isn't packaged (fix(validate): read the spec doc at runtime in the error-code test (fixes Nix channel) #1659 lesson). Runs in 0.01s.
  • tla.yml lockstep guard — regenerates the corpus from the spec in CI and fails on drift, so the replay can never silently certify against a stale model. (This PR triggers the TLA workflow itself, so the guard is verified on this very PR.)
  • RUST_MAPPING.md — documents the layer + how to extend to the booking-method specs next.

Verified

Generator self-test OK; corpus generated from a real TLC run; all 678 behaviors replay identically; clippy -D warnings + fmt clean.

🤖 Generated with Claude Code

The specs bounds are tiny (Conservation: 207 states / 678 transitions), so TLC
can enumerate the COMPLETE state graph. New model-based-testing layer:

- scripts/tla-behaviors.py: TLC -dump dot,actionlabels state graph ->
  edge-coverage behavior corpus (one behavior per transition = shortest
  canonical path to the source state + the transition). Output is canonical
  (content-derived ordering, no node ids), so regeneration is byte-stable
  regardless of TLC dump order. --self-test included.
- spec/tla/behaviors/Conservation.json: the committed corpus (678 behaviors
  covering all 678 transitions).
- tests/tla_behavior_replay.rs: replays EVERY behavior against the real
  Inventory, asserting after each step (a) the abstraction — units ==
  spec inventory, harness accumulators == totalAdded/totalReduced — and
  (b) enabledness: a model-enabled Reduce may not Err. Hermetic (no
  Java/TLC in cargo test); graceful skip when spec/ is not packaged
  (published-crate / crane builds, the #1659 lesson).
- tla.yml: lockstep guard — regenerates the corpus from the spec and fails
  on drift, so the replay test can never silently certify against a stale
  model.
- RUST_MAPPING.md: documents the layer and how to extend it to more specs.

Exhaustive conformance up to the model bound: strictly stronger than the
sampled tla_proptest properties, one notch below a refinement proof.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI review requested due to automatic review settings July 2, 2026 15:12
@robcohen
robcohen requested a review from a team as a code owner July 2, 2026 15:12
@codecov

codecov Bot commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 32.5ms ± 1.1ms 23.7 MB
beancount 875.2ms ± 16.4ms 50.6 MB
Comparison 26.9x faster 2.1x less

vs main branch (2026-07-02)

Metric main this PR Change
Time 39.8ms 32.5ms -18.3% 🟢
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a model-based testing layer that exhaustively replays every TLC transition (within the spec’s configured bounds) against the real rustledger-core::Inventory, keeping the committed behavior corpus in CI lockstep with the TLA+ model.

Changes:

  • Introduces a TLC state-graph → canonical JSON “behavior corpus” generator (scripts/tla-behaviors.py) with a self-test.
  • Adds a Rust integration test that replays the committed corpus (tla_behavior_replay.rs) and checks per-step abstraction + enabledness.
  • Extends the TLA CI workflow to regenerate and byte-compare the corpus to prevent silent drift; documents the approach in RUST_MAPPING.md.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
spec/tla/RUST_MAPPING.md Documents the new behavior-replay (MBT) conformance layer and how it fits with existing verification.
scripts/tla-behaviors.py Generates a canonical edge-coverage behavior corpus from TLC -dump dot,actionlabels output (includes --self-test).
spec/tla/behaviors/Conservation.json Committed corpus for Conservation.tla used by cargo test without requiring Java/TLC.
crates/rustledger-core/tests/tla_behavior_replay.rs Replays every behavior against Inventory and asserts abstraction + enabledness at each step (with graceful skip when spec/ isn’t packaged).
.github/workflows/tla.yml Adds a CI lockstep guard that regenerates the corpus and fails on drift.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…nostic edge parsing

The PR's own lockstep guard caught the exact failure mode it exists for: CI's
tla2tools (1.8.0) emits dot edges the 1.7.4-tuned label regex didn't match, so
CI regenerated a corpus with 207 states but ZERO transitions and the byte-diff
failed. Fixes:

- Edge regex matches any 'id -> id' line; the action label is optional
  decoration. Semantics are DERIVED from the state delta (totalAdded grew ->
  Add, totalReduced grew -> Reduce), so committed corpus and CI regeneration
  agree regardless of TLC's dot dialect.
- The generator now REFUSES to emit a vacuous corpus (states without
  transitions = parse failure, hard error instead of a garbage diff).
- Stutter self-loops (emitted by some TLC versions) are skipped, and
  coverage.transitions counts only semantic edges, keeping the byte-diff
  stable across versions.
- Self-test extended: stripping all action labels must produce the identical
  corpus.

Committed corpus regenerated with the new derivation: byte-identical
(678 behaviors / 678 transitions), confirming the change is purely parsing.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 792
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants