Paneflow emits an event whenever something changes: a pane opens, a terminal
changes directory, a program exits, an agent starts waiting for you. Events can be
handled in Lua with `paneflow.on` and `paneflow.wait`, or streamed from the CLI
with `paneflow events` and `paneflow watch`. This page describes Paneflow 0.20.0.

Events come from two places:

| Source | Describes | Read with |
| --- | --- | --- |
| Worker event log | Layouts, terminals, agents, the configuration and grants, numbered in order. | `paneflow events`, `events.follow`, and `paneflow.on` and `paneflow.wait` in Lua |
| Window stream | Terminal output and agent hooks, as the open window sees them. | `paneflow watch`, `events.subscribe` |

## Events in Lua

A handler is called with `ctx` and `ev`. The fields listed for each event on this
page are found directly on `ev`:

```lua
paneflow.on("terminal.exited", function(ctx, ev)
  paneflow.log("info", ev.lifecycle .. " in surface " .. tostring(ctx.surface_id))
end)
```

Every `ev` also has these fields:

| Field | Description |
| --- | --- |
| `ev.type` | The event's type, such as `terminal.exited`. |
| `ev.seq` | The event's position in the worker's log. |
| `ev.session` | The terminal session the event is about. Absent for most events. |

The ids of the workspace, tab, pane and surface are in `ctx`, not on `ev`. For an
event that carries only a session, Lua looks the session up and fills
`ctx.surface_id`, `ctx.tab_id` and `ctx.workspace_id`; `ctx.pane_id` stays `nil`.

The name given to `paneflow.on` or `paneflow.wait` is an exact type, a family such
as `terminal.*`, or `*` for every event. Paneflow never checks a name against a
list, so a handler can be registered for an event only a newer version sends.

In `paneflow do`, a script hears the events of the workspace its pane belongs to,
plus the events that carry no ids, unless it runs with `--all`. When events were
dropped because the script fell behind, handlers receive an `events.resync` event;
`*` does not match it, `events.*` does.

## Layout events

These describe the layout the worker holds. Each carries the ids listed, and none
has fields of its own except `layout.changed`.

| Event | When | Ids |
| --- | --- | --- |
| `workspace.created` | A workspace is created. | `workspace_id` |
| `workspace.closed` | A workspace is closed. | `workspace_id` |
| `workspace.renamed` | A workspace's title changes. | `workspace_id` |
| `workspace.selected` | A different workspace is shown, including a new one. | `workspace_id` |
| `tab.created` | A tab is created. | `workspace_id`, `tab_id` |
| `tab.closed` | A tab is closed, including each tab of a closed workspace. | `workspace_id`, `tab_id` |
| `tab.renamed` | A tab's title changes. | `workspace_id`, `tab_id` |
| `tab.selected` | A workspace's active tab changes. Not sent for a new workspace. | `workspace_id`, `tab_id` |
| `pane.created` | A pane is created. | `workspace_id`, `tab_id`, `pane_id` |
| `pane.closed` | A pane is closed. | `workspace_id`, `tab_id`, `pane_id` |
| `pane.focused` | A tab's focused pane changes. Not sent for a new tab. | `workspace_id`, `tab_id`, `pane_id` |
| `surface.created` | A surface is created. | `workspace_id`, `tab_id`, `pane_id`, `surface_id`, and `session` when it shows one |
| `surface.closed` | A surface is closed. | the same |
| `surface.renamed` | A surface's display name changes. | the same |
| `layout.changed` | A tab's split tree or zoomed pane changes. Fields: `revision`, the layout's revision after the change. | `workspace_id`, `tab_id` |

`layout.changed` follows any change to splits, ratios, the panes in a tab or the
order of their surfaces. A title or focus change alone does not send it.

## Terminal events

These come from the terminal sessions the host runs. They carry only `session`.

| Event | When | Fields |
| --- | --- | --- |
| `terminal.cwd_changed` | The terminal reports a new working directory. | `cwd` |
| `terminal.title_changed` | The program sets a new title. | `title` |
| `terminal.bell` | The terminal receives a bell character. | `bell_at_ms` |
| `terminal.exited` | The terminal's program stops running. | `lifecycle`, and `exit_code` and `signal` when it exited |

A few notes on these:

- No terminal event is sent the first time the worker sees a session, and changes
  between two observations arrive as one event with the latest value.
- `cwd` is an absolute path. `title` can be an empty string after a terminal reset.
- `bell_at_ms` is the time of the bell in milliseconds since the Unix epoch. A bell
  within 2 s of the previous one is ignored.
