Skip to content

fix(wasm)!: give a validation finding the hint rledger check gives it - #2973

Merged
robcohen merged 4 commits into
mainfrom
fix/wasm-hint-canonical
Nov 13, 2026
Merged

robcohen merged 4 commits into
mainfrom
fix/wasm-hint-canonical

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

WASM's validateSource and ParsedLedger now convert a validation finding through the loader's conversion, which rledger check uses, instead of a rule of their own.

  • The loader's ValidationError -> LedgerError conversion (inline in ledger_errors_extend) is now public as rustledger_loader::validation_error_to_ledger_error. process still converts every finding through it, so rledger check output does not change.
  • validation_error_to_wasm (crates/rustledger-wasm/src/helpers.rs) takes the message, code, phase, hint and severity from that function. It still places the finding by the single-source rule (file id 0 via SourceMap::locate, else the date-to-line fallback), so locations do not change.

Why

validation_error_to_wasm set hint to the finding's hint, else its note, else its context, and left the note out of the message. So a failing balance (E2001) carried hint: "difference: 1, tolerance: 0", which rledger check, the multi-file WASM surfaces and the FFI do not give. The loader's rule is the canonical one:

  • hint is the suggested fix only;
  • the note is folded into the message (<message>\n note: <note>);
  • the context is not reported.

Calling the loader's function means the rule exists in one place only, per the canonical-function discipline in AGENTS.md. A side effect: E1005 is now phase: "parse" on these surfaces, as check reports it.

