Skip to content

fix(fx): leave only the truly borrowed part of a loan repayment to reconcile by hand - #464

Merged
GeiserX merged 2 commits into
mainfrom
fix/overdraft-loan-cr463
Nov 18, 2026
Merged

GeiserX merged 2 commits into
mainfrom
fix/overdraft-loan-cr463

Conversation

@GeiserX

@GeiserX GeiserX commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Problem

CodeRabbit reviewed #463 after it shipped in v0.66.12. It found that the loan note could leave too much to reconcile by hand. This affects only the default method, «Criterio DGT (multidivisa)».

When shares bought on a loan are sold, they give back that loan's dollars. Those dollars can repay another loan. The engine took them out of their own loan's still-borrowed count only when that repayment booked 0. When it booked a gain or a loss, the count stayed too high.

Later, a funding paid for the rest of that loan. Its dollars still counted as borrowed, so their repayment of a third loan was placed outside 1633/1637. In the reviewer's case the note said 35 € of 75 € were in the boxes, so 40 € were left to reconcile by hand. The right split is 55 € in the boxes and 20 € by hand.

The docs also described the hand-reconcile case wrongly. They said another loan's dollars stay outside the boxes until your own currency pays that loan. The code actually counts a loan as paid by any repayment inside 1633/1637, and a repayment with a result is always inside.

Fix

  • Every loan repayment now takes the borrowed dollars it spends out of their loan's count, whatever it books, borrowed dollars first. Only a repayment that books 0 still counts that money outside the boxes.
  • docs/casillas.md, docs/traza-fx.md and the Both FX methods: a buy paid by a later day's sale (overnight overdraft) misses the economic total #449 section of CLAUDE.md now say what the code does:
    • a repayment with a gain or a loss is always in 1633/1637, and it pays the loan it repays;
    • another loan's dollars stay outside only for the part of that loan no repayment in 1633/1637 has paid yet, also when your own currency already paid another part.
  • New tests pin 4 cases: PASS (the reviewer's case), PASS-ZERO (the same events at gain 0), P2 and P2-ZERO.

What users see

  • No tax box, stock or FX figure changes. No committed fixture changes in any FX method. «Tradicional con divisa» and monodivisa do not change at all.
  • Only the in-box split of the hand-reconcile note (boxGain/boxLoss) and the hint text that prints it can change. In the default method this happens only when the Cash Report passes the loan gate and one loan's dollars repay another at a nonzero result.
  • Over 3,009 generated loan runs, only PASS changed: boxGain went from 35.00 to 55.00. One handwritten chain of 4 loans (CHAIN4) moved from 39.00 to 51.00 for the same reason. Both were checked by hand.
  • Migration: none.

Left on purpose

  • On a day a loan opens, another loan's dollars can cover its slices without leaving their own loan's count. No test or generated case reaches this, and it can only send more to hand reconciliation, never less.
  • A gain or loss on one pass-on counts that loan as paid, and this changes how a later zero-gain repayment of another loan is classified. P2 and P2-ZERO pin it. Changing it would move PASS, CHAIN and MIXED, so it needs its own decision.

Checks

  • Tests: with the old code back in place, PASS goes red (35 instead of 55).
  • Suite: vitest, typecheck, typecheck:tests, lint and build:web all pass.
  • Byte-identity: 0 differences against the base over the committed fixtures in all three FX methods.

Follow-up to #463, from the CodeRabbit review of its exact diff on the fork mirror.

…ther loan with a result

The default kept a loan's borrowed-principal count (borrowedLeft) too high
when its returned principal repaid another loan at a nonzero result: only a
gain-0 row took the spent units out. A later pass-on of the paid-for rest
then went outside 1633/1637, and the loan note said `_manual` with too small
a boxGain (PASS: 35 instead of 55 of 75 EUR). Tax figures never changed.

Every repayment now retires the borrowed units it spends from their loan,
borrowed units first; only a gain-0 row still counts them outside the boxes.
docs/casillas.md, docs/traza-fx.md and the #449 Hard Trace now say that a
repayment with a result is always in 1633/1637, and that another loan's
principal is outside only for the part of its loan your currency has not
paid, also when it paid another part (PR #463 review).
…borrowed principal pays it

docs/casillas.md and docs/traza-fx.md told the user that a loan's principal
stays borrowed until currency of their own pays the loan. The code counts a
loan as paid by any repayment in 1633/1637, also a row with a result paid
with another loan's borrowed principal (P2: L2's principal repays L1 with a
5 EUR result, then L1's principal repays L3 inside the boxes, the plain note
with gain 75; only at gain 0, P2-ZERO, does L1 stay borrowed: `_manual`,
boxGain 15 of 65). The docs now say that, CLAUDE.md #449 pins P2 and
P2-ZERO, and the PASS-ZERO test no longer claims the same split as PASS
(PR #463 review).
@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@GeiserX
GeiserX merged commit cc724a9 into main Nov 18, 2026
5 of 6 checks passed
@GeiserX
GeiserX deleted the fix/overdraft-loan-cr463 branch November 18, 2026 22:41
@codecov

codecov Bot commented Nov 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.07%. Comparing base (0fda3db) to head (609340d).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #464   +/-   ##
=======================================
  Coverage   94.07%   94.07%           
=======================================
  Files          64       64           
  Lines        8382     8383    +1     
  Branches     2904     2905    +1     
=======================================
+ Hits         7885     7886    +1     
  Misses        409      409           
  Partials       88       88           
Files with missing lines Coverage Δ
src/engine/fx-fifo.ts 99.39% <100.00%> (+<0.01%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

1 participant