← Back to Pull requests

Keep a due-day shift from adding a second occurrence to a paid cycle #4380

Open
unknown opened this pull request with 495 lines diff across 6 files
View on GitHub
unknown unknown commented on Oct 21, 2026 08:49 UTC
Author

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 current main (79563708d) kept upstream's version and dropped this PR's own FullyAllocatedError, 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 extended regenerate_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:

  • (a) skip removed: 4 failures;

  • (b) distance only, without the leftover condition;

  • (c) back to rejecting every pair within reach: 1 failure.
  • Results:

  • Full suite on 0c7f8320d: 12,858 runs, 59,393 assertions, 0 failures, 0 errors, 36 skips.

  • RuboCop and Brakeman: clean.

  • No UI markup, migration or JavaScript change.
  • <!-- 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 -->

    Diff: 495 lines (+371 / -124) across 6 files Reviewer: jaysbeekay Review duration: 1368m
    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
    0.47
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.4% (MEDIUM) · Higher = larger blast radius
    47.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 25.3% · Safe to merge
    25.3%