This is a breaking change to WASM output (see the CHANGELOG ### Breaking entry). WASM cache v111, since a ParsedLedger archives its findings. v110 is held by #2931. The loader cache is unchanged at v73.

Drift guard

test_validation_findings_match_the_loaders (in parse_error_mapping_parity, crates/rustledger-wasm/src/helpers.rs):

  • End to end: compares validateSource's findings with the loader's LedgerErrors (load_with_fs, as rledger check loads) on every field: message, code, phase, hint, file, line, column, end line, end column and severity. It runs on two ledgers: the E2001 ledger from the issue, and one with several findings after a multi-byte character. On the issue's ledger it also asserts the exact loader row (hint: None).
  • Unit level: builds one finding that carries a note, a context and a hint, and runs it through both conversions. It asserts that they agree and gives the exact result: the note is in the message, the hint is the hint, and the context appears nowhere. No single-source ledger can produce a note or a hint:
    • The validator attaches a note only to a directive that a plugin synthesized with no location at all. Every plugin a single source can run gives its directives a file, even if only a placeholder.
    • E8001's hint needs the documents roots resolved against a directory, and a single source has no directory.

Other surfaces with their own ValidationError mapping (out of scope)

  • LSP (validation_error_to_diagnostic, crates/rustledger-lsp/src/handlers/diagnostics.rs): builds its own message. It appends (date), a context: line and a hint: line, and drops the note. That is a separate presentation for an editor, not the wire hint field. Changing it would change LSP output.
  • FFI component (semantic_validation_errors, crates/rustledger-ffi-component/src/convert.rs): sends the bare err.message. The note is not folded in, and the WIT error record has no hint field. A note never reaches the FFI, while check prints it in the message.
  • WASM single-source location of a finding on a plugin's placeholder file (for example a check_closing balance at <check_closing>): the loader locates it at <check_closing>:0:0, and validateSource gives no line. This is a location rule, separate from the hint/note/context rule fixed here.
  • WASM single-source E8001 hint: run_validation does not resolve documents roots, so validateSource never gives the documents-folder hint that check and the multi-file surfaces give.

How to test

cargo test -p rustledger-wasm --all-features --lib parse_error_mapping_parity
cargo test -p rustledger-wasm --all-features
cargo test -p rustledger-loader --all-features

By hand: run validateSource on the ledger from the issue. E2001 now has hint: null, matching rledger check --format json and validateMultiFile.

Closes #2972

🤖 Generated with Claude Code

validateSource and ParsedLedger mapped a ValidationError through their
own rule in validation_error_to_wasm: the hint fell back from the
finding's hint to its note to its context, and the note stayed out of
the message. So a failing balance (E2001) carried the hint
"difference: 1, tolerance: 0", which rledger check, the multi-file WASM
surfaces and the FFI do not give.

The loader's conversion, which check uses, is now public as
rustledger_loader::validation_error_to_ledger_error (process's
ledger_errors_extend calls it), and validation_error_to_wasm takes the
message, code, phase, hint and severity from it, placing the finding by
its own single-source rule as before. A finding's hint is its suggested
fix only, its note is folded into the message, its context is not
reported, and E1005 is phase "parse", as check reports them. rledger
check's output does not change.

test_validation_findings_match_the_loaders pins validateSource to the
loader field by field (message, code, phase, hint, file, line, column,
end, severity) on a failing balance and a ledger with several findings,
and one finding carrying a note, a context and a hint through both
conversions. WASM cache v111 (v110 is held by #2931), since a
ParsedLedger archives its findings; the loader cache stays at v73.

Closes #2972

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

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 41.9ms ± 1.9ms 604.3ms ± 3.2ms 14.4x 45.0 MB / 50.9 MB
investment 67.9ms ± 2.3ms 2217.1ms ± 66.3ms 32.6x 47.1 MB / 59.1 MB
fifo 63.1ms ± 1.9ms 12425.4ms ± 657.0ms 196.8x 49.0 MB / 59.0 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 41.4ms ± 0.5ms 41.9ms ± 1.9ms +1.2% ✅ within noise 45.0 MB → 45.0 MB (+0.1%)
investment 65.2ms ± 2.5ms 67.9ms ± 2.3ms +4.1% ✅ within noise 46.1 MB → 47.1 MB (+2.0%)
fifo 63.7ms ± 1.7ms 63.1ms ± 1.9ms -0.9% ✅ within noise 49.0 MB → 49.0 MB (+0.0%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.9ms, investment 1.5ms, fifo 1.1ms), 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 13, 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 826
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 and others added 3 commits November 13, 2026 06:03
Review of #2973:

- test_validation_findings_match_the_loaders gains an invalid-root
  ledger (E1005), so validateSource is compared with the loader on a
  parse-phase code, phase included. Forcing phase "validate" in
  validation_error_to_wasm now fails it.
- rustledger-loader tests validation_error_to_ledger_error directly:
  the note folded into the message, the context dropped, the hint kept,
  and a parse-phase code given phase "parse".
- The CHANGELOG breaking entry names every code whose context was a
  hint on validateSource: E2001, E2002, E2003, E8001 and E1005.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…hints validateSource drops

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen merged commit 27e8bce into main Nov 13, 2026
67 of 68 checks passed
@robcohen
robcohen deleted the fix/wasm-hint-canonical branch November 13, 2026 18:22
robcohen added a commit that referenced this pull request Nov 14, 2026
…r it, as beancount does (#2974)

* fix(parser): read a mid-line `#!` or `#+` as a `#` and the token after it, as beancount does

A `#!` or `#+` that does not start its line was one comment-like token
(`Shebang` / `EmacsDirective`) running to the end of the line. beancount
skips a `#` line only at column 0 (`^[\*\:\#]/.`); anywhere else it lexes
a `#` (HASH), then a `!` (FLAG) or a `+` (PLUS), and the rest of the line
as usual. So every invalid token after a mid-line `#!` went unreported
(`2024-01-02 * "x" #!cd` is two errors in bean-check 3.2.3, one here),
and an indented `#+STARTUP` or `#!` line, under a directive or a posting,
was skipped as a comment where beancount reports it.

- Lexer: a `Shebang` / `EmacsDirective` token not at column 0 is rewritten
  to `Hash`, and the lexer resumes right after the `#`, as the mid-line `%`
  rewrite does. `error_later_in_run` lexes on past a `#` the same way, so
  a lone capital glued to `#!x` still makes the run one invalid token.
- CST recovery: the mid-line SHEBANG / EMACS_DIRECTIVE special cases are
  gone (the transaction header's `#!` defect, `indented_comment_check`, the
  two-character defect range), since those tokens now occur at column 0
  only. A header defect at a `#` keeps the "only `;` starts a comment in
  the middle of a line" hint.

Parse-error parity (pinned fuzz corpus 9bc79f4): 78,500 of 111,323
inputs match beancount, from 78,478: 71 newly match, 49 regress. 48 of
the 49 count each differing line as beancount does when the line stands
alone (beancount's recovery after an earlier error line, #2872), and the
other is a lone capital glued to tokens that all lex (`X!!#+++02`).
`tracked` and `compat` are unchanged. The BOM combination matrix keeps its
beancount and main columns; its generated lists are updated with
`BOM_MATRIX_UPDATE=1 BOM_MATRIX_RELABEL=1`, the hand-kept worse-than-main
list drops 341 lines no longer further from beancount than main and gains
51 that a hidden `#!` / `#+` used to mask, and a line main counted closer
only because it did not read past a mid-line `#!` / `#+` is verified as
such (`main_read_less`).

Caches: loader v74 and WASM v112. A ledger with an indented `#+` line
loaded with no error before, so an error-free v73 loader blob of it is
stale too. (WASM v111 is held by #2973.)

Refs #2928

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

* fix(parser): name a mid-line `#!` or `#+` in its line's error

Review round 1 of #2974.

- Removing `indented_comment_check` lost the message that named an
  indented `#!` / `#+`: the line's error became the generic `unexpected
  input` at the indentation. `name_mid_line_hash_comments`, a shared
  post-pass beside `name_misspelled_booleans`, finds each mid-line marker
  with the lexer itself (a `#` followed right away by its `!` or `+`, so
  never one in a string or a comment) and rewrites the line's first error
  at or before the `#` to point at the two characters and say that only
  `;` starts a comment that is indented or in the middle of a line. A
  defect already reported at the `#` (a posting's, a directive header's)
  is widened to both characters with the same hint. Only messages and
  spans move, on the same line, so every count stays beancount's. The
  hint now also reaches a top-level indented line and a directive's
  metadata, which main read as comments. Pinned by
  `tests/mid_line_hash_bang.rs` (exact message and span under a posting,
  at the top level, after a column-0 `#+TITLE` line, in an `open`'s
  metadata, and on posting and header lines).
- The `Shebang` / `EmacsDirective` arms of `invalid_runs` are gone: its
  text starts with a blank, so a `#!` there is mid-line. The
  `is_comment_token` rustdoc no longer calls an indented `#+STARTUP`
  comment trivia.
- BOM matrix harness: `hash_bang_cut` finds the markers with the lexer,
  so it no longer cuts inside a string or a comment, and `main_read_less`
  also requires the cut to move the line's count; its doc says it accepts
  a line whatever its cause. The matrix test passes with no list change.
- CHANGELOG: the entry names the restored message and the cache bumps.

Refs #2928

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

* fix(parser): rename a line's error for a mid-line `#!` only where the marker leads the line

Review round 2 of #2974.

`name_mid_line_hash_comments` renamed the first generic `unexpected input`
anywhere on the line before the `#`, so an earlier real error on the line
(`$x` in `  Assets:A  1 USD $x #+y`, `open Assets:A ÜÜ #!x`, a stray `\r`
before `#!x`, a line led by `$x`, `true`, `k: true` or `é`) was moved onto
the marker and its own report vanished, and the error list could leave its
start order. Now the line's error is renamed only when the marker leads its
line (after its indentation); otherwise only a defect reported at the `#`
itself is widened. Only blanks lie between the line start and a leading
marker, so the list stays sorted.

Tests in `tests/mid_line_hash_bang.rs`: an earlier bad token keeps its own
error over its span (posting, `open` header, a line led by `$x`, metadata),
a stray `\r` before `#!x` keeps its error, a `#!` inside a string renames
nothing, errors stay sorted by start, and the `#+` of `<BOM>  #+x` is bytes
5..7. Per-line counts match bean-check 3.2.3 on every repro.

Refs #2928

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

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 14, 2026
…s an invalid token (#2975)

A transaction whose posting holds an invalid token is kept under error
recovery (#2932) with what recovery could read of it, so a posting's
price or cost after the token is gone (`1 HOOL <BOM>{2 USD}`,
`1 HOOL \x11@ 2 USD`, `1 HOOL $x{2 USD}`). The transaction then did not
balance, and every surface reported two `E3001` beside the token's parse
error, where bean-check 3.2.3 reports the token alone: beancount's grammar
never builds such a transaction.

The late balance check now asks, only once it has found a residual,
whether the transaction holds an invalid token, by the parser's own rule:
`rustledger_parser::transaction_holds_invalid_token` parses the
transaction's own text (its directive span) and applies
`holds_an_invalid_token`, the rule the duplicate-metadata check already
skips such a transaction by. The text comes through `SourceLines`, which
every surface already hands the validator: `SourceLines::with_text` /
`text`, filled by `rustledger_loader::source_lines_overlaid` from the
source map and from an editor's buffers (whose text is now kept), and by
the new `source_lines_of_text` for an LSP buffer validated outside a
ledger. Without the text (dropped, or a synthesized transaction) the
residual is reported as before. A ledger that balances never reads its
source here.

The parse output is unchanged. Pinned by a surface-agreement case (CLI,
loader, WASM, FFI, LSP) for `{`, `@`, `@@`, BOM-led, BOM-glued, control
character and invalid-run shapes beside a transaction that really does
not balance, by `test_transaction_holds_invalid_token_reads_the_parsers_rule`,
and by a loader test of the text access.

Caches: WASM v113 (its payloads archive validation findings; v111 is held
by #2973 and v112 by #2974). Loader v73, unchanged: every affected source
has a parse error, and that cache is saved only for an error-free load.

Closes #2954

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.

WASM validateSource gives E2001 a hint that rledger check and the multi-file surfaces do not

1 participant