Skip to content

Set up, check and remove a Linux desktop with codevisor desktop (851-4485) - #1492

Merged
pondorasti merged 11 commits into
mainfrom
alexandru/851-4485-linux-desktop-step-2-codevisor-desktop
Nov 26, 2026
Merged

pondorasti merged 11 commits into
mainfrom
alexandru/851-4485-linux-desktop-step-2-codevisor-desktop

Conversation

@pondorasti

@pondorasti pondorasti commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

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 setup
==> Checking this machine
    Ubuntu 24.04.5 LTS, no desktop set up
==> Installing Xfce and TigerVNC (about 640 MB)
==> Starting the desktop as codevisor
==> The desktop is ready. Open it from this machine in Codevisor.
  • codevisor desktop setup gives 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 status says whether it is set up and running, as which account, whether it starts at boot, and where its log is.
  • codevisor desktop remove stops 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

  • The server owns setup. The command makes three kinds of calls: look (GET /v1/desktop), do a root step (POST /v1/desktop/packages, POST /v1/desktop/replace-old), and start (POST /v1/desktop/start); DELETE /v1/desktop removes. Step 4's button can drive the same calls.
  • Root is needed only for packages and for removing an old root desktop. Three cases, by the sudo the server's account has: no password needed (or the server is root) → the server runs the step with sudo -n; sudo asks → the command runs sudo in 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).
  • The command never runs what the server's answer says. It builds the apt and systemctl commands itself, from the machine's own files, with the code the server uses. The answer only says whether a step is needed and who runs it. See "What the command trusts" below.
  • Only apt is known so far. On other systems setup names the programs to install (step 3).
  • Boot service of the server's account. codevisor-desktop.service is a systemd user unit that runs codevisor 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.
  • An older root desktop is replaced after asking once (or with --yes). It is recognised by the unit the old script wrote. Declining, or having no terminal to answer in, changes nothing.
  • Viewers that are connected are disconnected when the desktop is withdrawn or becomes unsafe. The server looks at each live relay every five seconds, and at once on a removal, and closes it with the reason ("The desktop on this machine was removed.", or the socket's problem).
  • A new desktop shows up at once. After a start or a removal the server publishes a new event, 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).
  • Setting up twice is safe. A running desktop is left alone, so nothing open in it closes.

apps/server/README.md documents the commands, the sudo cases and the boot service.

What the command trusts from the server's port

codevisor desktop setup may be run with sudo, and it talks to 127.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:

  • Never trusted: which commands to run. The status no longer carries any (install and oldDesktop.removal are gone from the API). Whether an old root desktop exists is read from /etc/systemd/system by the command, and unit names must match vncserver@<digits>.service.
  • Trusted as a yes/no only: missing (install needed?), sudo (who runs the root step), state and managed (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.
  • Printed, after cleaning: system name, problem, failure, log hint and error texts lose control characters (no terminal escapes); missing programs are matched against the known list; the account name must look like one, else "the server's account" is printed. The linger command is built by the command, not taken from the answer.
  • Other commands (status, machines, flags, auth, update, logs) print what the port answers but execute nothing from it; logs and start run journalctl, tail and systemctl with 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 through sudo -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), every status wording, remove with and without confirmation.
  • routes/desktop.test.ts through 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.ts through 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/tcp and tcp6 (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.
  • Swift, 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 new codevisor account with passwordless sudo):

  • status before: "No desktop on this machine."
  • setup installed the packages through sudo and started the desktop. Everything in it runs as codevisor; 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.
  • Through the real server: the feature is listed, capabilities answer available, the relay gives the RFB 003.008 greeting.
  • remove --yes with 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 codevisor with linger, as the installer sets it up):

  • Fresh setup through passwordless sudo: the desktop is codevisor-desktop.service of that account, socket /run/user/1000/codevisor-desktop.sock mode 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 --yes with 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). status named it. setup with no terminal and no --yes refused and changed nothing. setup --yes replaced it: old unit and unit file gone, port 5901 closed, new desktop running as codevisor.

  • 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 setup left the running desktop alone (same Xvnc pid) and said so; a window opened in the desktop belongs to codevisor; another account is refused on X11 ("Authorization required") and on the socket (permission denied); 2× and 1× through the server answer ok; after removing one package, setup had the server install it in the background and waited; stopping the unit ends with Result=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:

  • setup took 59 s; server.features.updated arrived at 59.3 s and /v1/info listed the desktop from then; after remove --yes the event arrived again and the desktop was no longer listed.
  • After removal no desktop process is left running and the socket and cookie are gone.

Not checked by hand:

  • The Desktop card appearing in the Mac app itself. Computer Use on this Mac reads no windows of any app (tried again after the last push), so the app can't be driven. What stands in for it: the server publishing the event (seen in the dev container: 0.3 s after setup finished, and again after removal) and the app re-reading a machine's features when the event arrives (Swift test).
  • Typing a sudo password at a real keyboard (it was fed through a pty), a machine with IPv6 turned off, and distros other than Ubuntu 24.04 and Debian 12.
  • Any real machine, the VPS included.

Not in this PR

  • codevisor desktop remove --remove-packages from the design. Recording and removing packages is per package manager, so it moves to step 3 with the other package managers. remove leaves packages installed and says so.
  • Root's own old config (/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.
  • The installer hint, the codevisor setup question and the app's button are steps 6 and 4.

Code review (medium) on this PR

