Add `DockerContainer(desktop=True)` for computer use

Merged
AD
11d ago
+2835-46

Part of #9347

This PR adds DockerContainer(desktop=True): a local Docker container with a graphical desktop, so ComputerUse() (#11037) has a screen to see and control. It also adds the X11 code every Xvfb desktop sandbox shares: the xdotool driver, the start script, and a display that starts the first time it's needed. The agent also gets the container's shell and files, so Coder and ComputerUse can work on the same machine.

This PR now includes the X11 provisioning work from #12963: the start script installs the display's programs on a sandbox that lacks them, and the panel gets a button for the image's own browser. The sandbox PRs (#11210, #11211, #12968, #12964, #12965, #12966, #12967, #13986, #13987, #13988) each build on this PR directly.

agent = Agent('anthropic:claude-sonnet-5-5', capabilities=[DockerContainer(desktop=True), ComputerUse()])

How to review:

  1. Read _x11.py. It turns each action into an xdotool command, releases held keys and buttons when an action fails, and holds the start script.
  2. Then read _display.py. LazyDisplay starts the display on first use or before a shell command, and decides what to retry.
  3. Then read _drag.py and the desktop parts of docker_container/_backend.py.

What the PR adds

  • Options. desktop=True gives a 1280x800 screen. For another size, pass desktop=(width, height). It works on DockerContainer and DockerContainerBackend.
  • The image. Without an image, the first run builds pydantic-ai-desktop:<hash> from DESKTOP_DOCKERFILE, and later runs reuse it. It has Debian, Xvfb, fluxbox, a tint2 panel with Terminal and Firefox buttons, and CJK fonts. DESKTOP_DOCKERFILE is exported from pydantic_ai_harness.docker_container.
  • Images without the programs. 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. The desktop's own programs (fluxbox tint2 xterm) are a separate install, so one the package lists lack never stops the display.
  • The panel. A labelled Terminal button comes first. If the image has a browser (firefox-esr, firefox, chromium, chromium-browser or google-chrome), it gets a labelled button after it. Chromium and Chrome get --no-sandbox only as root or when user namespaces don't work.
  • The display. It starts on first use or before the first shell command, whichever comes first. Starts at the same time share one X server, under a lock. Shell commands get DISPLAY, so a program the agent starts opens on the desktop. The first screenshot waits for the panel, so it isn't black.
  • Input. Actions run through xdotool; screenshots and zoom through scrot. Typing is ASCII only: any other character fails the action, with a message the model reads.

Design decisions

Build the image on first use, not publish one. We host nothing and keep nothing patched. The cost is a slower first run. The image is cached, so the docs show the rebuild command (--pull --no-cache) for security updates.

The host's network is documented, not refused. With --network host, programs in the container could reach an X server on the host through Linux's abstract socket. Xvfb starts with -nolisten tcp -nolisten local, so our own X server listens only on its socket file. The start also fails when another X server already answers on display :1 through a shared network. The docs and THREAT_MODEL.md say not to put a desktop container on the host's network. An earlier version read the container's NetworkMode and refused; that check is cut, since the two guards above already keep xdotool off a foreign X server.

Start Xvfb with -noreset. Without it, the X server resets each time an xdotool command exits while no window manager runs. A reset moves the pointer to the middle of the screen and releases held buttons.

Start the display before each shell command. The instructions tell the model that a program it starts from the shell opens on the screen. So every shell command starts the display, outside the backend's lock. File operations that use the shell also start it, so a broken desktop makes them fail too.

ASCII-only typing. To type a character with no key, such as é, xdotool remaps a spare key for each keystroke. Chrome reads the key map afterwards and gets a different character, or none. Refusing the action tells the model why.

LazyDisplay holds the start and retry rules for every harness desktop. Creating it starts nothing. A call that only looks, such as a screenshot, is retried once after a failure, which restarts a lost display. An action is never retried, so nothing is done twice.

Drags move in steps. A drag presses, moves in steps of at most 200 pixels (at least two), 50 ms apart, then moves 1 pixel past the end and back before releasing. Chromium answers each pointer position with what the page said about an earlier one, so without one more position after the page accepts the drop, it cancels the drop on release. A web page's drag-and-drop can still miss a drag now and then; the docs say so.

One builder for every Xvfb backend. x11_display(run_command, size, describe_environment) returns the LazyDisplay, or None without a desktop. Each backend sets DISPLAY in its own env in one line.

Changes in this round

  • Drags: the press pause is gone, and the drag ends with a 1-pixel move past the end and back.
  • One x11_display() builder replaces x11_display() plus desktop_display().
  • The Docker host-network refusal, the Firefox policies.json, ca-certificates and the emoji font are cut.
  • The release timeout is one 5 s RELEASE_TIMEOUT in _display.py, which the X11, Daytona and boxd drivers share.
  • Clearer names across _x11.py and _display.py (checked_desktop_size, DEFAULT_DESKTOP_BASE_IMAGE, mark_stopped, operation=, TINT2_BUTTON_STYLE), and the test-only screen() helper is now full_display().
  • The two live-test waits flagged in review carry a real-wait reason, and test_x11.py no longer patches a private limit: it types 20,000 characters and drags through 2,500 points instead.

Evidence

MeasurementResult
Time per wheel click, used in the scroll's time limit20 ms
Rebuilding after docker rmi without --no-cacheShows CACHED and keeps the old layer
Panel start without waiting for a window managertint2 waits about 16 s for one
A Daytona sandbox (uid 1001, no xdotool, no /tmp/.X11-unix) and an E2B desktop starting through the install path7 s and 9 s

Live tests (tests/harness/docker_container/test_docker_live.py), each event observed with xev: the display contract, the first screenshot isn't black, key names arrive as their X key names, buttons 1, 2, 3, 8 and 9, one wheel click per scroll step, a drag holds the button the whole way, a cancelled action releases what it pressed, and typed text reaches the focused window.

Live runs (2026-11-20, ComputerUse only, no shell; task P is "latest Python on python.org", task E is "title of example.com"; requests per run):

ImagePanelSonnet PSonnet EGPT PGPT EBrowser use
Docker defaultTerminal, Firefox5365All 4 clicked Firefox at step 2. All correct.
Docker, Chromium-only imageTerminal, Chromium7465All 4 clicked Chromium at step 2. All correct.

Known limits and follow-ups

  • There is no live viewer. To watch a run, save each screenshot as it arrives. The docs show how.
  • A screenshot whose base64 text is over 10 MiB goes past the backend's output limit. Only near-random pixels get that large.
  • Containers keep running after a run, as with DockerContainer today. The docs show how to clean them up.
  • The start script installs the display's programs as root. THREAT_MODEL.md says so; a maintainer may want to weigh in on that design.

Testing

  • Unit tests with a fake docker CLI, LazyDisplay against a fake driver, and the X11 driver against scripted command results (test_x11.py 62 passed, test_docker_desktop.py 52 passed; _x11.py, _display.py and _drag.py at 100% branch coverage).
  • Live tests on Docker as above.

Merging

This PR builds on #11006 and #11037. Merge it before the sandbox PRs listed above.

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.