From b2d23ccabb72a9654b06fe48ab74bbd054ba64c7 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 29 Jul 2026 04:54:05 +0200 Subject: [PATCH] fix(orchestrator): preserve terminal effect outcomes Co-authored-by: codex --- .../src/orchestration-v2/EffectWorker.test.ts | 72 +++++++++++++++++++ .../src/orchestration-v2/EffectWorker.ts | 27 ++++--- 2 files changed, 85 insertions(+), 14 deletions(-) diff --git a/apps/server/src/orchestration-v2/EffectWorker.test.ts b/apps/server/src/orchestration-v2/EffectWorker.test.ts index cbecfe224c..baa74c883e 100644 --- a/apps/server/src/orchestration-v2/EffectWorker.test.ts +++ b/apps/server/src/orchestration-v2/EffectWorker.test.ts @@ -453,6 +453,78 @@ it.effect("keeps a process-bound executor failure retryable when retry settlemen }), ); +it.effect("keeps a max-attempt replay-safe failure terminal when fail settlement fails", () => + Effect.gen(function* () { + const now = DateTime.formatIso(yield* DateTime.now); + const effectId = "effect:worker-replay-safe-terminal-settlement-failure"; + const workerId = "worker-replay-safe-terminal-settlement-failure"; + const claimedEffect: OrchestrationEffectV2 = { + id: effectId, + commandId: CommandId.make("command:worker-replay-safe-terminal-settlement-failure"), + threadId: ThreadId.make("thread:worker-replay-safe-terminal-settlement-failure"), + request: { type: "terminal.cleanup" }, + status: "running", + attemptCount: 5, + availableAt: now, + leaseOwner: workerId, + leaseExpiresAt: now, + createdAt: now, + updatedAt: now, + completedAt: null, + lastError: null, + }; + const failAttempts = yield* Ref.make(0); + const retries = 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, + fail: () => + Ref.updateAndGet(failAttempts, (count) => count + 1).pipe( + Effect.flatMap((attempt) => + attempt === 1 + ? Effect.fail( + new EffectOutboxError({ + operation: "fail", + effectId, + cause: "simulated terminal settlement failure", + }), + ) + : Effect.succeed(true), + ), + ), + retry: () => Ref.update(retries, (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 terminal cleanup failure", + }), + ), + }), + ); + const workerLayer = effectWorkerLayerWithOptions({ workerId, maxAttempts: 5 }).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(failAttempts), 2); + assert.equal(yield* Ref.get(retries), 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 d3619817da..fd932da03b 100644 --- a/apps/server/src/orchestration-v2/EffectWorker.ts +++ b/apps/server/src/orchestration-v2/EffectWorker.ts @@ -382,18 +382,8 @@ export const layerWithOptions = ( }), ), ); - const recoverClaimAfterExecutionStarted = ( - effect: OrchestrationEffectV2, - cause: Cause.Cause, - ) => { + const terminalizeClaim = (effect: OrchestrationEffectV2, cause: Cause.Cause) => { if (Cause.hasInterruptsOnly(cause)) return Effect.void; - if ( - REPLAY_SAFE_EFFECT_TYPES_AFTER_PROCESS_LOSS.some( - (effectType) => effectType === effect.request.type, - ) - ) { - return requeueClaim(effect, cause); - } return outbox .fail({ effectId: effect.id, @@ -427,6 +417,15 @@ export const layerWithOptions = ( ), ); }; + const recoverPostSuccessSettlement = ( + effect: OrchestrationEffectV2, + cause: Cause.Cause, + ) => + REPLAY_SAFE_EFFECT_TYPES_AFTER_PROCESS_LOSS.some( + (effectType) => effectType === effect.request.type, + ) + ? requeueClaim(effect, cause) + : terminalizeClaim(effect, cause); const runOnce = Effect.gen(function* () { const claimExit = yield* Effect.exit(outbox.claimNext({ workerId, leaseDurationMs })); @@ -490,7 +489,7 @@ export const layerWithOptions = ( }); } return true; - }).pipe(Effect.onError((cause) => recoverClaimAfterExecutionStarted(effect, cause))); + }).pipe(Effect.onError((cause) => recoverPostSuccessSettlement(effect, cause))); } const error = Cause.pretty(exit.cause); @@ -507,11 +506,11 @@ export const layerWithOptions = ( const updated = nonRetryable ? yield* outbox .succeed({ effectId: effect.id, workerId }) - .pipe(Effect.onError((cause) => recoverClaimAfterExecutionStarted(effect, cause))) + .pipe(Effect.onError((cause) => terminalizeClaim(effect, cause))) : effect.attemptCount >= maxAttempts ? yield* outbox .fail({ effectId: effect.id, workerId, error }) - .pipe(Effect.onError((cause) => recoverClaimAfterExecutionStarted(effect, cause))) + .pipe(Effect.onError((cause) => terminalizeClaim(effect, cause))) : yield* outbox .retry({ effectId: effect.id,