Add `DaytonaSandbox`: a workspace that runs `Coder`, `Shell`, `FileSystem` and every other harness capability in a Daytona sandbox

Merged
AD
1mo ago
+4586-70

Closes #9247.

Adds DaytonaSandbox, which supplies a Daytona sandbox as the run's workspace, so any harness capability runs there.

from pydantic_ai import Agent
from pydantic_ai_harness.coder import Coder
from pydantic_ai_harness.daytona_sandbox import DaytonaSandbox

agent = Agent('anthropic:claude-opus-5-5', capabilities=[DaytonaSandbox(), Coder()])
result = agent.run_sync('Clone https://github.com/pydantic/pydantic-ai and summarize how capabilities work.')

DaytonaSandboxBackend is the Daytona implementation of core's WorkspaceBackend. await backend.get_sandbox() returns the daytona.AsyncSandbox, and DaytonaSandboxBackend(sandbox=...) wraps one you already have. Monorepo version of pydantic/pydantic-ai-harness#736, brought in line with how Modal, E2B and Sprites work on main today.

Behaviour (the same as Modal, E2B and Sprites)

  • Created on first use. Constructing the capability does no remote work. A new sandbox gets working_dir created for it; an attached one must already have it, or commands raise WorkspaceError, as with Sprites and E2B.
  • Continuing. A later run with message_history=result.all_messages(), or workspace=result.workspace.ref, reattaches to the same sandbox. workspace='new' starts a fresh one.
  • Never deleted by Pydantic AI. DaytonaSandbox().destroy(ref) deletes by ID without starting a stopped sandbox, and returns quietly when the sandbox is already gone. Creation logs Created Daytona sandbox <id> at INFO.
  • Errors. Missing or rejected credentials, an unknown snapshot, a deleted sandbox, and one Daytona stopped while idle under a backend still in use raise WorkspaceUnavailableError. A command over its timeout= raises WorkspaceTimeoutError with the last 4 KiB of each stream. Path failures raise the builtin errors. Any other 4xx Daytona answers is a refusal (WorkspaceError, or WorkspaceUnavailableError on creation). Transport errors, request timeouts, 5xx and rate limits propagate unchanged, so durable engines retry them. Non-string env keys or values raise TypeError at construction, like E2B.
  • Client lifecycle like Sprites. Without client=, a run opens one AsyncDaytona and closes it when the run ends. A backend used outside a run (one per Temporal activity, or result.workspace after the run) closes its client after each operation, so nothing is left open.
  • asyncio only. Under Trio every operation raises UserError, like the other providers.

Daytona specifics

  • Lifetime. Daytona stops an idle container sandbox after auto_stop_interval minutes (15 by default, 0 disables it) and archives it after 7 days; a VM sandbox pauses after 60 idle minutes instead. Attaching starts a stopped, archived or paused sandbox again, and Daytona never deletes it. A backend still in use whose sandbox Daytona stopped while idle raises WorkspaceUnavailableError instead of restarting it mid-run, like E2B's paused sandbox; the next run that attaches starts it.
  • Commands run in their own Daytona process session, which is deleted on timeout or cancellation to stop the command. The command writes stdout and stderr to files in the sandbox, which are downloaded after it exits and then removed. Daytona's logs are only used to see the command finish: live probes showed the stored logs swap blocks between stdout and stderr above a few KB, and the combined log inserts stray bytes and newlines. run() returns when the command exits, even if it left a background child running.
  • Output limit. Output over 10 MiB per stream raises WorkspaceOutputLimitError with the first 64 KiB of each stream, checked by size before anything is downloaded, and again after, in case a background child kept writing. A timeout reports the last 4 KiB of each stream.
  • snapshot=, allow_internet_access= (named and defaulted like E2B and Smol; False is not an exfiltration boundary below Daytona's Tier 3, which still allows registries, GitHub and AI APIs), and client= for a caller-owned AsyncDaytona.
  • SDK pin daytona>=0.223.1,<1: 0.223.1 is the newest release outside the 7-day dependency cooldown. The <1 cap is needed because Daytona's PyPI project also has an old 1.0.1 and a 2.0.0rc1 from a different API line, which uv would otherwise pick.

Shared changes outside the Daytona package

  • run_to_completion moves from the Sprites backend into _workspace_provider.py so both providers use one copy. Sprites behaviour is unchanged. Daytona uses it for sandbox creation too, so a run cancelled while a sandbox is being created records the sandbox and then raises the cancellation, the same as Sprites.
  • _workspace.py: a run continued without a workspace now suggests DaytonaSandbox() for a Daytona ref, like it does for the other providers.
  • Docs, the harness skill references, the threat model, and the capability tables list Daytona next to the other sandboxes.
  • sandbox-live.yml / sandbox-live-nightly.yml run a daytona job with the DAYTONA_API_KEY secret.

Testing

  • Core's conformance suite (WorkspaceBackendSuite) runs over a fake Daytona SDK on every CI run; the Daytona package and its tests are at 100% line and branch coverage.
  • The live tier runs the conformance suite, cancellation and timeout behaviour, a Coder session, and every example on the docs page against real Daytona, then deletes what it created with the page's own cleanup snippet. Live run on the current head (58 passed; the one skip is the fresh_backend rule, which the Modal and E2B live classes skip too): https://github.com/pydantic/pydantic-ai/actions/runs/39493627675

Notes for review

  • The backend closes its own AsyncDaytona clients with the SDK's close(), then waits for the SDK's event-connection task, which close() cancels without awaiting. The live suite still occasionally ends with an unclosed transport warning after every test has passed. A CI probe traced the leaked socket to the SDK's own socket.io websocket, opened by the short-lived client the docs-example cleanup uses to delete a sandbox; it never reproduced off GitHub's runners (about 70 local runs), so the exact cause is a follow-up.
  • The live tests open a Daytona client per test. A module-scoped client kept its socket.io connection open through the synchronous docs tests, which block the event loop for minutes; at teardown socket.io was reconnecting, and closing the client orphaned that connection (2 of 2 local full runs leaked, 0 of 5 after the change).

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.