Skip to content

fix(parser)!: require a flag or txn after a transaction's date, as beancount does - #2909

Merged
robcohen merged 3 commits into
mainfrom
fix/flagless-transaction
Nov 10, 2026
Merged

robcohen merged 3 commits into
mainfrom
fix/flagless-transaction

Conversation

@robcohen

@robcohen robcohen commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

What changed

A transaction header with no flag (2024-01-02 "x" followed by postings, with no *, ! or txn) loaded clean in rledger as a * transaction. beancount 3.2.3 rejects it with syntax error, unexpected STRING, because its grammar needs txn or a flag after the date. It is now a parse error.

  • txn_header::first_header_defect (the one rule shared by the green and red walkers) has a new MissingFlag defect: a string, tag or link straight after the date. It is reported at that token, with the message unexpected "\"x\"" in transaction header: a transaction needs a flag (*or!) or txn after the date.
  • identify_directive now also opens a TRANSACTION node on DATE TAG and DATE LINK, so a header that starts with a tag or link keeps its postings. It used to give one error for the header and another for each posting line. Now it gives one error, as beancount does.
  • Error recovery: the transaction is reported but kept, the same as every other transaction-header defect (fix(parser): stray characters inside a directive parse silently and rledger format deletes them #2802 / fix(parser): report stray characters in directive header lines #2805, REPORTS BUT DOES NOT DROP), so one missing flag does not cascade into unopened-account or balance errors. beancount drops the transaction, but the load fails on the parse error either way. spec/impl/error-recovery.md already describes this (missing flag: recover, keep, report).
  • beancount's grammar stops at the first string of a flagless header and discards the rest of the line. So on such a line, the | deprecation warning (which comes from beancount's grammar) and a mid-line #! / #+ are no longer reported on top of the header error. These two follow-ups are scoped to MissingFlag only.
  • The comments that called the flagless form "implied txn shorthand that beancount accepts" have been corrected. CHANGELOG.md has a Breaking entry naming the fix (add * or txn after the date).
  • Loader cache v65, WASM cache v86. WASM v85 is on main (fix(validate)!: pad from one shared plan as beancount does, and drop E2004 #2898), and open PR fix(parser): lex an account component with any non-ASCII start, as beancount does #2908 claims loader v64, so this takes the next free numbers. Each version has exactly one note, and the WASM pin is at loader v65.

History check: not a deliberate extension

  • rledger's first parser (the chumsky parser in 0b08da4c7a5, "Initial release") required a flag: transaction_body starts with flag(), with no .or_not().
  • The flagless form arrived in d65a62fdeac ("perf(parser): add winnow-based parser", Feb 2026) as one arm, Token::String(_) => '*', // Implied txn. It had no test, doc or CHANGELOG entry.
  • The CST port (feat(parser): #1262 phase 2.1b TRANSACTION header recognition + multi-line body #1274, ae22b566cf0) copied that arm. Copilot's review only pointed out that identify_directive did not match the legacy parser's STRING dispatch. The words "Beancount accepts the implied-transaction shorthand" were written in feat(parser): #1262 phase 2.1b TRANSACTION header recognition + multi-line body #1274's own fixup commit, and transaction_with_implied_flag_via_bare_string pinned them. The claim is false.
  • Nothing in docs/ or CHANGELOG.md describes it as an rledger extension.

bean-check comparison (beancount 3.2.3, nix develop .#bench)

Each case has open Assets:A and open Assets:B on lines 1 and 2, with the header under test on line 3.

Input (line 3) bean-check main this PR
2024-01-02 "x" + postings 3: unexpected STRING no error 3:12 header error
2024-01-02 "p" "x" + postings 3: unexpected STRING no error 3:12
2024-01-02 "x" #t ^l + postings 3: unexpected STRING no error 3:12
2024-01-02 #t ^l + postings 3: unexpected TAG 3 errors (lines 3, 4, 5) 3:12 (one)
2024-01-03 ^l "y" + postings 3: unexpected LINK 3 errors (lines 3, 4, 5) 3:12 (one)
2024-01-02 "p" | "x" + postings 3: unexpected STRING no error 3:12 (no pipe warning)
2024-01-02 "x" $ (#2903 review) 3: unexpected STRING, 3: Invalid token: '$' 3:16 only 3:12 and 3:16
2024-01-02 "x", then 2024-01-03 * "y" + postings 3: unexpected STRING, y kept no error 3:12, y kept
bare 2024-01-02 + postings 4: unexpected EOL lines 3, 4, 5 unchanged (not a header this changes)

There are two known differences, both from bison's three-token error recovery, which suppresses a second error that comes too soon after the first:

  • "x" #t ^l + postings followed by 2024-01-03 ^l "y" + postings: beancount reports only line 3, and rledger reports lines 3 and 6.
  • Two flagless headers in a row: bean-check gives 1 error, and rledger gives 2.

Parse-error parity harness (scripts/compat-parse-error-parity.py)

Source main this PR
compat (491) 488 match 488 match (unchanged)
tracked (384) 381 match 381 match (unchanged)
fuzz (44192) 25312 25408
matrix (66256) 50705 50876
all (111323) 76886 (69.1%) 77153 (69.3%)

bc-only (beancount errors, rledger silent) went from 78 to 27. The manifest is re-pinned with --update, and no ledger row changed. --check is clean.

12 fuzz inputs that used to match no longer do. In every one, the earlier match was a coincidence that the new header error now exposes:

  • 10 have a UTF-8 BOM inside a string. rledger reports BomInDirectiveBody there, but beancount accepts a BOM in a string (2024-01-02 * "ab" loads clean in bean-check and errors in rledger, on main too). Before this PR, that BOM error stood in for beancount's syntax error in the line count. This is a separate, pre-existing divergence and worth its own issue.
  • 1 is bison's three-token recovery suppressing a second error (! on line 1, then a flagless header on line 2).
  • 1 has a first header string that spans two lines. beancount gives the line where the string ends, and rledger gives the line where it starts.

Real-ledger impact

  • 0 of 731 files in the compatibility corpus have a flagless transaction (by grep for a date followed directly by a string, tag or link, and by the harness's unchanged compat rows).
  • 0 of 405 in-tree .beancount files have one.
  • The only in-tree occurrence is the illustrative snippet in spec/impl/error-recovery.md, which already expects a missing-flag error.

This is still user-visible: any user ledger with this form stops loading, so the CHANGELOG entry is under Breaking.

Downstream consumers

  • Formatter: rledger format, LSP formatting and try_format_source all refuse a file with parse errors, so a flagless file is refused rather than rewritten. The CST formatter never removes a flag.
  • LSP and WASM completion: after a date, the candidates are DIRECTIVES, which offer txn, * and !. No snippet inserts a bare string after a date.
  • rledger add, CSV/OFX importers: they build Transaction::new(..).with_flag('*'). Core Transaction::flag is a char that is always rendered (format_transaction_lines writes "{date} {flag}").
  • ag-rledger writes through the same core formatter.
  • Residual: a WASM importer or plugin that returns a whitespace flag (" ") passes data_to_transaction (it accepts any single char) and would render a header with no visible flag. This is pre-existing, applies to any invalid flag char, and is not changed here.

How to test

cargo test -p rustledger-parser --test txn_header_conformance
cargo test -p rustledger-parser --lib txn_header

The four new conformance tests fail on main. They check both parse and parse_red_only.

Verified locally:

  • cargo test --workspace --all-features
  • cargo clippy --all-features --all-targets -D warnings
  • RUSTDOCFLAGS=-Dwarnings cargo doc --workspace --no-deps
  • cargo fmt --check
  • STRICT corpus_baseline and cst_baseline (no row moved)
  • the harness with --check

Closes #2905

🤖 Generated with Claude Code

…ancount does

`2024-01-02 "x"` with postings, with no `*`, `!` or `txn`, loaded clean as
a `*` transaction, where beancount 3.2.3 reports `syntax error, unexpected
STRING`: its grammar has no flagless transaction. The form was never a
documented extension. rledger's first parser required a flag; the
shorthand came in with the winnow parser and was carried into the CST
parser on the belief that beancount accepts it.

A header whose first string, tag or link comes straight after the date
is now a `MissingFlag` header defect, reported at that token by the one
rule both converters share (`txn_header::first_header_defect`). As for
every header defect, the transaction is kept, so one missing flag does
not cascade into unopened-account or balance errors. A header that starts
with a tag or a link is now a transaction node too, so its postings are
not each reported as stray lines: one error, as in beancount. On a
flagless line, the `|` deprecation and a mid-line `#!` / `#+` after the
first string are not reported, since beancount's grammar stops there.

Parse-error parity: 76886 -> 77153 matching inputs; compat and tracked
ledgers unchanged. Loader cache v65, WASM cache v86.

Closes #2905

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

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

🔴 Slower than base on: simple

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 59.2ms ± 3.9ms 918.4ms ± 12.1ms 15.5x 42.7 MB / 51.1 MB
investment 93.1ms ± 1.6ms 3335.1ms ± 112.9ms 35.8x 58.8 MB / 59.1 MB
fifo 85.4ms ± 1.6ms 16208.6ms ± 371.5ms 189.7x 45.0 MB / 58.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 56.4ms ± 0.6ms 59.2ms ± 3.9ms +5.1% 🟡 slower 42.9 MB → 42.7 MB (-0.5%)
investment 91.6ms ± 1.3ms 93.1ms ± 1.6ms +1.6% ✅ within noise 58.9 MB → 58.8 MB (-0.1%)
fifo 83.6ms ± 1.4ms 85.4ms ± 1.6ms +2.2% ✅ within noise 44.9 MB → 45.0 MB (+0.3%)

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

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 2 commits November 9, 2026 08:27
# Conflicts:
#	CHANGELOG.md
#	crates/rustledger-wasm/src/cache.rs
…t newline byte

Review of #2909. The `#!` / `#+` suppression on a header with no flag
found the line's end with the first `\n` byte, which can sit inside a
first string that spans two lines, so `2024-01-02 "a\nb" #!` still
reported the `#!`. It now uses the NEWLINE start that
`header_line_tokens` returns, and the conformance test pins that shape.
Also corrects a stale comment in tests/ast.rs that still called the
flagless form an implied-flag arm.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen merged commit 5740f11 into main Nov 10, 2026
69 of 70 checks passed
@robcohen
robcohen deleted the fix/flagless-transaction branch November 10, 2026 03:06
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.

fix(parser): a transaction with no flag is accepted; beancount reports a syntax error

1 participant