Skip to content

test: compare every diagnostic surface with rledger check on the corpus - #3055

Merged
robcohen merged 3 commits into
mainfrom
test/surface-parity-drift-guard
Nov 27, 2026
Merged

robcohen merged 3 commits into
mainfrom
test/surface-parity-drift-guard

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

A drift guard for the "surface X doesn't do what rledger check does" bug class (#2937, #2946, #2952, #2971, #2857, ...), plus the first divergence it found.

New crate rustledger-surface-parity-tests (tests/surface_parity.rs). Every ledger of a shared corpus goes through every surface, each called natively as Rust:

Surface Entry points
CLI (the reference) rledger check --format json, in-process through cmd::check::run_with_writer
WASM validateSource, ParsedLedger (+ restored from its cache), validateMultiFile, Ledger.fromFiles (+ restored from its cache)
FFI component validate, load, validate-file, load-file; query, query-file
LSP all_diagnostics for a ledger with no includes; the real server through the protocol harness (lsp_protocol/harness.rs) for a ledger with includes, collecting what it publishes for every file

Corpus: tests/compatibility/files, tests/fixtures (parse, booking, validation, plugin, include and option errors, lima-tests), tests/regressions, the loader's and CLI's test ledgers, and the hand-written surface_agreement cases. That is 349 ledgers, 18 of them with includes.

Assertions:

  • Diagnostics are normalized to (file, line, code, message, severity) and must be identical, compared as whole sorted lists, with no substring matching. Normalization is limited to representation: a missing location becomes line 0 (check's <given path>:1 stand-in is told apart by passing the entry as <dir>/./<entry>), and an in-memory <string> is named as the entry file. The LSP's fixed message layout (<message> (<date>) plus context:/hint: lines) is parsed exactly, back into the validator's message.
  • Booked directives of WASM ParsedLedger/Ledger (fresh and from cache) and FFI load/load-file must match the validating load check runs, fingerprinted by kind, date, posting accounts, units and costs.
  • A query answer: FFI query/query-file must give rledger query --format json's rows on every error-free ledger (104 ledgers).

Allowlist. Legitimate differences are listed in ALLOWED / ALLOWED_BOOKING. Each entry names its surfaces, the exact (file, code, line) items it removes, and a reason. An entry that no longer applies fails the test, so a fixed divergence forces its entry out. The entries:

  • The LSP skips WASM and Python plugins (E8006 instead of the plugin's outcome).
  • No disk on the single-source and file-map surfaces: relative plugin paths, documents roots (E7006, discovery), relative document paths.
  • The file map holds only the ledger's directory, so includes that reach above it fail.
  • validate-file confines includes by default. load-file is run with allow-unrestricted-includes and agrees with check.
  • OPEN (see below): load-error codes.

To make the surfaces callable natively. WASM gains a hidden surface_probe module; validateSource/validateMultiFile are split into a Rust core plus the JsValue wrapper, so the probe runs the exports' own bodies. The FFI component gains an rlib target and a hidden surface_probe that re-exports the functions its exports delegate to. Three doc comments were reshaped for the lint that now applies to them.

Fix (separate commit): the LSP now runs document discovery for option "documents" as check does. Before, it built the plugin context only when there was a plugin line, in single-file mode and in multi-file mode, so documents alone never ran discovery. With a plugin line, a single file's source map named /tmp/rustledger-lsp-buffer.beancount, so a relative documents directory resolved under /tmp. Regression test: test_document_discovery_runs_as_check_runs_it.

Why

Each earlier fix in this class came with a hand-written case. This test is the differential half: it covers inputs nobody wrote a case for, so a re-derivation that drifts goes red in the normal test job.

Open divergence (not fixed, needs a decision)

check renders the loader's LoadErrors itself (cmd/check.rs). It uses the codes rledger explain documents (E0001 unreadable include, E0002 include cycle, E0004 duplicate include, E0005 glob without a match), its own wording ("failed to read included file ..."), and its own placement: a duplicate include goes on line 1 of the duplicated file. Every other surface takes these errors from rustledger_loader::process, the canonical renderer, which codes every one LOAD and gives a duplicate include no line. Making them agree means either moving the E000x mapping into process, which changes the wire code for WASM, the FFI and the LSP (LOAD is asserted in LSP and WASM tests and listed in explain), or making check emit LOAD, which drops the documented codes and --exclude-rules E0002. That choice is the maintainer's, so these cases are an allowlist entry marked OPEN, which will fail once either side changes.

How to test

cargo test -p rustledger-surface-parity-tests --all-features -- --nocapture
cargo test -p rustledger-lsp --all-features --lib test_document_discovery_runs_as_check_runs_it

The crate is a workspace member, so CI's cargo nextest run --workspace --all-features runs it. It takes about 6 s warm, and up to about 15-25 s on a first run, when the Python-plugin fixtures compile the plugin runtime. To check that the guard can fail: revert the LSP commit and the test reports doc_discovery_unknown.beancount: lsp disagrees with rledger check.

No cached output changes (the WASM changes are refactors), so there is no cache version bump.

🤖 Generated with Claude Code

robcohen and others added 2 commits November 26, 2026 21:41
…eck does

The LSP built its plugin context only when the ledger had a `plugin`
line, so `option "documents"` alone never ran document discovery (an
implicit synth plugin in the loader's `run_plugins`), in a single file or
in a loaded ledger. With a `plugin` line, a single file's source map named
a stand-in path in /tmp, so a relative documents directory resolved there
rather than next to the file. A document filed under an account no
directive names, which `check` warns about, was missing from the editor.

The plugin context is now built when there are plugins or documents
roots, and a single file's source map names the buffer's own path.

Found by the cross-surface drift guard added in the next commit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A new test crate, rustledger-surface-parity-tests, runs every ledger of
the corpus (tests/compatibility/files, tests/fixtures, tests/regressions,
the loader's and the CLI's test ledgers, and the surface_agreement cases)
through `rledger check --format json` in-process, WASM (validateSource,
ParsedLedger, validateMultiFile, Ledger.fromFiles, the last two also from
their caches), the FFI component (validate, load, validate-file,
load-file) and the LSP (all_diagnostics, and the real server through the
protocol harness for a ledger with includes). The diagnostics, normalized
to (file, line, code, message, severity), must be identical, compared
whole. The booked directives must be those of the validating load check
runs, and the FFI query / query-file must answer as `rledger query`.

Legitimate differences are an allowlist of exact (file, code, line) items,
each entry naming its reason, and an entry that no longer applies fails
the test. One entry is open, not a decision: check codes load errors
E0001-E0005, while the loader's canonical renderer codes them LOAD.

To call each surface natively, WASM gains a hidden `surface_probe` module
whose functions run the exports' bodies (validateSource and
validateMultiFile are split into a Rust core and the JsValue wrapper), and
the FFI component gains an rlib target and a hidden `surface_probe`
re-exporting the functions its exports delegate to.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner November 27, 2026 03:45
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 52.4ms ± 1.0ms 809.8ms ± 13.8ms 15.4x 43.1 MB / 51.1 MB
investment 78.6ms ± 1.5ms 2968.1ms ± 58.5ms 37.8x 47.0 MB / 59.2 MB
fifo 79.5ms ± 1.2ms 15039.5ms ± 178.3ms 189.2x 48.7 MB / 59.1 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 52.4ms ± 1.2ms 52.4ms ± 1.0ms +0.0% ✅ within noise 43.0 MB → 43.1 MB (+0.3%)
investment 78.9ms ± 2.0ms 78.6ms ± 1.5ms -0.4% ✅ within noise 47.3 MB → 47.0 MB (-0.8%)
fifo 80.3ms ± 1.7ms 79.5ms ± 1.2ms -1.0% ✅ within noise 49.1 MB → 48.7 MB (-0.8%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.7ms, investment 1.1ms, fifo 0.9ms), 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

The document discovery test joined "docs/Expenses/Gone/..." as one
string, so on Windows the expected path had forward slashes where the
discovered one has backslashes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@codecov

codecov Bot commented Nov 27, 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

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

@robcohen
robcohen merged commit eb550f5 into main Nov 27, 2026
56 of 57 checks passed
@robcohen
robcohen deleted the test/surface-parity-drift-guard branch November 27, 2026 04:13
robcohen added a commit that referenced this pull request Nov 27, 2026
…rface differences

#3055's corpus includes crates/rustledger/tests, so it now runs the
upstream-check cases. Six hit existing classes (no disk for document roots,
load errors coded LOAD, the LSP's E8006 for an unsupported plugin), and
one is new and OPEN: a check_closing balance is at <check_closing>, 0 in
check and in the ledger file on every other surface.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 27, 2026
…rface differences

#3055's corpus includes crates/rustledger/tests, so it runs the
upstream-check cases. Five hit existing classes (no disk for document roots,
a file map's own words for a load error, the LSP's E8006 for an
unsupported plugin), and one is new and OPEN: a check_closing balance is at
<check_closing>, 0 in check and in the ledger file on every other surface.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 27, 2026
…st drift (#3054)

* test(compat): table every check beancount reports, and guard it against drift

ops/validation.py one function at a time. This does that walk for every
module and leaves a guard behind.

- crates/rustledger/tests/fixtures/upstream-checks/enumerate.py lists every
  check the installed beancount can report, from its source (ast): each
  error-tuple construction (module.function + message template), each raise
  in ops/parser/plugins/loader, each plugin module, each deprecated option.
  126 for beancount 3.2.3, plus one manual row (gpg decryption).
- checks.toml gives each a status: 96 covered, 2 deliberate deviations,
  24 not applicable (dead code, unreachable, beancount tooling), 5 missing.
- Each covered row names a small ledger in cases/. upstream_checks_test runs
  it through rledger check and bean-check and asserts the lines each tool
  reports the row's code/message on, so "covered" means the same lines, or
  the row's note says how they differ. Missing and deviation rows are pinned
  the same way, so a fix cannot leave the table stale.
- The drift guard compares enumerate.py's output with the table's ids, both
  ways, so a beancount upgrade that adds, removes or rewords a check goes
  red and names it. It introspects rather than hashing the scanned files: a
  hash goes red on any docstring edit and does not say which check changed.
- The Python half runs in the python-gated-cargo-tests job (skip marker
  already in its grep); the rledger half runs in every cargo test.
- compatibility.md gains section 20: AVERAGE booking and {*} merging, which
  beancount 3.2.3 refuses and rledger implements (ADR 0007).
- docs/reference/upstream-checks.md documents the table and how to update it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(compat): pin the upstream-check case ledgers in the parse-error parity manifest

The case ledgers are tracked .beancount files, so the parse-error-parity job
compares them. All match but grammar-duplicate-merge ({*, *}, a missing row,
bc-only until the follow-up fix). beancount's deprecation error for
allow_deprecated_none_for_tags_and_links joins NOT_PARSE_ERRORS beside
allow_pipe_separator's: rledger reports option errors when it loads.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(compat): accept E8005 for a Python module name in a build without the Python runtime

The slim release build (no default features) reports a Python module name
as E8005, runtime not compiled in, where the default build reports E8004.
A row's code may now list alternatives (E8004|E8005); each is still an
exact assertion on the line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(surface-parity): admit the upstream-check case ledgers' known surface differences

#3055's corpus includes crates/rustledger/tests, so it runs the
upstream-check cases. Five hit existing classes (no disk for document roots,
a file map's own words for a load error, the LSP's E8006 for an
unsupported plugin), and one is new and OPEN: a check_closing balance is at
<check_closing>, 0 in check and in the ledger file on every other surface.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[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.

1 participant