Repository navigation
Description
What is the problem this feature would solve?
Activity.retry leaves out the schedule option (Omit<Effect.Retry.Options<E>, "schedule">), so a retried activity can't wait between attempts. That's understandable: Effect.retry waits with the in-memory clock. The wait is lost if the process restarts, and a long wait keeps the workflow in memory instead of suspending it.
Backing off between attempts is still common, though, for example when calling an unreliable external API. At the moment every workflow has to build it by hand from CurrentAttempt and DurableClock.sleep:
Effect.gen(function* () {
const currentAttempt = yield* Activity.CurrentAttempt
if (currentAttempt === 1) {
return
}
yield* DurableClock.sleep({
name: `sleep-${executionId}-${currentAttempt}`,
duration: Duration.min(
Duration.times(Duration.minutes(10), Math.pow(2, currentAttempt - 2)),
Duration.hours(12),
),
})
}).pipe(
Effect.andThen(
Activity.make({
name: 'ProcessReceivedReviewRequest',
error: ProcessReceivedReviewRequestError,
execute: processReceivedReviewRequest(reviewRequestId),
}),
),
Activity.retry({ times: 10 }),
)This repeats the first-attempt check and the clock naming in every workflow, and the backoff policy is written as arithmetic instead of a Schedule.
What is the feature you are proposing to solve the problem?
Let Activity.retry accept a schedule and wait with DurableClock.sleep instead of Clock:
Activity.make({
name: 'ProcessReceivedReviewRequest',
error: ProcessReceivedReviewRequestError,
execute: processReceivedReviewRequest(reviewRequestId),
}).pipe(
Activity.retry({
times: 10,
schedule: Schedule.exponential('10 minutes').pipe(
Schedule.modifyDelay(({ duration }) => Effect.succeed(Duration.min(duration, Duration.hours(12)))),
),
}),
)After a failed attempt, Activity.retry would ask the schedule for the next delay. It would then call DurableClock.sleep with a name made from the attempt number (and perhaps a name option). Short delays would still run in memory through the existing inMemoryThreshold, and long ones would suspend the workflow.
The open question is replay. Once a named clock has fired, it returns straight away on replay whatever duration is passed, so jitter in the delay is harmlto retry again is different: if it depends on time or randomness (Schedule.during, for example), replay could follow a different path. Two options:
- Only support schedules whose decisions depend on the attempt number and input.
- Record each schedule decision as part of replay reads it back.
What alternatives have you considered?
- Writing the backoff by hand, as above. It works, but every workflow has to repeat it. (e.g. https://github.com/PREreview/prereview.org/blob/00e33e18e21dc6b79c985ebc220566cd6484329e/src/Domain/ReviewRequests/Workflows/index.ts#L55-L64, https://github.com/PREreview/prereview.org/blob/00e33e18e21dc6b79c985ebc220566cd6484329e/src/Domain/ReviewRequests/Workflows/index.ts#L97-L109)
- Writing our own
retryWithSchedulehelper on top ofCurrentAttemptandDurableClock.sleep. That's possible in user code, but getting the replay rules right belongs in the library, next toDurableClock. - Using
Effect.retrywith a schedule directly. The waits aren't durable, and the attempt numbers in activity keys would no longer increase.
Activity
thewilkybarkid commentedon Nov 24, 2026
We've now needed this in a second place, and doing it by hand there turned up a pitfall that a built-in
schedulewould avoid.Our comment workflows each run two activities in a row and retry each one with backoff. We pulled the pattern into a helper:
(https://github.com/PREreview/prereview.org/blob/2a0a1e16665ff1fcdc22d9632f1bcc44fb824cf6/src/Domain/Comments/Workflows/index.ts#L35-L56)
The sleep's name has to include the activity's name as well as the execution ID and the attempt. With only
sleep-${executionId}-${currentAttempt}, as in the original example, the second activity's waits would share names with the first activity's, so a retry of the second activity could reuse a sleep that had already finished and not wait at all. Nothing warns about this, so it's easy to get wrong when copying the pattern.A durable
scheduleonActivity.retrycould name its sleeps from the activity, in the same way activity attempts already get their own keys, so callers wouldn't need to think about it.