← Back to Pull requests

Check for an existing bill before creating one from a transaction #4375

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 21, 2026 03:06 UTC
Author

Closes #4368

Fork pre-flight: jaysbeekay/sure#524, from the same branch and commit. Its CI runs and reviews, and the fork issue with the triage plan (jaysbeekay/sure#508), are there.

Problem

Create bill on a transaction opens the declare dialog prefilled from that transaction. Saving it never asked whether a bill already covered the payment, so a transaction that was already paying a detected bill got a second bill with the same name, account, amount and day. The second bill's first cycle then sits "due" with Find payment, because the real payment is allocated to the first one, and the Bills totals count the commitment twice.

No unique index can catch it. A declared bill never sets merchant_id, so it lands in idx_recurring_txns_acct_name, while a merchant-keyed detected bill sits in idx_recurring_txns_acct_merchant. DeclaredBill.save also stamps dedup_scope with the amount, and detection writes "".

Mark as recurring on the same panel had the same gap. Transaction#existing_manual_recurring_transaction only looked at manual: true rows with an exact account, merchant or name, and amount, so a detected bill never disabled the button or blocked the POST.

Change

  • RecurringTransaction.covering(entry) is a class method, so it runs inside whatever scope it is called on, and it always pins to the entry's family. It returns the first non-ended series that either:

  • 1. already holds a confirmed allocation for the entry, or
    2. passes the matcher's identity rules: the same currency, the same account (or a series with no account), and not a transfer series. If the series has a merchant, the merchant must match. Otherwise one of the series' known names must equal the entry's normalised name or its merchant's name, which is the name the prefill copies. The amount must also be inside amount_within_variance_band?. That band is signed and inclusive at 2x and at half, so a second price tier can still be created and an inflow never matches a bill.
  • RecurringTransaction#known_names is the series' name plus its matcher_hints["name_aliases"], normalised. Matcher#known_names now memoises it rather than building its own copy, so series names are compared in one place. If #4222 lands, matcher_name goes into that one method.

  • RecurringTransactionsController#new and RecurringTransactions::SmartFillsController#create both prefill through prefill_recurring_from_entry. That method now looks up the covering series among the bills the user can open (accessible_by(Current.user)). When there is one, the dialog shows a DS::Alert naming the bill, with an Open bill link, and the form sits inside a DS::Disclosure labelled "Create a separate bill anyway".

  • The form carries the source transaction as a top-level hidden entry_id, outside recurring_transaction[...], so DeclaredBill and the permitted params are unchanged. The copy of the form under "anyway" also carries allow_duplicate=1. #create re-resolves entry_id through Current.accessible_entries and answers 422 with the warning when a covering series exists and allow_duplicate is absent. That covers a stale dialog or a direct POST. A check built from the posted name could never find a merchant-keyed bill, which is the case reported. Without an entry_id (the plain manual dialog), nothing changes.

  • When the covering series is still a suggestion, the alert says Sure already suggested a bill for this payment and offers Confirm bill beside Open bill. That is the existing recurring_transactions#confirm action, which also backfills the bill's history, so this payment gets linked. A separate bill would become the duplicate the moment the suggestion was confirmed. The button shows only when the user can write the bill (series_writable?, the check ensure_series_writable now calls), so a read-only share sees the warning without a button that would 404. "Create a separate bill anyway" stays below.

  • Mark as recurring uses the same query, family-wide as before, for both the button state and the POST guard. It keeps its hard refusal and gets no "anyway" option. Since a detected or suggested bill now blocks it too, its error, button subtitle and tooltip say "A bill already tracks this payment" instead of naming a manual recurring transaction. The fallback for the database race keeps the old text, because only a manual row can collide there. Transaction#existing_manual_recurring_transaction had no other callers and is removed.

  • New strings are in en.yml only. Other locales fall back to English.
  • Left out on purpose: merging or deleting existing duplicates, detection, the unique indexes, and the assistant's create_bill tool (it has no source transaction). Allocator#learn_from_manual_attach! also builds a series' name list, but it does that to decide whether to learn an alias, not to compare identity, so it is unchanged.

    Screenshots

    Captured by a system test against the test fixtures: before on main at 7917bc1e4, after on this branch. Create bill opened from a transaction that a detected bill, City Water, already covers.

    | Before: the plain prefilled form | After: the warning, with the form folded away |
    |---|---|
    | !Before: the prefilled form, no warning | !After: the warning with Open bill and the folded form |

    | After: the form opened under "Create a separate bill anyway" | After: a suggested bill, with Confirm bill |
    |---|---|
    | !After: the form opened under the disclosure | !After: the suggestion warning with Confirm bill |

    Tests

    All written first and seen failing on main:

  • RecurringTransactionTest, covering:

  • - Positive cases: a detected merchant-keyed bill; a hand-made bill keyed by name; a declared bill named after the entry's merchant; a bill found through a learned name alias; an accountless bill; the bill the entry is already allocated to, when it shares no name or merchant.
    - The band at both edges: 50, 150 and 200 against a 100 bill are covered; 49.99 and 201 are not.
    - Negative cases: another account, another merchant, another currency, the opposite sign (and the matching inflow is covered by the income series), an ended bill, a merchant-keyed bill matched on name alone, a recurring transfer with the same name, and another family's accountless bill when called unscoped.
  • RecurringTransactionsControllerTest:

  • - #new from a covered transaction names and links the bill and carries entry_id and allow_duplicate.
    - #new from an uncovered transaction shows no warning, carries entry_id and has no allow_duplicate.
    - #create against a merchant-keyed covering bill (merchant set, name null), posting only the name, answers 422 and creates nothing.
    - #create with allow_duplicate creates the bill.
    - #create for an uncovered transaction still creates it.
    - #new from a transaction an active bill covers offers no Confirm button.
    - #new from a transaction a suggested bill covers shows the suggestion wording and a POST Confirm link targeting _top, keeps Open bill, and keeps the "anyway" form.
    - A read-only share sees the suggestion warning and Open bill, but no Confirm link.
  • RecurringTransactions::SmartFillsControllerTest: smart fill on a covered transaction still warns and carries entry_id.

  • TransactionsControllerTest: with only a detected (non-manual) bill covering the transaction, the button renders disabled with the new tooltip, the subtitle shows the new text, and the POST creates nothing with the new error. Three existing tests (two manual-duplicate POSTs and the re-rendered show) now expect the new text; the race-fallback test still expects the old one.

  • TransactionTest: the three existing_manual_recurring_transaction tests are removed along with the method. Their cases (account, merchant, currency) are now covered by the covering tests.
  • Mutations, each one broken on purpose and caught by a test:

    | Mutation | Caught by |
    |---|---|
    | Drop the allocation branch | finds the bill an entry is already allocated to |
    | Drop the merchant's name from the entry's names | finds a declared bill named after the entry's merchant |
    | Drop name_aliases from known_names | finds a bill through a learned name alias |
    | Band ratio 1.99 (narrower) | variance band inclusive at both edges |
    | Band ratio 2.02 (wider) | variance band inclusive at both edges |
    | Drop the band | both edges; opposite sign; the two existing "allows a second manual recurring transaction ... different amount" tests |
    | Drop the ended filter | ignores an ended bill |
    | Drop the account filter | ignores a bill on another account |
    | Account must match exactly (no accountless) | finds an accountless bill from any account |
    | Ignore the merchant | does not match a merchant-keyed bill on name alone; ignores a bill for another merchant |
    | Drop the currency filter | ignores a bill in another currency |
    | Drop the family pin | never returns another family's bill |
    | Drop the transfer filter | ignores a recurring transfer |
    | Drop the #create guard | create refuses a second bill for a merchant-keyed bill |
    | Ignore allow_duplicate | create makes the separate bill when asked to |
    | No covering series in the prefill | new names that bill; smart fill still warns |
    | Old lookup in the mark_as_recurring POST guard | mark_as_recurring refuses a transaction a detected bill covers |
    | Old lookup for the button state | the same test (the disabled-button assertion) |
    | No hidden entry_id | new (both); smart fill still warns |
    | No hidden allow_duplicate | new names that bill |
    | Confirm offered without the writability check | read-only share is not offered the confirm |
    | Confirm offered for any covering bill, not only a suggestion | new names that bill (no Confirm link) |
    | Suggestion shown with the generic title | suggested bill offers to confirm it; read-only share |
    | Confirm link as GET | suggested bill offers to confirm it |
    | Old text in the mark-as-recurring error | detected bill refuses; manual duplicate; same-name duplicate |
    | Old text in the button subtitle only | detected bill refuses (subtitle assertion) |
    | Old text in the button tooltip only | detected bill refuses (tooltip assertion) |

  • bin/rails test: 12,495 runs, 57,755 assertions, 0 failures, 0 errors, 36 skips

  • bin/rubocop and erb_lint on the changed files: no offenses

  • bin/brakeman: no warnings

  • No JavaScript changed, so Biome does not apply.
  • Since the pre-flight

  • c9e316f35: merged current main. #4222 added matcher_name, the detected name kept on rename, to the matcher's names. The model's known_names, which the matcher and the existing-bill check share, now includes it.

  • 54a7b77ae: a test that a renamed bill still covers its source transaction. Dropping matcher_name from known_names fails it.

  • Full suite: 12,875 runs, 0 failures.
  • <!-- This is an auto-generated comment: release notes by coderabbit.ai -->
    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

  • New Features

  • - When creating a bill from a transaction that’s already covered by another bill, you’ll see which bill tracks it and can open it or confirm a suggested bill when available.
    - You can still create a separate bill for a covered transaction through an explicit option.
  • Bug Fixes

  • - Transactions already covered by a recurring bill are identified consistently, and the “Mark as Recurring” action is disabled for them. Attempts to mark a covered transaction now show a clear alert.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: jaysbeekay Review duration: 1711m
    All Vouch review confidence checks passed
    Residual risk 0.30 = Change Risk (0.52) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    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%
    42%
    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.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #4375"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% 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: 52% (MEDIUM) · Higher = larger blast radius
    52%
    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: 43.0% · Lower = rushed or shallow review
    43.0%
    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: 29.7% · Safe to merge
    29.7%