From 75247d396e1886eefa662e16ff522dfc8575991f Mon Sep 17 00:00:00 2001 From: "Claude Opus 4.6" Date: Mon, 30 Mar 2026 20:24:08 +1100 Subject: [PATCH] fix: retry on stream_version race in OrchestrationEventStore.append The stream_version is computed via subquery (SELECT max + 1), so two concurrent appends to the same stream both read the same max version and the second hits the UNIQUE constraint. Add optimistic retry (up to 3 attempts) on ConstraintError so the loser re-reads the incremented version and succeeds. Co-Authored-By: Claude Opus 4.6 (1M context) --- apps/server/src/persistence/Layers/OrchestrationEventStore.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/apps/server/src/persistence/Layers/OrchestrationEventStore.ts b/apps/server/src/persistence/Layers/OrchestrationEventStore.ts index 4d81cf5e8d..688d063ab9 100644 --- a/apps/server/src/persistence/Layers/OrchestrationEventStore.ts +++ b/apps/server/src/persistence/Layers/OrchestrationEventStore.ts @@ -192,6 +192,10 @@ const makeEventStore = Effect.gen(function* () { payloadJson: event.payload, metadataJson: event.metadata, }).pipe( + Effect.retry({ + while: (error) => error._tag === "SqlError" && error.reason._tag === "ConstraintError", + times: 3, + }), Effect.mapError( toPersistenceSqlOrDecodeError( "OrchestrationEventStore.append:insert",