# `Spectre.Operation.Controller`
[🔗](https://github.com/elchemista/spectre/blob/0.4.0/lib/spectre/operation/controller.ex#L1)

Behaviour shared by Work, Vigil and library-owned operational controllers.

Controller callbacks are deterministic reducers. They run briefly inside
the Agent owner; external work must be represented by a registered
`Spectre.Operation.Request` and is executed by a temporary Runner.

A `kind: :directive` Definition with `can_start: [:work]` may return
`start_loops: [{:work, controller, input, opts}]` from a successful
`apply_result/4`. Each intent requires a stable `:intent_id`; the Agent
validates and commits the parent Result and every new Work atomically.
Work and Vigil controllers cannot acquire this capability.

# `context`

```elixir
@type context() :: map()
```

# `apply_result`

```elixir
@callback apply_result(
  term(),
  Spectre.Operation.Request.t(),
  Spectre.Operation.Result.t(),
  context()
) ::
  {:ok, term()} | {:ok, term(), keyword() | map()} | {:error, term()}
```

# `apply_update`
*optional* 

```elixir
@callback apply_update(term(), term(), Spectre.Operation.Update.t(), context()) ::
  {:ok, term(), term()}
  | {:ok, term(), term(), keyword() | map()}
  | {:error, term()}
```

# `budget_exhausted`
*optional* 

```elixir
@callback budget_exhausted(term(), map(), context()) ::
  :terminate | {:terminate, term()} | {:terminate, term(), map() | keyword()}
```

# `checkpoint_compatible?`
*optional* 

```elixir
@callback checkpoint_compatible?(term(), term()) :: boolean()
```

# `complete`

```elixir
@callback complete(term(), context()) ::
  :continue
  | false
  | {:complete, term()}
  | {:blocked, term()}
  | {:error, term()}
```

# `handle_trigger`
*optional* 

```elixir
@callback handle_trigger(term(), term(), context()) ::
  {:ok, term()} | {:ok, term(), keyword() | map()} | {:error, term()}
```

# `init`

```elixir
@callback init(term(), context()) :: {:ok, term()} | {:error, term()}
```

# `next`

```elixir
@callback next(term(), context()) ::
  {:run, Spectre.Operation.Request.t()}
  | {:run, atom() | String.t(), term()}
  | {:wait, Spectre.Operation.Wait.t()}
  | {:blocked, term()}
  | {:complete, term()}
  | {:error, term()}
```

---

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