Skip to content

feat(ofx): warn when the statement contradicts the configured account - #2259

Merged
robcohen merged 2 commits into
mainfrom
feat/ofx-account-kind-warning
Sep 5, 2026
Merged

robcohen merged 2 commits into
mainfrom
feat/ofx-account-kind-warning

Conversation

@robcohen

@robcohen robcohen commented Sep 5, 2026 •

Copy link
Copy Markdown
Member

Addresses #2256, requested by @petemounce in #2126.

An OFX file states which side of the balance sheet it describes, twice over: the message set (CREDITCARDMSGSRSV1 vs BANKMSGSRSV1) and, for bank statements, ACCTTYPE. The parser read neither — it reads only DTPOSTED, TRNAMT, NAME, MEMO, CURDEF, CURRENCY — so importing a credit-card statement into an Assets: account silently inverted the sign of every transaction.

$ rledger extract card.qfx
warning: statement describes a liability account but --account is `Assets:Bank:Checking`;
         every imported amount will carry the opposite sign of what you expect.
         Check the account, or ignore this if the mapping is deliberate.
2024-01-15 * "COFFEE SHOP"
  Assets:Bank:Checking  -50.00 USD
  Expenses:Unknown

Why it warns instead of inferring

The issue asked for inference. This deliberately does less, because the account name is not in the file. Liabilities:CreditCard is a guess about someone's chart of accounts, and this module already refuses to guess a currency for exactly that reason:

config.currency is required (an OFX file may not declare a currency at the transaction or statement level; we refuse to guess and produce empty-string-currency Amounts).

The side of the balance sheet is a fact in the document, and getting it wrong is the expensive error. So that is all this claims. Name inference stays open in #2256.

Conservative in three ways

Each is a test, not an assertion in prose:

  • Mixed files decline. A file carrying both message sets describes more than one account; the check returns a single answer, so it returns None rather than pick one. A warning naming the wrong statement is worse than no warning.
  • Unrecognized roots yield no opinion. A ledger using localized account roots (Actifs, Passif) is not misconfigured. Warning at it would be noise, so an unrecognized root means "no opinion", not "mismatch". This avoids plumbing ledger AccountTypes into the importer, which has no access to ledger options today.
  • CREDITLINE is a liability even though it arrives in the bank message set.

The warning is advisory — the import still produces its transactions.

Tests

11 total. Verified not vacuous by neutralizing the check and re-running:

credit_card_statement_into_an_asset_account_warns   FAILED
bank_statement_into_a_liability_account_warns       FAILED
creditline_acct_type_is_a_liability                 FAILED
the_warning_does_not_suppress_transactions          FAILED
matching_accounts_do_not_warn                       ok
unrecognized_account_root_does_not_warn             ok
a_mixed_file_does_not_warn                          ok
a_statement_with_neither_message_set_does_not_warn  ok

The four that pass either way are the silence cases, and that is inherent: an end-to-end silence test cannot distinguish "correctly quiet" from "feature deleted". So detect_statement_kind and account_kind_mismatch are also tested directly, where declining is a positive assertion about the function rather than an absence of output.

Also verified end to end against a real .qfx through the built binary: warns into Assets:Bank:Checking, silent with -a Liabilities:CreditCard.

Adjacent bug, filed separately

While testing I found that an importers.toml entry's account is not applied when an OFX file is dispatched by extension. Chased it down and filed as #2260: named entries force the CSV parser onto the OFX file, and unnamed ones dispatch to OFX but build config from CLI args alone, so there is no way to configure the OFX importer from importers.toml at all.

That interacts with this PR: the warning reports the account actually used, which is the CLI default, so a user with a correct importers.toml will still see a mismatch warning until #2260 is fixed. The warning is accurate about what happened — the import really did post to Assets:Bank:Checking — but the cause is #2260, not their config.

Not fixed here; it is a schema question, not a parser one.

An OFX file states which side of the balance sheet it describes, twice
over: the message set (`CREDITCARDMSGSRSV1` vs `BANKMSGSRSV1`) and, for
bank statements, `ACCTTYPE`. The parser read neither, so importing a
credit-card statement into an `Assets:` account silently inverted the
sign of every transaction. Addresses #2256.

Deliberately warns rather than infers. The account NAME cannot be derived
from an OFX file — `Liabilities:CreditCard` is a guess about someone's
chart of accounts, not a fact in the document — and this module already
refuses to guess a currency for the same reason. The side of the balance
sheet IS a fact, so that is all this claims.

Conservative in three ways, each covered by a test:

- A file carrying BOTH message sets describes more than one account, and
  the check returns one answer, so it declines rather than pick.
- An account whose root is not the configured Assets/Liabilities name
  yields no opinion. A ledger using localized roots (`Actifs`, `Passif`)
  is not misconfigured, and warning at it would be noise.
- `CREDITLINE` is a liability even though it arrives in the bank message
  set.

The warning is advisory: the import still produces its transactions.

11 tests. Four assert the warning and fail without the check; four assert
silence and cannot distinguish "correctly quiet" from "feature deleted",
so `detect_statement_kind` and `account_kind_mismatch` are also tested
directly, where declining is a positive assertion.

