Skip to content

fix(plugin): close_tree closes a tree by a parent that was never opened, as beancount does - #3073

Merged
robcohen merged 2 commits into
mainfrom
fix/close-tree-unopened
Nov 28, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/close-tree-unopened

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

close_tree follows beancount 3.2.3's plugin on two points:

  • a close of an account that no open opens is dropped, once its opened descendants are closed;
  • the descendants it closes are the opened accounts only, not every account a posting names.

Why

Closing a parent that was never opened is how the plugin closes a whole tree (close Assets:Bank with only Assets:Bank:Checking opened). rledger closed the sub-account but kept the parent's close, which then failed as closing an account never opened (E1001), where bean-check is clean. And a posting to an account never opened (already the validator's error) also got a close.

Black-box, beancount 3.2.3 with plugin "beancount.plugins.close_tree":

ledger beancount
close Expenses:R (never opened) no entries, no errors
open Expenses:R:X + close Expenses:R open Expenses:R:X, close Expenses:R:X (plugin), no error
open Expenses:R + close Expenses:R both kept
posting to Expenses:R:X (never opened) + close Expenses:R no close for Expenses:R:X; the unknown-account error

Found by the semantic differential (#3071), category entries|extra-in-impl|close.

What this does not change: rledger runs auto_accounts in the pre-booking synth pass, so with auto_accounts declared after close_tree the never-opened account is still opened first and its close kept, where beancount runs the two in declaration order. That ordering is listed as an open question in #3071, not decided here.

How to test

cargo test -p rustledger-plugin --test native_plugins_test close_tree
cargo test -p rustledger-loader --test loader_test close_tree

New: test_close_tree_drops_the_close_of_an_unopened_parent, test_close_tree_drops_the_close_of_a_never_opened_account, test_close_tree_closes_only_opened_descendants, test_close_tree_closes_a_tree_by_an_unopened_parent. docs/reference/plugins.md updated. No cache change: the plugin runs on every load.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 42.7ms ± 2.1ms 592.3ms ± 18.3ms 13.9x 45.0 MB / 51.1 MB
investment 64.9ms ± 2.4ms 2248.2ms ± 32.3ms 34.6x 47.2 MB / 59.1 MB
fifo 64.5ms ± 2.0ms 13097.9ms ± 1011.2ms 203.0x 48.9 MB / 59.0 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 41.5ms ± 0.2ms 42.7ms ± 2.1ms +3.0% ✅ within noise 45.0 MB → 45.0 MB (+0.0%)
investment 66.9ms ± 2.6ms 64.9ms ± 2.4ms -2.9% ✅ within noise 46.9 MB → 47.2 MB (+0.7%)
fifo 65.4ms ± 1.8ms 64.5ms ± 2.0ms -1.3% ✅ within noise 49.1 MB → 48.9 MB (-0.4%)

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

@github-actions

github-actions Bot commented Nov 27, 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 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 and others added 2 commits November 27, 2026 18:23
…ed, as beancount does

Closing Assets:Bank when only Assets:Bank:Checking was opened is how the
plugin closes a whole tree. rledger closed the sub-account but kept the
parent close, which then failed as closing an account never opened
(E1001), where bean-check is clean. The close of an account no open opens
is now dropped once its descendants are closed, and only opened accounts
are closed (a posting to an account never opened is the validator error,
and closing it too reported it twice).

Found by the semantic differential (scripts/compat-semantic-parity.py).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A Ledger archives its processed directives, which close_tree now changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen force-pushed the fix/close-tree-unopened branch from 09f62aa to 1d45fe8 Compare November 28, 2026 00:27
@codecov

codecov Bot commented Nov 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@robcohen
robcohen merged commit 1fb5b8a into main Nov 28, 2026
67 of 72 checks passed
@robcohen
robcohen deleted the fix/close-tree-unopened branch November 28, 2026 00:52
robcohen added a commit that referenced this pull request Nov 28, 2026
With close_tree (#3073) on main, auto_accounts at its declared place clears
both entries|extra-in-impl|open (8) and entries|extra-in-impl|close (12):
99.50% -> 99.74% of judged ledgers match, no regression.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen added a commit that referenced this pull request Nov 28, 2026
…s beancount does (#3075)

* fix(loader): run auto_accounts where it is declared, after booking, as beancount does

auto_accounts (and the auto plugin) ran in the pre-booking synth pass,
ahead of every other plugin whatever the declared order, and over
transactions booking then refused. It opened the accounts of a refused
transaction and of an elided posting booking drops, where bean-check opens
neither, and a plugin declared before it saw its opens.

It is now a regular plugin, run at its declared place. Early validation
still judges posting accounts against the opens it will insert, through
rustledger_loader::early_validation_view: a copy of the stream with them,
computed by the plugin itself, that nothing downstream sees. The LSP reads
the same view. check --auto runs it after the file plugins, as
bean-check --auto does. Document discovery stays in the synth pass (beancount
runs it first; its documents carry no amounts).

WASM cache v157. Maintainer decision on question 1 of #3071.

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

* test(compat): ratchet the semantic parity manifest

With close_tree (#3073) on main, auto_accounts at its declared place clears
both entries|extra-in-impl|open (8) and entries|extra-in-impl|close (12):
99.50% -> 99.74% of judged ledgers match, no regression.

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