GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — FIX: cap LLM retries at 5 instead of retrying forever · wanhongqing123/opencode@581476c

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/opencode
    • src/session
      • retry.ts
    • test/session
      • retry.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: packages/opencode/src/session/retry.ts
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ export const RETRY_INITIAL_DELAY = 2000
2727
export const RETRY_BACKOFF_FACTOR = 2
2828
export const RETRY_MAX_DELAY_NO_HEADERS = 30_000 // 30 seconds
2929
export const RETRY_MAX_DELAY = 2_147_483_647 // max 32-bit signed integer for setTimeout
30+
// Give up after this many retries instead of looping forever. Quota errors are the
31+
// motivating case: the provider says the limit resets in two days, so attempt #6 is
32+
// no more likely to succeed than attempt #600 — it just hides the failure from the
33+
// user while the session sits there retrying.
34+
export const RETRY_MAX_ATTEMPTS = 5
3035

3136
const RETRYABLE_MESSAGE_PATTERNS = [
3237
/429|500|502|503|504|524/i,
@@ -209,6 +214,8 @@ export function policy(opts: {
209214
const error = opts.parse(meta.input)
210215
const retry = retryable(error, opts.provider)
211216
if (!retry) return Cause.done(meta.attempt)
217+
// meta.attempt is 1 on the first retry, so this allows attempts 1..RETRY_MAX_ATTEMPTS.
218+
if (meta.attempt > RETRY_MAX_ATTEMPTS) return Cause.done(meta.attempt)
212219
return Effect.gen(function* () {
213220
const wait = delay(meta.attempt, SessionV1.APIError.isInstance(error) ? error : undefined)
214221
const now = yield* Clock.currentTimeMillis
Collapse file

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

Copy file name to clipboardExpand all lines: packages/opencode/test/session/retry.test.ts
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,49 @@ describe("session.retry.delay", () => {
115115
})
116116
}),
117117
)
118+
119+
it.instance("stops retrying after RETRY_MAX_ATTEMPTS instead of looping forever", () =>
120+
Effect.gen(function* () {
121+
const sessionID = SessionID.make("session-retry-cap-test")
122+
// Still classified as retryable — the cap, not retryable(), has to be what stops it.
123+
const error = apiError({ "retry-after-ms": "0" })
124+
const status = yield* SessionStatus.Service
125+
const attempts: number[] = []
126+
127+
const step = yield* Schedule.toStepWithMetadata(
128+
SessionRetry.policy({
129+
provider: "test",
130+
parse: Schema.decodeUnknownSync(SessionV1.APIError.Schema),
131+
set: (info) => {
132+
attempts.push(info.attempt)
133+
return status.set(sessionID, {
134+
type: "retry",
135+
attempt: info.attempt,
136+
message: info.message,
137+
next: info.next,
138+
})
139+
},
140+
}),
141+
)
142+
143+
for (let i = 0; i < SessionRetry.RETRY_MAX_ATTEMPTS; i++) yield* step(error)
144+
145+
// One step past the cap: the schedule is exhausted, so the step fails
146+
// (Cause.Done) rather than scheduling another wait.
147+
const exhausted = yield* Effect.exit(step(error))
148+
expect(exhausted._tag).toBe("Failure")
149+
150+
// Exactly RETRY_MAX_ATTEMPTS waits, numbered 1..N with no gaps or repeats,
151+
// and nothing recorded for the step that gave up.
152+
expect(attempts).toEqual(Array.from({ length: SessionRetry.RETRY_MAX_ATTEMPTS }, (_, i) => i + 1))
153+
}),
154+
)
155+
156+
test("caps retries at five", () => {
157+
// Pinned deliberately: the point of the cap is that it is small. If someone
158+
// raises it, that should be a conscious edit here, not a silent drift.
159+
expect(SessionRetry.RETRY_MAX_ATTEMPTS).toBe(5)
160+
})
118161
})
119162

120163
describe("session.retry.retryable", () => {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.