trigger.dev icon indicating copy to clipboard operation
trigger.dev copied to clipboard

bug: batchTrigger returns stale failed runs instead of re-triggering when idempotency key points to a dead run

Open Jaimin2687 opened this issue 3 months ago • 2 comments

Bug Description

batchTrigger and single trigger behave differently when an idempotency key points to a run that already failed.

With single trigger, if a previous run with the same idempotency key ended up in a terminal failure state (CRASHED, SYSTEM_FAILURE, TIMED_OUT, EXPIRED, COMPLETED_WITH_ERRORS, INTERRUPTED), it correctly clears the key and creates a fresh run. This works because IdempotencyKeyConcern.handleExistingRun calls shouldIdempotencyKeyBeCleared(status) before deciding whether to return a cached result.

With batchTrigger, that same check is missing. The batch path in BatchTriggerV3Service.#prepareRunData only checks time-based expiration (idempotencyKeyExpiresAt < now), so a failed run gets returned as isCached: true — silently handing back a dead run that will never produce output.

Steps to Reproduce

// 1. A task that always fails
export const failingTask = task({
  id: "failing-task",
  run: async () => { throw new Error("boom"); },
});

// 2. Batch trigger with idempotency key
await tasks.batchTrigger("failing-task", [
  { payload: {}, options: { idempotencyKey: "key-1" } },
]);
// Wait for the run to reach CRASHED / COMPLETED_WITH_ERRORS...

// 3. Trigger again with the same key
await tasks.batchTrigger("failing-task", [
  { payload: {}, options: { idempotencyKey: "key-1" } },
]);
// Expected: isCached: false (fresh run created)
// Actual:   isCached: true  (dead run returned)

Compare with single trigger — this already works correctly:

await tasks.trigger("failing-task", {}, { idempotencyKey: "key-2" });
// Wait for failure...
await tasks.trigger("failing-task", {}, { idempotencyKey: "key-2" });
// Correctly returns isCached: false and creates a new run

Root Cause

The SQL query in PostgresRunStore.findRunsByIdempotencyKeys never selects the status column, and the IdempotencyKeyRunMatch type doesn't include it. So #prepareRunData has no way to check whether a matched run is in a clearable failure state — it can only check time-based expiration.

The single-trigger path works because IdempotencyKeyConcern.handleExistingRun fetches the full run record and calls shouldIdempotencyKeyBeCleared(status) at idempotencyKeys.server.ts:463. The batch path at batchTriggerV3.server.ts:452-469 skips this entirely.

Check Single trigger Batch trigger
Time-based expiry ✅ idempotencyKeyExpiresAt < now ✅ idempotencyKeyExpiresAt < now
Failure status check ✅ shouldIdempotencyKeyBeCleared(status) ❌ Missing

Affected Statuses

These statuses should clear the idempotency key and re-trigger (works for single trigger, broken for batch):

  • CRASHED
  • SYSTEM_FAILURE
  • TIMED_OUT
  • EXPIRED
  • COMPLETED_WITH_ERRORS
  • INTERRUPTED

Suggested Fix

I already wrote and tested a patch for this. My PR #4818 was auto-closed since I'm not a vouched contributor, but the fix is ready on my fork if you'd like to pull it in or use it as reference:

Branch: Jaimin2687:fix/batch-trigger-idempotency-key-status-check

The fix is three small changes:

  1. internal-packages/run-store/src/types.ts — Add status: string to IdempotencyKeyRunMatch
  2. internal-packages/run-store/src/PostgresRunStore.ts — Add "status" to the raw SQL SELECT in findRunsByIdempotencyKeys
  3. apps/webapp/app/v3/services/batchTriggerV3.server.ts — Import shouldIdempotencyKeyBeCleared and add the guard after the expiry check in #prepareRunData, mirroring the single-trigger path

The branch also includes 21 unit tests and 2 integration tests (testcontainers) covering all TaskRunStatus values. Everything passes typecheck, build, format, and lint.

