Skip to content

Show apps opened by agents and terminals in the Linux desktop (851-4494) - #1498

Merged
pondorasti merged 4 commits into
mainfrom
alexandru/851-4494-linux-desktop-step-7-apps-opened-by
Nov 27, 2026
Merged

pondorasti merged 4 commits into
mainfrom
alexandru/851-4494-linux-desktop-step-7-apps-opened-by

Conversation

@pondorasti

@pondorasti pondorasti commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Fixes 851-4494

Step 7 of the Linux desktop design (851-4479, decision 6): what the server starts on a machine with a running Codevisor-managed desktop gets that desktop's DISPLAY and XAUTHORITY, so a window it opens shows in the desktop you are watching.

What changed

  • One helper, desktopEnvironment (apps/server/src/desktop/desktop-environment.ts). It reads screen-sharing.json each time a process is started, so a desktop that is set up, moves to another display number or is removed while the server runs is followed by the next launch.
  • Four launch paths take it as the same hook (launchEnvironment), wired once in serve.ts:
    • agent harnesses: every adapter starts sessions from the runtime's envFor, which now applies it;
    • terminals: new, restarted and restored ones all go through the terminal manager's spawn preparation;
    • exec commands run on the machine for an agent (the environment is now read per command instead of once at server start);
    • stdio MCP servers and plugins (a browser MCP server is the "browser under test" case).
  • Precedence, documented in apps/server/README.md:
    1. An environment that already has DISPLAY or WAYLAND_DISPLAY (even empty) is left exactly as it is, cookie included.
    2. Otherwise DISPLAY and XAUTHORITY are set together, never one without the other.
    3. The pair Codevisor sets is marked with the display (CODEVISOR_DESKTOP_DISPLAY) and the config it came from (CODEVISOR_DESKTOP_CONFIG). A server manages only its own pair: replaced when the desktop moved, taken away when it is down or removed. Another server's pair (a dev server with its own data folder, started in a Codevisor terminal) is left exactly as it is unless this server has a desktop of its own up.
    4. Whatever runs later (a shell profile) still wins.
  • Nothing is set with no desktop, with a desktop that is not up, with a config that has no service key (someone else's desktop, including the dev container's own script), with an older port-based config, or on macOS.
  • One check for "up" (desktop-liveness.ts), shared by codevisor desktop status and the helper: the socket is there and private, and the session mark names a live process that is this server's desktop runner. A stale mark whose process id was reused doesn't count.
  • At server start, restored terminals and agent turns the server continues by itself wait for the desktop (resumed by the server, or by systemd after a reboot), at most 15 seconds from the server's start, and not at all for a desktop nothing will start. A terminal's launch environment is now resolved when its process is spawned.
  • No new setting or UI. serve.ts was at the 500-line cap, so a small helper moved to serve-boot.ts and its two imports from that file were merged.

The server has no user setup scripts today (worktree setup only creates the worktree), so there was no such path to cover.

Not changed, on purpose

  • Codevisor's own tools (its browser, Computer Use, Git) keep the server's own environment.
  • A process that is already running keeps the environment it started with. A terminal opened before the desktop was set up has no display; open a new one. OpenCode sessions already running keep the display their server started with.

Proof

Dev container (node:22-bookworm, the dev container's entry script, started without its own desktop so the server's environment has no DISPLAY), one container at a time, removed afterwards. The desktop was set up with codevisor desktop setup --yes (config has "service": "process", display :1).

Captured inside the container with import -window root (ImageMagick), on the second commit; the launch paths it shows are unchanged since:

Desktop with windows opened by a Codevisor terminal, an OpenCode agent and an exec command

Window Started by
xterm "Opened from a Codevisor terminal" a command typed into a terminal made with POST /v1/terminals
xterm "Opened by an agent (OpenCode)" an OpenCode agent started with POST /v1/agents, running the command with its shell tool (the agent replied "done")
xclock POST /v1/exec

Run again on the final commit (echo "DISPLAY=[$DISPLAY] XAUTHORITY=[$XAUTHORITY]"):

When Result
No desktop exec: DISPLAY=[] XAUTHORITY=[]
After codevisor desktop setup desktop status: running. New terminal and exec: DISPLAY=[:1] XAUTHORITY=[/root/.codevisor/run/codevisor-desktop.xauth], with both marks. The mark's process is node …/cli.js desktop run --data-dir /codevisor-data/data
Machine restarted (docker restart: every process dies, the socket and mark files stay) The stale mark stayed for about 4 s, the server started the desktop again (new mark at about 10 s), and the restored terminal's shell appeared only then, with DISPLAY=:1
Runner killed with kill -9, mark left pointing at another live process desktop status: isn't running. exec: DISPLAY=[] XAUTHORITY=[]

