Skip to content

fix(run-engine): decrement totalWeight in #weightedShuffle after splice (#4001)#4008

Closed
gtx20060124-bot wants to merge 2 commits into
triggerdotdev:mainfrom
gtx20060124-bot:fix-postgres-epoch-constant
Closed

fix(run-engine): decrement totalWeight in #weightedShuffle after splice (#4001)#4008
gtx20060124-bot wants to merge 2 commits into
triggerdotdev:mainfrom
gtx20060124-bot:fix-postgres-epoch-constant

Conversation

@gtx20060124-bot

Copy link
Copy Markdown

Fixes #4001

#weightedShuffle was not decrementing totalWeight after splicing an item, causing the weighted random selection to produce non-uniform ordering when biases are enabled.

The sibling methods #weightedRandomQueueOrder and #selectTopEnvs both decrement totalWeight correctly — this was a copy-paste omission.

Changed: moved totalWeight -= items[index].weight before splice() to use the correct index.

@changeset-bot

changeset-bot Bot commented Jun 20, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 9a462a6

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions

Copy link
Copy Markdown
Contributor

Hi @gtx20060124-bot, thanks for your interest in contributing!

This project requires that pull request authors are vouched, and you are not in the list of vouched users.

This PR will be closed automatically. See https://github.com/triggerdotdev/trigger.dev/blob/main/CONTRIBUTING.md for more details.

@github-actions github-actions Bot closed this Jun 20, 2026
@coderabbitai

coderabbitai Bot commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: a369067a-e63e-4ad2-b110-14cea89f4a0d

📥 Commits

Reviewing files that changed from the base of the PR and between 135c7e9 and 9a462a6.

📒 Files selected for processing (2)
  • internal-packages/replication/src/client.ts
  • internal-packages/run-engine/src/run-queue/fairQueueSelectionStrategy.ts

Walkthrough

Two independent bug fixes are applied across separate internal packages. In internal-packages/replication/src/client.ts, the PostgreSQL epoch offset constant is corrected from 946080000000 to 946684800000 in both the keepalive message timestamp parsing path and the WAL acknowledgement payload construction path within LogicalReplicationClient. In internal-packages/run-engine/src/run-queue/fairQueueSelectionStrategy.ts, the #weightedShuffle method's selection loop is fixed to subtract the selected item's weight from totalWeight after each iteration, correcting the probability distribution for subsequent selections.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

Open in Devin Review


// Add selected item to result and remove from items
result.push(items[index].envId);
totalWeight -= items[index].weight;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚩 totalWeight fix matches existing pattern in #weightedRandomQueueOrder but variable is declared const

The addition of totalWeight -= items[index].weight; at line 229 is the correct fix for the weighted shuffle algorithm — without it, this._rng() * totalWeight on line 217 uses a stale total after items are removed, biasing selection toward later items in the array. The analogous method #weightedRandomQueueOrder at lines 293-310 correctly uses let totalWeight and decrements it at line 309. However, totalWeight in #weightedShuffle is declared as const at line 212, while the new line 229 attempts to reassign it. The declaration should be let to match the pattern in the analogous method.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fair-queue #weightedShuffle never decrements totalWeight, biasing env ordering when biases are enabled

1 participant