← Back to Pull requests

Batch the hold commission release #4646

Closed
unknown closed this pull request without merging on Oct 08, 2026 18:34 UTC
View on GitHub
unknown unknown commented on Oct 08, 2026 16:17 UTC
Author

Changes

Fixes https://linear.app/dubinc/issue/ENG-1776/weekday-batch-the-hold-commission-release-with-take-and-hoist-the

releaseAllHoldCommissions:

  • Release hold commissions in batches of 250 (PRISMA_UPDATEMANY_LIMIT).

  • Read the program once, before the loop. Return 0 if the program no longer exists.

  • After the loop, sync each partner once and trigger the aggregate due commissions job once. Both run in a finally block, so they also run when a later batch fails.
  • releaseHoldCommissions: trigger the aggregate due commissions job once in a finally block, instead of once per batch.

    No index changes. The hold query uses the existing [programId, status, ...] index.

    Motivation

    The loop read all hold commissions of the program in one query, with no take. The updateMany, the activity log write and the partner syncs then ran on all of them at once. holdPendingCommissions already batches with PRISMA_UPDATEMANY_LIMIT.

    Performance

    I ran the old code (main) and the new code on the same seeded data in a local MySQL database. The hold commissions were spread across 5 partners.

    releaseAllHoldCommissions:

    | Metric | 10k old | 10k new | 50k old | 50k new |
    |---|---|---|---|---|
    | Rows in one read, update or activity log insert | 10,000 | 250 | 50,000 | 250 |
    | Slowest single SQL statement | 184 ms | 34 ms | 222 ms | 26 ms |
    | Largest log line | 4.7 MB | 120 KB | 23.4 MB | 120 KB |
    | Peak heap growth | 47 MB | 55 MB | 111 MB | 63 MB |
    | Total time | 1.1 s | 1.6 s | 6.6 s | 9.0 s |
    | SQL statements | 15 | 322 | 41 | 1,603 |
    | QStash triggers | 1 | 1 | 1 | 1 |
    | Partner sync events | 5 | 5 | 5 | 5 |

    releaseHoldCommissions with 10k hold commissions for 1 partner: QStash triggers go from 40 to 1. The other metrics stay the same.

    Each statement now touches at most 250 rows, so row locks are held for a shorter time. The largest log line came from trackActivityLog, which logs all of its inputs in one line. The total time is longer, because each pass adds round trips. Peak heap depends on garbage collection timing, so it changes between runs.

    Question

    releaseHoldCommissions runs commissionRecorded workflows for the released earnings, but releaseAllHoldCommissions does not. Is this intentional, because the workspace is leaving the Advanced plan? This PR does not change it.

    How to test

    releaseAllHoldCommissions:

    1. In your local database, put 600 or more commissions of one program in status = hold, across a few partners.
    2. POST { "programId": "<id>" } to /api/cron/fraud/release-all-hold-commissions, or call releaseAllHoldCommissions({ programId }) from a script in apps/web/scripts.
    3. All of these commissions are now pending, and the returned count is equal to the number of hold commissions.
    4. The logs show passes of at most 250 rows, and one "Triggered aggregate due commissions" line at the end.
    5. There is one commission.updated activity log for each released commission.

    releaseHoldCommissions:

    1. Put 600 or more commissions of one partner in status = hold. The partner must have no other pending fraud groups in the program.
    2. Call releaseHoldCommissions({ programId, partnerId, resolvedGroupIds: ["<any id>"] }) from a script in apps/web/scripts.
    3. All of these commissions are now pending, and the logs show "Released 250 hold commissions" for each full pass and one "Triggered aggregate due commissions" line at the end.

    I ran both tests locally with 620 commissions each:

  • releaseAllHoldCommissions (5 partners): 3 passes (250, 250, 120), 1 Program query, 1 trigger and 620 activity logs.

  • releaseHoldCommissions (1 partner): 3 passes (250, 250, 120), 1 trigger and 620 activity logs.

  • For both functions, I made the second updateMany throw. The function threw the error, 250 rows were pending, and the trigger ran one time. A retry released the other 370 rows and triggered one time. In releaseAllHoldCommissions, all 5 partners were also synced one time.
  • <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    Summary by CodeRabbit

    Bug Fixes
    Held commissions are released in bounded batches, and aggregate commission totals are updated once after processing when commissions were released—even if a later batch fails.
    If aggregate updates fail, the error is logged without interrupting commission releases or replacing errors from the release process.
    Commission status tracking and partner commission syncing continue during batch processing, with distinct partners synced in batches.
    * Releasing all held commissions now safely returns zero when the associated program is missing.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 594 lines (+445 / -149) across 7 files Reviewer: Joker666 Review duration: 137m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 594 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    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.382
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #4646"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% 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: 50% (MEDIUM) · Higher = larger blast radius
    50%
    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: 60.5% · Lower = rushed or shallow review
    60.5%
    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: 19.8% · Safe to merge
    19.8%
    Repository