← Back to Pull requests

Recalculate the account after a Wise linked-balance processing failure #6139

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Dec 04, 2026 13:06 UTC
Author

Closes #6105

Fork pre-flight: jaysbeekay/sure#1060 (same branch, up/wise-linked-failure-sync, same commit).

Summary

When WiseItem::ProcessLinkedAccountJob fails while processing a newly linked balance, after the processor has committed transactions, the account is now recalculated, as on success. Before, the rescue only logged, so the booked entries had no materialised balances, no completed Sync for the family caches and no transfer matching until the next connection sync.

What changed

  • The job tracks its stage. After a processing failure the rescue queues account.sync_later once it has logged. The processor commits rows one by one, and a raise loses the signal of whether any were committed, so the job recalculates either way; without new rows the account sync re-materialises balances that are already right. sync_later joins a visible sync, so it does not duplicate one the connection's own sync scheduled.

  • After a jar-linking failure it does not recalculate: the account sync's transfer matching must not see legs the linker has not linked. The next connection sync links before it recalculates.

  • The success path's sync_later moves to the rescue's else branch, so a failure to schedule is not logged as a failed process and is not tried twice.

  • A failure to schedule is logged with a DebugLogEntry (provider wise, the balance's account provider, the error class), so it never turns a logged failure into a failed job. Nothing is scheduled when the account itself could not be read.

  • The ActiveRecord::Deadlocked branch is unchanged.
  • Non-test diff: +27 / -2 in app/jobs/wise_item/process_linked_account_job.rb.

    Left out: making the processor transactional (the job comment explains why), retrying, the current balance after an update_balance! failure (a :reverse account sync does not write accounts.balance), a sync already running when the rows commit (the success path has the same gap), and WiseItem::Syncer Phase 3 calling link_jar_transfers! without a rescue.

    Demo data

    No demo data needed: no Wise data in the demo generator, and this is a failure path.

    Migration notes

    No migration.

    Verification

  • Red first on main 7e8693344: "recalculates the account when processing fails after booking its transactions" failed with syncs.count didn't change by 1, but by 0; the updated "logs a processing failure for support instead of raising" with Expected: 1 Actual: 0. The deadlock test (now asserting no Sync), "does not recalculate the account when jar linking fails" and "a failure before the account is read" pass on main, as guards.

  • Red first on the first commit edae65b46: "does not recalculate the account when jar linking fails" (syncs changed by 1, expected 0); "a sync_later failure after success is logged once as a scheduling failure" (logged as "Failed to process"); "a sync_later failure after a processing failure is logged for support" (DebugLogEntry +1, expected +2).

  • Mutations, each red: recalculation after processing removed (both processing-failure tests); recalculation after a linking failure too (the linking test); the else recalculation removed (the success test and the success scheduling test); sync_later back in the body (scheduling test logs "Failed to process"); the stage never set (both processing-failure tests); recalculate's rescue removed (the scheduling-failure test raises); its DebugLogEntry removed (both scheduling tests); the Account#update! stub removed (first positive test, DebugLogEntry 0, expected 1).

  • bin/rails test test/jobs/wise_item test/models/wise_item test/models/wise_item_test.rb test/models/wise_account/processor_test.rb: 254 runs, 0 failures.

  • Review follow-up (07d803c26, 3d7c7b18b): a deadlock while scheduling the recalculation is re-raised so ApplicationJob's retry_on retries the job, on both the success and the processing-failure path; any other scheduling failure is still logged. The failure log's metadata names the stage that failed (loading, processing or linking). "retries a deadlock scheduling the recalculation" failed before with No enqueued job found; the stage tests failed with Actual: nil.

  • Full bin/rails test on 3d7c7b18b: 20282 runs, 0 failures, 8 errors (all ReleaseNotesWorkflowTest: node is missing from the local test image), 9 skips.

  • rubocop, erb_lint, Brakeman: clean.
  • Blast radius

    The job is queued only from WiseItem#sync_linked_account_later, which WiseItemsController#complete_account_setup, #link_accounts and #link_existing_account call. Nothing else calls the job or reads its result. account.sync_later is the call the success path already makes. #5278 adds categorize_card_refunds! before sync_later inside the same begin, so a failure there is covered by this rescue too; #4740 changes only the header comment.

    <!-- This is an auto-generated description by cubic. -->
    ---

    Summary by cubic


    Closes we-promise/sure#6105.

    When WiseItem::ProcessLinkedAccountJob fails after the processor has booked transactions for a newly linked Wise balance, the account is now recalculated as on success. The failure path previously only logged, so committed entries went without materialised balances, a completed Sync, or transfer matching until the next connection sync.

  • The job tracks whether it is processing or linking. After a processing failure it queues account.sync_later; after a linking failure it does not, so the account sync's transfer matching doesn't see unlinked legs.

  • The success path's sync_later moves to the rescue's else branch, so a scheduling failure is logged as such and not tried twice.

  • A scheduling failure is logged with a DebugLogEntry instead of raising, so it can't turn a logged failure into a failed job.
  • <sup>Written for commit acbde6cbba620589778a200f545f59592e3753aa. Summary will update on new commits.</sup>

    <a href="https://cubic.dev/pr/we-promise/sure/pull/6139?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>

    <!-- End of auto-generated description by cubic. -->

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

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

    Summary by CodeRabbit

    Bug Fixes
    Account balances are now refreshed after successful linked-account processing and after processing errors, helping keep account information current.
    Failures while linking an account no longer trigger an unnecessary refresh.
    Deadlocks during processing or account refresh are retried, while other errors continue to be logged without automatic retries.

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

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: jaysbeekay Review duration: 1538m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). 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%
    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.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #6139"
    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: 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: 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: 27.7% · Safe to merge
    27.7%