# `Nous.Eval.Optimizer.Context`
[🔗](https://github.com/nyo16/nous/blob/v0.21.1/lib/nous/eval/optimizer/context.ex#L1)

What a `Nous.Eval.Optimizer.Strategy` searches with: the splits, the
components it may change, and helpers that do the budget accounting and
enforce the split and safety rules.

  * `evaluate/4` evaluates a profile on case ids (train, val or the
    guard cases), charging the budget; it returns
    `{:error, :budget_exhausted}` once the search budget is spent
  * `propose/4` asks the reflector for new text for some components from
    a train evaluation's records, then lints it (`Nous.Eval.Optimizer.Lint`)
  * `generate/4` asks for whole new versions (OPRO), each linted
  * `derive/4` turns proposed text into a sealed child profile
  * `feasible/3` runs the guard filter on a candidate

Fields a strategy reads: `:train`, `:val` (case ids), `:components`,
`:base_text` (component key => the agent's base text), `:k`,
`:minibatch_size`, `:seed`, `:rng`, `:mode` (`:round_robin | :all |
:coupled`), `:merge` (max merges, `0` disables), `:step`, plus
strategy-specific `:strategy_opts`.

# `t`

```elixir
@type t() :: %Nous.Eval.Optimizer.Context{
  adapter: term(),
  adapter_state: term(),
  agent: term(),
  base_text: term(),
  cache: term(),
  components: term(),
  feedback: term(),
  guard: term(),
  guard_cases: term(),
  heldout: term(),
  k: term(),
  ledger: term(),
  merge: term(),
  minibatch_size: term(),
  mode: term(),
  name: term(),
  provenance: term(),
  reflector: term(),
  rng: term(),
  seed: term(),
  step: term(),
  strategy_opts: term(),
  train: term(),
  val: term()
}
```

# `current_text`

```elixir
@spec current_text(t(), Nous.Profile.t()) :: %{required(String.t()) =&gt; String.t()}
```

The text `profile` gives each optimized component (its own, else the base).

# `derive`

```elixir
@spec derive(t(), Nous.Profile.t(), %{required(String.t()) =&gt; String.t()}, map()) ::
  {:ok, Nous.Profile.t()} | {:error, term()}
```

The child of `parent` with `updates` applied, sealed against the agent,
named like the optimization and carrying the optimizer's provenance.

# `evaluate`

```elixir
@spec evaluate(t(), Nous.Profile.t(), [String.t()], String.t(), :search | :final) ::
  {:ok, Nous.Eval.Optimizer.Evaluation.t()} | {:error, term()}
```

Evaluates `profile` on `case_ids`, labelled `split`, within what is left
of the search budget (the `:final` phase may also use the reserve held
back for the acceptance test).

Returns `{:ok, evaluation}` for a completed evaluation,
`{:error, :budget_exhausted}`, or `{:error, {:evaluation_failed, reason}}`
for an operational failure (the strategy should halt with it).

# `feasible`

```elixir
@spec feasible(t(), Nous.Eval.Optimizer.Candidate.t(), Nous.Profile.t()) ::
  :ok | {:error, term()}
```

The guard filter: `:ok` when `child` does not regress the guard-tagged
cases relative to `parent` (or there are none), `{:error, reason}`
otherwise. Evaluations of the guard cases are cached per profile.

# `generate`

```elixir
@spec generate(t(), [{Nous.Profile.t(), float()}], [String.t()], pos_integer()) ::
  {:ok, [%{required(String.t()) =&gt; String.t()}], [term()]} | {:error, term()}
```

Asks for `n` complete versions of every component from scored `history`
(`[{profile, score}]`, best first) and example train inputs. Versions
that fail the lint or change nothing are dropped and reported as
rejections: `{:ok, versions, rejections}`.

# `guard_non_regression`

```elixir
@spec guard_non_regression(
  Nous.Eval.Optimizer.Evaluation.t(),
  Nous.Eval.Optimizer.Evaluation.t()
) ::
  :ok | {:error, term()}
```

The default guard filter: on every guard-tagged case, the share of
samples whose `guard_verdict` scores all passed must not drop. Cases
without a `guard_verdict` score are ignored.

# `propose`

```elixir
@spec propose(
  t(),
  Nous.Eval.Optimizer.Candidate.t(),
  Nous.Eval.Optimizer.Evaluation.t(),
  [String.t()]
) ::
  {:ok, %{required(String.t()) =&gt; String.t()}} | {:error, term()}
```

Proposes new text for `keys` of `parent` from a train evaluation's
reflective records. Returns `{:ok, %{key => text}}` or `{:error, reason}`
(`:budget_exhausted`, `:unparseable`, `:unrequested_component`,
`:empty_proposal`, `{:heldout_leak, key}`, `{:fence_marker, key}`, ...).
Text identical to the parent's is dropped; a proposal that changes
nothing is `{:error, :no_change}`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
