Offer actual/360 as a loan's day-count basis

Draft
JA
2mo ago
+172-13

Summary

Closes #284 on the fork.

A lender charging on actual/360 (US commercial loans, some consumer loans) couldn't be represented. The nearest basis left every period about 1.4% low (5/365).

661852dda and a6af757e4, 2026-10-12: the rollback now refuses rather than rewriting loans (nightly review BLOCKER; owner's instruction: "go with your recommendations").

  • Before: down moved every actual/360 loan to actual/365 so the narrower constraint would accept it. That changed those loans' interest and lost the basis their contracts name, and a later re-upgrade could not tell which loans had it.
  • Now: down first counts the loans on actual/360.
    • If there are any, it raises ActiveRecord::IrreversibleMigration and changes nothing. The message names how many loans are on actual/360 and says to set each one's basis before rolling back again.
    • With none, it restores the three-value constraint as before.
  • Tests: test/migrations/allow_actual_360_day_count_on_loans_migration_test.rb is rewritten, because the old test asserted the rewrite.
    • "down refuses while a loan accrues on actual/360 and leaves every loan's basis as it was" failed first, because nothing was raised.
    • "down restores the three-value constraint when no loan accrues on actual/360" is the negative case and passes both before and after.
  • Mutations caught: the guard removed, refusing always, and counting the wrong basis.
  • a6af757e4 merges #397's matching change (031824d3e), so the 30/360 migration on this branch refuses the same way. Both migration tests pass here: 4 runs, 0 failures.
  • Validation: full bin/rails test on a6af757e4 gave 12,384 runs and 1 failure, the known Ruby 3.3 Provider::OpenaiTest string. bin/rubocop reports no offenses, and bin/brakeman 0 warnings.
  • Propagation: merged into claude/loans-integration-base, then #405 and #404 (see #404).

The change itself:

  • Loan::InterestAccrual gains :actual_360: the actual days in the period over a 360-day year.
  • The fall-through is closed. day_count_denominator returned 365 for anything but actual_actual, so a basis added to the list without its own branch would have accrued silently as actual/365. That's the fork's version of the accrual_factor fall-through #284 names upstream. It now has one branch per basis and an else raise.
  • chk_loans_day_count_convention is replaced with a four-value constraint. On rollback, down refuses while any loan is on actual_360 (above), and otherwise restores three values.
  • Labels and the hint in every locale that carries them (en, de, ru, tr, uk). en is load-bearing: t(... raise: true) raises only if every locale lacks the key.
  • Contract row C2 keeps its defect (actual/365 must not use 366 in a leap year); its anchor in config/loan_contract_mutations.yml moves to the new when :actual_365 branch.

Scope and dependencies

  • Issue: #284. Upstream counterpart: we-promise/sure discussion #3924; the upstream PR rides after we-promise/sure#3639, as the issue says. This is the fork side.
  • Parent PR/branch, if stacked: stacked on #397 (claude/loans-188-thirty-360, merged in), which is stacked on #396. Retarget as they merge.
  • Intentionally out of scope: 30/365, 30E/360, provider mapping, any change to the default.

Blast radius. InterestAccrual has one caller, Simulator#accrue_daily_period. The three existing bases take the same denominators as before, and the golden master doesn't move. Loan::DAY_COUNT_CONVENTIONS, the validation and the form read the accrual's list.

Plan deviation, with reason. The plan said two existing tests use actual_360 as their unsupported example and need rewriting to actual_366. That was true of up/loan-day-count-convention. On the fork no test named actual_360; #397 already moved the fork's two "unsupported" tests to actual_366.

Evidence checklist

  • Each issue in this PR has its own acceptance criteria and no unrelated issue work is included.
  • Contract rows touched by this change name existing tests.
  • Any gate test has an observed failing mutation transcript attached.
  • Fixtures used as evidence assert internal consistency.
  • Financial calculations use BigDecimal and documented rounding points.
  • De-identification uses token-shape allowlists; no personal data is included.
  • Files changed, decisions, tests/runtime, risks, and next unblocked issue are documented below.

test/models/loan/actual_360_test.rb and two loans_controller_test.rb cases, red on the base first:

TestOn the base
actual/360 charges the period's days over 360 (100,000 × 6% × 31/360 = 516.67)ArgumentError
actual/360 is not actual/365ArgumentError
a leap February differs on each actual basis: 483.33 / 476.71 / 475.41 (29/360, 29/365, 29/366)ArgumentError
a basis with no denominator raises rather than defaulting"nothing was raised": the fall-through
an unsupported basis passed to the accrual still raisespasses both
a loan saves actual_360; the database refuses actual_366RecordInvalid
saves the actual/360 basis from the form; the form offers itfail

Mutations, each restored from a copy:

M1 ACTUAL_360_DAY_COUNT = 365        -> 3 failures (the value, leap-February and not-actual/365 tests)
M2 else branch returns 365 again     -> "a basis with no denominator raises rather than defaulting"

Contract gate (bin/rails "loans:verify_contract_mutations[C1,C2]", on a clean tree):

C1   baseline=pass  mutated=failed    the accrual denominator stops being 365, ...
C2   baseline=pass  mutated=failed    leap years silently switch to a 366 denominator ...
Verified 2 contract rows

Migration on the scratch fork database: migrates, rolls back, re-migrates. db/schema.rb changes only the constraint line and the version stamp.

Rollback with rows on the new bases:

  • Before 1c87b65, the restored constraint rejected the rows (PG::CheckViolation, as recorded on #397).
  • 1c87b65's down moved them to actual_365 instead.
  • Since 661852dda, it refuses, with the count, and leaves every loan as it was.

Validation

  • Loan, loans/accounts controller, helper and loan component suites: 691 runs, 0 failures. After merging #397's review fixes: the day-count, interest-accrual and loans-controller tests, 68 runs, 0 failures.
  • bin/rubocop on changed files: no offenses. Contract mutation manifest test: green.
  • Full suite on a6af757e4: 12,384 runs, 1 failure (the known Provider::OpenaiTest Ruby-version string, unrelated). Earlier, on the integration branch: 12,468 runs, the same 1 failure.
  • Checked against upstream AGENTS.md.

Review findings

  • cubic, rollback fails once a loan stores actual_360: valid. Fixed in 1c87b65 by moving such loans to actual/365, which the nightly review then rightly called a data-integrity blocker. Superseded by 661852dda, under which the rollback refuses instead.

  • cubic, translate the uk hint: declined. The uk hint and labels were already English on main, as are ru's and tr's. This PR keeps them in step with en; translating the loan form is a separate locale pass. The hint now also carries #397's correction for 30/360 part periods.

  • Gatekeeper light review (comment 6092368769): DRAFT_ON_TRACK, no material findings. Its observations:

    • The rollback rewrite: it was disclosed then, and is now replaced by a refusal (above).
    • The uk/ru/tr hint parity: covered by the decline above.
    • The stacked-parent retarget (#397, then #396): noted under Scope.
  • "code master" review (comment 6106183224), hold, on 1c87b65: nothing confirmed. One test gap (F5) is closed in fc6f2c70e, pushed on the owner's instruction (2026-10-11: "Push the held commits; go with your recommendations").

    • F1, a rollback makes old code accrue actual/360 as actual/365: declined. The old code never reaches its 365 fall-through. normalize_day_count_convention raises first: run on the base file, "actual_360" gives unsupported day-count convention: "actual_360" (expected one of actual_365, actual_actual, thirty_360). The base Loan validation also refuses to save such a loan. So a rollback without down is a loud error on those loans, not a silent under-accrual. Compose deploys run db:prepare in one container; the Helm chart rolls pods, but helm rollback wouldn't hide it either.
    • F2, down's raw UPDATE skips callbacks, so cached schedules go stale: moot since 661852dda. down no longer updates any loan.
    • F3, a window with no constraint: declined; the review's premise is wrong.
      • The migration has no disable_ddl_transaction!, so remove and add run in one transaction. A concurrent writer waiting on the DROP CONSTRAINT lock timed out rather than writing.
      • add_check_constraint does validate existing rows: on a scratch database with a planted 'bogus' row, db:migrate:up failed with PG::CheckViolation ... is violated by some row, and the whole migration rolled back.
    • F4, day_count_denominator raises on thirty_360: declined. It has one caller (interest_accrual.rb:61), behind the thirty_360 early return at :56-58, and normalize limits the input to the four bases. Reaching the raise needs a basis added without its own branch, which is exactly the silent fall-through this PR closes.
    • F5, no test of the down path: test gap; closed in fc6f2c70e, and the test is rewritten for the refusal in 661852dda (above).
    • F6, ru, tr and uk hints in English: already decided (the cubic bullet above).
  • Nightly review (comment 6120538925), NOT READY TO MERGE, on fc6f2c70e.

    • BLOCKER, the rollback overwrites the contract basis: confirmed, and fixed in 661852dda with the second option it named: the rollback fails with a clear error while affected loans exist and overwrites nothing (top of this description).
  • Nightly review (comment 6134304134), READY WITH CONDITIONS, on a6af757e4. No blocking findings. MEDIUM, merge #397 first, retarget and rerun: agreed, and it is already the plan. #397 has its own READY TO MERGE nightly review and awaits the owner's approval. Once it merges, this PR is retargeted to main, main is merged in, and the migration and loan suites run again at that head. Nothing to change now.

  • Nightly review (comment 6157140552), READY WITH CONDITIONS, on 88ebb1973. No blocking findings. MEDIUM, merge #397 first, retarget to main, rerun both day-count migration tests and the loan suite on the merge head: agreed, and it is the plan, as for the previous nightly. Interim evidence: the integration branch #404, refreshed on 2026-10-15 to 8d918f96d, carries this PR over current main (8d4842986) plus #432. Its full bin/rails test gives 12,716 runs with no code failure. The five failures are environmental: the known Provider::OpenaiTest Ruby 3.3 vs 3.4 string, and four Anthropic settings tests that read the container's ANTHROPIC_BASE_URL and pass with it unset (89 runs, 0 failures). Nothing to change now.

Review responses are recorded here rather than as thread replies, because replies posted from this session get an assistant footer appended, which this repository's attribution rule forbids.

Risks, blockers, and follow-up

  • Rollback refuses while any loan is on actual/360 (and, through #397, on 30/360). Whoever rolls back has to choose those loans' basis first; the migration will not choose it for them.
  • Next: the upstream PR after we-promise/sure#3639, per the issue.

Generated by Claude Code