Repository navigation
feat(ofx): warn when the statement contradicts the configured account - #2259
Conversation
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
There was a problem hiding this comment.
🟢 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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
📊 Benchmark Results
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
✅ Compatibility Test Results
What's tested
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
* 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]>
Addresses #2256, requested by @petemounce in #2126.
An OFX file states which side of the balance sheet it describes, twice over: the message set (
CREDITCARDMSGSRSV1vsBANKMSGSRSV1) and, for bank statements,ACCTTYPE. The parser read neither — it reads onlyDTPOSTED,TRNAMT,NAME,MEMO,CURDEF,CURRENCY— so importing a credit-card statement into anAssets:account silently inverted the sign of every transaction.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:CreditCardis a guess about someone's chart of accounts, and this module already refuses to guess a currency for exactly that reason: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:
Nonerather than pick one. A warning naming the wrong statement is worse than no warning.Actifs,Passif) is not misconfigured. Warning at it would be noise, so an unrecognized root means "no opinion", not "mismatch". This avoids plumbing ledgerAccountTypesinto the importer, which has no access to ledger options today.CREDITLINEis 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:
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_kindandaccount_kind_mismatchare 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
.qfxthrough the built binary: warns intoAssets:Bank:Checking, silent with-a Liabilities:CreditCard.Adjacent bug, filed separately
While testing I found that an
importers.tomlentry'saccountis 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 fromimporters.tomlat 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.tomlwill still see a mismatch warning until #2260 is fixed. The warning is accurate about what happened — the import really did post toAssets:Bank:Checking— but the cause is #2260, not their config.Not fixed here; it is a schema question, not a parser one.