Check for an existing bill before creating one from a transaction #4375
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
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.388
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #4375"
general
weight: 0.35
jaysbeekay
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 inidx_recurring_txns_acct_name, while a merchant-keyed detected bill sits inidx_recurring_txns_acct_merchant.DeclaredBill.savealso stampsdedup_scopewith the amount, and detection writes"".Mark as recurring on the same panel had the same gap.
Transaction#existing_manual_recurring_transactiononly looked atmanual: truerows 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_namesis the series' name plus itsmatcher_hints["name_aliases"], normalised.Matcher#known_namesnow memoises it rather than building its own copy, so series names are compared in one place. If #4222 lands,matcher_namegoes into that one method.RecurringTransactionsController#newandRecurringTransactions::SmartFillsController#createboth prefill throughprefill_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 aDS::Alertnaming the bill, with an Open bill link, and the form sits inside aDS::Disclosurelabelled "Create a separate bill anyway".entry_id, outsiderecurring_transaction[...], soDeclaredBilland the permitted params are unchanged. The copy of the form under "anyway" also carriesallow_duplicate=1.#createre-resolvesentry_idthroughCurrent.accessible_entriesand answers 422 with the warning when a covering series exists andallow_duplicateis 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 anentry_id(the plain manual dialog), nothing changes.recurring_transactions#confirmaction, 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 checkensure_series_writablenow calls), so a read-only share sees the warning without a button that would 404. "Create a separate bill anyway" stays below.Transaction#existing_manual_recurring_transactionhad no other callers and is removed.en.ymlonly. Other locales fall back to English.Left out on purpose: merging or deleting existing duplicates, detection, the unique indexes, and the assistant's
create_billtool (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
mainat7917bc1e4, 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:-
#newfrom a covered transaction names and links the bill and carriesentry_idandallow_duplicate.-
#newfrom an uncovered transaction shows no warning, carriesentry_idand has noallow_duplicate.-
#createagainst a merchant-keyed covering bill (merchant set, name null), posting only the name, answers 422 and creates nothing.-
#createwithallow_duplicatecreates the bill.-
#createfor an uncovered transaction still creates it.-
#newfrom a transaction an active bill covers offers no Confirm button.-
#newfrom 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 carriesentry_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 threeexisting_manual_recurring_transactiontests are removed along with the method. Their cases (account, merchant, currency) are now covered by thecoveringtests.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_aliasesfromknown_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
#createguard | 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_recurringPOST 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 skipsbin/rubocopanderb_linton the changed files: no offensesbin/brakeman: no warningsSince the pre-flight
c9e316f35: merged currentmain. #4222 addedmatcher_name, the detected name kept on rename, to the matcher's names. The model'sknown_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. Droppingmatcher_namefromknown_namesfails it.<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
<!-- coderabbit-ai-generated:v1 -->
Summary by CodeRabbit
- 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.
- 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 -->