# Read values, execute explicitly

Source: https://libtmux.org/en/elixir/pr-173/concepts/mental-model/

> Hold one captured graph for several decisions and run a command when you need another.

- Capture once when several decisions need the same graph, and keep that
  snapshot while you select and filter.
- Run a new command explicitly for another observation or an effect.
- Capture covers an interval, so fields can change during it. The value you
  get stays fixed while tmux keeps changing.

## The values you hold

| Value | What it represents | I/O boundary |
| --- | --- | --- |
| [`LibTmux.Server`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-server/>) | Executable, socket choice and environment for an existing server | Pure descriptor construction |
| [`LibTmux.Snapshot`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-snapshot/>) | An indexed graph captured over one interval | [`LibTmux.Server.snapshot/2`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-server-snapshot-2/>) captures a new value |
| [`LibTmux.Selection`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-selection/>) | Ordered items and their originating snapshot | Filtering and enumeration use captured values |
| [`LibTmux.Ref`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-ref/>) | An observed exact ID and server instance | Pure identity data; it reserves nothing |
| [`LibTmux.Control`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-control/>) | A handle pinned to one connection epoch | Status reads are local; startup explicitly attaches a client |
| [`LibTmux.Operation`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-operation/>) | Access to one retained operation record | Result/status reads are local; awaiting registers a separate waiter |

- Windows are physical objects. [`LibTmux.WindowLink`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-windowlink/>) gives a window's session
  membership, index and active state.
- A linked window can appear more than once, while its panes stay the same
  physical panes.
- Choose window links when membership matters. Choose panes when an action
  should address each physical pane once.

See [captured state](https://libtmux.org/en/elixir/pr-173/concepts/observations/).

## Coverage tells you which questions the graph can answer

- A loaded field holds a value, an empty string or a documented [`nil`](<https://elixir.hexdocs.pm/typespecs.html#literals>).
- `:unloaded` means this snapshot lacks the observation. `:unavailable` means
  the capability is absent. Neither means known absence.
- Relationship predicates need complete captured join domains. A missing pane
  relation cannot satisfy "no pane matches."
- Validation raises a structured coverage error before filtering, even for an
  empty selection.
- Capture again explicitly for more coverage. Existing selections keep their
  graph.

See [filtering](https://libtmux.org/en/elixir/pr-173/guides/filtering/).

## Server identity and connection epochs have different lifetimes

- A server instance identifies one tmux process incarnation. Native object
  IDs can be reused after that process exits.
- References keep the observed instance, so qualified control requests can
  reject known stale targets. One-shot commands keep a check/use race; an
  observed ID reserves nothing.
- An epoch identifies one control connection and its owned transport actors.
  The server instance can stay the same while the epoch changes.
- [`LibTmux.Control.reconnect/2`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-control-reconnect-2/>) returns the ready epoch, unchanged if it is
  already ready. Close first to require a replacement.
- A replacement waits for retirement and returns a new pinned handle. Old
  handles cannot submit into it, and reconnect never replays work.

## Waiting, command outcomes and retirement are separate

| Result | What the caller can conclude |
| --- | --- |
| [`LibTmux.Control.submit/3`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-control-submit-3/>) returns `{:ok, operation}` | Admission succeeded; inspect or await the retained operation |
| [`LibTmux.Operation.await/2`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-operation-await-2/>) returns an API timeout | This waiter ended; the operation may still advance |
| A canonical outcome is `:completed` or `:failed` with `:acknowledged` certainty | tmux supplied a command reply; arbitrary programs started by that command have their own lifetimes |
| A canonical outcome is `:skipped` with `:no_write` certainty | The operation was classified before dispatch |
| A canonical outcome is `:unknown` | Available evidence cannot determine its effects |
| Operation status reports `wire: :retired` | Reply attribution has retired; the retained record may still occupy capacity |
| [`LibTmux.Control.close/2`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-control-close-2/>) returns `:ok` | The pinned connection epoch retired; its enclosing supervisor remains running |

- Each await call has its own budget. A timeout or the caller's death ends
  that waiter without cancelling the operation.
- For wire-active work, [`LibTmux.Operation.stop/1`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-operation-stop-1/>) asks to close the whole
  pinned epoch and can affect siblings. An already retired operation returns
  `:ok` without closing its connection.
- [`LibTmux.Control.close/2`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-control-close-2/>) waits for connection retirement.
- Inspect retained results instead of replaying an uncertain mutation.
- A canonical outcome never changes once it exists. Bang operation APIs raise
  API and context errors, and still return failed, skipped and unknown
  outcomes as values.
- Wire retirement, record expiry and native process cleanup each have their
  own evidence. [Ownership](https://libtmux.org/en/elixir/pr-173/topics/ownership/) covers deadlines, bounds and
  remaining limits.

## Return conventions

These APIs answer different questions. Command completion does not mean that
a program started by tmux has finished.

| API | Normal return | Failure handling |
| --- | --- | --- |
| Domain actions such as [`LibTmux.Window.rename/4`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-window-rename-4/>) | `{:ok, result}` only for completion, retaining value and evidence | Unsuccessful outcomes use [`LibTmux.Action.CompletionError`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-action-completionerror/>); API and admitted execution errors retain their own types |
| Domain bangs such as [`LibTmux.Window.rename!/4`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-window-rename-bang-4/>) | Completed value | Raise the same errors with their evidence |
| [`LibTmux.Control.execute/3`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-control-execute-3/>) and [`LibTmux.Operation.await/2`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-operation-await-2/>) | `{:ok, canonical_result}`, including failed, skipped and unknown outcomes | Outer errors describe execution context or waiting; their bangs raise those errors and return canonical outcomes unchanged |
| [`LibTmux.Selection.one/2`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-selection-one-2/>) and [`LibTmux.Selection.one_or_nil/2`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-selection-one_or_nil-2/>) | `{:ok, item}`, or allowed `{:ok, nil}` | Cardinality errors are tuples; bangs raise them. Predicate and coverage validation can raise in either form |
| Relationship helpers such as [`LibTmux.Session.panes/2`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-session-panes-2/>) | Ordered selection, or observation/nil for to-one relationships; no I/O | Identity, coverage and ambiguity errors raise |
| [`LibTmux.Control.with_connection/2`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-control-with_connection-2/>) | `{:ok, callback_value}` after cleanup, even when that value is `{:error, reason}` | Startup errors return errors; callback raise/throw/exit propagates after cleanup. Unproved cleanup returns [`LibTmux.Control.ScopeFailure`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-control-scopefailure/>) preserving the primary outcome |

- The [recovery example](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/docs/recovery.md) handles lower-level execution and
  cleanup separately.
- Option validation may raise. A name without a bang does not promise that
  every malformed call returns an error tuple.
