# `ASM.Session.OwnerGuard`
[🔗](https://github.com/nshkrdotcom/agent_session_manager/blob/v0.15.0/lib/asm/session/owner_guard.ex#L1)

Binds a session subtree's lifetime to an owner process.

The provider form of `ASM.query/3` starts a session, runs one prompt, and
stops the session from the caller's `after` block. An untrappable
`Process.exit(caller, :kill)` skips that block, and the supervised subtree
keeps running with its provider process group still attached. This guard
closes that window: it monitors the owner and terminates the subtree child
through `ASM.Session.Supervisor.stop_session/2` when the owner goes down.

Two placement facts matter:

- stopping a session subtree is `ASM.Session.Server` stopping itself is not
  enough, because `ASM.Session.Subtree` would restart it; the subtree child
  itself has to be terminated
- the guard therefore lives OUTSIDE the subtree it guards, under
  `ASM.Session.GuardSupervisor`. A guard supervised by the subtree it is
  terminating would be shut down while blocked in that very call

The guard also monitors the subtree, so an ordinary `ASM.stop_session/1`
retires the guard instead of leaving it waiting on an owner forever.

# `state`

```elixir
@type state() :: %{
  session_id: String.t(),
  supervisor: GenServer.server(),
  owner_ref: reference(),
  subtree_ref: reference()
}
```

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `start_link`

```elixir
@spec start_link(keyword()) :: GenServer.on_start()
```

---

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