Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/core/src/v1/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ export const Info = Schema.Struct({
mcp_timeout: Schema.optional(PositiveInt).annotate({
description: "Timeout in milliseconds for model context protocol (MCP) requests",
}),
max_retries: Schema.optional(PositiveInt).annotate({
description:
"Maximum number of retry attempts for transient provider errors before stopping the session. When not set, retries are unbounded.",
}),
policies: Schema.optional(Schema.mutable(Schema.Array(ConfigExperimental.Policy))).annotate({
description: "Policy statements applied to supported resources, such as provider access",
}),
Expand Down
1 change: 1 addition & 0 deletions packages/opencode/src/session/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,7 @@ export const layer = Layer.effect(
SessionRetry.policy({
provider: input.model.providerID,
parse,
maxRetries: (yield* config.get()).experimental?.max_retries,
set: (info) => {
// TODO(v2): Temporary dual-write while migrating session messages to v2 events.
const event = mirrorAssistant
Expand Down
2 changes: 2 additions & 0 deletions packages/opencode/src/session/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,14 @@ export function policy(opts: {
provider: string
parse: (error: unknown) => Err
set: (input: { attempt: number; message: string; action?: Retryable["action"]; next: number }) => Effect.Effect<void>
maxRetries?: number
}) {
return Schedule.fromStepWithMetadata(
Effect.succeed((meta: Schedule.InputMetadata<unknown>) => {
const error = opts.parse(meta.input)
const retry = retryable(error, opts.provider)
if (!retry) return Cause.done(meta.attempt)
if (opts.maxRetries !== undefined && meta.attempt > opts.maxRetries) return Cause.done(meta.attempt)
return Effect.gen(function* () {
const wait = delay(meta.attempt, SessionV1.APIError.isInstance(error) ? error : undefined)
const now = yield* Clock.currentTimeMillis
Expand Down
32 changes: 32 additions & 0 deletions packages/opencode/test/session/retry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,38 @@ describe("session.retry.delay", () => {
})
}),
)

it.instance("policy stops after max retry attempts", () =>
Effect.gen(function* () {
const sessionID = SessionID.make("session-retry-max-test")
const error = apiError({ "retry-after-ms": "0" })
const status = yield* SessionStatus.Service

const step = yield* Schedule.toStepWithMetadata(
SessionRetry.policy({
provider: "test",
parse: Schema.decodeUnknownSync(SessionLegacy.APIError.Schema),
maxRetries: 1,
set: (info) =>
status.set(sessionID, {
type: "retry",
attempt: info.attempt,
message: info.message,
next: info.next,
}),
}),
)
yield* step(error)
const result = yield* Effect.exit(step(error))

expect(result._tag).toBe("Failure")
expect(yield* status.get(sessionID)).toMatchObject({
type: "retry",
attempt: 1,
message: "boom",
})
}),
)
})

describe("session.retry.retryable", () => {
Expand Down
Loading