Keep a due-day shift from adding a second occurrence to a paid cycle #4380
All Vouch review confidence checks passed
Residual risk 0.25 = Change Risk (0.47) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.47 · 495 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.371
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #4380"
general
weight: 0.35
jaysbeekay
Closes #4300
Fork pre-flight: jaysbeekay/sure#485, from the same branch. It passed the fork's CI and its light and full Gatekeeper reviews there (full review: APPROVE). The fork issue with the triage plan is jaysbeekay/sure#468.
Scope change since the pre-flight: #4376 has since landed the second half of this fix: a clear message when a used-up transaction can't pay a bill, via
EntryExhaustedError. Merging currentmain(79563708d) kept upstream's version and dropped this PR's ownFullyAllocatedError, its locale keys and its tests. So this PR now changes only the occurrence generator.Problem
When detection moves a bill's due day by a day or two,
regenerate_future!keeps the paid or past-due row of the current cycle, as it should, and then generates a row on the new day as well. The upsert is keyed on(series, original_due_on), so the new day doesn't collide with the old one. The cycle shows as both paid and overdue.Change
OccurrenceGenerator#upsert_window: each row the current schedule no longer produces (a leftover of the previous rule, which already is its cycle) stands in for ONE generated occurrence: the nearest within half a cycle, with ties going to the earlier date. That occurrence is skipped.- Half a cycle is
Matcher#window_for's reach (14 days for monthly, 7 for semimonthly), so generation and matching agree.- Only one occurrence per leftover is skipped (
0c7f8320d, from review). So a semimonthly bill on the 1st and 5th, whose paid 1st moves to the 2nd, still gets its 5th.- The nearest-occurrence search uses the same widened window the leftover rows are read from, so a nearest occurrence just outside the window isn't mistaken for a farther one inside.
- A row the current schedule produces never counts, so two close days of one schedule both stay.
- Existing rows are never deleted or moved.
Callers of
upsert_window:generate!(create callback,GenerateRecurringOccurrencesJob,Pipeline,BillsController): in steady state no nearby row is a leftover, so nothing changes.regenerate_future!: the path fixed.backfill!: the same defect in past windows, now skipped too.Known boundary (owner decision on jaysbeekay/sure#468): moving to a longer cadence (weekly to monthly) can let a paid weekly row stand in for the first monthly occurrence of that one cycle. Telling it apart would need the previous cadence, which the generator doesn't store.
Validation
Fail first: against
upstream/main's code, the earlier shift (paid), the earlier shift (unpaid past-due) and the later shift tests all failed. The semimonthly close-days test added from review failed against the previous version of this fix with "Expected [Oct 1, Nov 2, Nov 5, Dec 2, Dec 5] to include Oct 5". The extendedregenerate_future!test (a 15-day move still gets its own row) and the semimonthly horizon test pass as guards.Mutations, each caught by a named test:
Results:
0c7f8320d: 12,858 runs, 59,393 assertions, 0 failures, 0 errors, 36 skips.<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
<!-- coderabbit-ai-generated:v1 -->
Summary by CodeRabbit
Bug Fixes
Recurring transaction occurrences now remain consistent when a schedule’s due date changes. Existing past-due entries are accounted for, preventing duplicate occurrences while retaining distinct dates on closely spaced monthly and semimonthly schedules.
* Repeated occurrence generation no longer creates duplicates for dates already represented by existing transactions, whether those entries have been paid or remain unpaid.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->