Environment

  • trigger.dev main branch (as of Aug 28, 2025)
  • Affects all environments (dev, staging, production)
  • Backend only, no UI impact

Jaimin2687 avatar Aug 28 '26 13:08 Jaimin2687

Confirmed the asymmetry against main, and there's a wrinkle that makes this bigger than a one-line check.

The single-trigger path does have the guard — idempotencyKeys.server.ts:463:

// If the existing run failed or was expired, we clear the key and do a new run
if (shouldIdempotencyKeyBeCleared(existingRun.status)) {

The batch path (batchTriggerV3.server.ts:453) only tests time:

if (cachedRun.idempotencyKeyExpiresAt && cachedRun.idempotencyKeyExpiresAt < new Date()) {
  expiredRunIds.add(cachedRun.friendlyId);
  return { ...isCached: false };
}
return { id: cachedRun.friendlyId, isCached: true, ... };   // dead run returned here

The wrinkle: cachedRun has no status to check. The lookup selects five columns and status isn't one of them — PostgresRunStore.ts:1864:

SELECT "id", "createdAt", "friendlyId", "idempotencyKey", "idempotencyKeyExpiresAt" FROM "TaskRun" WHERE ...

and the return type agrees (internal-packages/run-store/src/types.ts:25):

export type IdempotencyKeyRunMatch = {
  id: string;
  createdAt: Date;
  friendlyId: string;
  idempotencyKey: string | null;
  idempotencyKeyExpiresAt: Date | null;
};

So dropping shouldIdempotencyKeyBeCleared(cachedRun.status) into the batch path won't compile — the field doesn't exist. The fix spans three files:

  1. internal-packages/run-store/src/types.ts — add status to IdempotencyKeyRunMatch
  2. internal-packages/run-store/src/PostgresRunStore.ts:1864 — add "status" to the SELECT
  3. apps/webapp/app/v3/services/batchTriggerV3.server.ts:453 — add the status check beside the expiry check

Only PostgresRunStore needs the SQL change: delegatingRunStore.ts:439 and runOpsStore.ts:781 both just forward the call.

Two things worth deciding while implementing:

  • The expiry branch adds to expiredRunIds so the key gets cleared afterwards. A status-cleared run presumably wants the same treatment, so it's likely the same branch with an || rather than a separate one — otherwise the stale key survives and the next batch hits it again.
  • shouldIdempotencyKeyBeCleared lives in apps/webapp/app/v3/taskStatus.ts, so the webapp keeps owning the policy and the run-store change is purely "return one more column". That seems like the right split, but it does mean the store starts carrying a field only one caller uses.

internal-packages/run-store/src/PostgresRunStore.findRunsByIdempotencyKeys.test.ts already exists and looks like the natural home for a regression test at the store level, with the batch-level assertion going alongside the existing batch trigger tests.

itzzdev09 avatar Aug 28 '26 22:08 itzzdev09

@Jaimin2687 Confirmed - the asymmetry is real: the single-trigger path clears the key when the cached run hit a clearable terminal state, but the batch path only tested time expiry, so a key pointing at a dead run returned that FAILED run as cached forever. @itzzdev09's analysis nailed the wrinkle too - cachedRun had no status column to check, so the fix had to span the run store.

Fix is up in #4912: the idempotency lookup now selects status, and the batch path clears the key and mints a fresh run when shouldIdempotencyKeyBeCleared(status) holds - same branch as time expiry, so the stale key cannot survive to the next batch. Verified at SQL level on Postgres 14 and with the repo's own status constants; a store-level regression test is included (the repo harness needs Docker, so CI will run it).

Built by breken, your AI support engineer - breken.ai finds the bug, proves the fix with unmatched RCA, answers customers in your voice, and keeps your code and support docs clean - this one's on us.

breken-ai avatar Sep 09 '26 12:09 breken-ai