Skip to content

fix(wasm): run Early validation over the stream as written, so an E3002 on a transaction that books is reported - #3027

Merged
robcohen merged 1 commit into
mainfrom
fix/wasm-early-validation
Nov 26, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/wasm-early-validation

Conversation

@robcohen

@robcohen robcohen commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

What

WASM's single-source surfaces (validateSource, ParsedLedger) and the FFI component's source surfaces (validate, load, batch) load without validation and then validate the load themselves. They ran validation's Early phase over the booked directives. By then booking had filled the elided amounts in, so an E3002 on a transaction that booking still books never showed up on those surfaces. rledger check and bean-check 3.2.3 both report it.

The issue's three inputs, with lines checked against bean-check 3.2.3:

Transaction bean-check / rledger check WASM / FFI before
Assets:B 10 USD / Assets:C HOOL {2 USD} / Assets:C E3002 on the HOOL posting nothing
Assets:A 1 USD / Assets:B 2 EUR / Assets:C / Assets:D E3002 on Assets:D nothing
Assets:A 1 USD / Assets:B USD / Assets:C E3002 on Assets:B nothing

How

The fix reuses the loader's own pipeline (sort → synth-plugins → Early → book → regular-plugins → Late → finalize) and does not rebuild it on the WASM/FFI side:

  • Loader: a new opt-in LoadOptions::retain_pre_booking_directives keeps a copy of the exact stream process() hands to Early (sorted, synth plugins applied, not booked) in Ledger::pre_booking_directives. It defaults to off, so other loads pay nothing.
  • WASM: load_and_book sets the option, and run_validation runs Early over ProcessedLedger::pre_booking. Late still runs over the booked stream minus booking_refused, as before. Both phases now get the same streams the validating loader gives them.
  • FFI: ffi_wasi::helpers::load_source sets the option too. semantic_validation_errors runs Early over LoadResult::pre_booking_directives.
  • The multi-file surfaces (validateMultiFile, Ledger.fromFiles, the FFI *-file surfaces) already validate through the loader and needed no change.
  • A transaction booking refuses still has exactly one E3002. The compat(validate): two amount-less postings in one currency are one error, on the second posting's line #2998 restating-BOOK handling is unchanged.
  • WASM CACHE_VERSION goes from 130 to 131, because the ParsedLedger payload's findings move. The loader cache is unchanged: parsed output did not change.

Breaking

  • Hosts now see an E3002 (and valid: false) where these surfaces used to report nothing.
  • LoadOptions, Ledger and ffi_wasi::helpers::LoadResult each gain a public field, and none of them is #[non_exhaustive]. CHANGELOG has a Breaking entry and a Fixed entry.

How to test

  • cargo test -p rustledger-loader --all-features --test surface_agreement_test: the new SurfaceCase with the three inputs runs on the CLI, loader, WASM, FFI and LSP surfaces.
  • cargo test -p rustledger-ffi-component report_the_shared_findings: fails on main, passes here.
  • cargo test -p rustledger-wasm e3002_on_a_transaction_that_books_is_a_finding: a native test that puts the three inputs beside a refused transaction. It checks that ParsedLedger lists the refused E3002 in parse_errors and the three others as validation findings, and that the validateSource view gives each of the four once. It fails without the fix.
  • cd crates/rustledger-wasm && wasm-pack test --node -- --test wasm_surface_agreement passes.
  • Also run and passing: cargo test -p rustledger-wasm -p rustledger-validate -p rustledger-loader --all-features, the FFI crates, rustledger-lsp, cargo build -p rustledger-wasm --target wasm32-unknown-unknown, and clippy -D warnings on the changed crates.

Closes #3001

🤖 Generated with Claude Code

@robcohen
robcohen requested a review from a team as a code owner November 25, 2026 17:53
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 32.8ms ± 0.3ms 429.0ms ± 1.9ms 13.1x 42.1 MB / 50.8 MB
investment 48.9ms ± 0.7ms 1589.6ms ± 57.9ms 32.5x 47.3 MB / 58.8 MB
fifo 47.7ms ± 0.9ms 8074.9ms ± 124.2ms 169.1x 49.3 MB / 58.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 33.2ms ± 1.6ms 32.8ms ± 0.3ms -1.4% ✅ within noise 43.5 MB → 42.1 MB (-3.3%)
investment 49.2ms ± 0.7ms 48.9ms ± 0.7ms -0.7% ✅ within noise 47.0 MB → 47.3 MB (+0.7%)
fifo 47.2ms ± 0.6ms 47.7ms ± 0.9ms +1.2% ✅ within noise 49.1 MB → 49.3 MB (+0.3%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.7ms, investment 0.5ms, fifo 0.5ms), and is at least 5%.
10,000 transactions per shape · 20 runs, 3 warmup · hyperfine

What do these numbers mean?
  • simple — Two-posting transfers, one currency. The historical workload — keep as the floor, not as the benchmark.
  • investment — Cost specs, per-unit prices and lot reductions. Exercises booking and inventory, which simple leaves at 0.3%.
  • fifo — Bare {} reductions against a FIFO account — how FIFO sells are normally written. The other four shapes reach the ordered-selection path twice in 2k transactions between them, so it was effectively unprofiled until it turned out to be quadratic (plan_ordered scans every lot per reduction: FIFO ledgers are quadratic (8.8s at 20k transactions) #2083).
  • vs base: the PR and the base-branch commit it is built on, built and measured in the same job
  • RSS: peak resident set size, a single sample — reported without a verdict because there is no spread to test it against
  • A shape with no beancount column is one bean-check reports errors on; it is timed for rledger only

@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-loader/src/process.rs 83.33% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 179 masked)
Full AST Match 100%
Total Files 828
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
    The headline is the EFFECTIVE rate, after pairs where beanquery
    itself is wrong are masked. raw is the same run with nothing
    masked. A widening gap means the registry grew, not that rledger
    changed.
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

…02 on a transaction that books is reported (#3001)

WASM validateSource and ParsedLedger, and the FFI component's source
surfaces (validate, load, batch), load without validation and validate
the load themselves. They ran the Early phase over the booked
directives, where booking had already filled elided amounts in, so an
E3002 on a transaction booking still books (an elided posting beside a
cost-bearing one, two fully elided postings beside a second currency,
an elided `Assets:B USD` beside a fully elided posting) was missing
there while rledger check and bean-check report it.

The loader gains LoadOptions::retain_pre_booking_directives, which keeps
the exact stream its Early phase runs on (sorted, synth plugins applied,
unbooked) in Ledger::pre_booking_directives. The WASM and FFI single-
source loads set it and run Early over that stream, Late over the booked
one, as process() does. A refused transaction still has one E3002
(#2998).

Surface-agreement case covering the issue's three inputs (CLI, loader,
WASM, FFI, LSP), plus a native ParsedLedger/validateSource test mixing
them with a refused transaction. WASM cache 130 -> 131; loader cache
unchanged.

Closes #3001

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen force-pushed the fix/wasm-early-validation branch from aacc656 to 845db06 Compare November 26, 2026 00:39
@robcohen
robcohen merged commit 735b8bb into main Nov 26, 2026
67 of 68 checks passed
@robcohen
robcohen deleted the fix/wasm-early-validation branch November 26, 2026 01:00
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.

compat(wasm): an E3002 on a transaction that books fine is missing from WASM's validation, unlike rledger check

1 participant