Skip to content

feat(extract): refuse an unconfigured liability import - #2263

Merged
robcohen merged 2 commits into
mainfrom
feat/ofx-unconfigured-account-error
Sep 6, 2026
Merged

robcohen merged 2 commits into
mainfrom
feat/ofx-unconfigured-account-error

Conversation

@robcohen

@robcohen robcohen commented Sep 6, 2026

Copy link
Copy Markdown
Member

Closes #2256.

#2259 used the OFX statement kind to warn when a configured account contradicts it. This covers the case underneath: when nothing configured an account at all.

$ rledger extract card.qfx
error: this statement describes a liability account, but no account was configured
       so the default `Assets:Bank:Checking` would be used — every amount would
       carry the opposite sign
  name the account with one of:
    --account Liabilities:...
    an `importers.toml` entry with `account = "Liabilities:..."`
    an `open` directive plus --ledger (see `rledger extract --help`)

Previously that imported silently, inverting the sign of every transaction into an account the user never named.

--account becomes Option<String>

"Not named" and "named as the default" are genuinely different, and only the first is refused. My first attempt compared the resolved account against the default string, which refused an explicit -a Assets:Bank:Checking while merely warning about -a Assets:Foo — arbitrary, and the error text claimed "no account was configured" when one had been. Modelling the absence honestly removes that.

The clap default is gone; Args::account_or_default() applies it at the point of use, and a test pins that clap and the guard share one constant, including that it is an Assets: account (which is what makes the contradiction a contradiction).

What this deliberately does not do

The issue also asked to infer the account name — CREDITCARDMSGSRSV1 → Liabilities:.... Not implemented, on purpose:

So the remaining value was in the unconfigured path, which is what this closes. Reasoning recorded on the issue rather than left implicit.

Tests

483 pass. Four new:

an_unconfigured_credit_card_import_is_refused       ← fails with the guard disabled
naming_the_account_explicitly_is_not_refused        ← incl. naming the default string
an_unconfigured_bank_import_still_uses_the_default  ← refusal is specific to a contradiction
the_default_account_is_what_the_guard_assumes       ← pins clap ↔ guard, and "Assets:"

Also checked by hand that CSV imports, --ledger profiles, importers.toml entries and explicit -a are all unaffected.

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
@robcohen
robcohen requested a review from a team as a code owner September 6, 2026 18:56
Copilot AI lite review requested due to automatic review settings September 6, 2026 18:56
@codecov

codecov Bot commented Sep 6, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.83721% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger/src/cmd/extract_cmd/mod.rs 98.79% 1 Missing ⚠️

📢 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.

🟡 Changes recommended

The new refusal guard can incorrectly trigger even when the account was configured via importers.toml or --ledger (including to the default string), contradicting the intended “unset vs explicitly set” semantics.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR hardens rledger extract against accidentally importing OFX/QFX liability statements (credit cards / credit lines) into the default Assets:Bank:Checking account when the user did not configure an account, preventing silent sign inversion and mis-posting.

Changes:

  • Make --account optional (Option<String>) and apply the default only at point-of-use via Args::account_or_default().
  • Refuse unconfigured OFX/QFX liability imports when the default account would otherwise be used, with a prescriptive error message.
  • Expose OFX statement-kind detection (StatementKind, detect_statement_kind) for reuse by the CLI, and update docs/tests accordingly.
File summaries
File Description
docs/commands/extract.md Documents the new refusal behavior for unconfigured OFX liability statements.
crates/rustledger/src/cmd/extract_cmd/mod.rs Implements Option<String> for account, centralizes defaulting, adds refusal guard + tests.
crates/rustledger/src/bin/ag-rledger.rs Updates struct-literal arg construction to preserve “unset” account as None.
crates/rustledger-importer/src/ofx_importer.rs Exposes statement-kind detection API needed by the CLI guard.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 2
  • Review effort level: Lite

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

@github-actions

github-actions Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

🔴 Slower than base on: investment, fifo

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 47.0ms ± 1.3ms 754.9ms ± 10.9ms 16.1x 55.0 MB / 50.1 MB
investment 77.9ms ± 7.6ms 2849.9ms ± 28.8ms 36.6x 59.3 MB / 58.1 MB
fifo 72.6ms ± 6.9ms 17057.7ms ± 621.5ms 234.9x 61.2 MB / 57.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 47.4ms ± 4.0ms 47.0ms ± 1.3ms -0.8% ✅ within noise 55.1 MB → 55.0 MB (-0.1%)
investment 71.1ms ± 3.0ms 77.9ms ± 7.6ms +9.5% 🟡 slower 59.1 MB → 59.3 MB (+0.3%)
fifo 69.1ms ± 3.2ms 72.6ms ± 6.9ms +5.1% 🟡 slower 61.1 MB → 61.2 MB (+0.2%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 1.9ms, investment 3.7ms, fifo 3.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 6, 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 #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
@robcohen
robcohen enabled auto-merge (squash) September 6, 2026 21:07
@robcohen
robcohen merged commit ba74a52 into main Sep 6, 2026
51 checks passed
@robcohen
robcohen deleted the feat/ofx-unconfigured-account-error branch September 6, 2026 21:08
robcohen added a commit that referenced this pull request Sep 6, 2026
Minor bump because two of the five commits are breaking under 0.x semver:

- importers.toml entries reject unknown keys (#2261). A config carrying a
  stray key now fails to parse instead of ignoring it. That is the point
  of the change, but it breaks configs that were quietly wrong.
- extract refuses an OFX credit-card statement when nothing named an
  account (#2263), rather than posting it to the Assets default with
  every sign inverted.

Bumped with `cargo set-version 0.24.0`, plus the three surfaces it does
not reach: mcp-server package.json version only (the @rustledger/wasm
dependency and package-lock.json stay put, as release-publish.yml sets
both from the tag), the rpm spec Version/Source0/%setup, and all eight
standalone lockfiles enumerated from git rather than a hand-kept list.

Pre-flight: check-publish-crates.py --check-registry reports 17/17 crates
present on crates.io; workspace check with --all-features --all-targets,
mcp-server npm ci + tsc, and wasm-pack build all pass.


Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
robcohen added a commit that referenced this pull request Sep 14, 2026
…adapter

Review of #2308 (Copilot, three threads).

`--balance` built its assertion from the raw flags, so an `importers.toml`
entry naming `Liabilities:Card` in GBP produced postings there and a balance on
`Assets:Bank:Checking` in USD — an assertion that can never hold. Even with
`--account` the currency stayed USD. It now reads the resolved config, after
the entry, flags, and any `--ledger` profile. This pre-dates the PR, but it is
the same bug class as #2304 in the same function. Mutation-verified: reading
`args` again fails the new test.

`ag-rledger`'s `build_extract_args` constructs `Args` by hand, and nothing
tested it against an entry: restoring a USD default there left all 14 existing
tests passing. A binary-level test now checks both directions through the real
binary — omitted flags keep the entry's account, currency, delimiter and
skip_rows; passed flags replace them, including an explicit `--skip-rows 0`.
It reads `result.stdout`, because the envelope echoes the command line and
would contain the flag values either way. Restoring the currency, skip_rows or
delimiter default each fails it.

The `Args` field type change is source-breaking for code that constructs
`Args` directly, and the crate is published. Recorded under Breaking in the
CHANGELOG, together with the identical change #2263 made to `account` without
an entry.

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

OFX: infer the account type from CREDITCARDMSGSRSV1 / ACCTTYPE instead of requiring it in config

2 participants