Events
zterm can report what happens in it as a stream of events: windows, tabs and panes opening and closing, titles changing, panes changing size and place, and focus moving. This page is the reference for anyone writing a program that receives those events, such as the Zestful daemon or a test harness.
Three sections change how a receiver has to be written. Read them before you write one:
If this page and zterm's behaviour disagree, one of them has a bug. Please report which you found.
Turning events on
Events are off unless you do one of these. zterm decides once, when it starts, so restart zterm after changing either.
- Set the
ZESTFUL_EVENTS_URLenvironment variable. zterm sends events to that address, with no authentication header. - Set
send_events = truein your settings file. zterm then reads two files in~/.config/zestful/, both written by the Zestful daemon:terminal-events-urlandlocal-token. It sends the token with every request as theX-Zestful-Tokenheader.
The variable wins if you do both. The daemon's files do nothing without the setting, so a running daemon does not turn events on. With events off, zterm sends nothing and stores nothing.
The URL must be plain http:// on a loopback address: 127.0.0.0/8, ::1 or
localhost. zterm turns events off for any other URL, https:// included.
zterm sends a localhost URL to 127.0.0.1, and it ignores proxy settings
such as HTTP_PROXY.
Delivery
Each event is one HTTP POST with Content-Type: application/json. The body is
one envelope, described below. zterm never sends several events in one request.
An event is delivered when the receiver answers 2xx. zterm does not follow
redirects: any other answer, a 3xx included, is a failed delivery.
Delivery is best effort. A worker thread sends events from a queue of 1024, so a slow or absent receiver never blocks the terminal. If sending keeps failing and the queue fills, zterm drops the oldest events first.
Expect to miss events. Lost events explains how to tell how many.
The envelope
Every event has the same outer fields. All of them are always present.
| field | JSON type | meaning |
|---|---|---|
event_id |
string | A ULID: 26 characters of uppercase Crockford base32. It is unique. It sorts by creation time to the millisecond only, so do not use it to break ties within a millisecond. |
event_type |
string | A dotted name from Event types. |
source |
string | Always "terminal". |
sender_id |
string | A ULID that names the zterm process that sent the event. Each run of zterm gets a new one, so two zterm processes on one machine are two senders. An empty string means the sender does not identify itself. zterm never sends an empty one. |
event_ts |
number (u64) | Unix time in milliseconds, not seconds. |
seq |
number (u32) | A counter for each sender. It starts at 1 and goes up by one for each event. 0 means the sender does not count. zterm never sends 0. |
dropped |
number (u32) | How many events this sender has discarded so far, as of the moment this event left the queue. |
schema_version |
number (u32) | Always 1. |
context |
object | {"app": "zestful-terminal", "version": "<zterm version>"}. Exactly those two keys today. |
payload |
object | The fields for this event type. Never null. A later version may add keys. |
{
"event_id": "01HQ8ZK9V4X7YQJ4M2N6P8R0TB",
"event_type": "focus.gained",
"source": "terminal",
"sender_id": "01HQ8ZK4B7WN0G2FQ5T3XJ9MDC",
"event_ts": 1753459200123,
"seq": 47,
"dropped": 0,
"schema_version": 1,
"context": { "app": "zestful-terminal", "version": "0.1.0" },
"payload": {
"uri": "workspace://zestful-terminal/window:01HQ.../tab:01HQ.../pane:01HQ..."
}
}
Payload values
Four kinds of value appear in payloads:
uriis a string, the full address of a pane:workspace://zestful-terminal/window:<W>/tab:<T>/pane:<P>. Each of the three ids is a 26-character ULID. zterm always sends this exact form. The control socket also accepts other forms, such as lower-case ids and parts in a different order. If you key a map by URI, normalise any URI that did not come from this stream.window_idandtab_idare strings, each a bare 26-character ULID with no prefix. Window, tab and layout events carry them. Pane events carry auri, which already contains both.pidis a number (u32). It is the process zterm started in the pane, which is the shell. Seepidis the shell.geometryis an object, a pane's size and place in character cells. Onlylayout.changedcarries it. See Layout.
Ids last for one run of zterm. Splitting panes, dragging tabs and reordering them leave ids unchanged. The next run creates new ones.
Event types
There are thirteen. Every payload key listed is required. One value can be
null: tty in pane.process_changed. No other value is ever null.
Windows
event_type |
payload |
|---|---|
window.opened |
{"window_id": string} |
window.closed |
{"window_id": string} |
Tabs
event_type |
payload |
|---|---|
tab.opened |
{"window_id": string, "tab_id": string} |
tab.closed |
{"window_id": string, "tab_id": string} |
tab.moved |
{"tab_id": string, "from_window_id": string, "to_window_id": string} |
A tab event names its window, because a tab_id alone does not say which
window the tab is in.
tab.moved is sent when a tab moves to another window, which dragging a tab
onto another window and move_tab_to_next_window do. It is one event and not a
tab.closed followed by a tab.opened, because the tab and its panes never
stopped. No pane.moved is sent for the panes in the tab. A pane URI contains
the id of its window, so if you keep pane URIs as keys, replace
from_window_id with to_window_id in the URI of every pane in that tab. If
the move empties the window the tab left, that window closes and
window.closed follows.
Moving a tab to another position in the same window sends no event. zterm serve has one window, so it never sends tab.moved.
Panes
event_type |
payload |
|---|---|
pane.opened |
{"uri": string} |
pane.closed |
{"uri": string} |
pane.title_changed |
{"uri": string, "title": string} |
pane.process_changed |
{"uri": string, "pid": number (u32), "tty": string or null} |
pane.moved |
{"uri": string, "from_uri": string} |
pane.opened is sent when the pane gets its id, which is before its process
starts. The pid arrives later in pane.process_changed. Do not expect a pid in
pane.opened.
pane.moved is sent when a running pane moves to another tab, which join_pane
and break_pane do. swap_pane sends two, one for each pane, when the panes it
trades are in different tabs, and none when they share a tab. uri is where the pane is now and from_uri is where it
was. The pane id is the same in both. It is one event and not a pane.closed
followed by a pane.opened, because the pane and its program never stopped. If
you keep pane URIs as keys, replace from_uri with uri when you see it.
pane.process_changed is sent when zterm attaches a process to the pane.
Today that happens once for each pane, when the pane starts. It does not report
commands you run afterwards.
title is set by whatever runs in the pane. With the usual zsh and bash prompt
settings it is often the command line that is running. Treat it as untrusted
text that may be sensitive. zterm removes control characters (Unicode category
Cc) before it sends a title, so a title cannot carry an escape sequence into
your terminal. Every other printable character is passed through.
Layout
event_type |
payload |
|---|---|
layout.changed |
{"window_id": string, "tab_id": string, "panes": array} |
layout.changed is sent when a pane of the tab changes size or place. A split,
a close, a dragged seam, the resize_pane_* actions, equalize_panes, the four
tile_* actions, zoom_pane, swap_pane, a change of font size and a resized
window all send it. An action that moves no pane sends none, so tile_grid
twice in a row sends one.
panes is every pane of the tab, moved or not, in the order zterm ls lists
them. Each element has the two keys a zterm ls row has for the pane:
{
"uri": "workspace://zestful-terminal/window:01HQ.../tab:01HQ.../pane:01HQ...",
"geometry": { "cols": 80, "rows": 24, "col": 1, "row": 2 }
}
geometry has the same keys and the same meaning as geometry in zterm ls,
so one parser reads both. cols and rows are the size the pane's program was
told. col and row are where the pane's text starts in the tab. zoomed is
present, and true, only on the pane the tab is zoomed to. That pane reports
the whole pane area, and the panes hidden behind it report the places they
keep. How the panes are laid out
describes each key.
Each event carries the whole tab, so a receiver that missed one is correct again after the next.
A new tab sends its first layout.changed once its pane has a size, which is
after pane.opened. A pane that has no size yet is left out of panes, and the
tab sends again when it has one. A tab that closes sends none.
A window resize sends one event for each tab of the window, background tabs included, because every pane in them changed size. Switching tabs sends none.
join_pane and break_pane send pane.moved first and then layout.changed
for each tab whose panes moved, which is both tabs for a join. swap_pane
across tabs does the same.
zterm sends at most one layout.changed for a tab every 100 ms. The first
change is sent at once. Changes inside the next 100 ms are held, and one event
is sent at the end of it with the layout as it is then. While a seam or a window
edge is dragged you get up to ten events a second, and the last one you get
matches zterm ls.
Focus
event_type |
payload |
|---|---|
focus.gained |
{"uri": string} |
focus.lost |
{"uri": string} |
These two are not a matched pair. Read Tracking focus before you use them.
Consent
event_type |
payload |
|---|---|
consent.asked |
{"ask_id": string, "subject": string, "who": string, "pane": string, "needs": string, "stable": string, "presence": bool} |
consent.answered |
{"ask_id": string, "answer": string, "confirmed": bool, "os_code": integer} |
A control request that needs a permission the caller has not got raises a small
window, and the person who is at the keyboard answers it. These two events are
what that looks like from outside: consent.asked when the question goes up,
consent.answered when it is answered.
ask_id is the same value in both, so a reader pairs them. It is how long a
person took, and which question they answered.
subject says what was asked about. grant is a caller that wants to read,
write or control a pane outside its own family. outsider is a caller that is
not running in any pane of this terminal: a shell the person opened outside
zterm, a script, or a service.
who is the URI of the pane the caller is running in. A caller that occupies no
pane is reported as principal:no-pane, and an outsider as pid:<n> <name>.
pane is the URI of the pane the request would have acted on, and is null
when the question is not about one.
who names the caller for a person to read. It is not an identity to group
by. A process id is reused, so two rows carrying the same pid:<n> <name>
can be two different programs. Use ask_id to pair a question with its answer,
and read stable to see what the identity behind a decision was worth.
needs is read, write or control, and is null when the request needs no
grant of any class. Five actions are in that case: opening a tab, either
split, opening a window, and spawning. They are asked about for a different
reason: each one can start a program.
stable says which identity the operating system gave for an outsider, and is
null for any other subject. audit_token on macOS, pid_fd or start_time
on Linux, start_time on Windows. bare means the system gave a process id and
nothing that survives its reuse; an approval for such a caller covers the one
request it was given for, and the caller is asked again next time.
presence says whether an approval had to be confirmed by the operating
system's own check. It is true for an outsider and false for everything
else.
answer is approve or deny. Every way out of the window that is not
Approve is a deny, including the close box, and so is a pane or a window
closing under the question.
confirmed says whether an operating-system check confirmed an approval. It is
true when a person answered Approve and Touch ID or Windows Hello then
confirmed they were there. It is false for every deny, and false for an
Approve on a machine that has no such check, where the window's answer stands
on its own.
os_code is the platform's own code from that check, and is null when no
check ran. Read confirmed to find out whether a person was confirmed, not
os_code. Zero is not a reserved value: it is what a confirmation carries,
and it is also what a refusal carries when the platform gave no code of its
own. The numbers come from whichever system answered, so they are worth
recording and not worth comparing across machines.
Nothing is written to a file. These events and the terminal's own permissions list are the whole record.
Lost events: seq and dropped
dropped tells you how many events were lost. Gaps in seq tell you where.
You need both, and neither is exact.
dropped is an upper limit. It counts events the queue discarded. A send
that fails and succeeds on a retry is not counted. When a failed batch goes
back into the queue and no longer fits beside newer events, zterm trims the
batch from its oldest end and counts what it trimmed.
The count can be too high. A POST can fail on zterm's side after your receiver
has already accepted it. zterm queues that event again, and if the trim then
discards it, dropped includes an event you have. The count is never lower
than what the queue discarded.
A gap in seq may close later. zterm sends a failed event again behind the
events that passed it, so events do not arrive in seq order. A retry happens
when that sender next has an event to send. There is no timer. On an idle
terminal a gap can stay open for hours, or until zterm quits. Compare gaps with
dropped. Do not declare an event lost because time has passed.
The first seq you see from a sender is your starting point, and it may not
be 1. The counter starts when the zterm process starts, not when your
receiver connects. If you expect 1, you will report a false gap. You cannot
know about anything a sender sent before your first event from it.
Keep a separate count for each sender_id. Two senders' counters have
nothing to do with each other, and more than one sender is normal.
Drop duplicates by event_id. A retry can deliver an event twice. The
second copy has the same event_id and seq and a newer dropped. Keep the
largest dropped you have seen from each sender.
Events lost as zterm quits are not counted. Nothing is sent afterwards to report them. See Close events.
Ordering. Within one sender, order events by seq. event_ts is not fine
enough: when a window closes, its pane.closed, tab.closed and
window.closed events often share a millisecond. Across senders, order by
event_ts.
Tracking focus
Moving from pane A to pane B sends one event: focus.gained for B. zterm sends
no focus.lost for A. zterm has at most one focused pane, so the gain tells
you everything.
When you receive focus.gained, mark that pane focused and mark every other
pane from the same sender unfocused. If you mark only the named pane and wait
for a focus.lost to clear the old one, the old pane stays focused forever.
focus.lost means focus left every pane. That happens when the zterm window
loses focus to another application, or when the last pane in a tab closes. It
is not the other half of a move.
Three more points:
- Brief focus changes inside one user action are merged. Focusing a pane in a background tab changes the active tab and then the active pane. zterm sends only the net result. If the changes cancel out, it sends nothing.
- Switching between two zterm windows is not merged. You receive
focus.lostand thenfocus.gained. For that instant no pane had focus, so the events are accurate. Do not act on afocus.lostalone. Wait a moment for afocus.gainedto follow. - Apply the rule to one sender at a time. A
focus.gainedfrom one zterm process must not clear panes that belong to another. Usesender_idto keep them apart.
Close events
zterm sends pane.closed, tab.closed and window.closed when something
closes during a session. It also sends them for everything still open when it
quits. A session therefore ends with its last window, tabs and panes all
reported closed.
Two limits remain:
- When zterm quits, it waits up to 250 ms to send its last events. A receiver that is not responding at that moment misses them, and zterm does not retry.
- If zterm is killed with
SIGKILL, it sends nothing.
Do not treat a pane as alive because you have seen no pane.closed for it. Ask
the terminal with zterm ls, which is cheap and always current.
pid is the shell
pane.process_changed carries the pid of the process zterm started in the
pane, which is the shell. It is not the foreground program, and it does not
change when you run vim or a build in that shell.
This stream never contains the pid of a program you started from the shell. To
find the pane for such a program, use zterm locate <pid>, which searches up
the process tree. See Integrating.
zterm has no event for the foreground program. Windows has no way to ask which program is in the foreground of a pane, so any feature built on that question can only work on macOS and Linux.
One event carries a tty
pane.process_changed carries the pane's tty path in tty, for example
/dev/ttys003. No other event carries it. The same path is available from
zterm ls and zterm locate.
On Windows tty is always null, because a Windows pane has no tty path. That
is the correct answer, so do not retry. Anything that needs a tty path works on
macOS and Linux only.
On macOS and Linux tty is null only if the system would not give zterm the
path when the pane started. pid is never null.
zterm serve sends no focus events
Two programs send this stream: the windowed terminal and zterm serve, which
hosts panes on a machine with no display. Nothing in an event says which one
sent it. sender_id tells two senders apart. It does not say what kind each
one is.
zterm serve sends every window, tab and pane event. It never sends
layout.changed, because its panes have no places: zterm ls reports no
geometry for them either. It never sends focus.gained or focus.lost. zterm takes focus from the window that has the
operating system's focus, and zterm serve has no window.
For a receiver this means:
- A pane can open, run and close without ever gaining focus. Do not decide a pane is closed or unreachable because you never saw it gain focus.
- Keep focus state separately for each
sender_id. Asender_idnames one run of one process. It does not name a machine. A machine that restarts zterm produces a second sender, and the first sender's panes are gone, not unfocused. - A
zterm servehost still has an active pane.zterm focusworks against it. The stream does not report it.
context holds only app and version. The sender's identity is sender_id
in the envelope, and context has no host or process field.
The Zestful daemon currently ignores sender_id when it stores events. You get
the field if you read zterm's POSTs directly. You do not get it from the
daemon's store.
In the source
For readers with the zterm repository:
| what | where |
|---|---|
| Event names and payload keys | map_event in crates/zterm/src/emitter.rs |
| The merging of focus events | coalesce_focus in crates/zterm/src/events_bridge.rs |
Envelope fields, and the reserved seq: 0 and empty sender_id |
crates/zterm-emitter/src/envelope.rs |
Delivery, the queue and its drop count, sender_id, seq, and the loopback rule |
crates/zterm-emitter/src/sender.rs |
| Parsing and normalising pane URIs | crates/zterm-identity/src/uri.rs |
| Where the windowed terminal produces events | apply_registry_events in crates/zterm/src/app.rs |
When layout.changed is sent, and the 100 ms interval |
crates/zterm/src/layout_news.rs |
Where zterm serve produces events |
flush_events in crates/zterm/src/serve.rs |
| The control socket | crates/zterm-control/src/protocol.rs |
Two tests in crates/zterm/src/emitter.rs hold this page and the emitter
together. the_events_page_lists_every_event_and_payload_key_the_emitter_sends
reads the tables under Event types and fails if an event name or
a payload key is on this page and not on the wire, or on the wire and not on
this page. every_daemon_registry_event_has_a_wire_name_and_payload pins each
payload's values. Neither reads the prose, or the types in a payload cell.