fix(orchestrator): preserve terminal effect outcomes

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-08-14 11:23:29 +02:00
1 parent 8db6d9d7b3
commit b2d23ccabb
2 files changed
+85 -14

No files matched your search

@@ -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);
@@ -382,18 +382,8 @@ export const layerWithOptions = (
}),
),
);
const recoverClaimAfterExecutionStarted = (
effect: OrchestrationEffectV2,
cause: Cause.Cause<unknown>,
) => {
const terminalizeClaim = (effect: OrchestrationEffectV2, cause: Cause.Cause<unknown>) => {
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<unknown>,
) =>
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,