Skip to content

test(core): booking-family behavior replay + fix NONE shorting (#1686) - #1688

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

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

Conversation

@robcohen

@robcohen robcohen commented Jul 2, 2026

Copy link
Copy Markdown
Member

Replaces #1687 (GitHub closed the stacked PR when #1685's base branch was deleted — branch + commits intact, rebased onto main). Two commits:

1. Extend behavior-replay conformance to the booking-method specs

~3,976 enumerated behaviors across 7 corpora, replayed in <0.1s of plain cargo test:

Corpus Behaviors Replay checks
Conservation 678 units + totals abstraction, enabledness
FIFO / LIFO 422 each per-date totals; matched lot's date == model selection
HIFO 422 per-cost totals; matched lot's cost == model selection
STRICT 136 success/no_match/ambiguous ↔ Ok/NoMatchingLot/AmbiguousMatch
AVERAGE 1,500 units abstraction (model uses integer \div; documented)
NONE 396 balance incl. shorts

Generator: structured TLA value parsing (records/sequences/sets, escaped strings, TLC's pretty-printed multi-segment values — two real dump-dialect quirks found and fixed), SPECS registry with delta-derived actions. Conservation's corpus stays byte-identical. tla.yml lockstep guard loops over all corpora.

First-run adjudications: STRICT's apparent divergence traced to the documented same-cost-interchangeable FIFO fallback in reduce_strict → mapping made faithful (distinct cost per model lot). NONE's divergence was real → …

2. Fix NONE shorting — Closes #1686

reduce_none refused to short from a positive balance while allowing it from zero — the outcome depended on whether zero was crossed in one step (0 → -2 allowed; +1 → -1 rejected). Per NONECorrect.tla and beancount NONE (no booking at all), the reduction always applies: the over-reduction arm now consumes what's available and carries the remainder as a negative simple position.

Instructive detail: two hand-written unit tests faithfully pinned the bug (assert!(matches!(.., InsufficientUnits))) — only the model-derived corpus contradicted the shared assumption. Both flipped with balance assertions; the property test is widened past the holdings range; the full 396-behavior NONE corpus now replays clean.

Verification

7/7 replay suites, core lib 422/422, booking + validate suites green; generator self-tests (incl. reorder + label-stripping invariance); clippy -D warnings + fmt clean.

🤖 Generated with Claude Code

robcohen and others added 2 commits July 2, 2026 15:44
…specs

Generalizes the MBT layer to the full booking family — FIFO/LIFO/HIFO/STRICT/
AVERAGE/NONE — bringing the enumerated conformance corpus to ~3,976 behaviors
(replayed in <0.1s of plain cargo test):

- tla-behaviors.py: structured TLA value parsing (records/sequences/sets,
  escaped strings, TLC's pretty-printed multi-segment values), a SPECS
  registry with per-spec delta-derived actions, canonicalization for the
  richer states. Conservation output stays byte-identical.
- Per-spec replay interpreters: FIFO/LIFO assert the matched lot's DATE and
  per-date totals; HIFO the matched COST; STRICT maps the model's
  success/no_match/ambiguous onto Ok/NoMatchingLot/AmbiguousMatch; AVERAGE
  asserts the units abstraction (the model's average uses integer \div, the
  implementation divides exactly — documented); NONE asserts balance incl.
  shorts.
- tla.yml lockstep guard now loops over ALL committed corpora.

First-run findings, both adjudicated:
- STRICT 'ambiguous' vs impl Ok — a mapping-fidelity issue, not a bug:
  reduce_strict deliberately treats same-cost lots as financially
  interchangeable (documented FIFO fallback), so the replay now gives each
  model lot a distinct cost number to reproduce lot-identity ambiguity.
- NONE: reduce_none refuses to short from a POSITIVE balance while allowing
  it from zero — inconsistent with itself, the model, and beancount NONE.
  Filed as #1686; the replay pins the divergent outcome by issue number and
  asserts the pin FIRES, so a fix flips it loudly.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Closes #1686 — found by the behavior-replay suite's first run over the
NONECorrect corpus. reduce_none returned InsufficientUnits when reducing more
than a POSITIVE balance while allowing the same short from zero/same-sign, so
the outcome depended on whether zero was crossed in one step (0 -> -2 allowed;
+1 -> -1 rejected). NONE performs no booking: per NONECorrect.tla and beancount
NONE semantics the reduction always applies. The over-reduction arm now
consumes everything available and carries the remainder as a negative simple
position.

- The two unit tests that pinned the old behavior are flipped with balance
  assertions (reduce_none_exact_succeeds_over_reduction_shorts,
  test_none_booking_shorts_past_zero).
- prop_none_allows_any_reduction widened past the holdings range with balance
  arithmetic asserted through the short.
- The #1686 pin in the NONE replay is removed: the full 396-behavior corpus
  now replays clean.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner July 2, 2026 15:50
Copilot AI review requested due to automatic review settings July 2, 2026 15:50
@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 32ms ± 0.4ms 23.7 MB
beancount 872.2ms ± 8.9ms 50.5 MB
Comparison 27.3x faster 2.1x less

vs main branch (2026-07-02)

Metric main this PR Change
Time 39.8ms 32ms -19.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

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

Extends the TLA+ model-based “behavior replay” harness to cover the full booking-method family (FIFO/LIFO/HIFO/STRICT/AVERAGE/NONE) and fixes NONE booking semantics so over-reductions short past zero rather than erroring, aligning with the model and beancount behavior.

Changes:

  • Generalize scripts/tla-behaviors.py to parse richer TLC dot dumps and derive canonical, delta-based action steps for multiple specs/corpora.
  • Expand tla_behavior_replay.rs to replay all booking-method corpora with spec-specific assertions (lot selection, STRICT outcomes, AVERAGE units-only caveat, NONE shorts).
  • Fix Inventory::reduce_none to allow shorting from a positive balance, and update unit/property tests and CI corpus lockstep verification accordingly.

Reviewed changes

Copilot reviewed 7 out of 14 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
spec/tla/RUST_MAPPING.md Documents the expanded set of replayed specs/corpora and the abstraction used for comparison.
scripts/tla-behaviors.py Adds structured TLA value parsing + spec registry and derives canonical behavior corpora across booking specs.
crates/rustledger-core/tests/tla_proptest.rs Widens NONE reduction property range and asserts resulting balance arithmetic through shorts.
crates/rustledger-core/tests/tla_behavior_replay.rs Reworks replay harness to load/replay multiple corpora with method-specific checks.
crates/rustledger-core/src/inventory/mod.rs Updates NONE booking unit test to assert shorting behavior instead of InsufficientUnits.
crates/rustledger-core/src/inventory/booking.rs Fixes reduce_none to short on over-reduction; updates corresponding reduction test.
.github/workflows/tla.yml Verifies lockstep regeneration for all behavior corpora rather than only Conservation.

💡 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

✅ 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 e828c78 into main Jul 2, 2026
50 checks passed
@robcohen
robcohen deleted the feat/tla-behavior-replay-booking branch July 2, 2026 16:15
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.

NONE booking: reduce refuses to short from a positive balance but allows it from zero

2 participants