Add `desktop` to `KubernetesSandbox` for `ComputerUse`

Merged
AD
11d ago
+330-15

Part of #9347. Builds on #11040.

This PR adds KubernetesSandbox(warm_pool=..., desktop=True), which lets ComputerUse() (#11037) see and control a screen inside an agent-sandbox pod. It uses the shared X11 driver from #11040, the same one E2B uses.

agent = Agent(
    'anthropic:claude-opus-5-5',
    capabilities=[KubernetesSandbox(warm_pool='desktop-pool', desktop=True), Shell(), ComputerUse()],
)

How to review: kubernetes_sandbox/_backend.py: __init__, run and the new _run_argv. Then the test cluster's sandboxd.Dockerfile and pools.yaml.

What the PR adds

  • Options. KubernetesSandbox.desktop and KubernetesSandboxBackend(desktop=...) take True for 1280x800, or (width, height). The default is False. Specs can set it.
  • The screen. The documented template runs as a non-root user, so nothing is installed. Its image must already have the programs. (A custom root template would install them through the shared start script.) Otherwise the first use raises WorkspaceUnavailableError naming the missing ones. The docs show a Dockerfile with the Debian packages xvfb xdotool x11-utils scrot util-linux and a browser.
  • Shell commands see it. run() starts the display before each command and adds DISPLAY=:1, so a program the agent starts from the shell opens on the screen. The display's own commands go through _run_argv, which doesn't start it.
  • No port. No port is added to the pod for the screen, and the X server listens on no TCP or local socket.
  • Test cluster. cluster/sandboxd.Dockerfile gets a desktop target with the X packages. pools.yaml gets a desktop template and desktop-pool, and up.sh builds, loads and waits for it.
  • Live tier. The nightly live run now builds a second image (the desktop target) and keeps one more warm pod (desktop-pool, 1 replica, with the other pools' 64Mi/50m requests and 512Mi limit). Two tests in test_kubernetes_live.py. The first runs the shared display contract on desktop-pool, and checks that a shell command sees the display (xdotool getdisplaygeometry). The second checks that python-pool, which has no X programs, refuses by name.
  • Docs. A "Give the agent a desktop" section on the Kubernetes page and README, a desktop row in the configuration table, a row in ComputerUse's "Where the screen comes from" table, and the harness skill reference.

Design decisions

Refuse rather than install. The documented pods run runAsNonRoot with allowPrivilegeEscalation: false, so there is no root and no sudo. The shared start script already refuses in that case and names what is missing.

Start the display before each shell command. This is the decision from #11040.

Tests

tests/harness/kubernetes_sandbox/test_kubernetes_desktop.py, against the existing fake cluster. The fake gets a display hook that answers the X commands with the shared FakeDisplay, routed by FakeDisplay.answers. Every other command still runs through the fake sandboxd.

  • a shell command finds the desktop running, with DISPLAY and the user's env;
  • a desktop size reaches the display it starts;
  • a reattached desktop sandbox gets its display back, and a non-desktop one has none;
  • KubernetesSandbox(desktop=True) plus ComputerUse runs the computer tool on a new claim.

test_kubernetes_sandbox.py adds desktop to the spec schema list and the from_spec round trip.

test_kubernetes_desktop.py 5 passed, test_kubernetes_sandbox.py 49.

Live check

Run on 2026-11-20 on a new kind cluster built with tests/harness/kubernetes_sandbox/cluster/up.sh (all warm pools ready, desktop-pool included), with Python 3.14 as in the nightly job:

PYDANTIC_AI_HARNESS_KUBERNETES_LIVE=1 KUBERNETES_REQUIRE_LIVE=1 uv run --python 3.14 pytest -m kubernetes_live \
  tests/harness/kubernetes_sandbox/test_kubernetes_live.py \
  -k 'claim_names_its_pool or desktop_pool_serves or without_the_display_programs'
3 passed

test_a_desktop_pool_serves_the_display passed against the real pod: the shared display checks, the window manager answering on the display, the tint2 panel showing, and its terminal button opening xterm. test_an_image_without_the_display_programs_is_refused_by_name passed on python-pool. The same tests on Python 3.12 fail in teardown for every live test, the existing ones included: gRPC's channel cleanup waits on a thread lock inside the event loop, which the test suite's blocking-call check rejects. That is outside this PR.

Model runs on desktop-pool (kind), with ComputerUse and the sandbox only, no shell (2026-11-23). The task: "Open a terminal, type echo hello-desktop, press Enter, and tell me what the terminal shows." In each run the final screenshot shows the command, hello-desktop and a new prompt, and the model reported it. No tool errors.

ModelResultRequests / computer callsTime
anthropic:claude-sonnet-5-5pass4 / 38.9 s (warm pod)
openai-responses:gpt-5.5pass4 / 310.0 s

The pod's peak memory over its whole life was 139 MB (Sonnet run) and 57 MB (GPT run), under a quarter of 512Mi, so the desktop pool uses the same limit as the others.

The documented Dockerfile and template were also built and applied as written: the pool became ready, the display contract passed at 1280x800 as uid 1000, and the panel's Firefox button opened a visible Firefox window.

Changes in this round

  • The display is built with the shared x11_display() from #11040, and DISPLAY is set in one line.
  • desktop-pool goes back to 50m/64Mi requests and a 512Mi limit, and the memory comment is gone: the measured peak was 139 MB.
  • DESKTOP_EXTRA_PACKAGES is cut: up.sh never passed it.
  • The live xwininfo -root | grep -c Width assert is cut: the xdotool getdisplaygeometry check under it also proves the shell sees DISPLAY.
  • The docstrings and the skill bullet point at the display's programs instead of listing packages in two different ways.
  • The test that an image without the programs is refused by name is cut: the message is the shared code's, covered by the E2B tests and test_x11.py. The live refusal test on python-pool stays.
  • The test fake routes display commands with the shared FakeDisplay.answers instead of its own program list.

Review rounds

Four rounds of gpt-6.2-sol (high effort) on the diff against the shared desktop code.

  • Round 1: the desktop example named desktop-pool, but the setup only creates python-pool. The docs now copy the template and pool under the names desktop and desktop-pool.
  • Rounds 1 to 4: the install rule is now stated as the start script applies it. Without root or passwordless sudo in the pod, the image must have the programs. The documented template is runAsNonRoot with no privilege escalation.
  • Round 4: one test gap, logged.

Logged, not fixed:

  • aclose() racing the first command's display start.
  • A pod replaced under the same claim keeps the "display started" state (the parked restart detection).
  • The shared Trio lock issue.

Known limits

  • The agent can type only ASCII, as with every X11 desktop.
  • Without fluxbox in the image, the screen has no window manager.