From bac4703bb1ade274eac8d20f2c92c076a82ca1da Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 29 Jul 2026 04:52:28 +0200 Subject: [PATCH] fix(orchestrator): preserve retryable effect failures Co-authored-by: codex --- .../src/orchestration-v2/EffectWorker.test.ts | 76 +++++++++++++++++++ .../src/orchestration-v2/EffectWorker.ts | 60 ++++++++------- 2 files changed, 108 insertions(+), 28 deletions(-) diff --git a/apps/server/src/orchestration-v2/EffectWorker.test.ts b/apps/server/src/orchestration-v2/EffectWorker.test.ts index 6095f59e0f..cbecfe224c 100644 --- a/apps/server/src/orchestration-v2/EffectWorker.test.ts +++ b/apps/server/src/orchestration-v2/EffectWorker.test.ts @@ -24,6 +24,7 @@ import { executorLayer, isNonRetryableProviderTurnControlFailure, layerWithOptions as effectWorkerLayerWithOptions, + OrchestrationEffectExecutionError, OrchestrationEffectExecutorV2, OrchestrationEffectWorkerError, OrchestrationEffectWorkerV2, @@ -377,6 +378,81 @@ it.effect("requeues a replay-safe claim when success settlement fails", () => }), ); +it.effect("keeps a process-bound executor failure retryable when retry settlement fails", () => + Effect.gen(function* () { + const now = DateTime.formatIso(yield* DateTime.now); + const effectId = "effect:worker-process-bound-retry-settlement-failure"; + const workerId = "worker-process-bound-retry-settlement-failure"; + const claimedEffect: OrchestrationEffectV2 = { + id: effectId, + commandId: CommandId.make("command:worker-process-bound-retry-settlement-failure"), + threadId: ThreadId.make("thread:worker-process-bound-retry-settlement-failure"), + request: { + type: "provider-turn.start", + runId: RunId.make("run:worker-process-bound-retry-settlement-failure"), + }, + status: "running", + attemptCount: 1, + availableAt: now, + leaseOwner: workerId, + leaseExpiresAt: now, + createdAt: now, + updatedAt: now, + completedAt: null, + lastError: null, + }; + const retryAttempts = yield* Ref.make(0); + const terminalizations = yield* Ref.make(0); + const outboxLayer = Layer.mock(EffectOutboxV2)({ + claimNext: () => Effect.succeed(Option.some(claimedEffect)), + get: () => Effect.succeed(Option.some(claimedEffect)), + awaitCancellation: () => Effect.never, + clearCancellation: () => Effect.void, + retry: () => + Ref.updateAndGet(retryAttempts, (count) => count + 1).pipe( + Effect.flatMap((attempt) => + attempt === 1 + ? Effect.fail( + new EffectOutboxError({ + operation: "retry", + effectId, + cause: "simulated retry settlement failure", + }), + ) + : Effect.succeed(true), + ), + ), + fail: () => Ref.update(terminalizations, (count) => count + 1).pipe(Effect.as(true)), + }); + const executorLayer = Layer.succeed( + OrchestrationEffectExecutorV2, + OrchestrationEffectExecutorV2.of({ + execute: () => + Effect.fail( + new OrchestrationEffectExecutionError({ + effectId, + effectType: claimedEffect.request.type, + cause: "simulated provider execution failure", + }), + ), + }), + ); + const workerLayer = effectWorkerLayerWithOptions({ workerId }).pipe( + Layer.provide(Layer.merge(outboxLayer, executorLayer)), + ); + + const exit = yield* OrchestrationEffectWorkerV2.pipe( + Effect.flatMap((worker) => worker.runOnce), + Effect.provide(workerLayer), + Effect.exit, + ); + + assert.isTrue(Exit.isFailure(exit)); + assert.equal(yield* Ref.get(retryAttempts), 2); + assert.equal(yield* Ref.get(terminalizations), 0); + }), +); + it.effect("uses durable deadlines, notifications, and a slow liveness poll", () => Effect.gen(function* () { const attempts = yield* Ref.make(0); diff --git a/apps/server/src/orchestration-v2/EffectWorker.ts b/apps/server/src/orchestration-v2/EffectWorker.ts index abe45bbdbb..d3619817da 100644 --- a/apps/server/src/orchestration-v2/EffectWorker.ts +++ b/apps/server/src/orchestration-v2/EffectWorker.ts @@ -493,38 +493,42 @@ export const layerWithOptions = ( }).pipe(Effect.onError((cause) => recoverClaimAfterExecutionStarted(effect, cause))); } - return yield* Effect.gen(function* () { - const error = Cause.pretty(exit.cause); - const nonRetryable = isNonRetryableProviderTurnControlFailure(effect.request.type, error); - yield* Effect.logWarning("Orchestration effect execution failed", { - effectId: effect.id, - effectType: effect.request.type, - attemptCount: effect.attemptCount, - nonRetryable, - error, - }); - // Prefer succeed for terminal interrupt races so the outbox does not - // keep a failed interrupt around; fail only when we must not retry. - const updated = nonRetryable - ? yield* outbox.succeed({ effectId: effect.id, workerId }) - : effect.attemptCount >= maxAttempts - ? yield* outbox.fail({ effectId: effect.id, workerId, error }) - : yield* outbox.retry({ + const error = Cause.pretty(exit.cause); + const nonRetryable = isNonRetryableProviderTurnControlFailure(effect.request.type, error); + yield* Effect.logWarning("Orchestration effect execution failed", { + effectId: effect.id, + effectType: effect.request.type, + attemptCount: effect.attemptCount, + nonRetryable, + error, + }); + // Prefer succeed for terminal interrupt races so the outbox does not + // keep a failed interrupt around; fail only when we must not retry. + const updated = nonRetryable + ? yield* outbox + .succeed({ effectId: effect.id, workerId }) + .pipe(Effect.onError((cause) => recoverClaimAfterExecutionStarted(effect, cause))) + : effect.attemptCount >= maxAttempts + ? yield* outbox + .fail({ effectId: effect.id, workerId, error }) + .pipe(Effect.onError((cause) => recoverClaimAfterExecutionStarted(effect, cause))) + : yield* outbox + .retry({ effectId: effect.id, workerId, error, delayMs: Math.min(30_000, 100 * 2 ** Math.max(0, effect.attemptCount - 1)), - }); - if (!updated) { - if (yield* wasCancelled(effect.id)) return true; - return yield* new OrchestrationEffectWorkerError({ - operation: "reschedule", - effectId: effect.id, - cause: "The worker no longer owns the effect lease.", - }); - } - return true; - }).pipe(Effect.onError((cause) => recoverClaimAfterExecutionStarted(effect, cause))); + }) + .pipe(Effect.onError((cause) => requeueClaim(effect, cause))); + if (!updated) { + if (yield* wasCancelled(effect.id)) return true; + return yield* new OrchestrationEffectWorkerError({ + operation: "reschedule", + effectId: effect.id, + cause: "The worker no longer owns the effect lease.", + }); + } + return true; }).pipe( Effect.mapError((cause) => isOrchestrationEffectWorkerError(cause)