Imp.Optimizer.GEPA.Callback behaviour (Imp v0.5.0)

Copy Markdown View Source

Synchronous, observational callbacks for GEPA optimization.

This behaviour mirrors the lifecycle in GEPA v0.1.1 while making the callback contract explicit for the BEAM. A callback is a module or {module, context}. Every hook is optional and takes (event, context): the event map, and the context the callback was registered with, or nil for a bare module.

Callbacks run synchronously in registration order. Their return values are ignored, so callbacks cannot replace optimizer state or decisions. A callback failure is isolated, reported as redacted telemetry and a warning, and does not prevent later callbacks from observing the event.

defmodule AuditCallback do
  @behaviour Imp.Optimizer.GEPA.Callback

  @impl true
  def on_iteration_end(event, owner) do
    send(owner, {:gepa_iteration, event.iteration, event.proposal_accepted})
  end
end

Imp.Optimizer.GEPA.new(metric, callbacks: [{AuditCallback, self()}])

Event maps use atom keys and expose immutable Elixir values. In particular, :state and :final_state are snapshots by value rather than mutable handles. Candidate identifiers, rejection reasons, trajectories, and exceptions stay as native Elixir terms rather than being stringified. This is the deliberate Elixir equivalent of upstream's observational state access.

Summary

Functions

Returns the callback lifecycle hook names in upstream order.

Types

callback()

@type callback() :: module() | {module(), term()}

callbacks()

@type callbacks() :: [callback()]

event()

@type event() :: map()

Callbacks

on_budget_updated(event, context)

(optional)
@callback on_budget_updated(event(), context :: term()) :: term()

on_candidate_accepted(event, context)

(optional)
@callback on_candidate_accepted(event(), context :: term()) :: term()

on_candidate_rejected(event, context)

(optional)
@callback on_candidate_rejected(event(), context :: term()) :: term()

on_candidate_selected(event, context)

(optional)
@callback on_candidate_selected(event(), context :: term()) :: term()

on_combee_aggregation(event, context)

(optional)
@callback on_combee_aggregation(event(), context :: term()) :: term()

on_combee_batch_selected(event, context)

(optional)
@callback on_combee_batch_selected(event(), context :: term()) :: term()

on_error(event, context)

(optional)
@callback on_error(event(), context :: term()) :: term()

on_evaluation_end(event, context)

(optional)
@callback on_evaluation_end(event(), context :: term()) :: term()

on_evaluation_skipped(event, context)

(optional)
@callback on_evaluation_skipped(event(), context :: term()) :: term()

on_evaluation_start(event, context)

(optional)
@callback on_evaluation_start(event(), context :: term()) :: term()

on_iteration_end(event, context)

(optional)
@callback on_iteration_end(event(), context :: term()) :: term()

on_iteration_start(event, context)

(optional)
@callback on_iteration_start(event(), context :: term()) :: term()

on_merge_accepted(event, context)

(optional)
@callback on_merge_accepted(event(), context :: term()) :: term()

on_merge_attempted(event, context)

(optional)
@callback on_merge_attempted(event(), context :: term()) :: term()

on_merge_rejected(event, context)

(optional)
@callback on_merge_rejected(event(), context :: term()) :: term()

on_minibatch_sampled(event, context)

(optional)
@callback on_minibatch_sampled(event(), context :: term()) :: term()

on_optimization_end(event, context)

(optional)
@callback on_optimization_end(event(), context :: term()) :: term()

on_optimization_start(event, context)

(optional)
@callback on_optimization_start(event(), context :: term()) :: term()

on_pareto_front_updated(event, context)

(optional)
@callback on_pareto_front_updated(event(), context :: term()) :: term()

on_proposal_end(event, context)

(optional)
@callback on_proposal_end(event(), context :: term()) :: term()

on_proposal_start(event, context)

(optional)
@callback on_proposal_start(event(), context :: term()) :: term()

on_reflective_dataset_built(event, context)

(optional)
@callback on_reflective_dataset_built(event(), context :: term()) :: term()

on_state_saved(event, context)

(optional)
@callback on_state_saved(event(), context :: term()) :: term()

on_valset_evaluated(event, context)

(optional)
@callback on_valset_evaluated(event(), context :: term()) :: term()

Functions

events()

@spec events() :: [atom()]

Returns the callback lifecycle hook names in upstream order.