Ten findings. Fixed, each with a test:

  1. The desktop couldn't start with IPv6 turned off (/proc/net/tcp6 missing). A missing table now counts as empty.
  2. Without systemd, any live process with the saved pid counted as the desktop. It must now be the runner.
  3. Without systemd, an old socket file made a dead desktop look running, so a server boot didn't restart it. The boot now goes by the runner being alive, and status says "the desktop's process isn't running".
  4. A package install longer than five minutes would have failed in the command with "server is not running" (the HTTP client's limit). The server now installs in the background and the command polls; the status carries busy and failure.
  5. A config something else wrote (the dev container's) decided the display number. Only this server's own config does.
  6. A withdrawn viewer could keep its connection to the desktop until it answered the close. The connection to the desktop is cut at once.
  7. Two setup actions at once could interfere. They run one at a time, and are refused while an install runs.
  8. Setup could say "ready" for a desktop that stopped a second later. The socket has to stay up for 2.5 s first.
  9. A dollar sign in a path broke the unit's command. It is escaped.

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:

  1. A cut-off viewer kept its control seat and its text messages were still handled until it answered the close. Cutting it off now releases the seat and the clipboard bridge and ignores its messages.
  2. A stop arriving while the runner was starting could leave a session behind. The runner starts nothing once it is stopping.
  3. A failed install was reported forever. It is cleared once the programs are there or another action ran.
  4. The restart at server boot ran outside the one-at-a-time queue and for configs it can't share. It waits its turn and needs a usable config.
  5. The TCP port check passed silently when it couldn't read the kernel's table. It fails then; only a missing IPv6 table counts as empty.
  6. On stop, only the group leader got the grace period. Everything in the group gets it.
  7. The command could say "already running" for a desktop the server then replaced. The status says whether the server runs the desktop (managed).
  8. An install outlasting the wait was reported as "still missing", and one unanswered check ended setup. Both are handled.
  9. A desktop started without systemd read as stopped once systemd became available. It reads as running, and starting or removing also stops a desktop left under the other kind of service. This also covers the finding I had left from the first review.

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:

  1. Security: the command ran as root whatever command lists the port's answer carried. It builds them itself now (see above).
  2. The boot unit named the running runtime's folder and was never rewritten. The server rewrites it when it starts and when setup finds the desktop running.
  3. Any program on port 5900 + display made the desktop refuse to start. Only Xvnc's own listening sockets are checked (by socket inode), on any port.
  4. A recorded display was reused without checking. It is reused only while free or held by this desktop's own X server.
  5. apt failed when its lock was held. The install waits up to ten minutes for the dpkg lock (and see the delta review below for update).
  6. Setup dropped a name or size from the config and replaced a hand-configured desktop unasked. Both are kept, and replacing asks first.

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 :1 the 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:

  1. Any Codevisor server on the account rewrote the one desktop unit at start, so an alpha or dev server could point it at its own data folder. The unit is now only touched by the server whose data folder it names; another server's setup is refused with a clear message, and its remove leaves the unit alone.
  2. The apt lock wait didn't cover 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.
  3. The "Codevisor didn't set this up" question only fired while that desktop was up. It now goes by the config, up or down.
  4. The TCP check trusted the pid started as Xvnc. It now requires that pid to hold the desktop's socket before believing an empty list.
  5. "Already running" could be printed for a desktop the server then moved to another kind of service. The status says whether a start leaves it alone (keepsRunning).
  6. A check racing with Xvnc's exit could hide the exit reason. A vanished process is left to the exit report.
  7. Display reuse compared against a socket path computed now, not the one in the config.
  8. Multi-line server text could pass for the command's own lines. Further lines are marked with |; 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:

  1. Replacing the old root desktop could leave an instance running that was started by hand and never enabled. Every loaded vncserver@N instance is stopped now, and setup stops with what is left if a root X server from it survives.
  2. remove deleted 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.
  3. In process mode a killed runner left an X server that blocked every restart. The orphan is stopped first.
  4. A server without XDG_RUNTIME_DIR (a system unit with User=) 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.
  5. A desktop recorded as a process wasn't restarted once the server saw systemd. It is started under the user unit at server start and the config is rewritten.
  6. Setup could say "ready" for a crash-looping desktop. The runner writes a mark three seconds after the session started; "ready" and "running" need it.

Small ones: --yes help 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 the service key counts as someone else's, readable or not.

Re-run on a fresh systemd VM: (1) the old script's desktop on :1 plus a second instance on :2 started by hand: status named both, setup --yes left no root X server and no listener on 5901/5902; (6) with a startxfce4 that 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 said systemd-user; (4) the server as a system unit with User=codevisor and no XDG_RUNTIME_DIR: the desktop ran in codevisor-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

@pondorasti
pondorasti force-pushed the alexandru/851-4485-linux-desktop-step-2-codevisor-desktop branch 2 times, most recently from e4b793d to 53b0123 Compare November 26, 2026 21:46
pondorasti and others added 11 commits November 26, 2026 13:53
…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
pondorasti force-pushed the alexandru/851-4485-linux-desktop-step-2-codevisor-desktop branch from 53b0123 to 986483a Compare November 26, 2026 22:06
@pondorasti
pondorasti merged commit 77c1a73 into main Nov 26, 2026
@pondorasti
pondorasti deleted the alexandru/851-4485-linux-desktop-step-2-codevisor-desktop branch November 26, 2026 22:07
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.

1 participant