Module: OpenAICompatibleErrors::Retry

Defined in:
lib/openai_compatible_errors/retry.rb

Constant Summary collapse

TRANSIENT =
%i[rate_limit timeout network upstream server stream].freeze
PERMANENT =
%i[authentication permission quota validation not_found
payload_too_large endpoint].freeze

Class Method Summary collapse

Class Method Details

.decide_retry(error, context, policy: RetryPolicy.new, random: nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/openai_compatible_errors/retry.rb', line 122

def decide_retry(error, context, policy: RetryPolicy.new, random: nil)
  return plan(:manual_decision, :invalid_context) unless error.is_a?(ApiError)
  return plan(:manual_decision, :invalid_context) unless context.is_a?(RetryContext)
  return plan(:manual_decision, :invalid_context) unless policy.is_a?(RetryPolicy)
  return plan(:manual_decision, :invalid_context) unless context.attempt.positive? &&
    context.elapsed_ms >= 0 && !context.method.empty?

  return plan(:do_not_retry, :partial_stream_output) if context.has_stream_output ||
    context.phase == :sse_after_output
  return plan(:do_not_retry, :request_completed) if context.phase == :completed
  return plan(:do_not_retry, :caller_aborted) if error.category == :aborted
  return plan(:manual_decision, :conflict_requires_resolution) if error.category == :conflict
  return plan(:do_not_retry, :permanent_error) if PERMANENT.include?(error.category)
  return plan(:do_not_retry, :replay_unsafe) if context.replay_safety == :unsafe
  return plan(:manual_decision, :unknown_phase) if context.phase == :unknown
  return plan(:manual_decision, :unknown_replay_safety) if context.replay_safety == :unknown
  return plan(:manual_decision, :unclassified_error) unless TRANSIENT.include?(error.category)
  return plan(:do_not_retry, :attempt_budget_exhausted) if context.attempt >= policy.max_attempts
  return plan(:do_not_retry, :time_budget_exhausted) if context.elapsed_ms >= policy.max_elapsed_ms

  if error.retry_after_ms
    delay = error.retry_after_ms
    return plan(:do_not_retry, :retry_after_exceeds_budget) if delay > policy.max_delay_ms ||
      context.elapsed_ms + delay > policy.max_elapsed_ms

    return RetryPlan.new(action: :retry, reason: :transient_and_replay_safe,
                         delay_ms: delay, delay_source: :server)
  end

  exponent = [context.attempt - 1, 30].min
  cap = [policy.base_delay_ms * (2**exponent), policy.max_delay_ms].min
  delay =
    if policy.jitter == :none
      cap
    else
      sample = random ? random.call : Kernel.rand
      return plan(:manual_decision, :invalid_context) unless sample.is_a?(Numeric) &&
        sample.finite? && sample >= 0 && sample <= 1

      (cap * sample).round
    end
  return plan(:do_not_retry, :time_budget_exhausted) if context.elapsed_ms + delay > policy.max_elapsed_ms

  RetryPlan.new(action: :retry, reason: :transient_and_replay_safe,
                delay_ms: delay, delay_source: :backoff)
rescue StandardError
  plan(:manual_decision, :invalid_context)
end