Skip to content

explicit zmx lead for remote-attached sessions, with a cover on the pane that follows - #635

Merged
umputun merged 9 commits into
masterfrom
remote-attach-lead
Sep 21, 2026
Merged

umputun merged 9 commits into
masterfrom
remote-attach-lead

Conversation

@umputun

@umputun umputun commented Sep 21, 2026

Copy link
Copy Markdown
Owner

a session attached from another Mac has one zmx daemon per pane and two clients on it, and only one of them, the leader, decides the pty size. Stock zmx moves the lead to whichever client last sent bytes it classifies as user input. A freshly attached pane therefore stayed at the origin's size until a key was pressed in it. The lead also kept moving back with nobody touching the origin: its zmx log showed 10 leadership changes in 50 minutes on one pane, each leaving the other side drawn for the wrong grid. From the source, automation typing into the origin pane (session.type, used by the chat skills) retakes the lead, and the classifier also accepts Kitty keyboard-status replies. The individual changes in that log were not traced to a cause.

this makes the lead explicit.

zmx patch. scripts/zmx-patches/0001-explicit-leadership.patch is applied to the pinned zmx by scripts/setup.sh, and the patch digest is part of the build stamp and the CI cache key. A client started with ZMX_MANAGED=<nonce> is managed. Managed input never takes the lead, and a follower's input is dropped. A managed attach claims explicitly or fills a vacant slot. Unmanaged clients keep stock takeover behavior. The daemon tells a client when its role changes, and the client relays it to agterm as a reserved OSC 2 title. It normally waits for a boundary in the output stream, and after 250 ms cancels an unfinished sequence in the follower, whose screen is thrown away anyway. Two commands are added: zmx screen reads the daemon's own grid, and zmx type types into it as key events encoded for the terminal's current keyboard mode.

agterm. A remote attach claims the lead in every pane at attach time, so all panes take this Mac's size right away. Local panes attach without the claim, so an origin relaunched while another Mac leads comes back covered. A pane that does not lead is covered with a panel saying where it is in use. A keypress without Command on it, or the new session lead command, takes the lead through a fresh attach into a new surface. When the leader goes away the pane re-attaches by itself. surfaces[].lead in tree is the read-back.

automation on a covered pane. On the Mac the pane runs on, session type, session text --lines/--all and surface cursor are served by the daemon and keep working under the cover, which is what the chat skills need. After the pane's first role report, local session type uses the daemon's acknowledged path regardless of its current role. On an attached Mac whose pane is covered, type, text and cursor requests are refused because its daemon is remote. Commands that can only act on the local surface (paste, selectall, copy, opening or navigating a search) refuse on a covered pane and name session lead; search --to close still works.

a static title in the user's ghostty config made libghostty drop every OSC title, including the role relay. The key is now cleared from the built config and agterm applies the configured title itself.

the zmx patch is kept upstreamable and has its own tests (107 pass with the patch) plus scripts/zmx-patches/check.py, an end-to-end check with two managed clients on ptys.

two known limits. Before a local pane's first role report session type keeps the legacy surface path, which can report success for input that is dropped if another client already leads. And zmx screen and zmx type run synchronously on the main actor, like the existing ZmxClient calls.

setup.sh applies scripts/zmx-patches/*.patch over ZMX_REV and folds their digest into the build stamp and the CI cache key, so editing a patch rebuilds zmx like a pin change does.

the patch adds an opt-in for a terminal that manages leadership itself. a client attached with ZMX_MANAGED leads only by claiming at attach, never by typing or resizing, and is told its role through a reserved OSC 777 notification written at a safe point of the output. zmx screen reads the daemon's own terminal and zmx type queues input with an acknowledgement and without taking the lead. clients that do not opt in behave as upstream.
…nother Mac

every zmx attach agterm starts is now a managed client. attaching a session from another Mac claims the lead in every pane at once, so its grid applies without a key press, and typed automation on either side no longer moves the lead back.

a pane whose client does not lead is covered, since what it drew is laid out for the other grid. its first key press takes the lead back through a fresh attach into a new surface, and the same happens by itself when the leader goes away. while a pane is covered session.text and surface.cursor are answered by the daemon's own terminal and session.type goes through zmx type, so pane-to-pane automation keeps working on the Mac a session runs on. a covered pane attached from another Mac refuses those three instead.

the role travels as a title under a reserved prefix. libghostty drops a desktop notification that follows another within a second, app-wide, which lost the report after every re-attach. a static title in the user's ghostty config would drop the reports too, so it is cleared in every config build and its effect, ignoring program titles, is kept by the title callback.

tree reports each pane's lead on its surface node.
session.lead takes the lead of a pane for this Mac, the control twin of a key press on the pane's cover. a pane that already leads answers ok, one whose zmx reports no role says so, and surfaces[].lead is the read-back.

a closed pane drops its lead state. hosted tests cover the covered-pane read and type paths, the takeover key and the zmx client's screen and type calls.

the control-api rule gains a pane lead section replacing the accepted v1 limitations it removes, and the backlog item it resolves is deleted. docs.html, commands.html and the bundled skill describe the new behaviour.
session.type and the layout reads of a local managed pane always go through the daemon. the role the app holds trails the daemon's by a main-queue hop and up to 250 ms, so keying delivery on it answered ok for input the daemon had already started dropping; the realize poll repeats the check too. only the default viewport read stays on a leading pane's own surface.

an attach that ends on its exit prompt drops the pane's lead state, so a failed take-over no longer sits under a cover that swallows its keys. the cover moves below a pane's own overlay and is mounted by terminal zoom and the dashboard as well. a re-attach clears a search the old surface owned synchronously and carries a dashboard cell's transient font as the override instead of seeding the new surface from it.

the configured static title is kept and applied by agterm rather than discarded. the zmx daemon ignores a session switch while its leader is managed, whose nonce, generation and daemon reads are bound to that session.
routing every managed local pane's session.type through zmx type sent a fixed carriage return for Return, which is wrong for a program that asked for the kitty keyboard protocol, and skipped the pending IME composition the surface path commits first. both reached ordinary live panes, not only shared ones.

zmx type now carries typed text: the daemon encodes each CR as a Return press and release and each run as typed text with ghostty's key encoder against its terminal's live keyboard mode, which is what the surface's own key path does. a pending composition is sent ahead of the scripted text on the same acknowledged call and dropped locally only once the daemon took it.
paste, selectall, copy and an opening or navigating search act on the local surface, which on a covered pane holds output laid out for another grid. they now refuse with a message naming session lead; search --to close stays available so a search opened before the demotion can still be dismissed, and the refusal checks the pane the search is pinned to.

a takeover key whose release was never seen no longer swallows its next press: a fresh non-repeat press of the same key clears the latch, other keys leave it alone. lead state of a closed primary is forgotten before the survivor's identity replaces it. check.py predicates are booleans and the docs name the no-lead case accurately.
the refusal ran once, before the realization poll, the needle-change pause and the settle wait. A pane demoted during any of them was still searched and answered ok. The owner is now re-checked after realization, after the needle-change pause before anything is mutated, and before the result is returned. close stays exempt.
…ocused one

with a search open on a leading pane and split focus on a covered one, the re-check after realization looked at the focused pane and refused a command that drives the other pane.
Copilot AI lite review requested due to automatic review settings September 21, 2026 01:13

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@umputun
umputun merged commit cf66285 into master Sep 21, 2026
8 checks passed
@umputun
umputun deleted the remote-attach-lead branch September 21, 2026 01:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants