[Bug] SureImport fabricates opening_anchor valuations from current account balance → readback mismatch + phantom cash_adjustments
Before you start (required)
General checklist
- I have removed personal / sensitive data from screenshots and logs
- I have searched existing issues and discussions to ensure this is not a duplicate issue
How are you using Sure?
- I was a paying Maybe customer (hosted version)
- I use it on PikaPod, Umbrel or similar (VPS included)
- I am a self-hosted user (local only)
Self hoster checklist
Paying, hosted users should delete this entire section.
- Self hosted app commit SHA (find in user menu):
deb61feb4497822db71345ce01ae03a174ca50af(stable release 0.7.3, imagesha256:edf4df98b049031dd87499155000fdf82aacd9fe7d41ab39dfc15b6412915774)- I have confirmed that my app's commit is the latest version of Sure
- Where are you hosting?
- Render
- Docker Compose
- Umbrel
- PikaPod
- Other (please specify)
Bug description
When importing a family export NDJSON (export_version: 2) via SureImport, the importer fabricates opening_anchor valuations that do not exist in the source file. For any account that has no explicit opening_anchor valuation and no authoritative_balance_history: true flag on its Account record (a flag that export v2 never emits), the importer takes the account's current balance (account.balance — the balance at export time) and places it as an opening anchor at oldest_entry_date - 1 day.
This is mathematically inconsistent whenever the export contains a complete daily balance history (in my case: 3,565 Balance records). Consequences:
- Readback verification mismatch:
{"valuations": {"actual": 27, "expected": 24}}— 3 fabricated anchors. - Phantom
cash_adjustments: balance recalculation creates bridge adjustments / phantom balances that never existed in reality. - Once a provider (e.g. Enable Banking) is re-connected and sets a real
current_anchor,Balance::ReverseCalculatorinherits the fabricated anchor values and produces incorrect historical balances until the anchors are manually deleted.
The three affected accounts all show the same pattern — today's balance becomes the anchor at the very start of history:
| Account | Fabricated opening_anchor | Real first Balance in export |
|---|---|---|
| Account A (Depository) | 1633.02 @ 2024-06-08 | 906.49 @ 2024-06-09 |
| Account B (Depository) | 2033.32 @ 2024-09-14 | 36.00 @ 2024-09-15 |
| Account C (Depository) | 3972.03 @ 2025-05-31 | 6120.29 @ 2025-06-01 |
To Reproduce
Minimal reproduction verified on a freshly flushed self-hosted 0.7.3 instance, through the full SureImport#import! pipeline (not just Family::DataImporter):
- Start Sure 0.7.3 and create a family.
- Create a file
min.ndjsoncontaining noopening_anchorvaluation:
{"type":"Account","data":{"id":"aaaaaaaa-0000-4000-8000-000000000001","accountable_type":"Depository","name":"Test Account A","balance":"100.00","cash_balance":"100.00","currency":"EUR","status":"active","institution_name":"Test Bank","institution_domain":"test-bank.example"}}
{"type":"Balance","data":{"account_id":"aaaaaaaa-0000-4000-8000-000000000001","date":"2025-06-01","balance":"80.00","currency":"EUR"}}
{"type":"Balance","data":{"account_id":"aaaaaaaa-0000-4000-8000-000000000001","date":"2025-06-02","balance":"90.00","currency":"EUR"}}
{"type":"Balance","data":{"account_id":"aaaaaaaa-0000-4000-8000-000000000001","date":"2025-06-03","balance":"100.00","currency":"EUR"}}
{"type":"Transaction","data":{"id":"bbbb0000-0000-4000-8000-000000000001","account_id":"aaaaaaaa-0000-4000-8000-000000000001","date":"2025-06-02","amount":"10.00","currency":"EUR","name":"Test purchase 1","kind":"standard","excluded":false}}
{"type":"Transaction","data":{"id":"bbbb0001-0000-4000-8000-000000000001","account_id":"aaaaaaaa-0000-4000-8000-000000000001","date":"2025-06-03","amount":"-10.00","currency":"EUR","name":"Test refund 1","kind":"standard","excluded":false}}
{"type":"Valuation","data":{"id":"cccc0000-0000-4000-8000-000000000001","account_id":"aaaaaaaa-0000-4000-8000-000000000001","date":"2025-06-03","amount":"100.00","currency":"EUR","kind":"current_anchor","name":"Current balance"}}- Import it via Settings → Import → Sure Import (NDJSON).
- Open the completed import: readback verification reports a mismatch for
valuations(actual 2, expected 1). - Inspect the DB:
SUMMARY={"valuations":{"created":1,...}} # importer summary says 1 created
ALL_VALUATIONS=[["opening_anchor","2025-05-31","100.0"], # ← NOT in the file
["current_anchor","2025-06-03","100.0"]] # imported as-isThe fabricated anchor is opening_anchor @ 2025-05-31 = 100.0 "Opening balance": date = oldest Balance date − 1 day, amount = account.balance ("100.00" from the Account record), even though the source account never had a 100.00 balance on 2025-05-31.
Root cause, app/models/family/data_importer.rb:
if created && data["balance"].present? && !skip_opening_balance_import?(old_id, data)
manager = Account::OpeningBalanceManager.new(account)
result = manager.set_opening_balance(
balance: data["balance"].to_d, # ← CURRENT balance of the account
date: opening_balance_date_for(old_id, data) # ← oldest entry date - 1 day
)def skip_opening_balance_import?(old_id, data)
@imported_opening_anchor_account_ids.include?(old_id) ||
truthy?(data["skip_opening_balance_import"]) ||
truthy?(data["authoritative_balance_history"]) # ← never set by export v2
endThe import's built-in mitigation (authoritative_balance_history) never triggers because export v2 does not emit the flag, even when the export carries an authoritative daily balance stream. Account::OpeningBalanceManager#set_opening_balance only validates that the anchor date is before the oldest entry date — it cannot detect that the balance value (today's balance) is implausible for a date ~2 years earlier.
Expected behavior
When the import provides an authoritative daily balance history for an account (and no explicit opening_anchor valuation), the importer should either:
- skip opening-anchor creation entirely and let
Balance::ReverseCalculatorderive it fromcurrent_anchor+ transactions, or - create the opening anchor from the oldest
Balancerecord (its date AND amount), not fromaccount.balance.
Alternatively (export-side): export v2 should emit authoritative_balance_history: true on Account records that ship with a full daily balance history, so skip_opening_balance_import? works as designed.
Screenshots and/or recordings
None needed; the bug is fully reproducible from the 7-line NDJSON above and verifiable from the database after import:
imports.readback_verification→{"status": "mismatch", "mismatches": {"valuations": {"actual": 27, "expected": 24}}}(real export) orvaluations: actual 2, expected 1(minimal repro)SELECT ... FROM balances WHERE cash_adjustments <> 0→ phantom adjustment rows
Workaround (DB)
Delete the fabricated opening_anchor valuations and their entries rows after import; the readback counts then align with the source file (verified: deleting the 3 fabricated anchors brings the accounts back to 24 valuations / 3,565 balances, matching the export). Without this, the fabricated anchors poison Balance::ReverseCalculator once a provider (Enable Banking) is re-linked and a fresh current_anchor is set, producing incorrect historical balances.
Source: we-promise/sure