- `lifecycle` is `exited`, `failed`, `lost` or `unverified`. `exit_code` is set
  only when it is `exited`, and `signal` only when a signal ended the program.
  `terminal.exited` is sent once, and not for a session that disappears or was
  first seen already ended.

## Agent events

| Event | When | Fields |
| --- | --- | --- |
| `agent.state_changed` | The status the worker derives for a session's agent changes. | `tool`, `previous`, `state` |

`previous` and `state` are `busy`, `idle`, `attention` or `errored`. `tool` is the
agent's hook name, such as `claude`, else the id of the runtime Paneflow observed,
such as `com.openai.codex`, else `nil`. The event carries `session`.

## Configuration and grant events

These carry no ids.

| Event | When | Fields |
| --- | --- | --- |
| `config.reloaded` | `init.lua` is loaded or reloaded, whether the load succeeds or fails. | `ok`, `found`, `path`, `actions`, `bindings`, `errors`, `warnings`, and `problem` when there is one |
| `grant.changed` | A pane grant is issued or revoked. | `revision`, `revoked`, `grants` |

`problem` is the first problem found, a table with `severity` (`error` or
`warning`), `message`, and `file` and `line` when known. `revoked` lists the ids of
the grants this change removed, and `grants` is the number held after it.

## Events on the CLI

`paneflow events` follows the worker's log and prints one line per event, with its
seq, type and ids:

```text
42 pane.created workspace_id=1 tab_id=2 pane_id=5
43 terminal.bell session=s-1
```

With `--json` it prints each event as the worker sends it: `seq`, `ts` (milliseconds
since the Unix epoch), `type`, the ids present, and `data`, which holds the fields
listed on this page:

```json
{"seq":42,"ts":1764158400123,"type":"pane.created","workspace_id":1,"tab_id":2,"pane_id":5,"data":{}}
```

| Option | Effect |
| --- | --- |
| `--types` | Only these types, comma-separated: exact names or a family such as `terminal.*`. A bare `*` is refused. |
| `--since` | Replay the events after this seq first. |
| `--workspace-id` | Only events whose `workspace_id` is this id. Terminal, agent, configuration and grant events have none, so they are left out. |

The worker keeps the last 1024 events in memory, numbered from 1 each time it
starts. `paneflow events` reconnects on its own. When the events it missed are no
longer kept, it prints a line starting with `-- the worker restarted` (with
`--json`, a `restarted` object) and continues from the current seq. The
`events.follow` API method refuses such a request with error `-32040`. A reader
that falls more than 8 MiB behind is disconnected. Filtered output skips seq
values.

## Window stream

The open window streams a second set of events to `paneflow watch` and the
`events.subscribe` API method. Each line is one JSON object whose `type` names
the event, with its fields at the top level and every field present, `null` when
empty.

| Event | When | Fields |
| --- | --- | --- |
| `surface_changed` | A terminal produced output. The window checks every 50 ms and sends one event per terminal whose output counter moved. | `surface_id`, `output_generation`, `ts` |
| `ai.session_start` | An agent's session starts. | the agent fields |
| `ai.prompt_submit` | A prompt is submitted to the agent. | the agent fields |
| `ai.tool_use` | The agent starts a tool. | the agent fields |
| `ai.notification` | The agent raises a notification, usually to ask for input or permission. | the agent fields |
| `ai.stop` | The agent finishes a turn. | the agent fields |
| `ai.exit` | The agent's process exits. | the agent fields |
| `ai.session_end` | The agent's session ends. | the agent fields |

The agent fields are `workspace_id`, `surface_id`, `pid`, `tool` (the agent's
command, such as `claude` or `codex`), `state` (`thinking`, `waiting_for_input`,
`finished` or `errored`), `message` (the last notification's text),
`active_tool_name` (the tool running now) and `ts`. The `ai.*` events reach the
stream only while the worker runs and the agent's terminal is open in the window.

`paneflow watch --surface` keeps one surface and `--type`, repeatable, keeps exact
types; families are not accepted here. The stream also sends these frames, which
`--events-only` hides:

| Frame | When |
| --- | --- |
| `{"type":"subscribed","id":1}` | Once, when the subscription starts. `id` numbers the subscription. |
| `{"type":"heartbeat"}` | After 30 s without an event. |
| `{"type":"dropped","count":5}` | Before the next frame, when that many events did not fit the subscriber's queue of 1024. |

The window accepts at most 16 subscriptions at once.

## Unknown events

New versions of Paneflow add events and fields. Handle the events you know and
ignore fields you do not recognize rather than failing on them.