Co-Authored-By: Claude Opus 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 September 5, 2026 01:22
Copilot AI lite review requested due to automatic review settings September 5, 2026 01:22

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.

🟢 Approval recommended

The change is narrowly scoped, behavior-preserving aside from an advisory warning, and includes thorough tests for both warning and silence cases.

Pull request overview

Adds an OFX/QFX statement “kind” sanity check so rledger extract can warn when the statement’s implied balance-sheet side (asset vs liability) contradicts the configured target account, preventing silent sign inversion when importing (e.g., credit-card statements into Assets: accounts).

Changes:

  • Detects statement kind (asset/liability) from message-set wrappers and ACCTTYPE, declining on ambiguous inputs.
  • Emits an advisory warning when the statement kind contradicts config.account’s root type (Assets vs Liabilities).
  • Adds focused unit + end-to-end tests covering mismatch warnings, silence cases, and non-suppression of transaction import.
File summaries
File Description
crates/rustledger-importer/src/ofx_importer.rs Adds statement-kind detection + mismatch warning and comprehensive tests to prevent silent sign inversion on OFX imports.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@codecov

codecov Bot commented Sep 5, 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 Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 38.2ms ± 4.3ms 582.7ms ± 2.2ms 15.2x 54.9 MB / 50.1 MB
investment 54.3ms ± 1.8ms 2178.6ms ± 10.2ms 40.1x 61.1 MB / 58.1 MB
fifo 52.6ms ± 1.9ms 12191.4ms ± 23.4ms 231.8x 61.1 MB / 57.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 36.5ms ± 2.8ms 38.2ms ± 4.3ms +4.8% ✅ within noise 55.0 MB → 54.9 MB (-0.1%)
investment 54.5ms ± 1.6ms 54.3ms ± 1.8ms -0.3% ✅ within noise 61.0 MB → 61.1 MB (+0.2%)
fifo 52.9ms ± 2.5ms 52.6ms ± 1.9ms -0.5% ✅ within noise 61.0 MB → 61.1 MB (+0.2%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 2.3ms, investment 1.1ms, fifo 1.4ms), 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 Sep 5, 2026

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 99% (raw 93%, 169 masked)
Full AST Match 100%
Total Files 812
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

Copilot review on #2259: the comment said "configured Assets/Liabilities
names", implying the check reads the ledger's name_assets/name_liabilities
options. It compares against AccountTypes::default(). Reworded to say so,
and to state why the coupling is not worth adding for a warning.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen
robcohen enabled auto-merge (squash) September 5, 2026 09:56
@robcohen
robcohen merged commit 0a3c3b9 into main Sep 5, 2026
50 checks passed
@robcohen
robcohen deleted the feat/ofx-account-kind-warning branch September 5, 2026 10:16
robcohen added a commit that referenced this pull request Sep 6, 2026
* feat(extract): refuse an unconfigured liability import

Closes #2256.

An OFX statement states which side of the balance sheet it describes.
#2259 used that to warn when the configured account contradicts it. This
covers the case underneath: when nothing configured an account at all.

Importing a credit-card statement with no `--account`, no importers.toml
entry and no `--ledger` profile posted it to the `Assets:Bank:Checking`
default, inverting the sign of every transaction and using an account the
user never named. That is now refused, with the three ways to name one.

The account argument becomes `Option<String>` rather than carrying a clap
default, because "not named" and "named as the default" are genuinely
different and only the first is refused. Comparing against the default
string instead would have refused an explicit `-a Assets:Bank:Checking`
while merely warning about `-a Assets:Foo`, which is arbitrary. A test
pins that clap and the guard share one constant.

Deliberately not the account NAME inference the issue also asked about.
`Liabilities:CreditCard` is a guess about someone's chart of accounts,
not a fact in the file, and the module already refuses to guess a
currency for the same reason. Since the issue was filed, #2261 and #2262
gave two ways to supply the account without repeating it, so the case for
inventing a name is weaker than it was; the reasoning is recorded on the
issue.

Four tests, and the refusal test fails with the guard disabled.

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

* fix(extract): the refusal must consider every account source

Copilot review on #2263, plus the rustdoc failure it exposed.

The guard's comment said "no --account, no importers.toml entry, no
--ledger profile", but the code checked only `args.account`. An entry or
profile naming the same string as the default was therefore treated as
"nobody named an account" and the import refused. The comment described
the intent; the code implemented a third of it.

All three sources now collapse into one `named_account` binding, used
both to build the config and to decide the refusal, so the two cannot
disagree about what was named.

Also:

- The rustdoc build failed: the public `account` field linked to
  `DEFAULT_ACCOUNT` and `Args::account_or_default`, both private. Named
  in prose instead. `cargo clippy` and `cargo test` do not catch this;
  `cargo doc -D warnings` does, and it is now run before pushing.

- The `DEFAULT_ACCOUNT` comment still called it "the clap default" after
  the clap default was removed.

One test, which fails with the guard reverted to the args-only check.

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

---------

Co-authored-by: Claude Opus 5 (1M context) <[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.

2 participants