server:observe(session, options) returns a Request for an observation lease.
Pass a session handle from the same Server's snapshot or creation result. Leases
for one session share one persistent client. Other commands use the
process command API.
Observation needs tmux 3.7 or newer.
- On an older server,
server:observereturnsunsupportedwithrequired = "3.7"andeffect = "not_sent", before any lease, process or client exists. A short control attach can crash servers that lack the 3.7 initialization fixes. - Commands, typed operations, snapshots and polling captures work from 3.3a.
To wait for text on any release, use
pane:wait_for_text.
Opening checks the daemon before spawning. The client's first command is a
fence on the daemon's pid, start time and version, so a replaced daemon
attaches nothing and reports stale_generation. Every client uses -N, so a
missing session fails without creating a session or linking a window. An
endpoint owns at most eight observation clients.
The observation offers:
watch_pane(pane, options): a ready pane-output watch.watch_pane_lines(pane, options): a ready watch for complete lines from a pane.watch_notifications(options): a ready notification watch that keeps unknown events and raw lines.subscribe_format(pane, field_names, options): a typed format watch over generated pane fields.coverage(): a copy of the session and pane coverage, and the connection generation.close(): closes this lease's watches. The last close waits for native cleanup.
Handles and options:
- Handles must belong to the same Server. Their private identities pick the
targets, so overwriting a public
referencemethod cannot redirect one. - Options are copied on submission. Shared connection limits must agree across
leases, or the call returns
option_conflict. Separate Server handles keep separate pools, even for the same socket path.
Opening and closing:
- Cancelling an open releases only that caller's claim. Startup continues for the others, and cancelling the last claim closes the client.
- Opening during final cleanup returns
closing. Await the finalclose()before reopening. A new connection has a new generation and needs new watches. - A failed startup keeps its pool entry until cleanup ends. A new open on a failed shared connection returns its recorded loss error. Close the old leases to open a fresh one.
Coverage:
- Creating a watch installs its receiver before a
list-panescoverage check on the same connection. A pane outside the session returnsuncovered_pane. - A layout change anywhere in the observed session closes its pane watches
with
observation_gap. A split in another window can end your watch even when the watched pane stays the same size. - Adding or closing a window in the session also ends its pane watches.
An
unlinked-window-closeends watches for the covered window tmux closed. - If the connection is still live, call
observation:watch_pane(pane):await()again. The new watch checks coverage and receives future output. It cannot replay missed output. If the connection itself was lost, close the old leases and open a new observation. - These checks are observations, not a transaction. Capture is a separate operation with no lossless handoff to the stream.
watch:next({ timeout = milliseconds }) returns a Request for one event. An
absolute deadline uses the runtime driver's clock. When you supply both,
the runtime validates both and uses the earlier expiry.
timeout must be from 0 to 2,147,483,647 milliseconds. deadline must be
finite and at most that far in the future. An invalid limit returns
invalid_request. If the request's deadline has already elapsed at
submission, it returns deadline_exceeded before reading an event.
- Only one read may be pending per watch; another returns
concurrent_read. - Cancellation or deadline expiry detaches that waiter. The next read can receive events still buffered by the watch.
watch:close()is explicit and idempotent. A watch you closed returnsnil, nil. A lost connection returnsnil, err.
Event fields:
| Event | Fields |
|---|---|
| Pane output | kind = "output", pane, data, sequence, generation, server_generation; extended output adds age and metadata |
| Format | kind = "format", a typed value, session_id, window_id, the window link index and pane |
datais a Lua byte string, NUL and non-UTF-8 bytes included.- A sequence orders events within one connection. It does not mean the history is complete or that a program finished.
- Format watches take catalog field names only. An unknown or unsupported field fails before anything is written, and caller text never becomes a format expression.
- Registration acknowledgement means the watch is ready. Values arrive on tmux's one-second sampling timer, so readiness does not wait for the first sample. tmux's title cleaning and other field behavior show in the values. Closing a format watch unregisters it.
Open a line watch before sending the request. Give the read a literal reply prefix and a timeout in milliseconds.
local lines = must(observation:watch_pane_lines(pane):await())
must(pane:send_text("sum 19 23"):await())
must(pane:send_keys({ "Enter" }):await())
local line, err = lines:next({ prefix = "result=", timeout = 5000 }):await()The example runs a calculator in a
private server and prints result=42. The terminal may echo the request
and the app may print progress lines. Choose a prefix the app reserves for
replies.
$ lua examples/api/read_pane_line.lualines:next()returns a Request for the next line in order. It joins output across events and removes the LF and one preceding CR, if present. Empty lines return an empty string. Other bytes remain as received.- The first line starts at the first byte received by the watch. If it opens while a line is being printed, that line can start mid-record.
- With
prefix, a read consumes earlier lines until it finds a match. Chatter uses the same deadline. Usetimeoutfrom submission or an absolute monotonicdeadline. With neither option it waits until a matching line or a closed watch. - A read that times out returns
deadline_exceeded. A timeout or cancellation keeps the current partial line and any unread lines for the next call. Lines already skipped by a prefix stay consumed. - One read may be pending per line watch. A second returns
concurrent_read. A separatewatch_paneon the same pane has its own queue. Closing the line watch normally returnsnil, nilto its reader.
Line watches allow 64 KiB before each LF, including a CR
(max_line_bytes). They queue at most 1,024 framed lines (max_lines)
and 1 MiB total (max_bytes), including input chunks held during framing,
partial lines and buffer overhead.
The runtime dispatches framing and prefix searches in slices. A long line
gets a fresh slice when its fragments must be joined into one Lua string.
Exceeding a watch's buffer limit or losing coverage closes it with
observation_gap and discards its buffered output.
Defaults:
| Limit | Default |
|---|---|
| Pending housekeeping replies | 128 |
| Watches | 128 |
| Retained connection data | 4 MiB |
| Events per watch | 1,024, or 1 MiB |
- Each claim, shared attachment and public watch takes one runtime resource slot. The runtime allows 128 in total. Runtime byte and logical-request limits also apply, and watch bytes include event metadata.
- Overflow closes the affected watch with
observation_gap. On an event watch, the partial result reports the discarded event count and decoded pane bytes. Other watches and the shared reader continue. tmux's%pausealso breaks pane continuity. - There is no silent drop, no output coalescing, and no claim that tmux pause is lossless backpressure.
- The reader parses bounded slices whether or not watches are consumed. Completion callbacks run through the runtime scheduler, so a callback must not block the shared Lua event loop.
- A persistent connection holds a resource lease, not an active process slot.
A pending
nextuses the logical-request lane. - A write or framing failure closes the connection and fails pending callers. No command or pane input is retried or replayed.
- A normal root return closes leases after owned requests finish. Closing the endpoint, or a new generation, also closes its observation clients.
- Cleanup waits for the client to exit, its pipes to close and pending writes to finish. The post-exit pipe drain is bounded at 250 ms. An explicit close ends stdin first, then after 100 ms signals only the owned client, and escalates after another 100 ms. It never signals the tmux daemon or a pane's program.
- The client runs
attach-session -E -f ignore-size,active-paneon an exact session ID. It does not select, resize, detach or update the session environment. - Client listings, attachment state, focus hooks and lifecycle policy stay
observable, and hooks can change state themselves. The
focus-eventsoption is unchanged; disabling it cannot suppress every attachment focus hook, and tmux offers no client flag that guarantees it. - No hidden sessions are created and no window links change.
- The client omits
-r. On tmux 3.7 and 3.7c a read-only observer can make independentsend-keysfail with "client is read-only". The private writer restricts observation commands without it. - Closing an observer removes its client. A policy such as
destroy-unattachedcan then destroy its session, and no startup check can promise otherwise when options or other clients can change. The library does not rewrite borrowed options or keep hidden clients to prevent that. - Observation assumes the server's native command semantics. Aliases and hooks can change built-in commands, and control framing does not authenticate server output. It does not accelerate general commands.
Reconnect and subscription replay are not implemented. A new connection has a new generation: establish new watches and treat the time between connections as a gap.