# Running pardes: an attach is always a client (ruled; built 2026-11-20, all but `--detach` forking) > the way it's distributed with client/server and detached and stuff, it's > confusing to me now and I think it must be confusing in the code too, > the way Rex does things is a good parameter to compare Ruled by the user on 2026-11-17, answering the reorganisation plan's questions: the words of distribution (*session*, *client*, *far*, *frontend*, *detached*, *attach*); "attach is always a client"; the daemon stays as built; the vocabulary and the manual's page first, the code after. Built on 2026-11-20 (agent pardes-attach, report `.scratch/sketch/reports/pardes-attach.md`): an attach is always a client, the in-place attach deleted, and a session registering again with a restarted daemon. Not built: `--detach` forking (below). What runs, with a figure for each way of starting pardes, is the manual's "How pardes runs" (`docs/typ/running.typ`); the daemon's history is `docs/next-daemon.md`. ## The ruling A session's own frontend is only the one it was born with. Once it has let that go (a Detach, its terminal or window closing), it stays detached, and every attach after that is a client: a process of its own with a replica core, as the second attach to a session already was. Two more things go with it (the coordinator's, put to the user with the question): - a session registers again with a daemon that was restarted, so a daemon's death no longer leaves its sessions unlisted for good (built); - `--detach` forks as a terminal's session does, so the process you typed returns at once and `ps` always shows a session the same way (not built). ## Before, and now ![What `pardes attach` did before, and does now](next-run.svg) The figure is `docs/next-run.typ`, drawn by `docs/typ/figures.typ` (`typst compile --root . --ignore-system-fonts -f svg docs/next-run.typ docs/next-run.svg`). Before, `pardes attach` was four mechanisms, picked by the daemon's `find` and by `main.zig`'s `attachTo`: a terminal handed to a detached terminal's session, which drew on it in its own process while the attach process waited as its stub; a window's detached session asked to open its own window again, while the attach process exited; a client here, when the session had a frontend or was of the other kind; a far client through `ssh -L`. Now it is the last two only: a client, here or far. `runtime/main.zig`'s `attachTo` goes to this machine's replica socket with the name (the daemon refuses one no session has); bare, `bareAttach` takes the one detached session from the daemon's `list`, else the one session, else names them and asks which. What a user who relied on the in-place attach does now: `pardes attach work` (or `pardes-gui attach work`) is a client of the detached session, its own process in this terminal or window. What differs from having the session's own frontend back: the client is a second process (a terminal's 10-16 MB, a window's about 110 MB); an addon that is not plain runs in the session alone (its panes show a line saying so); its Detach ends the client with "detached from 'work', which is still running" and exit 0, as the stub's did: the session's last word on a link says which of two things happened, `left` (a Detach in that client) or `ended` with the session's exit code, which the client exits with. ## What it removed Counted on the change itself (deleted lines, comments with them; a function's added replacement in brackets): | What went | Where | Lines | |---|---|---| | The daemon choosing which session takes a frontend: `find`, its `other` answer, the `attach` verb and its test | `runtime/daemon.zig` `Daemon.find`, `Daemon.take` | 99 | | The attach request: `daemon.attach`, `Answer`, `Note.attach` and the reader's arm | `runtime/daemon.zig` | 36 | | The stub's second use: `stubWait`'s `child` and its `r` and `x` words, `tellStubExit` | `runtime/daemon.zig`, `runtime/main.zig` | 37 | | `attachTo`'s two hand-offs and `crossAttach` | `runtime/main.zig` | 41 (+16: `attachTo`; +33: `bareAttach` and its test, what `find` chose, done here) | | A terminal handed to a session: `attachTerminal`, `refuse`, the `.attach` message and its three arms | `dev/tui/tui.zig` | 52 | | A window opened again in place: `openTheWindow`, `Shell.detached`, the queue's `.window` and its arms | `dev/gui/gui.zig`, `dev/Inbox.zig`, `runtime/macos.zig` | 34 | | `setState(false)`, which nothing says now | `runtime/daemon.zig` (`setDetached`) | 2 | | **in all** | | **305 deleted, 94 added** | What did not go: the upgrade record's terminal and window fields (a session born with a terminal still takes it across an Upgrade); Detach of the session's own frontend (it is how a session becomes detached); `reopenWindow`, which a WindowOpacity under 100 makes a window again with; `daemon.Kind` in the hello and in `pardes ps` (`tty` or `gui`). ## What a client lacked, which the change made work The ruling turns these from objections into work. Each is checked here: what is true, and whether it was measured. - **What a client costs in memory.** Measured on 2026-11-17 (`.scratch/sketch/reports/pardes-measure.md` section A; PSS from `smaps_rollup` after 4 s idle, the installed 0.24 ReleaseFast): a terminal's client is 10-16 MB (20-25 after typing: tree-sitter data the session grows by as much), a window's about 110 MB (187 MB RSS), the same as any window session: three quarters of it the GPU and display libraries and the window's buffers. Its core and the rollback copy it keeps are about 1-2 MB of that. So the "+25 MB a client" measured on 2026-11-06 (`.scratch/arch/client-report.md:21-22`) holds for a terminal's client and is four or five times too small for a window's; what would shed it is a lighter way to draw, not a lighter core. A client adds about 1.3 MB to its session (a terminal's), 13 MB (a window's with addon panes). Before, only a second attach paid it; now every attach to a detached session does, which is the tmux habit (`pardes --detach=work`, then `pardes attach work`). - **An addon's code does not run in a replica.** No longer so for an addon whose state is plain (`PA_PLAIN`: the games, the picture and the PDF): a client loads its library at that kind's first pane (`Addon.plainOps`), runs it as the session does and draws its pictures itself; one it lacks shows a line saying so. Any other addon's panes show a line in a client saying it runs in the session alone (`Kind.draw`): the session sends no pane's pictures to a client (the path that painted a pane for each client and sent its frames was deleted). - **A window client scrolls whole rows and keeps its own fonts.** Not so for the wheel, measured (`.scratch/sketch/reports/pardes-measure.md` sections C and E): a notch in a client glides as it does in the session's own window (5 frames, 0 to -22 px, in both). Only whole rows go to the session (`dev/gui/gui.zig:2200-2214`; its comment still says "no glide", which `docs/next-daemon.md`'s GUI replica notes said too until 2026-11-19); the picture moves between them on the client's own clock (`glideFrame`, `dev/gui/gui.zig:3673`). A touchpad's fractions are the client window's own picture too (the test "detached trackpad scrolling keeps fractions local and queues whole rows for replication", `dev/gui/gui.zig:8190`). Fonts: a client follows the session's font request by the face's name when that face is installed on its machine (`dev/gui/gui.zig:595-623`, `syncReplicaFont`); its opacity is its own (not in the client's per-frame chores, `dev/gui/gui.zig:1296-1322`). The note said "keeps its own font"; it is corrected there. Measured: a face the client lacks leaves it at its old face and its old size, and it diverged and joined again (2 runs of 2, cause not found). - **Speed.** "The regular tty and GUI get no slower" is the daemon's first rule (`docs/next-daemon.md`, decided by the user). After the change, a reattached session is the client path, not the session's own frontend. Not measured here: a keystroke to its frame, local client against own frontend, on the soak gate. ## A session registering again (built) Before, a session said hello once, at its start, or on its link after an Upgrade; when the daemon died the session's reader saw the link end and nothing dialled again, so the session ran on unlisted: `pardes ps` and attach did not find it, Detach and Adopt refused, and its frontend closing ended it. With "an attach is always a client" that would have meant no attach to any session after a daemon's death. Now (`runtime/daemon.zig` `readNotes`, `redial`, `relinked`; `runtime/session.zig` `watch`; `dev/Ptys.zig` `tellDaemon`): the reader thread, blocked in its read, is woken by the link's end alone. If the daemon had taken the hello (an `ok` came) and the session is not ending, it dials the next daemon, starting one if none answers (`daemon.connect(true)`: the lock decides which of several sessions starts it), puts the new connection on the link's own fd (`dup3`, so the session loop's own sends never race a new fd) and says the hello with its kind, name and whether it is detached. It then posts a note that wakes the session loop once, which tells the new daemon a dup of every pty master it holds and its clients' pids. Nothing polls and nothing waits on a timer: an idle session is at 0 wakeups before and after (`test/daemon.py --relink`, 2 s each). A link the daemon never took (another build's daemon refuses the hello) is not dialled again, so two builds cannot loop. What cannot come back: the shells of sessions killed earlier, which only the dead daemon held. ## `--detach` forking Today `--detach` is the launch process itself (`runtime/main.zig:1517-1569`): it blocks the shell unless backgrounded by hand, and it stays the job of the terminal it was typed in. `pardes-gui --detach` runs the `pardes` beside it in its place (`runtime/main.zig:1518-1534`), so a window's binary is a detached session only where no `pardes` is beside it, and that one ends on its terminal's hangup (no handler of its own: `runtime/9p_io.zig:704-729`). The work: fork as `daemon.forkSession` does (`runtime/daemon.zig:663-698`), `setsid` in the child, its stdin, stdout and stderr on `/dev/null`, and the parent printing the session's name and exiting 0, with no stub (nothing waits on it). For the window's build the fork comes before SDL starts. Then `pardes --detach=work` returns at once, `&` is not needed, and every session in `ps` is either its stub's child or a process of its own. ## `PARDES_PAIR`: every frontend a client (an experiment, built) With `PARDES_PAIR=1` in its environment (`export PARDES_PAIR=1` in a shell's rc; unset, nothing below happens), `pardes FILE` in a terminal and `pardes-gui FILE` start the way the last step's design has every frontend start: the process you started forks a session with no terminal or window of its own (`setsid`, `/dev/null`; a window's child execs the `pardes` beside it) and is its first client over a socket pair, the job your shell waits on (`runtime/main.zig` `pair`). A session is `kept` or not: kept from birth when started with `new --detach[=NAME]` or `--detach`, kept once a Detach is typed in any of its clients, never unkept, and an Upgrade carries it (`runtime/daemon.zig` `State`, the upgrade record's `state`). `pardes ps` says `(tty, kept)` of a kept session and `(tty)` of one that is not; a session with a frontend of its own still says `(tty, has a frontend)` and a detached one `(tty, detached)`. Its lifetime (`core/replica.zig` `Peers.leave`, `runtime/session.zig`): | What happens | Not kept | Kept | |---|---|---| | Detach typed in a client | it becomes kept; the client says "detached from 'NAME', which is still running" and exits 0 | the same | | a window closed, a terminal closed (its hangup), a TERM to a client | the client says goodbye with a close and exits 0, its terminal put back; the session ends if that was its last client | it runs on | | a client killed outright (KILL) or crashed | the link ends with no goodbye: it runs on, listed by `pardes ps`, attachable | it runs on | | no daemon | it ends when its last client's link ends, goodbye or not | cannot be: Detach refuses | Tested by `test/daemon.py --pair` (`pair`, `pair_rows`) and `--gui` (`gui_pair`). ## Rex beside pardes From Rex's own help (captured under `.scratch/sketch/reorg/pardes-plan/rex/`), and for the client row its binary's strings (`/usr/sbin/rex` links libghostty); Rex was not run. | | Rex | pardes | |---|---|---| | Who holds the programs | one server an account holds every session and runs every program (`skill-show.txt`) | each session is its own process, with the truth; the daemon holds a dup of each pty master and a list | | What starts it | any command starts a missing server (`--autostart`) | the first session starts the daemon; what you start is the session | | A client | "one program connected to the server" (`help-client.txt`); not a thin view: it replays the server's stream into its own libghostty and draws locally (the binary links libghostty; the help text does not say so, and this row first called it a view of the server's state: the user's correction, 2026-11-17) | the same principle, the stream shipped and the state derived where it is shown, into a whole replica core with its own view; what pardes adds is a client applying its own input before the session confirms it (speculation, rolled back when the session ordered things otherwise) and a predicted echo | | Attach | no user's verb; in the API "start receiving its events and get its current view" | one: a client (four mechanisms until 2026-11-20) | | The last client leaves | "the session keeps running" (`api-session.detach.txt`) | the same for a client; today a session's own frontend closing depends on its kind and the daemon | | Another machine | the same commands with `-S HOST` | `attach HOST:NAME`; a far session's 9P is not reached that way | | The server or daemon dies | "Stopping the server ends every program" (`help-server-stop.txt`) | a killed daemon ends nothing; its sessions start the next and register with it again | What pardes bought that Rex does not have: a deterministic core replicated in each client, so a far session feels local (a key is applied at once, and rolled back when the session ordered things otherwise); a view of its own for each client; and shells that outlive the editor's own death (Adopt) and an Upgrade in place. What it paid: a session that may also be a frontend, which is where `attach`'s four meanings came from. The ruling keeps the replicas and the shells and removes the frontend a session takes back. ## Open questions - `--detach` with no daemon starts a session nothing can attach to (`runtime/main.zig:1567-1569` ignores the failed registration). Refuse, or keep it as a 9P-only session? - `pardes ps` says "has a frontend" for a session with a frontend of its own (the daemon's list, and so /sessions, still says `attached`, a wire word). After the change that frontend can only be the first one: is "has a frontend" the word, or "with its first frontend"?