Repository navigation
Set up, check and remove a Linux desktop with codevisor desktop (851-4485) - #1492
Merged
pondorasti merged 11 commits intoNov 26, 2026
Merged
Conversation
pondorasti
force-pushed
the
alexandru/851-4485-linux-desktop-step-2-codevisor-desktop
branch
2 times, most recently
from
November 26, 2026 21:46
e4b793d to
53b0123
Compare
…851-4485) codevisor desktop setup asks the running server to do the work, so the desktop, its config and its boot service belong to the account the server runs as. The server installs packages with apt and removes an older root desktop itself when its account needs no sudo password; otherwise the command runs sudo in the terminal or prints the commands for an administrator. The desktop runs as its own systemd user unit (codevisor desktop run: an X11 cookie, Xvnc on the private socket with no TCP port, an Xfce session), or as a plain process the server restarts where there is no systemd. Connected viewers are disconnected with the reason when the desktop is removed or its socket stops being private. The server publishes server.features.updated after a start or removal, and the apps read the machine's features again, so a new desktop shows up at once. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…boot a desktop this server runs (851-4485) A second codevisor desktop setup no longer restarts a running desktop, so nothing open in it closes. Removing a desktop clears the unit's failed state. At boot the server restarts only a desktop whose config says the server runs it as a process, not one something else set up. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…iew (851-4485) Code review follow-ups. The server runs the package install in the background and the command polls for its end, so a slow install no longer runs into the HTTP client's five-minute limit. Setup actions run one at a time and are refused while an install is going on. The desktop starts on machines with IPv6 turned off (no /proc/net/tcp6). Where there is no systemd, a pid only counts as the desktop when it is the runner, and a server boot restarts the desktop even when an old socket file is still there. A desktop is called ready only once its socket has stayed up for a moment. A config something else wrote does not decide the display number. A withdrawn viewer's connection to the desktop is cut at once. The unit escapes dollar signs. Co-Authored-By: Claude Fable 5.2 <[email protected]>
… cleaning up (851-4485) On SIGTERM the runner exited before it had stopped and reaped Xvnc and the session: the CLI runtime's own signal handling ended the process first. Where there is no systemd that left the X11 cookie file and the session's D-Bus helpers behind; under systemd the unit ended as failed. The runner now runs uninterrupted, stops each child's process group, waits for them, removes the socket and cookie, and then exits with its own status. A second setup on a running desktop says it is already running. Co-Authored-By: Claude Fable 5.2 <[email protected]>
The test learned a helper's pid from a file the shell created and then wrote; a watcher could see the empty file and get no second event. The shell now writes the file under another name and renames it, and each test stops its processes when it finishes. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…ile (851-4485) The helper process connects to the test, and its connection closing shows that stopping took it down. File watching was not dependable in the full test run. Co-Authored-By: Claude Fable 5.2 <[email protected]>
A viewer that is cut off also loses its control seat and its messages are ignored at once. The runner starts nothing more once it is stopping, and gives everything in a child's process group the grace period before forcing it. The TCP port check fails when it cannot read the kernel's table, and only a missing IPv6 table counts as empty. A failed install is no longer reported after the programs are there or another action ran. The restart at server boot waits its turn behind other actions and only acts on a config it can share. Starting or removing a desktop also stops one left running under the other kind of service. The command says "already running" only for a desktop the server runs, keeps waiting through a few unanswered checks during an install, and says so when an install outlasts its wait. Co-Authored-By: Claude Fable 5.2 <[email protected]>
… unit, display and port checks (851-4485) Review follow-ups. codevisor desktop setup no longer runs command lists from the server's answer: it builds the apt and old-desktop commands itself from the machine's files, and cleans and checks what it prints from the answer. The status no longer carries commands. The boot unit is rewritten when the server's program files have moved. Only Xvnc's own listening sockets count for the no-TCP check, so another program on port 5900 + display no longer stops the desktop. A recorded display is reused only while it is free or this desktop's own. apt waits for its lock. A name and size in the config are kept, and a shared desktop Codevisor didn't set up is replaced only when asked. sudo is tried only when a root step is pending. Starting a detached desktop no longer leaks a file descriptor, and a stop during the first second no longer logs that Xvnc didn't start. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…etry apt update on its lock (851-4485) Delta review follow-ups. One desktop unit exists per account. Only the server whose data folder the unit names refreshes, stops or replaces it; another server's setup is refused. apt-get update, which does not wait for its lists lock, is retried while that lock is held. A config Codevisor didn't write is only replaced when asked, whether or not that desktop is up. The no-TCP check requires the process started as Xvnc to hold the desktop's socket, and leaves a vanished process to the exit report. Display reuse compares against the socket in the config. The status says whether a start would leave the desktop running. Further lines of server text are marked when printed, and printed commands are quoted for the shell. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Final review follow-ups. Replacing the old root desktop stops every instance of it, enabled or started by hand, and reports a root X server that outlives the removal. Removing a desktop Codevisor didn't set up only unshares it, after its own question, and says it was left running; any config without the service key counts as someone else's. A server without XDG_RUNTIME_DIR reaches its account's systemd through /run/user/<uid> instead of running the desktop inside itself. A desktop recorded as a plain process is started under systemd once the server reaches it. An X server orphaned by a killed runner is stopped before a restart. "Ready" and "running" need a mark the runner writes once the session has stayed up. A timeout is no longer reported as a server that isn't running, and an answer that isn't a desktop status gives one plain line. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…485) The start-up restart could rewrite a desktop unit that names another Codevisor server's data folder, when this server's desktop was recorded as a plain process and its runner was gone. It now returns, the desktop stays down, and status says another server on the account runs a desktop. An X server left behind by a killed runner is stopped whenever neither service is set up, whatever the kind, so the user unit can take the display. Co-Authored-By: Claude Fable 5.2 <[email protected]>
pondorasti
force-pushed
the
alexandru/851-4485-linux-desktop-step-2-codevisor-desktop
branch
from
November 26, 2026 22:06
53b0123 to
986483a
Compare
pondorasti
deleted the
alexandru/851-4485-linux-desktop-step-2-codevisor-desktop
branch
November 26, 2026 22:07
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes 851-4485
Part of 851-4479 (Linux desktops: screen sharing that works after installing codevisor-server). Step 2 of the approved design.
What a user gets
codevisor desktop setupgives a Linux machine a desktop that runs as the server's own account, starts at boot, and shows up in the apps at once.codevisor desktop statussays whether it is set up and running, as which account, whether it starts at boot, and where its log is.codevisor desktop removestops it (after asking) and stops sharing it. Packages stay installed.This fixes the VPS case from the ticket: the command asks the running server to do the work, so the config and the desktop always belong to the account the server runs as, never to whoever ran a script.
How it works
GET /v1/desktop), do a root step (POST /v1/desktop/packages,POST /v1/desktop/replace-old), and start (POST /v1/desktop/start);DELETE /v1/desktopremoves. Step 4's button can drive the same calls.sudo -n; sudo asks → the command runssudoin your terminal and you type the password, and without a terminal it prints the commands; no sudo → it prints the commands for an administrator and stops (run by root, the command runs them itself).codevisor-desktop.serviceis a systemd user unit that runscodevisor desktop run: it writes the X11 cookie, starts Xvnc on the private socket with no TCP port, checks twice that the desktop's TCP port is closed, starts an Xfce session, and stops both if either exits so systemd restarts them. It is separate from the server's unit, so restarting or updating the server leaves the desktop running. Without systemd (containers) the server runs the same command as a plain process and starts it again when the server starts.--yes). It is recognised by the unit the old script wrote. Declining, or having no terminal to answer in, changes nothing.server.features.updated; the apps read the machine's features again when it arrives. This is the only Swift change (MachineController+NavigationSync.swift, plus a test).apps/server/README.mddocuments the commands, the sudo cases and the boot service.What the command trusts from the server's port
codevisor desktop setupmay be run with sudo, and it talks to127.0.0.1:<port>, which the server's unprivileged account answers, or any local account that binds the port while the server is stopped. So:installandoldDesktop.removalare gone from the API). Whether an old root desktop exists is read from/etc/systemd/systemby the command, and unit names must matchvncserver@<digits>.service.missing(install needed?),sudo(who runs the root step),stateandmanaged(start, already running, or ask before replacing). The worst a hostile answer can do is make an administrator install the fixed package list or be asked a question.status,machines,flags,auth,update,logs) print what the port answers but execute nothing from it;logsandstartrunjournalctl,tailandsystemctlwith fixed arguments. Their printed text is not cleaned of control characters; that predates this PR and I left it.Tests
They fail without the change:
desktop/desktop-manager.test.ts, against a fake machine: what is missing and how apt installs it; the sudo kinds; packages installed throughsudo -n, directly as root, refused when sudo would ask, and the package manager's last lines on failure; the old root desktop found and removed in order, and someone else's VNC unit not mistaken for it; start writes the config in the server's data folder and the boot unit and waits for the socket; the next free display; a running desktop left alone; missing programs refused; "didn't start" with the log; the no-systemd process mode; removal withdraws the desktop from viewers before stopping it; a server boot restarting a process-mode desktop.cli/desktop.test.ts: each sudo case, the unknown system, a failed install, the old desktop (asked once, declined, no terminal,--yes, sudo in the terminal), everystatuswording,removewith and without confirmation.routes/desktop.test.tsthrough the real server: each route, the manager's refusal passed on, browsers refused, a Mac answering 501, and the event published after a start or removal, a failed start included.routes/vnc-relays.test.tsthrough the real server and a real socket: a connected viewer is disconnected with the reason when the config is removed, when the socket stops being private, and when the config names another desktop; it keeps streaming otherwise.desktop/desktop-parts.test.ts: listening ports read from/proc/net/tcpandtcp6(a missing table included), the X11 cookie file's bytes, the unit's quoting.desktop/supervised-processes.test.ts, with real processes: stopping takes a child's own children with it and waits until they are gone; a child that ends by itself is reported; a missing program is reported.MachineConnectionTests: the event makes a machine's desktop appear and disappear in the app's model.Checked by hand
In a throwaway container (
node:22-bookworm, no systemd; the server run as a newcodevisoraccount with passwordless sudo):statusbefore: "No desktop on this machine."setupinstalled the packages through sudo and started the desktop. Everything in it runs ascodevisor; the config is in that account's data folder and nothing is in root's home; the only listening TCP port is the server's.available, the relay gives theRFB 003.008greeting.remove --yeswith a viewer connected, then: feature gone, "This machine has no desktop to share", no desktop process and no config left.On a throwaway VM with real systemd (Ubuntu 24.04; the server as a systemd user unit of
codevisorwith linger, as the installer sets it up):Fresh setup through passwordless sudo: the desktop is
codevisor-desktop.serviceof that account, socket/run/user/1000/codevisor-desktop.sockmode 600, no extra TCP port.Restarting the server left the desktop running (same Xvnc pid).
After a reboot, with nobody logged in, the desktop was back and viewable.
remove --yeswith a viewer connected: the viewer's connection closed with "The desktop on this machine was removed."; no failed unit left behind.The VPS case: I ran the old script's root setup (root desktop on
:1, port 5901 open, config in/root).statusnamed it.setupwith no terminal and no--yesrefused and changed nothing.setup --yesreplaced it: old unit and unit file gone, port 5901 closed, new desktop running ascodevisor.Sudo that asks for a password: with no terminal, setup printed the three commands for an administrator and changed nothing; in a terminal (a pty, the password typed at sudo's prompt) it removed the old desktop and started the new one.
No sudo rights: setup printed the apt commands for an administrator and exited 1.
A killed Xvnc was back within seconds (systemd restarted the unit) and viewable.
With the final code: the desktop came back by itself after OrbStack restarted the VM; a second
setupleft the running desktop alone (same Xvnc pid) and said so; a window opened in the desktop belongs tocodevisor; another account is refused on X11 ("Authorization required") and on the socket (permission denied); 2× and 1× through the server answerok; after removing one package,setuphad the server install it in the background and waited; stopping the unit ends withResult=success, exit 0, no process and no runtime file left.In the dev container (
bun run dev:linux; the server is root and PID 1 there, no systemd), with a subscriber on the server's event stream the way the apps subscribe:setuptook 59 s;server.features.updatedarrived at 59.3 s and/v1/infolisted the desktop from then; afterremove --yesthe event arrived again and the desktop was no longer listed.Not checked by hand:
Not in this PR
codevisor desktop remove --remove-packagesfrom the design. Recording and removing packages is per package manager, so it moves to step 3 with the other package managers.removeleaves packages installed and says so./root/.codevisor/data/screen-sharing.json) stays where it is when an old desktop is replaced. It is in root's home and nothing reads it.codevisor setupquestion and the app's button are steps 6 and 4.Code review (medium) on this PR
Ten findings. Fixed, each with a test:
/proc/net/tcp6missing). A missing table now counts as empty.statussays "the desktop's process isn't running".busyandfailure.Found by hand after that, fixed with a test: on SIGTERM the runner exited before it had stopped and reaped its children (the CLI runtime's signal handling ended the process first). It now stops each child's process group, waits, cleans up, then exits.
Second code review (medium), of those fixes. Nine findings, all fixed with tests:
managed).After these the final code was run once more on a fresh systemd VM: setup, a second setup ("already running", same Xvnc pid), stopping the unit (
Result=success, nothing left), and removal with a viewer connected (closed with "The desktop on this machine was removed.").Review of e3ee1d4, all fixed with tests that fail without the fix:
update).Small ones: sudo is only tried when a root step is pending (a set-up machine's auth log stays quiet); the detached start no longer leaks a file descriptor; stopping in the first second no longer logs "Xvnc didn't start".
Checked on a fresh systemd VM after these: setup with apt's lock held for 25 s and another program on port 5901 (59 s, succeeded); five status calls logged no sudo attempt; a unit pointing at a folder that no longer exists was rewritten on server restart with the desktop untouched; a changed name and size survived a new setup; with another account's X server on
:1the desktop moved to:2; run by root with no sudo rights for the account, the command ran its own two apt commands.Code review (medium) of that delta (e3ee1d4..7ce29e3), eight findings, all fixed with tests:
apt-get update(checked on Debian: it fails at once on the lists lock). The update step now retries for up to ten minutes while that lock is held.Xvnc. It now requires that pid to hold the desktop's socket before believing an empty list.keepsRunning).|; tabs become spaces.Checked on a fresh systemd VM after these: setup with apt's lists lock held for 15 s succeeded; the desktop started with the socket-holding check in place; a second server on the same account (other data folder) left the unit byte-for-byte unchanged through its start, setup attempt (refused) and remove; a hand-made config whose VNC server was down was not replaced without
--yes, and with it the name was kept.Review of e4b793d (last round), all fixed with tests:
vncserver@Ninstance is stopped now, and setup stops with what is left if a root X server from it survives.removedeleted a hand-made config and said "removed". A desktop Codevisor didn't set up is only unshared, after its own question, and the command says it was left running.XDG_RUNTIME_DIR(a system unit withUser=) fell back to process mode. It now reaches the account's systemd through/run/user/<uid>; the README says which install ends up in which mode.Small ones:
--yeshelp says what it answers; a timeout is reported as "running but didn't answer within N s", not "not running"; an answer that isn't a desktop status gives one plain line; any existing config without theservicekey counts as someone else's, readable or not.Re-run on a fresh systemd VM: (1) the old script's desktop on
:1plus a second instance on:2started by hand: status named both,setup --yesleft no root X server and no listener on 5901/5902; (6) with astartxfce4that exits after a second, setup said "The desktop didn't start" with the log instead of "ready"; (5) a config edited to"service": "process"with no unit: after a server restart the desktop ran under the user unit and the config saidsystemd-user; (4) the server as a system unit withUser=codevisorand noXDG_RUNTIME_DIR: the desktop ran incodevisor-desktop.service, and restarting the server left the same Xvnc pid.After the read of e4b793d..53b0123, one bug fixed, with a test that fails without it: the server's start-up restart (
resume) could overwrite a desktop unit that names another server's data folder. It now leaves that unit alone, the desktop stays down, and status says "Another Codevisor server on this account already runs a desktop". Also: an X server left behind by a killed runner is now stopped whenever neither service is set up, whatever the kind, so a desktop moving from a plain process to the user unit can take its display (test added).🤖 Generated with Claude Code