# `Minga.Extension.Instance.StopContext`
[🔗](https://github.com/jsmestad/minga/blob/main/lib/minga/extension/instance/stop_context.ex#L1)

Phase-owned state and transitions for one extension stop operation.

# `completion`

```elixir
@type completion() :: :stop | {:start_failure, term(), [GenServer.from()]}
```

# `queued_start`

```elixir
@type queued_start() ::
  {:current, GenServer.from()}
  | {:deferred, GenServer.from(), Minga.Extension.Entry.t()}
```

# `stage`

```elixir
@type stage() ::
  :quiescing
  | :draining
  | :editor_finalize
  | :editor_unload
  | :terminating
  | :cleanup
```

# `t`

```elixir
@type t() :: %Minga.Extension.Instance.StopContext{
  completion: completion(),
  drain_done?: boolean(),
  drain_ref: reference() | nil,
  drain_timer: reference() | nil,
  editor_done?: boolean(),
  editor_ref: reference() | nil,
  exit_kind: :explicit | :normal | :crash,
  exit_reason: term(),
  finalizer_error: term() | nil,
  runtime: Minga.Extension.Instance.Runtime.t() | nil,
  stage: stage(),
  start_waiters: [queued_start()],
  token: reference() | nil,
  waiters: [GenServer.from()],
  worker: Minga.Extension.Instance.Worker.t() | nil
}
```

# `attach_finalizer`

```elixir
@spec attach_finalizer(t(), Minga.Extension.Instance.Worker.t()) :: t()
```

Attaches the worker and acknowledgement identity for an editor finalizer.

# `attach_worker`

```elixir
@spec attach_worker(t(), Minga.Extension.Instance.Worker.t()) :: t()
```

Records the worker performing termination or cleanup.

# `begin_cleanup`

```elixir
@spec begin_cleanup(t(), Minga.Extension.Instance.Worker.t()) :: t()
```

Moves to cleanup and records its worker.

# `begin_drain`

```elixir
@spec begin_drain(t(), reference(), reference(), reference()) :: t()
```

Begins lease draining.

# `begin_editor_finalize`

```elixir
@spec begin_editor_finalize(t()) :: t()
```

Moves directly to editor finalization when no active lease exists.

# `begin_editor_unload`

```elixir
@spec begin_editor_unload(t()) :: t()
```

Moves the stop barrier to editor unload.

# `begin_termination`

```elixir
@spec begin_termination(t()) :: t()
```

Moves the stop operation to runtime termination.

# `cleanup_retry`

```elixir
@spec cleanup_retry(
  t(),
  keyword()
) :: t()
```

Builds a clean retry after cleanup failed.

# `clear_drain_timer`

```elixir
@spec clear_drain_timer(t()) :: t()
```

Clears a fired or cancelled drain timer.

# `editor_effects_done`

```elixir
@spec editor_effects_done(t(), :ok | {:error, term()}) :: t()
```

Records completion of editor effects and merges a finalizer result.

# `fail_finalizer`

```elixir
@spec fail_finalizer(t(), term()) :: t()
```

Records an explicit finalizer failure.

# `finish_work`

```elixir
@spec finish_work(t()) :: t()
```

Clears completed or failed worker bookkeeping.

# `join`

```elixir
@spec join(t(), GenServer.from()) :: t()
```

Adds another stop caller.

# `merge_finalizer_result`

```elixir
@spec merge_finalizer_result(t(), :ok | {:error, term()}) :: t()
```

Merges an unload or completion result into finalizer failure state.

# `new`

```elixir
@spec new(
  GenServer.from() | nil,
  :explicit | :normal | :crash,
  term(),
  Minga.Extension.Instance.Runtime.t() | nil
) :: t()
```

Builds an initial quiescing stop transition.

# `queue_start`

```elixir
@spec queue_start(t(), queued_start()) :: t()
```

Queues a start intent behind this stop.

# `record_terminal_failure`

```elixir
@spec record_terminal_failure(t(), term()) :: t()
```

Records a terminal-runtime cleanup failure.

# `remember_failed_drain`

```elixir
@spec remember_failed_drain(t(), reference(), reference()) :: t()
```

Remembers a failed drain request so unload can be aborted correctly.

# `replace_start_failure`

```elixir
@spec replace_start_failure(t(), term(), [GenServer.from()]) :: t()
```

Replaces the failed-start reason after projection also fails.

# `retry_cleanup`

```elixir
@spec retry_cleanup(t(), GenServer.from()) :: t()
```

Starts a cleanup-only retry for one caller.

# `retry_quiescence`

```elixir
@spec retry_quiescence(t(), GenServer.from()) :: t()
```

Restarts quiescence retry state for one caller.

# `rollback_start`

```elixir
@spec rollback_start(t(), term(), [GenServer.from()], [GenServer.from()]) :: t()
```

Records that this stop is rolling back a failed start.

# `runtime_exited`

```elixir
@spec runtime_exited(t()) :: t()
```

Clears runtime identity after its DOWN arrives during stop.

# `source_drained`

```elixir
@spec source_drained(t()) :: t()
```

Marks the code source drained and clears timer ownership.

# `transfer_waiters`

```elixir
@spec transfer_waiters(t(), [GenServer.from()]) :: t()
```

Replaces stop waiters when a queued stop is transferred from a start.

---

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