Skip to content

test(core): error-path conformance for TLA+ refinement — Err must be a stutter - #1683

Merged
robcohen merged 1 commit into
mainfrom
fix/tla-error-path-conformance
Jul 2, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/tla-error-path-conformance

Conversation

@robcohen

@robcohen robcohen commented Jul 2, 2026

Copy link
Copy Markdown
Member

First of two formal-verification PRs (per the model↔implementation gap analysis after #1677).

The gap this closes

TLA+ actions are atomic — a disabled guard means no transition, so the specs cannot express (and TLC can never catch) "mutate halfway, then fail". That obligation lives at the refinement boundary, and it's exactly where #1677 hid: TLC was green while FIFO/LIFO/HIFO oversells drained lots before erroring.

The conformance layer couldn't see it either — validated line-by-line:

  • prop_conservation_invariant clamped every reduction to min(current) and skipped Err with if let Ok(..) → the buggy state was unreachable.
  • prop_non_negative_inventory generated the precise failing input (current + 1 oversell) but asserted nothing on the Err branch.

Changes

  • prop_conservation_invariant: unclamped amounts (error path reachable), Err ⟹ full-snapshot-unchanged, conservation re-checked after every step.
  • prop_non_negative_inventory: oversell must Err and leave the inventory untouched.
  • New prop_failed_reduce_is_a_stutter: all 7 booking methods × 4 error classes (oversell / wrong currency / unknown label / unmatched cost, + STRICT ambiguity) — Err ⟹ state bit-identical. 300 cases.
  • RUST_MAPPING.md: new Refinement Obligations section — the stutter rule, where each obligation is checked, and generator rules for future conformance tests (violate the guards; never if let Ok-skip).

Would have caught #1677 outright; with #1677's fix on main, all 20 properties pass. clippy -D warnings + fmt clean.

Companion PR: the TLC trace→Rust-test converter (roadmap's "Now" item).

🤖 Generated with Claude Code

…a stutter

TLA+ actions are atomic: a disabled guard means NO transition, so the models
cannot express (and TLC can never catch) partial-mutation-then-error. That
obligation lives at the refinement boundary — and it is exactly where #1677
hid: TLC was green while FIFO/LIFO/HIFO oversells drained lots before
erroring. The then-current conformance layer could not see it either:
prop_conservation_invariant clamped reductions to min(current) and skipped
Err with if-let-Ok; prop_non_negative_inventory generated the precise failing
input but asserted NOTHING on the Err branch.

- prop_conservation_invariant: unclamped reduce amounts (Err now reachable),
  Err asserts a full inventory snapshot unchanged, conservation checked after
  every step.
- prop_non_negative_inventory: oversell must Err AND leave the inventory
  untouched.
- NEW prop_failed_reduce_is_a_stutter: all 7 booking methods x 4 error
  classes (oversell / wrong currency / unknown label / unmatched cost,
  ambiguous under STRICT) — Err implies snapshot-identical state.
- RUST_MAPPING.md: new 'Refinement Obligations' section documenting the
  stutter obligation, where it is checked, and the generator rules for future
  conformance tests (violate the guards; never if-let-Ok-skip Err).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@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!

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 formal-verification conformance checks at the Rust↔TLA+ refinement boundary, ensuring failed Inventory::reduce operations behave like TLA+ stutter steps (no observable state transition) and making error paths reachable in existing invariants.

Changes:

  • Updated conservation + non-negative inventory properties to intentionally exercise failing reductions and assert inventory stutter on Err.
  • Added prop_failed_reduce_is_a_stutter to assert failed reductions do not mutate inventory across booking methods and error classes.
  • Documented the new “Refinement Obligations” (stutter rule) and where it is enforced.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
spec/tla/RUST_MAPPING.md Documents the refinement “Err must be a stutter” obligation and maps each obligation to its Rust test coverage.
crates/rustledger-core/tests/tla_proptest.rs Strengthens existing TLA+ proptests to cover Err paths + adds a new stutter-focused property with an inventory snapshot helper.

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

Comment thread spec/tla/RUST_MAPPING.md Resolved
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 34.7ms ± 0.7ms 23.5 MB
beancount 750ms ± 0.8ms 50.7 MB
Comparison 21.6x faster 2.2x less

vs main branch (2026-07-02)

Metric main this PR Change
Time 39.8ms 34.7ms -12.8% 🟢
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

@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

@robcohen
robcohen merged commit 1459a43 into main Jul 2, 2026
46 of 47 checks passed
@robcohen
robcohen deleted the fix/tla-error-path-conformance branch July 2, 2026 03:53
robcohen added a commit that referenced this pull request Jul 2, 2026
Patch release. User-facing fixes since v0.19.0:

- NONE booking: over-reduction now shorts past zero from ANY balance
  (consumes available units, appends the remainder as a negative position),
  fixing the inconsistency where shorting from a positive balance was
  rejected while shorting from zero was allowed (#1686, via #1688).
- try_reduce: the read-only preview is now reduce-on-a-clone — equivalent to
  the mutating path by construction. Previously it diverged under STRICT
  (FIFO fallback instead of AmbiguousMatch), NONE (error instead of short),
  and {*} merge (wrong dispatch) (#1690).

No WIT change (rustledger:ledger stays 3.2.0); embedders are unaffected.
Also carries the formal-verification hardening arc (#1683-#1697): behavior
replay across 11 corpora, dual-direction trace validation, exact-rational
AVERAGE model, coverage lint, weekly escalated replay, pad-directive spec,
and Kani proofs for the new NONE semantics.

Workspace 0.19.0 -> 0.19.1; packages/mcp-server bumped to match.

Co-authored-by: Claude Opus 4.8 <[email protected]>
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