Skip to content

Commit 84cdaf8

Browse files
committed
feat: add configurable max retry limit for SessionRetry
Add `session.retry.maxAttempts` config option to limit the number of retry attempts for transient errors (5xx, rate limits). Default is 5. Changes: - Add RETRY_MAX_ATTEMPTS constant (default: 5) to retry.ts - Add `maxAttempts` parameter to SessionRetry.policy() - Use Schedule.intersect with Schedule.recurs to enforce limit - Add `session.retry.maxAttempts` to config schema - Pass config value from processor to retry policy Closes anomalyco#26675
1 parent 903d818 commit 84cdaf8

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎packages/opencode/src/config/config.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,18 @@ export const Info = Schema.Struct({
283283
}),
284284
}),
285285
),
286+
session: Schema.optional(
287+
Schema.Struct({
288+
retry: Schema.optional(
289+
Schema.Struct({
290+
maxAttempts: Schema.optional(NonNegativeInt).annotate({
291+
description:
292+
"Maximum number of retry attempts for transient errors (5xx, rate limits). Set to 0 to disable retries. Default: 5",
293+
}),
294+
}),
295+
),
296+
}),
297+
),
286298
experimental: Schema.optional(
287299
Schema.Struct({
288300
disable_paste_summary: Schema.optional(Schema.Boolean),

‎packages/opencode/src/session/processor.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ export const layer: Layer.Layer<
749749
next: info.next,
750750
})
751751
},
752+
maxAttempts: (yield* config.get()).session?.retry?.maxAttempts,
752753
}),
753754
),
754755
Effect.catch(halt),

‎packages/opencode/src/session/retry.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const RETRY_INITIAL_DELAY = 2000
2525
export const RETRY_BACKOFF_FACTOR = 2
2626
export const RETRY_MAX_DELAY_NO_HEADERS = 30_000 // 30 seconds
2727
export const RETRY_MAX_DELAY = 2_147_483_647 // max 32-bit signed integer for setTimeout
28+
export const RETRY_MAX_ATTEMPTS = 5 // default max retry attempts
2829

2930
function cap(ms: number) {
3031
return Math.min(ms, RETRY_MAX_DELAY)
@@ -175,8 +176,10 @@ export function policy(opts: {
175176
provider: string
176177
parse: (error: unknown) => Err
177178
set: (input: { attempt: number; message: string; action?: Retryable["action"]; next: number }) => Effect.Effect<void>
179+
maxAttempts?: number
178180
}) {
179-
return Schedule.fromStepWithMetadata(
181+
const maxAttempts = opts.maxAttempts ?? RETRY_MAX_ATTEMPTS
182+
const retryPolicy = Schedule.fromStepWithMetadata(
180183
Effect.succeed((meta: Schedule.InputMetadata<unknown>) => {
181184
const error = opts.parse(meta.input)
182185
const retry = retryable(error, opts.provider)
@@ -194,6 +197,7 @@ export function policy(opts: {
194197
})
195198
}),
196199
)
200+
return Schedule.intersect(retryPolicy, Schedule.recurs(maxAttempts))
197201
}
198202

199203
export * as SessionRetry from "./retry"

0 commit comments

Comments
 (0)