Batch the hold commission release #4646
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 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.382
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #4646"
general
weight: 0.35
Joker666
Changes
Fixes https://linear.app/dubinc/issue/ENG-1776/weekday-batch-the-hold-commission-release-with-take-and-hoist-the
releaseAllHoldCommissions:PRISMA_UPDATEMANY_LIMIT).finallyblock, so they also run when a later batch fails.releaseHoldCommissions: trigger the aggregate due commissions job once in afinallyblock, 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. TheupdateMany, the activity log write and the partner syncs then ran on all of them at once.holdPendingCommissionsalready batches withPRISMA_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 |
releaseHoldCommissionswith 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
releaseHoldCommissionsrunscommissionRecordedworkflows for the released earnings, butreleaseAllHoldCommissionsdoes 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 callreleaseAllHoldCommissions({ programId })from a script inapps/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.updatedactivity 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 inapps/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), 1Programquery, 1 trigger and 620 activity logs.releaseHoldCommissions(1 partner): 3 passes (250, 250, 120), 1 trigger and 620 activity logs.updateManythrow. The function threw the error, 250 rows werepending, and the trigger ran one time. A retry released the other 370 rows and triggered one time. InreleaseAllHoldCommissions, 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 -->