Harness: add the `ComputerUse()` capability and the `Display` protocols it drives
Part of #9347
This PR adds ComputerUse() to Pydantic AI Harness. It gives the agent one tool, computer. The model sends a list of mouse and keyboard actions. They run in order on the screen of the run's workspace, and the tool returns a screenshot. It works with any model that accepts images.
The OpenAI PR (#11038) and the Anthropic PR (#11039) send the tool as each provider's own computer tool. The sandbox PRs (#11040 onwards) give workspaces a screen.
from pydantic_ai import Agent
from pydantic_ai_harness import ComputerUse, DockerContainer
agent = Agent('anthropic:claude-sonnet-5-5', capabilities=[DockerContainer(desktop=True), ComputerUse()])
How to review:
computer_use/_protocol.py: what a screen must offer.ComputerUseincomputer_use/_capability.py: its fields, the tool,_computer(workflow code) and_perform_call_on_display(the durable step).
Changes in this round
- One failed call stops the rest of its response. When a
computercall fails, the same response's latercomputercalls return "Not executed: an earlier computer action in this turn failed." and don't touch the screen.sequential=Trueruns them one at a time but doesn't stop after a failure. This is what lets each provider adapter map one provider call to onecomputercall, with no merging or splitting of batches (#11038, #11039). - Positions on a stale screenshot are refused instead of scaled. If the display's size differs from the latest screenshot the model saw, a call that gives positions is refused and the model is told to take a new screenshot. Calls without positions, such as
screenshot, still run. The coordinate mapping (about 45 lines and 120 test lines) is gone. max_retries=3on the tool is cut, so it uses the agent's default.ActionsPerformedEventpinsname='actions_performed', as the other capability events do.SupportsZoom.zoomno longer says "at the screen's full resolution": a zoom is a crop in screenshot pixels.- Smaller code: the pressed buttons live on the action runner instead of being passed through five functions, a release returns the buttons that stayed pressed, one context manager holds the screen, and two one-caller helpers are inlined. Names now say what they hold, for example
_RunPressedButtons.maybe_pressed,_FailedCall.error_typeand_CallInputs.buttons_outlive_call. - Docs: dropped the "below about 1024x768" clause, and added a line each for the stale-screenshot refusal and the stop after a failed call.
tests/harness/computer_use/shared_live.pyexportsis_object_listpublicly, so the live tests in #11038 and #11039 import it without a pyright ignore.
What the PR adds
ComputerUse(settle_seconds=2, native=True), with capability idcomputer_useand tool namecomputer.- Display protocols, in
pydantic_ai_harness.computer_use:Display(display_size,screenshot,act),SupportsDisplayfor a workspace backend, and the optionalSupportsZoomandSupportsCursorPosition.UnsupportedActionErrorrefuses an action a display can't run exactly. - What a call does: runs the actions in order until one fails, waits for the screen to settle, and returns a summary, what each action returned, and a screenshot. A failed action still returns a screenshot, with the error in the result's metadata.
- Workspace rules: a read-only workspace gets a schema with only the actions that look, and a call that acts anyway is refused. Positions outside the screen are refused before anything runs.
- Pressed mouse buttons stay pressed across calls until
mouse_up, and the run's end releases any still pressed, however it ends. - Turns: agents that share a screen take turns one whole call at a time.
- Prompt caching is on unless the run's settings say otherwise.
ActionsPerformedEventfor each call that finishes with a screenshot.
Design decisions
One tool that takes a list of actions. Fewer round trips to the model. The instructions tell the model to put actions with a predictable outcome in one call, and to stop where it needs to see the screen.
Stopping a response's calls after a failure lives here, not in each adapter. The model chose a response's calls together, so once one fails, the rest would act on a screen it didn't expect. Doing it in the capability takes about 6 lines and works the same for every provider, so the adapters only translate one call at a time. The run's button record notes the response whose call failed, and later calls from that response are refused. A capability loaded mid-run with defer_loading=True has no run record, so it doesn't stop later calls.
Refuse, don't scale, when the screen changed size. Scaling needed a mapping for every positional action and its tests. No live run needed it: a changed size is rare, and a new screenshot fixes it.
After acting, the tool waits up to 2 s for the screen to stop changing. It takes a screenshot every 50 ms until two in a row match. On a busy desktop, no wait gave 3 stale screenshots in 45, and the wait gave 0, at about 180 ms per call on average.
Short instructions, plus a shell paragraph only when the run has a shell. A tool counts as a shell from its declared command language (code_arg_language='shell' with a code_arg_name).
native defaults to True. A model with its own computer tool is trained on that tool's format, and every other model gets the function tool anyway. native=False sends the function tool to every model, which can zoom, triple-click, hold a key and press or release a mouse button, which OpenAI's own tool can't. In the 2026-11-18 live set, GPT-5.5 succeeded 6 of 6 tasks both with its own tool and with the function tool.
No DisplayRestartedEvent. It needed a new optional core protocol to count restarts and a counter in every sandbox backend, for a case where the next screenshot already shows the new screen. It was dropped; restart detection is parked in draft #11871. The docs say the model isn't told.
No limits on action amounts. No measurement showed a default limit was needed. A developer can refuse any call in a before_tool_execute hook.
Durable execution: each call's screen work is one durable step. The tool is Tool(durable=False), and the capability runs the display work as its own step. A replay never repeats a finished call. When a step fails, the model is told some actions may have run.
Code Mode leaves the computer tool outside run_code. The model has to see each screenshot.
Evidence
- Live set, 2026-11-18: 42 of 42 runs succeeded on the first try, 7 model setups times 6 tasks on a Docker desktop: GPT-5.5 with its own tool and with the function tool, Claude Sonnet 5.5, Claude Haiku 4.5, Gemini 3.8 Flash, and Grok 4.7 and Mistral Large 4 through OpenRouter.
- Calls in one response: Claude Sonnet 5.5, given the computer toolset and two function tools, put a function call between computer actions of one response in 1 of 6 unprompted runs and 4 of 4 prompted runs (16 runs, 80 responses). So a response's computer actions can't always be merged into one call. Each runs as its own call, and the stop after a failure keeps the later ones from acting on an unexpected screen.
- Prompt caching: a run cost $0.318 with caching off and $0.065 to $0.076 with it on.
- Shell paragraph: on the tasks that need the screen, the full paragraph succeeded 20 of 21 at 10.6 requests per run; no paragraph succeeded 31 of 40 at 15.4, with 4 runs hitting the 40-request limit.
Review threads
- The open
generalfinding on unbounded action lists: kept as is. The tool has no default limits by design (see above), and abefore_tool_executehook can cap the actions in a call.
Testing
Unit tests with function models and an in-memory display, under each durability setup: held buttons, turns, read-only workspaces, Temporal replay, and new tests for the stale-screenshot refusal (test_positions_on_a_screenshot_of_another_size_are_refused) and the stop after a failed call (test_after_a_call_fails_the_responses_later_calls_do_not_run).
Local: tests/harness/computer_use/test_computer_use.py 90 passed with 100% branch coverage of computer_use/, tests/harness/test_docs_parity.py 909 passed. ruff and pyright on computer_use/ and its tests: clean. scripts/check_complexity.py --base origin/main: clean.
Merging
This PR builds on #11006. Merge it right after #11006, before the provider and sandbox PRs.