Skip to content

fix(query): skip advisory-only diagnostics on stderr, as check does - #2534

Merged
robcohen merged 6 commits into
mainfrom
fix/query-advisory-diagnostics
Oct 9, 2026
Merged

robcohen merged 6 commits into
mainfrom
fix/query-advisory-diagnostics

Conversation

@robcohen

@robcohen robcohen commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

rledger query printed E1004: Cannot close account ... with non-zero balance on ledgers that rledger check and bean-check both pass.

E1004 is advisory-only. Beancount does not flag closing an account that still holds a balance, so bean-query prints nothing, and check has skipped the code since #1446 (rledger lint closed-nonempty reports it). #2238 turned validation on in query so that #balances.discrepancy could be computed, but it printed every diagnostic, including the advisory one.

query now filters with is_advisory_only_code, the same predicate check uses. Every other diagnostic is still printed.

Repro:

2024-01-01 open Assets:Cash
2024-01-01 open Expenses:Food
2024-01-07 * "lunch"
  Expenses:Food  12.50 USD
  Assets:Cash
2024-02-20 close Expenses:Food
stderr
bean-query (beanquery 0.2.0) nothing
rledger check No errors found
rledger query before E1004: Cannot close account Expenses:Food with non-zero balance
rledger query after nothing

Tests: crates/rustledger/tests/query_advisory_diagnostics_test.rs covers two cases: the advisory code is not printed, and a real E2001 on the same ledger still is. Both fail with the fix reverted.

The other surfaces don't have this problem. report does not validate. The wasm/LSP paths deliberately show advisory codes as warnings.

Review pass: every surface that validates and prints diagnostics

The loader validates by default (LoadOptions::default().validate == true), so I checked every call site.

surface validates advisory E1004 real diagnostics
rledger check (text and --format json) yes hidden (is_advisory_only_code) shown
rledger query (text, csv, json: one stderr path) yes hidden (is_advisory_only_code), this PR shown; option diagnostics added in a8a04ff
rledger lint closed-nonempty yes reported: it is the advisory's home, keyed on the same ErrorCode n/a
rledger report *, price, extract (--ledger / --existing loads), doctor no (validate: false) never produced parse, booking and plugin errors only
ag-rledger query same run_with_writer, same filter hidden written to stderr, not to the envelope's warnings (see below)
LSP, wasm, FFI component yes a warning, by ErrorCode::severity() errors
  • The structured surfaces don't use the predicate, on purpose. LSP, wasm and the component carry each diagnostic's severity, so a host can filter on it, and E1004 is Severity::Warning. Hiding it there would take it from editors and Fava, which show warnings as warnings. is_advisory_only_code is the rule for the text CLIs that mirror bean-check / bean-query. It is now used at both places that print validation diagnostics as text.
  • Found and fixed here (a8a04ff): query dropped the option diagnostics (E7001 unknown option, E7002 invalid value, ...). They live on options.warnings, not errors, and check and bean-query both print them. query now prints them first, as bean-query does. query_prints_option_diagnostics fails without the change.
  • JSON / CSV output: diagnostics go to stderr in every output format, through the one filtered loop, and stdout stays data. Checked by hand in -f json, -f csv and text.
  • Exit status: no diagnostic changes query's exit code, advisory or not. query exits 0 after printing an E1001 or an E7001, and so does bean-query 0.2.0 on the same ledger. Only the parse and booking refusals (query/report exit 0 and emit data from a file that failed to parse #1908) exit non-zero. Unchanged by this PR.
  • Not fixed here: ag-rledger query passes its diagnostics to the process's stderr. ag-rledger report routes them into the envelope's warnings, so an agent reading only the envelope sees a report's warnings but not a query's E2001. That is the same gap the report sink was introduced to close. It is a follow-up: it needs a diagnostics sink on query::run_with_writer.

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

