Skip to content

fix(webapp): clear idempotency key and re-trigger dead runs in batchT… - #4969

Closed
kaiizer777 wants to merge 1 commit into
triggerdotdev:mainfrom
kaiizer777:fix/batch-trigger-idempotency-key-status-check
Closed

kaiizer777 wants to merge 1 commit into
triggerdotdev:mainfrom
kaiizer777:fix/batch-trigger-idempotency-key-status-check

Conversation

@kaiizer777

Copy link
Copy Markdown

…rigger

Closes #

✅ Checklist

  • I have followed every step in the contributing guide
  • The PR title follows the convention.
  • I ran and tested the code works

Testing

[Describe the steps you took to test this change]


Changelog

[Short description of what has changed]


Screenshots

[Screenshots]

💯

@changeset-bot

changeset-bot Bot commented Sep 21, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: a79bd57

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 @kaiizer777, 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 Sep 21, 2026
@coderabbitai

coderabbitai Bot commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: d04ad90e-8c2a-415d-ba74-865bb8631a95

📥 Commits

Reviewing files that changed from the base of the PR and between d8c3530 and a79bd57.

📒 Files selected for processing (7)
  • .server-changes/batch-trigger-idempotency-key-status-check.md
  • apps/webapp/app/v3/services/batchTriggerV3.server.ts
  • apps/webapp/test/batchTriggerV3IdempotencyKeyStatus.test.ts
  • internal-packages/run-store/src/PostgresRunStore.findRunsByIdempotencyKeys.test.ts
  • internal-packages/run-store/src/PostgresRunStore.ts
  • internal-packages/run-store/src/runOpsStore.shardMap.test.ts
  • internal-packages/run-store/src/types.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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.

@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 3 potential issues.

Devin Review

Comment on lines +467 to +468
const shouldClear =
isExpired || shouldIdempotencyKeyBeCleared(cachedRun.status as TaskRunStatus);

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.

🟡 Late dead runs remain untriggered

When shouldClear marks a late run in a mostly cached batch, parallel processing can skip it. The parallel scheduler covers a prefix sized from newRunCount, then seals the batch.

Learn more

Parallel batch jobs process positional slices of batch.runIds. Cached entries remain in that array, so the number of uncached runs does not determine the last position requiring processing. Marking a dead run uncached increases newRunCount, but the parallel scheduler still starts every range within the first ceil(newRunCount / 50) * 50 positions. A dead run after that prefix is never passed to TriggerTaskService, although the final processing job seals the batch.

Example: A 100-item batch has 99 live cached runs and one CRASHED run at index 99. newRunCount is 1, so only range 0–49 is enqueued. The batch seals after that job, while the fresh ID at index 99 never becomes a run.

Recommended fix: Build parallel ranges from runs.length, not newRunCount, because processing is indexed over the complete ordered arrays. Keep newRunCount only for queue-limit accounting.

Devin Review


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

Comment on lines +470 to 473
if (shouldClear) {
expiredRunIds.add(cachedRun.friendlyId);

return {

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.

🟡 Repeated keys expose nonexistent runs

When repeated items share a failed key, shouldClear mints a different ID for each item. Later triggers deduplicate to the first run, but the batch retains every unused ID.

(Refers to this code)

Learn more

Every repeated item finds the same failed row in the preloaded cachedRuns snapshot. Each item therefore enters this branch and receives a separate minted ID before the old key is cleared. During processing, the first item creates a run with that key; TriggerTaskService returns that run as cached for later items. The batch's stored runIds and returned runs are never reconciled with the actual cached run ID.

Example: Two items use key invoice-42, which belongs to a CRASHED run. Preparation returns fresh IDs run_A and run_B. Processing creates run_A, then deduplicates the second item to run_A, while the response still exposes nonexistent run_B.

Recommended fix: Resolve repeated (taskIdentifier, idempotencyKey) entries as one preparation unit and assign the same fresh ID to all matching items. Also cover concurrent batches, where a competing trigger can win after preparation; reconcile the actual TriggerTaskService result with the batch's stored run ID or serialize key replacement and creation.

Devin Review


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

Comment on lines +3 to +16
// Mock DB layer singletons
vi.mock("~/db.server", () => ({
prisma: {},
$replica: {},
runOpsNewPrisma: {},
runOpsLegacyPrisma: {},
runOpsNewReplica: {},
runOpsLegacyReplica: {},
}));

import type { TaskRunStatus } from "@trigger.dev/database";
import type { AuthenticatedEnvironment } from "~/services/apiAuth.server";
import { BatchTriggerV3Service } from "~/v3/services/batchTriggerV3.server";
import { shouldIdempotencyKeyBeCleared } from "~/v3/taskStatus";

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.

🔍 Tests bypass the integration policy

The new suite mocks database singletons and the run store, contrary to the mandatory testcontainers policy. It also tests through an added private wrapper instead of the public service contract.

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.

1 participant