Skip to content

spec(tla): exact-arithmetic AVERAGE model; replay checks pool value - #1692

Merged
robcohen merged 1 commit into
mainfrom
feat/average-exact-model
Jul 2, 2026
Merged

robcohen merged 1 commit into
mainfrom
feat/average-exact-model

Conversation

@robcohen

@robcohen robcohen commented Jul 2, 2026

Copy link
Copy Markdown
Member

Last item from the FV improvement menu: AVERAGECorrect.tla's integer \div made the pool value model-inexact, so the 1,500-behavior replay could only check units. Now:

Exact rational model

Pool value = valueNum / valueDen (GCD-normalized, opCount-bounded); reduction is exactly value·(t−u)/t. Exactness admits a new invariant the integer model could not state: ZeroUnitsZeroValue — full liquidation empties the pool exactly. TLC: 1,354 states, all six invariants hold.

Value-checked replay

Corpus regenerated: 3,584 behaviors (was 1,500). The replay now asserts pool-value conformance — implementation (28-digit Decimal) vs the model's exact num/den, within a 1e-12 tolerance (~17 orders above accumulated Decimal rounding, ~7 below the smallest genuine discrepancy at these bounds). Units remain exact. All 3,584 behaviors conform — the #1444 AVERAGE-pool fix is now value-verified exhaustively up to the model bound.

Machinery

Generator derive updated for rational state (add-cost recovered exactly from the numerator delta, denominator invariance asserted); lockstep guard covers the new corpus automatically; this PR triggers the TLA workflow, so model-check + corpus lockstep + trace validation all run on it.

Verified: TLC clean · 7/7 replay suites · generator self-test · clippy -D warnings + fmt clean.

🤖 Generated with Claude Code

…s pool VALUE

AVERAGECorrect.tla computed the average cost with integer division (\div),
which made the pool value model-inexact — the behavior replay could only
check the units abstraction. The model now tracks the pool value as an exact
rational valueNum/valueDen (GCD-normalized, opCount-bounded), with reduction
as value' = value * (t - u) / t. Exactness admits an invariant the integer
model could not state: ZeroUnitsZeroValue — full liquidation empties the pool
EXACTLY. TLC checks all six invariants over 1,354 states.

- Corpus regenerated: 3,584 behaviors (was 1,500) covering the exact model's
  full transition graph.
- The replay now asserts pool-value conformance: impl value (28-digit
  Decimal) vs the model's exact num/den, within a 1e-12 tolerance — ~17
  orders above accumulated Decimal rounding and ~7 below the smallest
  genuine conformance discrepancy at these bounds. Units stay exact.
- Generator derive updated for the rational state (add-cost recovered
  exactly from the numerator delta; denominator invariance asserted).

All 3,584 behaviors conform — the #1444 AVERAGE-pool fix is now value-
verified exhaustively up to the model bound, not just units-verified.

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

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

This PR upgrades the AVERAGECorrect TLA+ spec from integer-division modeling to an exact rational (valueNum/valueDen) model so pool-value conformance can be replay-checked (in addition to unit totals), and updates the behavior generation + Rust replay harness accordingly.

Changes:

  • Reworks spec/tla/AVERAGECorrect.tla to track pool value as a normalized rational and adds invariants enabled by exact arithmetic (e.g., ZeroUnitsZeroValue).
  • Updates the TLC config (AVERAGECorrect.cfg) and documentation (RUST_MAPPING.md) to reflect the new invariants and expanded replay corpus.
  • Updates the behavior corpus derivation (scripts/tla-behaviors.py) and the Rust replay test to assert value conformance against the model’s rational state.

Reviewed changes

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

Show a summary per file
File Description
spec/tla/RUST_MAPPING.md Updates the spec coverage table to reflect exact-rational AVERAGE modeling and the larger replay corpus.
spec/tla/AVERAGECorrect.tla Switches AVERAGE pool value tracking to exact rational arithmetic and adds/updates invariants.
spec/tla/AVERAGECorrect.cfg Adds MaxOperations and updates the invariant set to match the new model.
scripts/tla-behaviors.py Adjusts AVERAGE behavior derivation/state schema to emit valueNum/valueDen for replay.
crates/rustledger-core/tests/tla_behavior_replay.rs Extends AVERAGE replay to validate pool value vs model rational (with tolerance).

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

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 34ms ± 5ms 23.8 MB
beancount 887.6ms ± 4.6ms 50.6 MB
Comparison 26.1x faster 2.1x less

vs main branch (2026-07-02)

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

@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

✅ 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 6129fdc into main Jul 2, 2026
47 checks passed
@robcohen
robcohen deleted the feat/average-exact-model branch July 2, 2026 18:27
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