Harden refund links against purchase state drift (follow-up to #3428)

Draft
JJ
21d ago
+2001-161

Stacked on #3428 — do not merge before it. The branch is cut from that PR's head (ad9c4dcb3), so the diff against main shows all of #3428 as well. Once #3428 merges, this collapses to the three commits' worth of changes described below. Draft until then.

This carries the follow-up items from the review of #3428. That PR is good to merge on its own; none of these block it.

Problem

Transaction::Refundable#valid_refund_relationship re-validates the purchase on every save of the refund, and refundable_purchase? reads the purchase's live state (standard?, positive amount, not pending, not a split parent, not excluded). So a change to the purchase could leave the refund permanently unsavable — any later edit to the refund fails with "Refund of is invalid", and Family::DataImporter#restore_refund_links aborts the entire archive import.

#3428 closed the exclusion door with Entry#cannot_exclude_refund_linked_purchase, which is the right guard, but two ways in remain:

  • Entry.auto_exclude_stale_pending writes through update_all, which bypasses validations entirely.
  • refundable_purchase? also requires !pending?, and nothing guards the pending flag — a provider re-sync writing extra[provider]["pending"] = true onto a linked purchase reopens the same failure.

Changes

1. Validate the link when it moves, not forever after. The purchase check now runs only when refund_of_id changes or the refund's kind changes. Establishing a link to an unlinkable purchase is still rejected, and dropping refund classification while a link remains is still rejected — but a purchase that drifts afterwards no longer bricks the refund. Drift belongs at the source: the guards on exclusion, splitting, transfers and trade conversion are what hold the invariant, and a new one belongs there rather than in the refund's own validation.

2. A stale archived pairing no longer fails the whole import. restore_refund_links skips a link whose purchase is not refundable_purchase?, logs a warning, and imports the refund unlinked — instead of raising RecordInvalid and taking every other record in the archive with it. This is distinct from a missing purchase, whose strict-mode rejection (added in #3428) is deliberate and unchanged.

3. A refused edit in the transaction drawer now says why. TransactionsController#update's failure branch rendered :show at 422 into a template with no error area, so the auto-submitting exclude toggle just snapped back unexplained. It now renders the entry's errors in a DS::Alert and calls restore_attributes first, so the form shows the state that is actually stored rather than the rejected assignment. This also covers the pre-existing cannot_unexclude_split_parent case.

docs/llm-guides/refunds.md records the new validation and import semantics.

Tests

Four added, covering each change:

  • a purchase excluded via update_column (the update_all path) leaves its refund editable;
  • a purchase that is not linkable still cannot be linked in the first place;
  • dropping refund classification while a link remains is still rejected;
  • an archive whose purchase is no longer linkable imports every record, with the refund unlinked;
  • the drawer returns 422, keeps the stored state, and renders the reason.

Two items deliberately not changed here

Rollover carries refunds forward as extra envelope capacity — needs a product decision, not a patch. Budget::RolloverCalculator#leftover_for computes [budgeted + incoming - actual, 0].max with actual taken raw from budget_category_actual_spending, which is now negative for a refund-dominated category. A $1000 envelope with −$800 actual rolls $1800 into the next month, and incoming feeds the month after, so it compounds. That is defensible envelope accounting, but the purchase was budgeted in an earlier month — if that month's surplus was already rolled or forfeited, the money is credited twice. Clamping actual at 0 there is a one-line change; whether it should be clamped is a call for whoever owns budgeting, so I have not made it. Not urgent either way: rolled_over_amount is derived and recomputed in a full forward pass whenever a budget is bootstrapped or an allocation changes, so changing the formula later self-heals historical values.

The split-child import asymmetry is moot, not fixed. The review noted that import_split_lines! records a refund link only when the archive value is present, while the parent path also handles transaction.refund_of_id.present? so a replay can clear a link. Making the split path symmetric turns out to be a literal no-op: Entry#split! unconditionally creates new children, so a replayed split archive never reuses a child and transaction.refund_of_id is always nil there. The real question underneath is that replaying an archive containing split lines duplicates the children — a separate, pre-existing importer issue, out of scope here. I wrote the symmetric version, confirmed it changed nothing, and reverted it rather than ship a comment implying otherwise.

Validation

Ruby syntax checks pass on every changed file. I could not run the suite or the linters in my environment (gems are not installed there), so CI is the first real execution of these tests — treat the test expectations as unverified until it goes green. The ERB change is a straight DS::Alert render matching the alert idiom already in that template.

🤖 Generated with Claude Code

https://claude.ai/code/session_018dKQjZ7uBz8VyTC3JTXDNn


Generated by Claude Code