Install the X display programs in a sandbox on first use
- Part of #9347
Why
The shared X11 desktop (pydantic_ai_harness/_x11.py, from #11211) needs Xvfb, xdotool, xwininfo, scrot and setsid in the sandbox. It also needs an X socket directory, which Xvfb only creates when it runs as root. Most sandbox images have neither. This PR makes the start script set both up on first use, so the follow-up PRs can give Modal, Sprites, BoxLite and smolvm sandboxes a desktop with one flag.
Changes since last review
-
The panel shows the image's own browser (74885eb82f). When the desktop first starts, it looks on
PATHforfirefox-esr,firefox,chromium,chromium-browserandgoogle-chrome, in that order. The first one found gets a labelled button (Firefox, Chromium or Chrome) next to Terminal. With none, the panel has only Terminal. Nothing is installed for it. -
Chromium and Chrome get
--no-sandboxonly where they need it: as root, or whenunshare -Ur truefails. Live, on a Chromium-only image:How it ran unshare -UrFlag Result Root n/a added Opens. Without the flag: "Running as root without --no-sandbox is not supported". User, default seccomp fails added Opens with the warning bar. Without the flag: "No usable sandbox!". User, seccomp=unconfinedworks none Opens with no warning bar -
Live runs (2026-11-20,
ComputerUseonly, no shell; task P is "latest Python on python.org", task E is "title of example.com"; requests per run, with the earlier study's count in brackets):Sandbox Panel Sonnet P Sonnet E GPT P GPT E Browser use Docker default Terminal, Firefox 5 (7) 3 (3) 6 (6) 5 (5) All 4 clicked Firefox at step 2. All correct. Docker, Chromium-only image Terminal, Chromium 7 4 6 5 All 4 clicked Chromium at step 2. All correct. Modal (no browser) Terminal only 9 6 9 15 No browser button. All went through the terminal (curl, python, apt-get install lynx). All correct. -
desktop_display()moved here (1676076fe3). Modal (#12964) had its own helper, and Render, OpenSandbox and Kubernetes each built the display and mergedDISPLAYinto their env themselves. Now one helper in_x11.pygives every Xvfb desktop backend the same start and env. -
Docs: "What the display needs" now says how the browser is found, what each default image has, and how to add one (
RUN apt-get update && apt-get install -y firefox-esr). -
Tests:
test_docker_desktop.py53 passed,test_x11.py62, docs parity 909. New:test_the_panel_has_a_labelled_button_for_the_first_browser_the_image_has,test_without_a_browser_the_panel_has_only_the_terminal_button,test_chromium_gets_no_sandbox_only_as_root_or_without_user_namespaces.
New public surface
None. desktop_display() in _x11.py is private.
User-visible behavior
When the display first starts and a program is missing, the start script installs xvfb xdotool x11-utils scrot util-linux with apt-get, as root or through passwordless sudo. Without either, the error names what's missing:
Could not start the display in <sandbox>: it needs Xvfb, xdotool, xwininfo, scrot and setsid (missing xdotool scrot, and there's no root or passwordless sudo to install them with)
A failed install says which packages failed and shows the end of apt-get's log. For a user without root, the script creates /tmp/.X11-unix itself. The start also refuses when an X server outside the sandbox already listens on display :1, which happens when the sandbox shares the host's network namespace, as OpenSandbox's Docker runtime does by default. In that case xdotool would reach the host's X server before ours. Our Xvfb runs with -nolisten local, so an abstract socket for :1 is never ours; a live smolvm check showed it creates only the socket file. The start timeout goes up to 600 s to leave room for the install.
The desktop's programs (fluxbox tint2 xterm) are installed by a separate apt-get install, so one the package lists lack never stops the display. After a failed desktop-program install that followed a working apt-get update, the missing set is written to /tmp/pydantic-ai-desktop-install-failed. Later starts with the same missing set skip the install. A failed update records nothing, a successful install removes the file, and deleting the file forces a retry.
DESKTOP_PACKAGES in _x11.py is the one tuple of every Debian and Ubuntu package the desktop needs, for sandboxes that build them into their image. desktop.Dockerfile installs all of it; util-linux was added there.
The panel's terminal entry is a button labelled "Terminal", placed first on the panel, so its place doesn't depend on whether the image has a browser. The image's browser, if it has one, gets a labelled button after it. In the boxd live runs (#12968), GPT-5.5 needed 7 to 19 requests with an icon-only launcher, against about 3 to 13 with a labelled button.
The E2B tests' scripted X display moves to tests/harness/_fake_x_display.py, so the follow-up PRs' tests can use it.
Verification
tests/harness/docker_container/test_docker_desktop.py: install as root and through sudo, the error without either, a failed install, and the socket directory for a user without root.tests/harness/docker_container/test_docker_desktop.py:test_the_desktop_image_installs_every_package_the_desktop_needs,test_a_failed_desktop_install_is_not_run_again_for_the_same_programs,test_a_working_desktop_install_removes_the_record_of_a_failed_one,test_a_failed_desktop_install_is_not_recorded_through_a_symlink.tests/harness/e2b_sandbox/test_e2b_desktop.py::TestStartScriptruns the start script in a real shell, now with stubs for the install check, and checks that an X server outside the sandbox on:1stops the start.- Live: an E2B desktop and a Daytona sandbox (uid 1001, no
xdotool, no/tmp/.X11-unix) started their display through this script in 9 s and 7 s. The Modal, BoxLite and smolvm PRs above this one also start their displays through it; see their live runs. - Live on the Docker desktop image, 2026-11-20:
- The panel is 1280x40 at y=760, and the Terminal button spans x 4 to 117.
- One click at (60, 780) opened xterm at 1280x738, with the keyboard focus: typing
echo panel-ok-$((6*7))printedpanel-ok-42. - With the button after the Firefox icon (the earlier order), the click was at (95, 780).
What changes for existing users
Nothing for DockerContainer(desktop=True) or E2BSandbox(desktop=True) on their default images, which already have the programs. A custom image without them now gets them installed instead of failing.
Review rounds
One round with gpt-6.2-sol on this PR's own diff. Three findings, all edge cases left for a real issue: an attached sandbox running Xvnc with no Xvfb and no root fails the install check; cancelling the first start under sudo -n can't stop root's apt-get; when one program is missing, apt is asked for all five packages, which can upgrade ones already installed.
Depends on
#11211. First of a stack: this, then Modal (#12964), Sprites (#12965), BoxLite (#12966) and smolvm (#12967).
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
- PR title is fit for the release changelog.