Cyclium.Strategy.Retry (Cyclium v0.1.10)

Copy Markdown View Source

Lightweight retry helper for strategies.

Tracks attempt counts inside the strategy's state map (under :__retries) so strategies can declaratively retry failed steps without hand-rolling counters.

Usage

alias Cyclium.Strategy.Retry

# On synthesis success — reset the counter
def handle_result(state, %{kind: :synthesis}, {:ok, result}) do
  {:ok, state |> Retry.reset(:synthesis) |> Map.put(:assessment, result)}
end

# On synthesis failure — retry up to 3 times with 2s backoff
def handle_result(state, %{kind: :synthesis} = step, {:error, _}) do
  case Retry.check(state, step, max_attempts: 3, backoff_ms: 2_000) do
    {:retry, new_state}             -> {:retry, new_state}
    {:give_up, _attempts, new_state} -> {:abort, "synthesis_failed_after_retries"}
  end
end

The strategy's next_step/2 doesn't need special logic — when handle_result returns {:retry, state}, the runner calls do_loop which calls next_step again. The strategy should naturally re-emit the same step type based on its phase/state (which hasn't changed, only :__retries was updated).

Summary

Functions

Evaluates whether a failed step should be retried.

Resets retry tracking for a given key. Call on success to clear the counter so a later failure of the same step kind gets fresh attempts.

Resets all retry tracking.

Functions

check(state, step, opts \\ [])

Evaluates whether a failed step should be retried.

Returns:

  • {:retry, updated_state} — attempt recorded, caller should return {:retry, state} from handle_result
  • {:give_up, attempt_count, updated_state} — max attempts exhausted, counter reset

Options

  • :max_attempts — total attempts including the original (default 3)
  • :backoff_ms — milliseconds to sleep before retry, 0 for immediate (default 0)
  • :step_key — key for tracking this retry series (default step.kind)

reset(state, key)

Resets retry tracking for a given key. Call on success to clear the counter so a later failure of the same step kind gets fresh attempts.

reset_all(state)

Resets all retry tracking.