Date a new loan's opening anchor on its start date
Summary
Closes #424
Upstream issue: we-promise/sure#3383
Upstream draft PR: we-promise/sure#4174 (pull/4174), opened 2026-10-13 from this same branch; same branch, same commit, now head 43d571b07.
Fork-main fix: #432 (fix/424-loan-anchor-start-date into main). This branch is the upstream-shaped port of the same fix, written fresh on the mirror rather than cherry-picked; see "Differences from #432".
Base: mirror/upstream-main at 3e35aaa07 (an exact copy of we-promise/sure main).
A new loan's opening anchor holds the original principal, but Account.create_and_sync dated it on the form's "Opening balance date", which defaults to today minus two years. A loan that started earlier therefore had no recorded balance between its start date and that default, so every chart window opening before the default read $0 and then jumped to the principal.
With this change, when a new loan is created with an original balance and a start date, the opening anchor is dated on the start date. The form always sends an opening balance date, so the server cannot tell the default from a deliberate choice, and the start date wins (owner decision on #424, 2026-10-12).
The defect on upstream main
The defect is reproduced on 3e35aaa07 by the new tests, run before the fix:
- "a new loan's opening anchor is dated on its start date":
Expected: Wed, 15 Jan 2020 / Actual: Sun, 13 Oct 2024 - "a new loan's recorded balance opens on its principal at its start date": first monthly
balance_seriesvalueExpected: 400000 / Actual: 0.0 - "create_and_sync dates a loan's opening anchor on its start date" (model):
Expected: Wed, 15 Jan 2020 / Actual: Sun, 13 Oct 2024 - "a loan started today has its opening anchor today and no reconciliation":
Expected: 1 / Actual: 2valuations
Changes
app/models/account.rb,Account.create_and_sync: wheninitial_balanceis present, the account is a loan and the loan has astart_date, that date replacesopening_balance_datefor the opening anchor, provided it is afterEntry.min_supported_date(30 years ago), whichEntryvalidates. A loan started on or before that day keeps the form's opening date, as onmain, and its schedule still uses its real start date (43d571b07, upstream review comment 4266747155).config/locales/views/loans/en.yml:loans.form.start_date_hintnow says a new loan's balance history starts on this date. No ERB, CSS or JavaScript changed. The ca/de/ru/tr/uk strings still say "leave blank to use the account's opening date", which stays true, so they are left for translators.- Tests in
test/controllers/loans_controller_test.rbandtest/models/account_test.rb.
Differences from #432
Upstream's Loan already validates start_date <= Date.current (loan.rb:34), which the fork's Loan does not. So:
- the fork's "on or before today" bound is dropped here: it is unreachable, because
account.save!raises first. The comment says the fix relies on that validation; - the fork's "a loan with a future start date keeps the form's opening date and today's balance" test (a 422 here) is replaced by "create_and_sync rejects a loan with a future start date and writes no anchor", which pins the validation the fix relies on. Removing the validation turns it red;
- with the bound gone, the fork's single
todaycapture is not needed, so the reconciliation lines are untouched.
Everything else (the guard, the hint text, the other tests and the create_loan helper) is the same as #432. #432 stays as the fork-main fix, because the fork's Loan has no start-date validation and needs the bound.
Blast radius
Who else hits this line, and what do they pass? Re-grepped at 3e35aaa07. create_and_sync has 26 call sites besides its definition:
account.rb: thecreate_from_*call sites (:301,:329,:341,:377,:405,:423,:449,:468) and the crypto-exchange path at:533, which passesaccountable_type: "Crypto"withsubtypeandtax_treatmentonly;qif_import.rb:503;- CoinStats
exchange_portfolio_account_manager.rb:54anddefi_account_manager.rb:68; - Brex
account_flow.rb:178andaccount_flow/setup.rb:127; - the Lunchflow (
:188,:571), Sophtron (:270,:572), Up:366, Akahu:295, Monobank:354, Fio:349, Mercury (:186,:569) and Enable Banking:259controllers; AccountableResource#create:44.
None of the provider or import callers passes initial_balance or a loan start_date. Only AccountableResource#create (the manual forms, reached for loans through LoansController, which permits both) can pass both. "a provider-created loan keeps the default opening date" pins the SimpleFIN loan path.
What else reads what I changed? The anchor date is read by:
Balance::BaseCalculator#calculation_start_date, the forward and reverse calculators andBalance::Materializer: rows now start on the start date;Account#history_start_dateandUI::Account::Chart#period: "All" starts on the start date;Family#oldest_entry_date: the family-wide "All" can start earlier. This is deliberate;- net worth and balance-sheet series: the liability appears from the start date;
Loan#origination_date(start_date || first_valuation&.date || opening_anchor_date): unchanged, because it already prefersstart_date.Loan#original_balanceprefersinitial_balanceand otherwise reads the anchor amount, which does not change;Loan::PayoffChart#actual_series: it starts the recorded line at the later of origination and the first balance row. The first balance row is now the start date, so the recorded line opens at origination instead of two years ago.
OpeningBalanceManager's rule that the anchor precedes the oldest entry cannot trigger, because a new account has no other entries. Family::DataImporter, AccountImport, QifImport and IbkrAccount::Processor call set_opening_balance directly and are unchanged.
Tests
Positive, each failing before the fix (above).
Negative, passing before and after:
- "a new loan keeps its entered current balance": today's valuation and balance row are 399,600 (we-promise/sure#3301);
- "a loan without a start date keeps the form's opening date";
- "a loan with a start date but no original balance keeps the form's opening date";
- "create_and_sync keeps a depository account's opening balance date";
- "a provider-created loan keeps the default opening date";
- "create_and_sync rejects a loan with a future start date and writes no anchor".
Boundary pair for the earliest supported entry date (43d571b07):
- "a loan whose start date is on the earliest supported entry date keeps the form's opening date" errored with
RecordInvalidon5f3ee36cb; the controller test "a loan started on the earliest supported entry date is created with the form's opening date" got a 422 there; - "a loan started the day after the earliest supported date is anchored on its start date" is the other side.
The negative tests post an opening date of today minus three years. OpeningBalanceManager's own default is two years, so a mutant passing nil through would otherwise be indistinguishable.
Mutations
Applied to the committed fix, with each file restored by git checkout HEAD --. The tree was clean afterwards.
| Mutation | Result | Failing tests (examples) |
|---|---|---|
| Remove the override | red, 4 failures | the four positive tests |
Apply to every accountable (account.start_date, no guards) | red, 10 failures | depository keeps its date; loan without a start date; and others |
Drop the initial_balance guard | red, 1 failure | a loan with a start date but no original balance |
Drop the account.loan? guard | survives (equivalent) | Only Loan accepts initial_balance, so the guard cannot change the outcome. It is kept for intent |
Drop the start_date presence guard | red, 5 failures | loan without a start date; depository keeps its date; uses provided opening balance date |
| Reconcile on the start date instead of today | red, 4 failures + 2 errors | a new loan keeps its entered current balance; and others |
Remove Loan's start_date validation | red, 1 failure | create_and_sync rejects a loan with a future start date |
Remove the Entry.min_supported_date guard, or use >= | red, 1 failure + 1 error | the on-boundary model and controller tests |
| Move the bound a day later | red, 1 failure | the day-after test |
Validation
- The two touched files: 115 runs, 431 assertions, 0 failures, 0 errors, 0 skips.
- Full
bin/rails teston43d571b07: 11834 runs, 54595 assertions, 0 failures, 0 errors, 34 skips. (Onecc26bd70: 11793 runs, 0 failures, 0 errors.) bin/rubocop -f githubon the changed Ruby files: no offenses.bin/brakeman --no-pager -q: 0 warnings.- erb_lint and Biome: no ERB or JavaScript changed. No system tests apply.
- Checked against upstream
AGENTS.md(identical to the mirror's copy): logic in the model, controller untouched; Minitest with fixtures and Mocha only forsync_later; i18n for the changed text; commit subject under 72 characters; no migration.
Deliberately left out
- Existing loans: no data migration. An owner can edit the opening balance valuation's date.
- Later edits: the anchor does not move when
start_dateis edited after creation. - A falling recorded line: the recorded balance falls as repayments are recorded; the scheduled line is already drawn from origination by
Loan::PayoffChart.