Skip to content

fix: ops::clamp threads account config instead of hardcoding Equity roots - #1810

Merged
robcohen merged 2 commits into
mainfrom
feat/clamp-accounts-config
Jul 19, 2026
Merged

robcohen merged 2 commits into
mainfrom
feat/clamp-accounts-config

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

rustledger_ops::clamp had two independent hardcodings — the issue named one, and the second is the more dangerous — both broken on a ledger that renames its roots or sets account_previous_*. This folds both into one ClampAccounts config.

1. Synthesized summary account names (the issue's original scope): the opening-balance contra (Equity:Opening-Balances) and the earnings rollup target (Equity:Earnings:Previous) were literals. They now come from account_previous_balances / account_previous_earnings.

2. Account classification (surfaced during review): is_balance_sheet / is_income_statement matched root prefixes by literal string ("Assets" | "Liabilities" | ...) — exactly the AccountTypes anti-pattern the canonical-function discipline forbids. On a option "name_assets" "Activa" ledger the classifier matched nothing, so no opening balances were synthesized at all — a silently empty clamp. Now uses the canonical AccountTypes.

Design

pub struct ClampAccounts {
    pub types: AccountTypes,        // classification (fix #2)
    pub previous_balances: String,  // opening-balance contra (fix #1)
    pub previous_earnings: String,  // earnings rollup
}

Default reproduces the prior hardcoded behavior exactly, so existing callers are unaffected. clamp / clamp_indexed take &ClampAccounts. The two component callers:

Scope note

There is no rledger clamp CLI subcommand — the only two callers are both in the component (the issue's "CLI" mention was inaccurate). And only the two previous_* options are relevant: clamp does no conversion / current-earnings summarization, so account_previous_conversions / account_current_* are deliberately not threaded.

Tests

  • ops-level drift guard (clamp_honors_renamed_roots_and_summary_accounts): clamps a fully renamed ledger (Activa/Vermogen/Inkomsten roots + renamed summary accounts) and asserts the renamed asset root IS carried forward — catching the silent-empty classifier failure a names-only test would miss — plus that summary legs use the configured accounts and no Equity:* default leaks.
  • default-equivalence (clamp_accounts_default_matches_legacy_hardcoding): pins the pre-ops::clamp synthesizes hardcoded Equity:Opening-Balances for every caller #1806 behavior.
  • component-level (clamp_honors_held_options_for_classification_and_summaries): proves session.clamp consumes held options (renamed income rolls into the configured earnings account) where options-less from_entries defaults.

Workspace green (3946 tests), clippy/doc/fmt clean.

Closes #1806.

🤖 Generated with Claude Code

https://claude.ai/code/session_013gJH7uBBF9Xw9et9QFRcsU

…oots

clamp had two independent hardcodings, both broken on a ledger that
renames its roots or sets account_previous_*:

1. Synthesized summary account names — the opening-balance contra
   (Equity:Opening-Balances) and the earnings rollup target
   (Equity:Earnings:Previous), which should come from
   account_previous_balances / account_previous_earnings.

2. Account classification — is_balance_sheet / is_income_statement
   matched root prefixes by literal string ("Assets" | "Liabilities" |
   ...). On a renamed-root ledger the classifier matched nothing, so NO
   opening balances were synthesized at all — a silently empty clamp,
   the more dangerous of the two. This is the AccountTypes anti-pattern
   the canonical-function discipline forbids.

Both fold into one ClampAccounts config (AccountTypes classifier +
previous_balances/previous_earnings names), threaded through
clamp/clamp_indexed. Default reproduces the prior hardcoded behavior
exactly. session.clamp builds it from the held options (the #1766
payoff — from-entries-with-options now drives clamp too), sharing a new
account_types() helper with query; the builder free clamp over a bare
directive list passes the default.

Drift guards: an ops-level test clamps a fully renamed ledger and
asserts the renamed asset root IS carried forward (catches the
silent-empty classifier failure a names-only test would miss) and the
summary legs use the configured accounts; a component test proves
session.clamp consumes the held options where from_entries defaults.

Closes #1806.
Copilot AI review requested due to automatic review settings July 19, 2026 03:42
@robcohen
robcohen requested a review from a team as a code owner July 19, 2026 03:42
@codecov

codecov Bot commented Jul 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates rustledger_ops::clamp to honor ledger-configured account roots and configured “previous balances/earnings” summary accounts, eliminating hardcoded Equity-root behavior that breaks renamed-root ledgers. This also threads the config through the WASI component so session.clamp uses held ledger options while the options-less free function falls back to defaults.

Changes:

  • Introduce ClampAccounts (with AccountTypes + configurable summary accounts) and thread it through clamp/clamp_indexed.
  • Switch clamp’s classification from hardcoded root-prefix matching to the canonical AccountTypes classifier.
  • Update the component’s clamp surfaces to pass either defaults (free function) or options-derived ClampAccounts (session), and add regression tests.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
crates/rustledger-ops/src/clamp.rs Adds ClampAccounts, uses AccountTypes for classification, threads configurable contra/earnings accounts, and expands tests.
crates/rustledger-ffi-component/src/convert.rs Updates WIT/component clamp paths to pass default vs held-options clamp config; deduplicates AccountTypes construction via a helper; adds component-level test.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/rustledger-ops/src/clamp.rs Resolved
Comment thread crates/rustledger-ops/src/clamp.rs Resolved
@github-actions

github-actions Bot commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 34.7ms ± 2.8ms 23.7 MB
beancount 895.8ms ± 12.6ms 50.6 MB
Comparison 25.8x faster 2.1x less

vs main branch (2026-07-19)

Metric main this PR Change
Time 35ms 34.7ms -0.9% ✅
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 811
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

Copilot review: the parenthetical listed both ClampAccounts fields
together, reading as if `contra` were previous_earnings. Spell out each
parameter's source field explicitly.
@robcohen
robcohen merged commit ca6b48e into main Jul 19, 2026
55 checks passed
@robcohen
robcohen deleted the feat/clamp-accounts-config branch July 19, 2026 04:33
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.

ops::clamp synthesizes hardcoded Equity:Opening-Balances for every caller

2 participants