Closing an account that still holds a balance is E1004, which is
advisory-only: beancount does not flag it, so bean-check and bean-query
print nothing, and `rledger check` already skips it (#1446). #2238 turned
validation on in `query` (for `#balances.discrepancy`) without that skip,
so `rledger query` printed "E1004: Cannot close account ... with non-zero
balance" on ledgers `rledger check` called clean.

`query` now filters with the same `is_advisory_only_code` predicate as
`check`. Other diagnostics on the same load are still printed.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen
robcohen requested a review from a team as a code owner October 8, 2026 19:56
@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 45.6ms ± 1.2ms 820.4ms ± 1.4ms 18.0x 51.2 MB / 50.1 MB
investment 73.9ms ± 1.6ms 3033.2ms ± 74.3ms 41.1x 57.3 MB / 58.0 MB
fifo 67.9ms ± 2.7ms 15428.5ms ± 190.2ms 227.2x 55.2 MB / 57.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 45.1ms ± 1.1ms 45.6ms ± 1.2ms +1.1% ✅ within noise 51.1 MB → 51.2 MB (+0.1%)
investment 70.4ms ± 1.2ms 73.9ms ± 1.6ms +4.9% ✅ within noise 57.1 MB → 57.3 MB (+0.2%)
fifo 66.9ms ± 1.2ms 67.9ms ± 2.7ms +1.5% ✅ within noise 55.3 MB → 55.2 MB (-0.2%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.7ms, investment 0.9ms, fifo 1.3ms), 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 Oct 8, 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 Oct 8, 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 819
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 October 8, 2026 22:08
`query` printed `ledger.errors` only. Option diagnostics (E7001 unknown
option, E7002 invalid value, the rest of E7xxx) are kept on
`options.warnings`, so a misspelled option was reported by `rledger
check` and bean-query and went unreported here. They are now printed
first, as bean-query does, ahead of the validation errors, through the
same `--no-errors` switch.

Neither kind of diagnostic changes query's exit status (bean-query
also exits 0).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen

robcohen commented Oct 8, 2026 •

Copy link
Copy Markdown
Member Author

Review pass

I checked every surface that validates and prints diagnostics (table in the PR body), JSON output, and exit status.

  • Found and fixed (a8a04ff). query dropped the option diagnostics. E7001 (unknown option), E7002 (invalid value) and the rest of E7xxx live on options.warnings, not errors, so a misspelled option was reported by rledger check and bean-query and went silent in query. They are now printed first, as in bean-query. query_prints_option_diagnostics fails without the change.
  • One predicate. Both text surfaces that print validation diagnostics, check and query, filter with is_advisory_only_code. report, price, extract and doctor load with validate: false, so E1004 never arises there. lint closed-nonempty is the advisory's home. LSP, wasm and the component carry severity, so there E1004 is a warning, not hidden; that is deliberate.
  • JSON / CSV. There is one stderr path for every output format, filtered the same way, and stdout stays data.
  • Exit status. No diagnostic changes query's exit code, advisory or real. It exits 0, as bean-query 0.2.0 does on the same ledgers.
  • Follow-up, not fixed here. ag-rledger query writes diagnostics to the process's stderr, not into the envelope's warnings, which ag-rledger report fills. Closing that needs a diagnostics sink on query::run_with_writer.
  • Main merged (3fa96ad). The CHANGELOG conflict with fix(query): make the MIN/MAX property test exact on tied values (flaky in CI) #2535 is resolved, keeping both entries.

Review pass 2

Looked at the new option-diagnostic printing. Nothing found.

  • Ordering: option diagnostics print first and in line order, then validation errors. This is the order bean-query 0.2.0 uses on the same ledger: unknown option, bad booking_method, missing document root, failing balance.
  • No duplicates: option diagnostics live only on options.warnings. Nothing copies them into errors, and E7006 is added to options.warnings alone.
  • Same text as check: the E7001, E7002, E7003 and E7006 message text is identical in check and query. Only the prefix differs: check writes error[E7001]:, and query writes E7001: as it does for every diagnostic, as bean-query does.
  • JSON / CSV: diagnostics go to stderr only, and stdout stays valid JSON.

@robcohen
robcohen merged commit 4cea77c into main Oct 9, 2026
74 of 76 checks passed
@robcohen
robcohen deleted the fix/query-advisory-diagnostics branch October 9, 2026 06:58
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