fix: preserve simplified settlement plans while payments are recorded #8

Merged
exler merged 2 commits from fix/stable-settlement-plan into main 2026-11-02 09:10:31 +01:00
Collaborator

Summary

Preserve the simplified repayment route as suggested payments are recorded. Full payments remove only their transfer; partial payments reduce only that transfer. Reloads and other members see the same persisted plan.

Previously, recording Bartek → Kacper 196.71 PLN rebuilt the greedy route, rerouting Wojtek and Pawel and increasing the eventual transfer count. The regression reproduces the original seven-person balances.

Implementation

  • Persist residual transfers, balance snapshot, base currency and last consumed settlement ID in a per-group SettlementPlan.
  • Consume on-route payments and check the expected balances against the live ledger. Recompute when the plan no longer explains current balances (expense/split changes, member merges, FX, off-route payments, overpayment).
  • Reset on a settlement-method change through group settings. Exact settlement is unchanged.
  • Existing groups initialize from current residual balances; settled history is never revived.

Verification

  • Behavioral RED on original code, then GREEN for the exact repro (recorded log).
  • Reviewer run: uv run pytest tests/test_settlement_plan.py tests/test_settlement_method.py tests/test_expenses.py -q — 108 passed.
  • Implementation run: full uv run pytest -q — 362 passed, browser tests included, none skipped.
  • Ruff check/format on changed files, Django checks and migration-drift check passed in implementation run.
  • Reviewer inspected complete diff, git diff --check and SQLite migration SQL.
  • Existing mypy debt: 82 errors, same count as main; no new-line errors reported. Existing missing-staticfiles-directory warnings remain.

Migration and risks

Migration only creates a new table, without altering expenses/payments. Lazy initialization, no backfill. Balance reads take a short IMMEDIATE SQLite transaction (existing configured mode); concurrency relies on serialization, with no dedicated concurrency test added. Off-route/overpaid/other-currency payments and changed balances intentionally rebuild the route. No UI or dependency changes. Not deployed to Birdie.

Final CI verification

Initial CI stopped at the committed-CSS gate: Tailwind interpreted the word shrink in a new Python docstring as a utility class. Reworded it to reduce with no behavioral change. Post-change settlement-plan regression suite: 17 passed. Bun is unavailable in the reviewer environment; the locked CSS build passed in CI.

Final head ad02e1e62f23127d64f724cad0555e151f065a12: test job passed in run 55, including CSS consistency, Django/migration checks and the full browser-inclusive test suite. Image publication is intentionally skipped on pull requests. Not merged or deployed.

## Summary Preserve the simplified repayment route as suggested payments are recorded. Full payments remove only their transfer; partial payments reduce only that transfer. Reloads and other members see the same persisted plan. Previously, recording Bartek → Kacper 196.71 PLN rebuilt the greedy route, rerouting Wojtek and Pawel and increasing the eventual transfer count. The regression reproduces the original seven-person balances. ## Implementation - Persist residual transfers, balance snapshot, base currency and last consumed settlement ID in a per-group SettlementPlan. - Consume on-route payments and check the expected balances against the live ledger. Recompute when the plan no longer explains current balances (expense/split changes, member merges, FX, off-route payments, overpayment). - Reset on a settlement-method change through group settings. Exact settlement is unchanged. - Existing groups initialize from current residual balances; settled history is never revived. ## Verification - Behavioral RED on original code, then GREEN for the exact repro (recorded log). - Reviewer run: `uv run pytest tests/test_settlement_plan.py tests/test_settlement_method.py tests/test_expenses.py -q` — **108 passed**. - Implementation run: full `uv run pytest -q` — **362 passed**, browser tests included, none skipped. - Ruff check/format on changed files, Django checks and migration-drift check passed in implementation run. - Reviewer inspected complete diff, `git diff --check` and SQLite migration SQL. - Existing mypy debt: 82 errors, same count as main; no new-line errors reported. Existing missing-staticfiles-directory warnings remain. ## Migration and risks Migration only creates a new table, without altering expenses/payments. Lazy initialization, no backfill. Balance reads take a short IMMEDIATE SQLite transaction (existing configured mode); concurrency relies on serialization, with no dedicated concurrency test added. Off-route/overpaid/other-currency payments and changed balances intentionally rebuild the route. No UI or dependency changes. **Not deployed to Birdie.** ### Final CI verification Initial CI stopped at the committed-CSS gate: Tailwind interpreted the word `shrink` in a new Python docstring as a utility class. Reworded it to `reduce` with no behavioral change. Post-change settlement-plan regression suite: **17 passed**. Bun is unavailable in the reviewer environment; the locked CSS build passed in CI. Final head `ad02e1e62f23127d64f724cad0555e151f065a12`: test job **passed** in [run 55](https://git.marut.network/exler/spendbee/actions/runs/55), including CSS consistency, Django/migration checks and the full browser-inclusive test suite. Image publication is intentionally skipped on pull requests. Not merged or deployed.
fix(balances): keep the simplified settlement plan stable as payments are recorded
Some checks failed
Test and push Docker image / test (pull_request) Failing after 1m43s
Test and push Docker image / push (pull_request) Has been skipped
aac744f462
Recording one suggested payment used to regenerate the greedy plan, which could
reroute every other transfer. The plan is now persisted per group (SettlementPlan)
and reused while it still explains the current balances: a base-currency payment
along a suggested route shrinks only that transfer and removes it once fully paid.
Anything else that moves balances (expense/split/member changes, off-route,
overpaid or other-currency payments, exchange rates, base currency) is detected
centrally on read and recomputes the route from the current residual balances.
Switching the settlement method starts a fresh route.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
chore: avoid a Tailwind candidate in settlement plan documentation
All checks were successful
Test and push Docker image / test (pull_request) Successful in 2m8s
Test and push Docker image / push (pull_request) Has been skipped
ad02e1e62f
exler merged commit c1cf2fd373 into main 2026-11-02 09:10:31 +01:00
exler deleted branch fix/stable-settlement-plan 2026-11-02 09:10:32 +01:00
Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
exler/spendbee!8
No description provided.