← Back to Pull requests

Fill a bill's blank category from its transactions #4374

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

Closes #4363

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

Problem

A bill's category was copied from its transactions only once, when detection first created the series. Transactions categorised later never reached it, so the bill kept "No category". The two manual routes from a transaction to a bill never set the category at all:

  • "Mark as recurring" (RecurringTransaction.create_from_transaction) built the bill without a category_id.

  • "New bill" prefilled from an entry lost it in four places. The prefill didn't set it, the New dialog had no category field, the params didn't permit it, and DeclaredBill#build didn't assign it.
  • Change

    The rule is fill, never overwrite. A category already on the bill is left alone, and income series stay uncategorised, as they are at creation. When the transactions carry mixed categories, the most common one wins, which is the existing Classifier#modal_category_id rule.

  • Identifier#update_claimed_series fills a blank category from the cluster's most common one. The race-recovery branch calls the same method, so it is covered too. Classification runs only when the series can actually be filled.

  • Identifier#update_manual_recurring_transactions does the same from its matched entries. The batched entry query now selects transactions.category_id beside merchant_id, so no per-series query is added.

  • create_from_transaction takes the transaction's category for an expense.

  • New bill from an entry:

  • - The prefill sets the category for an outflow.
    - The New dialog shows the existing DS::CategorySelect for a bill. Income doesn't get it.
    - :category_id is permitted.
    - DeclaredBill#build assigns it unless the bill is income.
    - Ownership is checked by the existing category_belongs_to_family validation, so another family's category gives a 422.
    - Smart fill shares the prefill, so its dialog keeps the transaction's category too. Its suggested category is still dropped, as before.
  • config/brakeman.ignore: the existing entry for new_recurring_transaction_params gets its new fingerprint, plus a note on how category_id is checked.
  • Deliberately unchanged:

  • Assistant::Function::CreateBill, which sets the category itself after build.

  • create_from_transfer, because a transfer is not spending.

  • The API and the data importer, which take explicit input.

  • Re-syncing a category once the bill has one.
  • Known limit: a blank category can't tell "never set" from "deliberately cleared". A bill the user cleared is refilled on the next detection run if its transactions carry a category.

    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 categorised Food & Drink.

    | Before | After |
    |---|---|
    | !Before: no category on the form | !After: the category is preselected as Food & Drink |

    Tests

    New tests, each run against unmodified main first. Nine failed there for the expected reason, either a missing category or a missing field. The other seven are negative controls that pass both before and after the change:

  • Identifier, detected series:

  • - A blank series takes its entries' category.
    - A set category is kept.
    - Mixed categories resolve to the most common one. Two bills, with the minority at the newest entry of one and the oldest of the other, so taking either end fails.
  • Identifier, manual series:

  • - A blank series takes its matched entries' category.
    - Mixed categories resolve to the most common one, with the same two-bill arrangement.
    - A set category is kept.
    - A manual income series stays blank.
  • Both paths: uncategorised entries leave a blank series blank, and the run completes.

  • create_from_transaction: an expense takes the transaction's category, and an inflow stays uncategorised.

  • Controller:

  • - GET new?entry_id= preselects the entry's category.
    - A paycheck dialog offers no category field.
    - POST create keeps the chosen category.
    - A declared paycheck stays uncategorised.
    - Another family's category is rejected with a 422.
  • Smart fill keeps the transaction's category.
  • After the change, I broke each piece in turn and checked that a test fails. That covered each fill call, both halves of the fill guard, the most-common rule (including filling from the first or the last entry instead, on both paths), the selected column, the create_from_transaction category and its income guard, the prefill, the permit, the DeclaredBill assignment and its income guard, the form field and its income guard, and the ownership validation. All were caught.

  • Targeted: test/models/recurring_transaction (directory), test/models/recurring_transaction_test.rb, test/controllers/recurring_transactions_controller_test.rb, test/controllers/recurring_transactions/smart_fills_controller_test.rb, test/models/assistant/function/create_bill_test.rb and test/controllers/transactions_controller_test.rb give 524 runs, 1796 assertions, 0 failures, 0 errors.

  • Full suite (bin/rails test): 12490 runs, 57722 assertions, 0 failures, 0 errors, 36 skips.

  • bin/rubocop and erb_lint are clean on the changed files. bin/brakeman reports 0 warnings.
  • <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    New Features
    Recurring bills and other non-income transactions can now be assigned a category when created or edited. The category selector follows your family’s category-lock setting.
    Categories from the original transaction are retained when setting up a recurring transaction, and recurring bills without a category may be assigned the most common category from matching transactions.
    Bug Fixes
    Income recurring transactions remain uncategorized, even if a category is submitted.
    Categories belonging to another family are rejected when creating a recurring transaction.

    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: jaysbeekay Review duration: 1715m
    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 #4374"
    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%