Skip to content
Merged
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: 3 additions & 1 deletion packages/core/src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,12 @@ export const locationLayer = Layer.effect(
return connection
}),
cooldown: Effect.fn("Integration.connection.cooldown")(function* (id, owner, retryAfterMs = 0) {
const selected = selection.get(`${id}:${owner}`)
const ownerKey = `${id}:${owner}`
const selected = selection.get(ownerKey)
if (!selected) return
console.log(`[credential-pool] ${id} owner=${owner}: penalizing ${selected.slice(0, 12)}... retryAfterMs=${retryAfterMs}`)
pools.get(id)?.pool.penalize(selected, retryAfterMs, yield* Clock.currentTimeMillis)
selection.delete(ownerKey)
}),
release: Effect.fn("Integration.connection.release")(function* (id, owner) {
const key = `${id}:${owner}`
Expand Down
14 changes: 14 additions & 0 deletions packages/opencode/src/session/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,27 @@ const layer = Layer.effect(
let aborted = false
let currentStreamInput: LLM.StreamInput | undefined
let failoverAttempted = false
let retryKeyCount = 0

const switchProvider = Effect.fnUntraced(function* (error: SessionRetry.Err) {
if (failoverAttempted) return false
if (ctx.currentText || Object.keys(ctx.reasoningMap).length > 0 || Object.keys(ctx.toolcalls).length > 0)
return false
if (!CredentialFailover.eligible(error)) return false
if (ctx.model.providerID !== "opencode" && ctx.model.providerID !== "opencode-go") return false

const allAuth = yield* auth.all().pipe(Effect.orDie)
const currentAuth = allAuth[ctx.model.providerID]
if (currentAuth && currentAuth.type === "api") {
const keys = currentAuth.metadata?.keys ? JSON.parse(currentAuth.metadata.keys) : [currentAuth.key]
if (Array.isArray(keys) && retryKeyCount < keys.length - 1) {
retryKeyCount++
console.log(`[failover-v1] ${ctx.model.providerID} key ${retryKeyCount}/${keys.length} failed, trying next key before switching provider`)
return false
}
console.log(`[failover-v1] ${ctx.model.providerID} all ${keys.length} keys exhausted, switching provider`)
}

const destination = ctx.model.providerID === "opencode" ? "opencode-go" : "opencode"
const all = (yield* providers.list()) as Record<string, Provider.Info>
const destinationProvider = Object.values(all).find((item) => String(item.id) === destination)
Expand Down
Loading