Seen on the second commit and not run again: after codevisor desktop remove, and with a hand-made config without a service key, a new terminal and exec got nothing; the first agent's harness process kept :1 after the removal, as documented.

Not shown live, unit tests only: the "already has a DISPLAY" rule, the rules for a marked pair (this server's and another server's), agent turns waiting at server start, and macOS. The Mac app was not driven (this Mac's screen is locked).

Tests

  • desktop-liveness.test.ts: the shared up-check (socket problem, no mark, dead runner, reused process id, another server's runner).
  • desktop-environment.test.ts: the helper (running managed desktop, launch-time read, existing display kept, a pair from this server followed or taken away, a pair from another server left alone, Wayland beside Codevisor's pair, stopped desktop, someone else's config, old port config, no cookie, unreadable config, macOS).
  • desktop-manager-robust.test.ts: the start-up wait (until up, gives up after 15 s, no wait with no desktop, someone else's, another server's unit, or a systemd it can't reach).
  • One test per launch path that the hook is applied when the process starts: launch-environment.test.ts (agent runtime, including a hook that has to wait), terminal-manager-shell.test.ts (including restored terminals waiting, one closed while waiting, and the saved program kept in snapshots meanwhile), exec-tasks.test.ts, mcp-stdio-environment.test.ts (a real stdio server reporting what it was started with).

Review

  • First round (code-review skill): a stopped desktop no longer gives its display, the injected display is marked, and the display is never paired with a foreign cookie.
  • Second round (reviewer's five items): restored terminals wait for the desktop; one shared up-check that also wants a live runner; OpenCode's pool checked (an idle server stops 60 s after its last session ends, so nothing to prune) and a README sentence added; the Wayland branch returns the stripped environment; another server's pair is left alone.
  • Code-review skill once more on that result. Fixed: agent turns continued at start wait too; a restored terminal keeps its saved program in snapshots while it waits; the up-check wants this server's runner; the wait is skipped when nothing will start the desktop and is bounded from the server's start; the helper no longer needs the account's passwd entry. Not changed: harness and account environments are overlaid after the hook in the adapters, but none of them can set a display today.

🤖 Generated with Claude Code

pondorasti and others added 4 commits November 26, 2026 16:41
Agent harnesses, terminals, exec commands and stdio MCP servers that the
server starts while a Codevisor-managed desktop is set up get its DISPLAY
and XAUTHORITY. One helper reads screen-sharing.json at each launch, so a
desktop that appears, moves or goes is followed. An environment that
already has a display is left alone.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…sor's

Review findings on 851-4494:
- A desktop that is set up but not running no longer gives its display,
  so an app that can run headless doesn't fail on a display that isn't
  there.
- The injected pair is marked with CODEVISOR_DESKTOP_DISPLAY, so a server
  started from a Codevisor terminal still follows the config instead of
  treating the inherited display as chosen on purpose.
- DISPLAY and XAUTHORITY are only ever set together.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…splay it is

Second review round on 851-4494:
- Restored terminals start their shells once the desktop has been resumed
  and is up (at most 15 seconds), and every terminal's launch environment
  is now resolved when its process is spawned.
- One shared check for "the desktop is up", used by status and by the
  launch environment: the socket is there and private, and the session
  mark names a live desktop runner, so a stale mark with a reused process
  id no longer counts.
- The marked pair records which server's config it came from. A server
  manages only its own pair; another server's is left as it is unless
  this server has a desktop of its own up.
- An environment stripped of Codevisor's pair is what is returned when a
  Wayland display is there too.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
- Agent turns the server continues at start wait for the desktop like
  restored terminals do.
- A restored terminal keeps its saved program in snapshots until its
  process has started, and its resume file is only written then.
- The up-check wants the runner of this server's data folder, so another
  server's desktop on the same account doesn't count.
- The start-up wait is skipped for a desktop nothing will start, and
  ends 15 seconds after the server started at the latest.
- The launch environment no longer needs the account's passwd entry.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@pondorasti
pondorasti force-pushed the alexandru/851-4494-linux-desktop-step-7-apps-opened-by branch from 0e777e8 to d917884 Compare November 27, 2026 01:10
@pondorasti
pondorasti merged commit cf10bcc into main Nov 27, 2026
@pondorasti
pondorasti deleted the alexandru/851-4494-linux-desktop-step-7-apps-opened-by branch November 27, 2026 01:12
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