@@ -46,7 +46,7 @@ import { RunQueue } from "../run-queue/index.js";
4646import { RunQueueFullKeyProducer } from "../run-queue/keyProducer.js" ;
4747import { AuthenticatedEnvironment , MinimalAuthenticatedEnvironment } from "../shared/index.js" ;
4848import { BillingCache } from "./billingCache.js" ;
49- import { NotImplementedError , RunDuplicateIdempotencyKeyError } from "./errors.js" ;
49+ import { NotImplementedError , RunDuplicateIdempotencyKeyError , RunOneTimeUseTokenError } from "./errors.js" ;
5050import { EventBus , EventBusEvents } from "./eventBus.js" ;
5151import { RunLocker } from "./locking.js" ;
5252import { getFinalRunStatuses } from "./statuses.js" ;
@@ -703,15 +703,25 @@ export class RunEngine {
703703 } ) ;
704704
705705 if ( error . code === "P2002" ) {
706- this . logger . debug ( "engine.trigger(): throwing RunDuplicateIdempotencyKeyError" , {
706+ const target = ( error . meta as Record < string , unknown > ) ?. target ;
707+ const targetFields = Array . isArray ( target ) ? target : [ ] ;
708+
709+ this . logger . debug ( "engine.trigger(): P2002 unique constraint violation" , {
707710 code : error . code ,
708711 message : error . message ,
709712 meta : error . meta ,
713+ target : targetFields ,
710714 idempotencyKey,
711715 environmentId : environment . id ,
712716 } ) ;
713717
714- //this happens if a unique constraint failed, i.e. duplicate idempotency
718+ if ( targetFields . includes ( "oneTimeUseToken" ) ) {
719+ throw new RunOneTimeUseTokenError (
720+ `One-time use token has already been used`
721+ ) ;
722+ }
723+
724+ // Only idempotency key collisions should be retried
715725 throw new RunDuplicateIdempotencyKeyError (
716726 `Run with idempotency key ${ idempotencyKey } already exists`
717727 ) ;
0 commit comments