@@ -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" ;
@@ -709,15 +709,25 @@ export class RunEngine {
709709 } ) ;
710710
711711 if ( error . code === "P2002" ) {
712- this . logger . debug ( "engine.trigger(): throwing RunDuplicateIdempotencyKeyError" , {
712+ const target = ( error . meta as Record < string , unknown > ) ?. target ;
713+ const targetFields = Array . isArray ( target ) ? target : [ ] ;
714+
715+ this . logger . debug ( "engine.trigger(): P2002 unique constraint violation" , {
713716 code : error . code ,
714717 message : error . message ,
715718 meta : error . meta ,
719+ target : targetFields ,
716720 idempotencyKey,
717721 environmentId : environment . id ,
718722 } ) ;
719723
720- //this happens if a unique constraint failed, i.e. duplicate idempotency
724+ if ( targetFields . includes ( "oneTimeUseToken" ) ) {
725+ throw new RunOneTimeUseTokenError (
726+ `One-time use token has already been used`
727+ ) ;
728+ }
729+
730+ // Only idempotency key collisions should be retried
721731 throw new RunDuplicateIdempotencyKeyError (
722732 `Run with idempotency key ${ idempotencyKey } already exists`
723733 ) ;
0 commit comments