Architecture: state, packaging and hosts¶
Persistence, scrollback, resume, terminal reads, packaging, and how the docs are tested; other machines are in Hosts. Part of the architecture.
Decisions¶
- Persistence. The server saves its sessions to
$LEON_STATE_DIR/sessions.json(default~/.local/state/leon) on every exit through the socket loop (server stop, SIGTERM, SIGINT, SIGHUP, idle exit), and restores them after binding (--restore, default true;server stop --discardskips the save and deletes the file). It also saves while it runs: the loop's 200 ms tick saves at most a second after the first unsaved change to the model (a session's revision, or the set of sessions) and checks the screens' text once a minute, writing only when the sessions differ from the last write (the snapshot is taken under the state lock, serialized and written outside it). So a crash (SIGKILL, a panic, power loss) loses a second of layout and up to a minute of text, and never brings back the save from the stop before it. The tick is part of the loop, so the final save, or--discard's delete, is always the last write. Saved: sessions, windows, layout trees (split IDs and ratios), labels, focus, zoom, the active tab, revisions, and per block its label, keep-open, cwd, command, scrollback and resume command. IDs come back unchanged. Programs don't survive: a restored block runs a fresh login shell in its cwd (the shell process's cwd, else the last OSC 7 path, else where it started); the saved command is never re-run, since repeatingmake deployon a restart would be a surprise and would make the file a place that runs code. The dir must be private (0700, ours) and not flocked by another server, or nothing is saved (server statusshowsstate_file: null); the file is 0600, written via a temp file and a rename. A file that is corrupt, from a newer version, or inconsistent is moved tosessions.json.bad-<secs>-<n>with a warning in the log, never replaced. So the model never makes a state it can't load: at most 1024 blocks (leon_core::MAX_BLOCKS) and 32 nested splits in a window (layout::MAX_DEPTH), refused with 400 when a block or move would pass them. Past about 60 splits the file nests deeper than the 128 levels serde_json reads, and so wouldsession.viewreplies. The app takes no part: the server it starts restores at start, before it accepts the app's connection, so the app's "open a session when there is none" sees the restored ones. Tests and benches useembedded::start_at, an in-process server that keeps nothing. - Scrollback on restore. Each block's scrollback and screen (
capture's text; for an alt-screen app, its screen) is saved as text, capped at 2 MiB per block and 8 MiB over all blocks (each getsmin(2 MiB, 8 MiB / blocks)), newest whole lines kept. Every control character but\n(C0, DEL, C1) is removed when it is saved and again when it is restored, since the file is outside the server's hands in between; so a snapshot can't send an escape, a query or a carriage return. The new terminal's emulator is fed the text, then a dim— restored from <time> —line, before the shell's first output: the text is shown, never sent to the shell. Whatever a terminal showed (a printed token too) is on disk in the private 0600 file until the next save replaces it;server stop --discardforgets it. - Resume commands. A program registers the argv that resumes it with
dev.leon.terminal.set_resume {command}(1 to 64 args, at most 8 KiB of printable ASCII, no backslash; null clears it). Under "ask" the user agrees to what they see typed, so it must be what runs: no control key, no bidi or invisible character, no byte readline could read as Meta, no backslash (fish reads\'inside single quotes). The skill hook recordsclaude --resume <session_id>orcodex resume <session_id>from the agent's SessionStart JSON (an id of[A-Za-z0-9._-], no leading-), and only when the hook, its parent or its grandparent is its terminal's foreground process group: the interactive agent, not a nestedclaude -pthat an agent's tool started without a terminal; and not when one of those three has-p,--printor--print=…in its argv before any--(Claude Code's print mode: argv, not the program's name, since an npm install runs asnode …/claude). Restored blocks follow the config'sleon.set("resume", …), read once the start-up config has loaded and before restoring (the restore waits at most 1 s for it, since nothing is served until the restore is done; past that nothing is offered, as with"never"):"ask"(default) types Ctrl-U and the shell-quoted command at the new prompt and shows a dim hint, so Enter runs it;"always"adds the Enter;"never"types nothing. Typing waits (up to 60 s, then gives up) until the shell is the PTY's foreground process group with canonical mode off, so neither an rc file's picker nor typeahead gets it; zsh and fish must also have turned bracketed paste on, which their line editors do and a key an rc file reads (oh-my-zsh's update question, compinit'sread -q) doesn't. bash and sh are taken at canonical mode alone. dash (Debian'ssh) has no line editor and never turns canonical mode off, so a dash login shell is offered nothing: the wait gives up and types nothing. A command is offered once: when it has been typed (under"ask"or"always"), the restored block still shows it (block inspect) but it isn't saved again, so the next restart doesn't type the same stale command; a program that resumes registers again (the skill hook does on every agent start), and that registration is offered next time. A generation count, not the command's text, says which registration was typed, so an agent re-registering the same command isn't lost. Under"never", or when the wait gives up, nothing is typed and the command is kept. A restart before the user presses Enter loses the offer, though its text stays in the restored scrollback. With"always"the private state file is a place that runs the commands programs registered, which is why"ask"is the default. - Terminal reads. A thread per terminal only reads the PTY into a bounded queue (64 reads of
up to 16 KiB); the parser thread feeds what has queued, up to 64 KiB, to the OSC 7501 tracker and
the emulator in one go. The macOS tty buffers ~1 KiB, so reading and parsing on one thread
stalled the program while it parsed (docs/performance.md). Leon's own scans of the output (OSC
7501, the X10 mouse mode, history clears) jump from ESC to ESC with
memchrand step over the escapes they don't follow, so most bytes are looked at only by alacritty. - One server per socket. A server holds
server.lockbeside its socket (an flock) for its whole life, and a starting one takes it withLOCK_NB. Finding it held, it waits up to 2 s for the lock or for the socket to answer, then exits; it never unlinks a socket while the lock is held, since a busy server's full backlog refuses connects on macOS just as a stale socket does. A held lock with no socket at all (the path was removed) lets it bind anyway, unlocked. Each server raises its soft open-file limit to the hard one, at most 10240: launchd starts the app, and so the server it spawns, with 256, and each terminal holds three descriptors. - Version.
crates/leon/build.rssetsLEON_VERSION: the workspace version and the commit,0.1.0 (1a2b3c4d5e6f)(git rev-parse --short=12, orLEON_GIT_HASHwhen the build has no.git, as in Docker from a worktree).leon --versionprints it;server.versionkeeps the bare version. - Packaging.
scripts/bundle.shwraps the oneleonbinary inLeon.app(bundle iddev.leon.app, the bare version fromleon --version, the brand iconbrand/icon/Leon.icns,LICENSEandTHIRD-PARTY-NOTICES.txtinContents/Resources, ad-hoc signed); opened from Finder it runs with no arguments, which opens the app.scripts/install.shcopies the app to~/Applicationsand links~/.local/bin/leonto the binary inside it. Both replace only what they made: an app with our bundle id, or a link whose text is our binary's path. - Updates.
leon self-updateis theleon-updatecrate:Config::load(the one place the environment is read for what leon trusts and where it looks:LEON_RELEASE_URL, and in debug builds only the tests'LEON_TEST_UPDATE;HOMEBREW_PREFIXonly helps find a cask install),check(manifest, signature, rules) andinstall(download, sha256 and signature, unpack, rename). It shells out tocurlbehindfetch.rs, pins the keys incrates/leon/release-keys.pubwithinclude_str!, stages in.leon-updatebeside the install under aflock, and never replaces a Homebrew or dpkg install. The rules and their reasons are in docs/updates.md and docs/releases.md. Leon.app's side isleon-app/src/updates.rs, with no GPUI: a daily look on aleon-updatethread onceserver.settingssayscheck_for_updates, the time in$LEON_STATE_DIR/updates.jsonbefore the look starts, and part A'sinstallonly on the user's Install;view_update.rsdraws its notice, and the redraw signature hashes its generation, so a notice at rest draws nothing. A debug build looks on its own only withLEON_RELEASE_URLorLEON_TEST_UPDATEset, so no test reaches the real server. - Linux packages.
scripts/package-linux.sh [--arch arm64|amd64]builds a release binary (symbols stripped) in the same pinned image and writes, totarget/linux/<arch>/(emptied first), the bareleon(whichm16a_docker_sshdruns behind a real sshd),leon-<version>-<uname -m>-linux.tar.gz(leon,README.md,LICENSE,THIRD-PARTY-NOTICES.txt,docs/) andleon_<version>_<arch>.deb(hand-built withdpkg-deb:/usr/bin/leon, docs and the notices in/usr/share/doc/leon,Depends: libc6 (>= …)from the newest glibc symbol the binary uses, a fixed placeholder maintainer). It then installs the.debin a cleandebian:bookworm-slimand unpacks the tarball inubuntu:24.04(both pinned by digest), and fails unless eachleon --versionprints the version. Rust links glibc dynamically: the binary needs glibc 2.34 or newer (measured on arm64 and amd64; no musl build yet). - The login shell's PATH. Leon.app opened from Finder starts the server with launchd's bare
PATH, and
leon server proxyover ssh with sshd's, so programs weren't found (commands should run with the login shell's env).leon server runfirst runs$SHELL -i -l -c /usr/bin/env(else the passwd entry's shell) and takes its lastPATH=line: interactive, so a PATH set in~/.zshrccounts as in the user's tab; from the system PATH; withLEON_AUTOSTART=0, so an rc line that runs leon can't start a server whose probe runs it again; stdin /dev/null; in a session of its own, sozsh -iisn't stopped for taking a terminal, and killed with its group after 2 s (about 20 ms normally; an rc thatexecs tmux gives nothing and the PATH stays). If every dir of the server's PATH is in the login one, the login PATH is used in its order; otherwise the server came from a terminal with dirs of its own, which keep their order, and the login PATH's other dirs follow. Only absolute dirs are kept (a relative one would be looked up in each program's cwd), and only PATH is taken. - Notices and releases.
scripts/notices.shlists every crate in the binary (cargo tree, normal and build dependencies, since lua-src compiles Lua into it) with its license, then each distinct license file from the cargo registry once; it is generated per package, never committed.scripts/release.shclones one commit into a temp dir, buildsLeon-<version>-macos-<arch>.zipand both Linux arches' packages from it, and writesSHA256SUMS; it never tags, pushes or uploads. A pushed tag runs the same scripts on GitHub, signs every file with minisign against the keys pinned incrates/leon/release-keys.pub, and publishes the release with its manifest (docs/releases.md). LEON_BIN_DIR. Every terminal gets the server binary's directory asLEON_BIN_DIRand at the front ofPATH(empty entries dropped; no PATH at all falls back to the system dirs), soleonresolves inside the app. A login zsh'spath_helpermoves system dirs ahead of it, so an olderleonin/usr/local/bincan win there; the dir is still on PATH.- API contract.
help.allandserver.versionreportapi_version(semver,1.6.0), apart from the build's version. Each method doc carriesstability(stable,experimental,internal), and optionallydeprecated(since,replacement,removal) andrequest_example/response_example; all of it comes from one table,leon_server::contract, which must name every method and block method. Stable: help, server, sessions, windows, targets, blocks, layout, events and the terminal'swrite,format,size,resize,pwd,process,program_status,set_resume. Experimental: the screen transport (block.watch,screen,lines,find,selection,link_at),client.action,client.key; internal:client.keymap.docs/api.mdis rendered fromhelp.allanddocs/api-snapshot.jsonholds the stable schemas (prose dropped, refs inlined); tests fail when either drifts, and the snapshot can only be re-blessed (LEON_BLESS=1) with a newAPI_VERSION. The policy is in docs/integrators.md. - Themes and settings (M22a). The theme model is
leon_core::theme(the app re-exports it):DARKis the app's colours,LIGHTIron on Parchment, andtheme::importreads Ghostty theme files (also the format leon stores themes in, with its own chrome keys) and iTerm2 XML plists. An import keeps only the terminal palette and mixes the chrome from its background and foreground. Settings live only in init.lua:leon_lua::Settingsis typed with defaults and records each key'sfile:line,leon_lua::rewriteedits one literal in aleon.setcall and proves the edit by loading it before renaming it over the file, and the server'sserver.settingsandsettings_changedcarry names, not colours; M22b resolves the names. - Themes and settings in the app (M22b). The app asks
server.settingswhenever the keymap generation moves (every reload moves it, andsettings_changedonly comes with one), drops a reply asked for under an older generation, and resolves the theme names itself from thethemes/beside the config the server loaded; a name that no longer loads falls back to the built-in theme for that appearance. Pane colours are worked out on the server, so each connection sends its palette (paletteonblock.watchandterminal.lines: fg, bg, cursor, ANSI 0-15) and its screens come resolved in it, remote hosts' too; a server older than M22b refuses the field and its panes keep leon-dark's colours. The Settings window (⌘,) reads init.lua itself and writes throughleon_lua::rewrite::write_setting, then reloads the server, so init.lua stays the one place settings live. - Docs are tested.
docs/cli.mdis checked against every--help; theshblocks in the docs run against the real binary (a bad command line fails);docs/lua/'s examples go throughleon config checkandleon do -e. -
Naming. Method, action, event and flag names stay as
docs/api.mdlists them, so scripts and agents keep working (docs/integrators.md#compatibility). Env vars areLEON_*. -
Saved overlays (M20a). State file version 3 adds per-window
floatinganddockedarrays and permits a null tiled layout. Versions 1 and 2 still load with empty overlays. Save/restore preserves bounds, order, side, size and hidden flags. Validation checks finite contained bounds, dock sizes/sides, unique placement and visible focus before starting shells. Restart uses the existing fresh-shell/resume policy for hidden and visible blocks alike; hiding during a live session leaves programs running.scripts/test-peer-uid.shuses the Linux test image to verify the peer uid check with socket permissions opened deliberately, then disables the check in a disposable copy and requires the rejection test to fail. The real source and server policy stay intact. - Saved titles. State file version 4 adds each block's
title(the last one its program set). A restored terminal starts with it, so a tab keeps its name after a restart until its program sets a new one; the title is cleaned again on load, since it goes on screen. Versions 1 to 3 still load without titles; an older leon refuses version 4 as "saved by a newer leon" and moves the file aside. - Agent links. Version 5 adds each worker block's link to its orchestrator (
worker) and each orchestrator's unread reports (inbox), at most 1024 reports in all, so the file stays far below its size limit; reports with control characters are dropped on load.